1 //===-- X86ELFRelocationInfo.cpp ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/X86MCTargetDesc.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCExpr.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCRelocationInfo.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Object/ELFObjectFile.h"
17 #include "llvm/Support/ELF.h"
20 using namespace object;
24 class X86_64ELFRelocationInfo : public MCRelocationInfo {
26 X86_64ELFRelocationInfo(MCContext &Ctx) : MCRelocationInfo(Ctx) {}
28 const MCExpr *createExprForRelocation(RelocationRef Rel) override {
29 uint64_t RelType; Rel.getType(RelType);
30 symbol_iterator SymI = Rel.getSymbol();
32 StringRef SymName; SymI->getName(SymName);
33 uint64_t SymAddr; SymI->getAddress(SymAddr);
34 uint64_t SymSize = SymI->getSize();
35 int64_t Addend; getELFRelocationAddend(Rel, Addend);
37 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
38 // FIXME: check that the value is actually the same.
39 if (!Sym->isVariable())
40 Sym->setVariableValue(MCConstantExpr::create(SymAddr, Ctx));
42 const MCExpr *Expr = nullptr;
43 // If hasAddend is true, then we need to add Addend (r_addend) to Expr.
44 bool hasAddend = false;
46 // The AMD64 SysV ABI says:
47 // A: the addend used to compute the value of the relocatable field.
48 // B: the base address at which a shared object has been loaded into memory
49 // during execution. Generally, a shared object is built with a 0 base
50 // virtual address, but the execution address will be different.
51 // G: the offset into the global offset table at which the relocation
52 // entry's symbol will reside during execution.
53 // GOT: the address of the global offset table.
54 // L: the place (section offset or address) of the Procedure Linkage Table
55 // entry for a symbol.
56 // P: the place (section offset or address) of the storage unit being
57 // relocated (computed using r_offset).
58 // S: the value of the symbol whose index resides in the relocation entry.
59 // Z: the size of the symbol whose index resides in the relocation entry.
72 // S + A (We don't care about the result not fitting in 32 bits.)
77 // S + A - P (P/pcrel is implicit)
79 Expr = MCSymbolRefExpr::create(Sym, Ctx);
83 case R_X86_64_GOTPC32:
84 case R_X86_64_GOTPC64:
85 case R_X86_64_GOTPLT64:
88 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, Ctx);
91 // L + A - P -> S@PLT + A
93 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_PLT, Ctx);
95 case R_X86_64_GLOB_DAT:
96 case R_X86_64_JUMP_SLOT:
98 Expr = MCSymbolRefExpr::create(Sym, Ctx);
100 case R_X86_64_GOTPCREL:
101 case R_X86_64_GOTPCREL64:
102 // G + GOT + A - P -> S@GOTPCREL + A
104 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
106 case R_X86_64_GOTOFF64:
108 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTOFF, Ctx);
110 case R_X86_64_PLTOFF64:
113 case R_X86_64_SIZE32:
114 case R_X86_64_SIZE64:
116 Expr = MCConstantExpr::create(SymSize, Ctx);
119 Expr = MCSymbolRefExpr::create(Sym, Ctx);
122 if (Expr && hasAddend && Addend != 0)
123 Expr = MCBinaryExpr::createAdd(Expr,
124 MCConstantExpr::create(Addend, Ctx),
129 } // End unnamed namespace
131 /// createX86ELFRelocationInfo - Construct an X86 Mach-O RelocationInfo.
132 MCRelocationInfo *llvm::createX86_64ELFRelocationInfo(MCContext &Ctx) {
133 // We only handle x86-64 for now.
134 return new X86_64ELFRelocationInfo(Ctx);