Use a range loop. NFC.
[oota-llvm.git] / 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/MCELF.h"
25 #include "llvm/MC/MCELFSymbolFlags.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCFixupKindInfo.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCValue.h"
31 #include "llvm/MC/StringTableBuilder.h"
32 #include "llvm/Support/Compression.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ELF.h"
35 #include "llvm/Support/Endian.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include <vector>
38 using namespace llvm;
39
40 #undef  DEBUG_TYPE
41 #define DEBUG_TYPE "reloc-info"
42
43 namespace {
44 class FragmentWriter {
45   bool IsLittleEndian;
46
47 public:
48   FragmentWriter(bool IsLittleEndian);
49   template <typename T> void write(MCDataFragment &F, T Val);
50 };
51
52 typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy;
53
54 class SymbolTableWriter {
55   MCAssembler &Asm;
56   FragmentWriter &FWriter;
57   bool Is64Bit;
58   std::vector<const MCSectionELF *> &Sections;
59
60   // The symbol .symtab fragment we are writting to.
61   MCDataFragment *SymtabF;
62
63   // .symtab_shndx fragment we are writting to.
64   MCDataFragment *ShndxF;
65
66   // The numbel of symbols written so far.
67   unsigned NumWritten;
68
69   void createSymtabShndx();
70
71   template <typename T> void write(MCDataFragment &F, T Value);
72
73 public:
74   SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter, bool Is64Bit,
75                     std::vector<const MCSectionELF *> &Sections,
76                     MCDataFragment *SymtabF);
77
78   void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
79                    uint8_t other, uint32_t shndx, bool Reserved);
80 };
81
82 class ELFObjectWriter : public MCObjectWriter {
83   FragmentWriter FWriter;
84
85   protected:
86
87     static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
88     static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
89     static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
90     static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolData &Data,
91                            bool Used, bool Renamed);
92     static bool isLocal(const MCSymbolData &Data, bool isUsedInReloc);
93     static bool IsELFMetaDataSection(const MCSectionData &SD);
94
95     void writeDataSectionData(MCAssembler &Asm, const MCAsmLayout &Layout,
96                               const MCSectionData &SD);
97
98     /// Helper struct for containing some precomputed information on symbols.
99     struct ELFSymbolData {
100       MCSymbolData *SymbolData;
101       uint64_t StringIndex;
102       uint32_t SectionIndex;
103       StringRef Name;
104
105       // Support lexicographic sorting.
106       bool operator<(const ELFSymbolData &RHS) const {
107         unsigned LHSType = MCELF::GetType(*SymbolData);
108         unsigned RHSType = MCELF::GetType(*RHS.SymbolData);
109         if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
110           return false;
111         if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
112           return true;
113         if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
114           return SectionIndex < RHS.SectionIndex;
115         return Name < RHS.Name;
116       }
117     };
118
119     /// The target specific ELF writer instance.
120     std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
121
122     SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
123     SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
124     DenseMap<const MCSymbol *, const MCSymbol *> Renames;
125
126     llvm::DenseMap<const MCSectionData *, std::vector<ELFRelocationEntry>>
127     Relocations;
128     StringTableBuilder ShStrTabBuilder;
129
130     /// @}
131     /// @name Symbol Table Data
132     /// @{
133
134     StringTableBuilder StrTabBuilder;
135     std::vector<uint64_t> FileSymbolData;
136     std::vector<ELFSymbolData> LocalSymbolData;
137     std::vector<ELFSymbolData> ExternalSymbolData;
138     std::vector<ELFSymbolData> UndefinedSymbolData;
139
140     /// @}
141
142     bool NeedsGOT;
143
144     // This holds the symbol table index of the last local symbol.
145     unsigned LastLocalSymbolIndex;
146     // This holds the .strtab section index.
147     unsigned StringTableIndex;
148     // This holds the .symtab section index.
149     unsigned SymbolTableIndex;
150
151     unsigned ShstrtabIndex;
152
153
154     // TargetObjectWriter wrappers.
155     bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
156     bool hasRelocationAddend() const {
157       return TargetObjectWriter->hasRelocationAddend();
158     }
159     unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
160                           bool IsPCRel) const {
161       return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel);
162     }
163
164   public:
165     ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
166                     bool IsLittleEndian)
167         : MCObjectWriter(OS, IsLittleEndian), FWriter(IsLittleEndian),
168           TargetObjectWriter(MOTW), NeedsGOT(false) {}
169
170     void reset() override {
171       UsedInReloc.clear();
172       WeakrefUsedInReloc.clear();
173       Renames.clear();
174       Relocations.clear();
175       ShStrTabBuilder.clear();
176       StrTabBuilder.clear();
177       FileSymbolData.clear();
178       LocalSymbolData.clear();
179       ExternalSymbolData.clear();
180       UndefinedSymbolData.clear();
181       MCObjectWriter::reset();
182     }
183
184     ~ELFObjectWriter() override;
185
186     void WriteWord(uint64_t W) {
187       if (is64Bit())
188         Write64(W);
189       else
190         Write32(W);
191     }
192
193     template <typename T> void write(MCDataFragment &F, T Value) {
194       FWriter.write(F, Value);
195     }
196
197     void WriteHeader(const MCAssembler &Asm,
198                      unsigned NumberOfSections);
199
200     void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
201                      const MCAsmLayout &Layout);
202
203     void WriteSymbolTable(MCDataFragment *SymtabF, MCAssembler &Asm,
204                           const MCAsmLayout &Layout,
205                           std::vector<const MCSectionELF *> &Sections);
206
207     bool shouldRelocateWithSymbol(const MCAssembler &Asm,
208                                   const MCSymbolRefExpr *RefA,
209                                   const MCSymbolData *SD, uint64_t C,
210                                   unsigned Type) const;
211
212     void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
213                           const MCFragment *Fragment, const MCFixup &Fixup,
214                           MCValue Target, bool &IsPCRel,
215                           uint64_t &FixedValue) override;
216
217     uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
218                                          const MCSymbol *S);
219
220     // Map from a signature symbol to the group section
221     typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
222     // Start and end offset of each section
223     typedef std::vector<std::pair<uint64_t, uint64_t>> SectionOffsetsTy;
224
225     /// Compute the symbol table data
226     ///
227     /// \param Asm - The assembler.
228     /// \param SectionIndexMap - Maps a section to its index.
229     /// \param RevGroupMap - Maps a signature symbol to the group section.
230     void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
231                             const SectionIndexMapTy &SectionIndexMap,
232                             const RevGroupMapTy &RevGroupMap);
233
234     void maybeAddToGroup(MCAssembler &Asm, const RevGroupMapTy &RevGroupMap,
235                          const MCSectionELF &Section, unsigned Index);
236
237     void computeIndexMap(MCAssembler &Asm,
238                          std::vector<const MCSectionELF *> &Sections,
239                          SectionIndexMapTy &SectionIndexMap,
240                          const RevGroupMapTy &RevGroupMap);
241
242     MCSectionData *createRelocationSection(MCAssembler &Asm,
243                                            const MCSectionData &SD);
244
245     void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
246
247     void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout);
248
249     void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
250                                 std::vector<const MCSectionELF *> &Sections);
251
252     // Create the sections that show up in the symbol table. Currently
253     // those are the .note.GNU-stack section and the group sections.
254     void createIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
255                                RevGroupMapTy &RevGroupMap,
256                                std::vector<const MCSectionELF *> &Sections,
257                                SectionIndexMapTy &SectionIndexMap);
258
259     void ExecutePostLayoutBinding(MCAssembler &Asm,
260                                   const MCAsmLayout &Layout) override;
261
262     void writeSectionHeader(ArrayRef<const MCSectionELF *> Sections,
263                             MCAssembler &Asm, const MCAsmLayout &Layout,
264                             const SectionIndexMapTy &SectionIndexMap,
265                             const SectionOffsetsTy &SectionOffsets);
266
267     void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
268                           uint64_t Address, uint64_t Offset,
269                           uint64_t Size, uint32_t Link, uint32_t Info,
270                           uint64_t Alignment, uint64_t EntrySize);
271
272     void WriteRelocationsFragment(const MCAssembler &Asm,
273                                   MCDataFragment *F,
274                                   const MCSectionData *SD);
275
276     bool
277     IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
278                                            const MCSymbolData &DataA,
279                                            const MCFragment &FB,
280                                            bool InSet,
281                                            bool IsPCRel) const override;
282
283     bool isWeak(const MCSymbolData &SD) const override;
284
285     void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
286     void writeSection(MCAssembler &Asm,
287                       const SectionIndexMapTy &SectionIndexMap,
288                       uint32_t GroupSymbolIndex,
289                       uint64_t Offset, uint64_t Size, uint64_t Alignment,
290                       const MCSectionELF &Section);
291   };
292 }
293
294 FragmentWriter::FragmentWriter(bool IsLittleEndian)
295     : IsLittleEndian(IsLittleEndian) {}
296
297 template <typename T> void FragmentWriter::write(MCDataFragment &F, T Val) {
298   if (IsLittleEndian)
299     Val = support::endian::byte_swap<T, support::little>(Val);
300   else
301     Val = support::endian::byte_swap<T, support::big>(Val);
302   const char *Start = (const char *)&Val;
303   F.getContents().append(Start, Start + sizeof(T));
304 }
305
306 void SymbolTableWriter::createSymtabShndx() {
307   if (ShndxF)
308     return;
309
310   MCContext &Ctx = Asm.getContext();
311   const MCSectionELF *SymtabShndxSection =
312       Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
313   MCSectionData *SymtabShndxSD =
314       &Asm.getOrCreateSectionData(*SymtabShndxSection);
315   SymtabShndxSD->setAlignment(4);
316   ShndxF = new MCDataFragment(SymtabShndxSD);
317   Sections.push_back(SymtabShndxSection);
318
319   for (unsigned I = 0; I < NumWritten; ++I)
320     write(*ShndxF, uint32_t(0));
321 }
322
323 template <typename T>
324 void SymbolTableWriter::write(MCDataFragment &F, T Value) {
325   FWriter.write(F, Value);
326 }
327
328 SymbolTableWriter::SymbolTableWriter(
329     MCAssembler &Asm, FragmentWriter &FWriter, bool Is64Bit,
330     std::vector<const MCSectionELF *> &Sections, MCDataFragment *SymtabF)
331     : Asm(Asm), FWriter(FWriter), Is64Bit(Is64Bit), Sections(Sections),
332       SymtabF(SymtabF), ShndxF(nullptr), NumWritten(0) {}
333
334 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
335                                     uint64_t size, uint8_t other,
336                                     uint32_t shndx, bool Reserved) {
337   bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
338
339   if (LargeIndex)
340     createSymtabShndx();
341
342   if (ShndxF) {
343     if (LargeIndex)
344       write(*ShndxF, shndx);
345     else
346       write(*ShndxF, uint32_t(0));
347   }
348
349   uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
350
351   if (Is64Bit) {
352     write(*SymtabF, name);  // st_name
353     write(*SymtabF, info);  // st_info
354     write(*SymtabF, other); // st_other
355     write(*SymtabF, Index); // st_shndx
356     write(*SymtabF, value); // st_value
357     write(*SymtabF, size);  // st_size
358   } else {
359     write(*SymtabF, name);            // st_name
360     write(*SymtabF, uint32_t(value)); // st_value
361     write(*SymtabF, uint32_t(size));  // st_size
362     write(*SymtabF, info);            // st_info
363     write(*SymtabF, other);           // st_other
364     write(*SymtabF, Index);           // st_shndx
365   }
366
367   ++NumWritten;
368 }
369
370 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
371   const MCFixupKindInfo &FKI =
372     Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
373
374   return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
375 }
376
377 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
378   switch (Variant) {
379   default:
380     return false;
381   case MCSymbolRefExpr::VK_GOT:
382   case MCSymbolRefExpr::VK_PLT:
383   case MCSymbolRefExpr::VK_GOTPCREL:
384   case MCSymbolRefExpr::VK_GOTOFF:
385   case MCSymbolRefExpr::VK_TPOFF:
386   case MCSymbolRefExpr::VK_TLSGD:
387   case MCSymbolRefExpr::VK_GOTTPOFF:
388   case MCSymbolRefExpr::VK_INDNTPOFF:
389   case MCSymbolRefExpr::VK_NTPOFF:
390   case MCSymbolRefExpr::VK_GOTNTPOFF:
391   case MCSymbolRefExpr::VK_TLSLDM:
392   case MCSymbolRefExpr::VK_DTPOFF:
393   case MCSymbolRefExpr::VK_TLSLD:
394     return true;
395   }
396 }
397
398 ELFObjectWriter::~ELFObjectWriter()
399 {}
400
401 // Emit the ELF header.
402 void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
403                                   unsigned NumberOfSections) {
404   // ELF Header
405   // ----------
406   //
407   // Note
408   // ----
409   // emitWord method behaves differently for ELF32 and ELF64, writing
410   // 4 bytes in the former and 8 in the latter.
411
412   WriteBytes(ELF::ElfMagic); // e_ident[EI_MAG0] to e_ident[EI_MAG3]
413
414   Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
415
416   // e_ident[EI_DATA]
417   Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
418
419   Write8(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
420   // e_ident[EI_OSABI]
421   Write8(TargetObjectWriter->getOSABI());
422   Write8(0);                  // e_ident[EI_ABIVERSION]
423
424   WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
425
426   Write16(ELF::ET_REL);             // e_type
427
428   Write16(TargetObjectWriter->getEMachine()); // e_machine = target
429
430   Write32(ELF::EV_CURRENT);         // e_version
431   WriteWord(0);                    // e_entry, no entry point in .o file
432   WriteWord(0);                    // e_phoff, no program header for .o
433   WriteWord(0);                     // e_shoff = sec hdr table off in bytes
434
435   // e_flags = whatever the target wants
436   Write32(Asm.getELFHeaderEFlags());
437
438   // e_ehsize = ELF header size
439   Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
440
441   Write16(0);                  // e_phentsize = prog header entry size
442   Write16(0);                  // e_phnum = # prog header entries = 0
443
444   // e_shentsize = Section header entry size
445   Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
446
447   // e_shnum     = # of section header ents
448   if (NumberOfSections >= ELF::SHN_LORESERVE)
449     Write16(ELF::SHN_UNDEF);
450   else
451     Write16(NumberOfSections);
452
453   // e_shstrndx  = Section # of '.shstrtab'
454   if (ShstrtabIndex >= ELF::SHN_LORESERVE)
455     Write16(ELF::SHN_XINDEX);
456   else
457     Write16(ShstrtabIndex);
458 }
459
460 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
461                                       const MCAsmLayout &Layout) {
462   if (Data.isCommon() && Data.isExternal())
463     return Data.getCommonAlignment();
464
465   uint64_t Res;
466   if (!Layout.getSymbolOffset(&Data, Res))
467     return 0;
468
469   if (Layout.getAssembler().isThumbFunc(&Data.getSymbol()))
470     Res |= 1;
471
472   return Res;
473 }
474
475 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
476                                                const MCAsmLayout &Layout) {
477   // The presence of symbol versions causes undefined symbols and
478   // versions declared with @@@ to be renamed.
479
480   for (MCSymbolData &OriginalData : Asm.symbols()) {
481     const MCSymbol &Alias = OriginalData.getSymbol();
482
483     // Not an alias.
484     if (!Alias.isVariable())
485       continue;
486     auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
487     if (!Ref)
488       continue;
489     const MCSymbol &Symbol = Ref->getSymbol();
490     MCSymbolData &SD = Asm.getSymbolData(Symbol);
491
492     StringRef AliasName = Alias.getName();
493     size_t Pos = AliasName.find('@');
494     if (Pos == StringRef::npos)
495       continue;
496
497     // Aliases defined with .symvar copy the binding from the symbol they alias.
498     // This is the first place we are able to copy this information.
499     OriginalData.setExternal(SD.isExternal());
500     MCELF::SetBinding(OriginalData, MCELF::GetBinding(SD));
501
502     StringRef Rest = AliasName.substr(Pos);
503     if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
504       continue;
505
506     // FIXME: produce a better error message.
507     if (Symbol.isUndefined() && Rest.startswith("@@") &&
508         !Rest.startswith("@@@"))
509       report_fatal_error("A @@ version cannot be undefined");
510
511     Renames.insert(std::make_pair(&Symbol, &Alias));
512   }
513 }
514
515 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
516   uint8_t Type = newType;
517
518   // Propagation rules:
519   // IFUNC > FUNC > OBJECT > NOTYPE
520   // TLS_OBJECT > OBJECT > NOTYPE
521   //
522   // dont let the new type degrade the old type
523   switch (origType) {
524   default:
525     break;
526   case ELF::STT_GNU_IFUNC:
527     if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
528         Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
529       Type = ELF::STT_GNU_IFUNC;
530     break;
531   case ELF::STT_FUNC:
532     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
533         Type == ELF::STT_TLS)
534       Type = ELF::STT_FUNC;
535     break;
536   case ELF::STT_OBJECT:
537     if (Type == ELF::STT_NOTYPE)
538       Type = ELF::STT_OBJECT;
539     break;
540   case ELF::STT_TLS:
541     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
542         Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
543       Type = ELF::STT_TLS;
544     break;
545   }
546
547   return Type;
548 }
549
550 void ELFObjectWriter::WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
551                                   const MCAsmLayout &Layout) {
552   MCSymbolData &OrigData = *MSD.SymbolData;
553   assert((!OrigData.getFragment() ||
554           (&OrigData.getFragment()->getParent()->getSection() ==
555            &OrigData.getSymbol().getSection())) &&
556          "The symbol's section doesn't match the fragment's symbol");
557   const MCSymbol *Base = Layout.getBaseSymbol(OrigData.getSymbol());
558
559   // This has to be in sync with when computeSymbolTable uses SHN_ABS or
560   // SHN_COMMON.
561   bool IsReserved = !Base || OrigData.isCommon();
562
563   // Binding and Type share the same byte as upper and lower nibbles
564   uint8_t Binding = MCELF::GetBinding(OrigData);
565   uint8_t Type = MCELF::GetType(OrigData);
566   MCSymbolData *BaseSD = nullptr;
567   if (Base) {
568     BaseSD = &Layout.getAssembler().getSymbolData(*Base);
569     Type = mergeTypeForSet(Type, MCELF::GetType(*BaseSD));
570   }
571   uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
572
573   // Other and Visibility share the same byte with Visibility using the lower
574   // 2 bits
575   uint8_t Visibility = MCELF::GetVisibility(OrigData);
576   uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
577   Other |= Visibility;
578
579   uint64_t Value = SymbolValue(OrigData, Layout);
580   uint64_t Size = 0;
581
582   const MCExpr *ESize = OrigData.getSize();
583   if (!ESize && Base)
584     ESize = BaseSD->getSize();
585
586   if (ESize) {
587     int64_t Res;
588     if (!ESize->evaluateKnownAbsolute(Res, Layout))
589       report_fatal_error("Size expression must be absolute.");
590     Size = Res;
591   }
592
593   // Write out the symbol table entry
594   Writer.writeSymbol(MSD.StringIndex, Info, Value, Size, Other,
595                      MSD.SectionIndex, IsReserved);
596 }
597
598 void ELFObjectWriter::WriteSymbolTable(
599     MCDataFragment *SymtabF, MCAssembler &Asm, const MCAsmLayout &Layout,
600     std::vector<const MCSectionELF *> &Sections) {
601   // The string table must be emitted first because we need the index
602   // into the string table for all the symbol names.
603
604   // FIXME: Make sure the start of the symbol table is aligned.
605
606   SymbolTableWriter Writer(Asm, FWriter, is64Bit(), Sections, SymtabF);
607
608   // The first entry is the undefined symbol entry.
609   Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
610
611   for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
612     Writer.writeSymbol(FileSymbolData[i], ELF::STT_FILE | ELF::STB_LOCAL, 0, 0,
613                        ELF::STV_DEFAULT, ELF::SHN_ABS, true);
614   }
615
616   // Write the symbol table entries.
617   LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
618
619   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
620     ELFSymbolData &MSD = LocalSymbolData[i];
621     WriteSymbol(Writer, MSD, Layout);
622   }
623
624   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
625     ELFSymbolData &MSD = ExternalSymbolData[i];
626     MCSymbolData &Data = *MSD.SymbolData;
627     assert(((Data.getFlags() & ELF_STB_Global) ||
628             (Data.getFlags() & ELF_STB_Weak)) &&
629            "External symbol requires STB_GLOBAL or STB_WEAK flag");
630     WriteSymbol(Writer, MSD, Layout);
631     if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
632       LastLocalSymbolIndex++;
633   }
634
635   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
636     ELFSymbolData &MSD = UndefinedSymbolData[i];
637     MCSymbolData &Data = *MSD.SymbolData;
638     WriteSymbol(Writer, MSD, Layout);
639     if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
640       LastLocalSymbolIndex++;
641   }
642 }
643
644 // It is always valid to create a relocation with a symbol. It is preferable
645 // to use a relocation with a section if that is possible. Using the section
646 // allows us to omit some local symbols from the symbol table.
647 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
648                                                const MCSymbolRefExpr *RefA,
649                                                const MCSymbolData *SD,
650                                                uint64_t C,
651                                                unsigned Type) const {
652   // A PCRel relocation to an absolute value has no symbol (or section). We
653   // represent that with a relocation to a null section.
654   if (!RefA)
655     return false;
656
657   MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
658   switch (Kind) {
659   default:
660     break;
661   // The .odp creation emits a relocation against the symbol ".TOC." which
662   // create a R_PPC64_TOC relocation. However the relocation symbol name
663   // in final object creation should be NULL, since the symbol does not
664   // really exist, it is just the reference to TOC base for the current
665   // object file. Since the symbol is undefined, returning false results
666   // in a relocation with a null section which is the desired result.
667   case MCSymbolRefExpr::VK_PPC_TOCBASE:
668     return false;
669
670   // These VariantKind cause the relocation to refer to something other than
671   // the symbol itself, like a linker generated table. Since the address of
672   // symbol is not relevant, we cannot replace the symbol with the
673   // section and patch the difference in the addend.
674   case MCSymbolRefExpr::VK_GOT:
675   case MCSymbolRefExpr::VK_PLT:
676   case MCSymbolRefExpr::VK_GOTPCREL:
677   case MCSymbolRefExpr::VK_Mips_GOT:
678   case MCSymbolRefExpr::VK_PPC_GOT_LO:
679   case MCSymbolRefExpr::VK_PPC_GOT_HI:
680   case MCSymbolRefExpr::VK_PPC_GOT_HA:
681     return true;
682   }
683
684   // An undefined symbol is not in any section, so the relocation has to point
685   // to the symbol itself.
686   const MCSymbol &Sym = SD->getSymbol();
687   if (Sym.isUndefined())
688     return true;
689
690   unsigned Binding = MCELF::GetBinding(*SD);
691   switch(Binding) {
692   default:
693     llvm_unreachable("Invalid Binding");
694   case ELF::STB_LOCAL:
695     break;
696   case ELF::STB_WEAK:
697     // If the symbol is weak, it might be overridden by a symbol in another
698     // file. The relocation has to point to the symbol so that the linker
699     // can update it.
700     return true;
701   case ELF::STB_GLOBAL:
702     // Global ELF symbols can be preempted by the dynamic linker. The relocation
703     // has to point to the symbol for a reason analogous to the STB_WEAK case.
704     return true;
705   }
706
707   // If a relocation points to a mergeable section, we have to be careful.
708   // If the offset is zero, a relocation with the section will encode the
709   // same information. With a non-zero offset, the situation is different.
710   // For example, a relocation can point 42 bytes past the end of a string.
711   // If we change such a relocation to use the section, the linker would think
712   // that it pointed to another string and subtracting 42 at runtime will
713   // produce the wrong value.
714   auto &Sec = cast<MCSectionELF>(Sym.getSection());
715   unsigned Flags = Sec.getFlags();
716   if (Flags & ELF::SHF_MERGE) {
717     if (C != 0)
718       return true;
719
720     // It looks like gold has a bug (http://sourceware.org/PR16794) and can
721     // only handle section relocations to mergeable sections if using RELA.
722     if (!hasRelocationAddend())
723       return true;
724   }
725
726   // Most TLS relocations use a got, so they need the symbol. Even those that
727   // are just an offset (@tpoff), require a symbol in gold versions before
728   // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
729   // http://sourceware.org/PR16773.
730   if (Flags & ELF::SHF_TLS)
731     return true;
732
733   // If the symbol is a thumb function the final relocation must set the lowest
734   // bit. With a symbol that is done by just having the symbol have that bit
735   // set, so we would lose the bit if we relocated with the section.
736   // FIXME: We could use the section but add the bit to the relocation value.
737   if (Asm.isThumbFunc(&Sym))
738     return true;
739
740   if (TargetObjectWriter->needsRelocateWithSymbol(*SD, Type))
741     return true;
742   return false;
743 }
744
745 static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) {
746   const MCSymbol &Sym = Ref.getSymbol();
747
748   if (Ref.getKind() == MCSymbolRefExpr::VK_WEAKREF)
749     return &Sym;
750
751   if (!Sym.isVariable())
752     return nullptr;
753
754   const MCExpr *Expr = Sym.getVariableValue();
755   const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
756   if (!Inner)
757     return nullptr;
758
759   if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
760     return &Inner->getSymbol();
761   return nullptr;
762 }
763
764 // True if the assembler knows nothing about the final value of the symbol.
765 // This doesn't cover the comdat issues, since in those cases the assembler
766 // can at least know that all symbols in the section will move together.
767 static bool isWeak(const MCSymbolData &D) {
768   if (MCELF::GetType(D) == ELF::STT_GNU_IFUNC)
769     return true;
770
771   switch (MCELF::GetBinding(D)) {
772   default:
773     llvm_unreachable("Unknown binding");
774   case ELF::STB_LOCAL:
775     return false;
776   case ELF::STB_GLOBAL:
777     return false;
778   case ELF::STB_WEAK:
779   case ELF::STB_GNU_UNIQUE:
780     return true;
781   }
782 }
783
784 void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
785                                        const MCAsmLayout &Layout,
786                                        const MCFragment *Fragment,
787                                        const MCFixup &Fixup, MCValue Target,
788                                        bool &IsPCRel, uint64_t &FixedValue) {
789   const MCSectionData *FixupSection = Fragment->getParent();
790   uint64_t C = Target.getConstant();
791   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
792
793   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
794     assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
795            "Should not have constructed this");
796
797     // Let A, B and C being the components of Target and R be the location of
798     // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
799     // If it is pcrel, we want to compute (A - B + C - R).
800
801     // In general, ELF has no relocations for -B. It can only represent (A + C)
802     // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
803     // replace B to implement it: (A - R - K + C)
804     if (IsPCRel)
805       Asm.getContext().FatalError(
806           Fixup.getLoc(),
807           "No relocation available to represent this relative expression");
808
809     const MCSymbol &SymB = RefB->getSymbol();
810
811     if (SymB.isUndefined())
812       Asm.getContext().FatalError(
813           Fixup.getLoc(),
814           Twine("symbol '") + SymB.getName() +
815               "' can not be undefined in a subtraction expression");
816
817     assert(!SymB.isAbsolute() && "Should have been folded");
818     const MCSection &SecB = SymB.getSection();
819     if (&SecB != &FixupSection->getSection())
820       Asm.getContext().FatalError(
821           Fixup.getLoc(), "Cannot represent a difference across sections");
822
823     const MCSymbolData &SymBD = Asm.getSymbolData(SymB);
824     if (::isWeak(SymBD))
825       Asm.getContext().FatalError(
826           Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
827
828     uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD);
829     uint64_t K = SymBOffset - FixupOffset;
830     IsPCRel = true;
831     C -= K;
832   }
833
834   // We either rejected the fixup or folded B into C at this point.
835   const MCSymbolRefExpr *RefA = Target.getSymA();
836   const MCSymbol *SymA = RefA ? &RefA->getSymbol() : nullptr;
837   const MCSymbolData *SymAD = SymA ? &Asm.getSymbolData(*SymA) : nullptr;
838
839   unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
840   bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymAD, C, Type);
841   if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
842     C += Layout.getSymbolOffset(SymAD);
843
844   uint64_t Addend = 0;
845   if (hasRelocationAddend()) {
846     Addend = C;
847     C = 0;
848   }
849
850   FixedValue = C;
851
852   // FIXME: What is this!?!?
853   MCSymbolRefExpr::VariantKind Modifier =
854       RefA ? RefA->getKind() : MCSymbolRefExpr::VK_None;
855   if (RelocNeedsGOT(Modifier))
856     NeedsGOT = true;
857
858   if (!RelocateWithSymbol) {
859     const MCSection *SecA =
860         (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
861     auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
862     MCSymbol *SectionSymbol =
863         ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
864                : nullptr;
865     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
866     Relocations[FixupSection].push_back(Rec);
867     return;
868   }
869
870   if (SymA) {
871     if (const MCSymbol *R = Renames.lookup(SymA))
872       SymA = R;
873
874     if (const MCSymbol *WeakRef = getWeakRef(*RefA))
875       WeakrefUsedInReloc.insert(WeakRef);
876     else
877       UsedInReloc.insert(SymA);
878   }
879   ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
880   Relocations[FixupSection].push_back(Rec);
881   return;
882 }
883
884
885 uint64_t
886 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
887                                              const MCSymbol *S) {
888   const MCSymbolData &SD = Asm.getSymbolData(*S);
889   return SD.getIndex();
890 }
891
892 bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
893                                  const MCSymbolData &Data, bool Used,
894                                  bool Renamed) {
895   const MCSymbol &Symbol = Data.getSymbol();
896   if (Symbol.isVariable()) {
897     const MCExpr *Expr = Symbol.getVariableValue();
898     if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
899       if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
900         return false;
901     }
902   }
903
904   if (Used)
905     return true;
906
907   if (Renamed)
908     return false;
909
910   if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
911     return true;
912
913   if (Symbol.isVariable()) {
914     const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
915     if (Base && Base->isUndefined())
916       return false;
917   }
918
919   bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
920   if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
921     return false;
922
923   if (Symbol.isTemporary())
924     return false;
925
926   return true;
927 }
928
929 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isUsedInReloc) {
930   if (Data.isExternal())
931     return false;
932
933   const MCSymbol &Symbol = Data.getSymbol();
934   if (Symbol.isDefined())
935     return true;
936
937   if (isUsedInReloc)
938     return false;
939
940   return true;
941 }
942
943 void ELFObjectWriter::maybeAddToGroup(MCAssembler &Asm,
944                                       const RevGroupMapTy &RevGroupMap,
945                                       const MCSectionELF &Section,
946                                       unsigned Index) {
947   const MCSymbol *Sym = Section.getGroup();
948   if (!Sym)
949     return;
950   const MCSectionELF *Group = RevGroupMap.lookup(Sym);
951   MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
952   // FIXME: we could use the previous fragment
953   MCDataFragment *F = new MCDataFragment(&Data);
954   write(*F, Index);
955 }
956
957 void ELFObjectWriter::computeIndexMap(
958     MCAssembler &Asm, std::vector<const MCSectionELF *> &Sections,
959     SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap) {
960   for (const MCSectionData &SD : Asm) {
961     const MCSectionELF &Section =
962         static_cast<const MCSectionELF &>(SD.getSection());
963     if (Section.getType() != ELF::SHT_GROUP)
964       continue;
965     Sections.push_back(&Section);
966     SectionIndexMap[&Section] = Sections.size();
967   }
968
969   std::vector<const MCSectionELF *> RelSections;
970   for (const MCSectionData &SD : Asm) {
971     const MCSectionELF &Section =
972       static_cast<const MCSectionELF &>(SD.getSection());
973     if (Section.getType() == ELF::SHT_GROUP ||
974         Section.getType() == ELF::SHT_REL ||
975         Section.getType() == ELF::SHT_RELA)
976       continue;
977     Sections.push_back(&Section);
978     unsigned Index = Sections.size();
979     SectionIndexMap[&Section] = Index;
980     maybeAddToGroup(Asm, RevGroupMap, Section, Index);
981
982     if (MCSectionData *RelSD = createRelocationSection(Asm, SD)) {
983       const MCSectionELF *RelSection =
984           static_cast<const MCSectionELF *>(&RelSD->getSection());
985       RelSections.push_back(RelSection);
986     }
987   }
988
989   // Put relocation sections close together. The linker reads them
990   // first, so this improves cache locality.
991   for (const MCSectionELF *Sec : RelSections) {
992     Sections.push_back(Sec);
993     unsigned Index = Sections.size();
994     maybeAddToGroup(Asm, RevGroupMap, *Sec, Index);
995   }
996 }
997
998 void ELFObjectWriter::computeSymbolTable(
999     MCAssembler &Asm, const MCAsmLayout &Layout,
1000     const SectionIndexMapTy &SectionIndexMap,
1001     const RevGroupMapTy &RevGroupMap) {
1002   // FIXME: Is this the correct place to do this?
1003   // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
1004   if (NeedsGOT) {
1005     StringRef Name = "_GLOBAL_OFFSET_TABLE_";
1006     MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
1007     MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
1008     Data.setExternal(true);
1009     MCELF::SetBinding(Data, ELF::STB_GLOBAL);
1010   }
1011
1012   // Add the data for the symbols.
1013   for (MCSymbolData &SD : Asm.symbols()) {
1014     const MCSymbol &Symbol = SD.getSymbol();
1015
1016     bool Used = UsedInReloc.count(&Symbol);
1017     bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
1018     bool isSignature = RevGroupMap.count(&Symbol);
1019
1020     if (!isInSymtab(Layout, SD,
1021                     Used || WeakrefUsed || isSignature,
1022                     Renames.count(&Symbol)))
1023       continue;
1024
1025     ELFSymbolData MSD;
1026     MSD.SymbolData = &SD;
1027     const MCSymbol *BaseSymbol = Layout.getBaseSymbol(Symbol);
1028
1029     // Undefined symbols are global, but this is the first place we
1030     // are able to set it.
1031     bool Local = isLocal(SD, Used);
1032     if (!Local && MCELF::GetBinding(SD) == ELF::STB_LOCAL) {
1033       assert(BaseSymbol);
1034       MCSymbolData &BaseData = Asm.getSymbolData(*BaseSymbol);
1035       MCELF::SetBinding(SD, ELF::STB_GLOBAL);
1036       MCELF::SetBinding(BaseData, ELF::STB_GLOBAL);
1037     }
1038
1039     if (!BaseSymbol) {
1040       MSD.SectionIndex = ELF::SHN_ABS;
1041     } else if (SD.isCommon()) {
1042       assert(!Local);
1043       MSD.SectionIndex = ELF::SHN_COMMON;
1044     } else if (BaseSymbol->isUndefined()) {
1045       if (isSignature && !Used)
1046         MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap.lookup(&Symbol));
1047       else
1048         MSD.SectionIndex = ELF::SHN_UNDEF;
1049       if (!Used && WeakrefUsed)
1050         MCELF::SetBinding(SD, ELF::STB_WEAK);
1051     } else {
1052       const MCSectionELF &Section =
1053         static_cast<const MCSectionELF&>(BaseSymbol->getSection());
1054       MSD.SectionIndex = SectionIndexMap.lookup(&Section);
1055       assert(MSD.SectionIndex && "Invalid section index!");
1056     }
1057
1058     // The @@@ in symbol version is replaced with @ in undefined symbols and @@
1059     // in defined ones.
1060     //
1061     // FIXME: All name handling should be done before we get to the writer,
1062     // including dealing with GNU-style version suffixes.  Fixing this isn't
1063     // trivial.
1064     //
1065     // We thus have to be careful to not perform the symbol version replacement
1066     // blindly:
1067     //
1068     // The ELF format is used on Windows by the MCJIT engine.  Thus, on
1069     // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
1070     // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
1071     // C++ name mangling can legally have "@@@" as a sub-string. In that case,
1072     // the EFLObjectWriter should not interpret the "@@@" sub-string as
1073     // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
1074     // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
1075     // "__imp_?" or "__imp_@?".
1076     //
1077     // It would have been interesting to perform the MS mangling prefix check
1078     // only when the target triple is of the form *-pc-windows-elf. But, it
1079     // seems that this information is not easily accessible from the
1080     // ELFObjectWriter.
1081     StringRef Name = Symbol.getName();
1082     if (!Name.startswith("?") && !Name.startswith("@?") &&
1083         !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
1084       // This symbol isn't following the MSVC C++ name mangling convention. We
1085       // can thus safely interpret the @@@ in symbol names as specifying symbol
1086       // versioning.
1087       SmallString<32> Buf;
1088       size_t Pos = Name.find("@@@");
1089       if (Pos != StringRef::npos) {
1090         Buf += Name.substr(0, Pos);
1091         unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1092         Buf += Name.substr(Pos + Skip);
1093         Name = Buf;
1094       }
1095     }
1096
1097     // Sections have their own string table
1098     if (MCELF::GetType(SD) != ELF::STT_SECTION)
1099       MSD.Name = StrTabBuilder.add(Name);
1100
1101     if (MSD.SectionIndex == ELF::SHN_UNDEF)
1102       UndefinedSymbolData.push_back(MSD);
1103     else if (Local)
1104       LocalSymbolData.push_back(MSD);
1105     else
1106       ExternalSymbolData.push_back(MSD);
1107   }
1108
1109   for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1110     StrTabBuilder.add(*i);
1111
1112   StrTabBuilder.finalize(StringTableBuilder::ELF);
1113
1114   for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1115     FileSymbolData.push_back(StrTabBuilder.getOffset(*i));
1116
1117   for (ELFSymbolData &MSD : LocalSymbolData)
1118     MSD.StringIndex = MCELF::GetType(*MSD.SymbolData) == ELF::STT_SECTION
1119                           ? 0
1120                           : StrTabBuilder.getOffset(MSD.Name);
1121   for (ELFSymbolData &MSD : ExternalSymbolData)
1122     MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1123   for (ELFSymbolData& MSD : UndefinedSymbolData)
1124     MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1125
1126   // Symbols are required to be in lexicographic order.
1127   array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1128   array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1129   array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1130
1131   // Set the symbol indices. Local symbols must come before all other
1132   // symbols with non-local bindings.
1133   unsigned Index = FileSymbolData.size() + 1;
1134   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1135     LocalSymbolData[i].SymbolData->setIndex(Index++);
1136
1137   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1138     ExternalSymbolData[i].SymbolData->setIndex(Index++);
1139   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1140     UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1141 }
1142
1143 MCSectionData *
1144 ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
1145                                          const MCSectionData &SD) {
1146   if (Relocations[&SD].empty())
1147     return nullptr;
1148
1149   MCContext &Ctx = Asm.getContext();
1150   const MCSectionELF &Section =
1151       static_cast<const MCSectionELF &>(SD.getSection());
1152
1153   const StringRef SectionName = Section.getSectionName();
1154   std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
1155   RelaSectionName += SectionName;
1156
1157   unsigned EntrySize;
1158   if (hasRelocationAddend())
1159     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1160   else
1161     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
1162
1163   unsigned Flags = 0;
1164   if (Section.getFlags() & ELF::SHF_GROUP)
1165     Flags = ELF::SHF_GROUP;
1166
1167   const MCSectionELF *RelaSection = Ctx.createELFRelSection(
1168       RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
1169       Flags, EntrySize, Section.getGroup(), &Section);
1170   return &Asm.getOrCreateSectionData(*RelaSection);
1171 }
1172
1173 static SmallVector<char, 128>
1174 getUncompressedData(MCAsmLayout &Layout,
1175                     MCSectionData::FragmentListType &Fragments) {
1176   SmallVector<char, 128> UncompressedData;
1177   for (const MCFragment &F : Fragments) {
1178     const SmallVectorImpl<char> *Contents;
1179     switch (F.getKind()) {
1180     case MCFragment::FT_Data:
1181       Contents = &cast<MCDataFragment>(F).getContents();
1182       break;
1183     case MCFragment::FT_Dwarf:
1184       Contents = &cast<MCDwarfLineAddrFragment>(F).getContents();
1185       break;
1186     case MCFragment::FT_DwarfFrame:
1187       Contents = &cast<MCDwarfCallFrameFragment>(F).getContents();
1188       break;
1189     default:
1190       llvm_unreachable(
1191           "Not expecting any other fragment types in a debug_* section");
1192     }
1193     UncompressedData.append(Contents->begin(), Contents->end());
1194   }
1195   return UncompressedData;
1196 }
1197
1198 // Include the debug info compression header:
1199 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
1200 // useful for consumers to preallocate a buffer to decompress into.
1201 static bool
1202 prependCompressionHeader(uint64_t Size,
1203                          SmallVectorImpl<char> &CompressedContents) {
1204   const StringRef Magic = "ZLIB";
1205   if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1206     return false;
1207   if (sys::IsLittleEndianHost)
1208     sys::swapByteOrder(Size);
1209   CompressedContents.insert(CompressedContents.begin(),
1210                             Magic.size() + sizeof(Size), 0);
1211   std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
1212   std::copy(reinterpret_cast<char *>(&Size),
1213             reinterpret_cast<char *>(&Size + 1),
1214             CompressedContents.begin() + Magic.size());
1215   return true;
1216 }
1217
1218 // Return a single fragment containing the compressed contents of the whole
1219 // section. Null if the section was not compressed for any reason.
1220 static std::unique_ptr<MCDataFragment>
1221 getCompressedFragment(MCAsmLayout &Layout,
1222                       MCSectionData::FragmentListType &Fragments) {
1223   std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment());
1224
1225   // Gather the uncompressed data from all the fragments, recording the
1226   // alignment fragment, if seen, and any fixups.
1227   SmallVector<char, 128> UncompressedData =
1228       getUncompressedData(Layout, Fragments);
1229
1230   SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents();
1231
1232   zlib::Status Success = zlib::compress(
1233       StringRef(UncompressedData.data(), UncompressedData.size()),
1234       CompressedContents);
1235   if (Success != zlib::StatusOK)
1236     return nullptr;
1237
1238   if (!prependCompressionHeader(UncompressedData.size(), CompressedContents))
1239     return nullptr;
1240
1241   return CompressedFragment;
1242 }
1243
1244 typedef DenseMap<const MCSectionData *, std::vector<MCSymbolData *>>
1245 DefiningSymbolMap;
1246
1247 static void UpdateSymbols(const MCAsmLayout &Layout,
1248                           const std::vector<MCSymbolData *> &Symbols,
1249                           MCFragment &NewFragment) {
1250   for (MCSymbolData *Sym : Symbols) {
1251     Sym->setOffset(Sym->getOffset() +
1252                    Layout.getFragmentOffset(Sym->getFragment()));
1253     Sym->setFragment(&NewFragment);
1254   }
1255 }
1256
1257 static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
1258                                  const DefiningSymbolMap &DefiningSymbols,
1259                                  const MCSectionELF &Section,
1260                                  MCSectionData &SD) {
1261   StringRef SectionName = Section.getSectionName();
1262   MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
1263
1264   std::unique_ptr<MCDataFragment> CompressedFragment =
1265       getCompressedFragment(Layout, Fragments);
1266
1267   // Leave the section as-is if the fragments could not be compressed.
1268   if (!CompressedFragment)
1269     return;
1270
1271   // Update the fragment+offsets of any symbols referring to fragments in this
1272   // section to refer to the new fragment.
1273   auto I = DefiningSymbols.find(&SD);
1274   if (I != DefiningSymbols.end())
1275     UpdateSymbols(Layout, I->second, *CompressedFragment);
1276
1277   // Invalidate the layout for the whole section since it will have new and
1278   // different fragments now.
1279   Layout.invalidateFragmentsFrom(&Fragments.front());
1280   Fragments.clear();
1281
1282   // Complete the initialization of the new fragment
1283   CompressedFragment->setParent(&SD);
1284   CompressedFragment->setLayoutOrder(0);
1285   Fragments.push_back(CompressedFragment.release());
1286
1287   // Rename from .debug_* to .zdebug_*
1288   Asm.getContext().renameELFSection(&Section,
1289                                     (".z" + SectionName.drop_front(1)).str());
1290 }
1291
1292 void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm,
1293                                             MCAsmLayout &Layout) {
1294   if (!Asm.getContext().getAsmInfo()->compressDebugSections())
1295     return;
1296
1297   DefiningSymbolMap DefiningSymbols;
1298
1299   for (MCSymbolData &SD : Asm.symbols())
1300     if (MCFragment *F = SD.getFragment())
1301       DefiningSymbols[F->getParent()].push_back(&SD);
1302
1303   for (MCSectionData &SD : Asm) {
1304     const MCSectionELF &Section =
1305         static_cast<const MCSectionELF &>(SD.getSection());
1306     StringRef SectionName = Section.getSectionName();
1307
1308     // Compressing debug_frame requires handling alignment fragments which is
1309     // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1310     // for writing to arbitrary buffers) for little benefit.
1311     if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame")
1312       continue;
1313
1314     CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD);
1315   }
1316 }
1317
1318 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
1319   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
1320     MCSectionData &RelSD = *it;
1321     const MCSectionELF &RelSection =
1322         static_cast<const MCSectionELF &>(RelSD.getSection());
1323
1324     unsigned Type = RelSection.getType();
1325     if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
1326       continue;
1327
1328     const MCSectionELF *Section = RelSection.getAssociatedSection();
1329     MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
1330     RelSD.setAlignment(is64Bit() ? 8 : 4);
1331
1332     MCDataFragment *F = new MCDataFragment(&RelSD);
1333     WriteRelocationsFragment(Asm, F, &SD);
1334   }
1335 }
1336
1337 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1338                                        uint64_t Flags, uint64_t Address,
1339                                        uint64_t Offset, uint64_t Size,
1340                                        uint32_t Link, uint32_t Info,
1341                                        uint64_t Alignment,
1342                                        uint64_t EntrySize) {
1343   Write32(Name);        // sh_name: index into string table
1344   Write32(Type);        // sh_type
1345   WriteWord(Flags);     // sh_flags
1346   WriteWord(Address);   // sh_addr
1347   WriteWord(Offset);    // sh_offset
1348   WriteWord(Size);      // sh_size
1349   Write32(Link);        // sh_link
1350   Write32(Info);        // sh_info
1351   WriteWord(Alignment); // sh_addralign
1352   WriteWord(EntrySize); // sh_entsize
1353 }
1354
1355 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1356                                                MCDataFragment *F,
1357                                                const MCSectionData *SD) {
1358   std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1359
1360   // Sort the relocation entries. Most targets just sort by Offset, but some
1361   // (e.g., MIPS) have additional constraints.
1362   TargetObjectWriter->sortRelocs(Asm, Relocs);
1363
1364   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1365     const ELFRelocationEntry &Entry = Relocs[e - i - 1];
1366     unsigned Index =
1367         Entry.Symbol ? getSymbolIndexInSymbolTable(Asm, Entry.Symbol) : 0;
1368
1369     if (is64Bit()) {
1370       write(*F, Entry.Offset);
1371       if (TargetObjectWriter->isN64()) {
1372         write(*F, uint32_t(Index));
1373
1374         write(*F, TargetObjectWriter->getRSsym(Entry.Type));
1375         write(*F, TargetObjectWriter->getRType3(Entry.Type));
1376         write(*F, TargetObjectWriter->getRType2(Entry.Type));
1377         write(*F, TargetObjectWriter->getRType(Entry.Type));
1378       } else {
1379         struct ELF::Elf64_Rela ERE64;
1380         ERE64.setSymbolAndType(Index, Entry.Type);
1381         write(*F, ERE64.r_info);
1382       }
1383       if (hasRelocationAddend())
1384         write(*F, Entry.Addend);
1385     } else {
1386       write(*F, uint32_t(Entry.Offset));
1387
1388       struct ELF::Elf32_Rela ERE32;
1389       ERE32.setSymbolAndType(Index, Entry.Type);
1390       write(*F, ERE32.r_info);
1391
1392       if (hasRelocationAddend())
1393         write(*F, uint32_t(Entry.Addend));
1394     }
1395   }
1396 }
1397
1398 void ELFObjectWriter::CreateMetadataSections(
1399     MCAssembler &Asm, MCAsmLayout &Layout,
1400     std::vector<const MCSectionELF *> &Sections) {
1401   MCContext &Ctx = Asm.getContext();
1402   MCDataFragment *F;
1403
1404   unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1405
1406   // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
1407   const MCSectionELF *ShstrtabSection =
1408       Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
1409   MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1410   ShstrtabSD.setAlignment(1);
1411   ShstrtabIndex = Sections.size() + 1;
1412   Sections.push_back(ShstrtabSection);
1413
1414   const MCSectionELF *SymtabSection =
1415     Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1416                       EntrySize, "");
1417   MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1418   SymtabSD.setAlignment(is64Bit() ? 8 : 4);
1419   SymbolTableIndex = Sections.size() + 1;
1420   Sections.push_back(SymtabSection);
1421
1422   const MCSectionELF *StrtabSection;
1423   StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1424   MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1425   StrtabSD.setAlignment(1);
1426   StringTableIndex = Sections.size() + 1;
1427   Sections.push_back(StrtabSection);
1428
1429   // Symbol table
1430   F = new MCDataFragment(&SymtabSD);
1431   WriteSymbolTable(F, Asm, Layout, Sections);
1432
1433   F = new MCDataFragment(&StrtabSD);
1434   F->getContents().append(StrTabBuilder.data().begin(),
1435                           StrTabBuilder.data().end());
1436
1437   F = new MCDataFragment(&ShstrtabSD);
1438
1439   // Section header string table.
1440   for (auto it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
1441     const MCSectionELF &Section =
1442       static_cast<const MCSectionELF&>(it->getSection());
1443     ShStrTabBuilder.add(Section.getSectionName());
1444   }
1445   ShStrTabBuilder.finalize(StringTableBuilder::ELF);
1446   F->getContents().append(ShStrTabBuilder.data().begin(),
1447                           ShStrTabBuilder.data().end());
1448 }
1449
1450 void ELFObjectWriter::createIndexedSections(
1451     MCAssembler &Asm, MCAsmLayout &Layout, RevGroupMapTy &RevGroupMap,
1452     std::vector<const MCSectionELF *> &Sections,
1453     SectionIndexMapTy &SectionIndexMap) {
1454   MCContext &Ctx = Asm.getContext();
1455
1456   // Build the groups
1457   for (const MCSectionData &SD : Asm) {
1458     const MCSectionELF &Section =
1459         static_cast<const MCSectionELF &>(SD.getSection());
1460     if (!(Section.getFlags() & ELF::SHF_GROUP))
1461       continue;
1462
1463     const MCSymbol *SignatureSymbol = Section.getGroup();
1464     Asm.getOrCreateSymbolData(*SignatureSymbol);
1465     const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
1466     if (!Group) {
1467       Group = Ctx.createELFGroupSection(SignatureSymbol);
1468       MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1469       Data.setAlignment(4);
1470       MCDataFragment *F = new MCDataFragment(&Data);
1471       write(*F, uint32_t(ELF::GRP_COMDAT));
1472     }
1473   }
1474
1475   computeIndexMap(Asm, Sections, SectionIndexMap, RevGroupMap);
1476 }
1477
1478 void ELFObjectWriter::writeSection(MCAssembler &Asm,
1479                                    const SectionIndexMapTy &SectionIndexMap,
1480                                    uint32_t GroupSymbolIndex,
1481                                    uint64_t Offset, uint64_t Size,
1482                                    uint64_t Alignment,
1483                                    const MCSectionELF &Section) {
1484   uint64_t sh_link = 0;
1485   uint64_t sh_info = 0;
1486
1487   switch(Section.getType()) {
1488   default:
1489     // Nothing to do.
1490     break;
1491
1492   case ELF::SHT_DYNAMIC:
1493     sh_link = ShStrTabBuilder.getOffset(Section.getSectionName());
1494     break;
1495
1496   case ELF::SHT_REL:
1497   case ELF::SHT_RELA: {
1498     sh_link = SymbolTableIndex;
1499     assert(sh_link && ".symtab not found");
1500     const MCSectionELF *InfoSection = Section.getAssociatedSection();
1501     sh_info = SectionIndexMap.lookup(InfoSection);
1502     break;
1503   }
1504
1505   case ELF::SHT_SYMTAB:
1506   case ELF::SHT_DYNSYM:
1507     sh_link = StringTableIndex;
1508     sh_info = LastLocalSymbolIndex;
1509     break;
1510
1511   case ELF::SHT_SYMTAB_SHNDX:
1512     sh_link = SymbolTableIndex;
1513     break;
1514
1515   case ELF::SHT_GROUP:
1516     sh_link = SymbolTableIndex;
1517     sh_info = GroupSymbolIndex;
1518     break;
1519   }
1520
1521   if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1522       Section.getType() == ELF::SHT_ARM_EXIDX)
1523     sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1524
1525   WriteSecHdrEntry(ShStrTabBuilder.getOffset(Section.getSectionName()),
1526                    Section.getType(),
1527                    Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1528                    Alignment, Section.getEntrySize());
1529 }
1530
1531 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1532   return SD.getOrdinal() == ~UINT32_C(0) &&
1533     !SD.getSection().isVirtualSection();
1534 }
1535
1536 void ELFObjectWriter::writeDataSectionData(MCAssembler &Asm,
1537                                            const MCAsmLayout &Layout,
1538                                            const MCSectionData &SD) {
1539   if (IsELFMetaDataSection(SD)) {
1540     for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1541          ++i) {
1542       const MCFragment &F = *i;
1543       assert(F.getKind() == MCFragment::FT_Data);
1544       WriteBytes(cast<MCDataFragment>(F).getContents());
1545     }
1546   } else {
1547     Asm.writeSectionData(&SD, Layout);
1548   }
1549 }
1550
1551 void ELFObjectWriter::writeSectionHeader(
1552     ArrayRef<const MCSectionELF *> Sections, MCAssembler &Asm,
1553     const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1554     const SectionOffsetsTy &SectionOffsets) {
1555   const unsigned NumSections = Asm.size();
1556
1557   // Null section first.
1558   uint64_t FirstSectionSize =
1559       (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1560   uint32_t FirstSectionLink =
1561     ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1562   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1563
1564   for (unsigned i = 0; i < NumSections; ++i) {
1565     const MCSectionELF &Section = *Sections[i];
1566     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1567     uint32_t GroupSymbolIndex;
1568     if (Section.getType() != ELF::SHT_GROUP)
1569       GroupSymbolIndex = 0;
1570     else
1571       GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, Section.getGroup());
1572
1573     const std::pair<uint64_t, uint64_t> &Offsets = SectionOffsets[i];
1574     uint64_t Size = Section.getType() == ELF::SHT_NOBITS
1575                         ? Layout.getSectionAddressSize(&SD)
1576                         : Offsets.second - Offsets.first;
1577
1578     writeSection(Asm, SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1579                  SD.getAlignment(), Section);
1580   }
1581 }
1582
1583 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1584                                   const MCAsmLayout &Layout) {
1585   RevGroupMapTy RevGroupMap;
1586   SectionIndexMapTy SectionIndexMap;
1587
1588   CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
1589   std::vector<const MCSectionELF *> Sections;
1590   createIndexedSections(Asm, const_cast<MCAsmLayout &>(Layout), RevGroupMap,
1591                         Sections, SectionIndexMap);
1592
1593   // Compute symbol table information.
1594   computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap);
1595
1596   WriteRelocations(Asm, const_cast<MCAsmLayout &>(Layout));
1597
1598   CreateMetadataSections(const_cast<MCAssembler &>(Asm),
1599                          const_cast<MCAsmLayout &>(Layout), Sections);
1600
1601   unsigned NumSections = Asm.size();
1602   SectionOffsetsTy SectionOffsets;
1603
1604   // Write out the ELF header ...
1605   WriteHeader(Asm, NumSections + 1);
1606
1607   // ... then the sections ...
1608   for (const MCSectionELF *Section : Sections) {
1609     const MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
1610     uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
1611     WriteZeros(Padding);
1612
1613     // Remember the offset into the file for this section.
1614     uint64_t SecStart = OS.tell();
1615     writeDataSectionData(Asm, Layout, SD);
1616     uint64_t SecEnd = OS.tell();
1617     SectionOffsets.push_back(std::make_pair(SecStart, SecEnd));
1618   }
1619
1620   uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1621   uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
1622   WriteZeros(Padding);
1623
1624   const unsigned SectionHeaderOffset = OS.tell();
1625
1626   // ... then the section header table ...
1627   writeSectionHeader(Sections, Asm, Layout, SectionIndexMap, SectionOffsets);
1628
1629   if (is64Bit()) {
1630     uint64_t Val = SectionHeaderOffset;
1631     if (sys::IsLittleEndianHost != IsLittleEndian)
1632       sys::swapByteOrder(Val);
1633     OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1634               offsetof(ELF::Elf64_Ehdr, e_shoff));
1635   } else {
1636     uint32_t Val = SectionHeaderOffset;
1637     if (sys::IsLittleEndianHost != IsLittleEndian)
1638       sys::swapByteOrder(Val);
1639     OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1640               offsetof(ELF::Elf32_Ehdr, e_shoff));
1641   }
1642 }
1643
1644 bool ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1645     const MCAssembler &Asm, const MCSymbolData &DataA, const MCFragment &FB,
1646     bool InSet, bool IsPCRel) const {
1647   if (IsPCRel) {
1648     assert(!InSet);
1649     if (::isWeak(DataA))
1650       return false;
1651   }
1652   return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, DataA, FB,
1653                                                                 InSet, IsPCRel);
1654 }
1655
1656 bool ELFObjectWriter::isWeak(const MCSymbolData &SD) const {
1657   if (::isWeak(SD))
1658     return true;
1659
1660   // It is invalid to replace a reference to a global in a comdat
1661   // with a reference to a local since out of comdat references
1662   // to a local are forbidden.
1663   // We could try to return false for more cases, like the reference
1664   // being in the same comdat or Sym being an alias to another global,
1665   // but it is not clear if it is worth the effort.
1666   if (MCELF::GetBinding(SD) != ELF::STB_GLOBAL)
1667     return false;
1668
1669   const MCSymbol &Sym = SD.getSymbol();
1670   if (!Sym.isInSection())
1671     return false;
1672
1673   const auto &Sec = cast<MCSectionELF>(Sym.getSection());
1674   return Sec.getGroup();
1675 }
1676
1677 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1678                                             raw_pwrite_stream &OS,
1679                                             bool IsLittleEndian) {
1680   return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
1681 }