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 elf_symbol_iterator SymI = Rel.getSymbol();
32 StringRef SymName; SymI->getName(SymName);
33 uint64_t SymAddr; SymI->getAddress(SymAddr);
34 auto *Obj = cast<ELFObjectFileBase>(Rel.getObjectFile());
35 uint64_t SymSize = SymI->getSize();
36 int64_t Addend = *Obj->getRelocationAddend(Rel.getRawDataRefImpl());
38 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
39 // FIXME: check that the value is actually the same.
40 if (!Sym->isVariable())
41 Sym->setVariableValue(MCConstantExpr::create(SymAddr, Ctx));
43 const MCExpr *Expr = nullptr;
44 // If hasAddend is true, then we need to add Addend (r_addend) to Expr.
45 bool hasAddend = false;
47 // The AMD64 SysV ABI says:
48 // A: the addend used to compute the value of the relocatable field.
49 // B: the base address at which a shared object has been loaded into memory
50 // during execution. Generally, a shared object is built with a 0 base
51 // virtual address, but the execution address will be different.
52 // G: the offset into the global offset table at which the relocation
53 // entry's symbol will reside during execution.
54 // GOT: the address of the global offset table.
55 // L: the place (section offset or address) of the Procedure Linkage Table
56 // entry for a symbol.
57 // P: the place (section offset or address) of the storage unit being
58 // relocated (computed using r_offset).
59 // S: the value of the symbol whose index resides in the relocation entry.
60 // Z: the size of the symbol whose index resides in the relocation entry.
73 // S + A (We don't care about the result not fitting in 32 bits.)
78 // S + A - P (P/pcrel is implicit)
80 Expr = MCSymbolRefExpr::create(Sym, Ctx);
84 case R_X86_64_GOTPC32:
85 case R_X86_64_GOTPC64:
86 case R_X86_64_GOTPLT64:
89 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, Ctx);
92 // L + A - P -> S@PLT + A
94 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_PLT, Ctx);
96 case R_X86_64_GLOB_DAT:
97 case R_X86_64_JUMP_SLOT:
99 Expr = MCSymbolRefExpr::create(Sym, Ctx);
101 case R_X86_64_GOTPCREL:
102 case R_X86_64_GOTPCREL64:
103 // G + GOT + A - P -> S@GOTPCREL + A
105 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
107 case R_X86_64_GOTOFF64:
109 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTOFF, Ctx);
111 case R_X86_64_PLTOFF64:
114 case R_X86_64_SIZE32:
115 case R_X86_64_SIZE64:
117 Expr = MCConstantExpr::create(SymSize, Ctx);
120 Expr = MCSymbolRefExpr::create(Sym, Ctx);
123 if (Expr && hasAddend && Addend != 0)
124 Expr = MCBinaryExpr::createAdd(Expr,
125 MCConstantExpr::create(Addend, Ctx),
130 } // End unnamed namespace
132 /// createX86ELFRelocationInfo - Construct an X86 Mach-O RelocationInfo.
133 MCRelocationInfo *llvm::createX86_64ELFRelocationInfo(MCContext &Ctx) {
134 // We only handle x86-64 for now.
135 return new X86_64ELFRelocationInfo(Ctx);