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