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