[rtdyld,c++11] Range'ify symbol table walking.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206769 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jim Grosbach 2014-04-21 18:10:31 +00:00
parent e6b88dc26a
commit 72eedb3c1d

View File

@ -53,7 +53,7 @@ void RuntimeDyldImpl::resolveRelocations() {
// symbol for the relocation is located. The SectionID in the relocation // symbol for the relocation is located. The SectionID in the relocation
// entry provides the section to which the relocation will be applied. // entry provides the section to which the relocation will be applied.
uint64_t Addr = Sections[i].LoadAddress; uint64_t Addr = Sections[i].LoadAddress;
DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t" DEBUG(dbgs() << "Resolving relocations Section " << i << "\t"
<< format("%p", (uint8_t *)Addr) << "\n"); << format("%p", (uint8_t *)Addr) << "\n");
resolveRelocationList(Relocations[i], Addr); resolveRelocationList(Relocations[i], Addr);
Relocations.erase(i); Relocations.erase(i);
@ -131,24 +131,23 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
// Parse symbols // Parse symbols
DEBUG(dbgs() << "Parse symbols:\n"); DEBUG(dbgs() << "Parse symbols:\n");
for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E; for (const SymbolRef &Sym : Obj->symbols()) {
++I) {
object::SymbolRef::Type SymType; object::SymbolRef::Type SymType;
StringRef Name; StringRef Name;
Check(I->getType(SymType)); Check(Sym.getType(SymType));
Check(I->getName(Name)); Check(Sym.getName(Name));
uint32_t Flags = I->getFlags(); uint32_t Flags = Sym.getFlags();
bool IsCommon = Flags & SymbolRef::SF_Common; bool IsCommon = Flags & SymbolRef::SF_Common;
if (IsCommon) { if (IsCommon) {
// Add the common symbols to a list. We'll allocate them all below. // Add the common symbols to a list. We'll allocate them all below.
uint32_t Align; uint32_t Align;
Check(I->getAlignment(Align)); Check(Sym.getAlignment(Align));
uint64_t Size = 0; uint64_t Size = 0;
Check(I->getSize(Size)); Check(Sym.getSize(Size));
CommonSize += Size + Align; CommonSize += Size + Align;
CommonSymbols[*I] = CommonSymbolInfo(Size, Align); CommonSymbols[Sym] = CommonSymbolInfo(Size, Align);
} else { } else {
if (SymType == object::SymbolRef::ST_Function || if (SymType == object::SymbolRef::ST_Function ||
SymType == object::SymbolRef::ST_Data || SymType == object::SymbolRef::ST_Data ||
@ -157,8 +156,8 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
StringRef SectionData; StringRef SectionData;
bool IsCode; bool IsCode;
section_iterator SI = Obj->end_sections(); section_iterator SI = Obj->end_sections();
Check(getOffset(*I, SectOffset)); Check(getOffset(Sym, SectOffset));
Check(I->getSection(SI)); Check(Sym.getSection(SI));
if (SI == Obj->end_sections()) if (SI == Obj->end_sections())
continue; continue;
Check(SI->getContents(SectionData)); Check(SI->getContents(SectionData));
@ -180,14 +179,13 @@ ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
// Parse and process relocations // Parse and process relocations
DEBUG(dbgs() << "Parse relocations:\n"); DEBUG(dbgs() << "Parse relocations:\n");
for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections(); for (const SectionRef &Section : Obj->sections()) {
SI != SE; ++SI) {
unsigned SectionID = 0; unsigned SectionID = 0;
StubMap Stubs; StubMap Stubs;
section_iterator RelocatedSection = SI->getRelocatedSection(); section_iterator RelocatedSection = Section.getRelocatedSection();
relocation_iterator I = SI->relocation_begin(); relocation_iterator I = Section.relocation_begin();
relocation_iterator E = SI->relocation_end(); relocation_iterator E = Section.relocation_end();
if (I == E && !ProcessAllSections) if (I == E && !ProcessAllSections)
continue; continue;
@ -216,9 +214,8 @@ static uint64_t
computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes, computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
uint64_t Alignment) { uint64_t Alignment) {
uint64_t TotalSize = 0; uint64_t TotalSize = 0;
for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) { for (uint64_t Size : SectionSizes) {
uint64_t AlignedSize = uint64_t AlignedSize = (Size + Alignment - 1) / Alignment * Alignment;
(SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
TotalSize += AlignedSize; TotalSize += AlignedSize;
} }
return TotalSize; return TotalSize;
@ -238,10 +235,7 @@ void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
// Collect sizes of all sections to be loaded; // Collect sizes of all sections to be loaded;
// also determine the max alignment of all sections // also determine the max alignment of all sections
for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections(); for (const SectionRef &Section : Obj.sections()) {
SI != SE; ++SI) {
const SectionRef &Section = *SI;
bool IsRequired; bool IsRequired;
Check(Section.isRequiredForExecution(IsRequired)); Check(Section.isRequiredForExecution(IsRequired));
@ -324,13 +318,12 @@ unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
// necessary section allocation size in loadObject by walking all the sections // necessary section allocation size in loadObject by walking all the sections
// once. // once.
unsigned StubBufSize = 0; unsigned StubBufSize = 0;
for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections(); for (const SectionRef &Section : Obj.sections()) {
SI != SE; ++SI) { section_iterator RelSecI = Section.getRelocatedSection();
section_iterator RelSecI = SI->getRelocatedSection();
if (!(RelSecI == Section)) if (!(RelSecI == Section))
continue; continue;
for (const RelocationRef &Reloc : SI->relocations()) { for (const RelocationRef &Reloc : Section.relocations()) {
(void)Reloc; (void)Reloc;
StubBufSize += StubSize; StubBufSize += StubSize;
} }
@ -369,12 +362,11 @@ void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
<< format("%p", Addr) << " DataSize: " << TotalSize << "\n"); << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
// Assign the address of each symbol // Assign the address of each symbol
for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), for (const auto &Entry : CommonSymbols) {
itEnd = CommonSymbols.end(); it != itEnd; ++it) { uint64_t Size = Entry.second.first;
uint64_t Size = it->second.first; uint64_t Align = Entry.second.second;
uint64_t Align = it->second.second;
StringRef Name; StringRef Name;
it->first.getName(Name); Entry.first.getName(Name);
if (Align) { if (Align) {
// This symbol has an alignment requirement. // This symbol has an alignment requirement.
uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align); uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
@ -383,7 +375,7 @@ void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
DEBUG(dbgs() << "Allocating common symbol " << Name << " address " DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
<< format("%p\n", Addr)); << format("%p\n", Addr));
} }
Obj.updateSymbolAddress(it->first, (uint64_t)Addr); Obj.updateSymbolAddress(Entry.first, (uint64_t)Addr);
SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
Offset += Size; Offset += Size;
Addr += Size; Addr += Size;