1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements ELF object file writer information.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/ADT/iterator.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFObjectWriter.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCFixup.h"
29 #include "llvm/MC/MCFixupKindInfo.h"
30 #include "llvm/MC/MCFragment.h"
31 #include "llvm/MC/MCObjectWriter.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/MC/MCSymbolELF.h"
36 #include "llvm/MC/MCTargetOptions.h"
37 #include "llvm/MC/MCValue.h"
38 #include "llvm/MC/StringTableBuilder.h"
39 #include "llvm/Support/Alignment.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/Compression.h"
42 #include "llvm/Support/Endian.h"
43 #include "llvm/Support/EndianStream.h"
44 #include "llvm/Support/Error.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/Host.h"
47 #include "llvm/Support/LEB128.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/SMLoc.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include <algorithm>
52 #include <cassert>
53 #include <cstddef>
54 #include <cstdint>
55 #include <map>
56 #include <memory>
57 #include <string>
58 #include <utility>
59 #include <vector>
60
61 using namespace llvm;
62
63 #undef DEBUG_TYPE
64 #define DEBUG_TYPE "reloc-info"
65
66 namespace {
67
68 using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>;
69
70 class ELFObjectWriter;
71 struct ELFWriter;
72
isDwoSection(const MCSectionELF & Sec)73 bool isDwoSection(const MCSectionELF &Sec) {
74 return Sec.getName().endswith(".dwo");
75 }
76
77 class SymbolTableWriter {
78 ELFWriter &EWriter;
79 bool Is64Bit;
80
81 // indexes we are going to write to .symtab_shndx.
82 std::vector<uint32_t> ShndxIndexes;
83
84 // The numbel of symbols written so far.
85 unsigned NumWritten;
86
87 void createSymtabShndx();
88
89 template <typename T> void write(T Value);
90
91 public:
92 SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit);
93
94 void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
95 uint8_t other, uint32_t shndx, bool Reserved);
96
getShndxIndexes() const97 ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
98 };
99
100 struct ELFWriter {
101 ELFObjectWriter &OWriter;
102 support::endian::Writer W;
103
104 enum DwoMode {
105 AllSections,
106 NonDwoOnly,
107 DwoOnly,
108 } Mode;
109
110 static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
111 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
112 bool Used, bool Renamed);
113
114 /// Helper struct for containing some precomputed information on symbols.
115 struct ELFSymbolData {
116 const MCSymbolELF *Symbol;
117 StringRef Name;
118 uint32_t SectionIndex;
119 uint32_t Order;
120 };
121
122 /// @}
123 /// @name Symbol Table Data
124 /// @{
125
126 StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
127
128 /// @}
129
130 // This holds the symbol table index of the last local symbol.
131 unsigned LastLocalSymbolIndex;
132 // This holds the .strtab section index.
133 unsigned StringTableIndex;
134 // This holds the .symtab section index.
135 unsigned SymbolTableIndex;
136
137 // Sections in the order they are to be output in the section table.
138 std::vector<const MCSectionELF *> SectionTable;
139 unsigned addToSectionTable(const MCSectionELF *Sec);
140
141 // TargetObjectWriter wrappers.
142 bool is64Bit() const;
143 bool usesRela(const MCSectionELF &Sec) const;
144
145 uint64_t align(unsigned Alignment);
146
147 bool maybeWriteCompression(uint32_t ChType, uint64_t Size,
148 SmallVectorImpl<uint8_t> &CompressedContents,
149 unsigned Alignment);
150
151 public:
ELFWriter__anon4df038aa0111::ELFWriter152 ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
153 bool IsLittleEndian, DwoMode Mode)
154 : OWriter(OWriter),
155 W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
156
WriteWord__anon4df038aa0111::ELFWriter157 void WriteWord(uint64_t Word) {
158 if (is64Bit())
159 W.write<uint64_t>(Word);
160 else
161 W.write<uint32_t>(Word);
162 }
163
write__anon4df038aa0111::ELFWriter164 template <typename T> void write(T Val) {
165 W.write(Val);
166 }
167
168 void writeHeader(const MCAssembler &Asm);
169
170 void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
171 ELFSymbolData &MSD, const MCAsmLayout &Layout);
172
173 // Start and end offset of each section
174 using SectionOffsetsTy =
175 std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
176
177 // Map from a signature symbol to the group section index
178 using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
179
180 /// Compute the symbol table data
181 ///
182 /// \param Asm - The assembler.
183 /// \param SectionIndexMap - Maps a section to its index.
184 /// \param RevGroupMap - Maps a signature symbol to the group section.
185 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
186 const SectionIndexMapTy &SectionIndexMap,
187 const RevGroupMapTy &RevGroupMap,
188 SectionOffsetsTy &SectionOffsets);
189
190 void writeAddrsigSection();
191
192 MCSectionELF *createRelocationSection(MCContext &Ctx,
193 const MCSectionELF &Sec);
194
195 void writeSectionHeader(const MCAsmLayout &Layout,
196 const SectionIndexMapTy &SectionIndexMap,
197 const SectionOffsetsTy &SectionOffsets);
198
199 void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
200 const MCAsmLayout &Layout);
201
202 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
203 uint64_t Address, uint64_t Offset, uint64_t Size,
204 uint32_t Link, uint32_t Info, uint64_t Alignment,
205 uint64_t EntrySize);
206
207 void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
208
209 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
210 void writeSection(const SectionIndexMapTy &SectionIndexMap,
211 uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
212 const MCSectionELF &Section);
213 };
214
215 class ELFObjectWriter : public MCObjectWriter {
216 /// The target specific ELF writer instance.
217 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
218
219 DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
220
221 DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
222
223 bool SeenGnuAbi = false;
224
225 bool hasRelocationAddend() const;
226
227 bool shouldRelocateWithSymbol(const MCAssembler &Asm,
228 const MCSymbolRefExpr *RefA,
229 const MCSymbolELF *Sym, uint64_t C,
230 unsigned Type) const;
231
232 public:
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)233 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
234 : TargetObjectWriter(std::move(MOTW)) {}
235
reset()236 void reset() override {
237 SeenGnuAbi = false;
238 Relocations.clear();
239 Renames.clear();
240 MCObjectWriter::reset();
241 }
242
243 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
244 const MCSymbol &SymA,
245 const MCFragment &FB, bool InSet,
246 bool IsPCRel) const override;
247
checkRelocation(MCContext & Ctx,SMLoc Loc,const MCSectionELF * From,const MCSectionELF * To)248 virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
249 const MCSectionELF *From,
250 const MCSectionELF *To) {
251 return true;
252 }
253
254 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
255 const MCFragment *Fragment, const MCFixup &Fixup,
256 MCValue Target, uint64_t &FixedValue) override;
257
258 void executePostLayoutBinding(MCAssembler &Asm,
259 const MCAsmLayout &Layout) override;
260
markGnuAbi()261 void markGnuAbi() override { SeenGnuAbi = true; }
seenGnuAbi() const262 bool seenGnuAbi() const { return SeenGnuAbi; }
263
264 friend struct ELFWriter;
265 };
266
267 class ELFSingleObjectWriter : public ELFObjectWriter {
268 raw_pwrite_stream &OS;
269 bool IsLittleEndian;
270
271 public:
ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,bool IsLittleEndian)272 ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
273 raw_pwrite_stream &OS, bool IsLittleEndian)
274 : ELFObjectWriter(std::move(MOTW)), OS(OS),
275 IsLittleEndian(IsLittleEndian) {}
276
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)277 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
278 return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
279 .writeObject(Asm, Layout);
280 }
281
282 friend struct ELFWriter;
283 };
284
285 class ELFDwoObjectWriter : public ELFObjectWriter {
286 raw_pwrite_stream &OS, &DwoOS;
287 bool IsLittleEndian;
288
289 public:
ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,raw_pwrite_stream & DwoOS,bool IsLittleEndian)290 ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
291 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
292 bool IsLittleEndian)
293 : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
294 IsLittleEndian(IsLittleEndian) {}
295
checkRelocation(MCContext & Ctx,SMLoc Loc,const MCSectionELF * From,const MCSectionELF * To)296 bool checkRelocation(MCContext &Ctx, SMLoc Loc, const MCSectionELF *From,
297 const MCSectionELF *To) override {
298 if (isDwoSection(*From)) {
299 Ctx.reportError(Loc, "A dwo section may not contain relocations");
300 return false;
301 }
302 if (To && isDwoSection(*To)) {
303 Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
304 return false;
305 }
306 return true;
307 }
308
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)309 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
310 uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
311 .writeObject(Asm, Layout);
312 Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
313 .writeObject(Asm, Layout);
314 return Size;
315 }
316 };
317
318 } // end anonymous namespace
319
align(unsigned Alignment)320 uint64_t ELFWriter::align(unsigned Alignment) {
321 uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
322 W.OS.write_zeros(NewOffset - Offset);
323 return NewOffset;
324 }
325
addToSectionTable(const MCSectionELF * Sec)326 unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
327 SectionTable.push_back(Sec);
328 StrTabBuilder.add(Sec->getName());
329 return SectionTable.size();
330 }
331
createSymtabShndx()332 void SymbolTableWriter::createSymtabShndx() {
333 if (!ShndxIndexes.empty())
334 return;
335
336 ShndxIndexes.resize(NumWritten);
337 }
338
write(T Value)339 template <typename T> void SymbolTableWriter::write(T Value) {
340 EWriter.write(Value);
341 }
342
SymbolTableWriter(ELFWriter & EWriter,bool Is64Bit)343 SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
344 : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
345
writeSymbol(uint32_t name,uint8_t info,uint64_t value,uint64_t size,uint8_t other,uint32_t shndx,bool Reserved)346 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
347 uint64_t size, uint8_t other,
348 uint32_t shndx, bool Reserved) {
349 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
350
351 if (LargeIndex)
352 createSymtabShndx();
353
354 if (!ShndxIndexes.empty()) {
355 if (LargeIndex)
356 ShndxIndexes.push_back(shndx);
357 else
358 ShndxIndexes.push_back(0);
359 }
360
361 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
362
363 if (Is64Bit) {
364 write(name); // st_name
365 write(info); // st_info
366 write(other); // st_other
367 write(Index); // st_shndx
368 write(value); // st_value
369 write(size); // st_size
370 } else {
371 write(name); // st_name
372 write(uint32_t(value)); // st_value
373 write(uint32_t(size)); // st_size
374 write(info); // st_info
375 write(other); // st_other
376 write(Index); // st_shndx
377 }
378
379 ++NumWritten;
380 }
381
is64Bit() const382 bool ELFWriter::is64Bit() const {
383 return OWriter.TargetObjectWriter->is64Bit();
384 }
385
usesRela(const MCSectionELF & Sec) const386 bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
387 return OWriter.hasRelocationAddend() &&
388 Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
389 }
390
391 // Emit the ELF header.
writeHeader(const MCAssembler & Asm)392 void ELFWriter::writeHeader(const MCAssembler &Asm) {
393 // ELF Header
394 // ----------
395 //
396 // Note
397 // ----
398 // emitWord method behaves differently for ELF32 and ELF64, writing
399 // 4 bytes in the former and 8 in the latter.
400
401 W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
402
403 W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
404
405 // e_ident[EI_DATA]
406 W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
407 : ELF::ELFDATA2MSB);
408
409 W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION]
410 // e_ident[EI_OSABI]
411 uint8_t OSABI = OWriter.TargetObjectWriter->getOSABI();
412 W.OS << char(OSABI == ELF::ELFOSABI_NONE && OWriter.seenGnuAbi()
413 ? int(ELF::ELFOSABI_GNU)
414 : OSABI);
415 // e_ident[EI_ABIVERSION]
416 W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
417
418 W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
419
420 W.write<uint16_t>(ELF::ET_REL); // e_type
421
422 W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
423
424 W.write<uint32_t>(ELF::EV_CURRENT); // e_version
425 WriteWord(0); // e_entry, no entry point in .o file
426 WriteWord(0); // e_phoff, no program header for .o
427 WriteWord(0); // e_shoff = sec hdr table off in bytes
428
429 // e_flags = whatever the target wants
430 W.write<uint32_t>(Asm.getELFHeaderEFlags());
431
432 // e_ehsize = ELF header size
433 W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
434 : sizeof(ELF::Elf32_Ehdr));
435
436 W.write<uint16_t>(0); // e_phentsize = prog header entry size
437 W.write<uint16_t>(0); // e_phnum = # prog header entries = 0
438
439 // e_shentsize = Section header entry size
440 W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
441 : sizeof(ELF::Elf32_Shdr));
442
443 // e_shnum = # of section header ents
444 W.write<uint16_t>(0);
445
446 // e_shstrndx = Section # of '.strtab'
447 assert(StringTableIndex < ELF::SHN_LORESERVE);
448 W.write<uint16_t>(StringTableIndex);
449 }
450
SymbolValue(const MCSymbol & Sym,const MCAsmLayout & Layout)451 uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
452 const MCAsmLayout &Layout) {
453 if (Sym.isCommon())
454 return Sym.getCommonAlignment();
455
456 uint64_t Res;
457 if (!Layout.getSymbolOffset(Sym, Res))
458 return 0;
459
460 if (Layout.getAssembler().isThumbFunc(&Sym))
461 Res |= 1;
462
463 return Res;
464 }
465
mergeTypeForSet(uint8_t origType,uint8_t newType)466 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
467 uint8_t Type = newType;
468
469 // Propagation rules:
470 // IFUNC > FUNC > OBJECT > NOTYPE
471 // TLS_OBJECT > OBJECT > NOTYPE
472 //
473 // dont let the new type degrade the old type
474 switch (origType) {
475 default:
476 break;
477 case ELF::STT_GNU_IFUNC:
478 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
479 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
480 Type = ELF::STT_GNU_IFUNC;
481 break;
482 case ELF::STT_FUNC:
483 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
484 Type == ELF::STT_TLS)
485 Type = ELF::STT_FUNC;
486 break;
487 case ELF::STT_OBJECT:
488 if (Type == ELF::STT_NOTYPE)
489 Type = ELF::STT_OBJECT;
490 break;
491 case ELF::STT_TLS:
492 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
493 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
494 Type = ELF::STT_TLS;
495 break;
496 }
497
498 return Type;
499 }
500
isIFunc(const MCSymbolELF * Symbol)501 static bool isIFunc(const MCSymbolELF *Symbol) {
502 while (Symbol->getType() != ELF::STT_GNU_IFUNC) {
503 const MCSymbolRefExpr *Value;
504 if (!Symbol->isVariable() ||
505 !(Value = dyn_cast<MCSymbolRefExpr>(Symbol->getVariableValue())) ||
506 Value->getKind() != MCSymbolRefExpr::VK_None ||
507 mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC)
508 return false;
509 Symbol = &cast<MCSymbolELF>(Value->getSymbol());
510 }
511 return true;
512 }
513
writeSymbol(SymbolTableWriter & Writer,uint32_t StringIndex,ELFSymbolData & MSD,const MCAsmLayout & Layout)514 void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
515 ELFSymbolData &MSD, const MCAsmLayout &Layout) {
516 const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
517 const MCSymbolELF *Base =
518 cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
519
520 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
521 // SHN_COMMON.
522 bool IsReserved = !Base || Symbol.isCommon();
523
524 // Binding and Type share the same byte as upper and lower nibbles
525 uint8_t Binding = Symbol.getBinding();
526 uint8_t Type = Symbol.getType();
527 if (isIFunc(&Symbol))
528 Type = ELF::STT_GNU_IFUNC;
529 if (Base) {
530 Type = mergeTypeForSet(Type, Base->getType());
531 }
532 uint8_t Info = (Binding << 4) | Type;
533
534 // Other and Visibility share the same byte with Visibility using the lower
535 // 2 bits
536 uint8_t Visibility = Symbol.getVisibility();
537 uint8_t Other = Symbol.getOther() | Visibility;
538
539 uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
540 uint64_t Size = 0;
541
542 const MCExpr *ESize = MSD.Symbol->getSize();
543 if (!ESize && Base) {
544 // For expressions like .set y, x+1, if y's size is unset, inherit from x.
545 ESize = Base->getSize();
546
547 // For `.size x, 2; y = x; .size y, 1; z = y; z1 = z; .symver y, y@v1`, z,
548 // z1, and y@v1's st_size equals y's. However, `Base` is `x` which will give
549 // us 2. Follow the MCSymbolRefExpr assignment chain, which covers most
550 // needs. MCBinaryExpr is not handled.
551 const MCSymbolELF *Sym = &Symbol;
552 while (Sym->isVariable()) {
553 if (auto *Expr =
554 dyn_cast<MCSymbolRefExpr>(Sym->getVariableValue(false))) {
555 Sym = cast<MCSymbolELF>(&Expr->getSymbol());
556 if (!Sym->getSize())
557 continue;
558 ESize = Sym->getSize();
559 }
560 break;
561 }
562 }
563
564 if (ESize) {
565 int64_t Res;
566 if (!ESize->evaluateKnownAbsolute(Res, Layout))
567 report_fatal_error("Size expression must be absolute.");
568 Size = Res;
569 }
570
571 // Write out the symbol table entry
572 Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
573 IsReserved);
574 }
575
isInSymtab(const MCAsmLayout & Layout,const MCSymbolELF & Symbol,bool Used,bool Renamed)576 bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
577 bool Used, bool Renamed) {
578 if (Symbol.isVariable()) {
579 const MCExpr *Expr = Symbol.getVariableValue();
580 // Target Expressions that are always inlined do not appear in the symtab
581 if (const auto *T = dyn_cast<MCTargetExpr>(Expr))
582 if (T->inlineAssignedExpr())
583 return false;
584 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
585 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
586 return false;
587 }
588 }
589
590 if (Used)
591 return true;
592
593 if (Renamed)
594 return false;
595
596 if (Symbol.isVariable() && Symbol.isUndefined()) {
597 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
598 Layout.getBaseSymbol(Symbol);
599 return false;
600 }
601
602 if (Symbol.isTemporary())
603 return false;
604
605 if (Symbol.getType() == ELF::STT_SECTION)
606 return false;
607
608 return true;
609 }
610
computeSymbolTable(MCAssembler & Asm,const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap,const RevGroupMapTy & RevGroupMap,SectionOffsetsTy & SectionOffsets)611 void ELFWriter::computeSymbolTable(
612 MCAssembler &Asm, const MCAsmLayout &Layout,
613 const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
614 SectionOffsetsTy &SectionOffsets) {
615 MCContext &Ctx = Asm.getContext();
616 SymbolTableWriter Writer(*this, is64Bit());
617
618 // Symbol table
619 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
620 MCSectionELF *SymtabSection =
621 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize);
622 SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
623 SymbolTableIndex = addToSectionTable(SymtabSection);
624
625 uint64_t SecStart = align(SymtabSection->getAlignment());
626
627 // The first entry is the undefined symbol entry.
628 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
629
630 std::vector<ELFSymbolData> LocalSymbolData;
631 std::vector<ELFSymbolData> ExternalSymbolData;
632 MutableArrayRef<std::pair<std::string, size_t>> FileNames =
633 Asm.getFileNames();
634 for (const std::pair<std::string, size_t> &F : FileNames)
635 StrTabBuilder.add(F.first);
636
637 // Add the data for the symbols.
638 bool HasLargeSectionIndex = false;
639 for (auto It : llvm::enumerate(Asm.symbols())) {
640 const auto &Symbol = cast<MCSymbolELF>(It.value());
641 bool Used = Symbol.isUsedInReloc();
642 bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
643 bool isSignature = Symbol.isSignature();
644
645 if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
646 OWriter.Renames.count(&Symbol)))
647 continue;
648
649 if (Symbol.isTemporary() && Symbol.isUndefined()) {
650 Ctx.reportError(SMLoc(), "Undefined temporary symbol " + Symbol.getName());
651 continue;
652 }
653
654 ELFSymbolData MSD;
655 MSD.Symbol = cast<MCSymbolELF>(&Symbol);
656 MSD.Order = It.index();
657
658 bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
659 assert(Local || !Symbol.isTemporary());
660
661 if (Symbol.isAbsolute()) {
662 MSD.SectionIndex = ELF::SHN_ABS;
663 } else if (Symbol.isCommon()) {
664 if (Symbol.isTargetCommon()) {
665 MSD.SectionIndex = Symbol.getIndex();
666 } else {
667 assert(!Local);
668 MSD.SectionIndex = ELF::SHN_COMMON;
669 }
670 } else if (Symbol.isUndefined()) {
671 if (isSignature && !Used) {
672 MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
673 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
674 HasLargeSectionIndex = true;
675 } else {
676 MSD.SectionIndex = ELF::SHN_UNDEF;
677 }
678 } else {
679 const MCSectionELF &Section =
680 static_cast<const MCSectionELF &>(Symbol.getSection());
681
682 // We may end up with a situation when section symbol is technically
683 // defined, but should not be. That happens because we explicitly
684 // pre-create few .debug_* sections to have accessors.
685 // And if these sections were not really defined in the code, but were
686 // referenced, we simply error out.
687 if (!Section.isRegistered()) {
688 assert(static_cast<const MCSymbolELF &>(Symbol).getType() ==
689 ELF::STT_SECTION);
690 Ctx.reportError(SMLoc(),
691 "Undefined section reference: " + Symbol.getName());
692 continue;
693 }
694
695 if (Mode == NonDwoOnly && isDwoSection(Section))
696 continue;
697 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
698 assert(MSD.SectionIndex && "Invalid section index!");
699 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
700 HasLargeSectionIndex = true;
701 }
702
703 StringRef Name = Symbol.getName();
704
705 // Sections have their own string table
706 if (Symbol.getType() != ELF::STT_SECTION) {
707 MSD.Name = Name;
708 StrTabBuilder.add(Name);
709 }
710
711 if (Local)
712 LocalSymbolData.push_back(MSD);
713 else
714 ExternalSymbolData.push_back(MSD);
715 }
716
717 // This holds the .symtab_shndx section index.
718 unsigned SymtabShndxSectionIndex = 0;
719
720 if (HasLargeSectionIndex) {
721 MCSectionELF *SymtabShndxSection =
722 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, 4);
723 SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
724 SymtabShndxSection->setAlignment(Align(4));
725 }
726
727 StrTabBuilder.finalize();
728
729 // Make the first STT_FILE precede previous local symbols.
730 unsigned Index = 1;
731 auto FileNameIt = FileNames.begin();
732 if (!FileNames.empty())
733 FileNames[0].second = 0;
734
735 for (ELFSymbolData &MSD : LocalSymbolData) {
736 // Emit STT_FILE symbols before their associated local symbols.
737 for (; FileNameIt != FileNames.end() && FileNameIt->second <= MSD.Order;
738 ++FileNameIt) {
739 Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
740 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
741 ELF::SHN_ABS, true);
742 ++Index;
743 }
744
745 unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
746 ? 0
747 : StrTabBuilder.getOffset(MSD.Name);
748 MSD.Symbol->setIndex(Index++);
749 writeSymbol(Writer, StringIndex, MSD, Layout);
750 }
751 for (; FileNameIt != FileNames.end(); ++FileNameIt) {
752 Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
753 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
754 ELF::SHN_ABS, true);
755 ++Index;
756 }
757
758 // Write the symbol table entries.
759 LastLocalSymbolIndex = Index;
760
761 for (ELFSymbolData &MSD : ExternalSymbolData) {
762 unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
763 MSD.Symbol->setIndex(Index++);
764 writeSymbol(Writer, StringIndex, MSD, Layout);
765 assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
766 }
767
768 uint64_t SecEnd = W.OS.tell();
769 SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
770
771 ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
772 if (ShndxIndexes.empty()) {
773 assert(SymtabShndxSectionIndex == 0);
774 return;
775 }
776 assert(SymtabShndxSectionIndex != 0);
777
778 SecStart = W.OS.tell();
779 const MCSectionELF *SymtabShndxSection =
780 SectionTable[SymtabShndxSectionIndex - 1];
781 for (uint32_t Index : ShndxIndexes)
782 write(Index);
783 SecEnd = W.OS.tell();
784 SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
785 }
786
writeAddrsigSection()787 void ELFWriter::writeAddrsigSection() {
788 for (const MCSymbol *Sym : OWriter.AddrsigSyms)
789 encodeULEB128(Sym->getIndex(), W.OS);
790 }
791
createRelocationSection(MCContext & Ctx,const MCSectionELF & Sec)792 MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
793 const MCSectionELF &Sec) {
794 if (OWriter.Relocations[&Sec].empty())
795 return nullptr;
796
797 const StringRef SectionName = Sec.getName();
798 bool Rela = usesRela(Sec);
799 std::string RelaSectionName = Rela ? ".rela" : ".rel";
800 RelaSectionName += SectionName;
801
802 unsigned EntrySize;
803 if (Rela)
804 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
805 else
806 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
807
808 unsigned Flags = ELF::SHF_INFO_LINK;
809 if (Sec.getFlags() & ELF::SHF_GROUP)
810 Flags = ELF::SHF_GROUP;
811
812 MCSectionELF *RelaSection = Ctx.createELFRelSection(
813 RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
814 Sec.getGroup(), &Sec);
815 RelaSection->setAlignment(is64Bit() ? Align(8) : Align(4));
816 return RelaSection;
817 }
818
819 // Include the debug info compression header.
maybeWriteCompression(uint32_t ChType,uint64_t Size,SmallVectorImpl<uint8_t> & CompressedContents,unsigned Alignment)820 bool ELFWriter::maybeWriteCompression(
821 uint32_t ChType, uint64_t Size,
822 SmallVectorImpl<uint8_t> &CompressedContents, unsigned Alignment) {
823 uint64_t HdrSize =
824 is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
825 if (Size <= HdrSize + CompressedContents.size())
826 return false;
827 // Platform specific header is followed by compressed data.
828 if (is64Bit()) {
829 // Write Elf64_Chdr header.
830 write(static_cast<ELF::Elf64_Word>(ChType));
831 write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
832 write(static_cast<ELF::Elf64_Xword>(Size));
833 write(static_cast<ELF::Elf64_Xword>(Alignment));
834 } else {
835 // Write Elf32_Chdr header otherwise.
836 write(static_cast<ELF::Elf32_Word>(ChType));
837 write(static_cast<ELF::Elf32_Word>(Size));
838 write(static_cast<ELF::Elf32_Word>(Alignment));
839 }
840 return true;
841 }
842
writeSectionData(const MCAssembler & Asm,MCSection & Sec,const MCAsmLayout & Layout)843 void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
844 const MCAsmLayout &Layout) {
845 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
846 StringRef SectionName = Section.getName();
847
848 auto &MC = Asm.getContext();
849 const auto &MAI = MC.getAsmInfo();
850
851 bool CompressionEnabled =
852 MAI->compressDebugSections() != DebugCompressionType::None;
853 if (!CompressionEnabled || !SectionName.startswith(".debug_")) {
854 Asm.writeSectionData(W.OS, &Section, Layout);
855 return;
856 }
857
858 assert(MAI->compressDebugSections() == DebugCompressionType::Z &&
859 "expected zlib style compression");
860
861 SmallVector<char, 128> UncompressedData;
862 raw_svector_ostream VecOS(UncompressedData);
863 Asm.writeSectionData(VecOS, &Section, Layout);
864
865 SmallVector<uint8_t, 128> Compressed;
866 const uint32_t ChType = ELF::ELFCOMPRESS_ZLIB;
867 compression::zlib::compress(
868 makeArrayRef(reinterpret_cast<uint8_t *>(UncompressedData.data()),
869 UncompressedData.size()),
870 Compressed);
871
872 if (!maybeWriteCompression(ChType, UncompressedData.size(), Compressed,
873 Sec.getAlignment())) {
874 W.OS << UncompressedData;
875 return;
876 }
877
878 Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
879 // Alignment field should reflect the requirements of
880 // the compressed section header.
881 Section.setAlignment(is64Bit() ? Align(8) : Align(4));
882 W.OS << toStringRef(Compressed);
883 }
884
WriteSecHdrEntry(uint32_t Name,uint32_t Type,uint64_t Flags,uint64_t Address,uint64_t Offset,uint64_t Size,uint32_t Link,uint32_t Info,uint64_t Alignment,uint64_t EntrySize)885 void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
886 uint64_t Address, uint64_t Offset,
887 uint64_t Size, uint32_t Link, uint32_t Info,
888 uint64_t Alignment, uint64_t EntrySize) {
889 W.write<uint32_t>(Name); // sh_name: index into string table
890 W.write<uint32_t>(Type); // sh_type
891 WriteWord(Flags); // sh_flags
892 WriteWord(Address); // sh_addr
893 WriteWord(Offset); // sh_offset
894 WriteWord(Size); // sh_size
895 W.write<uint32_t>(Link); // sh_link
896 W.write<uint32_t>(Info); // sh_info
897 WriteWord(Alignment); // sh_addralign
898 WriteWord(EntrySize); // sh_entsize
899 }
900
writeRelocations(const MCAssembler & Asm,const MCSectionELF & Sec)901 void ELFWriter::writeRelocations(const MCAssembler &Asm,
902 const MCSectionELF &Sec) {
903 std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
904
905 // We record relocations by pushing to the end of a vector. Reverse the vector
906 // to get the relocations in the order they were created.
907 // In most cases that is not important, but it can be for special sections
908 // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
909 std::reverse(Relocs.begin(), Relocs.end());
910
911 // Sort the relocation entries. MIPS needs this.
912 OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
913
914 const bool Rela = usesRela(Sec);
915 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
916 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
917 unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
918
919 if (is64Bit()) {
920 write(Entry.Offset);
921 if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
922 write(uint32_t(Index));
923
924 write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
925 write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
926 write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
927 write(OWriter.TargetObjectWriter->getRType(Entry.Type));
928 } else {
929 struct ELF::Elf64_Rela ERE64;
930 ERE64.setSymbolAndType(Index, Entry.Type);
931 write(ERE64.r_info);
932 }
933 if (Rela)
934 write(Entry.Addend);
935 } else {
936 write(uint32_t(Entry.Offset));
937
938 struct ELF::Elf32_Rela ERE32;
939 ERE32.setSymbolAndType(Index, Entry.Type);
940 write(ERE32.r_info);
941
942 if (Rela)
943 write(uint32_t(Entry.Addend));
944
945 if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
946 if (uint32_t RType =
947 OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
948 write(uint32_t(Entry.Offset));
949
950 ERE32.setSymbolAndType(0, RType);
951 write(ERE32.r_info);
952 write(uint32_t(0));
953 }
954 if (uint32_t RType =
955 OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
956 write(uint32_t(Entry.Offset));
957
958 ERE32.setSymbolAndType(0, RType);
959 write(ERE32.r_info);
960 write(uint32_t(0));
961 }
962 }
963 }
964 }
965 }
966
writeSection(const SectionIndexMapTy & SectionIndexMap,uint32_t GroupSymbolIndex,uint64_t Offset,uint64_t Size,const MCSectionELF & Section)967 void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
968 uint32_t GroupSymbolIndex, uint64_t Offset,
969 uint64_t Size, const MCSectionELF &Section) {
970 uint64_t sh_link = 0;
971 uint64_t sh_info = 0;
972
973 switch(Section.getType()) {
974 default:
975 // Nothing to do.
976 break;
977
978 case ELF::SHT_DYNAMIC:
979 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
980
981 case ELF::SHT_REL:
982 case ELF::SHT_RELA: {
983 sh_link = SymbolTableIndex;
984 assert(sh_link && ".symtab not found");
985 const MCSection *InfoSection = Section.getLinkedToSection();
986 sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
987 break;
988 }
989
990 case ELF::SHT_SYMTAB:
991 sh_link = StringTableIndex;
992 sh_info = LastLocalSymbolIndex;
993 break;
994
995 case ELF::SHT_SYMTAB_SHNDX:
996 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
997 case ELF::SHT_LLVM_ADDRSIG:
998 sh_link = SymbolTableIndex;
999 break;
1000
1001 case ELF::SHT_GROUP:
1002 sh_link = SymbolTableIndex;
1003 sh_info = GroupSymbolIndex;
1004 break;
1005 }
1006
1007 if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
1008 // If the value in the associated metadata is not a definition, Sym will be
1009 // undefined. Represent this with sh_link=0.
1010 const MCSymbol *Sym = Section.getLinkedToSymbol();
1011 if (Sym && Sym->isInSection()) {
1012 const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
1013 sh_link = SectionIndexMap.lookup(Sec);
1014 }
1015 }
1016
1017 WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
1018 Section.getType(), Section.getFlags(), 0, Offset, Size,
1019 sh_link, sh_info, Section.getAlignment(),
1020 Section.getEntrySize());
1021 }
1022
writeSectionHeader(const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap,const SectionOffsetsTy & SectionOffsets)1023 void ELFWriter::writeSectionHeader(
1024 const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1025 const SectionOffsetsTy &SectionOffsets) {
1026 const unsigned NumSections = SectionTable.size();
1027
1028 // Null section first.
1029 uint64_t FirstSectionSize =
1030 (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1031 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1032
1033 for (const MCSectionELF *Section : SectionTable) {
1034 uint32_t GroupSymbolIndex;
1035 unsigned Type = Section->getType();
1036 if (Type != ELF::SHT_GROUP)
1037 GroupSymbolIndex = 0;
1038 else
1039 GroupSymbolIndex = Section->getGroup()->getIndex();
1040
1041 const std::pair<uint64_t, uint64_t> &Offsets =
1042 SectionOffsets.find(Section)->second;
1043 uint64_t Size;
1044 if (Type == ELF::SHT_NOBITS)
1045 Size = Layout.getSectionAddressSize(Section);
1046 else
1047 Size = Offsets.second - Offsets.first;
1048
1049 writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1050 *Section);
1051 }
1052 }
1053
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)1054 uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
1055 uint64_t StartOffset = W.OS.tell();
1056
1057 MCContext &Ctx = Asm.getContext();
1058 MCSectionELF *StrtabSection =
1059 Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1060 StringTableIndex = addToSectionTable(StrtabSection);
1061
1062 RevGroupMapTy RevGroupMap;
1063 SectionIndexMapTy SectionIndexMap;
1064
1065 std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1066
1067 // Write out the ELF header ...
1068 writeHeader(Asm);
1069
1070 // ... then the sections ...
1071 SectionOffsetsTy SectionOffsets;
1072 std::vector<MCSectionELF *> Groups;
1073 std::vector<MCSectionELF *> Relocations;
1074 for (MCSection &Sec : Asm) {
1075 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1076 if (Mode == NonDwoOnly && isDwoSection(Section))
1077 continue;
1078 if (Mode == DwoOnly && !isDwoSection(Section))
1079 continue;
1080
1081 // Remember the offset into the file for this section.
1082 const uint64_t SecStart = align(Section.getAlignment());
1083
1084 const MCSymbolELF *SignatureSymbol = Section.getGroup();
1085 writeSectionData(Asm, Section, Layout);
1086
1087 uint64_t SecEnd = W.OS.tell();
1088 SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1089
1090 MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1091
1092 if (SignatureSymbol) {
1093 unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1094 if (!GroupIdx) {
1095 MCSectionELF *Group =
1096 Ctx.createELFGroupSection(SignatureSymbol, Section.isComdat());
1097 GroupIdx = addToSectionTable(Group);
1098 Group->setAlignment(Align(4));
1099 Groups.push_back(Group);
1100 }
1101 std::vector<const MCSectionELF *> &Members =
1102 GroupMembers[SignatureSymbol];
1103 Members.push_back(&Section);
1104 if (RelSection)
1105 Members.push_back(RelSection);
1106 }
1107
1108 SectionIndexMap[&Section] = addToSectionTable(&Section);
1109 if (RelSection) {
1110 SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1111 Relocations.push_back(RelSection);
1112 }
1113
1114 OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
1115 }
1116
1117 for (MCSectionELF *Group : Groups) {
1118 // Remember the offset into the file for this section.
1119 const uint64_t SecStart = align(Group->getAlignment());
1120
1121 const MCSymbol *SignatureSymbol = Group->getGroup();
1122 assert(SignatureSymbol);
1123 write(uint32_t(Group->isComdat() ? unsigned(ELF::GRP_COMDAT) : 0));
1124 for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1125 uint32_t SecIndex = SectionIndexMap.lookup(Member);
1126 write(SecIndex);
1127 }
1128
1129 uint64_t SecEnd = W.OS.tell();
1130 SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1131 }
1132
1133 if (Mode == DwoOnly) {
1134 // dwo files don't have symbol tables or relocations, but they do have
1135 // string tables.
1136 StrTabBuilder.finalize();
1137 } else {
1138 MCSectionELF *AddrsigSection;
1139 if (OWriter.EmitAddrsigSection) {
1140 AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
1141 ELF::SHF_EXCLUDE);
1142 addToSectionTable(AddrsigSection);
1143 }
1144
1145 // Compute symbol table information.
1146 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1147 SectionOffsets);
1148
1149 for (MCSectionELF *RelSection : Relocations) {
1150 // Remember the offset into the file for this section.
1151 const uint64_t SecStart = align(RelSection->getAlignment());
1152
1153 writeRelocations(Asm,
1154 cast<MCSectionELF>(*RelSection->getLinkedToSection()));
1155
1156 uint64_t SecEnd = W.OS.tell();
1157 SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1158 }
1159
1160 if (OWriter.EmitAddrsigSection) {
1161 uint64_t SecStart = W.OS.tell();
1162 writeAddrsigSection();
1163 uint64_t SecEnd = W.OS.tell();
1164 SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
1165 }
1166 }
1167
1168 {
1169 uint64_t SecStart = W.OS.tell();
1170 StrTabBuilder.write(W.OS);
1171 SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
1172 }
1173
1174 const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
1175
1176 // ... then the section header table ...
1177 writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1178
1179 uint16_t NumSections = support::endian::byte_swap<uint16_t>(
1180 (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
1181 : SectionTable.size() + 1,
1182 W.Endian);
1183 unsigned NumSectionsOffset;
1184
1185 auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
1186 if (is64Bit()) {
1187 uint64_t Val =
1188 support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
1189 Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1190 offsetof(ELF::Elf64_Ehdr, e_shoff));
1191 NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1192 } else {
1193 uint32_t Val =
1194 support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
1195 Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1196 offsetof(ELF::Elf32_Ehdr, e_shoff));
1197 NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1198 }
1199 Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1200 NumSectionsOffset);
1201
1202 return W.OS.tell() - StartOffset;
1203 }
1204
hasRelocationAddend() const1205 bool ELFObjectWriter::hasRelocationAddend() const {
1206 return TargetObjectWriter->hasRelocationAddend();
1207 }
1208
executePostLayoutBinding(MCAssembler & Asm,const MCAsmLayout & Layout)1209 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1210 const MCAsmLayout &Layout) {
1211 // The presence of symbol versions causes undefined symbols and
1212 // versions declared with @@@ to be renamed.
1213 for (const MCAssembler::Symver &S : Asm.Symvers) {
1214 StringRef AliasName = S.Name;
1215 const auto &Symbol = cast<MCSymbolELF>(*S.Sym);
1216 size_t Pos = AliasName.find('@');
1217 assert(Pos != StringRef::npos);
1218
1219 StringRef Prefix = AliasName.substr(0, Pos);
1220 StringRef Rest = AliasName.substr(Pos);
1221 StringRef Tail = Rest;
1222 if (Rest.startswith("@@@"))
1223 Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
1224
1225 auto *Alias =
1226 cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
1227 Asm.registerSymbol(*Alias);
1228 const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
1229 Alias->setVariableValue(Value);
1230
1231 // Aliases defined with .symvar copy the binding from the symbol they alias.
1232 // This is the first place we are able to copy this information.
1233 Alias->setBinding(Symbol.getBinding());
1234 Alias->setVisibility(Symbol.getVisibility());
1235 Alias->setOther(Symbol.getOther());
1236
1237 if (!Symbol.isUndefined() && S.KeepOriginalSym)
1238 continue;
1239
1240 if (Symbol.isUndefined() && Rest.startswith("@@") &&
1241 !Rest.startswith("@@@")) {
1242 Asm.getContext().reportError(S.Loc, "default version symbol " +
1243 AliasName + " must be defined");
1244 continue;
1245 }
1246
1247 if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
1248 Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
1249 Symbol.getName());
1250 continue;
1251 }
1252
1253 Renames.insert(std::make_pair(&Symbol, Alias));
1254 }
1255
1256 for (const MCSymbol *&Sym : AddrsigSyms) {
1257 if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
1258 Sym = R;
1259 if (Sym->isInSection() && Sym->getName().startswith(".L"))
1260 Sym = Sym->getSection().getBeginSymbol();
1261 Sym->setUsedInReloc();
1262 }
1263 }
1264
1265 // It is always valid to create a relocation with a symbol. It is preferable
1266 // to use a relocation with a section if that is possible. Using the section
1267 // allows us to omit some local symbols from the symbol table.
shouldRelocateWithSymbol(const MCAssembler & Asm,const MCSymbolRefExpr * RefA,const MCSymbolELF * Sym,uint64_t C,unsigned Type) const1268 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1269 const MCSymbolRefExpr *RefA,
1270 const MCSymbolELF *Sym,
1271 uint64_t C,
1272 unsigned Type) const {
1273 // A PCRel relocation to an absolute value has no symbol (or section). We
1274 // represent that with a relocation to a null section.
1275 if (!RefA)
1276 return false;
1277
1278 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
1279 switch (Kind) {
1280 default:
1281 break;
1282 // The .odp creation emits a relocation against the symbol ".TOC." which
1283 // create a R_PPC64_TOC relocation. However the relocation symbol name
1284 // in final object creation should be NULL, since the symbol does not
1285 // really exist, it is just the reference to TOC base for the current
1286 // object file. Since the symbol is undefined, returning false results
1287 // in a relocation with a null section which is the desired result.
1288 case MCSymbolRefExpr::VK_PPC_TOCBASE:
1289 return false;
1290
1291 // These VariantKind cause the relocation to refer to something other than
1292 // the symbol itself, like a linker generated table. Since the address of
1293 // symbol is not relevant, we cannot replace the symbol with the
1294 // section and patch the difference in the addend.
1295 case MCSymbolRefExpr::VK_GOT:
1296 case MCSymbolRefExpr::VK_PLT:
1297 case MCSymbolRefExpr::VK_GOTPCREL:
1298 case MCSymbolRefExpr::VK_GOTPCREL_NORELAX:
1299 case MCSymbolRefExpr::VK_PPC_GOT_LO:
1300 case MCSymbolRefExpr::VK_PPC_GOT_HI:
1301 case MCSymbolRefExpr::VK_PPC_GOT_HA:
1302 return true;
1303 }
1304
1305 // An undefined symbol is not in any section, so the relocation has to point
1306 // to the symbol itself.
1307 assert(Sym && "Expected a symbol");
1308 if (Sym->isUndefined())
1309 return true;
1310
1311 unsigned Binding = Sym->getBinding();
1312 switch(Binding) {
1313 default:
1314 llvm_unreachable("Invalid Binding");
1315 case ELF::STB_LOCAL:
1316 break;
1317 case ELF::STB_WEAK:
1318 // If the symbol is weak, it might be overridden by a symbol in another
1319 // file. The relocation has to point to the symbol so that the linker
1320 // can update it.
1321 return true;
1322 case ELF::STB_GLOBAL:
1323 case ELF::STB_GNU_UNIQUE:
1324 // Global ELF symbols can be preempted by the dynamic linker. The relocation
1325 // has to point to the symbol for a reason analogous to the STB_WEAK case.
1326 return true;
1327 }
1328
1329 // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1330 // reloc that the dynamic loader will use to resolve the address at startup
1331 // time.
1332 if (Sym->getType() == ELF::STT_GNU_IFUNC)
1333 return true;
1334
1335 // If a relocation points to a mergeable section, we have to be careful.
1336 // If the offset is zero, a relocation with the section will encode the
1337 // same information. With a non-zero offset, the situation is different.
1338 // For example, a relocation can point 42 bytes past the end of a string.
1339 // If we change such a relocation to use the section, the linker would think
1340 // that it pointed to another string and subtracting 42 at runtime will
1341 // produce the wrong value.
1342 if (Sym->isInSection()) {
1343 auto &Sec = cast<MCSectionELF>(Sym->getSection());
1344 unsigned Flags = Sec.getFlags();
1345 if (Flags & ELF::SHF_MERGE) {
1346 if (C != 0)
1347 return true;
1348
1349 // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1350 // (http://sourceware.org/PR16794).
1351 if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1352 Type == ELF::R_386_GOTOFF)
1353 return true;
1354
1355 // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1356 // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1357 // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1358 // range of a MergeInputSection. We could introduce a new RelExpr member
1359 // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1360 // but the complexity is unnecessary given that GNU as keeps the original
1361 // symbol for this case as well.
1362 if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1363 !hasRelocationAddend())
1364 return true;
1365 }
1366
1367 // Most TLS relocations use a got, so they need the symbol. Even those that
1368 // are just an offset (@tpoff), require a symbol in gold versions before
1369 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1370 // http://sourceware.org/PR16773.
1371 if (Flags & ELF::SHF_TLS)
1372 return true;
1373 }
1374
1375 // If the symbol is a thumb function the final relocation must set the lowest
1376 // bit. With a symbol that is done by just having the symbol have that bit
1377 // set, so we would lose the bit if we relocated with the section.
1378 // FIXME: We could use the section but add the bit to the relocation value.
1379 if (Asm.isThumbFunc(Sym))
1380 return true;
1381
1382 if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
1383 return true;
1384 return false;
1385 }
1386
recordRelocation(MCAssembler & Asm,const MCAsmLayout & Layout,const MCFragment * Fragment,const MCFixup & Fixup,MCValue Target,uint64_t & FixedValue)1387 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1388 const MCAsmLayout &Layout,
1389 const MCFragment *Fragment,
1390 const MCFixup &Fixup, MCValue Target,
1391 uint64_t &FixedValue) {
1392 MCAsmBackend &Backend = Asm.getBackend();
1393 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1394 MCFixupKindInfo::FKF_IsPCRel;
1395 const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1396 uint64_t C = Target.getConstant();
1397 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1398 MCContext &Ctx = Asm.getContext();
1399
1400 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1401 const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
1402 if (SymB.isUndefined()) {
1403 Ctx.reportError(Fixup.getLoc(),
1404 Twine("symbol '") + SymB.getName() +
1405 "' can not be undefined in a subtraction expression");
1406 return;
1407 }
1408
1409 assert(!SymB.isAbsolute() && "Should have been folded");
1410 const MCSection &SecB = SymB.getSection();
1411 if (&SecB != &FixupSection) {
1412 Ctx.reportError(Fixup.getLoc(),
1413 "Cannot represent a difference across sections");
1414 return;
1415 }
1416
1417 assert(!IsPCRel && "should have been folded");
1418 IsPCRel = true;
1419 C += FixupOffset - Layout.getSymbolOffset(SymB);
1420 }
1421
1422 // We either rejected the fixup or folded B into C at this point.
1423 const MCSymbolRefExpr *RefA = Target.getSymA();
1424 const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
1425
1426 bool ViaWeakRef = false;
1427 if (SymA && SymA->isVariable()) {
1428 const MCExpr *Expr = SymA->getVariableValue();
1429 if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1430 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1431 SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1432 ViaWeakRef = true;
1433 }
1434 }
1435 }
1436
1437 const MCSectionELF *SecA = (SymA && SymA->isInSection())
1438 ? cast<MCSectionELF>(&SymA->getSection())
1439 : nullptr;
1440 if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1441 return;
1442
1443 unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1444 const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1445 // Emiting relocation with sybmol for CG Profile to help with --cg-profile.
1446 bool RelocateWithSymbol =
1447 shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type) ||
1448 (Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
1449 uint64_t Addend = 0;
1450
1451 FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
1452 ? C + Layout.getSymbolOffset(*SymA)
1453 : C;
1454 if (hasRelocationAddend()) {
1455 Addend = FixedValue;
1456 FixedValue = 0;
1457 }
1458
1459 if (!RelocateWithSymbol) {
1460 const auto *SectionSymbol =
1461 SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
1462 if (SectionSymbol)
1463 SectionSymbol->setUsedInReloc();
1464 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
1465 Relocations[&FixupSection].push_back(Rec);
1466 return;
1467 }
1468
1469 const MCSymbolELF *RenamedSymA = SymA;
1470 if (SymA) {
1471 if (const MCSymbolELF *R = Renames.lookup(SymA))
1472 RenamedSymA = R;
1473
1474 if (ViaWeakRef)
1475 RenamedSymA->setIsWeakrefUsedInReloc();
1476 else
1477 RenamedSymA->setUsedInReloc();
1478 }
1479 ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
1480 Relocations[&FixupSection].push_back(Rec);
1481 }
1482
isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler & Asm,const MCSymbol & SA,const MCFragment & FB,bool InSet,bool IsPCRel) const1483 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1484 const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1485 bool InSet, bool IsPCRel) const {
1486 const auto &SymA = cast<MCSymbolELF>(SA);
1487 if (IsPCRel) {
1488 assert(!InSet);
1489 if (SymA.getBinding() != ELF::STB_LOCAL ||
1490 SymA.getType() == ELF::STT_GNU_IFUNC)
1491 return false;
1492 }
1493 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1494 InSet, IsPCRel);
1495 }
1496
1497 std::unique_ptr<MCObjectWriter>
createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,bool IsLittleEndian)1498 llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1499 raw_pwrite_stream &OS, bool IsLittleEndian) {
1500 return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
1501 IsLittleEndian);
1502 }
1503
1504 std::unique_ptr<MCObjectWriter>
createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,raw_pwrite_stream & DwoOS,bool IsLittleEndian)1505 llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1506 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
1507 bool IsLittleEndian) {
1508 return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
1509 IsLittleEndian);
1510 }
1511