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