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