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
|
//===--------- lib/ReaderWriter/ELF/ARM/ARMTargetHandler.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_ARM_ARM_TARGET_HANDLER_H
#define LLD_READER_WRITER_ELF_ARM_ARM_TARGET_HANDLER_H
#include "ARMELFFile.h"
#include "ARMRelocationHandler.h"
#include "ELFReader.h"
#include "TargetLayout.h"
namespace lld {
class ELFLinkingContext;
namespace elf {
/// \brief ARM specific section (.ARM.exidx) with indexes to exception handlers
class ARMExidxSection : public AtomSection<ELF32LE> {
typedef AtomSection<ELF32LE> Base;
public:
ARMExidxSection(const ELFLinkingContext &ctx, StringRef sectionName,
int32_t permissions, int32_t order)
: Base(ctx, sectionName, DefinedAtom::typeARMExidx, permissions, order) {
this->_type = SHT_ARM_EXIDX;
this->_isLoadedInMemory = true;
}
bool hasOutputSegment() const override { return true; }
const AtomLayout *appendAtom(const Atom *atom) override {
const DefinedAtom *definedAtom = cast<DefinedAtom>(atom);
assert(definedAtom->contentType() == DefinedAtom::typeARMExidx &&
"atom content type for .ARM.exidx section has to be typeARMExidx");
DefinedAtom::Alignment atomAlign = definedAtom->alignment();
uint64_t fOffset = alignOffset(this->fileSize(), atomAlign);
uint64_t mOffset = alignOffset(this->memSize(), atomAlign);
_atoms.push_back(new (_alloc) AtomLayout(atom, fOffset, 0));
this->_fsize = fOffset + definedAtom->size();
this->_msize = mOffset + definedAtom->size();
DEBUG_WITH_TYPE("Section", llvm::dbgs()
<< "[" << this->name() << " " << this << "] "
<< "Adding atom: " << atom->name() << "@"
<< fOffset << "\n");
uint64_t alignment = atomAlign.value;
if (this->_alignment < alignment)
this->_alignment = alignment;
return _atoms.back();
}
};
class ARMTargetLayout : public TargetLayout<ELF32LE> {
public:
ARMTargetLayout(ELFLinkingContext &ctx) : TargetLayout(ctx) {}
SectionOrder getSectionOrder(StringRef name, int32_t contentType,
int32_t contentPermissions) override {
switch (contentType) {
case DefinedAtom::typeARMExidx:
return ORDER_ARM_EXIDX;
default:
return TargetLayout::getSectionOrder(name, contentType,
contentPermissions);
}
}
StringRef getOutputSectionName(StringRef archivePath, StringRef memberPath,
StringRef inputSectionName) const override {
return llvm::StringSwitch<StringRef>(inputSectionName)
.StartsWith(".ARM.exidx", ".ARM.exidx")
.StartsWith(".ARM.extab", ".ARM.extab")
.Default(TargetLayout::getOutputSectionName(archivePath, memberPath,
inputSectionName));
}
SegmentType getSegmentType(Section<ELF32LE> *section) const override {
switch (section->order()) {
case ORDER_ARM_EXIDX:
return llvm::ELF::PT_ARM_EXIDX;
default:
return TargetLayout::getSegmentType(section);
}
}
AtomSection<ELF32LE> *
createSection(StringRef name, int32_t contentType,
DefinedAtom::ContentPermissions contentPermissions,
SectionOrder sectionOrder) override {
if (contentType == DefinedAtom::typeARMExidx)
return new ARMExidxSection(_ctx, name, contentPermissions, sectionOrder);
return TargetLayout::createSection(name, contentType, contentPermissions,
sectionOrder);
}
uint64_t getGOTSymAddr() {
std::call_once(_gotSymOnce, [this]() {
if (AtomLayout *gotAtom = findAbsoluteAtom("_GLOBAL_OFFSET_TABLE_"))
_gotSymAddr = gotAtom->_virtualAddr;
});
return _gotSymAddr;
}
uint64_t getTPOffset() {
std::call_once(_tpOffOnce, [this]() {
for (const auto &phdr : *_programHeader) {
if (phdr->p_type == llvm::ELF::PT_TLS) {
_tpOff = llvm::RoundUpToAlignment(TCB_SIZE, phdr->p_align);
break;
}
}
assert(_tpOff != 0 && "TLS segment not found");
});
return _tpOff;
}
bool target1Rel() const { return _ctx.armTarget1Rel(); }
private:
// TCB block size of the TLS.
enum { TCB_SIZE = 0x8 };
private:
uint64_t _gotSymAddr = 0;
uint64_t _tpOff = 0;
std::once_flag _gotSymOnce;
std::once_flag _tpOffOnce;
};
class ARMTargetHandler final : public TargetHandler {
public:
ARMTargetHandler(ARMLinkingContext &ctx);
const TargetRelocationHandler &getRelocationHandler() const override {
return *_relocationHandler;
}
std::unique_ptr<Reader> getObjReader() override {
return llvm::make_unique<ELFReader<ARMELFFile>>(_ctx);
}
std::unique_ptr<Reader> getDSOReader() override {
return llvm::make_unique<ELFReader<DynamicFile<ELF32LE>>>(_ctx);
}
std::unique_ptr<Writer> getWriter() override;
private:
ARMLinkingContext &_ctx;
std::unique_ptr<ARMTargetLayout> _targetLayout;
std::unique_ptr<ARMTargetRelocationHandler> _relocationHandler;
};
} // end namespace elf
} // end namespace lld
#endif // LLD_READER_WRITER_ELF_ARM_ARM_TARGET_HANDLER_H
|