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