Index: test/tools/llvm-objcopy/COFF/weak-external.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/COFF/weak-external.test @@ -0,0 +1,49 @@ +# RUN: yaml2obj %s > %t.in.o + +# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE + +# RUN: llvm-objcopy -N func %t.in.o %t.out.o +# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-POST + +# RUN: not llvm-objcopy -N .weak.foobar.file1 %t.in.o %t.err.o 2>&1 | FileCheck %s --check-prefix=ERROR + +# SYMBOLS: SYMBOL TABLE: +# SYMBOLS-PRE-NEXT: func +# SYMBOLS-NEXT: .weak.foobar.file1 +# SYMBOLS-NEXT: foobar +# SYMBOLS-PRE-NEXT: AUX indx 1 +# SYMBOLS-POST-NEXT: AUX indx 0 +# SYMBOLS-EMPTY: + +# ERROR: Symbol 'foobar' is missing its weak target + +--- !COFF +header: + Machine: IMAGE_FILE_MACHINE_AMD64 + Characteristics: [ ] +sections: + - Name: .text + Characteristics: [ ] +symbols: + - Name: func + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: .weak.foobar.file1 + Value: 1 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL + - Name: foobar + Value: 0 + SectionNumber: 0 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_FUNCTION + StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL + WeakExternal: + TagIndex: 1 + Characteristics: IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY +... Index: tools/llvm-objcopy/COFF/Object.h =================================================================== --- tools/llvm-objcopy/COFF/Object.h +++ tools/llvm-objcopy/COFF/Object.h @@ -11,6 +11,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" #include "llvm/BinaryFormat/COFF.h" @@ -47,6 +48,7 @@ std::vector AuxData; ssize_t TargetSectionId; ssize_t AssociativeComdatTargetSectionId = 0; + Optional WeakTargetSymbolId; size_t UniqueId; size_t RawIndex; bool Referenced; Index: tools/llvm-objcopy/COFF/Reader.h =================================================================== --- tools/llvm-objcopy/COFF/Reader.h +++ tools/llvm-objcopy/COFF/Reader.h @@ -28,7 +28,7 @@ Error readExecutableHeaders(Object &Obj) const; Error readSections(Object &Obj) const; Error readSymbols(Object &Obj, bool IsBigObj) const; - Error setRelocTargets(Object &Obj) const; + Error setSymbolTargets(Object &Obj) const; public: explicit COFFReader(const COFFObjectFile &O) : COFFObj(O) {} Index: tools/llvm-objcopy/COFF/Reader.cpp =================================================================== --- tools/llvm-objcopy/COFF/Reader.cpp +++ tools/llvm-objcopy/COFF/Reader.cpp @@ -121,12 +121,18 @@ // For section definitions, check if it is comdat associative, and if // it is, find the target section unique id. const coff_aux_section_definition *SD = SymRef.getSectionDefinition(); + const coff_aux_weak_external *WE = SymRef.getWeakExternal(); if (SD && SD->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE) { int32_t Index = SD->getNumber(IsBigObj); if (Index <= 0 || static_cast(Index - 1) >= Sections.size()) return make_error("Unexpected associative section index", object_error::parse_failed); Sym.AssociativeComdatTargetSectionId = Sections[Index - 1].UniqueId; + } else if (WE) { + // This is a raw symbol index for now, but store it in the Symbol + // until we've added them to the Object, which assigns the final + // unique ids. + Sym.WeakTargetSymbolId = WE->TagIndex; } I += 1 + SymRef.getNumberOfAuxSymbols(); } @@ -134,13 +140,27 @@ return Error::success(); } -Error COFFReader::setRelocTargets(Object &Obj) const { +Error COFFReader::setSymbolTargets(Object &Obj) const { std::vector RawSymbolTable; for (const Symbol &Sym : Obj.getSymbols()) { RawSymbolTable.push_back(&Sym); for (size_t I = 0; I < Sym.Sym.NumberOfAuxSymbols; I++) RawSymbolTable.push_back(nullptr); } + for (Symbol &Sym : Obj.getMutableSymbols()) { + // Convert WeakTargetSymbolId from the original raw symbol index to + // a proper unique id. + if (Sym.WeakTargetSymbolId) { + if (*Sym.WeakTargetSymbolId >= RawSymbolTable.size()) + return createStringError(object_error::parse_failed, + "Weak external reference out of range"); + const Symbol *Target = RawSymbolTable[*Sym.WeakTargetSymbolId]; + if (Target == nullptr) + return createStringError(object_error::parse_failed, + "Invalid SymbolTableIndex"); + Sym.WeakTargetSymbolId = Target->UniqueId; + } + } for (Section &Sec : Obj.getMutableSections()) { for (Relocation &R : Sec.Relocs) { if (R.Reloc.SymbolTableIndex >= RawSymbolTable.size()) @@ -184,7 +204,7 @@ return std::move(E); if (Error E = readSymbols(*Obj, IsBigObj)) return std::move(E); - if (Error E = setRelocTargets(*Obj)) + if (Error E = setSymbolTargets(*Obj)) return std::move(E); return std::move(Obj); Index: tools/llvm-objcopy/COFF/Writer.h =================================================================== --- tools/llvm-objcopy/COFF/Writer.h +++ tools/llvm-objcopy/COFF/Writer.h @@ -31,7 +31,7 @@ StringTableBuilder StrTabBuilder; Error finalizeRelocTargets(); - Error finalizeSectionNumbers(); + Error finalizeSymbolContents(); void layoutSections(); size_t finalizeStringTable(); template std::pair finalizeSymbolTable(); Index: tools/llvm-objcopy/COFF/Writer.cpp =================================================================== --- tools/llvm-objcopy/COFF/Writer.cpp +++ tools/llvm-objcopy/COFF/Writer.cpp @@ -39,7 +39,7 @@ return Error::success(); } -Error COFFWriter::finalizeSectionNumbers() { +Error COFFWriter::finalizeSymbolContents() { for (Symbol &Sym : Obj.getMutableSymbols()) { if (Sym.TargetSectionId <= 0) { // Undefined, or a special kind of symbol. These negative values @@ -75,6 +75,18 @@ SD->NumberHighPart = static_cast(SDSectionNumber >> 16); } } + // Check that we actually have got AuxData to match the weak symbol target + // 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()); + const Symbol *Target = Obj.findSymbol(*Sym.WeakTargetSymbolId); + if (Target == nullptr) + return createStringError(object_error::invalid_symbol_index, + "Symbol '%s' is missing its weak target", + Sym.Name.str().c_str()); + WE->TagIndex = Target->RawIndex; + } } return Error::success(); } @@ -137,7 +149,7 @@ Error COFFWriter::finalize(bool IsBigObj) { if (Error E = finalizeRelocTargets()) return E; - if (Error E = finalizeSectionNumbers()) + if (Error E = finalizeSymbolContents()) return E; size_t SizeOfHeaders = 0;