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   bool CompressionEnabled =
852       MAI->compressDebugSections() != DebugCompressionType::None;
853   if (!CompressionEnabled || !SectionName.startswith(".debug_")) {
854     Asm.writeSectionData(W.OS, &Section, Layout);
855     return;
856   }
857 
858   assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
859           MAI->compressDebugSections() == DebugCompressionType::GNU) &&
860          "expected zlib or zlib-gnu style compression");
861 
862   SmallVector<char, 128> UncompressedData;
863   raw_svector_ostream VecOS(UncompressedData);
864   Asm.writeSectionData(VecOS, &Section, Layout);
865 
866   SmallVector<char, 128> CompressedContents;
867   if (Error E = zlib::compress(
868           StringRef(UncompressedData.data(), UncompressedData.size()),
869           CompressedContents)) {
870     consumeError(std::move(E));
871     W.OS << UncompressedData;
872     return;
873   }
874 
875   bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
876   if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
877                              ZlibStyle, Sec.getAlignment())) {
878     W.OS << UncompressedData;
879     return;
880   }
881 
882   if (ZlibStyle) {
883     // Set the compressed flag. That is zlib style.
884     Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
885     // Alignment field should reflect the requirements of
886     // the compressed section header.
887     Section.setAlignment(is64Bit() ? Align(8) : Align(4));
888   } else {
889     // Add "z" prefix to section name. This is zlib-gnu style.
890     MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
891   }
892   W.OS << CompressedContents;
893 }
894 
895 void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
896                                  uint64_t Address, uint64_t Offset,
897                                  uint64_t Size, uint32_t Link, uint32_t Info,
898                                  uint64_t Alignment, uint64_t EntrySize) {
899   W.write<uint32_t>(Name);        // sh_name: index into string table
900   W.write<uint32_t>(Type);        // sh_type
901   WriteWord(Flags);     // sh_flags
902   WriteWord(Address);   // sh_addr
903   WriteWord(Offset);    // sh_offset
904   WriteWord(Size);      // sh_size
905   W.write<uint32_t>(Link);        // sh_link
906   W.write<uint32_t>(Info);        // sh_info
907   WriteWord(Alignment); // sh_addralign
908   WriteWord(EntrySize); // sh_entsize
909 }
910 
911 void ELFWriter::writeRelocations(const MCAssembler &Asm,
912                                        const MCSectionELF &Sec) {
913   std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
914 
915   // We record relocations by pushing to the end of a vector. Reverse the vector
916   // to get the relocations in the order they were created.
917   // In most cases that is not important, but it can be for special sections
918   // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
919   std::reverse(Relocs.begin(), Relocs.end());
920 
921   // Sort the relocation entries. MIPS needs this.
922   OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
923 
924   const bool Rela = usesRela(Sec);
925   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
926     const ELFRelocationEntry &Entry = Relocs[e - i - 1];
927     unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
928 
929     if (is64Bit()) {
930       write(Entry.Offset);
931       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
932         write(uint32_t(Index));
933 
934         write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
935         write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
936         write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
937         write(OWriter.TargetObjectWriter->getRType(Entry.Type));
938       } else {
939         struct ELF::Elf64_Rela ERE64;
940         ERE64.setSymbolAndType(Index, Entry.Type);
941         write(ERE64.r_info);
942       }
943       if (Rela)
944         write(Entry.Addend);
945     } else {
946       write(uint32_t(Entry.Offset));
947 
948       struct ELF::Elf32_Rela ERE32;
949       ERE32.setSymbolAndType(Index, Entry.Type);
950       write(ERE32.r_info);
951 
952       if (Rela)
953         write(uint32_t(Entry.Addend));
954 
955       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
956         if (uint32_t RType =
957                 OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
958           write(uint32_t(Entry.Offset));
959 
960           ERE32.setSymbolAndType(0, RType);
961           write(ERE32.r_info);
962           write(uint32_t(0));
963         }
964         if (uint32_t RType =
965                 OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
966           write(uint32_t(Entry.Offset));
967 
968           ERE32.setSymbolAndType(0, RType);
969           write(ERE32.r_info);
970           write(uint32_t(0));
971         }
972       }
973     }
974   }
975 }
976 
977 void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
978                              uint32_t GroupSymbolIndex, uint64_t Offset,
979                              uint64_t Size, const MCSectionELF &Section) {
980   uint64_t sh_link = 0;
981   uint64_t sh_info = 0;
982 
983   switch(Section.getType()) {
984   default:
985     // Nothing to do.
986     break;
987 
988   case ELF::SHT_DYNAMIC:
989     llvm_unreachable("SHT_DYNAMIC in a relocatable object");
990 
991   case ELF::SHT_REL:
992   case ELF::SHT_RELA: {
993     sh_link = SymbolTableIndex;
994     assert(sh_link && ".symtab not found");
995     const MCSection *InfoSection = Section.getLinkedToSection();
996     sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
997     break;
998   }
999 
1000   case ELF::SHT_SYMTAB:
1001     sh_link = StringTableIndex;
1002     sh_info = LastLocalSymbolIndex;
1003     break;
1004 
1005   case ELF::SHT_SYMTAB_SHNDX:
1006   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1007   case ELF::SHT_LLVM_ADDRSIG:
1008     sh_link = SymbolTableIndex;
1009     break;
1010 
1011   case ELF::SHT_GROUP:
1012     sh_link = SymbolTableIndex;
1013     sh_info = GroupSymbolIndex;
1014     break;
1015   }
1016 
1017   if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
1018     // If the value in the associated metadata is not a definition, Sym will be
1019     // undefined. Represent this with sh_link=0.
1020     const MCSymbol *Sym = Section.getLinkedToSymbol();
1021     if (Sym && Sym->isInSection()) {
1022       const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
1023       sh_link = SectionIndexMap.lookup(Sec);
1024     }
1025   }
1026 
1027   WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
1028                    Section.getType(), Section.getFlags(), 0, Offset, Size,
1029                    sh_link, sh_info, Section.getAlignment(),
1030                    Section.getEntrySize());
1031 }
1032 
1033 void ELFWriter::writeSectionHeader(
1034     const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1035     const SectionOffsetsTy &SectionOffsets) {
1036   const unsigned NumSections = SectionTable.size();
1037 
1038   // Null section first.
1039   uint64_t FirstSectionSize =
1040       (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1041   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1042 
1043   for (const MCSectionELF *Section : SectionTable) {
1044     uint32_t GroupSymbolIndex;
1045     unsigned Type = Section->getType();
1046     if (Type != ELF::SHT_GROUP)
1047       GroupSymbolIndex = 0;
1048     else
1049       GroupSymbolIndex = Section->getGroup()->getIndex();
1050 
1051     const std::pair<uint64_t, uint64_t> &Offsets =
1052         SectionOffsets.find(Section)->second;
1053     uint64_t Size;
1054     if (Type == ELF::SHT_NOBITS)
1055       Size = Layout.getSectionAddressSize(Section);
1056     else
1057       Size = Offsets.second - Offsets.first;
1058 
1059     writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1060                  *Section);
1061   }
1062 }
1063 
1064 uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
1065   uint64_t StartOffset = W.OS.tell();
1066 
1067   MCContext &Ctx = Asm.getContext();
1068   MCSectionELF *StrtabSection =
1069       Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1070   StringTableIndex = addToSectionTable(StrtabSection);
1071 
1072   RevGroupMapTy RevGroupMap;
1073   SectionIndexMapTy SectionIndexMap;
1074 
1075   std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1076 
1077   // Write out the ELF header ...
1078   writeHeader(Asm);
1079 
1080   // ... then the sections ...
1081   SectionOffsetsTy SectionOffsets;
1082   std::vector<MCSectionELF *> Groups;
1083   std::vector<MCSectionELF *> Relocations;
1084   for (MCSection &Sec : Asm) {
1085     MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1086     if (Mode == NonDwoOnly && isDwoSection(Section))
1087       continue;
1088     if (Mode == DwoOnly && !isDwoSection(Section))
1089       continue;
1090 
1091     // Remember the offset into the file for this section.
1092     const uint64_t SecStart = align(Section.getAlignment());
1093 
1094     const MCSymbolELF *SignatureSymbol = Section.getGroup();
1095     writeSectionData(Asm, Section, Layout);
1096 
1097     uint64_t SecEnd = W.OS.tell();
1098     SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1099 
1100     MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1101 
1102     if (SignatureSymbol) {
1103       unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1104       if (!GroupIdx) {
1105         MCSectionELF *Group =
1106             Ctx.createELFGroupSection(SignatureSymbol, Section.isComdat());
1107         GroupIdx = addToSectionTable(Group);
1108         Group->setAlignment(Align(4));
1109         Groups.push_back(Group);
1110       }
1111       std::vector<const MCSectionELF *> &Members =
1112           GroupMembers[SignatureSymbol];
1113       Members.push_back(&Section);
1114       if (RelSection)
1115         Members.push_back(RelSection);
1116     }
1117 
1118     SectionIndexMap[&Section] = addToSectionTable(&Section);
1119     if (RelSection) {
1120       SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1121       Relocations.push_back(RelSection);
1122     }
1123 
1124     OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
1125   }
1126 
1127   for (MCSectionELF *Group : Groups) {
1128     // Remember the offset into the file for this section.
1129     const uint64_t SecStart = align(Group->getAlignment());
1130 
1131     const MCSymbol *SignatureSymbol = Group->getGroup();
1132     assert(SignatureSymbol);
1133     write(uint32_t(Group->isComdat() ? unsigned(ELF::GRP_COMDAT) : 0));
1134     for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1135       uint32_t SecIndex = SectionIndexMap.lookup(Member);
1136       write(SecIndex);
1137     }
1138 
1139     uint64_t SecEnd = W.OS.tell();
1140     SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1141   }
1142 
1143   if (Mode == DwoOnly) {
1144     // dwo files don't have symbol tables or relocations, but they do have
1145     // string tables.
1146     StrTabBuilder.finalize();
1147   } else {
1148     MCSectionELF *AddrsigSection;
1149     if (OWriter.EmitAddrsigSection) {
1150       AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
1151                                          ELF::SHF_EXCLUDE);
1152       addToSectionTable(AddrsigSection);
1153     }
1154 
1155     // Compute symbol table information.
1156     computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1157                        SectionOffsets);
1158 
1159     for (MCSectionELF *RelSection : Relocations) {
1160       // Remember the offset into the file for this section.
1161       const uint64_t SecStart = align(RelSection->getAlignment());
1162 
1163       writeRelocations(Asm,
1164                        cast<MCSectionELF>(*RelSection->getLinkedToSection()));
1165 
1166       uint64_t SecEnd = W.OS.tell();
1167       SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1168     }
1169 
1170     if (OWriter.EmitAddrsigSection) {
1171       uint64_t SecStart = W.OS.tell();
1172       writeAddrsigSection();
1173       uint64_t SecEnd = W.OS.tell();
1174       SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
1175     }
1176   }
1177 
1178   {
1179     uint64_t SecStart = W.OS.tell();
1180     StrTabBuilder.write(W.OS);
1181     SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
1182   }
1183 
1184   const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
1185 
1186   // ... then the section header table ...
1187   writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1188 
1189   uint16_t NumSections = support::endian::byte_swap<uint16_t>(
1190       (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
1191                                                       : SectionTable.size() + 1,
1192       W.Endian);
1193   unsigned NumSectionsOffset;
1194 
1195   auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
1196   if (is64Bit()) {
1197     uint64_t Val =
1198         support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
1199     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1200                   offsetof(ELF::Elf64_Ehdr, e_shoff));
1201     NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1202   } else {
1203     uint32_t Val =
1204         support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
1205     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1206                   offsetof(ELF::Elf32_Ehdr, e_shoff));
1207     NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1208   }
1209   Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1210                 NumSectionsOffset);
1211 
1212   return W.OS.tell() - StartOffset;
1213 }
1214 
1215 bool ELFObjectWriter::hasRelocationAddend() const {
1216   return TargetObjectWriter->hasRelocationAddend();
1217 }
1218 
1219 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1220                                                const MCAsmLayout &Layout) {
1221   // The presence of symbol versions causes undefined symbols and
1222   // versions declared with @@@ to be renamed.
1223   for (const MCAssembler::Symver &S : Asm.Symvers) {
1224     StringRef AliasName = S.Name;
1225     const auto &Symbol = cast<MCSymbolELF>(*S.Sym);
1226     size_t Pos = AliasName.find('@');
1227     assert(Pos != StringRef::npos);
1228 
1229     StringRef Prefix = AliasName.substr(0, Pos);
1230     StringRef Rest = AliasName.substr(Pos);
1231     StringRef Tail = Rest;
1232     if (Rest.startswith("@@@"))
1233       Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
1234 
1235     auto *Alias =
1236         cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
1237     Asm.registerSymbol(*Alias);
1238     const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
1239     Alias->setVariableValue(Value);
1240 
1241     // Aliases defined with .symvar copy the binding from the symbol they alias.
1242     // This is the first place we are able to copy this information.
1243     Alias->setBinding(Symbol.getBinding());
1244     Alias->setVisibility(Symbol.getVisibility());
1245     Alias->setOther(Symbol.getOther());
1246 
1247     if (!Symbol.isUndefined() && S.KeepOriginalSym)
1248       continue;
1249 
1250     if (Symbol.isUndefined() && Rest.startswith("@@") &&
1251         !Rest.startswith("@@@")) {
1252       Asm.getContext().reportError(S.Loc, "default version symbol " +
1253                                               AliasName + " must be defined");
1254       continue;
1255     }
1256 
1257     if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
1258       Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
1259                                               Symbol.getName());
1260       continue;
1261     }
1262 
1263     Renames.insert(std::make_pair(&Symbol, Alias));
1264   }
1265 
1266   for (const MCSymbol *&Sym : AddrsigSyms) {
1267     if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
1268       Sym = R;
1269     if (Sym->isInSection() && Sym->getName().startswith(".L"))
1270       Sym = Sym->getSection().getBeginSymbol();
1271     Sym->setUsedInReloc();
1272   }
1273 }
1274 
1275 // It is always valid to create a relocation with a symbol. It is preferable
1276 // to use a relocation with a section if that is possible. Using the section
1277 // allows us to omit some local symbols from the symbol table.
1278 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1279                                                const MCSymbolRefExpr *RefA,
1280                                                const MCSymbolELF *Sym,
1281                                                uint64_t C,
1282                                                unsigned Type) const {
1283   // A PCRel relocation to an absolute value has no symbol (or section). We
1284   // represent that with a relocation to a null section.
1285   if (!RefA)
1286     return false;
1287 
1288   MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
1289   switch (Kind) {
1290   default:
1291     break;
1292   // The .odp creation emits a relocation against the symbol ".TOC." which
1293   // create a R_PPC64_TOC relocation. However the relocation symbol name
1294   // in final object creation should be NULL, since the symbol does not
1295   // really exist, it is just the reference to TOC base for the current
1296   // object file. Since the symbol is undefined, returning false results
1297   // in a relocation with a null section which is the desired result.
1298   case MCSymbolRefExpr::VK_PPC_TOCBASE:
1299     return false;
1300 
1301   // These VariantKind cause the relocation to refer to something other than
1302   // the symbol itself, like a linker generated table. Since the address of
1303   // symbol is not relevant, we cannot replace the symbol with the
1304   // section and patch the difference in the addend.
1305   case MCSymbolRefExpr::VK_GOT:
1306   case MCSymbolRefExpr::VK_PLT:
1307   case MCSymbolRefExpr::VK_GOTPCREL:
1308   case MCSymbolRefExpr::VK_GOTPCREL_NORELAX:
1309   case MCSymbolRefExpr::VK_PPC_GOT_LO:
1310   case MCSymbolRefExpr::VK_PPC_GOT_HI:
1311   case MCSymbolRefExpr::VK_PPC_GOT_HA:
1312     return true;
1313   }
1314 
1315   // An undefined symbol is not in any section, so the relocation has to point
1316   // to the symbol itself.
1317   assert(Sym && "Expected a symbol");
1318   if (Sym->isUndefined())
1319     return true;
1320 
1321   unsigned Binding = Sym->getBinding();
1322   switch(Binding) {
1323   default:
1324     llvm_unreachable("Invalid Binding");
1325   case ELF::STB_LOCAL:
1326     break;
1327   case ELF::STB_WEAK:
1328     // If the symbol is weak, it might be overridden by a symbol in another
1329     // file. The relocation has to point to the symbol so that the linker
1330     // can update it.
1331     return true;
1332   case ELF::STB_GLOBAL:
1333     // Global ELF symbols can be preempted by the dynamic linker. The relocation
1334     // has to point to the symbol for a reason analogous to the STB_WEAK case.
1335     return true;
1336   }
1337 
1338   // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1339   // reloc that the dynamic loader will use to resolve the address at startup
1340   // time.
1341   if (Sym->getType() == ELF::STT_GNU_IFUNC)
1342     return true;
1343 
1344   // If a relocation points to a mergeable section, we have to be careful.
1345   // If the offset is zero, a relocation with the section will encode the
1346   // same information. With a non-zero offset, the situation is different.
1347   // For example, a relocation can point 42 bytes past the end of a string.
1348   // If we change such a relocation to use the section, the linker would think
1349   // that it pointed to another string and subtracting 42 at runtime will
1350   // produce the wrong value.
1351   if (Sym->isInSection()) {
1352     auto &Sec = cast<MCSectionELF>(Sym->getSection());
1353     unsigned Flags = Sec.getFlags();
1354     if (Flags & ELF::SHF_MERGE) {
1355       if (C != 0)
1356         return true;
1357 
1358       // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1359       // (http://sourceware.org/PR16794).
1360       if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1361           Type == ELF::R_386_GOTOFF)
1362         return true;
1363 
1364       // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1365       // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1366       // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1367       // range of a MergeInputSection. We could introduce a new RelExpr member
1368       // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1369       // but the complexity is unnecessary given that GNU as keeps the original
1370       // symbol for this case as well.
1371       if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1372           !hasRelocationAddend())
1373         return true;
1374     }
1375 
1376     // Most TLS relocations use a got, so they need the symbol. Even those that
1377     // are just an offset (@tpoff), require a symbol in gold versions before
1378     // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1379     // http://sourceware.org/PR16773.
1380     if (Flags & ELF::SHF_TLS)
1381       return true;
1382   }
1383 
1384   // If the symbol is a thumb function the final relocation must set the lowest
1385   // bit. With a symbol that is done by just having the symbol have that bit
1386   // set, so we would lose the bit if we relocated with the section.
1387   // FIXME: We could use the section but add the bit to the relocation value.
1388   if (Asm.isThumbFunc(Sym))
1389     return true;
1390 
1391   if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
1392     return true;
1393   return false;
1394 }
1395 
1396 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1397                                        const MCAsmLayout &Layout,
1398                                        const MCFragment *Fragment,
1399                                        const MCFixup &Fixup, MCValue Target,
1400                                        uint64_t &FixedValue) {
1401   MCAsmBackend &Backend = Asm.getBackend();
1402   bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1403                  MCFixupKindInfo::FKF_IsPCRel;
1404   const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1405   uint64_t C = Target.getConstant();
1406   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1407   MCContext &Ctx = Asm.getContext();
1408 
1409   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1410     const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
1411     if (SymB.isUndefined()) {
1412       Ctx.reportError(Fixup.getLoc(),
1413                       Twine("symbol '") + SymB.getName() +
1414                           "' can not be undefined in a subtraction expression");
1415       return;
1416     }
1417 
1418     assert(!SymB.isAbsolute() && "Should have been folded");
1419     const MCSection &SecB = SymB.getSection();
1420     if (&SecB != &FixupSection) {
1421       Ctx.reportError(Fixup.getLoc(),
1422                       "Cannot represent a difference across sections");
1423       return;
1424     }
1425 
1426     assert(!IsPCRel && "should have been folded");
1427     IsPCRel = true;
1428     C += FixupOffset - Layout.getSymbolOffset(SymB);
1429   }
1430 
1431   // We either rejected the fixup or folded B into C at this point.
1432   const MCSymbolRefExpr *RefA = Target.getSymA();
1433   const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
1434 
1435   bool ViaWeakRef = false;
1436   if (SymA && SymA->isVariable()) {
1437     const MCExpr *Expr = SymA->getVariableValue();
1438     if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1439       if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1440         SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1441         ViaWeakRef = true;
1442       }
1443     }
1444   }
1445 
1446   const MCSectionELF *SecA = (SymA && SymA->isInSection())
1447                                  ? cast<MCSectionELF>(&SymA->getSection())
1448                                  : nullptr;
1449   if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1450     return;
1451 
1452   unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1453   const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1454   // Emiting relocation with sybmol for CG Profile to  help with --cg-profile.
1455   bool RelocateWithSymbol =
1456       shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type) ||
1457       (Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
1458   uint64_t Addend = 0;
1459 
1460   FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
1461                    ? C + Layout.getSymbolOffset(*SymA)
1462                    : C;
1463   if (hasRelocationAddend()) {
1464     Addend = FixedValue;
1465     FixedValue = 0;
1466   }
1467 
1468   if (!RelocateWithSymbol) {
1469     const auto *SectionSymbol =
1470         SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
1471     if (SectionSymbol)
1472       SectionSymbol->setUsedInReloc();
1473     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
1474     Relocations[&FixupSection].push_back(Rec);
1475     return;
1476   }
1477 
1478   const MCSymbolELF *RenamedSymA = SymA;
1479   if (SymA) {
1480     if (const MCSymbolELF *R = Renames.lookup(SymA))
1481       RenamedSymA = R;
1482 
1483     if (ViaWeakRef)
1484       RenamedSymA->setIsWeakrefUsedInReloc();
1485     else
1486       RenamedSymA->setUsedInReloc();
1487   }
1488   ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
1489   Relocations[&FixupSection].push_back(Rec);
1490 }
1491 
1492 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1493     const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1494     bool InSet, bool IsPCRel) const {
1495   const auto &SymA = cast<MCSymbolELF>(SA);
1496   if (IsPCRel) {
1497     assert(!InSet);
1498     if (SymA.getBinding() != ELF::STB_LOCAL ||
1499         SymA.getType() == ELF::STT_GNU_IFUNC)
1500       return false;
1501   }
1502   return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1503                                                                 InSet, IsPCRel);
1504 }
1505 
1506 std::unique_ptr<MCObjectWriter>
1507 llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1508                             raw_pwrite_stream &OS, bool IsLittleEndian) {
1509   return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
1510                                                   IsLittleEndian);
1511 }
1512 
1513 std::unique_ptr<MCObjectWriter>
1514 llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1515                                raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
1516                                bool IsLittleEndian) {
1517   return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
1518                                                IsLittleEndian);
1519 }
1520