diff --git a/lld/lib/ReaderWriter/ELF/ReaderELF.cpp b/lld/lib/ReaderWriter/ELF/ReaderELF.cpp index 29d25cad2193..0f2f31faca6c 100644 --- a/lld/lib/ReaderWriter/ELF/ReaderELF.cpp +++ b/lld/lib/ReaderWriter/ELF/ReaderELF.cpp @@ -215,6 +215,22 @@ public: : (*(si + 1))->st_value - (*si)->st_value; } + + // Don't allocate content to a weak symbol, as they may be merged away. + // Create an anonymous atom to hold the data. + ELFDefinedAtom *anonAtom = nullptr; + if ((*si)->getBinding() == llvm::ELF::STB_WEAK && contentSize != 0) { + // Create a new non-weak ELF symbol. + auto sym = new (_readerStorage.Allocate()) Elf_Sym; + *sym = **si; + sym->setBinding(llvm::ELF::STB_GLOBAL); + anonAtom = createDefinedAtomAndAssignRelocations( + "", sectionName, sym, i.first, + ArrayRef( + (uint8_t *)sectionContents.data() + (*si)->st_value, contentSize)); + contentSize = 0; + } + ArrayRef symbolData = ArrayRef( (uint8_t *)sectionContents.data() + (*si)->st_value, contentSize); @@ -223,6 +239,8 @@ public: _definedAtoms._atoms.push_back(newAtom); _symbolToAtomMapping.insert(std::make_pair((*si), newAtom)); + if (anonAtom) + _definedAtoms._atoms.push_back(anonAtom); } } diff --git a/lld/test/elf/check.objtxt b/lld/test/elf/check.objtxt index 6ad87fb12532..21e219b3efbb 100644 --- a/lld/test/elf/check.objtxt +++ b/lld/test/elf/check.objtxt @@ -13,9 +13,9 @@ ELF-i386: 00, E8, FC, FF, FF, FF, C9, C3 ] ELF-i386: - name: weak_func ELF-i386: scope: global -ELF-i386: content: [ 55, 89, E5, 83, EC, 18, C7, 04, 24, 0E, 00, 00, -ELF-i386: 00, E8, FC, FF, FF, FF, C9, C3 ] ELF-i386: merge: as-weak +ELF-i386: content: [ 55, 89, E5, 83, EC, 18, C7, 04, 24, 0E, 00, 00, +ELF-i386: 00, E8, FC, FF, FF, FF, C9, C3 ] ELF-i386: - name: hidden_func ELF-i386: scope: hidden @@ -65,9 +65,9 @@ ELF-hexagon: 00, C0, 00, 5A, 1E, C0, 1E, 96 ] ELF-hexagon: - name: weak_func ELF-hexagon: scope: global -ELF-hexagon: content: [ 00, C0, 9D, A0, 00, 40, 00, 00, 00, C0, 00, 78, -ELF-hexagon: 00, C0, 00, 5A, 1E, C0, 1E, 96 ] ELF-hexagon: merge: as-weak +ELF-hexagon: content: [ 00, C0, 9D, A0, 00, 40, 00, 00, 00, C0, 00, 78, +ELF-hexagon: 00, C0, 00, 5A, 1E, C0, 1E, 96 ] ELF-hexagon: - name: hidden_func ELF-hexagon: scope: hidden