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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
//===- InputSection.cpp ---------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "InputSection.h"
#include "Error.h"
#include "InputFiles.h"
#include "OutputSections.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::object;
using namespace lld;
using namespace lld::elf2;
template <class ELFT>
InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
: File(F), Header(Header) {}
template <class ELFT>
void InputSection<ELFT>::relocateOne(uint8_t *Buf, const Elf_Rel &Rel,
uint32_t Type, uintX_t BaseAddr,
uintX_t SymVA) {
uintX_t Offset = Rel.r_offset;
uint8_t *Location = Buf + Offset;
switch (Type) {
case R_386_32:
support::endian::write32le(Location, SymVA);
break;
default:
llvm::errs() << Twine("unrecognized reloc ") + Twine(Type) << '\n';
break;
}
}
template <class ELFT>
void InputSection<ELFT>::relocateOne(uint8_t *Buf, const Elf_Rela &Rel,
uint32_t Type, uintX_t BaseAddr,
uintX_t SymVA) {
uintX_t Offset = Rel.r_offset;
uint8_t *Location = Buf + Offset;
switch (Type) {
case R_X86_64_PC32:
support::endian::write32le(Location,
SymVA + (Rel.r_addend - (BaseAddr + Offset)));
break;
case R_X86_64_64:
support::endian::write64le(Location, SymVA + Rel.r_addend);
break;
case R_X86_64_32: {
case R_X86_64_32S:
uint64_t VA = SymVA + Rel.r_addend;
if (Type == R_X86_64_32 && !isUInt<32>(VA))
error("R_X86_64_32 out of range");
else if (!isInt<32>(VA))
error("R_X86_64_32S out of range");
support::endian::write32le(Location, VA);
break;
}
default:
llvm::errs() << Twine("unrecognized reloc ") + Twine(Type) << '\n';
break;
}
}
template <class ELFT>
template <bool isRela>
void InputSection<ELFT>::relocate(
uint8_t *Buf, iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels,
const ObjectFile<ELFT> &File, uintX_t BaseAddr,
const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec) {
typedef Elf_Rel_Impl<ELFT, isRela> RelType;
bool IsMips64EL = File.getObj()->isMips64EL();
for (const RelType &RI : Rels) {
uint32_t SymIndex = RI.getSymbol(IsMips64EL);
uint32_t Type = RI.getType(IsMips64EL);
uintX_t SymVA;
// Handle relocations for local symbols -- they never get
// resolved so we don't allocate a SymbolBody.
const Elf_Shdr *SymTab = File.getSymbolTable();
if (SymIndex < SymTab->sh_info) {
const Elf_Sym *Sym = File.getObj()->getRelocationSymbol(&RI, SymTab);
if (!Sym)
continue;
SymVA = getLocalSymVA(Sym, File);
} else {
const SymbolBody *Body = File.getSymbolBody(SymIndex);
if (!Body)
continue;
switch (Body->kind()) {
case SymbolBody::DefinedRegularKind:
SymVA = getSymVA<ELFT>(cast<DefinedRegular<ELFT>>(Body));
break;
case SymbolBody::DefinedAbsoluteKind:
SymVA = cast<DefinedAbsolute<ELFT>>(Body)->Sym.st_value;
break;
case SymbolBody::DefinedCommonKind: {
auto *DC = cast<DefinedCommon<ELFT>>(Body);
SymVA = DC->OutputSec->getVA() + DC->OffsetInBSS;
break;
}
case SymbolBody::SharedKind:
if (relocNeedsPLT(Type)) {
SymVA = PltSec.getEntryAddr(*Body);
Type = R_X86_64_PC32;
} else if (relocNeedsGOT(Type)) {
SymVA = GotSec.getEntryAddr(*Body);
Type = R_X86_64_PC32;
} else {
continue;
}
break;
case SymbolBody::UndefinedKind:
assert(Body->isWeak() && "Undefined symbol reached writer");
SymVA = 0;
break;
case SymbolBody::LazyKind:
llvm_unreachable("Lazy symbol reached writer");
}
}
relocateOne(Buf, RI, Type, BaseAddr, SymVA);
}
}
template <class ELFT>
void InputSection<ELFT>::writeTo(uint8_t *Buf, const PltSection<ELFT> &PltSec,
const GotSection<ELFT> &GotSec) {
if (Header->sh_type == SHT_NOBITS)
return;
// Copy section contents from source object file to output file.
ArrayRef<uint8_t> Data = *File->getObj()->getSectionContents(Header);
memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
const ObjectFile<ELFT> *File = getFile();
ELFFile<ELFT> *EObj = File->getObj();
uint8_t *Base = Buf + getOutputSectionOff();
uintX_t BaseAddr = Out->getVA() + getOutputSectionOff();
// Iterate over all relocation sections that apply to this section.
for (const Elf_Shdr *RelSec : RelocSections) {
if (RelSec->sh_type == SHT_RELA)
relocate(Base, EObj->relas(RelSec), *File, BaseAddr, PltSec, GotSec);
else
relocate(Base, EObj->rels(RelSec), *File, BaseAddr, PltSec, GotSec);
}
}
template <class ELFT> StringRef InputSection<ELFT>::getSectionName() const {
ErrorOr<StringRef> Name = File->getObj()->getSectionName(Header);
error(Name);
return *Name;
}
namespace lld {
namespace elf2 {
template class InputSection<object::ELF32LE>;
template class InputSection<object::ELF32BE>;
template class InputSection<object::ELF64LE>;
template class InputSection<object::ELF64BE>;
}
}
|