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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
//===- lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h ----------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_READER_WRITER_ELF_MIPS_MIPS_TARGET_HANDLER_H
#define LLD_READER_WRITER_ELF_MIPS_MIPS_TARGET_HANDLER_H
#include "DefaultTargetHandler.h"
#include "MipsDynamicLibraryWriter.h"
#include "MipsELFReader.h"
#include "MipsExecutableWriter.h"
#include "MipsLinkingContext.h"
#include "MipsRelocationHandler.h"
#include "MipsSectionChunks.h"
#include "TargetLayout.h"
#include "llvm/ADT/DenseSet.h"
namespace lld {
namespace elf {
/// \brief TargetLayout for Mips
template <class ELFT> class MipsTargetLayout final : public TargetLayout<ELFT> {
public:
MipsTargetLayout(MipsLinkingContext &ctx)
: TargetLayout<ELFT>(ctx),
_gotSection(new (this->_allocator) MipsGOTSection<ELFT>(ctx)),
_pltSection(new (this->_allocator) MipsPLTSection<ELFT>(ctx)) {}
const MipsGOTSection<ELFT> &getGOTSection() const { return *_gotSection; }
const MipsPLTSection<ELFT> &getPLTSection() const { return *_pltSection; }
AtomSection<ELFT> *createSection(StringRef name, int32_t type,
DefinedAtom::ContentPermissions permissions,
Layout::SectionOrder order) override {
if (type == DefinedAtom::typeGOT && name == ".got")
return _gotSection;
if (type == DefinedAtom::typeStub && name == ".plt")
return _pltSection;
return DefaultLayout<ELFT>::createSection(name, type, permissions, order);
}
/// \brief GP offset relative to .got section.
uint64_t getGPOffset() const { return 0x7FF0; }
/// \brief Get '_gp' symbol atom layout.
AtomLayout *getGP() {
if (!_gpAtom.hasValue()) {
auto atom = this->findAbsoluteAtom("_gp");
_gpAtom = atom != this->absoluteAtoms().end() ? *atom : nullptr;
}
return *_gpAtom;
}
/// \brief Get '_gp_disp' symbol atom layout.
AtomLayout *getGPDisp() {
if (!_gpDispAtom.hasValue()) {
auto atom = this->findAbsoluteAtom("_gp_disp");
_gpDispAtom = atom != this->absoluteAtoms().end() ? *atom : nullptr;
}
return *_gpDispAtom;
}
/// \brief Return the section order for a input section
Layout::SectionOrder getSectionOrder(StringRef name, int32_t contentType,
int32_t contentPermissions) override {
if ((contentType == DefinedAtom::typeStub) && (name.startswith(".text")))
return DefaultLayout<ELFT>::ORDER_TEXT;
return DefaultLayout<ELFT>::getSectionOrder(name, contentType,
contentPermissions);
}
protected:
unique_bump_ptr<RelocationTable<ELFT>>
createRelocationTable(StringRef name, int32_t order) override {
return unique_bump_ptr<RelocationTable<ELFT>>(new (
this->_allocator) MipsRelocationTable<ELFT>(this->_ctx, name, order));
}
private:
MipsGOTSection<ELFT> *_gotSection;
MipsPLTSection<ELFT> *_pltSection;
llvm::Optional<AtomLayout *> _gpAtom;
llvm::Optional<AtomLayout *> _gpDispAtom;
};
/// \brief Mips Runtime file.
template <class ELFT> class MipsRuntimeFile final : public RuntimeFile<ELFT> {
public:
MipsRuntimeFile(MipsLinkingContext &ctx)
: RuntimeFile<ELFT>(ctx, "Mips runtime file") {}
};
/// \brief Auxiliary class holds relocation's names table.
class MipsRelocationStringTable {
static const Registry::KindStrings kindStrings[];
public:
static void registerTable(Registry ®istry);
};
/// \brief TargetHandler for Mips
template <class ELFT>
class MipsTargetHandler final : public DefaultTargetHandler<ELFT> {
public:
MipsTargetHandler(MipsLinkingContext &ctx)
: _ctx(ctx), _runtimeFile(new MipsRuntimeFile<ELFT>(ctx)),
_targetLayout(new MipsTargetLayout<ELFT>(ctx)),
_relocationHandler(createMipsRelocationHandler<ELFT>(ctx)) {}
MipsTargetLayout<ELFT> &getTargetLayout() override { return *_targetLayout; }
std::unique_ptr<Reader> getObjReader() override {
return llvm::make_unique<MipsELFObjectReader<ELFT>>(_ctx);
}
std::unique_ptr<Reader> getDSOReader() override {
return llvm::make_unique<MipsELFDSOReader<ELFT>>(_ctx);
}
const TargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}
std::unique_ptr<Writer> getWriter() override {
switch (_ctx.getOutputELFType()) {
case llvm::ELF::ET_EXEC:
return llvm::make_unique<MipsExecutableWriter<ELFT>>(
_ctx, *_targetLayout);
case llvm::ELF::ET_DYN:
return llvm::make_unique<MipsDynamicLibraryWriter<ELFT>>(
_ctx, *_targetLayout);
case llvm::ELF::ET_REL:
llvm_unreachable("TODO: support -r mode");
default:
llvm_unreachable("unsupported output type");
}
}
void registerRelocationNames(Registry ®istry) override {
MipsRelocationStringTable::registerTable(registry);
}
private:
MipsLinkingContext &_ctx;
std::unique_ptr<MipsRuntimeFile<ELFT>> _runtimeFile;
std::unique_ptr<MipsTargetLayout<ELFT>> _targetLayout;
std::unique_ptr<TargetRelocationHandler> _relocationHandler;
};
template <class ELFT> class MipsSymbolTable : public SymbolTable<ELFT> {
public:
typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
MipsSymbolTable(const ELFLinkingContext &ctx)
: SymbolTable<ELFT>(ctx, ".symtab",
DefaultLayout<ELFT>::ORDER_SYMBOL_TABLE) {}
void addDefinedAtom(Elf_Sym &sym, const DefinedAtom *da,
int64_t addr) override {
SymbolTable<ELFT>::addDefinedAtom(sym, da, addr);
switch (da->codeModel()) {
case DefinedAtom::codeMipsMicro:
sym.st_other |= llvm::ELF::STO_MIPS_MICROMIPS;
break;
case DefinedAtom::codeMipsMicroPIC:
sym.st_other |= llvm::ELF::STO_MIPS_MICROMIPS | llvm::ELF::STO_MIPS_PIC;
break;
default:
break;
}
}
void finalize(bool sort) override {
SymbolTable<ELFT>::finalize(sort);
for (auto &ste : this->_symbolTable) {
if (!ste._atom)
continue;
if (const auto *da = dyn_cast<DefinedAtom>(ste._atom)) {
if (da->codeModel() == DefinedAtom::codeMipsMicro ||
da->codeModel() == DefinedAtom::codeMipsMicroPIC) {
// Adjust dynamic microMIPS symbol value. That allows a dynamic
// linker to recognize and handle this symbol correctly.
ste._symbol.st_value = ste._symbol.st_value | 1;
}
}
}
}
};
template <class ELFT>
class MipsDynamicSymbolTable : public DynamicSymbolTable<ELFT> {
public:
MipsDynamicSymbolTable(const ELFLinkingContext &ctx,
MipsTargetLayout<ELFT> &layout)
: DynamicSymbolTable<ELFT>(ctx, layout, ".dynsym",
DefaultLayout<ELFT>::ORDER_DYNAMIC_SYMBOLS),
_targetLayout(layout) {}
void sortSymbols() override {
typedef typename DynamicSymbolTable<ELFT>::SymbolEntry SymbolEntry;
std::stable_sort(this->_symbolTable.begin(), this->_symbolTable.end(),
[this](const SymbolEntry &A, const SymbolEntry &B) {
if (A._symbol.getBinding() != STB_GLOBAL &&
B._symbol.getBinding() != STB_GLOBAL)
return A._symbol.getBinding() < B._symbol.getBinding();
return _targetLayout.getGOTSection().compare(A._atom, B._atom);
});
}
void finalize() override {
DynamicSymbolTable<ELFT>::finalize();
const auto &pltSection = _targetLayout.getPLTSection();
for (auto &ste : this->_symbolTable) {
const Atom *a = ste._atom;
if (!a)
continue;
if (auto *layout = pltSection.findPLTLayout(a)) {
a = layout->_atom;
// Under some conditions a dynamic symbol table record should hold
// a symbol value of the corresponding PLT entry. For details look
// at the PLT entry creation code in the class MipsRelocationPass.
// Let's update atomLayout fields for such symbols.
assert(!ste._atomLayout);
ste._symbol.st_value = layout->_virtualAddr;
ste._symbol.st_other |= ELF::STO_MIPS_PLT;
}
if (const auto *da = dyn_cast<DefinedAtom>(a)) {
if (da->codeModel() == DefinedAtom::codeMipsMicro ||
da->codeModel() == DefinedAtom::codeMipsMicroPIC) {
// Adjust dynamic microMIPS symbol value. That allows a dynamic
// linker to recognize and handle this symbol correctly.
ste._symbol.st_value = ste._symbol.st_value | 1;
}
}
}
}
private:
MipsTargetLayout<ELFT> &_targetLayout;
};
} // end namespace elf
} // end namespace lld
#endif
|