2012-11-07 23:22:07 +00:00
|
|
|
//===-- RelocVisitor.h - Visitor for object file relocations -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides a wrapper around all the different types of relocations
|
|
|
|
// in different file formats, such that a client can handle them in a unified
|
|
|
|
// manner by only implementing a minimal number of functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_OBJECT_RELOCVISITOR_H
|
|
|
|
#define LLVM_OBJECT_RELOCVISITOR_H
|
2012-11-07 23:22:07 +00:00
|
|
|
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-08-08 22:27:13 +00:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2012-11-07 23:22:07 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-01-19 19:56:11 +00:00
|
|
|
#include "llvm/Support/ELF.h"
|
2012-11-07 23:22:07 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace object {
|
|
|
|
|
|
|
|
struct RelocToApply {
|
|
|
|
// The computed value after applying the relevant relocations.
|
|
|
|
int64_t Value;
|
|
|
|
|
|
|
|
// The width of the value; how many bytes to touch when applying the
|
|
|
|
// relocation.
|
|
|
|
char Width;
|
|
|
|
RelocToApply(int64_t Value, char Width) : Value(Value), Width(Width) {}
|
|
|
|
RelocToApply() : Value(0), Width(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief Base class for object file relocation visitors.
|
|
|
|
class RelocVisitor {
|
|
|
|
public:
|
2013-01-13 16:01:15 +00:00
|
|
|
explicit RelocVisitor(StringRef FileFormat)
|
2012-11-07 23:22:07 +00:00
|
|
|
: FileFormat(FileFormat), HasError(false) {}
|
|
|
|
|
|
|
|
// TODO: Should handle multiple applied relocations via either passing in the
|
|
|
|
// previously computed value or just count paired relocations as a single
|
|
|
|
// visit.
|
|
|
|
RelocToApply visit(uint32_t RelocType, RelocationRef R, uint64_t SecAddr = 0,
|
|
|
|
uint64_t Value = 0) {
|
|
|
|
if (FileFormat == "ELF64-x86-64") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_X86_64_NONE:
|
|
|
|
return visitELF_X86_64_NONE(R);
|
|
|
|
case llvm::ELF::R_X86_64_64:
|
|
|
|
return visitELF_X86_64_64(R, Value);
|
|
|
|
case llvm::ELF::R_X86_64_PC32:
|
|
|
|
return visitELF_X86_64_PC32(R, Value, SecAddr);
|
|
|
|
case llvm::ELF::R_X86_64_32:
|
|
|
|
return visitELF_X86_64_32(R, Value);
|
|
|
|
case llvm::ELF::R_X86_64_32S:
|
|
|
|
return visitELF_X86_64_32S(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2012-12-27 01:07:07 +00:00
|
|
|
} else if (FileFormat == "ELF32-i386") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_386_NONE:
|
|
|
|
return visitELF_386_NONE(R);
|
|
|
|
case llvm::ELF::R_386_32:
|
|
|
|
return visitELF_386_32(R, Value);
|
|
|
|
case llvm::ELF::R_386_PC32:
|
|
|
|
return visitELF_386_PC32(R, Value, SecAddr);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2013-01-26 08:27:29 +00:00
|
|
|
} else if (FileFormat == "ELF64-ppc64") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_PPC64_ADDR32:
|
|
|
|
return visitELF_PPC64_ADDR32(R, Value);
|
2013-06-22 13:03:15 +00:00
|
|
|
case llvm::ELF::R_PPC64_ADDR64:
|
|
|
|
return visitELF_PPC64_ADDR64(R, Value);
|
2013-01-26 08:27:29 +00:00
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2013-05-18 16:00:35 +00:00
|
|
|
} else if (FileFormat == "ELF32-ppc") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_PPC_ADDR32:
|
|
|
|
return visitELF_PPC_ADDR32(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2013-01-26 08:27:36 +00:00
|
|
|
} else if (FileFormat == "ELF32-mips") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_MIPS_32:
|
|
|
|
return visitELF_MIPS_32(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2014-02-18 15:57:52 +00:00
|
|
|
} else if (FileFormat == "ELF64-mips") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_MIPS_32:
|
|
|
|
return visitELF_MIPS_32(R, Value);
|
|
|
|
case llvm::ELF::R_MIPS_64:
|
|
|
|
return visitELF_MIPS_64(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2013-02-11 11:16:02 +00:00
|
|
|
} else if (FileFormat == "ELF64-aarch64") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_AARCH64_ABS32:
|
|
|
|
return visitELF_AARCH64_ABS32(R, Value);
|
|
|
|
case llvm::ELF::R_AARCH64_ABS64:
|
|
|
|
return visitELF_AARCH64_ABS64(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2013-05-03 11:11:15 +00:00
|
|
|
} else if (FileFormat == "ELF64-s390") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_390_32:
|
|
|
|
return visitELF_390_32(R, Value);
|
|
|
|
case llvm::ELF::R_390_64:
|
|
|
|
return visitELF_390_64(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2014-01-26 05:13:44 +00:00
|
|
|
} else if (FileFormat == "ELF32-sparc") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_SPARC_32:
|
|
|
|
case llvm::ELF::R_SPARC_UA32:
|
|
|
|
return visitELF_SPARC_32(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
|
|
|
} else if (FileFormat == "ELF64-sparc") {
|
|
|
|
switch (RelocType) {
|
|
|
|
case llvm::ELF::R_SPARC_32:
|
|
|
|
case llvm::ELF::R_SPARC_UA32:
|
|
|
|
return visitELF_SPARCV9_32(R, Value);
|
|
|
|
case llvm::ELF::R_SPARC_64:
|
|
|
|
case llvm::ELF::R_SPARC_UA64:
|
|
|
|
return visitELF_SPARCV9_64(R, Value);
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
}
|
2014-04-03 02:27:00 +00:00
|
|
|
} else if (FileFormat == "ELF32-arm") {
|
|
|
|
switch (RelocType) {
|
|
|
|
default:
|
|
|
|
HasError = true;
|
|
|
|
return RelocToApply();
|
|
|
|
case llvm::ELF::R_ARM_ABS32:
|
|
|
|
return visitELF_ARM_ABS32(R, Value);
|
|
|
|
}
|
2012-11-07 23:22:07 +00:00
|
|
|
}
|
2013-01-26 08:27:23 +00:00
|
|
|
HasError = true;
|
2012-11-07 23:22:07 +00:00
|
|
|
return RelocToApply();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool error() { return HasError; }
|
|
|
|
|
|
|
|
private:
|
2013-01-13 16:01:15 +00:00
|
|
|
StringRef FileFormat;
|
2012-11-07 23:22:07 +00:00
|
|
|
bool HasError;
|
|
|
|
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t getAddend32LE(RelocationRef R) {
|
|
|
|
const ELF32LEObjectFile *Obj = cast<ELF32LEObjectFile>(R.getObjectFile());
|
|
|
|
DataRefImpl DRI = R.getRawDataRefImpl();
|
2013-05-09 03:39:05 +00:00
|
|
|
int64_t Addend;
|
2013-05-09 15:10:36 +00:00
|
|
|
Obj->getRelocationAddend(DRI, Addend);
|
|
|
|
return Addend;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t getAddend64LE(RelocationRef R) {
|
|
|
|
const ELF64LEObjectFile *Obj = cast<ELF64LEObjectFile>(R.getObjectFile());
|
|
|
|
DataRefImpl DRI = R.getRawDataRefImpl();
|
|
|
|
int64_t Addend;
|
|
|
|
Obj->getRelocationAddend(DRI, Addend);
|
|
|
|
return Addend;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t getAddend32BE(RelocationRef R) {
|
|
|
|
const ELF32BEObjectFile *Obj = cast<ELF32BEObjectFile>(R.getObjectFile());
|
|
|
|
DataRefImpl DRI = R.getRawDataRefImpl();
|
|
|
|
int64_t Addend;
|
|
|
|
Obj->getRelocationAddend(DRI, Addend);
|
2013-05-09 03:39:05 +00:00
|
|
|
return Addend;
|
|
|
|
}
|
|
|
|
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t getAddend64BE(RelocationRef R) {
|
|
|
|
const ELF64BEObjectFile *Obj = cast<ELF64BEObjectFile>(R.getObjectFile());
|
|
|
|
DataRefImpl DRI = R.getRawDataRefImpl();
|
|
|
|
int64_t Addend;
|
|
|
|
Obj->getRelocationAddend(DRI, Addend);
|
|
|
|
return Addend;
|
|
|
|
}
|
2012-11-07 23:22:07 +00:00
|
|
|
/// Operations
|
|
|
|
|
2012-12-27 01:07:07 +00:00
|
|
|
/// 386-ELF
|
|
|
|
RelocToApply visitELF_386_NONE(RelocationRef R) {
|
|
|
|
return RelocToApply(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally the Addend here will be the addend in the data for
|
|
|
|
// the relocation. It's not actually the case for Rel relocations.
|
|
|
|
RelocToApply visitELF_386_32(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend32LE(R);
|
2012-12-27 01:07:07 +00:00
|
|
|
return RelocToApply(Value + Addend, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocToApply visitELF_386_PC32(RelocationRef R, uint64_t Value,
|
|
|
|
uint64_t SecAddr) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend32LE(R);
|
2012-12-27 01:07:07 +00:00
|
|
|
uint64_t Address;
|
2013-04-25 12:28:45 +00:00
|
|
|
R.getOffset(Address);
|
2012-12-27 01:07:07 +00:00
|
|
|
return RelocToApply(Value + Addend - Address, 4);
|
|
|
|
}
|
|
|
|
|
2012-11-07 23:22:07 +00:00
|
|
|
/// X86-64 ELF
|
|
|
|
RelocToApply visitELF_X86_64_NONE(RelocationRef R) {
|
|
|
|
return RelocToApply(0, 0);
|
|
|
|
}
|
|
|
|
RelocToApply visitELF_X86_64_64(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2012-11-07 23:22:07 +00:00
|
|
|
return RelocToApply(Value + Addend, 8);
|
|
|
|
}
|
|
|
|
RelocToApply visitELF_X86_64_PC32(RelocationRef R, uint64_t Value,
|
|
|
|
uint64_t SecAddr) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2012-11-07 23:22:07 +00:00
|
|
|
uint64_t Address;
|
2013-04-25 12:28:45 +00:00
|
|
|
R.getOffset(Address);
|
2012-11-07 23:22:07 +00:00
|
|
|
return RelocToApply(Value + Addend - Address, 4);
|
|
|
|
}
|
|
|
|
RelocToApply visitELF_X86_64_32(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2012-11-07 23:22:07 +00:00
|
|
|
uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
|
|
|
|
return RelocToApply(Res, 4);
|
|
|
|
}
|
|
|
|
RelocToApply visitELF_X86_64_32S(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2012-11-07 23:22:07 +00:00
|
|
|
int32_t Res = (Value + Addend) & 0xFFFFFFFF;
|
|
|
|
return RelocToApply(Res, 4);
|
|
|
|
}
|
2013-01-26 08:27:29 +00:00
|
|
|
|
|
|
|
/// PPC64 ELF
|
|
|
|
RelocToApply visitELF_PPC64_ADDR32(RelocationRef R, uint64_t Value) {
|
2014-06-18 15:15:49 +00:00
|
|
|
int64_t Addend;
|
|
|
|
getELFRelocationAddend(R, Addend);
|
2013-01-26 08:27:29 +00:00
|
|
|
uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
|
|
|
|
return RelocToApply(Res, 4);
|
|
|
|
}
|
2013-06-22 13:03:15 +00:00
|
|
|
RelocToApply visitELF_PPC64_ADDR64(RelocationRef R, uint64_t Value) {
|
2014-06-18 15:15:49 +00:00
|
|
|
int64_t Addend;
|
|
|
|
getELFRelocationAddend(R, Addend);
|
2013-06-22 13:03:15 +00:00
|
|
|
return RelocToApply(Value + Addend, 8);
|
|
|
|
}
|
2013-01-26 08:27:36 +00:00
|
|
|
|
2013-05-18 16:00:35 +00:00
|
|
|
/// PPC32 ELF
|
|
|
|
RelocToApply visitELF_PPC_ADDR32(RelocationRef R, uint64_t Value) {
|
|
|
|
int64_t Addend = getAddend32BE(R);
|
|
|
|
uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
|
|
|
|
return RelocToApply(Res, 4);
|
|
|
|
}
|
|
|
|
|
2013-01-26 08:27:36 +00:00
|
|
|
/// MIPS ELF
|
|
|
|
RelocToApply visitELF_MIPS_32(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend;
|
|
|
|
getELFRelocationAddend(R, Addend);
|
2013-01-26 08:27:36 +00:00
|
|
|
uint32_t Res = (Value + Addend) & 0xFFFFFFFF;
|
|
|
|
return RelocToApply(Res, 4);
|
|
|
|
}
|
2013-02-11 11:16:02 +00:00
|
|
|
|
2014-02-18 15:57:52 +00:00
|
|
|
RelocToApply visitELF_MIPS_64(RelocationRef R, uint64_t Value) {
|
|
|
|
int64_t Addend;
|
|
|
|
getELFRelocationAddend(R, Addend);
|
|
|
|
uint64_t Res = (Value + Addend);
|
|
|
|
return RelocToApply(Res, 8);
|
|
|
|
}
|
|
|
|
|
2013-02-11 11:16:02 +00:00
|
|
|
// AArch64 ELF
|
|
|
|
RelocToApply visitELF_AARCH64_ABS32(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2013-02-11 11:16:02 +00:00
|
|
|
int64_t Res = Value + Addend;
|
|
|
|
|
|
|
|
// Overflow check allows for both signed and unsigned interpretation.
|
|
|
|
if (Res < INT32_MIN || Res > UINT32_MAX)
|
|
|
|
HasError = true;
|
|
|
|
|
|
|
|
return RelocToApply(static_cast<uint32_t>(Res), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocToApply visitELF_AARCH64_ABS64(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64LE(R);
|
2013-02-11 11:16:02 +00:00
|
|
|
return RelocToApply(Value + Addend, 8);
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:11:15 +00:00
|
|
|
// SystemZ ELF
|
|
|
|
RelocToApply visitELF_390_32(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64BE(R);
|
2013-05-03 11:11:15 +00:00
|
|
|
int64_t Res = Value + Addend;
|
|
|
|
|
|
|
|
// Overflow check allows for both signed and unsigned interpretation.
|
|
|
|
if (Res < INT32_MIN || Res > UINT32_MAX)
|
|
|
|
HasError = true;
|
|
|
|
|
|
|
|
return RelocToApply(static_cast<uint32_t>(Res), 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocToApply visitELF_390_64(RelocationRef R, uint64_t Value) {
|
2013-05-09 15:10:36 +00:00
|
|
|
int64_t Addend = getAddend64BE(R);
|
2013-05-03 11:11:15 +00:00
|
|
|
return RelocToApply(Value + Addend, 8);
|
|
|
|
}
|
2014-01-26 05:13:44 +00:00
|
|
|
|
|
|
|
RelocToApply visitELF_SPARC_32(RelocationRef R, uint32_t Value) {
|
|
|
|
int32_t Addend = getAddend32BE(R);
|
|
|
|
return RelocToApply(Value + Addend, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocToApply visitELF_SPARCV9_32(RelocationRef R, uint64_t Value) {
|
|
|
|
int32_t Addend = getAddend64BE(R);
|
|
|
|
return RelocToApply(Value + Addend, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocToApply visitELF_SPARCV9_64(RelocationRef R, uint64_t Value) {
|
|
|
|
int64_t Addend = getAddend64BE(R);
|
|
|
|
return RelocToApply(Value + Addend, 8);
|
|
|
|
}
|
|
|
|
|
2014-04-03 02:27:00 +00:00
|
|
|
RelocToApply visitELF_ARM_ABS32(RelocationRef R, uint64_t Value) {
|
|
|
|
int64_t Addend = getAddend32LE(R);
|
|
|
|
return RelocToApply(Value + Addend, 4);
|
|
|
|
}
|
|
|
|
|
2012-11-07 23:22:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|