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 
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 
97   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(uint64_t Size,
148                              SmallVectorImpl<char> &CompressedContents,
149                              bool ZLibStyle, unsigned Alignment);
150 
151 public:
152   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 
157   void WriteWord(uint64_t Word) {
158     if (is64Bit())
159       W.write<uint64_t>(Word);
160     else
161       W.write<uint32_t>(Word);
162   }
163 
164   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   bool EmitAddrsigSection = false;
225   std::vector<const MCSymbol *> AddrsigSyms;
226 
227   bool hasRelocationAddend() const;
228 
229   bool shouldRelocateWithSymbol(const MCAssembler &Asm,
230                                 const MCSymbolRefExpr *RefA,
231                                 const MCSymbolELF *Sym, uint64_t C,
232                                 unsigned Type) const;
233 
234 public:
235   ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
236       : TargetObjectWriter(std::move(MOTW)) {}
237 
238   void reset() override {
239     SeenGnuAbi = false;
240     Relocations.clear();
241     Renames.clear();
242     MCObjectWriter::reset();
243   }
244 
245   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
246                                               const MCSymbol &SymA,
247                                               const MCFragment &FB, bool InSet,
248                                               bool IsPCRel) const override;
249 
250   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
251                                const MCSectionELF *From,
252                                const MCSectionELF *To) {
253     return true;
254   }
255 
256   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
257                         const MCFragment *Fragment, const MCFixup &Fixup,
258                         MCValue Target, uint64_t &FixedValue) override;
259 
260   void executePostLayoutBinding(MCAssembler &Asm,
261                                 const MCAsmLayout &Layout) override;
262 
263   void markGnuAbi() override { SeenGnuAbi = true; }
264   bool seenGnuAbi() const { return SeenGnuAbi; }
265   void emitAddrsigSection() override { EmitAddrsigSection = true; }
266   void addAddrsigSymbol(const MCSymbol *Sym) override {
267     AddrsigSyms.push_back(Sym);
268   }
269 
270   friend struct ELFWriter;
271 };
272 
273 class ELFSingleObjectWriter : public ELFObjectWriter {
274   raw_pwrite_stream &OS;
275   bool IsLittleEndian;
276 
277 public:
278   ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
279                         raw_pwrite_stream &OS, bool IsLittleEndian)
280       : ELFObjectWriter(std::move(MOTW)), OS(OS),
281         IsLittleEndian(IsLittleEndian) {}
282 
283   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
284     return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
285         .writeObject(Asm, Layout);
286   }
287 
288   friend struct ELFWriter;
289 };
290 
291 class ELFDwoObjectWriter : public ELFObjectWriter {
292   raw_pwrite_stream &OS, &DwoOS;
293   bool IsLittleEndian;
294 
295 public:
296   ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
297                      raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
298                      bool IsLittleEndian)
299       : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
300         IsLittleEndian(IsLittleEndian) {}
301 
302   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
303                                const MCSectionELF *From,
304                                const MCSectionELF *To) override {
305     if (isDwoSection(*From)) {
306       Ctx.reportError(Loc, "A dwo section may not contain relocations");
307       return false;
308     }
309     if (To && isDwoSection(*To)) {
310       Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
311       return false;
312     }
313     return true;
314   }
315 
316   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
317     uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
318                         .writeObject(Asm, Layout);
319     Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
320                 .writeObject(Asm, Layout);
321     return Size;
322   }
323 };
324 
325 } // end anonymous namespace
326 
327 uint64_t ELFWriter::align(unsigned Alignment) {
328   uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
329   W.OS.write_zeros(NewOffset - Offset);
330   return NewOffset;
331 }
332 
333 unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
334   SectionTable.push_back(Sec);
335   StrTabBuilder.add(Sec->getName());
336   return SectionTable.size();
337 }
338 
339 void SymbolTableWriter::createSymtabShndx() {
340   if (!ShndxIndexes.empty())
341     return;
342 
343   ShndxIndexes.resize(NumWritten);
344 }
345 
346 template <typename T> void SymbolTableWriter::write(T Value) {
347   EWriter.write(Value);
348 }
349 
350 SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
351     : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
352 
353 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
354                                     uint64_t size, uint8_t other,
355                                     uint32_t shndx, bool Reserved) {
356   bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
357 
358   if (LargeIndex)
359     createSymtabShndx();
360 
361   if (!ShndxIndexes.empty()) {
362     if (LargeIndex)
363       ShndxIndexes.push_back(shndx);
364     else
365       ShndxIndexes.push_back(0);
366   }
367 
368   uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
369 
370   if (Is64Bit) {
371     write(name);  // st_name
372     write(info);  // st_info
373     write(other); // st_other
374     write(Index); // st_shndx
375     write(value); // st_value
376     write(size);  // st_size
377   } else {
378     write(name);            // st_name
379     write(uint32_t(value)); // st_value
380     write(uint32_t(size));  // st_size
381     write(info);            // st_info
382     write(other);           // st_other
383     write(Index);           // st_shndx
384   }
385 
386   ++NumWritten;
387 }
388 
389 bool ELFWriter::is64Bit() const {
390   return OWriter.TargetObjectWriter->is64Bit();
391 }
392 
393 bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
394   return OWriter.hasRelocationAddend() &&
395          Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
396 }
397 
398 // Emit the ELF header.
399 void ELFWriter::writeHeader(const MCAssembler &Asm) {
400   // ELF Header
401   // ----------
402   //
403   // Note
404   // ----
405   // emitWord method behaves differently for ELF32 and ELF64, writing
406   // 4 bytes in the former and 8 in the latter.
407 
408   W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
409 
410   W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
411 
412   // e_ident[EI_DATA]
413   W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
414                                            : ELF::ELFDATA2MSB);
415 
416   W.OS << char(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
417   // e_ident[EI_OSABI]
418   uint8_t OSABI = OWriter.TargetObjectWriter->getOSABI();
419   W.OS << char(OSABI == ELF::ELFOSABI_NONE && OWriter.seenGnuAbi()
420                    ? int(ELF::ELFOSABI_GNU)
421                    : OSABI);
422   // e_ident[EI_ABIVERSION]
423   W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
424 
425   W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
426 
427   W.write<uint16_t>(ELF::ET_REL);             // e_type
428 
429   W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
430 
431   W.write<uint32_t>(ELF::EV_CURRENT);         // e_version
432   WriteWord(0);                    // e_entry, no entry point in .o file
433   WriteWord(0);                    // e_phoff, no program header for .o
434   WriteWord(0);                     // e_shoff = sec hdr table off in bytes
435 
436   // e_flags = whatever the target wants
437   W.write<uint32_t>(Asm.getELFHeaderEFlags());
438 
439   // e_ehsize = ELF header size
440   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
441                               : sizeof(ELF::Elf32_Ehdr));
442 
443   W.write<uint16_t>(0);                  // e_phentsize = prog header entry size
444   W.write<uint16_t>(0);                  // e_phnum = # prog header entries = 0
445 
446   // e_shentsize = Section header entry size
447   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
448                               : sizeof(ELF::Elf32_Shdr));
449 
450   // e_shnum     = # of section header ents
451   W.write<uint16_t>(0);
452 
453   // e_shstrndx  = Section # of '.strtab'
454   assert(StringTableIndex < ELF::SHN_LORESERVE);
455   W.write<uint16_t>(StringTableIndex);
456 }
457 
458 uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
459                                 const MCAsmLayout &Layout) {
460   if (Sym.isCommon())
461     return Sym.getCommonAlignment();
462 
463   uint64_t Res;
464   if (!Layout.getSymbolOffset(Sym, Res))
465     return 0;
466 
467   if (Layout.getAssembler().isThumbFunc(&Sym))
468     Res |= 1;
469 
470   return Res;
471 }
472 
473 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
474   uint8_t Type = newType;
475 
476   // Propagation rules:
477   // IFUNC > FUNC > OBJECT > NOTYPE
478   // TLS_OBJECT > OBJECT > NOTYPE
479   //
480   // dont let the new type degrade the old type
481   switch (origType) {
482   default:
483     break;
484   case ELF::STT_GNU_IFUNC:
485     if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
486         Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
487       Type = ELF::STT_GNU_IFUNC;
488     break;
489   case ELF::STT_FUNC:
490     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
491         Type == ELF::STT_TLS)
492       Type = ELF::STT_FUNC;
493     break;
494   case ELF::STT_OBJECT:
495     if (Type == ELF::STT_NOTYPE)
496       Type = ELF::STT_OBJECT;
497     break;
498   case ELF::STT_TLS:
499     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
500         Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
501       Type = ELF::STT_TLS;
502     break;
503   }
504 
505   return Type;
506 }
507 
508 static bool isIFunc(const MCSymbolELF *Symbol) {
509   while (Symbol->getType() != ELF::STT_GNU_IFUNC) {
510     const MCSymbolRefExpr *Value;
511     if (!Symbol->isVariable() ||
512         !(Value = dyn_cast<MCSymbolRefExpr>(Symbol->getVariableValue())) ||
513         Value->getKind() != MCSymbolRefExpr::VK_None ||
514         mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC)
515       return false;
516     Symbol = &cast<MCSymbolELF>(Value->getSymbol());
517   }
518   return true;
519 }
520 
521 void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
522                             ELFSymbolData &MSD, const MCAsmLayout &Layout) {
523   const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
524   const MCSymbolELF *Base =
525       cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
526 
527   // This has to be in sync with when computeSymbolTable uses SHN_ABS or
528   // SHN_COMMON.
529   bool IsReserved = !Base || Symbol.isCommon();
530 
531   // Binding and Type share the same byte as upper and lower nibbles
532   uint8_t Binding = Symbol.getBinding();
533   uint8_t Type = Symbol.getType();
534   if (isIFunc(&Symbol))
535     Type = ELF::STT_GNU_IFUNC;
536   if (Base) {
537     Type = mergeTypeForSet(Type, Base->getType());
538   }
539   uint8_t Info = (Binding << 4) | Type;
540 
541   // Other and Visibility share the same byte with Visibility using the lower
542   // 2 bits
543   uint8_t Visibility = Symbol.getVisibility();
544   uint8_t Other = Symbol.getOther() | Visibility;
545 
546   uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
547   uint64_t Size = 0;
548 
549   const MCExpr *ESize = MSD.Symbol->getSize();
550   if (!ESize && Base)
551     ESize = Base->getSize();
552 
553   if (ESize) {
554     int64_t Res;
555     if (!ESize->evaluateKnownAbsolute(Res, Layout))
556       report_fatal_error("Size expression must be absolute.");
557     Size = Res;
558   }
559 
560   // Write out the symbol table entry
561   Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
562                      IsReserved);
563 }
564 
565 bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
566                            bool Used, bool Renamed) {
567   if (Symbol.isVariable()) {
568     const MCExpr *Expr = Symbol.getVariableValue();
569     // Target Expressions that are always inlined do not appear in the symtab
570     if (const auto *T = dyn_cast<MCTargetExpr>(Expr))
571       if (T->inlineAssignedExpr())
572         return false;
573     if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
574       if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
575         return false;
576     }
577   }
578 
579   if (Used)
580     return true;
581 
582   if (Renamed)
583     return false;
584 
585   if (Symbol.isVariable() && Symbol.isUndefined()) {
586     // FIXME: this is here just to diagnose the case of a var = commmon_sym.
587     Layout.getBaseSymbol(Symbol);
588     return false;
589   }
590 
591   if (Symbol.isTemporary())
592     return false;
593 
594   if (Symbol.getType() == ELF::STT_SECTION)
595     return false;
596 
597   return true;
598 }
599 
600 void ELFWriter::computeSymbolTable(
601     MCAssembler &Asm, const MCAsmLayout &Layout,
602     const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
603     SectionOffsetsTy &SectionOffsets) {
604   MCContext &Ctx = Asm.getContext();
605   SymbolTableWriter Writer(*this, is64Bit());
606 
607   // Symbol table
608   unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
609   MCSectionELF *SymtabSection =
610       Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize);
611   SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
612   SymbolTableIndex = addToSectionTable(SymtabSection);
613 
614   uint64_t SecStart = align(SymtabSection->getAlignment());
615 
616   // The first entry is the undefined symbol entry.
617   Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
618 
619   std::vector<ELFSymbolData> LocalSymbolData;
620   std::vector<ELFSymbolData> ExternalSymbolData;
621   MutableArrayRef<std::pair<std::string, size_t>> FileNames =
622       Asm.getFileNames();
623   for (const std::pair<std::string, size_t> &F : FileNames)
624     StrTabBuilder.add(F.first);
625 
626   // Add the data for the symbols.
627   bool HasLargeSectionIndex = false;
628   for (auto It : llvm::enumerate(Asm.symbols())) {
629     const auto &Symbol = cast<MCSymbolELF>(It.value());
630     bool Used = Symbol.isUsedInReloc();
631     bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
632     bool isSignature = Symbol.isSignature();
633 
634     if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
635                     OWriter.Renames.count(&Symbol)))
636       continue;
637 
638     if (Symbol.isTemporary() && Symbol.isUndefined()) {
639       Ctx.reportError(SMLoc(), "Undefined temporary symbol " + Symbol.getName());
640       continue;
641     }
642 
643     ELFSymbolData MSD;
644     MSD.Symbol = cast<MCSymbolELF>(&Symbol);
645     MSD.Order = It.index();
646 
647     bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
648     assert(Local || !Symbol.isTemporary());
649 
650     if (Symbol.isAbsolute()) {
651       MSD.SectionIndex = ELF::SHN_ABS;
652     } else if (Symbol.isCommon()) {
653       if (Symbol.isTargetCommon()) {
654         MSD.SectionIndex = Symbol.getIndex();
655       } else {
656         assert(!Local);
657         MSD.SectionIndex = ELF::SHN_COMMON;
658       }
659     } else if (Symbol.isUndefined()) {
660       if (isSignature && !Used) {
661         MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
662         if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
663           HasLargeSectionIndex = true;
664       } else {
665         MSD.SectionIndex = ELF::SHN_UNDEF;
666       }
667     } else {
668       const MCSectionELF &Section =
669           static_cast<const MCSectionELF &>(Symbol.getSection());
670 
671       // We may end up with a situation when section symbol is technically
672       // defined, but should not be. That happens because we explicitly
673       // pre-create few .debug_* sections to have accessors.
674       // And if these sections were not really defined in the code, but were
675       // referenced, we simply error out.
676       if (!Section.isRegistered()) {
677         assert(static_cast<const MCSymbolELF &>(Symbol).getType() ==
678                ELF::STT_SECTION);
679         Ctx.reportError(SMLoc(),
680                         "Undefined section reference: " + Symbol.getName());
681         continue;
682       }
683 
684       if (Mode == NonDwoOnly && isDwoSection(Section))
685         continue;
686       MSD.SectionIndex = SectionIndexMap.lookup(&Section);
687       assert(MSD.SectionIndex && "Invalid section index!");
688       if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
689         HasLargeSectionIndex = true;
690     }
691 
692     StringRef Name = Symbol.getName();
693 
694     // Sections have their own string table
695     if (Symbol.getType() != ELF::STT_SECTION) {
696       MSD.Name = Name;
697       StrTabBuilder.add(Name);
698     }
699 
700     if (Local)
701       LocalSymbolData.push_back(MSD);
702     else
703       ExternalSymbolData.push_back(MSD);
704   }
705 
706   // This holds the .symtab_shndx section index.
707   unsigned SymtabShndxSectionIndex = 0;
708 
709   if (HasLargeSectionIndex) {
710     MCSectionELF *SymtabShndxSection =
711         Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, 4);
712     SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
713     SymtabShndxSection->setAlignment(Align(4));
714   }
715 
716   StrTabBuilder.finalize();
717 
718   // Make the first STT_FILE precede previous local symbols.
719   unsigned Index = 1;
720   auto FileNameIt = FileNames.begin();
721   if (!FileNames.empty())
722     FileNames[0].second = 0;
723 
724   for (ELFSymbolData &MSD : LocalSymbolData) {
725     // Emit STT_FILE symbols before their associated local symbols.
726     for (; FileNameIt != FileNames.end() && FileNameIt->second <= MSD.Order;
727          ++FileNameIt) {
728       Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
729                          ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
730                          ELF::SHN_ABS, true);
731       ++Index;
732     }
733 
734     unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
735                                ? 0
736                                : StrTabBuilder.getOffset(MSD.Name);
737     MSD.Symbol->setIndex(Index++);
738     writeSymbol(Writer, StringIndex, MSD, Layout);
739   }
740   for (; FileNameIt != FileNames.end(); ++FileNameIt) {
741     Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
742                        ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
743                        ELF::SHN_ABS, true);
744     ++Index;
745   }
746 
747   // Write the symbol table entries.
748   LastLocalSymbolIndex = Index;
749 
750   for (ELFSymbolData &MSD : ExternalSymbolData) {
751     unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
752     MSD.Symbol->setIndex(Index++);
753     writeSymbol(Writer, StringIndex, MSD, Layout);
754     assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
755   }
756 
757   uint64_t SecEnd = W.OS.tell();
758   SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
759 
760   ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
761   if (ShndxIndexes.empty()) {
762     assert(SymtabShndxSectionIndex == 0);
763     return;
764   }
765   assert(SymtabShndxSectionIndex != 0);
766 
767   SecStart = W.OS.tell();
768   const MCSectionELF *SymtabShndxSection =
769       SectionTable[SymtabShndxSectionIndex - 1];
770   for (uint32_t Index : ShndxIndexes)
771     write(Index);
772   SecEnd = W.OS.tell();
773   SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
774 }
775 
776 void ELFWriter::writeAddrsigSection() {
777   for (const MCSymbol *Sym : OWriter.AddrsigSyms)
778     encodeULEB128(Sym->getIndex(), W.OS);
779 }
780 
781 MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
782                                                  const MCSectionELF &Sec) {
783   if (OWriter.Relocations[&Sec].empty())
784     return nullptr;
785 
786   const StringRef SectionName = Sec.getName();
787   bool Rela = usesRela(Sec);
788   std::string RelaSectionName = Rela ? ".rela" : ".rel";
789   RelaSectionName += SectionName;
790 
791   unsigned EntrySize;
792   if (Rela)
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 = ELF::SHF_INFO_LINK;
798   if (Sec.getFlags() & ELF::SHF_GROUP)
799     Flags = ELF::SHF_GROUP;
800 
801   MCSectionELF *RelaSection = Ctx.createELFRelSection(
802       RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
803       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   const bool Rela = usesRela(Sec);
929   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
930     const ELFRelocationEntry &Entry = Relocs[e - i - 1];
931     unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
932 
933     if (is64Bit()) {
934       write(Entry.Offset);
935       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
936         write(uint32_t(Index));
937 
938         write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
939         write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
940         write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
941         write(OWriter.TargetObjectWriter->getRType(Entry.Type));
942       } else {
943         struct ELF::Elf64_Rela ERE64;
944         ERE64.setSymbolAndType(Index, Entry.Type);
945         write(ERE64.r_info);
946       }
947       if (Rela)
948         write(Entry.Addend);
949     } else {
950       write(uint32_t(Entry.Offset));
951 
952       struct ELF::Elf32_Rela ERE32;
953       ERE32.setSymbolAndType(Index, Entry.Type);
954       write(ERE32.r_info);
955 
956       if (Rela)
957         write(uint32_t(Entry.Addend));
958 
959       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
960         if (uint32_t RType =
961                 OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
962           write(uint32_t(Entry.Offset));
963 
964           ERE32.setSymbolAndType(0, RType);
965           write(ERE32.r_info);
966           write(uint32_t(0));
967         }
968         if (uint32_t RType =
969                 OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
970           write(uint32_t(Entry.Offset));
971 
972           ERE32.setSymbolAndType(0, RType);
973           write(ERE32.r_info);
974           write(uint32_t(0));
975         }
976       }
977     }
978   }
979 }
980 
981 void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
982                              uint32_t GroupSymbolIndex, uint64_t Offset,
983                              uint64_t Size, const MCSectionELF &Section) {
984   uint64_t sh_link = 0;
985   uint64_t sh_info = 0;
986 
987   switch(Section.getType()) {
988   default:
989     // Nothing to do.
990     break;
991 
992   case ELF::SHT_DYNAMIC:
993     llvm_unreachable("SHT_DYNAMIC in a relocatable object");
994 
995   case ELF::SHT_REL:
996   case ELF::SHT_RELA: {
997     sh_link = SymbolTableIndex;
998     assert(sh_link && ".symtab not found");
999     const MCSection *InfoSection = Section.getLinkedToSection();
1000     sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
1001     break;
1002   }
1003 
1004   case ELF::SHT_SYMTAB:
1005     sh_link = StringTableIndex;
1006     sh_info = LastLocalSymbolIndex;
1007     break;
1008 
1009   case ELF::SHT_SYMTAB_SHNDX:
1010   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1011   case ELF::SHT_LLVM_ADDRSIG:
1012     sh_link = SymbolTableIndex;
1013     break;
1014 
1015   case ELF::SHT_GROUP:
1016     sh_link = SymbolTableIndex;
1017     sh_info = GroupSymbolIndex;
1018     break;
1019   }
1020 
1021   if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
1022     // If the value in the associated metadata is not a definition, Sym will be
1023     // undefined. Represent this with sh_link=0.
1024     const MCSymbol *Sym = Section.getLinkedToSymbol();
1025     if (Sym && Sym->isInSection()) {
1026       const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
1027       sh_link = SectionIndexMap.lookup(Sec);
1028     }
1029   }
1030 
1031   WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
1032                    Section.getType(), Section.getFlags(), 0, Offset, Size,
1033                    sh_link, sh_info, Section.getAlignment(),
1034                    Section.getEntrySize());
1035 }
1036 
1037 void ELFWriter::writeSectionHeader(
1038     const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1039     const SectionOffsetsTy &SectionOffsets) {
1040   const unsigned NumSections = SectionTable.size();
1041 
1042   // Null section first.
1043   uint64_t FirstSectionSize =
1044       (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1045   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1046 
1047   for (const MCSectionELF *Section : SectionTable) {
1048     uint32_t GroupSymbolIndex;
1049     unsigned Type = Section->getType();
1050     if (Type != ELF::SHT_GROUP)
1051       GroupSymbolIndex = 0;
1052     else
1053       GroupSymbolIndex = Section->getGroup()->getIndex();
1054 
1055     const std::pair<uint64_t, uint64_t> &Offsets =
1056         SectionOffsets.find(Section)->second;
1057     uint64_t Size;
1058     if (Type == ELF::SHT_NOBITS)
1059       Size = Layout.getSectionAddressSize(Section);
1060     else
1061       Size = Offsets.second - Offsets.first;
1062 
1063     writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1064                  *Section);
1065   }
1066 }
1067 
1068 uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
1069   uint64_t StartOffset = W.OS.tell();
1070 
1071   MCContext &Ctx = Asm.getContext();
1072   MCSectionELF *StrtabSection =
1073       Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1074   StringTableIndex = addToSectionTable(StrtabSection);
1075 
1076   RevGroupMapTy RevGroupMap;
1077   SectionIndexMapTy SectionIndexMap;
1078 
1079   std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1080 
1081   // Write out the ELF header ...
1082   writeHeader(Asm);
1083 
1084   // ... then the sections ...
1085   SectionOffsetsTy SectionOffsets;
1086   std::vector<MCSectionELF *> Groups;
1087   std::vector<MCSectionELF *> Relocations;
1088   for (MCSection &Sec : Asm) {
1089     MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1090     if (Mode == NonDwoOnly && isDwoSection(Section))
1091       continue;
1092     if (Mode == DwoOnly && !isDwoSection(Section))
1093       continue;
1094 
1095     // Remember the offset into the file for this section.
1096     const uint64_t SecStart = align(Section.getAlignment());
1097 
1098     const MCSymbolELF *SignatureSymbol = Section.getGroup();
1099     writeSectionData(Asm, Section, Layout);
1100 
1101     uint64_t SecEnd = W.OS.tell();
1102     SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1103 
1104     MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1105 
1106     if (SignatureSymbol) {
1107       unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1108       if (!GroupIdx) {
1109         MCSectionELF *Group =
1110             Ctx.createELFGroupSection(SignatureSymbol, Section.isComdat());
1111         GroupIdx = addToSectionTable(Group);
1112         Group->setAlignment(Align(4));
1113         Groups.push_back(Group);
1114       }
1115       std::vector<const MCSectionELF *> &Members =
1116           GroupMembers[SignatureSymbol];
1117       Members.push_back(&Section);
1118       if (RelSection)
1119         Members.push_back(RelSection);
1120     }
1121 
1122     SectionIndexMap[&Section] = addToSectionTable(&Section);
1123     if (RelSection) {
1124       SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1125       Relocations.push_back(RelSection);
1126     }
1127 
1128     OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
1129   }
1130 
1131   for (MCSectionELF *Group : Groups) {
1132     // Remember the offset into the file for this section.
1133     const uint64_t SecStart = align(Group->getAlignment());
1134 
1135     const MCSymbol *SignatureSymbol = Group->getGroup();
1136     assert(SignatureSymbol);
1137     write(uint32_t(Group->isComdat() ? unsigned(ELF::GRP_COMDAT) : 0));
1138     for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1139       uint32_t SecIndex = SectionIndexMap.lookup(Member);
1140       write(SecIndex);
1141     }
1142 
1143     uint64_t SecEnd = W.OS.tell();
1144     SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1145   }
1146 
1147   if (Mode == DwoOnly) {
1148     // dwo files don't have symbol tables or relocations, but they do have
1149     // string tables.
1150     StrTabBuilder.finalize();
1151   } else {
1152     MCSectionELF *AddrsigSection;
1153     if (OWriter.EmitAddrsigSection) {
1154       AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
1155                                          ELF::SHF_EXCLUDE);
1156       addToSectionTable(AddrsigSection);
1157     }
1158 
1159     // Compute symbol table information.
1160     computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1161                        SectionOffsets);
1162 
1163     for (MCSectionELF *RelSection : Relocations) {
1164       // Remember the offset into the file for this section.
1165       const uint64_t SecStart = align(RelSection->getAlignment());
1166 
1167       writeRelocations(Asm,
1168                        cast<MCSectionELF>(*RelSection->getLinkedToSection()));
1169 
1170       uint64_t SecEnd = W.OS.tell();
1171       SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1172     }
1173 
1174     if (OWriter.EmitAddrsigSection) {
1175       uint64_t SecStart = W.OS.tell();
1176       writeAddrsigSection();
1177       uint64_t SecEnd = W.OS.tell();
1178       SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
1179     }
1180   }
1181 
1182   {
1183     uint64_t SecStart = W.OS.tell();
1184     StrTabBuilder.write(W.OS);
1185     SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
1186   }
1187 
1188   const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
1189 
1190   // ... then the section header table ...
1191   writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1192 
1193   uint16_t NumSections = support::endian::byte_swap<uint16_t>(
1194       (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
1195                                                       : SectionTable.size() + 1,
1196       W.Endian);
1197   unsigned NumSectionsOffset;
1198 
1199   auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
1200   if (is64Bit()) {
1201     uint64_t Val =
1202         support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
1203     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1204                   offsetof(ELF::Elf64_Ehdr, e_shoff));
1205     NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1206   } else {
1207     uint32_t Val =
1208         support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
1209     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1210                   offsetof(ELF::Elf32_Ehdr, e_shoff));
1211     NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1212   }
1213   Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1214                 NumSectionsOffset);
1215 
1216   return W.OS.tell() - StartOffset;
1217 }
1218 
1219 bool ELFObjectWriter::hasRelocationAddend() const {
1220   return TargetObjectWriter->hasRelocationAddend();
1221 }
1222 
1223 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1224                                                const MCAsmLayout &Layout) {
1225   // The presence of symbol versions causes undefined symbols and
1226   // versions declared with @@@ to be renamed.
1227   for (const MCAssembler::Symver &S : Asm.Symvers) {
1228     StringRef AliasName = S.Name;
1229     const auto &Symbol = cast<MCSymbolELF>(*S.Sym);
1230     size_t Pos = AliasName.find('@');
1231     assert(Pos != StringRef::npos);
1232 
1233     StringRef Prefix = AliasName.substr(0, Pos);
1234     StringRef Rest = AliasName.substr(Pos);
1235     StringRef Tail = Rest;
1236     if (Rest.startswith("@@@"))
1237       Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
1238 
1239     auto *Alias =
1240         cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
1241     Asm.registerSymbol(*Alias);
1242     const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
1243     Alias->setVariableValue(Value);
1244 
1245     // Aliases defined with .symvar copy the binding from the symbol they alias.
1246     // This is the first place we are able to copy this information.
1247     Alias->setBinding(Symbol.getBinding());
1248     Alias->setVisibility(Symbol.getVisibility());
1249     Alias->setOther(Symbol.getOther());
1250 
1251     if (!Symbol.isUndefined() && S.KeepOriginalSym)
1252       continue;
1253 
1254     if (Symbol.isUndefined() && Rest.startswith("@@") &&
1255         !Rest.startswith("@@@")) {
1256       Asm.getContext().reportError(S.Loc, "default version symbol " +
1257                                               AliasName + " must be defined");
1258       continue;
1259     }
1260 
1261     if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
1262       Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
1263                                               Symbol.getName());
1264       continue;
1265     }
1266 
1267     Renames.insert(std::make_pair(&Symbol, Alias));
1268   }
1269 
1270   for (const MCSymbol *&Sym : AddrsigSyms) {
1271     if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
1272       Sym = R;
1273     if (Sym->isInSection() && Sym->getName().startswith(".L"))
1274       Sym = Sym->getSection().getBeginSymbol();
1275     Sym->setUsedInReloc();
1276   }
1277 }
1278 
1279 // It is always valid to create a relocation with a symbol. It is preferable
1280 // to use a relocation with a section if that is possible. Using the section
1281 // allows us to omit some local symbols from the symbol table.
1282 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1283                                                const MCSymbolRefExpr *RefA,
1284                                                const MCSymbolELF *Sym,
1285                                                uint64_t C,
1286                                                unsigned Type) const {
1287   // A PCRel relocation to an absolute value has no symbol (or section). We
1288   // represent that with a relocation to a null section.
1289   if (!RefA)
1290     return false;
1291 
1292   MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
1293   switch (Kind) {
1294   default:
1295     break;
1296   // The .odp creation emits a relocation against the symbol ".TOC." which
1297   // create a R_PPC64_TOC relocation. However the relocation symbol name
1298   // in final object creation should be NULL, since the symbol does not
1299   // really exist, it is just the reference to TOC base for the current
1300   // object file. Since the symbol is undefined, returning false results
1301   // in a relocation with a null section which is the desired result.
1302   case MCSymbolRefExpr::VK_PPC_TOCBASE:
1303     return false;
1304 
1305   // These VariantKind cause the relocation to refer to something other than
1306   // the symbol itself, like a linker generated table. Since the address of
1307   // symbol is not relevant, we cannot replace the symbol with the
1308   // section and patch the difference in the addend.
1309   case MCSymbolRefExpr::VK_GOT:
1310   case MCSymbolRefExpr::VK_PLT:
1311   case MCSymbolRefExpr::VK_GOTPCREL:
1312   case MCSymbolRefExpr::VK_GOTPCREL_NORELAX:
1313   case MCSymbolRefExpr::VK_PPC_GOT_LO:
1314   case MCSymbolRefExpr::VK_PPC_GOT_HI:
1315   case MCSymbolRefExpr::VK_PPC_GOT_HA:
1316     return true;
1317   }
1318 
1319   // An undefined symbol is not in any section, so the relocation has to point
1320   // to the symbol itself.
1321   assert(Sym && "Expected a symbol");
1322   if (Sym->isUndefined())
1323     return true;
1324 
1325   unsigned Binding = Sym->getBinding();
1326   switch(Binding) {
1327   default:
1328     llvm_unreachable("Invalid Binding");
1329   case ELF::STB_LOCAL:
1330     break;
1331   case ELF::STB_WEAK:
1332     // If the symbol is weak, it might be overridden by a symbol in another
1333     // file. The relocation has to point to the symbol so that the linker
1334     // can update it.
1335     return true;
1336   case ELF::STB_GLOBAL:
1337     // Global ELF symbols can be preempted by the dynamic linker. The relocation
1338     // has to point to the symbol for a reason analogous to the STB_WEAK case.
1339     return true;
1340   }
1341 
1342   // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1343   // reloc that the dynamic loader will use to resolve the address at startup
1344   // time.
1345   if (Sym->getType() == ELF::STT_GNU_IFUNC)
1346     return true;
1347 
1348   // If a relocation points to a mergeable section, we have to be careful.
1349   // If the offset is zero, a relocation with the section will encode the
1350   // same information. With a non-zero offset, the situation is different.
1351   // For example, a relocation can point 42 bytes past the end of a string.
1352   // If we change such a relocation to use the section, the linker would think
1353   // that it pointed to another string and subtracting 42 at runtime will
1354   // produce the wrong value.
1355   if (Sym->isInSection()) {
1356     auto &Sec = cast<MCSectionELF>(Sym->getSection());
1357     unsigned Flags = Sec.getFlags();
1358     if (Flags & ELF::SHF_MERGE) {
1359       if (C != 0)
1360         return true;
1361 
1362       // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1363       // (http://sourceware.org/PR16794).
1364       if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1365           Type == ELF::R_386_GOTOFF)
1366         return true;
1367 
1368       // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1369       // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1370       // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1371       // range of a MergeInputSection. We could introduce a new RelExpr member
1372       // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1373       // but the complexity is unnecessary given that GNU as keeps the original
1374       // symbol for this case as well.
1375       if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1376           !hasRelocationAddend())
1377         return true;
1378     }
1379 
1380     // Most TLS relocations use a got, so they need the symbol. Even those that
1381     // are just an offset (@tpoff), require a symbol in gold versions before
1382     // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1383     // http://sourceware.org/PR16773.
1384     if (Flags & ELF::SHF_TLS)
1385       return true;
1386   }
1387 
1388   // If the symbol is a thumb function the final relocation must set the lowest
1389   // bit. With a symbol that is done by just having the symbol have that bit
1390   // set, so we would lose the bit if we relocated with the section.
1391   // FIXME: We could use the section but add the bit to the relocation value.
1392   if (Asm.isThumbFunc(Sym))
1393     return true;
1394 
1395   if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
1396     return true;
1397   return false;
1398 }
1399 
1400 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1401                                        const MCAsmLayout &Layout,
1402                                        const MCFragment *Fragment,
1403                                        const MCFixup &Fixup, MCValue Target,
1404                                        uint64_t &FixedValue) {
1405   MCAsmBackend &Backend = Asm.getBackend();
1406   bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1407                  MCFixupKindInfo::FKF_IsPCRel;
1408   const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1409   uint64_t C = Target.getConstant();
1410   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1411   MCContext &Ctx = Asm.getContext();
1412 
1413   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1414     const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
1415     if (SymB.isUndefined()) {
1416       Ctx.reportError(Fixup.getLoc(),
1417                       Twine("symbol '") + SymB.getName() +
1418                           "' can not be undefined in a subtraction expression");
1419       return;
1420     }
1421 
1422     assert(!SymB.isAbsolute() && "Should have been folded");
1423     const MCSection &SecB = SymB.getSection();
1424     if (&SecB != &FixupSection) {
1425       Ctx.reportError(Fixup.getLoc(),
1426                       "Cannot represent a difference across sections");
1427       return;
1428     }
1429 
1430     assert(!IsPCRel && "should have been folded");
1431     IsPCRel = true;
1432     C += FixupOffset - Layout.getSymbolOffset(SymB);
1433   }
1434 
1435   // We either rejected the fixup or folded B into C at this point.
1436   const MCSymbolRefExpr *RefA = Target.getSymA();
1437   const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
1438 
1439   bool ViaWeakRef = false;
1440   if (SymA && SymA->isVariable()) {
1441     const MCExpr *Expr = SymA->getVariableValue();
1442     if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1443       if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1444         SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1445         ViaWeakRef = true;
1446       }
1447     }
1448   }
1449 
1450   const MCSectionELF *SecA = (SymA && SymA->isInSection())
1451                                  ? cast<MCSectionELF>(&SymA->getSection())
1452                                  : nullptr;
1453   if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1454     return;
1455 
1456   unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1457   const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1458   // Emiting relocation with sybmol for CG Profile to  help with --cg-profile.
1459   bool RelocateWithSymbol =
1460       shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type) ||
1461       (Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
1462   uint64_t Addend = 0;
1463 
1464   FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
1465                    ? C + Layout.getSymbolOffset(*SymA)
1466                    : C;
1467   if (hasRelocationAddend()) {
1468     Addend = FixedValue;
1469     FixedValue = 0;
1470   }
1471 
1472   if (!RelocateWithSymbol) {
1473     const auto *SectionSymbol =
1474         SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
1475     if (SectionSymbol)
1476       SectionSymbol->setUsedInReloc();
1477     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
1478     Relocations[&FixupSection].push_back(Rec);
1479     return;
1480   }
1481 
1482   const MCSymbolELF *RenamedSymA = SymA;
1483   if (SymA) {
1484     if (const MCSymbolELF *R = Renames.lookup(SymA))
1485       RenamedSymA = R;
1486 
1487     if (ViaWeakRef)
1488       RenamedSymA->setIsWeakrefUsedInReloc();
1489     else
1490       RenamedSymA->setUsedInReloc();
1491   }
1492   ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
1493   Relocations[&FixupSection].push_back(Rec);
1494 }
1495 
1496 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1497     const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1498     bool InSet, bool IsPCRel) const {
1499   const auto &SymA = cast<MCSymbolELF>(SA);
1500   if (IsPCRel) {
1501     assert(!InSet);
1502     if (SymA.getBinding() != ELF::STB_LOCAL ||
1503         SymA.getType() == ELF::STT_GNU_IFUNC)
1504       return false;
1505   }
1506   return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1507                                                                 InSet, IsPCRel);
1508 }
1509 
1510 std::unique_ptr<MCObjectWriter>
1511 llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1512                             raw_pwrite_stream &OS, bool IsLittleEndian) {
1513   return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
1514                                                   IsLittleEndian);
1515 }
1516 
1517 std::unique_ptr<MCObjectWriter>
1518 llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1519                                raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
1520                                bool IsLittleEndian) {
1521   return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
1522                                                IsLittleEndian);
1523 }
1524