Originally added in r139314.
Back then it didn't actually get the address, it got whatever value the
relocation used: address or offset.
The values in different object formats are:
* MachO: Always an offset.
* COFF: Always an address, but when talking about the virtual address of
sections it says: "for simplicity, compilers should set this to zero".
* ELF: An offset for .o files and and address for .so files. In the case of the
.so, the relocation in not linked to any section (sh_info is 0). We can't
really compute an offset.
Some API mappings would be:
* Use getAddress for everything. It would be quite cumbersome. To compute the
address elf has to follow sh_info, which can be corrupted and therefore the
method has to return an ErrorOr. The address of the section is also the same
for every relocation in a section, so we shouldn't have to check the error
and fetch the value for every relocation.
* Use a getValue and make it up to the user to know what it is getting.
* Use a getOffset and:
* Assert for dynamic ELF objects. That is a very peculiar case and it is
probably fair to ask any tool that wants to support it to use ELF.h. The
only tool we have that reads those (llvm-readobj) already does that. The
only other use case I can think of is a dynamic linker.
* Check that COFF .obj files have sections with zero virtual address spaces. If
it turns out that some assembler/compiler produces these, we can change
COFFObjectFile::getRelocationOffset to subtract it. Given COFF format,
this can be done without the need for ErrorOr.
The getRelocationAddress method was never implemented for COFF. It also
had exactly one use in a very peculiar case: a shortcut for adding the
section value to a pcrel reloc on MachO.
Given that, I don't expect that there is any use out there of the C API. If
that is not the case, let me know and I will add it back with the implementation
inlined and do a proper deprecation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241450
91177308-0d34-0410-b5e6-
96231b3b80d8
self.expired = False
- @CachedProperty
- def address(self):
- """The address of this relocation, in long bytes."""
- if self.expired:
- raise Exception('Relocation instance has expired.')
-
- return lib.LLVMGetRelocationAddress(self)
-
@CachedProperty
def offset(self):
"""The offset of this relocation, in long bytes."""
library.LLVMGetSymbolSize.argtypes = [Symbol]
library.LLVMGetSymbolSize.restype = c_uint64
- library.LLVMGetRelocationAddress.argtypes = [c_object_p]
- library.LLVMGetRelocationAddress.restype = c_uint64
-
library.LLVMGetRelocationOffset.argtypes = [c_object_p]
library.LLVMGetRelocationOffset.restype = c_uint64
uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI);
// RelocationRef accessors
-uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI);
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI);
LLVMSymbolIteratorRef LLVMGetRelocationSymbol(LLVMRelocationIteratorRef RI);
uint64_t LLVMGetRelocationType(LLVMRelocationIteratorRef RI);
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
void moveRelocationNext(DataRefImpl &Rel) const override;
- ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
uint64_t getRelocationType(DataRefImpl Rel) const override;
section_iterator getRelocatedSection(DataRefImpl Sec) const override;
void moveRelocationNext(DataRefImpl &Rel) const override;
- ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
uint64_t getRelocationType(DataRefImpl Rel) const override;
return symbol_iterator(SymbolRef(SymbolData, this));
}
-template <class ELFT>
-ErrorOr<uint64_t>
-ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel) const {
- uint64_t ROffset = getROffset(Rel);
- const Elf_Ehdr *Header = EF.getHeader();
-
- if (Header->e_type == ELF::ET_REL) {
- const Elf_Shdr *RelocationSec = getRelSection(Rel);
- ErrorOr<const Elf_Shdr *> RelocatedSec =
- EF.getSection(RelocationSec->sh_info);
- if (std::error_code EC = RelocatedSec.getError())
- return EC;
- return ROffset + (*RelocatedSec)->sh_addr;
- }
- return ROffset;
-}
-
template <class ELFT>
uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
assert(EF.getHeader()->e_type == ELF::ET_REL &&
relocation_iterator section_rel_end(DataRefImpl Sec) const override;
void moveRelocationNext(DataRefImpl &Rel) const override;
- ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
uint64_t getRelocationOffset(DataRefImpl Rel) const override;
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
section_iterator getRelocationSection(DataRefImpl Rel) const;
// MachO specific.
std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
+ section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
+
// TODO: Would be useful to have an iterator based version
// of the load command interface too.
void moveNext();
- ErrorOr<uint64_t> getAddress() const;
uint64_t getOffset() const;
symbol_iterator getSymbol() const;
uint64_t getType() const;
// Same as above for RelocationRef.
friend class RelocationRef;
virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
- virtual ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const = 0;
virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
return OwningObject->moveRelocationNext(RelocationPimpl);
}
-inline ErrorOr<uint64_t> RelocationRef::getAddress() const {
- return OwningObject->getRelocationAddress(RelocationPimpl);
-}
-
inline uint64_t RelocationRef::getOffset() const {
return OwningObject->getRelocationOffset(RelocationPimpl);
}
}
void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
- const ObjectFile &BaseTObj,
const relocation_iterator &RI,
unsigned OffsetToNextPC) {
- const MachOObjectFile &Obj =
- static_cast<const MachOObjectFile &>(BaseTObj);
- MachO::any_relocation_info RelInfo =
- Obj.getRelocation(RI->getRawDataRefImpl());
-
- bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
- if (IsPCRel) {
- ErrorOr<uint64_t> RelocAddr = RI->getAddress();
- Value.Offset += *RelocAddr + OffsetToNextPC;
- }
+ auto &O = *cast<MachOObjectFile>(RI->getObject());
+ section_iterator SecI = O.getRelocationRelocatedSection(RI);
+ Value.Offset += RI->getOffset() + OffsetToNextPC + SecI->getAddress();
}
void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
/// Make the RelocationValueRef addend PC-relative.
void makeValueAddendPCRel(RelocationValueRef &Value,
- const ObjectFile &BaseTObj,
const relocation_iterator &RI,
unsigned OffsetToNextPC);
bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
if (!IsExtern && RE.IsPCRel)
- makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size);
+ makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
RE.Addend = Value.Offset;
getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
if (RE.IsPCRel)
- makeValueAddendPCRel(Value, Obj, RelI, 8);
+ makeValueAddendPCRel(Value, RelI, 8);
if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
processBranchRelocation(RE, Value, Stubs);
// Value.Addend += RelocAddr + 4;
// }
if (RE.IsPCRel)
- makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size);
+ makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
RE.Addend = Value.Offset;
bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
if (!IsExtern && RE.IsPCRel)
- makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size);
+ makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
if (RE.RelType == MachO::X86_64_RELOC_GOT ||
RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
}
-ErrorOr<uint64_t> COFFObjectFile::getRelocationAddress(DataRefImpl Rel) const {
- report_fatal_error("getRelocationAddress not implemented in COFFObjectFile");
-}
-
uint64_t COFFObjectFile::getRelocationOffset(DataRefImpl Rel) const {
const coff_relocation *R = toRel(Rel);
return R->VirtualAddress;
++Rel.d.b;
}
-ErrorOr<uint64_t> MachOObjectFile::getRelocationAddress(DataRefImpl Rel) const {
- uint64_t Offset = getRelocationOffset(Rel);
-
- DataRefImpl Sec;
- Sec.d.a = Rel.d.a;
- uint64_t SecAddress = getSectionAddress(Sec);
- return SecAddress + Offset;
-}
-
uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
assert(getHeader().filetype == MachO::MH_OBJECT &&
"Only implemented for MH_OBJECT");
return std::error_code();
}
+section_iterator
+MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
+ DataRefImpl Sec;
+ Sec.d.a = Rel->getRawDataRefImpl().d.a;
+ return section_iterator(SectionRef(Sec, this));
+}
+
basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
return getSymbolByIndex(0);
}
}
// RelocationRef accessors
-uint64_t LLVMGetRelocationAddress(LLVMRelocationIteratorRef RI) {
- ErrorOr<uint64_t> Ret = (*unwrap(RI))->getAddress();
- if (std::error_code EC = Ret.getError())
- report_fatal_error(EC.message());
- return *Ret;
-}
-
uint64_t LLVMGetRelocationOffset(LLVMRelocationIteratorRef RI) {
return (*unwrap(RI))->getOffset();
}