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