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