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