ELFObjectWriter: Run ComputeSymbolTable before recording relocations. This way we can use the information it has computed and don't have to recompute the same stuff over and over again.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111844 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2010-08-23 21:19:37 +00:00
parent 3efc0778c9
commit 0b6cbfe04c

View File

@ -228,29 +228,8 @@ namespace {
const MCFragment *Fragment, const MCFixup &Fixup, const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue); MCValue Target, uint64_t &FixedValue);
// XXX-PERF: this should be cached uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
uint64_t getNumOfLocalSymbols(const MCAssembler &Asm) { const MCSymbol *S);
std::vector<const MCSymbol*> Local;
uint64_t Index = 0;
for (MCAssembler::const_symbol_iterator it = Asm.symbol_begin(),
ie = Asm.symbol_end(); it != ie; ++it) {
const MCSymbol &Symbol = it->getSymbol();
// Ignore non-linker visible symbols.
if (!Asm.isSymbolLinkerVisible(Symbol))
continue;
if (it->isExternal() || Symbol.isUndefined())
continue;
Index++;
}
return Index;
}
uint64_t getSymbolIndexInSymbolTable(MCAssembler &Asm, const MCSymbol *S);
/// ComputeSymbolTable - Compute the symbol table data /// ComputeSymbolTable - Compute the symbol table data
/// ///
@ -271,7 +250,10 @@ namespace {
void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout); void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
void ExecutePostLayoutBinding(MCAssembler &Asm) {} void ExecutePostLayoutBinding(MCAssembler &Asm) {
// Compute symbol table information.
ComputeSymbolTable(Asm);
}
void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
uint64_t Address, uint64_t Offset, uint64_t Address, uint64_t Offset,
@ -512,10 +494,10 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
if (Base) { if (Base) {
if (MCFragment *F = SD.getFragment()) { if (MCFragment *F = SD.getFragment()) {
Index = F->getParent()->getOrdinal() + getNumOfLocalSymbols(Asm) + 1; Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
Value += Layout.getSymbolAddress(&SD); Value += Layout.getSymbolAddress(&SD);
} else } else
Index = getSymbolIndexInSymbolTable(const_cast<MCAssembler &>(Asm), Symbol); Index = getSymbolIndexInSymbolTable(Asm, Symbol);
if (Base != &SD) if (Base != &SD)
Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base); Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
Addend = Value; Addend = Value;
@ -525,7 +507,7 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
if (F) { if (F) {
// Index of the section in .symtab against this symbol // Index of the section in .symtab against this symbol
// is being relocated + 2 (empty section + abs. symbols). // is being relocated + 2 (empty section + abs. symbols).
Index = F->getParent()->getOrdinal() + getNumOfLocalSymbols(Asm) + 1; Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
MCSectionData *FSD = F->getParent(); MCSectionData *FSD = F->getParent();
// Offset of the symbol in the section // Offset of the symbol in the section
@ -597,62 +579,19 @@ void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
Relocations[Fragment->getParent()].push_back(ERE); Relocations[Fragment->getParent()].push_back(ERE);
} }
// XXX-PERF: this should be cached uint64_t
uint64_t ELFObjectWriterImpl::getSymbolIndexInSymbolTable(MCAssembler &Asm, ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
const MCSymbol *S) { const MCSymbol *S) {
std::vector<ELFSymbolData> Local; for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
std::vector<ELFSymbolData> External; if (&LocalSymbolData[i].SymbolData->getSymbol() == S)
std::vector<ELFSymbolData> Undefined;
for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
ie = Asm.symbol_end(); it != ie; ++it) {
const MCSymbol &Symbol = it->getSymbol();
// Ignore non-linker visible symbols.
if (!Asm.isSymbolLinkerVisible(Symbol))
continue;
if (it->isExternal() || Symbol.isUndefined())
continue;
ELFSymbolData MSD;
MSD.SymbolData = it;
Local.push_back(MSD);
}
for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
ie = Asm.symbol_end(); it != ie; ++it) {
const MCSymbol &Symbol = it->getSymbol();
// Ignore non-linker visible symbols.
if (!Asm.isSymbolLinkerVisible(Symbol))
continue;
if (!it->isExternal() && !Symbol.isUndefined())
continue;
ELFSymbolData MSD;
MSD.SymbolData = it;
if (Symbol.isUndefined())
Undefined.push_back(MSD);
else
External.push_back(MSD);
}
array_pod_sort(Local.begin(), Local.end());
array_pod_sort(External.begin(), External.end());
array_pod_sort(Undefined.begin(), Undefined.end());
for (unsigned i = 0, e = Local.size(); i != e; ++i)
if (&Local[i].SymbolData->getSymbol() == S)
return i + /* empty symbol */ 1; return i + /* empty symbol */ 1;
for (unsigned i = 0, e = External.size(); i != e; ++i) for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
if (&External[i].SymbolData->getSymbol() == S) if (&ExternalSymbolData[i].SymbolData->getSymbol() == S)
return i + Local.size() + Asm.size() + /* empty symbol */ 1; return i + LocalSymbolData.size() + Asm.size() + /* empty symbol */ 1;
for (unsigned i = 0, e = Undefined.size(); i != e; ++i) for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
if (&Undefined[i].SymbolData->getSymbol() == S) if (&UndefinedSymbolData[i].SymbolData->getSymbol() == S)
return i + Local.size() + External.size() + Asm.size() + /* empty symbol */ 1; return i + LocalSymbolData.size() + ExternalSymbolData.size() +
Asm.size() + /* empty symbol */ 1;
llvm_unreachable("Cannot find symbol which should exist!"); llvm_unreachable("Cannot find symbol which should exist!");
} }
@ -908,9 +847,6 @@ void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm, void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm,
const MCAsmLayout &Layout) { const MCAsmLayout &Layout) {
// Compute symbol table information.
ComputeSymbolTable(const_cast<MCAssembler&>(Asm));
CreateMetadataSections(const_cast<MCAssembler&>(Asm), CreateMetadataSections(const_cast<MCAssembler&>(Asm),
const_cast<MCAsmLayout&>(Layout)); const_cast<MCAsmLayout&>(Layout));