Skip to content

Commit b6f9800

Browse files
authored
[llvm-readobj] Support --string-table for COFF (#141552)
1 parent 24f432d commit b6f9800

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

llvm/include/llvm/Object/COFF.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,10 @@ class COFFObjectFile : public ObjectFile {
939939
return uintptr_t(0);
940940
}
941941

942+
StringRef getStringTable() const {
943+
return StringRef(StringTable, StringTableSize);
944+
}
945+
942946
uint16_t getMachine() const {
943947
if (COFFHeader) {
944948
if (CHPEMetadata) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# RUN: yaml2obj %s -o %t.obj
2+
# RUN: llvm-readobj --string-table %t.obj \
3+
# RUN: %p/Inputs/coff-load-config-x64.dll \
4+
# RUN: %p/Inputs/zero-string-table.obj.coff-i386 | FileCheck %s
5+
6+
# CHECK-LABEL: File: {{.*}}string-table.test.tmp.obj
7+
# CHECK: StringTable {
8+
# CHECK-NEXT: Length: 31
9+
# CHECK-NEXT: [ 4] .debug_str
10+
# CHECK-NEXT: [ f] _main_test_test
11+
# CHECK-NEXT: }
12+
13+
# CHECK-LABEL: File: {{.*}}coff-load-config-x64.dll
14+
# CHECK: StringTable {
15+
# CHECK-NEXT: Length: 0
16+
# CHECK-NEXT: }
17+
18+
# CHECK-LABEL: File: {{.*}}zero-string-table.obj.coff-i386
19+
# CHECK: StringTable {
20+
# CHECK-NEXT: Length: 4
21+
# CHECK-NEXT: }
22+
23+
--- !COFF
24+
header:
25+
Machine: IMAGE_FILE_MACHINE_AMD64
26+
sections:
27+
- Name: .text
28+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
29+
- Name: .debug_str
30+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
31+
symbols:
32+
- Name: _main_test_test
33+
Value: 0
34+
SectionNumber: 1
35+
SimpleType: IMAGE_SYM_TYPE_NULL
36+
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
37+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL

llvm/tools/llvm-readobj/COFFDumper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class COFFDumper : public ObjDumper {
108108
void printStackMap() const override;
109109
void printAddrsig() override;
110110
void printCGProfile() override;
111+
void printStringTable() override;
111112

112113
private:
113114
StringRef getSymbolName(uint32_t Index);
@@ -2219,6 +2220,17 @@ void COFFDumper::printCGProfile() {
22192220
}
22202221
}
22212222

2223+
void COFFDumper::printStringTable() {
2224+
DictScope DS(W, "StringTable");
2225+
StringRef StrTable = Obj->getStringTable();
2226+
uint32_t StrTabSize = StrTable.size();
2227+
W.printNumber("Length", StrTabSize);
2228+
// Print strings from the fifth byte, since the first four bytes contain the
2229+
// length (in bytes) of the string table (including the length field).
2230+
if (StrTabSize > 4)
2231+
printAsStringList(StrTable, 4);
2232+
}
2233+
22222234
StringRef COFFDumper::getSymbolName(uint32_t Index) {
22232235
Expected<COFFSymbolRef> Sym = Obj->getSymbol(Index);
22242236
if (!Sym)

llvm/tools/llvm-readobj/ObjDumper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ class ObjDumper {
157157
llvm::codeview::GlobalTypeTableBuilder &GlobalCVTypes,
158158
bool GHash) {}
159159

160-
// Only implemented for XCOFF.
160+
// Only implemented for XCOFF/COFF.
161161
virtual void printStringTable() {}
162+
163+
// Only implemented for XCOFF.
162164
virtual void printAuxiliaryHeader() {}
163165
virtual void printExceptionSection() {}
164166
virtual void printLoaderSection(bool PrintHeader, bool PrintSymbols,

llvm/tools/llvm-readobj/Opts.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defm sort_symbols : Eq<"sort-symbols", "Specify the keys to sort the symbols bef
4444
def stack_sizes : FF<"stack-sizes", "Display contents of all stack sizes sections. This option has no effect for GNU style output">;
4545
def stackmap : FF<"stackmap", "Display contents of stackmap section">;
4646
defm string_dump : Eq<"string-dump", "Display the specified section(s) as a list of strings">, MetaVarName<"<name or index>">;
47-
def string_table : FF<"string-table", "Display the string table (only for XCOFF now)">;
47+
def string_table : FF<"string-table", "Display the string table (only for XCOFF/COFF now)">;
4848
def symbols : FF<"symbols", "Display the symbol table. Also display the dynamic symbol table when using GNU output style for ELF">;
4949
def unwind : FF<"unwind", "Display unwind information">;
5050

0 commit comments

Comments
 (0)