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