[PATCH] D56584: [llvm-objcopy] [COFF] Fix writing object files without symbols/string table

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 11 01:49:09 PST 2019


mstorsjo created this revision.
mstorsjo added reviewers: jhenderson, jakehehrlich, alexshap, rnk, rupprecht.

Previously, this was broken - by setting PointerToSymbolTable to zero but still actually writing the string table length, the object file header was corrupted.

Splitting out this independent fix from D56481 <https://reviews.llvm.org/D56481>.


Repository:
  rL LLVM

https://reviews.llvm.org/D56584

Files:
  test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml
  test/tools/llvm-objcopy/COFF/basic-copy.test
  tools/llvm-objcopy/COFF/Writer.cpp


Index: tools/llvm-objcopy/COFF/Writer.cpp
===================================================================
--- tools/llvm-objcopy/COFF/Writer.cpp
+++ tools/llvm-objcopy/COFF/Writer.cpp
@@ -157,12 +157,11 @@
   size_t PointerToSymbolTable = FileSize;
   // StrTabSize <= 4 is the size of an empty string table, only consisting
   // of the length field.
-  if (SymTabSize == 0 && StrTabSize <= 4) {
-    // Don't point to the symbol table if empty.
+  if (SymTabSize == 0 && StrTabSize <= 4 && Obj.IsPE) {
+    // For executables, don't point to the symbol table and skip writing
+    // the length field, if both the symbol and string tables are empty.
     PointerToSymbolTable = 0;
-    // For executables, skip the length field of an empty string table.
-    if (Obj.IsPE)
-      StrTabSize = 0;
+    StrTabSize = 0;
   }
 
   size_t NumRawSymbols = SymTabSize / SymbolSize;
Index: test/tools/llvm-objcopy/COFF/basic-copy.test
===================================================================
--- test/tools/llvm-objcopy/COFF/basic-copy.test
+++ test/tools/llvm-objcopy/COFF/basic-copy.test
@@ -40,3 +40,9 @@
 RUN: obj2yaml %t.x86_64.exe > %t.x86_64.exe.yaml
 RUN: obj2yaml %t.x86_64-copy.exe > %t.x86_64-copy.exe.yaml
 RUN: cmp %t.x86_64.exe.yaml %t.x86_64-copy.exe.yaml
+
+RUN: yaml2obj %p/Inputs/no-symbols.yaml > %t.no-symbols.o
+RUN: llvm-objcopy %t.no-symbols.o %t.no-symbols-copy.o
+RUN: obj2yaml %t.no-symbols.o > %t.no-symbols.o.yaml
+RUN: obj2yaml %t.no-symbols-copy.o > %t.no-symbols-copy.o.yaml
+RUN: cmp %t.no-symbols.o.yaml %t.no-symbols-copy.o.yaml
Index: test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml
===================================================================
--- /dev/null
+++ test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml
@@ -0,0 +1,11 @@
+--- !COFF
+header:          
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:        
+  - Name:            .text
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     E800000000C3C3C3
+symbols:         
+...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56584.181222.patch
Type: text/x-patch
Size: 2047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190111/e25fb4c5/attachment.bin>


More information about the llvm-commits mailing list