Skip to content

Commit 0dad3f6

Browse files
author
Esme-Yi
committed
[llvm-readobj][XCOFF] Add support for printing the String Table.
Summary: The patch adds the StringTable dumping to llvm-readobj. Currently only XCOFF is supported. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D104613
1 parent 26d72bd commit 0dad3f6

File tree

8 files changed

+56
-19
lines changed

8 files changed

+56
-19
lines changed

llvm/docs/CommandGuide/llvm-readobj.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ file formats.
123123
Display the specified section(s) as a list of strings. ``section`` may be a
124124
section index or section name.
125125

126+
.. option:: --string-table
127+
128+
Display contents of the string table.
129+
126130
.. option:: --symbols, --syms, -s
127131

128132
Display the symbol table.

llvm/include/llvm/Object/XCOFFObjectFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ class XCOFFObjectFile : public ObjectFile {
424424
// This function returns string table entry.
425425
Expected<StringRef> getStringTableEntry(uint32_t Offset) const;
426426

427+
// This function returns the string table.
428+
StringRef getStringTable() const;
429+
427430
const XCOFF::SymbolAuxType *getSymbolAuxType(uintptr_t AuxEntryAddress) const;
428431

429432
static uintptr_t getAdvancedSymbolEntryAddress(uintptr_t CurrentAddress,

llvm/lib/Object/XCOFFObjectFile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ XCOFFObjectFile::getStringTableEntry(uint32_t Offset) const {
187187
object_error::parse_failed);
188188
}
189189

190+
StringRef XCOFFObjectFile::getStringTable() const {
191+
return StringRef(StringTable.Data, StringTable.Size);
192+
}
193+
190194
Expected<StringRef>
191195
XCOFFObjectFile::getCFileName(const XCOFFFileAuxEnt *CFileEntPtr) const {
192196
if (CFileEntPtr->NameInStrTbl.Magic != XCOFFSymbolRef::NAME_IN_STR_TBL_MAGIC)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## Test that the string table works well for long symbol names.
2-
## TODO: Dump the raw string table and check the contents.
32
# RUN: yaml2obj %s -o %t
4-
# RUN: llvm-readobj --symbols %t | FileCheck %s
3+
# RUN: llvm-readobj --symbols --string-table %t | FileCheck %s
4+
5+
## FIXME: The first item of StringTable should be `[ 4] .longname`.
56

67
# CHECK: AddressSize: 32bit
78
# CHECK-NEXT: Symbols [
@@ -24,6 +25,9 @@
2425
# CHECK-NEXT: NumberOfAuxEntries: 0
2526
# CHECK-NEXT: }
2627
# CHECK-NEXT: ]
28+
# CHECK-NEXT: StringTable {
29+
# CHECK-NEXT: [ 3] ..longname
30+
# CHECK-NEXT: }
2731

2832
--- !XCOFF
2933
FileHeader:

llvm/tools/llvm-readobj/ObjDumper.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ 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) {
56+
const uint8_t *StrContent = StringContent.bytes_begin();
57+
const uint8_t *CurrentWord = StrContent;
58+
const uint8_t *StrEnd = StringContent.bytes_end();
59+
60+
while (CurrentWord <= StrEnd) {
61+
size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
62+
StrEnd - CurrentWord);
63+
if (!WordSize) {
64+
CurrentWord++;
65+
continue;
66+
}
67+
W.startLine() << format("[%6tx] ", CurrentWord - StrContent);
68+
printAsPrintable(W.startLine(), CurrentWord, WordSize);
69+
W.startLine() << '\n';
70+
CurrentWord += WordSize + 1;
71+
}
72+
}
73+
5574
static std::vector<object::SectionRef>
5675
getSectionRefsByNameOrIndex(const object::ObjectFile &Obj,
5776
ArrayRef<std::string> Sections) {
@@ -109,23 +128,7 @@ void ObjDumper::printSectionsAsString(const object::ObjectFile &Obj,
109128

110129
StringRef SectionContent =
111130
unwrapOrError(Obj.getFileName(), Section.getContents());
112-
113-
const uint8_t *SecContent = SectionContent.bytes_begin();
114-
const uint8_t *CurrentWord = SecContent;
115-
const uint8_t *SecEnd = SectionContent.bytes_end();
116-
117-
while (CurrentWord <= SecEnd) {
118-
size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
119-
SecEnd - CurrentWord);
120-
if (!WordSize) {
121-
CurrentWord++;
122-
continue;
123-
}
124-
W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
125-
printAsPrintable(W.startLine(), CurrentWord, WordSize);
126-
W.startLine() << '\n';
127-
CurrentWord += WordSize + 1;
128-
}
131+
printAsStringList(SectionContent);
129132
}
130133
}
131134

llvm/tools/llvm-readobj/ObjDumper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,13 @@ class ObjDumper {
105105
virtual void printMachOIndirectSymbols() { }
106106
virtual void printMachOLinkerOptions() { }
107107

108+
// Currently only implemented for XCOFF.
109+
virtual void printStringTable() { }
110+
108111
virtual void printStackMap() const = 0;
109112

113+
void printAsStringList(StringRef StringContent);
114+
110115
void printSectionsAsString(const object::ObjectFile &Obj,
111116
ArrayRef<std::string> Sections);
112117
void printSectionsAsHex(const object::ObjectFile &Obj,

llvm/tools/llvm-readobj/XCOFFDumper.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class XCOFFDumper : public ObjDumper {
3434
void printUnwindInfo() override;
3535
void printStackMap() const override;
3636
void printNeededLibraries() override;
37+
void printStringTable() override;
3738

3839
private:
3940
template <typename T> void printSectionHeaders(ArrayRef<T> Sections);
@@ -456,6 +457,12 @@ void XCOFFDumper::printSymbols() {
456457
printSymbol(S);
457458
}
458459

460+
void XCOFFDumper::printStringTable() {
461+
DictScope DS(W, "StringTable");
462+
StringRef StrTable = Obj.getStringTable();
463+
printAsStringList(StrTable);
464+
}
465+
459466
void XCOFFDumper::printDynamicSymbols() {
460467
llvm_unreachable("Unimplemented functionality for XCOFFDumper");
461468
}

llvm/tools/llvm-readobj/llvm-readobj.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ namespace opts {
206206
cl::aliasopt(StringDump), cl::Prefix,
207207
cl::NotHidden);
208208

209+
// --string-table
210+
cl::opt<bool>
211+
StringTable("string-table",
212+
cl::desc("Display the string table (only for XCOFF now)"));
213+
209214
// --hex-dump, -x
210215
cl::list<std::string>
211216
HexDump("hex-dump", cl::value_desc("number|name"),
@@ -541,6 +546,8 @@ static void dumpObject(ObjectFile &Obj, ScopedPrinter &Writer,
541546
Dumper->printGnuHashTable();
542547
if (opts::VersionInfo)
543548
Dumper->printVersionInfo();
549+
if (opts::StringTable)
550+
Dumper->printStringTable();
544551
if (Obj.isELF()) {
545552
if (opts::DependentLibraries)
546553
Dumper->printDependentLibs();

0 commit comments

Comments
 (0)