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