/src/keystone/llvm/lib/MC/ELFObjectWriter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements ELF object file writer information. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "llvm/MC/MCELFObjectWriter.h" |
15 | | #include "llvm/ADT/STLExtras.h" |
16 | | #include "llvm/ADT/SmallPtrSet.h" |
17 | | #include "llvm/ADT/SmallString.h" |
18 | | #include "llvm/ADT/StringMap.h" |
19 | | #include "llvm/MC/MCAsmBackend.h" |
20 | | #include "llvm/MC/MCAsmInfo.h" |
21 | | #include "llvm/MC/MCAsmLayout.h" |
22 | | #include "llvm/MC/MCAssembler.h" |
23 | | #include "llvm/MC/MCContext.h" |
24 | | #include "llvm/MC/MCExpr.h" |
25 | | #include "llvm/MC/MCFixupKindInfo.h" |
26 | | #include "llvm/MC/MCObjectWriter.h" |
27 | | #include "llvm/MC/MCSectionELF.h" |
28 | | #include "llvm/MC/MCSymbolELF.h" |
29 | | #include "llvm/MC/MCValue.h" |
30 | | #include "llvm/MC/StringTableBuilder.h" |
31 | | #include "llvm/Support/Debug.h" |
32 | | #include "llvm/Support/ELF.h" |
33 | | #include "llvm/Support/Endian.h" |
34 | | #include "llvm/Support/ErrorHandling.h" |
35 | | #include "llvm/Support/StringSaver.h" |
36 | | #include <vector> |
37 | | using namespace llvm_ks; |
38 | | |
39 | | #undef DEBUG_TYPE |
40 | | #define DEBUG_TYPE "reloc-info" |
41 | | |
42 | | namespace { |
43 | | |
44 | | typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy; |
45 | | |
46 | | class ELFObjectWriter; |
47 | | |
48 | | class SymbolTableWriter { |
49 | | ELFObjectWriter &EWriter; |
50 | | bool Is64Bit; |
51 | | |
52 | | // indexes we are going to write to .symtab_shndx. |
53 | | std::vector<uint32_t> ShndxIndexes; |
54 | | |
55 | | // The numbel of symbols written so far. |
56 | | unsigned NumWritten; |
57 | | |
58 | | void createSymtabShndx(); |
59 | | |
60 | | template <typename T> void write(T Value); |
61 | | |
62 | | public: |
63 | | SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit); |
64 | | |
65 | | void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size, |
66 | | uint8_t other, uint32_t shndx, bool Reserved); |
67 | | |
68 | 0 | ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; } |
69 | | }; |
70 | | |
71 | | class ELFObjectWriter : public MCObjectWriter { |
72 | | static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout); |
73 | | static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol, |
74 | | bool Used, bool Renamed); |
75 | | |
76 | | /// Helper struct for containing some precomputed information on symbols. |
77 | | struct ELFSymbolData { |
78 | | const MCSymbolELF *Symbol; |
79 | | uint32_t SectionIndex; |
80 | | StringRef Name; |
81 | | |
82 | | // Support lexicographic sorting. |
83 | 0 | bool operator<(const ELFSymbolData &RHS) const { |
84 | 0 | unsigned LHSType = Symbol->getType(); |
85 | 0 | unsigned RHSType = RHS.Symbol->getType(); |
86 | 0 | if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION) |
87 | 0 | return false; |
88 | 0 | if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
89 | 0 | return true; |
90 | 0 | if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
91 | 0 | return SectionIndex < RHS.SectionIndex; |
92 | 0 | return Name < RHS.Name; |
93 | 0 | } |
94 | | }; |
95 | | |
96 | | /// The target specific ELF writer instance. |
97 | | std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter; |
98 | | |
99 | | DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames; |
100 | | |
101 | | llvm_ks::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> |
102 | | Relocations; |
103 | | |
104 | | /// @} |
105 | | /// @name Symbol Table Data |
106 | | /// @{ |
107 | | |
108 | | BumpPtrAllocator Alloc; |
109 | | StringSaver VersionSymSaver{Alloc}; |
110 | | StringTableBuilder StrTabBuilder{StringTableBuilder::ELF}; |
111 | | |
112 | | /// @} |
113 | | |
114 | | // This holds the symbol table index of the last local symbol. |
115 | | unsigned LastLocalSymbolIndex; |
116 | | // This holds the .strtab section index. |
117 | | unsigned StringTableIndex; |
118 | | // This holds the .symtab section index. |
119 | | unsigned SymbolTableIndex; |
120 | | |
121 | | // Sections in the order they are to be output in the section table. |
122 | | std::vector<const MCSectionELF *> SectionTable; |
123 | | unsigned addToSectionTable(const MCSectionELF *Sec); |
124 | | |
125 | | // TargetObjectWriter wrappers. |
126 | 2.67k | bool is64Bit() const { return TargetObjectWriter->is64Bit(); } |
127 | 83.7k | bool hasRelocationAddend() const { |
128 | | // Keystone doesn't want relocation addends. |
129 | | /* return TargetObjectWriter->hasRelocationAddend(); */ |
130 | 83.7k | return false; |
131 | 83.7k | } |
132 | | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
133 | 79.7k | const MCFixup &Fixup, bool IsPCRel) const { |
134 | 79.7k | return TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel); |
135 | 79.7k | } |
136 | | |
137 | | void align(unsigned Alignment); |
138 | | |
139 | | public: |
140 | | ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS, |
141 | | bool IsLittleEndian) |
142 | 129k | : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {} |
143 | | |
144 | 0 | void reset() override { |
145 | 0 | Renames.clear(); |
146 | 0 | Relocations.clear(); |
147 | 0 | StrTabBuilder.clear(); |
148 | 0 | SectionTable.clear(); |
149 | 0 | MCObjectWriter::reset(); |
150 | 0 | } |
151 | | |
152 | | ~ELFObjectWriter() override; |
153 | | |
154 | 0 | void WriteWord(uint64_t W) { |
155 | 0 | if (is64Bit()) |
156 | 0 | write64(W); |
157 | 0 | else |
158 | 0 | write32(W); |
159 | 0 | } |
160 | | |
161 | 0 | template <typename T> void write(T Val) { |
162 | 0 | if (IsLittleEndian) |
163 | 0 | support::endian::Writer<support::little>(getStream()).write(Val); |
164 | 0 | else |
165 | 0 | support::endian::Writer<support::big>(getStream()).write(Val); |
166 | 0 | } Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::ELFObjectWriter::write<unsigned short>(unsigned short) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::ELFObjectWriter::write<unsigned int>(unsigned int) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::ELFObjectWriter::write<unsigned long>(unsigned long) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::ELFObjectWriter::write<unsigned char>(unsigned char) |
167 | | |
168 | | void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, |
169 | | ELFSymbolData &MSD, const MCAsmLayout &Layout); |
170 | | |
171 | | // Start and end offset of each section |
172 | | typedef std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>> |
173 | | SectionOffsetsTy; |
174 | | |
175 | | bool shouldRelocateWithSymbol(const MCAssembler &Asm, |
176 | | const MCSymbolRefExpr *RefA, |
177 | | const MCSymbol *Sym, uint64_t C, |
178 | | unsigned Type) const; |
179 | | |
180 | | void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
181 | | const MCFragment *Fragment, const MCFixup &Fixup, |
182 | | MCValue Target, bool &IsPCRel, |
183 | | uint64_t &FixedValue) override; |
184 | | |
185 | | // Map from a signature symbol to the group section index |
186 | | typedef DenseMap<const MCSymbol *, unsigned> RevGroupMapTy; |
187 | | |
188 | | /// Compute the symbol table data |
189 | | /// |
190 | | /// \param Asm - The assembler. |
191 | | /// \param SectionIndexMap - Maps a section to its index. |
192 | | /// \param RevGroupMap - Maps a signature symbol to the group section. |
193 | | void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, |
194 | | const SectionIndexMapTy &SectionIndexMap, |
195 | | const RevGroupMapTy &RevGroupMap, |
196 | | SectionOffsetsTy &SectionOffsets); |
197 | | |
198 | | MCSectionELF *createRelocationSection(MCContext &Ctx, |
199 | | const MCSectionELF &Sec); |
200 | | |
201 | | const MCSectionELF *createStringTable(MCContext &Ctx); |
202 | | |
203 | | void executePostLayoutBinding(MCAssembler &Asm, |
204 | | const MCAsmLayout &Layout) override; |
205 | | |
206 | | void writeSectionHeader(const MCAsmLayout &Layout, |
207 | | const SectionIndexMapTy &SectionIndexMap, |
208 | | const SectionOffsetsTy &SectionOffsets); |
209 | | |
210 | | void writeSectionData(const MCAssembler &Asm, MCSection &Sec, |
211 | | const MCAsmLayout &Layout); |
212 | | |
213 | | void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
214 | | uint64_t Address, uint64_t Offset, uint64_t Size, |
215 | | uint32_t Link, uint32_t Info, uint64_t Alignment, |
216 | | uint64_t EntrySize); |
217 | | |
218 | | void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec); |
219 | | |
220 | | bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
221 | | const MCSymbol &SymA, |
222 | | const MCFragment &FB, |
223 | | bool InSet, |
224 | | bool IsPCRel) const override; |
225 | | |
226 | | bool isWeak(const MCSymbol &Sym) const override; |
227 | | |
228 | | void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; |
229 | | void writeSection(const SectionIndexMapTy &SectionIndexMap, |
230 | | uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, |
231 | | const MCSectionELF &Section); |
232 | | }; |
233 | | } |
234 | | |
235 | 53.2k | void ELFObjectWriter::align(unsigned Alignment) { |
236 | 53.2k | uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment); |
237 | 53.2k | WriteZeros(Padding); |
238 | 53.2k | } |
239 | | |
240 | 103k | unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) { |
241 | 103k | SectionTable.push_back(Sec); |
242 | 103k | StrTabBuilder.add(Sec->getSectionName()); |
243 | 103k | return SectionTable.size(); |
244 | 103k | } |
245 | | |
246 | 0 | void SymbolTableWriter::createSymtabShndx() { |
247 | 0 | if (!ShndxIndexes.empty()) |
248 | 0 | return; |
249 | 0 |
|
250 | 0 | ShndxIndexes.resize(NumWritten); |
251 | 0 | } |
252 | | |
253 | 0 | template <typename T> void SymbolTableWriter::write(T Value) { |
254 | 0 | EWriter.write(Value); |
255 | 0 | } Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::SymbolTableWriter::write<unsigned int>(unsigned int) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::SymbolTableWriter::write<unsigned char>(unsigned char) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::SymbolTableWriter::write<unsigned short>(unsigned short) Unexecuted instantiation: ELFObjectWriter.cpp:void (anonymous namespace)::SymbolTableWriter::write<unsigned long>(unsigned long) |
256 | | |
257 | | SymbolTableWriter::SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit) |
258 | | : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {} |
259 | | |
260 | | void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value, |
261 | | uint64_t size, uint8_t other, |
262 | 0 | uint32_t shndx, bool Reserved) { |
263 | 0 | bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved; |
264 | 0 |
|
265 | 0 | if (LargeIndex) |
266 | 0 | createSymtabShndx(); |
267 | 0 |
|
268 | 0 | if (!ShndxIndexes.empty()) { |
269 | 0 | if (LargeIndex) |
270 | 0 | ShndxIndexes.push_back(shndx); |
271 | 0 | else |
272 | 0 | ShndxIndexes.push_back(0); |
273 | 0 | } |
274 | 0 |
|
275 | 0 | uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx; |
276 | 0 |
|
277 | 0 | if (Is64Bit) { |
278 | 0 | write(name); // st_name |
279 | 0 | write(info); // st_info |
280 | 0 | write(other); // st_other |
281 | 0 | write(Index); // st_shndx |
282 | 0 | write(value); // st_value |
283 | 0 | write(size); // st_size |
284 | 0 | } else { |
285 | 0 | write(name); // st_name |
286 | 0 | write(uint32_t(value)); // st_value |
287 | 0 | write(uint32_t(size)); // st_size |
288 | 0 | write(info); // st_info |
289 | 0 | write(other); // st_other |
290 | 0 | write(Index); // st_shndx |
291 | 0 | } |
292 | 0 |
|
293 | 0 | ++NumWritten; |
294 | 0 | } |
295 | | |
296 | | ELFObjectWriter::~ELFObjectWriter() |
297 | 129k | {} |
298 | | |
299 | | uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym, |
300 | 0 | const MCAsmLayout &Layout) { |
301 | 0 | if (Sym.isCommon() && Sym.isExternal()) |
302 | 0 | return Sym.getCommonAlignment(); |
303 | 0 |
|
304 | 0 | uint64_t Res; |
305 | 0 | bool valid; |
306 | 0 | if (!Layout.getSymbolOffset(Sym, Res, valid)) |
307 | 0 | return 0; |
308 | 0 |
|
309 | 0 | if (Layout.getAssembler().isThumbFunc(&Sym)) |
310 | 0 | Res |= 1; |
311 | 0 |
|
312 | 0 | return Res; |
313 | 0 | } |
314 | | |
315 | | void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
316 | 56.5k | const MCAsmLayout &Layout) { |
317 | | // The presence of symbol versions causes undefined symbols and |
318 | | // versions declared with @@@ to be renamed. |
319 | | |
320 | 667k | for (const MCSymbol &A : Asm.symbols()) { |
321 | 667k | const auto &Alias = cast<MCSymbolELF>(A); |
322 | | // Not an alias. |
323 | 667k | if (!Alias.isVariable()) |
324 | 654k | continue; |
325 | 12.8k | auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue()); |
326 | 12.8k | if (!Ref) |
327 | 5.65k | continue; |
328 | 7.15k | const auto &Symbol = cast<MCSymbolELF>(Ref->getSymbol()); |
329 | | |
330 | 7.15k | StringRef AliasName = Alias.getName(); |
331 | 7.15k | size_t Pos = AliasName.find('@'); |
332 | 7.15k | if (Pos == StringRef::npos) |
333 | 3.24k | continue; |
334 | | |
335 | | // Aliases defined with .symvar copy the binding from the symbol they alias. |
336 | | // This is the first place we are able to copy this information. |
337 | 3.90k | Alias.setExternal(Symbol.isExternal()); |
338 | 3.90k | Alias.setBinding(Symbol.getBinding()); |
339 | | |
340 | 3.90k | StringRef Rest = AliasName.substr(Pos); |
341 | 3.90k | if (!Symbol.isUndefined() && !Rest.startswith("@@@")) |
342 | 630 | continue; |
343 | | |
344 | | // FIXME: produce a better error message. |
345 | 3.27k | if (Symbol.isUndefined() && Rest.startswith("@@") && |
346 | 3.27k | !Rest.startswith("@@@")) |
347 | 0 | report_fatal_error("A @@ version cannot be undefined"); |
348 | | |
349 | 3.27k | Renames.insert(std::make_pair(&Symbol, &Alias)); |
350 | 3.27k | } |
351 | 56.5k | } |
352 | | |
353 | 0 | static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) { |
354 | 0 | uint8_t Type = newType; |
355 | 0 |
|
356 | 0 | // Propagation rules: |
357 | 0 | // IFUNC > FUNC > OBJECT > NOTYPE |
358 | 0 | // TLS_OBJECT > OBJECT > NOTYPE |
359 | 0 | // |
360 | 0 | // dont let the new type degrade the old type |
361 | 0 | switch (origType) { |
362 | 0 | default: |
363 | 0 | break; |
364 | 0 | case ELF::STT_GNU_IFUNC: |
365 | 0 | if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT || |
366 | 0 | Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS) |
367 | 0 | Type = ELF::STT_GNU_IFUNC; |
368 | 0 | break; |
369 | 0 | case ELF::STT_FUNC: |
370 | 0 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
371 | 0 | Type == ELF::STT_TLS) |
372 | 0 | Type = ELF::STT_FUNC; |
373 | 0 | break; |
374 | 0 | case ELF::STT_OBJECT: |
375 | 0 | if (Type == ELF::STT_NOTYPE) |
376 | 0 | Type = ELF::STT_OBJECT; |
377 | 0 | break; |
378 | 0 | case ELF::STT_TLS: |
379 | 0 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
380 | 0 | Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC) |
381 | 0 | Type = ELF::STT_TLS; |
382 | 0 | break; |
383 | 0 | } |
384 | 0 |
|
385 | 0 | return Type; |
386 | 0 | } |
387 | | |
388 | | void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer, |
389 | | uint32_t StringIndex, ELFSymbolData &MSD, |
390 | 0 | const MCAsmLayout &Layout) { |
391 | 0 | const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol); |
392 | 0 | const MCSymbolELF *Base = |
393 | 0 | cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol)); |
394 | 0 |
|
395 | 0 | // This has to be in sync with when computeSymbolTable uses SHN_ABS or |
396 | 0 | // SHN_COMMON. |
397 | 0 | bool IsReserved = !Base || Symbol.isCommon(); |
398 | 0 |
|
399 | 0 | // Binding and Type share the same byte as upper and lower nibbles |
400 | 0 | uint8_t Binding = Symbol.getBinding(); |
401 | 0 | uint8_t Type = Symbol.getType(); |
402 | 0 | if (Base) { |
403 | 0 | Type = mergeTypeForSet(Type, Base->getType()); |
404 | 0 | } |
405 | 0 | uint8_t Info = (Binding << 4) | Type; |
406 | 0 |
|
407 | 0 | // Other and Visibility share the same byte with Visibility using the lower |
408 | 0 | // 2 bits |
409 | 0 | uint8_t Visibility = Symbol.getVisibility(); |
410 | 0 | uint8_t Other = Symbol.getOther() | Visibility; |
411 | 0 |
|
412 | 0 | uint64_t Value = SymbolValue(*MSD.Symbol, Layout); |
413 | 0 | uint64_t Size = 0; |
414 | 0 |
|
415 | 0 | const MCExpr *ESize = MSD.Symbol->getSize(); |
416 | 0 | if (!ESize && Base) |
417 | 0 | ESize = Base->getSize(); |
418 | 0 |
|
419 | 0 | if (ESize) { |
420 | 0 | int64_t Res; |
421 | 0 | if (!ESize->evaluateKnownAbsolute(Res, Layout)) |
422 | 0 | report_fatal_error("Size expression must be absolute."); |
423 | 0 | Size = Res; |
424 | 0 | } |
425 | 0 |
|
426 | 0 | // Write out the symbol table entry |
427 | 0 | Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex, |
428 | 0 | IsReserved); |
429 | 0 | } |
430 | | |
431 | | // It is always valid to create a relocation with a symbol. It is preferable |
432 | | // to use a relocation with a section if that is possible. Using the section |
433 | | // allows us to omit some local symbols from the symbol table. |
434 | | bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm, |
435 | | const MCSymbolRefExpr *RefA, |
436 | | const MCSymbol *S, uint64_t C, |
437 | 79.7k | unsigned Type) const { |
438 | 79.7k | const auto *Sym = cast_or_null<MCSymbolELF>(S); |
439 | | // A PCRel relocation to an absolute value has no symbol (or section). We |
440 | | // represent that with a relocation to a null section. |
441 | 79.7k | if (!RefA) |
442 | 9.47k | return false; |
443 | | |
444 | 70.2k | MCSymbolRefExpr::VariantKind Kind = RefA->getKind(); |
445 | 70.2k | switch (Kind) { |
446 | 70.1k | default: |
447 | 70.1k | break; |
448 | | // The .odp creation emits a relocation against the symbol ".TOC." which |
449 | | // create a R_PPC64_TOC relocation. However the relocation symbol name |
450 | | // in final object creation should be NULL, since the symbol does not |
451 | | // really exist, it is just the reference to TOC base for the current |
452 | | // object file. Since the symbol is undefined, returning false results |
453 | | // in a relocation with a null section which is the desired result. |
454 | 70.1k | case MCSymbolRefExpr::VK_PPC_TOCBASE: |
455 | 0 | return false; |
456 | | |
457 | | // These VariantKind cause the relocation to refer to something other than |
458 | | // the symbol itself, like a linker generated table. Since the address of |
459 | | // symbol is not relevant, we cannot replace the symbol with the |
460 | | // section and patch the difference in the addend. |
461 | 108 | case MCSymbolRefExpr::VK_GOT: |
462 | 108 | case MCSymbolRefExpr::VK_PLT: |
463 | 108 | case MCSymbolRefExpr::VK_GOTPCREL: |
464 | 108 | case MCSymbolRefExpr::VK_Mips_GOT: |
465 | 108 | case MCSymbolRefExpr::VK_PPC_GOT_LO: |
466 | 108 | case MCSymbolRefExpr::VK_PPC_GOT_HI: |
467 | 108 | case MCSymbolRefExpr::VK_PPC_GOT_HA: |
468 | 108 | return true; |
469 | 70.2k | } |
470 | | |
471 | | // An undefined symbol is not in any section, so the relocation has to point |
472 | | // to the symbol itself. |
473 | 70.1k | assert(Sym && "Expected a symbol"); |
474 | 70.1k | if (Sym->isUndefined()) |
475 | 0 | return true; |
476 | | |
477 | 70.1k | unsigned Binding = Sym->getBinding(); |
478 | 70.1k | switch(Binding) { |
479 | 0 | default: |
480 | 0 | llvm_unreachable("Invalid Binding"); |
481 | 70.1k | case ELF::STB_LOCAL: |
482 | 70.1k | break; |
483 | 0 | case ELF::STB_WEAK: |
484 | | // If the symbol is weak, it might be overridden by a symbol in another |
485 | | // file. The relocation has to point to the symbol so that the linker |
486 | | // can update it. |
487 | 0 | return true; |
488 | 0 | case ELF::STB_GLOBAL: |
489 | | // Global ELF symbols can be preempted by the dynamic linker. The relocation |
490 | | // has to point to the symbol for a reason analogous to the STB_WEAK case. |
491 | 0 | return true; |
492 | 70.1k | } |
493 | | |
494 | | // If a relocation points to a mergeable section, we have to be careful. |
495 | | // If the offset is zero, a relocation with the section will encode the |
496 | | // same information. With a non-zero offset, the situation is different. |
497 | | // For example, a relocation can point 42 bytes past the end of a string. |
498 | | // If we change such a relocation to use the section, the linker would think |
499 | | // that it pointed to another string and subtracting 42 at runtime will |
500 | | // produce the wrong value. |
501 | 70.1k | auto &Sec = cast<MCSectionELF>(Sym->getSection()); |
502 | 70.1k | unsigned Flags = Sec.getFlags(); |
503 | 70.1k | if (Flags & ELF::SHF_MERGE) { |
504 | 0 | if (C != 0) |
505 | 0 | return true; |
506 | | |
507 | | // It looks like gold has a bug (http://sourceware.org/PR16794) and can |
508 | | // only handle section relocations to mergeable sections if using RELA. |
509 | 0 | if (!hasRelocationAddend()) |
510 | 0 | return true; |
511 | 0 | } |
512 | | |
513 | | // Most TLS relocations use a got, so they need the symbol. Even those that |
514 | | // are just an offset (@tpoff), require a symbol in gold versions before |
515 | | // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed |
516 | | // http://sourceware.org/PR16773. |
517 | 70.1k | if (Flags & ELF::SHF_TLS) |
518 | 0 | return true; |
519 | | |
520 | | // If the symbol is a thumb function the final relocation must set the lowest |
521 | | // bit. With a symbol that is done by just having the symbol have that bit |
522 | | // set, so we would lose the bit if we relocated with the section. |
523 | | // FIXME: We could use the section but add the bit to the relocation value. |
524 | 70.1k | if (Asm.isThumbFunc(Sym)) |
525 | 0 | return true; |
526 | | |
527 | 70.1k | if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type)) |
528 | 10.0k | return true; |
529 | 60.1k | return false; |
530 | 70.1k | } |
531 | | |
532 | | // True if the assembler knows nothing about the final value of the symbol. |
533 | | // This doesn't cover the comdat issues, since in those cases the assembler |
534 | | // can at least know that all symbols in the section will move together. |
535 | 22.7k | static bool isWeak(const MCSymbolELF &Sym) { |
536 | 22.7k | if (Sym.getType() == ELF::STT_GNU_IFUNC) |
537 | 0 | return true; |
538 | | |
539 | 22.7k | switch (Sym.getBinding()) { |
540 | 0 | default: |
541 | 0 | llvm_unreachable("Unknown binding"); |
542 | 22.7k | case ELF::STB_LOCAL: |
543 | 22.7k | return false; |
544 | 0 | case ELF::STB_GLOBAL: |
545 | 0 | return false; |
546 | 0 | case ELF::STB_WEAK: |
547 | 0 | case ELF::STB_GNU_UNIQUE: |
548 | 0 | return true; |
549 | 22.7k | } |
550 | 22.7k | } |
551 | | |
552 | | void ELFObjectWriter::recordRelocation(MCAssembler &Asm, |
553 | | const MCAsmLayout &Layout, |
554 | | const MCFragment *Fragment, |
555 | | const MCFixup &Fixup, MCValue Target, |
556 | | bool &IsPCRel, uint64_t &FixedValue) |
557 | 140k | { |
558 | 140k | const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent()); |
559 | 140k | uint64_t C = Target.getConstant(); |
560 | 140k | bool valid; |
561 | 140k | uint64_t FixupOffset = Layout.getFragmentOffset(Fragment, valid) + Fixup.getOffset(); |
562 | 140k | MCContext &Ctx = Asm.getContext(); |
563 | | |
564 | 140k | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
565 | 68.3k | assert(RefB->getKind() == MCSymbolRefExpr::VK_None && |
566 | 68.3k | "Should not have constructed this"); |
567 | | |
568 | | // Let A, B and C being the components of Target and R be the location of |
569 | | // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). |
570 | | // If it is pcrel, we want to compute (A - B + C - R). |
571 | | |
572 | | // In general, ELF has no relocations for -B. It can only represent (A + C) |
573 | | // or (A + C - R). If B = R + K and the relocation is not pcrel, we can |
574 | | // replace B to implement it: (A - R - K + C) |
575 | 68.3k | if (IsPCRel) { |
576 | 7.55k | Ctx.reportError( |
577 | 7.55k | Fixup.getLoc(), |
578 | 7.55k | "No relocation available to represent this relative expression"); |
579 | 7.55k | return; |
580 | 7.55k | } |
581 | | |
582 | 60.8k | const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol()); |
583 | | |
584 | 60.8k | if (SymB.isUndefined()) { |
585 | 53.6k | Ctx.reportError(Fixup.getLoc(), |
586 | 53.6k | Twine("symbol '") + SymB.getName() + |
587 | 53.6k | "' can not be undefined in a subtraction expression"); |
588 | 53.6k | return; |
589 | 53.6k | } |
590 | | |
591 | 7.16k | assert(!SymB.isAbsolute() && "Should have been folded"); |
592 | 7.16k | const MCSection &SecB = SymB.getSection(); |
593 | 7.16k | if (&SecB != &FixupSection) { |
594 | 0 | Ctx.reportError(Fixup.getLoc(), |
595 | 0 | "Cannot represent a difference across sections"); |
596 | 0 | return; |
597 | 0 | } |
598 | | |
599 | 7.16k | bool valid; |
600 | 7.16k | uint64_t SymBOffset = Layout.getSymbolOffset(SymB, valid); |
601 | 7.16k | uint64_t K = SymBOffset - FixupOffset; |
602 | 7.16k | IsPCRel = true; |
603 | 7.16k | C -= K; |
604 | 7.16k | } |
605 | | |
606 | | // We either rejected the fixup or folded B into C at this point. |
607 | 79.7k | const MCSymbolRefExpr *RefA = Target.getSymA(); |
608 | 79.7k | const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr; |
609 | | |
610 | 79.7k | bool ViaWeakRef = false; |
611 | 79.7k | if (SymA && SymA->isVariable()) { |
612 | 2.36k | const MCExpr *Expr = SymA->getVariableValue(); |
613 | 2.36k | if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { |
614 | 1.59k | if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) { |
615 | 0 | SymA = cast<MCSymbolELF>(&Inner->getSymbol()); |
616 | 0 | ViaWeakRef = true; |
617 | 0 | } |
618 | 1.59k | } |
619 | 2.36k | } |
620 | | |
621 | 79.7k | unsigned Type = getRelocType(Ctx, Target, Fixup, IsPCRel); |
622 | 79.7k | bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type); |
623 | 79.7k | if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) { |
624 | 60.1k | bool valid; |
625 | 60.1k | C += Layout.getSymbolOffset(*SymA, valid); |
626 | 60.1k | } |
627 | | |
628 | 79.7k | uint64_t Addend = 0; |
629 | 79.7k | if (hasRelocationAddend()) { |
630 | 0 | Addend = C; |
631 | 0 | C = 0; |
632 | 0 | } |
633 | | |
634 | 79.7k | FixedValue = C; |
635 | | |
636 | 79.7k | if (!RelocateWithSymbol) { |
637 | 69.6k | const MCSection *SecA = |
638 | 69.6k | (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr; |
639 | 69.6k | auto *ELFSec = cast_or_null<MCSectionELF>(SecA); |
640 | 69.6k | const auto *SectionSymbol = |
641 | 69.6k | ELFSec ? cast<MCSymbolELF>(ELFSec->getBeginSymbol()) : nullptr; |
642 | 69.6k | if (SectionSymbol) |
643 | 60.1k | SectionSymbol->setUsedInReloc(); |
644 | 69.6k | ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend); |
645 | 69.6k | Relocations[&FixupSection].push_back(Rec); |
646 | 69.6k | return; |
647 | 69.6k | } |
648 | | |
649 | 10.1k | if (SymA) { |
650 | 10.1k | if (const MCSymbolELF *R = Renames.lookup(SymA)) |
651 | 3 | SymA = R; |
652 | | |
653 | 10.1k | if (ViaWeakRef) |
654 | 0 | SymA->setIsWeakrefUsedInReloc(); |
655 | 10.1k | else |
656 | 10.1k | SymA->setUsedInReloc(); |
657 | 10.1k | } |
658 | 10.1k | ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend); |
659 | 10.1k | Relocations[&FixupSection].push_back(Rec); |
660 | 10.1k | return; |
661 | 79.7k | } |
662 | | |
663 | | bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout, |
664 | | const MCSymbolELF &Symbol, bool Used, |
665 | 0 | bool Renamed) { |
666 | 0 | if (Symbol.isVariable()) { |
667 | 0 | const MCExpr *Expr = Symbol.getVariableValue(); |
668 | 0 | if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) { |
669 | 0 | if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF) |
670 | 0 | return false; |
671 | 0 | } |
672 | 0 | } |
673 | 0 |
|
674 | 0 | if (Used) |
675 | 0 | return true; |
676 | 0 |
|
677 | 0 | if (Renamed) |
678 | 0 | return false; |
679 | 0 |
|
680 | 0 | if (Symbol.isVariable() && Symbol.isUndefined()) { |
681 | 0 | // FIXME: this is here just to diagnose the case of a var = commmon_sym. |
682 | 0 | Layout.getBaseSymbol(Symbol); |
683 | 0 | return false; |
684 | 0 | } |
685 | 0 |
|
686 | 0 | if (Symbol.isUndefined() && !Symbol.isBindingSet()) |
687 | 0 | return false; |
688 | 0 |
|
689 | 0 | if (Symbol.isTemporary()) |
690 | 0 | return false; |
691 | 0 |
|
692 | 0 | if (Symbol.getType() == ELF::STT_SECTION) |
693 | 0 | return false; |
694 | 0 |
|
695 | 0 | return true; |
696 | 0 | } |
697 | | |
698 | | void ELFObjectWriter::computeSymbolTable( |
699 | | MCAssembler &Asm, const MCAsmLayout &Layout, |
700 | | const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap, |
701 | 0 | SectionOffsetsTy &SectionOffsets) { |
702 | 0 | MCContext &Ctx = Asm.getContext(); |
703 | 0 | SymbolTableWriter Writer(*this, is64Bit()); |
704 | 0 |
|
705 | 0 | // Symbol table |
706 | 0 | unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
707 | 0 | MCSectionELF *SymtabSection = |
708 | 0 | Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, ""); |
709 | 0 | SymtabSection->setAlignment(is64Bit() ? 8 : 4); |
710 | 0 | SymbolTableIndex = addToSectionTable(SymtabSection); |
711 | 0 |
|
712 | 0 | align(SymtabSection->getAlignment()); |
713 | 0 | uint64_t SecStart = getStream().tell(); |
714 | 0 |
|
715 | 0 | // The first entry is the undefined symbol entry. |
716 | 0 | Writer.writeSymbol(0, 0, 0, 0, 0, 0, false); |
717 | 0 |
|
718 | 0 | std::vector<ELFSymbolData> LocalSymbolData; |
719 | 0 | std::vector<ELFSymbolData> ExternalSymbolData; |
720 | 0 |
|
721 | 0 | // Add the data for the symbols. |
722 | 0 | bool HasLargeSectionIndex = false; |
723 | 0 | for (const MCSymbol &S : Asm.symbols()) { |
724 | 0 | const auto &Symbol = cast<MCSymbolELF>(S); |
725 | 0 | bool Used = Symbol.isUsedInReloc(); |
726 | 0 | bool WeakrefUsed = Symbol.isWeakrefUsedInReloc(); |
727 | 0 | bool isSignature = Symbol.isSignature(); |
728 | 0 |
|
729 | 0 | if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature, |
730 | 0 | Renames.count(&Symbol))) |
731 | 0 | continue; |
732 | 0 |
|
733 | 0 | if (Symbol.isTemporary() && Symbol.isUndefined()) { |
734 | 0 | Ctx.reportError(SMLoc(), "Undefined temporary symbol"); |
735 | 0 | continue; |
736 | 0 | } |
737 | 0 |
|
738 | 0 | ELFSymbolData MSD; |
739 | 0 | MSD.Symbol = cast<MCSymbolELF>(&Symbol); |
740 | 0 |
|
741 | 0 | bool Local = Symbol.getBinding() == ELF::STB_LOCAL; |
742 | 0 | assert(Local || !Symbol.isTemporary()); |
743 | 0 |
|
744 | 0 | if (Symbol.isAbsolute()) { |
745 | 0 | MSD.SectionIndex = ELF::SHN_ABS; |
746 | 0 | } else if (Symbol.isCommon()) { |
747 | 0 | assert(!Local); |
748 | 0 | MSD.SectionIndex = ELF::SHN_COMMON; |
749 | 0 | } else if (Symbol.isUndefined()) { |
750 | 0 | if (isSignature && !Used) { |
751 | 0 | MSD.SectionIndex = RevGroupMap.lookup(&Symbol); |
752 | 0 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
753 | 0 | HasLargeSectionIndex = true; |
754 | 0 | } else { |
755 | 0 | MSD.SectionIndex = ELF::SHN_UNDEF; |
756 | 0 | } |
757 | 0 | } else { |
758 | 0 | const MCSectionELF &Section = |
759 | 0 | static_cast<const MCSectionELF &>(Symbol.getSection()); |
760 | 0 | MSD.SectionIndex = SectionIndexMap.lookup(&Section); |
761 | 0 | assert(MSD.SectionIndex && "Invalid section index!"); |
762 | 0 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
763 | 0 | HasLargeSectionIndex = true; |
764 | 0 | } |
765 | 0 |
|
766 | 0 | // The @@@ in symbol version is replaced with @ in undefined symbols and @@ |
767 | 0 | // in defined ones. |
768 | 0 | // |
769 | 0 | // FIXME: All name handling should be done before we get to the writer, |
770 | 0 | // including dealing with GNU-style version suffixes. Fixing this isn't |
771 | 0 | // trivial. |
772 | 0 | // |
773 | 0 | // We thus have to be careful to not perform the symbol version replacement |
774 | 0 | // blindly: |
775 | 0 | // |
776 | 0 | // The ELF format is used on Windows by the MCJIT engine. Thus, on |
777 | 0 | // Windows, the ELFObjectWriter can encounter symbols mangled using the MS |
778 | 0 | // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC |
779 | 0 | // C++ name mangling can legally have "@@@" as a sub-string. In that case, |
780 | 0 | // the EFLObjectWriter should not interpret the "@@@" sub-string as |
781 | 0 | // specifying GNU-style symbol versioning. The ELFObjectWriter therefore |
782 | 0 | // checks for the MSVC C++ name mangling prefix which is either "?", "@?", |
783 | 0 | // "__imp_?" or "__imp_@?". |
784 | 0 | // |
785 | 0 | // It would have been interesting to perform the MS mangling prefix check |
786 | 0 | // only when the target triple is of the form *-pc-windows-elf. But, it |
787 | 0 | // seems that this information is not easily accessible from the |
788 | 0 | // ELFObjectWriter. |
789 | 0 | StringRef Name = Symbol.getName(); |
790 | 0 | SmallString<32> Buf; |
791 | 0 | if (!Name.startswith("?") && !Name.startswith("@?") && |
792 | 0 | !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) { |
793 | 0 | // This symbol isn't following the MSVC C++ name mangling convention. We |
794 | 0 | // can thus safely interpret the @@@ in symbol names as specifying symbol |
795 | 0 | // versioning. |
796 | 0 | size_t Pos = Name.find("@@@"); |
797 | 0 | if (Pos != StringRef::npos) { |
798 | 0 | Buf += Name.substr(0, Pos); |
799 | 0 | unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; |
800 | 0 | Buf += Name.substr(Pos + Skip); |
801 | 0 | Name = VersionSymSaver.save(Buf.c_str()); |
802 | 0 | } |
803 | 0 | } |
804 | 0 |
|
805 | 0 | // Sections have their own string table |
806 | 0 | if (Symbol.getType() != ELF::STT_SECTION) { |
807 | 0 | MSD.Name = Name; |
808 | 0 | StrTabBuilder.add(Name); |
809 | 0 | } |
810 | 0 |
|
811 | 0 | if (Local) |
812 | 0 | LocalSymbolData.push_back(MSD); |
813 | 0 | else |
814 | 0 | ExternalSymbolData.push_back(MSD); |
815 | 0 | } |
816 | 0 |
|
817 | 0 | // This holds the .symtab_shndx section index. |
818 | 0 | unsigned SymtabShndxSectionIndex = 0; |
819 | 0 |
|
820 | 0 | if (HasLargeSectionIndex) { |
821 | 0 | MCSectionELF *SymtabShndxSection = |
822 | 0 | Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, ""); |
823 | 0 | SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection); |
824 | 0 | SymtabShndxSection->setAlignment(4); |
825 | 0 | } |
826 | 0 |
|
827 | 0 | ArrayRef<std::string> FileNames = Asm.getFileNames(); |
828 | 0 | for (const std::string &Name : FileNames) |
829 | 0 | StrTabBuilder.add(Name); |
830 | 0 |
|
831 | 0 | StrTabBuilder.finalize(); |
832 | 0 |
|
833 | 0 | for (const std::string &Name : FileNames) |
834 | 0 | Writer.writeSymbol(StrTabBuilder.getOffset(Name), |
835 | 0 | ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT, |
836 | 0 | ELF::SHN_ABS, true); |
837 | 0 |
|
838 | 0 | // Symbols are required to be in lexicographic order. |
839 | 0 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
840 | 0 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
841 | 0 |
|
842 | 0 | // Set the symbol indices. Local symbols must come before all other |
843 | 0 | // symbols with non-local bindings. |
844 | 0 | unsigned Index = FileNames.size() + 1; |
845 | 0 |
|
846 | 0 | for (ELFSymbolData &MSD : LocalSymbolData) { |
847 | 0 | unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION |
848 | 0 | ? 0 |
849 | 0 | : StrTabBuilder.getOffset(MSD.Name); |
850 | 0 | MSD.Symbol->setIndex(Index++); |
851 | 0 | writeSymbol(Writer, StringIndex, MSD, Layout); |
852 | 0 | } |
853 | 0 |
|
854 | 0 | // Write the symbol table entries. |
855 | 0 | LastLocalSymbolIndex = Index; |
856 | 0 |
|
857 | 0 | for (ELFSymbolData &MSD : ExternalSymbolData) { |
858 | 0 | unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name); |
859 | 0 | MSD.Symbol->setIndex(Index++); |
860 | 0 | writeSymbol(Writer, StringIndex, MSD, Layout); |
861 | 0 | assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL); |
862 | 0 | } |
863 | 0 |
|
864 | 0 | uint64_t SecEnd = getStream().tell(); |
865 | 0 | SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd); |
866 | 0 |
|
867 | 0 | ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes(); |
868 | 0 | if (ShndxIndexes.empty()) { |
869 | 0 | assert(SymtabShndxSectionIndex == 0); |
870 | 0 | return; |
871 | 0 | } |
872 | 0 | assert(SymtabShndxSectionIndex != 0); |
873 | 0 |
|
874 | 0 | SecStart = getStream().tell(); |
875 | 0 | const MCSectionELF *SymtabShndxSection = |
876 | 0 | SectionTable[SymtabShndxSectionIndex - 1]; |
877 | 0 | for (uint32_t Index : ShndxIndexes) |
878 | 0 | write(Index); |
879 | 0 | SecEnd = getStream().tell(); |
880 | 0 | SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd); |
881 | 0 | } |
882 | | |
883 | | MCSectionELF * |
884 | | ELFObjectWriter::createRelocationSection(MCContext &Ctx, |
885 | 50.6k | const MCSectionELF &Sec) { |
886 | 50.6k | if (Relocations[&Sec].empty()) |
887 | 49.3k | return nullptr; |
888 | | |
889 | 1.33k | const StringRef SectionName = Sec.getSectionName(); |
890 | 1.33k | std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; |
891 | 1.33k | RelaSectionName += SectionName; |
892 | | |
893 | 1.33k | unsigned EntrySize; |
894 | 1.33k | if (hasRelocationAddend()) |
895 | 0 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
896 | 1.33k | else |
897 | 1.33k | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
898 | | |
899 | 1.33k | unsigned Flags = 0; |
900 | 1.33k | if (Sec.getFlags() & ELF::SHF_GROUP) |
901 | 0 | Flags = ELF::SHF_GROUP; |
902 | | |
903 | 1.33k | MCSectionELF *RelaSection = Ctx.createELFRelSection( |
904 | 1.33k | RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, |
905 | 1.33k | Flags, EntrySize, Sec.getGroup(), &Sec); |
906 | 1.33k | RelaSection->setAlignment(is64Bit() ? 8 : 4); |
907 | 1.33k | return RelaSection; |
908 | 50.6k | } |
909 | | |
910 | | void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec, |
911 | 53.2k | const MCAsmLayout &Layout) { |
912 | 53.2k | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
913 | 53.2k | StringRef SectionName = Section.getSectionName(); |
914 | | |
915 | | // Compressing debug_frame requires handling alignment fragments which is |
916 | | // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow |
917 | | // for writing to arbitrary buffers) for little benefit. |
918 | 53.2k | if (!Asm.getContext().getAsmInfo()->compressDebugSections() || |
919 | 53.2k | !SectionName.startswith(".debug_") || SectionName == ".debug_frame") { |
920 | 53.2k | Asm.writeSectionData(&Section, Layout); |
921 | 53.2k | return; |
922 | 53.2k | } |
923 | | |
924 | 0 | SmallVector<char, 128> UncompressedData; |
925 | 0 | raw_svector_ostream VecOS(UncompressedData); |
926 | 0 | raw_pwrite_stream &OldStream = getStream(); |
927 | 0 | setStream(VecOS); |
928 | 0 | Asm.writeSectionData(&Section, Layout); |
929 | 0 | setStream(OldStream); |
930 | |
|
931 | | #if 0 |
932 | | SmallVector<char, 128> CompressedContents; |
933 | | zlib::Status Success = zlib::compress( |
934 | | StringRef(UncompressedData.data(), UncompressedData.size()), |
935 | | CompressedContents); |
936 | | if (Success != zlib::StatusOK) { |
937 | | getStream() << UncompressedData; |
938 | | return; |
939 | | } |
940 | | |
941 | | if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) { |
942 | | getStream() << UncompressedData; |
943 | | return; |
944 | | } |
945 | | Asm.getContext().renameELFSection(&Section, |
946 | | (".z" + SectionName.drop_front(1)).str()); |
947 | | getStream() << CompressedContents; |
948 | | #endif |
949 | 0 | } |
950 | | |
951 | | void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, |
952 | | uint64_t Flags, uint64_t Address, |
953 | | uint64_t Offset, uint64_t Size, |
954 | | uint32_t Link, uint32_t Info, |
955 | | uint64_t Alignment, |
956 | 0 | uint64_t EntrySize) { |
957 | 0 | write32(Name); // sh_name: index into string table |
958 | 0 | write32(Type); // sh_type |
959 | 0 | WriteWord(Flags); // sh_flags |
960 | 0 | WriteWord(Address); // sh_addr |
961 | 0 | WriteWord(Offset); // sh_offset |
962 | 0 | WriteWord(Size); // sh_size |
963 | 0 | write32(Link); // sh_link |
964 | 0 | write32(Info); // sh_info |
965 | 0 | WriteWord(Alignment); // sh_addralign |
966 | 0 | WriteWord(EntrySize); // sh_entsize |
967 | 0 | } |
968 | | |
969 | | void ELFObjectWriter::writeRelocations(const MCAssembler &Asm, |
970 | 0 | const MCSectionELF &Sec) { |
971 | 0 | std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec]; |
972 | 0 |
|
973 | 0 | // We record relocations by pushing to the end of a vector. Reverse the vector |
974 | 0 | // to get the relocations in the order they were created. |
975 | 0 | // In most cases that is not important, but it can be for special sections |
976 | 0 | // (.eh_frame) or specific relocations (TLS optimizations on SystemZ). |
977 | 0 | std::reverse(Relocs.begin(), Relocs.end()); |
978 | 0 |
|
979 | 0 | // Sort the relocation entries. MIPS needs this. |
980 | 0 | TargetObjectWriter->sortRelocs(Asm, Relocs); |
981 | 0 |
|
982 | 0 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
983 | 0 | const ELFRelocationEntry &Entry = Relocs[e - i - 1]; |
984 | 0 | unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0; |
985 | 0 |
|
986 | 0 | if (is64Bit()) { |
987 | 0 | write(Entry.Offset); |
988 | 0 | if (TargetObjectWriter->isN64()) { |
989 | 0 | write(uint32_t(Index)); |
990 | 0 |
|
991 | 0 | write(TargetObjectWriter->getRSsym(Entry.Type)); |
992 | 0 | write(TargetObjectWriter->getRType3(Entry.Type)); |
993 | 0 | write(TargetObjectWriter->getRType2(Entry.Type)); |
994 | 0 | write(TargetObjectWriter->getRType(Entry.Type)); |
995 | 0 | } else { |
996 | 0 | struct ELF::Elf64_Rela ERE64; |
997 | 0 | ERE64.setSymbolAndType(Index, Entry.Type); |
998 | 0 | write(ERE64.r_info); |
999 | 0 | } |
1000 | 0 | if (hasRelocationAddend()) |
1001 | 0 | write(Entry.Addend); |
1002 | 0 | } else { |
1003 | 0 | write(uint32_t(Entry.Offset)); |
1004 | 0 |
|
1005 | 0 | struct ELF::Elf32_Rela ERE32; |
1006 | 0 | ERE32.setSymbolAndType(Index, Entry.Type); |
1007 | 0 | write(ERE32.r_info); |
1008 | 0 |
|
1009 | 0 | if (hasRelocationAddend()) |
1010 | 0 | write(uint32_t(Entry.Addend)); |
1011 | 0 | } |
1012 | 0 | } |
1013 | 0 | } |
1014 | | |
1015 | 0 | const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) { |
1016 | 0 | const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1]; |
1017 | 0 | getStream() << StrTabBuilder.data(); |
1018 | 0 | return StrtabSection; |
1019 | 0 | } |
1020 | | |
1021 | | void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap, |
1022 | | uint32_t GroupSymbolIndex, uint64_t Offset, |
1023 | 0 | uint64_t Size, const MCSectionELF &Section) { |
1024 | 0 | uint64_t sh_link = 0; |
1025 | 0 | uint64_t sh_info = 0; |
1026 | 0 |
|
1027 | 0 | switch(Section.getType()) { |
1028 | 0 | default: |
1029 | 0 | // Nothing to do. |
1030 | 0 | break; |
1031 | 0 |
|
1032 | 0 | case ELF::SHT_DYNAMIC: |
1033 | 0 | llvm_unreachable("SHT_DYNAMIC in a relocatable object"); |
1034 | 0 |
|
1035 | 0 | case ELF::SHT_REL: |
1036 | 0 | case ELF::SHT_RELA: { |
1037 | 0 | sh_link = SymbolTableIndex; |
1038 | 0 | assert(sh_link && ".symtab not found"); |
1039 | 0 | const MCSectionELF *InfoSection = Section.getAssociatedSection(); |
1040 | 0 | sh_info = SectionIndexMap.lookup(InfoSection); |
1041 | 0 | break; |
1042 | 0 | } |
1043 | 0 |
|
1044 | 0 | case ELF::SHT_SYMTAB: |
1045 | 0 | case ELF::SHT_DYNSYM: |
1046 | 0 | sh_link = StringTableIndex; |
1047 | 0 | sh_info = LastLocalSymbolIndex; |
1048 | 0 | break; |
1049 | 0 |
|
1050 | 0 | case ELF::SHT_SYMTAB_SHNDX: |
1051 | 0 | sh_link = SymbolTableIndex; |
1052 | 0 | break; |
1053 | 0 |
|
1054 | 0 | case ELF::SHT_GROUP: |
1055 | 0 | sh_link = SymbolTableIndex; |
1056 | 0 | sh_info = GroupSymbolIndex; |
1057 | 0 | break; |
1058 | 0 | } |
1059 | 0 |
|
1060 | 0 | if (TargetObjectWriter->getEMachine() == ELF::EM_ARM && |
1061 | 0 | Section.getType() == ELF::SHT_ARM_EXIDX) |
1062 | 0 | sh_link = SectionIndexMap.lookup(Section.getAssociatedSection()); |
1063 | 0 |
|
1064 | 0 | WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()), |
1065 | 0 | Section.getType(), Section.getFlags(), 0, Offset, Size, |
1066 | 0 | sh_link, sh_info, Section.getAlignment(), |
1067 | 0 | Section.getEntrySize()); |
1068 | 0 | } |
1069 | | |
1070 | | void ELFObjectWriter::writeSectionHeader( |
1071 | | const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap, |
1072 | 0 | const SectionOffsetsTy &SectionOffsets) { |
1073 | 0 | const unsigned NumSections = SectionTable.size(); |
1074 | 0 |
|
1075 | 0 | // Null section first. |
1076 | 0 | uint64_t FirstSectionSize = |
1077 | 0 | (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0; |
1078 | 0 | WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0); |
1079 | 0 |
|
1080 | 0 | for (const MCSectionELF *Section : SectionTable) { |
1081 | 0 | uint32_t GroupSymbolIndex; |
1082 | 0 | unsigned Type = Section->getType(); |
1083 | 0 | if (Type != ELF::SHT_GROUP) |
1084 | 0 | GroupSymbolIndex = 0; |
1085 | 0 | else |
1086 | 0 | GroupSymbolIndex = Section->getGroup()->getIndex(); |
1087 | 0 |
|
1088 | 0 | const std::pair<uint64_t, uint64_t> &Offsets = |
1089 | 0 | SectionOffsets.find(Section)->second; |
1090 | 0 | uint64_t Size; |
1091 | 0 | if (Type == ELF::SHT_NOBITS) |
1092 | 0 | Size = Layout.getSectionAddressSize(Section); |
1093 | 0 | else |
1094 | 0 | Size = Offsets.second - Offsets.first; |
1095 | 0 |
|
1096 | 0 | writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size, |
1097 | 0 | *Section); |
1098 | 0 | } |
1099 | 0 | } |
1100 | | |
1101 | | void ELFObjectWriter::writeObject(MCAssembler &Asm, |
1102 | | const MCAsmLayout &Layout) |
1103 | 51.4k | { |
1104 | 51.4k | MCContext &Ctx = Asm.getContext(); |
1105 | 51.4k | MCSectionELF *StrtabSection = |
1106 | 51.4k | Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0); |
1107 | 51.4k | StringTableIndex = addToSectionTable(StrtabSection); |
1108 | | |
1109 | 51.4k | RevGroupMapTy RevGroupMap; |
1110 | 51.4k | SectionIndexMapTy SectionIndexMap; |
1111 | | |
1112 | 51.4k | std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers; |
1113 | | |
1114 | | // ... then the sections ... |
1115 | 51.4k | SectionOffsetsTy SectionOffsets; |
1116 | 51.4k | std::vector<MCSectionELF *> Groups; |
1117 | 51.4k | std::vector<MCSectionELF *> Relocations; |
1118 | 53.2k | for (MCSection &Sec : Asm) { |
1119 | 53.2k | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
1120 | | |
1121 | 53.2k | align(Section.getAlignment()); |
1122 | | |
1123 | | // Remember the offset into the file for this section. |
1124 | 53.2k | uint64_t SecStart = getStream().tell(); |
1125 | | |
1126 | 53.2k | const MCSymbolELF *SignatureSymbol = Section.getGroup(); |
1127 | 53.2k | writeSectionData(Asm, Section, Layout); |
1128 | 53.2k | if (Asm.getError()) |
1129 | 2.54k | return; |
1130 | | |
1131 | 50.6k | uint64_t SecEnd = getStream().tell(); |
1132 | 50.6k | SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd); |
1133 | | |
1134 | 50.6k | MCSectionELF *RelSection = createRelocationSection(Ctx, Section); |
1135 | | |
1136 | 50.6k | if (SignatureSymbol) { |
1137 | 0 | Asm.registerSymbol(*SignatureSymbol); |
1138 | 0 | unsigned &GroupIdx = RevGroupMap[SignatureSymbol]; |
1139 | 0 | if (!GroupIdx) { |
1140 | 0 | MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol); |
1141 | 0 | GroupIdx = addToSectionTable(Group); |
1142 | 0 | Group->setAlignment(4); |
1143 | 0 | Groups.push_back(Group); |
1144 | 0 | } |
1145 | 0 | std::vector<const MCSectionELF *> &Members = |
1146 | 0 | GroupMembers[SignatureSymbol]; |
1147 | 0 | Members.push_back(&Section); |
1148 | 0 | if (RelSection) |
1149 | 0 | Members.push_back(RelSection); |
1150 | 0 | } |
1151 | | |
1152 | 50.6k | SectionIndexMap[&Section] = addToSectionTable(&Section); |
1153 | 50.6k | if (RelSection) { |
1154 | 1.33k | SectionIndexMap[RelSection] = addToSectionTable(RelSection); |
1155 | 1.33k | Relocations.push_back(RelSection); |
1156 | 1.33k | } |
1157 | 50.6k | } |
1158 | | |
1159 | 48.8k | return; |
1160 | | |
1161 | 48.8k | for (MCSectionELF *Group : Groups) { |
1162 | 0 | align(Group->getAlignment()); |
1163 | | |
1164 | | // Remember the offset into the file for this section. |
1165 | 0 | uint64_t SecStart = getStream().tell(); |
1166 | |
|
1167 | 0 | const MCSymbol *SignatureSymbol = Group->getGroup(); |
1168 | 0 | assert(SignatureSymbol); |
1169 | 0 | write(uint32_t(ELF::GRP_COMDAT)); |
1170 | 0 | for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) { |
1171 | 0 | uint32_t SecIndex = SectionIndexMap.lookup(Member); |
1172 | 0 | write(SecIndex); |
1173 | 0 | } |
1174 | |
|
1175 | 0 | uint64_t SecEnd = getStream().tell(); |
1176 | 0 | SectionOffsets[Group] = std::make_pair(SecStart, SecEnd); |
1177 | 0 | } |
1178 | | |
1179 | | // Compute symbol table information. |
1180 | 0 | computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets); |
1181 | |
|
1182 | 0 | for (MCSectionELF *RelSection : Relocations) { |
1183 | 0 | align(RelSection->getAlignment()); |
1184 | | |
1185 | | // Remember the offset into the file for this section. |
1186 | 0 | uint64_t SecStart = getStream().tell(); |
1187 | |
|
1188 | 0 | writeRelocations(Asm, *RelSection->getAssociatedSection()); // qq |
1189 | |
|
1190 | 0 | uint64_t SecEnd = getStream().tell(); |
1191 | 0 | SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd); |
1192 | 0 | } |
1193 | |
|
1194 | 0 | { |
1195 | 0 | uint64_t SecStart = getStream().tell(); |
1196 | 0 | const MCSectionELF *Sec = createStringTable(Ctx); |
1197 | 0 | uint64_t SecEnd = getStream().tell(); |
1198 | 0 | SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd); |
1199 | 0 | } |
1200 | |
|
1201 | 0 | uint64_t NaturalAlignment = is64Bit() ? 8 : 4; |
1202 | 0 | align(NaturalAlignment); |
1203 | |
|
1204 | 0 | const uint64_t SectionHeaderOffset = getStream().tell(); |
1205 | | |
1206 | | // ... then the section header table ... |
1207 | 0 | writeSectionHeader(Layout, SectionIndexMap, SectionOffsets); |
1208 | |
|
1209 | 0 | uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) |
1210 | 0 | ? (uint16_t)ELF::SHN_UNDEF |
1211 | 0 | : SectionTable.size() + 1; |
1212 | 0 | if (sys::IsLittleEndianHost != IsLittleEndian) |
1213 | 0 | sys::swapByteOrder(NumSections); |
1214 | 0 | unsigned NumSectionsOffset; |
1215 | |
|
1216 | 0 | if (is64Bit()) { |
1217 | 0 | uint64_t Val = SectionHeaderOffset; |
1218 | 0 | if (sys::IsLittleEndianHost != IsLittleEndian) |
1219 | 0 | sys::swapByteOrder(Val); |
1220 | 0 | getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
1221 | 0 | offsetof(ELF::Elf64_Ehdr, e_shoff)); |
1222 | 0 | NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum); |
1223 | 0 | } else { |
1224 | 0 | uint32_t Val = SectionHeaderOffset; |
1225 | 0 | if (sys::IsLittleEndianHost != IsLittleEndian) |
1226 | 0 | sys::swapByteOrder(Val); |
1227 | 0 | getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
1228 | 0 | offsetof(ELF::Elf32_Ehdr, e_shoff)); |
1229 | 0 | NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum); |
1230 | 0 | } |
1231 | 0 | getStream().pwrite(reinterpret_cast<char *>(&NumSections), |
1232 | 0 | sizeof(NumSections), NumSectionsOffset); |
1233 | 0 | } |
1234 | | |
1235 | | bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
1236 | | const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB, |
1237 | 143k | bool InSet, bool IsPCRel) const { |
1238 | 143k | const auto &SymA = cast<MCSymbolELF>(SA); |
1239 | 143k | if (IsPCRel) { |
1240 | 22.7k | assert(!InSet); |
1241 | 22.7k | if (::isWeak(SymA)) |
1242 | 0 | return false; |
1243 | 22.7k | } |
1244 | 143k | return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, |
1245 | 143k | InSet, IsPCRel); |
1246 | 143k | } |
1247 | | |
1248 | 0 | bool ELFObjectWriter::isWeak(const MCSymbol &S) const { |
1249 | 0 | const auto &Sym = cast<MCSymbolELF>(S); |
1250 | 0 | if (::isWeak(Sym)) |
1251 | 0 | return true; |
1252 | | |
1253 | | // It is invalid to replace a reference to a global in a comdat |
1254 | | // with a reference to a local since out of comdat references |
1255 | | // to a local are forbidden. |
1256 | | // We could try to return false for more cases, like the reference |
1257 | | // being in the same comdat or Sym being an alias to another global, |
1258 | | // but it is not clear if it is worth the effort. |
1259 | 0 | if (Sym.getBinding() != ELF::STB_GLOBAL) |
1260 | 0 | return false; |
1261 | | |
1262 | 0 | if (!Sym.isInSection()) |
1263 | 0 | return false; |
1264 | | |
1265 | 0 | const auto &Sec = cast<MCSectionELF>(Sym.getSection()); |
1266 | 0 | return Sec.getGroup(); |
1267 | 0 | } |
1268 | | |
1269 | | MCObjectWriter *llvm_ks::createELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
1270 | | raw_pwrite_stream &OS, |
1271 | 129k | bool IsLittleEndian) { |
1272 | 129k | return new ELFObjectWriter(MOTW, OS, IsLittleEndian); |
1273 | 129k | } |