[PATCH] D127369: [Object][COFF] Fix section name parsing error when the name field is not null-padded

Pengxuan Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 8 18:27:40 PDT 2022


pzheng created this revision.
pzheng added reviewers: rnk, jhenderson, thieta, thakis, hans, mstorsjo.
Herald added subscribers: rupprecht, hiraditya.
Herald added a reviewer: MaskRay.
Herald added a project: All.
pzheng requested review of this revision.
Herald added subscribers: llvm-commits, StephenFan.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127369

Files:
  llvm/lib/Object/COFFObjectFile.cpp
  llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml
  llvm/test/tools/llvm-objdump/COFF/long-section-name.test


Index: llvm/test/tools/llvm-objdump/COFF/long-section-name.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objdump/COFF/long-section-name.test
@@ -0,0 +1,28 @@
+# 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 '/4'
+
+## This should print the LongSectionName section.
+# RUN: llvm-objdump --headers %t.obj | FileCheck %s
+
+# CHECK: LongSectionName
+
+import sys
+
+if len(sys.argv) < 3:
+  print("Use: python3 long-section-name.test <OBJECT_FILE> <TEMPLATE>")
+  exit(1)
+
+template = bytes(sys.argv[2], 'utf-8')
+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(template)
+  outp.write(data[:pos])
+  outp.write(replacement)
+  outp.write(data[pos + len(replacement):])
Index: llvm/test/tools/llvm-objdump/COFF/Inputs/long-section-name.yaml
===================================================================
--- /dev/null
+++ 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
+...
Index: llvm/lib/Object/COFFObjectFile.cpp
===================================================================
--- llvm/lib/Object/COFFObjectFile.cpp
+++ llvm/lib/Object/COFFObjectFile.cpp
@@ -1168,7 +1168,7 @@
         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");
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127369.435397.patch
Type: text/x-patch
Size: 2297 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220609/03ef140d/attachment.bin>


More information about the llvm-commits mailing list