summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/X86/X86TargetHandler.cpp
blob: 3fe61a7ed6e3e282e97a1c2a220d099a891f134f (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//===- lib/ReaderWriter/ELF/X86/X86TargetHandler.cpp ----------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "X86TargetHandler.h"
#include "X86LinkingContext.h"

using namespace lld;
using namespace elf;

using namespace llvm::ELF;

/// \brief R_386_32 - word32:  S + A
static int reloc32(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
  int32_t result = (uint32_t)(S + A);
  *reinterpret_cast<llvm::support::ulittle32_t *>(location) = result |
            (uint32_t)*reinterpret_cast<llvm::support::ulittle32_t *>(location);
  return 0;
}

/// \brief R_386_PC32 - word32: S + A - P
static int relocPC32(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
  uint32_t result = (uint32_t)((S + A) - P);
  *reinterpret_cast<llvm::support::ulittle32_t *>(location) = result +
            (uint32_t)*reinterpret_cast<llvm::support::ulittle32_t *>(location);
  return 0;
}

std::unique_ptr<Writer> X86TargetHandler::getWriter() {
  switch (_x86LinkingContext.getOutputELFType()) {
  case llvm::ELF::ET_EXEC:
    return std::unique_ptr<Writer>(new elf::ExecutableWriter<X86ELFType>(
        _x86LinkingContext, *_x86TargetLayout.get()));
  case llvm::ELF::ET_DYN:
    return std::unique_ptr<Writer>(new elf::DynamicLibraryWriter<X86ELFType>(
        _x86LinkingContext, *_x86TargetLayout.get()));
  case llvm::ELF::ET_REL:
    llvm_unreachable("TODO: support -r mode");
  default:
    llvm_unreachable("unsupported output type");
  }
}

const Registry::KindStrings X86TargetHandler::kindStrings[] = {
  LLD_KIND_STRING_ENTRY(R_386_NONE),
  LLD_KIND_STRING_ENTRY(R_386_32),
  LLD_KIND_STRING_ENTRY(R_386_PC32),
  LLD_KIND_STRING_ENTRY(R_386_GOT32),
  LLD_KIND_STRING_ENTRY(R_386_PLT32),
  LLD_KIND_STRING_ENTRY(R_386_COPY),
  LLD_KIND_STRING_ENTRY(R_386_GLOB_DAT),
  LLD_KIND_STRING_ENTRY(R_386_JUMP_SLOT),
  LLD_KIND_STRING_ENTRY(R_386_RELATIVE),
  LLD_KIND_STRING_ENTRY(R_386_GOTOFF),
  LLD_KIND_STRING_ENTRY(R_386_GOTPC),
  LLD_KIND_STRING_ENTRY(R_386_32PLT),
  LLD_KIND_STRING_ENTRY(R_386_TLS_TPOFF),
  LLD_KIND_STRING_ENTRY(R_386_TLS_IE),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GOTIE),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LE),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GD),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDM),
  LLD_KIND_STRING_ENTRY(R_386_16),
  LLD_KIND_STRING_ENTRY(R_386_PC16),
  LLD_KIND_STRING_ENTRY(R_386_8),
  LLD_KIND_STRING_ENTRY(R_386_PC8),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GD_32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GD_PUSH),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GD_CALL),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GD_POP),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDM_32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDM_PUSH),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDM_CALL),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDM_POP),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LDO_32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_IE_32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_LE_32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_DTPMOD32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_DTPOFF32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_TPOFF32),
  LLD_KIND_STRING_ENTRY(R_386_TLS_GOTDESC),
  LLD_KIND_STRING_ENTRY(R_386_TLS_DESC_CALL),
  LLD_KIND_STRING_ENTRY(R_386_TLS_DESC),
  LLD_KIND_STRING_ENTRY(R_386_IRELATIVE),
  LLD_KIND_STRING_ENTRY(R_386_NUM),
  LLD_KIND_STRING_END
};

void X86TargetHandler::registerRelocationNames(Registry &registry) {
  registry.addKindTable(Reference::KindNamespace::ELF, Reference::KindArch::x86,
                        kindStrings);
}

std::error_code X86TargetRelocationHandler::applyRelocation(
    ELFWriter &writer, llvm::FileOutputBuffer &buf, const lld::AtomLayout &atom,
    const Reference &ref) const {
  uint8_t *atomContent = buf.getBufferStart() + atom._fileOffset;
  uint8_t *location = atomContent + ref.offsetInAtom();
  uint64_t targetVAddress = writer.addressOfAtom(ref.target());
  uint64_t relocVAddress = atom._virtualAddr + ref.offsetInAtom();

  if (ref.kindNamespace() != Reference::KindNamespace::ELF)
    return std::error_code();
  assert(ref.kindArch() == Reference::KindArch::x86);
  switch (ref.kindValue()) {
  case R_386_32:
    reloc32(location, relocVAddress, targetVAddress, ref.addend());
    break;
  case R_386_PC32:
    relocPC32(location, relocVAddress, targetVAddress, ref.addend());
    break;
  default : {
    std::string str;
    llvm::raw_string_ostream s(str);
    s << "Unhandled I386 relocation # " << ref.kindValue();
    s.flush();
    llvm_unreachable(str.c_str());
  }
  }

  return std::error_code();
}

X86TargetHandler::X86TargetHandler(X86LinkingContext &context)
    : DefaultTargetHandler(context), _x86LinkingContext(context),
      _x86TargetLayout(new X86TargetLayout<X86ELFType>(context)),
      _x86RelocationHandler(
          new X86TargetRelocationHandler(context, *_x86TargetLayout.get())) {}
OpenPOWER on IntegriCloud