Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -16,6 +16,7 @@ #include "Memory.h" #include "OutputSections.h" #include "Relocations.h" +#include "SymbolTable.h" #include "SyntheticSections.h" #include "Target.h" #include "Thunks.h" @@ -229,6 +230,25 @@ return Total; } +template static uint32_t getSymbolIndex(SymbolBody &Body) { + if (Body.IsLocal) { + size_t Ndx = 0; + auto *F = cast>(Body.File); + for (elf::ObjectFile *File : Symtab::X->getObjectFiles()) { + if (F == File) { + ArrayRef Arr = File->getLocalSymbols(); + return Ndx + llvm::find(Arr, &Body) - Arr.begin() + 1; + } + Ndx += File->getLocalSymbols().size(); + } + } + + assert(!Body.IsLocal); + ArrayRef Symbols = Symtab::X->getSymbols(); + return llvm::find(Symbols, Body.symbol()) - Symbols.begin() + + In::SymTab->NumLocals + 1; +} + // This is used for -r. We can't use memcpy to copy relocations because we need // to update symbol table offset and section index for each relocation. So we // copy relocations one by one. @@ -247,7 +267,7 @@ if (Config->Rela) P->r_addend = getAddend(Rel); P->r_offset = RelocatedSection->getOffset(Rel.r_offset); - P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL); + P->setSymbolAndType(getSymbolIndex(Body), Type, Config->Mips64EL); } } Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -1065,12 +1065,8 @@ this->OutSec->Info = this->Info = NumLocals + 1; this->OutSec->Entsize = this->Entsize; - if (Config->Relocatable) { - size_t I = NumLocals; - for (const SymbolTableEntry &S : Symbols) - S.Symbol->DynsymIndex = ++I; + if (Config->Relocatable) return; - } if (!StrTabSec.isDynamic()) { std::stable_sort( Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -466,8 +466,6 @@ if (!shouldKeepInSymtab(Sec, B->getName(), *B)) continue; ++In::SymTab->NumLocals; - if (Config->Relocatable) - B->DynsymIndex = In::SymTab->NumLocals; F->KeptLocalSyms.push_back(std::make_pair( DR, In::SymTab->StrTabSec.addString(B->getName()))); }