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