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();
30 elf_symbol_iterator SymI = Rel.getSymbol();
32 ErrorOr<StringRef> SymNameOrErr = SymI->getName();
33 if (std::error_code EC = SymNameOrErr.getError())
34 report_fatal_error(EC.message());
35 StringRef SymName = *SymNameOrErr;
37 uint64_t SymAddr; SymI->getAddress(SymAddr);
38 uint64_t SymSize = SymI->getSize();
39 int64_t Addend = *ELFRelocationRef(Rel).getAddend();
41 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
42 // FIXME: check that the value is actually the same.
43 if (!Sym->isVariable())
44 Sym->setVariableValue(MCConstantExpr::create(SymAddr, Ctx));
46 const MCExpr *Expr = nullptr;
47 // If hasAddend is true, then we need to add Addend (r_addend) to Expr.
48 bool hasAddend = false;
50 // The AMD64 SysV ABI says:
51 // A: the addend used to compute the value of the relocatable field.
52 // B: the base address at which a shared object has been loaded into memory
53 // during execution. Generally, a shared object is built with a 0 base
54 // virtual address, but the execution address will be different.
55 // G: the offset into the global offset table at which the relocation
56 // entry's symbol will reside during execution.
57 // GOT: the address of the global offset table.
58 // L: the place (section offset or address) of the Procedure Linkage Table
59 // entry for a symbol.
60 // P: the place (section offset or address) of the storage unit being
61 // relocated (computed using r_offset).
62 // S: the value of the symbol whose index resides in the relocation entry.
63 // Z: the size of the symbol whose index resides in the relocation entry.
76 // S + A (We don't care about the result not fitting in 32 bits.)
81 // S + A - P (P/pcrel is implicit)
83 Expr = MCSymbolRefExpr::create(Sym, Ctx);
87 case R_X86_64_GOTPC32:
88 case R_X86_64_GOTPC64:
89 case R_X86_64_GOTPLT64:
92 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOT, Ctx);
95 // L + A - P -> S@PLT + A
97 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_PLT, Ctx);
99 case R_X86_64_GLOB_DAT:
100 case R_X86_64_JUMP_SLOT:
102 Expr = MCSymbolRefExpr::create(Sym, Ctx);
104 case R_X86_64_GOTPCREL:
105 case R_X86_64_GOTPCREL64:
106 // G + GOT + A - P -> S@GOTPCREL + A
108 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
110 case R_X86_64_GOTOFF64:
112 Expr = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_GOTOFF, Ctx);
114 case R_X86_64_PLTOFF64:
117 case R_X86_64_SIZE32:
118 case R_X86_64_SIZE64:
120 Expr = MCConstantExpr::create(SymSize, Ctx);
123 Expr = MCSymbolRefExpr::create(Sym, Ctx);
126 if (Expr && hasAddend && Addend != 0)
127 Expr = MCBinaryExpr::createAdd(Expr,
128 MCConstantExpr::create(Addend, Ctx),
133 } // End unnamed namespace
135 /// createX86ELFRelocationInfo - Construct an X86 Mach-O RelocationInfo.
136 MCRelocationInfo *llvm::createX86_64ELFRelocationInfo(MCContext &Ctx) {
137 // We only handle x86-64 for now.
138 return new X86_64ELFRelocationInfo(Ctx);