#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/ObjectBuffer.h"
#include "llvm/ExecutionEngine/ObjectImage.h"
-#include "llvm/Object/ELF.h"
+#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/ELF.h"
using namespace llvm;
}
case ELF::R_AARCH64_PREL32: {
uint64_t Result = Value + Addend - FinalAddress;
- assert(static_cast<int64_t>(Result) >= INT32_MIN &&
+ assert(static_cast<int64_t>(Result) >= INT32_MIN &&
static_cast<int64_t>(Result) <= UINT32_MAX);
*TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU);
break;
uint64_t BranchImm = Value + Addend - FinalAddress;
// "Check that -2^27 <= result < 2^27".
- assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
+ assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) &&
static_cast<int64_t>(BranchImm) < (1LL << 27));
// AArch64 code is emitted with .rela relocations. The data already in any
// AArch64 code is emitted with .rela relocations. The data already in any
// bits affected by the relocation on entry is garbage.
- *TargetPtr &= 0xff80001fU;
+ *TargetPtr &= 0xffe0001fU;
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
*TargetPtr |= Result >> (48 - 5);
- // Shift is "lsl #48", in bits 22:21
- *TargetPtr |= 3 << 21;
+ // Shift must be "lsl #48", in bits 22:21
+ assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation");
break;
}
case ELF::R_AARCH64_MOVW_UABS_G2_NC: {
uint64_t Result = Value + Addend;
-
// AArch64 code is emitted with .rela relocations. The data already in any
// bits affected by the relocation on entry is garbage.
- *TargetPtr &= 0xff80001fU;
+ *TargetPtr &= 0xffe0001fU;
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
*TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5));
- // Shift is "lsl #32", in bits 22:21
- *TargetPtr |= 2 << 21;
+ // Shift must be "lsl #32", in bits 22:21
+ assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation");
break;
}
case ELF::R_AARCH64_MOVW_UABS_G1_NC: {
// AArch64 code is emitted with .rela relocations. The data already in any
// bits affected by the relocation on entry is garbage.
- *TargetPtr &= 0xff80001fU;
+ *TargetPtr &= 0xffe0001fU;
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
*TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5));
- // Shift is "lsl #16", in bits 22:21
- *TargetPtr |= 1 << 21;
+ // Shift must be "lsl #16", in bits 22:2
+ assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation");
break;
}
case ELF::R_AARCH64_MOVW_UABS_G0_NC: {
// AArch64 code is emitted with .rela relocations. The data already in any
// bits affected by the relocation on entry is garbage.
- *TargetPtr &= 0xff80001fU;
+ *TargetPtr &= 0xffe0001fU;
// Immediate goes in bits 20:5 of MOVZ/MOVK instruction
*TargetPtr |= ((Result & 0xffffU) << 5);
- // Shift is "lsl #0", in bits 22:21. No action needed.
+ // Shift must be "lsl #0", in bits 22:21.
+ assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation");
break;
}
}
uint32_t Value,
uint32_t Type,
int32_t Addend) {
+ uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress +
+ Offset);
uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Value += Addend;
llvm_unreachable("Not implemented relocation type!");
break;
case ELF::R_MIPS_32:
- *TargetPtr = Value + (*TargetPtr);
+ *TargetPtr = Value + (*Placeholder);
break;
case ELF::R_MIPS_26:
- *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
+ *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
break;
case ELF::R_MIPS_HI16:
// Get the higher 16-bits. Also add 1 if bit 15 is 1.
- Value += ((*TargetPtr) & 0x0000ffff) << 16;
+ Value += ((*Placeholder) & 0x0000ffff) << 16;
+ *TargetPtr = ((*Placeholder) & 0xffff0000) |
+ (((Value + 0x8000) >> 16) & 0xffff);
+ break;
+ case ELF::R_MIPS_LO16:
+ Value += ((*Placeholder) & 0x0000ffff);
+ *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff);
+ break;
+ case ELF::R_MIPS_UNUSED1:
+ // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2
+ // are used for internal JIT purpose. These relocations are similar to
+ // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into
+ // account.
*TargetPtr = ((*TargetPtr) & 0xffff0000) |
(((Value + 0x8000) >> 16) & 0xffff);
break;
- case ELF::R_MIPS_LO16:
- Value += ((*TargetPtr) & 0x0000ffff);
+ case ELF::R_MIPS_UNUSED2:
*TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
break;
}
continue;
}
- SymbolRef TargetSymbol;
uint64_t TargetSymbolOffset;
- check(i->getSymbol(TargetSymbol));
+ symbol_iterator TargetSymbol = i->getSymbol();
check(i->getOffset(TargetSymbolOffset));
int64_t Addend;
check(getELFRelocationAddend(*i, Addend));
continue;
section_iterator tsi(Obj.end_sections());
- check(TargetSymbol.getSection(tsi));
+ check(TargetSymbol->getSection(tsi));
Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
Rel.Addend = (intptr_t)Addend;
return;
(uint32_t)(Value & 0xffffffffL), Type,
(uint32_t)(Addend & 0xffffffffL));
break;
- case Triple::ppc64:
+ case Triple::ppc64: // Fall through.
+ case Triple::ppc64le:
resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
break;
case Triple::systemz:
Check(RelI.getType(RelType));
int64_t Addend;
Check(getELFRelocationAddend(RelI, Addend));
- SymbolRef Symbol;
- Check(RelI.getSymbol(Symbol));
+ symbol_iterator Symbol = RelI.getSymbol();
// Obtain the symbol name which is referenced in the relocation
StringRef TargetName;
- Symbol.getName(TargetName);
+ if (Symbol != Obj.end_symbols())
+ Symbol->getName(TargetName);
DEBUG(dbgs() << "\t\tRelType: " << RelType
<< " Addend: " << Addend
<< " TargetName: " << TargetName
<< "\n");
RelocationValueRef Value;
// First search for the symbol in the local symbol table
- SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
- SymbolRef::Type SymType;
- Symbol.getType(SymType);
+ SymbolTableMap::const_iterator lsi = Symbols.end();
+ SymbolRef::Type SymType = SymbolRef::ST_Unknown;
+ if (Symbol != Obj.end_symbols()) {
+ lsi = Symbols.find(TargetName.data());
+ Symbol->getType(SymType);
+ }
if (lsi != Symbols.end()) {
Value.SectionID = lsi->second.first;
Value.Addend = lsi->second.second + Addend;
} else {
// Search for the symbol in the global symbol table
- SymbolTableMap::const_iterator gsi =
- GlobalSymbolTable.find(TargetName.data());
+ SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end();
+ if (Symbol != Obj.end_symbols())
+ gsi = GlobalSymbolTable.find(TargetName.data());
if (gsi != GlobalSymbolTable.end()) {
Value.SectionID = gsi->second.first;
Value.Addend = gsi->second.second + Addend;
// 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());
- Symbol.getSection(si);
+ Symbol->getSection(si);
if (si == Obj.end_sections())
llvm_unreachable("Symbol section not found, bad object file format!");
DEBUG(dbgs() << "\t\tThis is section symbol\n");
case SymbolRef::ST_Unknown: {
Value.SymbolName = TargetName.data();
Value.Addend = Addend;
+
+ // Absolute relocations will have a zero symbol ID (STN_UNDEF), which
+ // will manifest here as a NULL symbol name.
+ // We can set this as a valid (but empty) symbol name, and rely
+ // on addRelocationForSymbol to handle this.
+ if (!Value.SymbolName)
+ Value.SymbolName = "";
break;
}
default:
// Creating Hi and Lo relocations for the filled stub instructions.
RelocationEntry REHi(SectionID,
StubTargetAddr - Section.Address,
- ELF::R_MIPS_HI16, Value.Addend);
+ ELF::R_MIPS_UNUSED1, Value.Addend);
RelocationEntry RELo(SectionID,
StubTargetAddr - Section.Address + 4,
- ELF::R_MIPS_LO16, Value.Addend);
+ ELF::R_MIPS_UNUSED2, Value.Addend);
if (Value.SymbolName) {
addRelocationForSymbol(REHi, Value.SymbolName);
RelType, 0);
Section.StubOffset += getMaxStubSize();
}
- } else if (Arch == Triple::ppc64) {
+ } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
if (RelType == ELF::R_PPC64_REL24) {
// A PPC branch relocation will need a stub function if the target is
// an external symbol (Symbol::ST_Unknown) or if the target address
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend);
// Extra check to avoid relocation againt empty symbols (usually
// the R_PPC64_TOC).
- if (Value.SymbolName && !TargetName.empty())
+ if (SymType != SymbolRef::ST_Unknown && TargetName.empty())
+ Value.SymbolName = NULL;
+
+ if (Value.SymbolName)
addRelocationForSymbol(RE, Value.SymbolName);
else
addRelocationForSection(RE, Value.SectionID);