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