Index: test/tools/llvm-objcopy/COFF/bigobj.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/COFF/bigobj.test @@ -0,0 +1,34 @@ +RUN: %python %p/../Inputs/ungzip.py %p/Inputs/bigobj.o.gz > %t.in.o + +RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-BIG,SYMBOLS-ORIG + +# Remove a section, making the section count fit into a small object. + +RUN: llvm-objcopy -R '.text$4' %t.in.o %t.small.o +RUN: llvm-objdump -t %t.small.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-SMALL,SYMBOLS-REMOVED-SMALL + +# Add a .gnu_debuglink section, forcing the object back to big format. + +RUN: llvm-objcopy --add-gnu-debuglink=%t.in.o %t.small.o %t.big.o + llvm-objdump -t %t.big.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-BIG,SYMBOLS-REMOVED-BIG + +SYMBOLS: SYMBOL TABLE: +SYMBOLS-NEXT: [ 0]{{.*}} (nx 1) {{.*}} .text +SYMBOLS-NEXT: AUX scnlen +SYMBOLS-SMALL-NEXT: [ 2]{{.*}} (nx 2) {{.*}} .file +SYMBOLS-BIG-NEXT: [ 2]{{.*}} (nx 1) {{.*}} .file +SYMBOLS-NEXT: AUX abcdefghijklmnopqrs +SYMBOLS-SMALL-NEXT: [ 5]{{.*}} (nx 0) {{.*}} foo +SYMBOLS-BIG-NEXT: [ 4]{{.*}} (nx 0) {{.*}} foo + +# Check that the section numbers outside of signed 16 bit int range +# are represented properly. + +# After removing one section, the section numbers decrease. Since the .file +# symbol expanded to having two aux symbol entries, the raw symbol index +# stays the same for REMOVED-SMALL, but decreases by one when going back to +# big format. + +SYMBOLS-ORIG: [65283](sec 65280){{.*}} symbol65280 +SYMBOLS-REMOVED-SMALL: [65283](sec 65279){{.*}} symbol65280 +SYMBOLS-REMOVED-BIG: [65282](sec 65279){{.*}} symbol65280 Index: test/tools/llvm-objcopy/ELF/Inputs/ungzip.py =================================================================== --- test/tools/llvm-objcopy/ELF/Inputs/ungzip.py +++ /dev/null @@ -1,13 +0,0 @@ -import gzip -import sys - -with gzip.open(sys.argv[1], 'rb') as f: - writer = getattr(sys.stdout, 'buffer', None) - if writer is None: - writer = sys.stdout - if sys.platform == "win32": - import os, msvcrt - msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY) - - writer.write(f.read()) - sys.stdout.flush() Index: test/tools/llvm-objcopy/ELF/auto-remove-shndx.test =================================================================== --- test/tools/llvm-objcopy/ELF/auto-remove-shndx.test +++ test/tools/llvm-objcopy/ELF/auto-remove-shndx.test @@ -1,4 +1,4 @@ -# RUN: %python %p/Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t +# RUN: %python %p/../Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t # RUN: llvm-objcopy -R .text -R s0 -R s1 -R s2 -R s3 -R s4 -R s5 -R s6 %t %t2 # RUN: llvm-readobj --sections %t2 | FileCheck --check-prefix=SECS %s Index: test/tools/llvm-objcopy/ELF/many-sections.test =================================================================== --- test/tools/llvm-objcopy/ELF/many-sections.test +++ test/tools/llvm-objcopy/ELF/many-sections.test @@ -1,4 +1,4 @@ -RUN: %python %p/Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t +RUN: %python %p/../Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t RUN: llvm-objcopy %t %t2 RUN: llvm-readobj --file-headers %t2 | FileCheck --check-prefix=EHDR %s RUN: llvm-readobj --sections %t2 | FileCheck --check-prefix=SECS %s Index: test/tools/llvm-objcopy/ELF/remove-shndx.test =================================================================== --- test/tools/llvm-objcopy/ELF/remove-shndx.test +++ test/tools/llvm-objcopy/ELF/remove-shndx.test @@ -1,6 +1,6 @@ # This test checks to see that a .symtab_shndx section is added to any binary # that needs it, even if the original was removed. -RUN: %python %p/Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t +RUN: %python %p/../Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t RUN: llvm-objcopy -R .symtab_shndx %t %t2 RUN: llvm-readobj --sections %t2 | FileCheck %s Index: test/tools/llvm-objcopy/ELF/strict-no-add.test =================================================================== --- test/tools/llvm-objcopy/ELF/strict-no-add.test +++ test/tools/llvm-objcopy/ELF/strict-no-add.test @@ -1,7 +1,7 @@ # This test makes sure that sections added at the end that don't have symbols # defined in them don't trigger the creation of a large index table. -RUN: %python %p/Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t.0 +RUN: %python %p/../Inputs/ungzip.py %p/Inputs/many-sections.o.gz > %t.0 RUN: cat %p/Inputs/alloc-symtab.o > %t RUN: llvm-objcopy -R .text -R s0 -R s1 -R s2 -R s3 -R s4 -R s5 -R s6 %t.0 %t2 RUN: llvm-objcopy --add-section=.s0=%t --add-section=.s1=%t --add-section=.s2=%t %t2 %t2 Index: test/tools/llvm-objcopy/Inputs/ungzip.py =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/Inputs/ungzip.py @@ -0,0 +1,13 @@ +import gzip +import sys + +with gzip.open(sys.argv[1], 'rb') as f: + writer = getattr(sys.stdout, 'buffer', None) + if writer is None: + writer = sys.stdout + if sys.platform == "win32": + import os, msvcrt + msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY) + + writer.write(f.read()) + sys.stdout.flush() Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -37,7 +37,7 @@ return 0; const Section &Last = Obj.getSections().back(); return alignTo(Last.Header.VirtualAddress + Last.Header.VirtualSize, - Obj.PeHeader.SectionAlignment); + Obj.IsPE ? Obj.PeHeader.SectionAlignment : 1); } static uint32_t getCRC32(StringRef Data) { @@ -75,7 +75,7 @@ Sec.Header.VirtualSize = Sec.getContents().size(); Sec.Header.VirtualAddress = StartRVA; Sec.Header.SizeOfRawData = - alignTo(Sec.Header.VirtualSize, Obj.PeHeader.FileAlignment); + alignTo(Sec.Header.VirtualSize, Obj.IsPE ? Obj.PeHeader.FileAlignment : 1); // Sec.Header.PointerToRawData is filled in by the writer. Sec.Header.PointerToRelocations = 0; Sec.Header.PointerToLinenumbers = 0; Index: tools/llvm-objcopy/COFF/Object.h =================================================================== --- tools/llvm-objcopy/COFF/Object.h +++ tools/llvm-objcopy/COFF/Object.h @@ -66,10 +66,24 @@ std::vector OwnedContents; }; +struct AuxSymbol { + AuxSymbol(ArrayRef In) { + assert(In.size() == sizeof(Opaque)); + std::copy(In.begin(), In.end(), Opaque); + } + + ArrayRef getRef() const { + return ArrayRef(Opaque, sizeof(Opaque)); + } + + uint8_t Opaque[sizeof(object::coff_symbol16)]; +}; + struct Symbol { object::coff_symbol32 Sym; StringRef Name; - std::vector AuxData; + std::vector AuxData; + StringRef AuxFile; ssize_t TargetSectionId; ssize_t AssociativeComdatTargetSectionId = 0; Optional WeakTargetSymbolId; @@ -132,7 +146,7 @@ ssize_t NextSectionUniqueId = 1; // Allow a UniqueId 0 to mean undefined. - // Update SymbolMap and RawIndex in each Symbol. + // Update SymbolMap. void updateSymbols(); // Update SectionMap and Index in each Section. Index: tools/llvm-objcopy/COFF/Object.cpp =================================================================== --- tools/llvm-objcopy/COFF/Object.cpp +++ tools/llvm-objcopy/COFF/Object.cpp @@ -26,12 +26,8 @@ void Object::updateSymbols() { SymbolMap = DenseMap(Symbols.size()); - size_t RawSymIndex = 0; - for (Symbol &Sym : Symbols) { + for (Symbol &Sym : Symbols) SymbolMap[Sym.UniqueId] = &Sym; - Sym.RawIndex = RawSymIndex; - RawSymIndex += 1 + Sym.Sym.NumberOfAuxSymbols; - } } const Symbol *Object::findSymbol(size_t UniqueId) const { Index: tools/llvm-objcopy/COFF/Reader.cpp =================================================================== --- tools/llvm-objcopy/COFF/Reader.cpp +++ tools/llvm-objcopy/COFF/Reader.cpp @@ -107,9 +107,17 @@ *reinterpret_cast(SymRef.getRawPtr())); if (auto EC = COFFObj.getSymbolName(SymRef, Sym.Name)) return errorCodeToError(EC); - Sym.AuxData = COFFObj.getSymbolAuxData(SymRef); - assert((Sym.AuxData.size() % - (IsBigObj ? sizeof(coff_symbol32) : sizeof(coff_symbol16))) == 0); + ArrayRef AuxData = COFFObj.getSymbolAuxData(SymRef); + size_t SymSize = IsBigObj ? sizeof(coff_symbol32) : sizeof(coff_symbol16); + assert(AuxData.size() == SymSize * SymRef.getNumberOfAuxSymbols()); + if (SymRef.isFileRecord()) { + Sym.AuxFile = StringRef(reinterpret_cast(AuxData.data()), + AuxData.size()) + .rtrim('\0'); + } else { + for (size_t I = 0; I < SymRef.getNumberOfAuxSymbols(); I++) + Sym.AuxData.push_back(AuxData.slice(I * SymSize, sizeof(AuxSymbol))); + } // Find the unique id of the section if (SymRef.getSectionNumber() <= 0) // Special symbol (undefined/absolute/debug) Index: tools/llvm-objcopy/COFF/Writer.h =================================================================== --- tools/llvm-objcopy/COFF/Writer.h +++ tools/llvm-objcopy/COFF/Writer.h @@ -30,11 +30,11 @@ size_t SizeOfInitializedData; StringTableBuilder StrTabBuilder; + template std::pair finalizeSymbolTable(); Error finalizeRelocTargets(); Error finalizeSymbolContents(); void layoutSections(); size_t finalizeStringTable(); - template std::pair finalizeSymbolTable(); Error finalize(bool IsBigObj); Index: tools/llvm-objcopy/COFF/Writer.cpp =================================================================== --- tools/llvm-objcopy/COFF/Writer.cpp +++ tools/llvm-objcopy/COFF/Writer.cpp @@ -55,7 +55,8 @@ if (Sym.Sym.NumberOfAuxSymbols == 1 && Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC) { coff_aux_section_definition *SD = - reinterpret_cast(Sym.AuxData.data()); + reinterpret_cast( + Sym.AuxData[0].Opaque); uint32_t SDSectionNumber; if (Sym.AssociativeComdatTargetSectionId == 0) { // Not a comdat associative section; just set the Number field to @@ -79,7 +80,7 @@ // we want to set. Only >= 1 would be required, but only == 1 makes sense. if (Sym.WeakTargetSymbolId && Sym.Sym.NumberOfAuxSymbols == 1) { coff_aux_weak_external *WE = - reinterpret_cast(Sym.AuxData.data()); + reinterpret_cast(Sym.AuxData[0].Opaque); const Symbol *Target = Obj.findSymbol(*Sym.WeakTargetSymbolId); if (Target == nullptr) return createStringError(object_error::invalid_symbol_index, @@ -141,13 +142,23 @@ template std::pair COFFWriter::finalizeSymbolTable() { - size_t SymTabSize = Obj.getSymbols().size() * sizeof(SymbolTy); - for (const auto &S : Obj.getSymbols()) - SymTabSize += S.AuxData.size(); - return std::make_pair(SymTabSize, sizeof(SymbolTy)); + size_t RawSymIndex = 0; + for (auto &S : Obj.getMutableSymbols()) { + if (!S.AuxFile.empty()) + S.Sym.NumberOfAuxSymbols = + alignTo(S.AuxFile.size(), sizeof(SymbolTy)) / sizeof(SymbolTy); + S.RawIndex = RawSymIndex; + RawSymIndex += 1 + S.Sym.NumberOfAuxSymbols; + } + return std::make_pair(RawSymIndex * sizeof(SymbolTy), sizeof(SymbolTy)); } Error COFFWriter::finalize(bool IsBigObj) { + size_t SymTabSize, SymbolSize; + std::tie(SymTabSize, SymbolSize) = IsBigObj + ? finalizeSymbolTable() + : finalizeSymbolTable(); + if (Error E = finalizeRelocTargets()) return E; if (Error E = finalizeSymbolContents()) @@ -199,10 +210,6 @@ } size_t StrTabSize = finalizeStringTable(); - size_t SymTabSize, SymbolSize; - std::tie(SymTabSize, SymbolSize) = IsBigObj - ? finalizeSymbolTable() - : finalizeSymbolTable(); size_t PointerToSymbolTable = FileSize; // StrTabSize <= 4 is the size of an empty string table, only consisting @@ -312,8 +319,18 @@ copySymbol(*reinterpret_cast(Ptr), S.Sym); Ptr += sizeof(SymbolTy); - std::copy(S.AuxData.begin(), S.AuxData.end(), Ptr); - Ptr += S.AuxData.size(); + if (!S.AuxFile.empty()) { + std::copy(S.AuxFile.begin(), S.AuxFile.end(), Ptr); + // This assumes that unwritten parts of the memory mapped file + // are initialized to zero. + Ptr += S.Sym.NumberOfAuxSymbols * sizeof(SymbolTy); + } else { + for (const AuxSymbol &AuxSym : S.AuxData) { + ArrayRef Ref = AuxSym.getRef(); + std::copy(Ref.begin(), Ref.end(), Ptr); + Ptr += sizeof(SymbolTy); + } + } } if (StrTabBuilder.getSize() > 4 || !Obj.IsPE) { // Always write a string table in object files, even an empty one.