Skip to content

Commit 064db24

Browse files
committed
[Object][COFF] Fix section name parsing error when the name field is not null-padded
Some object files produced by Mirosoft tools contain sections whose name field is not fully null-padded at the end. Microsoft's dumpbin is able to print the section name correctly, but this causes parsing errors with LLVM tools. So far, this issue only seems to happen when the section name is longer than 8 bytes. In this case, the section name field contains a slash (/) followed by the offset into the string table, but the name field is not fully null-padded at the end. Reviewed By: mstorsjo Differential Revision: https://reviews.llvm.org/D127369
1 parent 0759902 commit 064db24

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

llvm/lib/Object/COFFObjectFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ COFFObjectFile::getSectionName(const coff_section *Sec) const {
11681168
return createStringError(object_error::parse_failed,
11691169
"invalid section name");
11701170
} else {
1171-
if (Name.substr(1).getAsInteger(10, Offset))
1171+
if (Name.substr(1).consumeInteger(10, Offset))
11721172
return createStringError(object_error::parse_failed,
11731173
"invalid section name");
11741174
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--- !COFF
2+
header:
3+
Machine: IMAGE_FILE_MACHINE_ARM64
4+
Characteristics: [ ]
5+
sections:
6+
- Name: LongSectionName
7+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
8+
symbols:
9+
- Name: LongSectionName
10+
Value: 0
11+
SectionNumber: 1
12+
SimpleType: IMAGE_SYM_TYPE_NULL
13+
ComplexType: IMAGE_SYM_DTYPE_NULL
14+
StorageClass: IMAGE_SYM_CLASS_STATIC
15+
...
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# RUN: yaml2obj %S/Inputs/long-section-name.yaml -o %t.obj
2+
3+
## Replace the section name field of the object file with /4\0abcde emulating
4+
## a section name field not fully null-padded at the end.
5+
# RUN: %python %s %t.obj
6+
7+
## This should print the LongSectionName section.
8+
# RUN: llvm-objdump --headers %t.obj | FileCheck %s
9+
10+
# CHECK: LongSectionName
11+
12+
import sys
13+
14+
if len(sys.argv) < 2:
15+
print("Use: python3 long-section-name.test <OBJECT_FILE>")
16+
exit(1)
17+
18+
pattern = b'/4'
19+
replacement = b'/4\0abcde'
20+
21+
data = None
22+
with open(sys.argv[1], "rb") as inp:
23+
data = inp.read()
24+
with open(sys.argv[1], "wb") as outp:
25+
pos = data.find(pattern)
26+
if pos == -1:
27+
sys.exit("Error: Pattern /4 not found in " + sys.argv[1])
28+
outp.write(data[:pos])
29+
outp.write(replacement)
30+
outp.write(data[pos + len(replacement):])

0 commit comments

Comments
 (0)