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
39using namespace llvm;
40
41#undef  DEBUG_TYPE
42#define DEBUG_TYPE "reloc-info"
43
44namespace {
45typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy;
46
47class ELFObjectWriter;
48
49class 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
63public:
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
72class 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
142public:
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
239void ELFObjectWriter::align(unsigned Alignment) {
240  uint64_t Padding = OffsetToAlignment(getStream().tell(), Alignment);
241  WriteZeros(Padding);
242}
243
244unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
245  SectionTable.push_back(Sec);
246  StrTabBuilder.add(Sec->getSectionName());
247  return SectionTable.size();
248}
249
250void SymbolTableWriter::createSymtabShndx() {
251  if (!ShndxIndexes.empty())
252    return;
253
254  ShndxIndexes.resize(NumWritten);
255}
256
257template <typename T> void SymbolTableWriter::write(T Value) {
258  EWriter.write(Value);
259}
260
261SymbolTableWriter::SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit)
262    : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
263
264void 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
300ELFObjectWriter::~ELFObjectWriter()
301{}
302
303// Emit the ELF header.
304void 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
356uint64_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
371void 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
424static 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
459void 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.
505bool 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  auto &Sec = cast<MCSectionELF>(Sym->getSection());
572  unsigned Flags = Sec.getFlags();
573  if (Flags & ELF::SHF_MERGE) {
574    if (C != 0)
575      return true;
576
577    // It looks like gold has a bug (http://sourceware.org/PR16794) and can
578    // only handle section relocations to mergeable sections if using RELA.
579    if (!hasRelocationAddend())
580      return true;
581  }
582
583  // Most TLS relocations use a got, so they need the symbol. Even those that
584  // are just an offset (@tpoff), require a symbol in gold versions before
585  // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
586  // http://sourceware.org/PR16773.
587  if (Flags & ELF::SHF_TLS)
588    return true;
589
590  // If the symbol is a thumb function the final relocation must set the lowest
591  // bit. With a symbol that is done by just having the symbol have that bit
592  // set, so we would lose the bit if we relocated with the section.
593  // FIXME: We could use the section but add the bit to the relocation value.
594  if (Asm.isThumbFunc(Sym))
595    return true;
596
597  if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
598    return true;
599  return false;
600}
601
602// True if the assembler knows nothing about the final value of the symbol.
603// This doesn't cover the comdat issues, since in those cases the assembler
604// can at least know that all symbols in the section will move together.
605static bool isWeak(const MCSymbolELF &Sym) {
606  if (Sym.getType() == ELF::STT_GNU_IFUNC)
607    return true;
608
609  switch (Sym.getBinding()) {
610  default:
611    llvm_unreachable("Unknown binding");
612  case ELF::STB_LOCAL:
613    return false;
614  case ELF::STB_GLOBAL:
615    return false;
616  case ELF::STB_WEAK:
617  case ELF::STB_GNU_UNIQUE:
618    return true;
619  }
620}
621
622void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
623                                       const MCAsmLayout &Layout,
624                                       const MCFragment *Fragment,
625                                       const MCFixup &Fixup, MCValue Target,
626                                       bool &IsPCRel, uint64_t &FixedValue) {
627  const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
628  uint64_t C = Target.getConstant();
629  uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
630  MCContext &Ctx = Asm.getContext();
631
632  if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
633    assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
634           "Should not have constructed this");
635
636    // Let A, B and C being the components of Target and R be the location of
637    // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
638    // If it is pcrel, we want to compute (A - B + C - R).
639
640    // In general, ELF has no relocations for -B. It can only represent (A + C)
641    // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
642    // replace B to implement it: (A - R - K + C)
643    if (IsPCRel) {
644      Ctx.reportError(
645          Fixup.getLoc(),
646          "No relocation available to represent this relative expression");
647      return;
648    }
649
650    const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
651
652    if (SymB.isUndefined()) {
653      Ctx.reportError(Fixup.getLoc(),
654                      Twine("symbol '") + SymB.getName() +
655                          "' can not be undefined in a subtraction expression");
656      return;
657    }
658
659    assert(!SymB.isAbsolute() && "Should have been folded");
660    const MCSection &SecB = SymB.getSection();
661    if (&SecB != &FixupSection) {
662      Ctx.reportError(Fixup.getLoc(),
663                      "Cannot represent a difference across sections");
664      return;
665    }
666
667    uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
668    uint64_t K = SymBOffset - FixupOffset;
669    IsPCRel = true;
670    C -= K;
671  }
672
673  // We either rejected the fixup or folded B into C at this point.
674  const MCSymbolRefExpr *RefA = Target.getSymA();
675  const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
676
677  bool ViaWeakRef = false;
678  if (SymA && SymA->isVariable()) {
679    const MCExpr *Expr = SymA->getVariableValue();
680    if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
681      if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
682        SymA = cast<MCSymbolELF>(&Inner->getSymbol());
683        ViaWeakRef = true;
684      }
685    }
686  }
687
688  unsigned Type = getRelocType(Ctx, Target, Fixup, IsPCRel);
689  uint64_t OriginalC = C;
690  bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
691  if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
692    C += Layout.getSymbolOffset(*SymA);
693
694  uint64_t Addend = 0;
695  if (hasRelocationAddend()) {
696    Addend = C;
697    C = 0;
698  }
699
700  FixedValue = C;
701
702  if (!RelocateWithSymbol) {
703    const MCSection *SecA =
704        (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
705    auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
706    const auto *SectionSymbol =
707        ELFSec ? cast<MCSymbolELF>(ELFSec->getBeginSymbol()) : nullptr;
708    if (SectionSymbol)
709      SectionSymbol->setUsedInReloc();
710    ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA,
711                           OriginalC);
712    Relocations[&FixupSection].push_back(Rec);
713    return;
714  }
715
716  const auto *RenamedSymA = SymA;
717  if (SymA) {
718    if (const MCSymbolELF *R = Renames.lookup(SymA))
719      RenamedSymA = R;
720
721    if (ViaWeakRef)
722      RenamedSymA->setIsWeakrefUsedInReloc();
723    else
724      RenamedSymA->setUsedInReloc();
725  }
726  ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA,
727                         OriginalC);
728  Relocations[&FixupSection].push_back(Rec);
729}
730
731bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
732                                 const MCSymbolELF &Symbol, bool Used,
733                                 bool Renamed) {
734  if (Symbol.isVariable()) {
735    const MCExpr *Expr = Symbol.getVariableValue();
736    if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
737      if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
738        return false;
739    }
740  }
741
742  if (Used)
743    return true;
744
745  if (Renamed)
746    return false;
747
748  if (Symbol.isVariable() && Symbol.isUndefined()) {
749    // FIXME: this is here just to diagnose the case of a var = commmon_sym.
750    Layout.getBaseSymbol(Symbol);
751    return false;
752  }
753
754  if (Symbol.isUndefined() && !Symbol.isBindingSet())
755    return false;
756
757  if (Symbol.isTemporary())
758    return false;
759
760  if (Symbol.getType() == ELF::STT_SECTION)
761    return false;
762
763  return true;
764}
765
766void ELFObjectWriter::computeSymbolTable(
767    MCAssembler &Asm, const MCAsmLayout &Layout,
768    const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
769    SectionOffsetsTy &SectionOffsets) {
770  MCContext &Ctx = Asm.getContext();
771  SymbolTableWriter Writer(*this, is64Bit());
772
773  // Symbol table
774  unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
775  MCSectionELF *SymtabSection =
776      Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
777  SymtabSection->setAlignment(is64Bit() ? 8 : 4);
778  SymbolTableIndex = addToSectionTable(SymtabSection);
779
780  align(SymtabSection->getAlignment());
781  uint64_t SecStart = getStream().tell();
782
783  // The first entry is the undefined symbol entry.
784  Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
785
786  std::vector<ELFSymbolData> LocalSymbolData;
787  std::vector<ELFSymbolData> ExternalSymbolData;
788
789  // Add the data for the symbols.
790  bool HasLargeSectionIndex = false;
791  for (const MCSymbol &S : Asm.symbols()) {
792    const auto &Symbol = cast<MCSymbolELF>(S);
793    bool Used = Symbol.isUsedInReloc();
794    bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
795    bool isSignature = Symbol.isSignature();
796
797    if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
798                    Renames.count(&Symbol)))
799      continue;
800
801    if (Symbol.isTemporary() && Symbol.isUndefined()) {
802      Ctx.reportError(SMLoc(), "Undefined temporary symbol");
803      continue;
804    }
805
806    ELFSymbolData MSD;
807    MSD.Symbol = cast<MCSymbolELF>(&Symbol);
808
809    bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
810    assert(Local || !Symbol.isTemporary());
811
812    if (Symbol.isAbsolute()) {
813      MSD.SectionIndex = ELF::SHN_ABS;
814    } else if (Symbol.isCommon()) {
815      assert(!Local);
816      MSD.SectionIndex = ELF::SHN_COMMON;
817    } else if (Symbol.isUndefined()) {
818      if (isSignature && !Used) {
819        MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
820        if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
821          HasLargeSectionIndex = true;
822      } else {
823        MSD.SectionIndex = ELF::SHN_UNDEF;
824      }
825    } else {
826      const MCSectionELF &Section =
827          static_cast<const MCSectionELF &>(Symbol.getSection());
828      MSD.SectionIndex = SectionIndexMap.lookup(&Section);
829      assert(MSD.SectionIndex && "Invalid section index!");
830      if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
831        HasLargeSectionIndex = true;
832    }
833
834    // The @@@ in symbol version is replaced with @ in undefined symbols and @@
835    // in defined ones.
836    //
837    // FIXME: All name handling should be done before we get to the writer,
838    // including dealing with GNU-style version suffixes.  Fixing this isn't
839    // trivial.
840    //
841    // We thus have to be careful to not perform the symbol version replacement
842    // blindly:
843    //
844    // The ELF format is used on Windows by the MCJIT engine.  Thus, on
845    // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
846    // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
847    // C++ name mangling can legally have "@@@" as a sub-string. In that case,
848    // the EFLObjectWriter should not interpret the "@@@" sub-string as
849    // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
850    // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
851    // "__imp_?" or "__imp_@?".
852    //
853    // It would have been interesting to perform the MS mangling prefix check
854    // only when the target triple is of the form *-pc-windows-elf. But, it
855    // seems that this information is not easily accessible from the
856    // ELFObjectWriter.
857    StringRef Name = Symbol.getName();
858    SmallString<32> Buf;
859    if (!Name.startswith("?") && !Name.startswith("@?") &&
860        !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
861      // This symbol isn't following the MSVC C++ name mangling convention. We
862      // can thus safely interpret the @@@ in symbol names as specifying symbol
863      // versioning.
864      size_t Pos = Name.find("@@@");
865      if (Pos != StringRef::npos) {
866        Buf += Name.substr(0, Pos);
867        unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
868        Buf += Name.substr(Pos + Skip);
869        Name = VersionSymSaver.save(Buf.c_str());
870      }
871    }
872
873    // Sections have their own string table
874    if (Symbol.getType() != ELF::STT_SECTION) {
875      MSD.Name = Name;
876      StrTabBuilder.add(Name);
877    }
878
879    if (Local)
880      LocalSymbolData.push_back(MSD);
881    else
882      ExternalSymbolData.push_back(MSD);
883  }
884
885  // This holds the .symtab_shndx section index.
886  unsigned SymtabShndxSectionIndex = 0;
887
888  if (HasLargeSectionIndex) {
889    MCSectionELF *SymtabShndxSection =
890        Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
891    SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
892    SymtabShndxSection->setAlignment(4);
893  }
894
895  ArrayRef<std::string> FileNames = Asm.getFileNames();
896  for (const std::string &Name : FileNames)
897    StrTabBuilder.add(Name);
898
899  StrTabBuilder.finalize();
900
901  for (const std::string &Name : FileNames)
902    Writer.writeSymbol(StrTabBuilder.getOffset(Name),
903                       ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
904                       ELF::SHN_ABS, true);
905
906  // Symbols are required to be in lexicographic order.
907  array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
908  array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
909
910  // Set the symbol indices. Local symbols must come before all other
911  // symbols with non-local bindings.
912  unsigned Index = FileNames.size() + 1;
913
914  for (ELFSymbolData &MSD : LocalSymbolData) {
915    unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
916                               ? 0
917                               : StrTabBuilder.getOffset(MSD.Name);
918    MSD.Symbol->setIndex(Index++);
919    writeSymbol(Writer, StringIndex, MSD, Layout);
920  }
921
922  // Write the symbol table entries.
923  LastLocalSymbolIndex = Index;
924
925  for (ELFSymbolData &MSD : ExternalSymbolData) {
926    unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
927    MSD.Symbol->setIndex(Index++);
928    writeSymbol(Writer, StringIndex, MSD, Layout);
929    assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
930  }
931
932  uint64_t SecEnd = getStream().tell();
933  SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
934
935  ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
936  if (ShndxIndexes.empty()) {
937    assert(SymtabShndxSectionIndex == 0);
938    return;
939  }
940  assert(SymtabShndxSectionIndex != 0);
941
942  SecStart = getStream().tell();
943  const MCSectionELF *SymtabShndxSection =
944      SectionTable[SymtabShndxSectionIndex - 1];
945  for (uint32_t Index : ShndxIndexes)
946    write(Index);
947  SecEnd = getStream().tell();
948  SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
949}
950
951MCSectionELF *
952ELFObjectWriter::createRelocationSection(MCContext &Ctx,
953                                         const MCSectionELF &Sec) {
954  if (Relocations[&Sec].empty())
955    return nullptr;
956
957  const StringRef SectionName = Sec.getSectionName();
958  std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
959  RelaSectionName += SectionName;
960
961  unsigned EntrySize;
962  if (hasRelocationAddend())
963    EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
964  else
965    EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
966
967  unsigned Flags = 0;
968  if (Sec.getFlags() & ELF::SHF_GROUP)
969    Flags = ELF::SHF_GROUP;
970
971  MCSectionELF *RelaSection = Ctx.createELFRelSection(
972      RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
973      Flags, EntrySize, Sec.getGroup(), &Sec);
974  RelaSection->setAlignment(is64Bit() ? 8 : 4);
975  return RelaSection;
976}
977
978// Include the debug info compression header.
979bool ELFObjectWriter::maybeWriteCompression(
980    uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
981    unsigned Alignment) {
982  if (ZLibStyle) {
983    uint64_t HdrSize =
984        is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
985    if (Size <= HdrSize + CompressedContents.size())
986      return false;
987    // Platform specific header is followed by compressed data.
988    if (is64Bit()) {
989      // Write Elf64_Chdr header.
990      write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
991      write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
992      write(static_cast<ELF::Elf64_Xword>(Size));
993      write(static_cast<ELF::Elf64_Xword>(Alignment));
994    } else {
995      // Write Elf32_Chdr header otherwise.
996      write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
997      write(static_cast<ELF::Elf32_Word>(Size));
998      write(static_cast<ELF::Elf32_Word>(Alignment));
999    }
1000    return true;
1001  }
1002
1003  // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
1004  // useful for consumers to preallocate a buffer to decompress into.
1005  const StringRef Magic = "ZLIB";
1006  if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1007    return false;
1008  write(ArrayRef<char>(Magic.begin(), Magic.size()));
1009  writeBE64(Size);
1010  return true;
1011}
1012
1013void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
1014                                       const MCAsmLayout &Layout) {
1015  MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1016  StringRef SectionName = Section.getSectionName();
1017
1018  // Compressing debug_frame requires handling alignment fragments which is
1019  // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1020  // for writing to arbitrary buffers) for little benefit.
1021  bool CompressionEnabled =
1022      Asm.getContext().getAsmInfo()->compressDebugSections() !=
1023      DebugCompressionType::DCT_None;
1024  if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
1025      SectionName == ".debug_frame") {
1026    Asm.writeSectionData(&Section, Layout);
1027    return;
1028  }
1029
1030  SmallVector<char, 128> UncompressedData;
1031  raw_svector_ostream VecOS(UncompressedData);
1032  raw_pwrite_stream &OldStream = getStream();
1033  setStream(VecOS);
1034  Asm.writeSectionData(&Section, Layout);
1035  setStream(OldStream);
1036
1037  SmallVector<char, 128> CompressedContents;
1038  zlib::Status Success = zlib::compress(
1039      StringRef(UncompressedData.data(), UncompressedData.size()),
1040      CompressedContents);
1041  if (Success != zlib::StatusOK) {
1042    getStream() << UncompressedData;
1043    return;
1044  }
1045
1046  bool ZlibStyle = Asm.getContext().getAsmInfo()->compressDebugSections() ==
1047                   DebugCompressionType::DCT_Zlib;
1048  if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
1049                             ZlibStyle, Sec.getAlignment())) {
1050    getStream() << UncompressedData;
1051    return;
1052  }
1053
1054  if (ZlibStyle)
1055    // Set the compressed flag. That is zlib style.
1056    Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
1057  else
1058    // Add "z" prefix to section name. This is zlib-gnu style.
1059    Asm.getContext().renameELFSection(&Section,
1060                                      (".z" + SectionName.drop_front(1)).str());
1061  getStream() << CompressedContents;
1062}
1063
1064void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1065                                       uint64_t Flags, uint64_t Address,
1066                                       uint64_t Offset, uint64_t Size,
1067                                       uint32_t Link, uint32_t Info,
1068                                       uint64_t Alignment,
1069                                       uint64_t EntrySize) {
1070  write32(Name);        // sh_name: index into string table
1071  write32(Type);        // sh_type
1072  WriteWord(Flags);     // sh_flags
1073  WriteWord(Address);   // sh_addr
1074  WriteWord(Offset);    // sh_offset
1075  WriteWord(Size);      // sh_size
1076  write32(Link);        // sh_link
1077  write32(Info);        // sh_info
1078  WriteWord(Alignment); // sh_addralign
1079  WriteWord(EntrySize); // sh_entsize
1080}
1081
1082void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
1083                                       const MCSectionELF &Sec) {
1084  std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
1085
1086  // We record relocations by pushing to the end of a vector. Reverse the vector
1087  // to get the relocations in the order they were created.
1088  // In most cases that is not important, but it can be for special sections
1089  // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
1090  std::reverse(Relocs.begin(), Relocs.end());
1091
1092  // Sort the relocation entries. MIPS needs this.
1093  TargetObjectWriter->sortRelocs(Asm, Relocs);
1094
1095  for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1096    const ELFRelocationEntry &Entry = Relocs[e - i - 1];
1097    unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
1098
1099    if (is64Bit()) {
1100      write(Entry.Offset);
1101      if (TargetObjectWriter->isN64()) {
1102        write(uint32_t(Index));
1103
1104        write(TargetObjectWriter->getRSsym(Entry.Type));
1105        write(TargetObjectWriter->getRType3(Entry.Type));
1106        write(TargetObjectWriter->getRType2(Entry.Type));
1107        write(TargetObjectWriter->getRType(Entry.Type));
1108      } else {
1109        struct ELF::Elf64_Rela ERE64;
1110        ERE64.setSymbolAndType(Index, Entry.Type);
1111        write(ERE64.r_info);
1112      }
1113      if (hasRelocationAddend())
1114        write(Entry.Addend);
1115    } else {
1116      write(uint32_t(Entry.Offset));
1117
1118      struct ELF::Elf32_Rela ERE32;
1119      ERE32.setSymbolAndType(Index, Entry.Type);
1120      write(ERE32.r_info);
1121
1122      if (hasRelocationAddend())
1123        write(uint32_t(Entry.Addend));
1124    }
1125  }
1126}
1127
1128const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
1129  const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
1130  getStream() << StrTabBuilder.data();
1131  return StrtabSection;
1132}
1133
1134void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1135                                   uint32_t GroupSymbolIndex, uint64_t Offset,
1136                                   uint64_t Size, const MCSectionELF &Section) {
1137  uint64_t sh_link = 0;
1138  uint64_t sh_info = 0;
1139
1140  switch(Section.getType()) {
1141  default:
1142    // Nothing to do.
1143    break;
1144
1145  case ELF::SHT_DYNAMIC:
1146    llvm_unreachable("SHT_DYNAMIC in a relocatable object");
1147
1148  case ELF::SHT_REL:
1149  case ELF::SHT_RELA: {
1150    sh_link = SymbolTableIndex;
1151    assert(sh_link && ".symtab not found");
1152    const MCSectionELF *InfoSection = Section.getAssociatedSection();
1153    sh_info = SectionIndexMap.lookup(InfoSection);
1154    break;
1155  }
1156
1157  case ELF::SHT_SYMTAB:
1158  case ELF::SHT_DYNSYM:
1159    sh_link = StringTableIndex;
1160    sh_info = LastLocalSymbolIndex;
1161    break;
1162
1163  case ELF::SHT_SYMTAB_SHNDX:
1164    sh_link = SymbolTableIndex;
1165    break;
1166
1167  case ELF::SHT_GROUP:
1168    sh_link = SymbolTableIndex;
1169    sh_info = GroupSymbolIndex;
1170    break;
1171  }
1172
1173  if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1174      Section.getType() == ELF::SHT_ARM_EXIDX)
1175    sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1176
1177  WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
1178                   Section.getType(), Section.getFlags(), 0, Offset, Size,
1179                   sh_link, sh_info, Section.getAlignment(),
1180                   Section.getEntrySize());
1181}
1182
1183void ELFObjectWriter::writeSectionHeader(
1184    const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1185    const SectionOffsetsTy &SectionOffsets) {
1186  const unsigned NumSections = SectionTable.size();
1187
1188  // Null section first.
1189  uint64_t FirstSectionSize =
1190      (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1191  WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1192
1193  for (const MCSectionELF *Section : SectionTable) {
1194    uint32_t GroupSymbolIndex;
1195    unsigned Type = Section->getType();
1196    if (Type != ELF::SHT_GROUP)
1197      GroupSymbolIndex = 0;
1198    else
1199      GroupSymbolIndex = Section->getGroup()->getIndex();
1200
1201    const std::pair<uint64_t, uint64_t> &Offsets =
1202        SectionOffsets.find(Section)->second;
1203    uint64_t Size;
1204    if (Type == ELF::SHT_NOBITS)
1205      Size = Layout.getSectionAddressSize(Section);
1206    else
1207      Size = Offsets.second - Offsets.first;
1208
1209    writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1210                 *Section);
1211  }
1212}
1213
1214void ELFObjectWriter::writeObject(MCAssembler &Asm,
1215                                  const MCAsmLayout &Layout) {
1216  MCContext &Ctx = Asm.getContext();
1217  MCSectionELF *StrtabSection =
1218      Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1219  StringTableIndex = addToSectionTable(StrtabSection);
1220
1221  RevGroupMapTy RevGroupMap;
1222  SectionIndexMapTy SectionIndexMap;
1223
1224  std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1225
1226  // Write out the ELF header ...
1227  writeHeader(Asm);
1228
1229  // ... then the sections ...
1230  SectionOffsetsTy SectionOffsets;
1231  std::vector<MCSectionELF *> Groups;
1232  std::vector<MCSectionELF *> Relocations;
1233  for (MCSection &Sec : Asm) {
1234    MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1235
1236    align(Section.getAlignment());
1237
1238    // Remember the offset into the file for this section.
1239    uint64_t SecStart = getStream().tell();
1240
1241    const MCSymbolELF *SignatureSymbol = Section.getGroup();
1242    writeSectionData(Asm, Section, Layout);
1243
1244    uint64_t SecEnd = getStream().tell();
1245    SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1246
1247    MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1248
1249    if (SignatureSymbol) {
1250      Asm.registerSymbol(*SignatureSymbol);
1251      unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1252      if (!GroupIdx) {
1253        MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
1254        GroupIdx = addToSectionTable(Group);
1255        Group->setAlignment(4);
1256        Groups.push_back(Group);
1257      }
1258      std::vector<const MCSectionELF *> &Members =
1259          GroupMembers[SignatureSymbol];
1260      Members.push_back(&Section);
1261      if (RelSection)
1262        Members.push_back(RelSection);
1263    }
1264
1265    SectionIndexMap[&Section] = addToSectionTable(&Section);
1266    if (RelSection) {
1267      SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1268      Relocations.push_back(RelSection);
1269    }
1270  }
1271
1272  for (MCSectionELF *Group : Groups) {
1273    align(Group->getAlignment());
1274
1275    // Remember the offset into the file for this section.
1276    uint64_t SecStart = getStream().tell();
1277
1278    const MCSymbol *SignatureSymbol = Group->getGroup();
1279    assert(SignatureSymbol);
1280    write(uint32_t(ELF::GRP_COMDAT));
1281    for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1282      uint32_t SecIndex = SectionIndexMap.lookup(Member);
1283      write(SecIndex);
1284    }
1285
1286    uint64_t SecEnd = getStream().tell();
1287    SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1288  }
1289
1290  // Compute symbol table information.
1291  computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets);
1292
1293  for (MCSectionELF *RelSection : Relocations) {
1294    align(RelSection->getAlignment());
1295
1296    // Remember the offset into the file for this section.
1297    uint64_t SecStart = getStream().tell();
1298
1299    writeRelocations(Asm, *RelSection->getAssociatedSection());
1300
1301    uint64_t SecEnd = getStream().tell();
1302    SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1303  }
1304
1305  {
1306    uint64_t SecStart = getStream().tell();
1307    const MCSectionELF *Sec = createStringTable(Ctx);
1308    uint64_t SecEnd = getStream().tell();
1309    SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
1310  }
1311
1312  uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1313  align(NaturalAlignment);
1314
1315  const uint64_t SectionHeaderOffset = getStream().tell();
1316
1317  // ... then the section header table ...
1318  writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1319
1320  uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
1321                             ? (uint16_t)ELF::SHN_UNDEF
1322                             : SectionTable.size() + 1;
1323  if (sys::IsLittleEndianHost != IsLittleEndian)
1324    sys::swapByteOrder(NumSections);
1325  unsigned NumSectionsOffset;
1326
1327  if (is64Bit()) {
1328    uint64_t Val = SectionHeaderOffset;
1329    if (sys::IsLittleEndianHost != IsLittleEndian)
1330      sys::swapByteOrder(Val);
1331    getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1332                       offsetof(ELF::Elf64_Ehdr, e_shoff));
1333    NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1334  } else {
1335    uint32_t Val = SectionHeaderOffset;
1336    if (sys::IsLittleEndianHost != IsLittleEndian)
1337      sys::swapByteOrder(Val);
1338    getStream().pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1339                       offsetof(ELF::Elf32_Ehdr, e_shoff));
1340    NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1341  }
1342  getStream().pwrite(reinterpret_cast<char *>(&NumSections),
1343                     sizeof(NumSections), NumSectionsOffset);
1344}
1345
1346bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1347    const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1348    bool InSet, bool IsPCRel) const {
1349  const auto &SymA = cast<MCSymbolELF>(SA);
1350  if (IsPCRel) {
1351    assert(!InSet);
1352    if (::isWeak(SymA))
1353      return false;
1354  }
1355  return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1356                                                                InSet, IsPCRel);
1357}
1358
1359bool ELFObjectWriter::isWeak(const MCSymbol &S) const {
1360  const auto &Sym = cast<MCSymbolELF>(S);
1361  if (::isWeak(Sym))
1362    return true;
1363
1364  // It is invalid to replace a reference to a global in a comdat
1365  // with a reference to a local since out of comdat references
1366  // to a local are forbidden.
1367  // We could try to return false for more cases, like the reference
1368  // being in the same comdat or Sym being an alias to another global,
1369  // but it is not clear if it is worth the effort.
1370  if (Sym.getBinding() != ELF::STB_GLOBAL)
1371    return false;
1372
1373  if (!Sym.isInSection())
1374    return false;
1375
1376  const auto &Sec = cast<MCSectionELF>(Sym.getSection());
1377  return Sec.getGroup();
1378}
1379
1380MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1381                                            raw_pwrite_stream &OS,
1382                                            bool IsLittleEndian) {
1383  return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
1384}
1385