[llvm] 064db24 - [Object][COFF] Fix section name parsing error when the name field is not null-padded
Pengxuan Zheng via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 9 12:58:41 PDT 2022
Author: Pengxuan Zheng
Date: 2022-06-09T12:58:28-07:00
New Revision: 064db243113ccac50ab3f8695f8f83a9e71efd3f
URL: https://github.com/llvm/llvm-project/commit/064db243113ccac50ab3f8695f8f83a9e71efd3f
DIFF: https://github.com/llvm/llvm-project/commit/064db243113ccac50ab3f8695f8f83a9e71efd3f.diff
LOG: [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
Added:
llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml
llvm/test/tools/llvm-objdump/COFF/long-section-name.test
Modified:
llvm/lib/Object/COFFObjectFile.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Object/COFFObjectFile.cpp b/llvm/lib/Object/COFFObjectFile.cpp
index fe8fab23e54a4..e5013d7730bb0 100644
--- a/llvm/lib/Object/COFFObjectFile.cpp
+++ b/llvm/lib/Object/COFFObjectFile.cpp
@@ -1168,7 +1168,7 @@ COFFObjectFile::getSectionName(const coff_section *Sec) const {
return createStringError(object_error::parse_failed,
"invalid section name");
} else {
- if (Name.substr(1).getAsInteger(10, Offset))
+ if (Name.substr(1).consumeInteger(10, Offset))
return createStringError(object_error::parse_failed,
"invalid section name");
}
diff --git a/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml b/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml
new file mode 100644
index 0000000000000..bd9716cc86b96
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml
@@ -0,0 +1,15 @@
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_ARM64
+ Characteristics: [ ]
+sections:
+ - Name: LongSectionName
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+symbols:
+ - Name: LongSectionName
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+...
diff --git a/llvm/test/tools/llvm-objdump/COFF/long-section-name.test b/llvm/test/tools/llvm-objdump/COFF/long-section-name.test
new file mode 100644
index 0000000000000..4867ab7f04a5b
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/COFF/long-section-name.test
@@ -0,0 +1,30 @@
+# RUN: yaml2obj %S/Inputs/long-section-name.yaml -o %t.obj
+
+## Replace the section name field of the object file with /4\0abcde emulating
+## a section name field not fully null-padded at the end.
+# RUN: %python %s %t.obj
+
+## This should print the LongSectionName section.
+# RUN: llvm-objdump --headers %t.obj | FileCheck %s
+
+# CHECK: LongSectionName
+
+import sys
+
+if len(sys.argv) < 2:
+ print("Use: python3 long-section-name.test <OBJECT_FILE>")
+ exit(1)
+
+pattern = b'/4'
+replacement = b'/4\0abcde'
+
+data = None
+with open(sys.argv[1], "rb") as inp:
+ data = inp.read()
+with open(sys.argv[1], "wb") as outp:
+ pos = data.find(pattern)
+ if pos == -1:
+ sys.exit("Error: Pattern /4 not found in " + sys.argv[1])
+ outp.write(data[:pos])
+ outp.write(replacement)
+ outp.write(data[pos + len(replacement):])
More information about the llvm-commits
mailing list