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