Skip to content

Commit 6939689

Browse files
author
Esme-Yi
committed
[llvm-readobj][XCOFF] Fix the error dumping for the first
item of StringTable. Summary: For the string table in XCOFF, the first 4 bytes contains the length of the string table, so we should print the string entries from fifth bytes. This patch also adds tests for llvm-readobj dumping the string table. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D105522
1 parent 18c6ed2 commit 6939689

File tree

7 files changed

+64
-9
lines changed

7 files changed

+64
-9
lines changed

llvm/lib/Object/XCOFFObjectFile.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
188188
}
189189

190190
StringRef XCOFFObjectFile::getStringTable() const {
191-
return StringRef(StringTable.Data, StringTable.Size);
191+
// If the size is less than or equal to 4, then the string table contains no
192+
// string data.
193+
return StringRef(StringTable.Data,
194+
StringTable.Size <= 4 ? 0 : StringTable.Size);
192195
}
193196

194197
Expected<StringRef>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Test that the string table is dumped correctly.
2+
3+
## The string table contains more than one entry.
4+
# RUN: yaml2obj --docnum=1 %s -o %t1
5+
# RUN: llvm-readobj --string-table %t1 | FileCheck %s --check-prefix=BASIC
6+
7+
# BASIC: StringTable {
8+
# BASIC-NEXT: [ 4] name2
9+
# BASIC-NEXT: [ a] name1
10+
# BASIC-NEXT: }
11+
12+
--- !XCOFF
13+
FileHeader:
14+
MagicNumber: 0x01F7
15+
Symbols:
16+
- Name: name1
17+
- Name: name2
18+
19+
## The string table just contains a single-byte sized string entry.
20+
# RUN: yaml2obj --docnum=2 %s -o %t2
21+
# RUN: llvm-readobj --string-table %t2 | FileCheck %s --check-prefix=SINGLE-BYTE
22+
23+
# SINGLE-BYTE: StringTable {
24+
# SINGLE-BYTE-NEXT: [ 4] n
25+
# SINGLE-BYTE-NEXT: }
26+
27+
--- !XCOFF
28+
FileHeader:
29+
MagicNumber: 0x01F7
30+
Symbols:
31+
- Name: n
32+
33+
## There is no string table.
34+
# RUN: yaml2obj --docnum=3 %s -o %t3
35+
# RUN: llvm-readobj --string-table %t3 | FileCheck %s --check-prefix=NO-STRTBL
36+
37+
# NO-STRTBL: StringTable {
38+
# NO-STRTBL-NEXT: }
39+
40+
--- !XCOFF
41+
FileHeader:
42+
MagicNumber: 0x01F7

llvm/test/tools/yaml2obj/XCOFF/basic-doc64.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RUN: yaml2obj %s -o %t
2-
# RUN: llvm-readobj --headers --symbols %t | \
2+
# RUN: llvm-readobj --headers --symbols --string-table %t | \
33
# RUN: FileCheck %s --check-prefix=CHECK64
44

55
--- !XCOFF
@@ -134,3 +134,9 @@ Symbols:
134134
# CHECK64-NEXT: NumberOfAuxEntries: 0
135135
# CHECK64-NEXT: }
136136
# CHECK64-NEXT: ]
137+
# CHECK64-NEXT: StringTable {
138+
# CHECK64-NEXT: [ 4] .text
139+
# CHECK64-NEXT: [ a] .abs
140+
# CHECK64-NEXT: [ f] .undef
141+
# CHECK64-NEXT: [ 16] .file
142+
# CHECK64-NEXT: }

llvm/test/tools/yaml2obj/XCOFF/long-symbol-name.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# RUN: yaml2obj %s -o %t
33
# RUN: llvm-readobj --symbols --string-table %t | FileCheck %s
44

5-
## FIXME: The first item of StringTable should be `[ 4] .longname`.
6-
75
# CHECK: AddressSize: 32bit
86
# CHECK-NEXT: Symbols [
97
# CHECK-NEXT: Symbol {
@@ -26,7 +24,7 @@
2624
# CHECK-NEXT: }
2725
# CHECK-NEXT: ]
2826
# CHECK-NEXT: StringTable {
29-
# CHECK-NEXT: [ 3] ..longname
27+
# CHECK-NEXT: [ 4] .longname
3028
# CHECK-NEXT: }
3129

3230
--- !XCOFF

llvm/tools/llvm-readobj/ObjDumper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
5252
W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
5353
}
5454

55-
void ObjDumper::printAsStringList(StringRef StringContent) {
55+
void ObjDumper::printAsStringList(StringRef StringContent,
56+
size_t StringDataOffset) {
5657
const uint8_t *StrContent = StringContent.bytes_begin();
57-
const uint8_t *CurrentWord = StrContent;
58+
// Some formats contain additional metadata at the start which should not be
59+
// interpreted as strings. Skip these bytes, but account for them in the
60+
// string offsets.
61+
const uint8_t *CurrentWord = StrContent + StringDataOffset;
5862
const uint8_t *StrEnd = StringContent.bytes_end();
5963

6064
while (CurrentWord <= StrEnd) {

llvm/tools/llvm-readobj/ObjDumper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class ObjDumper {
110110

111111
virtual void printStackMap() const = 0;
112112

113-
void printAsStringList(StringRef StringContent);
113+
void printAsStringList(StringRef StringContent, size_t StringDataOffset = 0);
114114

115115
void printSectionsAsString(const object::ObjectFile &Obj,
116116
ArrayRef<std::string> Sections);

llvm/tools/llvm-readobj/XCOFFDumper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ void XCOFFDumper::printSymbols() {
460460
void XCOFFDumper::printStringTable() {
461461
DictScope DS(W, "StringTable");
462462
StringRef StrTable = Obj.getStringTable();
463-
printAsStringList(StrTable);
463+
// Print strings from the fifth byte, since the first four bytes contain the
464+
// length (in bytes) of the string table (including the length field).
465+
printAsStringList(StrTable, 4);
464466
}
465467

466468
void XCOFFDumper::printDynamicSymbols() {

0 commit comments

Comments
 (0)