aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Target/X86/MCTargetDesc/X86MachORelocationInfo.cpp
blob: 209b1d0ee8bfdfc6dc80e9c43a30aca9fd67fdae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//===-- X86MachORelocationInfo.cpp ----------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "MCTargetDesc/X86MCTargetDesc.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCRelocationInfo.h"
#include "llvm/Object/MachO.h"

using namespace llvm;
using namespace object;
using namespace MachO;

namespace {
class X86_64MachORelocationInfo : public MCRelocationInfo {
public:
  X86_64MachORelocationInfo(MCContext &Ctx) : MCRelocationInfo(Ctx) {}

  const MCExpr *createExprForRelocation(RelocationRef Rel) {
    const MachOObjectFile *Obj = cast<MachOObjectFile>(Rel.getObjectFile());

    uint64_t RelType; Rel.getType(RelType);
    symbol_iterator SymI = Rel.getSymbol();

    StringRef SymName; SymI->getName(SymName);
    uint64_t  SymAddr; SymI->getAddress(SymAddr);

    any_relocation_info RE = Obj->getRelocation(Rel.getRawDataRefImpl());
    bool isPCRel = Obj->getAnyRelocationPCRel(RE);

    MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
    // FIXME: check that the value is actually the same.
    if (Sym->isVariable() == false)
      Sym->setVariableValue(MCConstantExpr::Create(SymAddr, Ctx));
    const MCExpr *Expr = 0;

    switch(RelType) {
    case X86_64_RELOC_TLV:
      Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx);
      break;
    case X86_64_RELOC_SIGNED_4:
      Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
                                     MCConstantExpr::Create(4, Ctx),
                                     Ctx);
      break;
    case X86_64_RELOC_SIGNED_2:
      Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
                                     MCConstantExpr::Create(2, Ctx),
                                     Ctx);
      break;
    case X86_64_RELOC_SIGNED_1:
      Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
                                     MCConstantExpr::Create(1, Ctx),
                                     Ctx);
      break;
    case X86_64_RELOC_GOT_LOAD:
      Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
      break;
    case X86_64_RELOC_GOT:
      Expr = MCSymbolRefExpr::Create(Sym, isPCRel ?
                                     MCSymbolRefExpr::VK_GOTPCREL :
                                     MCSymbolRefExpr::VK_GOT,
                                     Ctx);
      break;
    case X86_64_RELOC_SUBTRACTOR:
      {
        RelocationRef RelNext;
        Obj->getRelocationNext(Rel.getRawDataRefImpl(), RelNext);
        any_relocation_info RENext = Obj->getRelocation(RelNext.getRawDataRefImpl());

        // X86_64_SUBTRACTOR must be followed by a relocation of type
        // X86_64_RELOC_UNSIGNED.
        // NOTE: Scattered relocations don't exist on x86_64.
        unsigned RType = Obj->getAnyRelocationType(RENext);
        if (RType != X86_64_RELOC_UNSIGNED)
          report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
                             "X86_64_RELOC_SUBTRACTOR.");

        const MCExpr *LHS = MCSymbolRefExpr::Create(Sym, Ctx);

        symbol_iterator RSymI = RelNext.getSymbol();
        uint64_t RSymAddr;
        RSymI->getAddress(RSymAddr);
        StringRef RSymName;
        RSymI->getName(RSymName);

        MCSymbol *RSym = Ctx.GetOrCreateSymbol(RSymName);
        if (RSym->isVariable() == false)
          RSym->setVariableValue(MCConstantExpr::Create(RSymAddr, Ctx));

        const MCExpr *RHS = MCSymbolRefExpr::Create(RSym, Ctx);

        Expr = MCBinaryExpr::CreateSub(LHS, RHS, Ctx);
        break;
      }
    default:
      Expr = MCSymbolRefExpr::Create(Sym, Ctx);
      break;
    }
    return Expr;
  }
};
} // End unnamed namespace

/// createX86_64MachORelocationInfo - Construct an X86-64 Mach-O RelocationInfo.
MCRelocationInfo *llvm::createX86_64MachORelocationInfo(MCContext &Ctx) {
  return new X86_64MachORelocationInfo(Ctx);
}