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