Skip to content

Commit 018548d

Browse files
authored
[objcopy][coff] Place section name first in strtab (#145266)
The prioritized string table builder was introduced in 9cc9efc. This patch sets highest priority for the section name to place it at the start of string table to avoid the issue described in 4d2eda2.
1 parent 3df36a2 commit 018548d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

llvm/lib/ObjCopy/COFF/COFFWriter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ void COFFWriter::layoutSections() {
120120

121121
Expected<size_t> COFFWriter::finalizeStringTable() {
122122
for (const auto &S : Obj.getSections())
123-
if (S.Name.size() > COFF::NameSize)
124-
StrTabBuilder.add(S.Name);
123+
if (S.Name.size() > COFF::NameSize) {
124+
// Put the section name at the start of strtab to ensure its offset is
125+
// less than Max7DecimalOffset. Otherwise, lldb/gdb will not read it.
126+
StrTabBuilder.add(S.Name, /*Priority=*/UINT8_MAX);
127+
}
125128

126129
for (const auto &S : Obj.getSymbols())
127130
if (S.Name.size() > COFF::NameSize)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Show that section names appear before symbol names in the COFF strtab.
2+
## These names are only added to the strtab if they exceed 8 characters.
3+
4+
# RUN: yaml2obj %s -o %t
5+
# RUN: touch %t.sec
6+
# RUN: llvm-objcopy --add-section=.debug_str=%t.sec %t %t1
7+
# RUN: llvm-readobj --string-table %t1 | FileCheck %s
8+
9+
# CHECK: StringTable {
10+
# CHECK-NEXT: Length: 37
11+
# CHECK-NEXT: [ 4] .debug_str
12+
# CHECK-NEXT: [ f] symbol_zzz
13+
# CHECK-NEXT: [ 1a] symbol_aaa
14+
# CHECK-NEXT: }
15+
16+
--- !COFF
17+
header:
18+
Machine: IMAGE_FILE_MACHINE_AMD64
19+
sections:
20+
- Name: .text
21+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
22+
symbols:
23+
- Name: symbol_aaa
24+
Value: 0
25+
SectionNumber: 1
26+
SimpleType: IMAGE_SYM_TYPE_NULL
27+
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
28+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
29+
- Name: symbol_zzz
30+
Value: 0
31+
SectionNumber: 1
32+
SimpleType: IMAGE_SYM_TYPE_NULL
33+
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
34+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL

0 commit comments

Comments
 (0)