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