Skip to content

Commit

Permalink
Fixed ObjectFile functions:
Browse files Browse the repository at this point in the history
- getSymbolOffset() renamed as getSymbolFileOffset()
- getSymbolFileOffset(), getSymbolAddress(), getRelocationAddress() returns same result for ELFObjectFile, MachOObjectFile and COFFObjectFile.
- added getRelocationOffset()
- fixed MachOObjectFile::getSymbolSize()
- fixed MachOObjectFile::getSymbolSection()
- fixed MachOObjectFile::getSymbolOffset() for symbols without section data.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@145408 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Danil Malyshev committed Nov 29, 2011
1 parent f68b214 commit b0436a7
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 48 deletions.
3 changes: 2 additions & 1 deletion include/llvm-c/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ void LLVMMoveToNextRelocation(LLVMRelocationIteratorRef RI);
// SymbolRef accessors
const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI);
uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI);
uint64_t LLVMGetSymbolOffset(LLVMSymbolIteratorRef SI);
uint64_t LLVMGetSymbolFileOffset(LLVMSymbolIteratorRef SI);
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);
// NOTE: Caller takes ownership of returned string of the two
Expand Down
4 changes: 3 additions & 1 deletion include/llvm/Object/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class COFFObjectFile : public ObjectFile {
protected:
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
Expand Down Expand Up @@ -135,6 +135,8 @@ class COFFObjectFile : public ObjectFile {
RelocationRef &Res) const;
virtual error_code getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const;
virtual error_code getRelocationType(DataRefImpl Rel,
Expand Down
4 changes: 3 additions & 1 deletion include/llvm/Object/MachO.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MachOObjectFile : public ObjectFile {
protected:
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
Expand Down Expand Up @@ -79,6 +79,8 @@ class MachOObjectFile : public ObjectFile {
RelocationRef &Res) const;
virtual error_code getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const;
virtual error_code getRelocationType(DataRefImpl Rel,
Expand Down
15 changes: 11 additions & 4 deletions include/llvm/Object/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class RelocationRef {
error_code getNext(RelocationRef &Result) const;

error_code getAddress(uint64_t &Result) const;
error_code getOffset(uint64_t &Result) const;
error_code getSymbol(SymbolRef &Result) const;
error_code getType(uint64_t &Result) const;

Expand Down Expand Up @@ -195,7 +196,7 @@ class SymbolRef {

error_code getName(StringRef &Result) const;
error_code getAddress(uint64_t &Result) const;
error_code getOffset(uint64_t &Result) const;
error_code getFileOffset(uint64_t &Result) const;
error_code getSize(uint64_t &Result) const;
error_code getType(SymbolRef::Type &Result) const;

Expand Down Expand Up @@ -254,7 +255,7 @@ class ObjectFile : public Binary {
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const =0;
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const =0;
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
virtual error_code getSymbolType(DataRefImpl Symb,
SymbolRef::Type &Res) const = 0;
Expand Down Expand Up @@ -289,6 +290,8 @@ class ObjectFile : public Binary {
RelocationRef &Res) const = 0;
virtual error_code getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const =0;
virtual error_code getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const =0;
virtual error_code getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const = 0;
virtual error_code getRelocationType(DataRefImpl Rel,
Expand Down Expand Up @@ -363,8 +366,8 @@ inline error_code SymbolRef::getAddress(uint64_t &Result) const {
return OwningObject->getSymbolAddress(SymbolPimpl, Result);
}

inline error_code SymbolRef::getOffset(uint64_t &Result) const {
return OwningObject->getSymbolOffset(SymbolPimpl, Result);
inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
return OwningObject->getSymbolFileOffset(SymbolPimpl, Result);
}

inline error_code SymbolRef::getSize(uint64_t &Result) const {
Expand Down Expand Up @@ -486,6 +489,10 @@ inline error_code RelocationRef::getAddress(uint64_t &Result) const {
return OwningObject->getRelocationAddress(RelocationPimpl, Result);
}

inline error_code RelocationRef::getOffset(uint64_t &Result) const {
return OwningObject->getRelocationOffset(RelocationPimpl, Result);
}

inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
}
Expand Down
15 changes: 9 additions & 6 deletions lib/Object/COFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ error_code COFFObjectFile::getSymbolNext(DataRefImpl Symb,
return getSymbolName(symb, Result);
}

error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb,
error_code COFFObjectFile::getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Result) const {
const coff_symbol *symb = toSymb(Symb);
const coff_section *Section = NULL;
Expand All @@ -113,7 +113,7 @@ error_code COFFObjectFile::getSymbolOffset(DataRefImpl Symb,
if (Type == 'U' || Type == 'w')
Result = UnknownAddressOrSize;
else if (Section)
Result = Section->VirtualAddress + symb->Value;
Result = Section->PointerToRawData + symb->Value;
else
Result = symb->Value;
return object_error::success;
Expand All @@ -131,11 +131,9 @@ error_code COFFObjectFile::getSymbolAddress(DataRefImpl Symb,
if (Type == 'U' || Type == 'w')
Result = UnknownAddressOrSize;
else if (Section)
Result = reinterpret_cast<uintptr_t>(base() +
Section->PointerToRawData +
symb->Value);
Result = Section->VirtualAddress + symb->Value;
else
Result = reinterpret_cast<uintptr_t>(base() + symb->Value);
Result = symb->Value;
return object_error::success;
}

Expand Down Expand Up @@ -624,6 +622,11 @@ error_code COFFObjectFile::getRelocationAddress(DataRefImpl Rel,
Res = toRel(Rel)->VirtualAddress;
return object_error::success;
}
error_code COFFObjectFile::getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const {
Res = toRel(Rel)->VirtualAddress;
return object_error::success;
}
error_code COFFObjectFile::getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const {
const coff_relocation* R = toRel(Rel);
Expand Down
49 changes: 36 additions & 13 deletions lib/Object/ELFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class ELFObjectFile : public ObjectFile {
protected:
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
Expand Down Expand Up @@ -355,6 +355,8 @@ class ELFObjectFile : public ObjectFile {
RelocationRef &Res) const;
virtual error_code getRelocationAddress(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const;
virtual error_code getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const;
virtual error_code getRelocationType(DataRefImpl Rel,
Expand Down Expand Up @@ -462,7 +464,7 @@ ELFObjectFile<target_endianness, is64Bits>

template<support::endianness target_endianness, bool is64Bits>
error_code ELFObjectFile<target_endianness, is64Bits>
::getSymbolOffset(DataRefImpl Symb,
::getSymbolFileOffset(DataRefImpl Symb,
uint64_t &Result) const {
validateSymbol(Symb);
const Elf_Sym *symb = getSymbol(Symb);
Expand All @@ -486,7 +488,8 @@ error_code ELFObjectFile<target_endianness, is64Bits>
case ELF::STT_FUNC:
case ELF::STT_OBJECT:
case ELF::STT_NOTYPE:
Result = symb->st_value;
Result = symb->st_value +
(Section ? Section->sh_offset - Section->sh_addr : 0);
return object_error::success;
default:
Result = UnknownAddressOrSize;
Expand All @@ -502,28 +505,25 @@ error_code ELFObjectFile<target_endianness, is64Bits>
const Elf_Sym *symb = getSymbol(Symb);
const Elf_Shdr *Section;
switch (getSymbolTableIndex(symb)) {
case ELF::SHN_COMMON: // Fall through.
case ELF::SHN_COMMON:
// Undefined symbols have no address yet.
case ELF::SHN_UNDEF:
Result = UnknownAddressOrSize;
return object_error::success;
case ELF::SHN_ABS:
Result = reinterpret_cast<uintptr_t>(base()+symb->st_value);
Result = symb->st_value;
return object_error::success;
default: Section = getSection(symb);
}
const uint8_t* addr = base();
if (Section)
addr += Section->sh_offset;

switch (symb->getType()) {
case ELF::STT_SECTION:
Result = reinterpret_cast<uintptr_t>(addr);
Result = Section ? Section->sh_addr : UnknownAddressOrSize;
return object_error::success;
case ELF::STT_FUNC: // Fall through.
case ELF::STT_OBJECT: // Fall through.
case ELF::STT_FUNC:
case ELF::STT_OBJECT:
case ELF::STT_NOTYPE:
addr += symb->st_value;
Result = reinterpret_cast<uintptr_t>(addr);
Result = symb->st_value;
return object_error::success;
default:
Result = UnknownAddressOrSize;
Expand Down Expand Up @@ -920,6 +920,29 @@ error_code ELFObjectFile<target_endianness, is64Bits>
return object_error::success;
}

template<support::endianness target_endianness, bool is64Bits>
error_code ELFObjectFile<target_endianness, is64Bits>
::getRelocationOffset(DataRefImpl Rel,
uint64_t &Result) const {
uint64_t offset;
const Elf_Shdr *sec = getSection(Rel.w.b);
switch (sec->sh_type) {
default :
report_fatal_error("Invalid section type in Rel!");
case ELF::SHT_REL : {
offset = getRel(Rel)->r_offset;
break;
}
case ELF::SHT_RELA : {
offset = getRela(Rel)->r_offset;
break;
}
}

Result = offset - sec->sh_addr;
return object_error::success;
}

template<support::endianness target_endianness, bool is64Bits>
error_code ELFObjectFile<target_endianness, is64Bits>
::getRelocationType(DataRefImpl Rel,
Expand Down
97 changes: 86 additions & 11 deletions lib/Object/MachOObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,27 @@ error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
return object_error::success;
}

error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
uint64_t &Result) const {
uint64_t SectionOffset;
uint8_t SectionIndex;
error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
uint64_t &Result) const {
if (MachOObj->is64Bit()) {
InMemoryStruct<macho::Symbol64TableEntry> Entry;
getSymbol64TableEntry(DRI, Entry);
Result = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (Entry->SectionIndex) {
InMemoryStruct<macho::Section64> Section;
getSection64(Sections[Entry->SectionIndex-1], Section);
Result += Section->Offset - Section->Address;
}
} else {
InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry);
Result = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (Entry->SectionIndex) {
InMemoryStruct<macho::Section> Section;
getSection(Sections[Entry->SectionIndex-1], Section);
Result += Section->Offset - Section->Address;
}
}
getSectionAddress(Sections[SectionIndex-1], SectionOffset);
Result -= SectionOffset;

return object_error::success;
}
Expand All @@ -162,7 +166,64 @@ error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,

error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
uint64_t &Result) const {
Result = UnknownAddressOrSize;
uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
uint64_t BeginOffset;
uint64_t EndOffset = 0;
uint8_t SectionIndex;
if (MachOObj->is64Bit()) {
InMemoryStruct<macho::Symbol64TableEntry> Entry;
getSymbol64TableEntry(DRI, Entry);
BeginOffset = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (!SectionIndex) {
Result = UnknownAddressOrSize;
return object_error::success;
}
// Unfortunately symbols are unsorted so we need to touch all
// symbols from load command
DRI.d.b = 0;
uint32_t Command = DRI.d.a;
while (Command == DRI.d.a) {
moveToNextSymbol(DRI);
if (DRI.d.a < LoadCommandCount) {
getSymbol64TableEntry(DRI, Entry);
if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
if (!EndOffset || Entry->Value < EndOffset)
EndOffset = Entry->Value;
}
DRI.d.b++;
}
} else {
InMemoryStruct<macho::SymbolTableEntry> Entry;
getSymbolTableEntry(DRI, Entry);
BeginOffset = Entry->Value;
SectionIndex = Entry->SectionIndex;
if (!SectionIndex) {
Result = UnknownAddressOrSize;
return object_error::success;
}
// Unfortunately symbols are unsorted so we need to touch all
// symbols from load command
DRI.d.b = 0;
uint32_t Command = DRI.d.a;
while (Command == DRI.d.a) {
moveToNextSymbol(DRI);
if (DRI.d.a < LoadCommandCount) {
getSymbolTableEntry(DRI, Entry);
if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
if (!EndOffset || Entry->Value < EndOffset)
EndOffset = Entry->Value;
}
DRI.d.b++;
}
}
if (!EndOffset) {
uint64_t Size;
getSectionSize(Sections[SectionIndex-1], Size);
getSectionAddress(Sections[SectionIndex-1], EndOffset);
EndOffset += Size;
}
Result = EndOffset - BeginOffset;
return object_error::success;
}

Expand Down Expand Up @@ -275,7 +336,7 @@ error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
if (index == 0)
Res = end_sections();
else
Res = section_iterator(SectionRef(Sections[index], this));
Res = section_iterator(SectionRef(Sections[index-1], this));

return object_error::success;
}
Expand Down Expand Up @@ -614,14 +675,28 @@ error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
bool isScattered = (Arch != Triple::x86_64) &&
(RE->Word0 & macho::RF_Scattered);
uint64_t RelAddr = 0;
if (isScattered)
if (isScattered)
RelAddr = RE->Word0 & 0xFFFFFF;
else
RelAddr = RE->Word0;

Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
return object_error::success;
}
error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
uint64_t &Res) const {
InMemoryStruct<macho::RelocationEntry> RE;
getRelocation(Rel, RE);

unsigned Arch = getArch();
bool isScattered = (Arch != Triple::x86_64) &&
(RE->Word0 & macho::RF_Scattered);
if (isScattered)
Res = RE->Word0 & 0xFFFFFF;
else
Res = RE->Word0;
return object_error::success;
}
error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
SymbolRef &Res) const {
InMemoryStruct<macho::RelocationEntry> RE;
Expand Down
Loading

0 comments on commit b0436a7

Please sign in to comment.