From 240fa2a2e33ddeba7b13f051157b05889ffa6e19 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Fri, 11 Jan 2019 13:47:37 +0000 Subject: [PATCH] [llvm-objcopy] [COFF] Fix writing object files without symbols/string table Previously, this was broken - by setting PointerToSymbolTable to zero but still actually writing the string table length, the object file header was corrupted. Differential Revision: https://reviews.llvm.org/D56584 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350926 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml | 11 +++++++++++ test/tools/llvm-objcopy/COFF/basic-copy.test | 6 ++++++ tools/llvm-objcopy/COFF/Writer.cpp | 9 ++++----- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml diff --git a/test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml b/test/tools/llvm-objcopy/COFF/Inputs/no-symbols.yaml new file mode 100644 index 00000000000..db8aeb63d5e --- /dev/null +++ b/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: +... diff --git a/test/tools/llvm-objcopy/COFF/basic-copy.test b/test/tools/llvm-objcopy/COFF/basic-copy.test index ddd12dcaf2f..ecdf430faf1 100644 --- a/test/tools/llvm-objcopy/COFF/basic-copy.test +++ b/test/tools/llvm-objcopy/COFF/basic-copy.test @@ -40,3 +40,9 @@ RUN: llvm-objcopy %t.x86_64.exe %t.x86_64-copy.exe 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 diff --git a/tools/llvm-objcopy/COFF/Writer.cpp b/tools/llvm-objcopy/COFF/Writer.cpp index d7a5224b5ef..213cb11b788 100644 --- a/tools/llvm-objcopy/COFF/Writer.cpp +++ b/tools/llvm-objcopy/COFF/Writer.cpp @@ -154,12 +154,11 @@ Error COFFWriter::finalize(bool IsBigObj) { 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; -- 2.11.4.GIT