From c201e6eaf165c83f0092c43b371e509fa8eaf4cc Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 1 May 2012 10:41:12 +0000 Subject: [PATCH] RuntimeDyld cleanup: - Improved parameter names for clarity - Added comments - emitCommonSymbols should return void because its return value is not being used anywhere - Attempt to reduce the usage of the RelocationValueRef type. Restricts it for a single goal and may serve as a step for eventual removal. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155908 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../RuntimeDyld/RuntimeDyld.cpp | 63 ++++++++----------- .../RuntimeDyld/RuntimeDyldELF.cpp | 30 ++++++--- .../RuntimeDyld/RuntimeDyldImpl.h | 23 ++++--- .../RuntimeDyld/RuntimeDyldMachO.cpp | 26 +++++--- 4 files changed, 77 insertions(+), 65 deletions(-) diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp index 7266f691909..dfa2e7f3132 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -182,10 +182,10 @@ bool RuntimeDyldImpl::loadObject(const MemoryBuffer *InputBuffer) { return false; } -unsigned RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, - const CommonSymbolMap &Map, - uint64_t TotalSize, - SymbolTableMap &Symbols) { +void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, + const CommonSymbolMap &CommonSymbols, + uint64_t TotalSize, + SymbolTableMap &SymbolTable) { // Allocate memory for the section unsigned SectionID = Sections.size(); uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void*), @@ -202,18 +202,16 @@ unsigned RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj, << "\n"); // Assign the address of each symbol - for (CommonSymbolMap::const_iterator it = Map.begin(), itEnd = Map.end(); - it != itEnd; it++) { - uint64_t Size = it->second; + for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(), + itEnd = CommonSymbols.end(); it != itEnd; it++) { StringRef Name; it->first.getName(Name); Obj.updateSymbolAddress(it->first, (uint64_t)Addr); - Symbols[Name.data()] = SymbolLoc(SectionID, Offset); + SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset); + uint64_t Size = it->second; Offset += Size; Addr += Size; } - - return SectionID; } unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj, @@ -312,36 +310,25 @@ unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj, return SectionID; } -void RuntimeDyldImpl::addRelocation(const RelocationValueRef &Value, - unsigned SectionID, uintptr_t Offset, - uint32_t RelType) { - DEBUG(dbgs() << "addRelocation SymNamePtr: " << format("%p", Value.SymbolName) - << " SID: " << Value.SectionID - << " Addend: " << format("%p", Value.Addend) - << " Offset: " << format("%p", Offset) - << " RelType: " << format("%x", RelType) - << "\n"); +void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, + unsigned SectionID) { + Relocations[SectionID].push_back(RE); +} - if (Value.SymbolName == 0) { - Relocations[Value.SectionID].push_back(RelocationEntry( - SectionID, - Offset, - RelType, - Value.Addend)); +void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, + StringRef SymbolName) { + // Relocation by symbol. If the symbol is found in the global symbol table, + // create an appropriate section relocation. Otherwise, add it to + // ExternalSymbolRelocations. + SymbolTableMap::const_iterator Loc = + GlobalSymbolTable.find(SymbolName); + if (Loc == GlobalSymbolTable.end()) { + ExternalSymbolRelocations[SymbolName].push_back(RE); } else { - // Relocation by symbol. If the symbol is found in the global symbol table, - // create an appropriate section relocation. Otherwise, add it to - // ExternalSymbolRelocations. - RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); - - SymbolTableMap::const_iterator Loc = - GlobalSymbolTable.find(Value.SymbolName); - if (Loc == GlobalSymbolTable.end()) { - ExternalSymbolRelocations[Value.SymbolName].push_back(RE); - } else { - RE.Addend += Loc->second.second; - Relocations[Loc->second.first].push_back(RE); - } + // Copy the RE since we want to modify its addend. + RelocationEntry RECopy = RE; + RECopy.Addend += Loc->second.second; + Relocations[Loc->second.first].push_back(RECopy); } } diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index e010785c446..39aed344558 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -339,21 +339,23 @@ void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL); intptr_t Addend = (intptr_t)Rel.AdditionalInfo; - RelocationValueRef Value; - StringRef TargetName; const SymbolRef &Symbol = Rel.Symbol; + + // Obtain the symbol name which is referenced in the relocation + StringRef TargetName; Symbol.getName(TargetName); DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend << " TargetName: " << TargetName << "\n"); - // First look the symbol in object file symbols. + RelocationValueRef Value; + // First search for the symbol in the local symbol table SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); if (lsi != Symbols.end()) { Value.SectionID = lsi->second.first; Value.Addend = lsi->second.second; } else { - // Second look the symbol in global symbol table. + // Search for the symbol in the global symbol table SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data()); if (gsi != GlobalSymbolTable.end()) { @@ -367,7 +369,7 @@ void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously // and can be changed by another developers. Maybe best way is add // a new symbol type ST_Section to SymbolRef and use it. - section_iterator si = Obj.end_sections(); + section_iterator si(Obj.end_sections()); Symbol.getSection(si); if (si == Obj.end_sections()) llvm_unreachable("Symbol section not found, bad object file format!"); @@ -411,14 +413,24 @@ void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, Stubs[Value] = Section.StubOffset; uint8_t *StubTargetAddr = createStubFunction(Section.Address + Section.StubOffset); - addRelocation(Value, Rel.SectionID, - StubTargetAddr - Section.Address, ELF::R_ARM_ABS32); + RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address, + ELF::R_ARM_ABS32, Value.Addend); + if (Value.SymbolName) + addRelocationForSymbol(RE, Value.SymbolName); + else + addRelocationForSection(RE, Value.SectionID); + resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + Section.StubOffset, RelType, 0); Section.StubOffset += getMaxStubSize(); } - } else - addRelocation(Value, Rel.SectionID, Rel.Offset, RelType); + } else { + RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); + if (Value.SymbolName) + addRelocationForSymbol(RE, Value.SymbolName); + else + addRelocationForSection(RE, Value.SectionID); + } } bool RuntimeDyldELF::isCompatibleFormat(const MemoryBuffer *InputBuffer) const { diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h index f3ae7dcd98f..bd6d287a91e 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -179,12 +179,13 @@ protected: return (uint8_t*)Sections[SectionID].Address; } - /// \brief Emits a section containing common symbols. - /// \return SectionID. - unsigned emitCommonSymbols(ObjectImage &Obj, - const CommonSymbolMap &Map, - uint64_t TotalSize, - SymbolTableMap &Symbols); + /// \brief Given the common symbols discovered in the object file, emit a + /// new section for them and update the symbol mappings in the object and + /// symbol table. + void emitCommonSymbols(ObjectImage &Obj, + const CommonSymbolMap &CommonSymbols, + uint64_t TotalSize, + SymbolTableMap &SymbolTable); /// \brief Emits section data from the object file to the MemoryManager. /// \param IsCode if it's true then allocateCodeSection() will be @@ -204,10 +205,12 @@ protected: bool IsCode, ObjSectionToIDMap &LocalSections); - /// \brief If Value.SymbolName is NULL then store relocation to the - /// Relocations, else store it in the SymbolRelocations. - void addRelocation(const RelocationValueRef &Value, unsigned SectionID, - uintptr_t Offset, uint32_t RelType); + // \brief Add a relocation entry that uses the given section. + void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID); + + // \brief Add a relocation entry that uses the given symbol. This symbol may + // be found in the global symbol table, or it may be external. + void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName); /// \brief Emits long jump instruction to Addr. /// \return Pointer to the memory area for emitting target address. diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index 9bf043754bc..0e3a9d4af50 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -215,16 +215,17 @@ void RuntimeDyldMachO::processRelocationRef(const ObjRelocationInfo &Rel, bool isExtern = (RelType >> 27) & 1; if (isExtern) { + // Obtain the symbol name which is referenced in the relocation StringRef TargetName; const SymbolRef &Symbol = Rel.Symbol; Symbol.getName(TargetName); - // First look the symbol in object file symbols. + // First search for the symbol in the local symbol table SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); if (lsi != Symbols.end()) { Value.SectionID = lsi->second.first; Value.Addend = lsi->second.second; } else { - // Second look the symbol in global symbol table. + // Search for the symbol in the global symbol table SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data()); if (gsi != GlobalSymbolTable.end()) { Value.SectionID = gsi->second.first; @@ -247,8 +248,8 @@ void RuntimeDyldMachO::processRelocationRef(const ObjRelocationInfo &Rel, Value.SectionID = findOrEmitSection(Obj, *si, true, ObjSectionToID); Value.Addend = *(const intptr_t *)Target; if (Value.Addend) { - // The MachO addend is offset from the current section, we need set it - // as offset from destination section + // The MachO addend is an offset from the current section. We need it + // to be an offset from the destination section Value.Addend += Section.ObjAddress - Sections[Value.SectionID].ObjAddress; } } @@ -267,15 +268,24 @@ void RuntimeDyldMachO::processRelocationRef(const ObjRelocationInfo &Rel, Stubs[Value] = Section.StubOffset; uint8_t *StubTargetAddr = createStubFunction(Section.Address + Section.StubOffset); - addRelocation(Value, Rel.SectionID, StubTargetAddr - Section.Address, - macho::RIT_Vanilla); + RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address, + macho::RIT_Vanilla, Value.Addend); + if (Value.SymbolName) + addRelocationForSymbol(RE, Value.SymbolName); + else + addRelocationForSection(RE, Value.SectionID); resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + Section.StubOffset, RelType, 0); Section.StubOffset += getMaxStubSize(); } - } else - addRelocation(Value, Rel.SectionID, Rel.Offset, RelType); + } else { + RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); + if (Value.SymbolName) + addRelocationForSymbol(RE, Value.SymbolName); + else + addRelocationForSection(RE, Value.SectionID); + } } -- 2.34.1