LCOV - code coverage report
Current view: top level - lib/MC - ELFObjectWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 514 566 90.8 %
Date: 2018-10-20 13:21:21 Functions: 35 43 81.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements ELF object file writer information.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/ADT/ArrayRef.h"
      15             : #include "llvm/ADT/DenseMap.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/ADT/SmallString.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/StringRef.h"
      20             : #include "llvm/ADT/Twine.h"
      21             : #include "llvm/BinaryFormat/ELF.h"
      22             : #include "llvm/MC/MCAsmBackend.h"
      23             : #include "llvm/MC/MCAsmInfo.h"
      24             : #include "llvm/MC/MCAsmLayout.h"
      25             : #include "llvm/MC/MCAssembler.h"
      26             : #include "llvm/MC/MCContext.h"
      27             : #include "llvm/MC/MCELFObjectWriter.h"
      28             : #include "llvm/MC/MCExpr.h"
      29             : #include "llvm/MC/MCFixup.h"
      30             : #include "llvm/MC/MCFixupKindInfo.h"
      31             : #include "llvm/MC/MCFragment.h"
      32             : #include "llvm/MC/MCObjectFileInfo.h"
      33             : #include "llvm/MC/MCObjectWriter.h"
      34             : #include "llvm/MC/MCSection.h"
      35             : #include "llvm/MC/MCSectionELF.h"
      36             : #include "llvm/MC/MCSymbol.h"
      37             : #include "llvm/MC/MCSymbolELF.h"
      38             : #include "llvm/MC/MCValue.h"
      39             : #include "llvm/MC/StringTableBuilder.h"
      40             : #include "llvm/Support/Allocator.h"
      41             : #include "llvm/Support/Casting.h"
      42             : #include "llvm/Support/Compression.h"
      43             : #include "llvm/Support/Endian.h"
      44             : #include "llvm/Support/Error.h"
      45             : #include "llvm/Support/ErrorHandling.h"
      46             : #include "llvm/Support/Host.h"
      47             : #include "llvm/Support/LEB128.h"
      48             : #include "llvm/Support/MathExtras.h"
      49             : #include "llvm/Support/SMLoc.h"
      50             : #include "llvm/Support/StringSaver.h"
      51             : #include "llvm/Support/SwapByteOrder.h"
      52             : #include "llvm/Support/raw_ostream.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         566 : bool isDwoSection(const MCSectionELF &Sec) {
      76         566 :   return Sec.getSectionName().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;
     104             :   support::endian::Writer W;
     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             :     uint32_t SectionIndex;
     120             :     StringRef Name;
     121             : 
     122             :     // Support lexicographic sorting.
     123     5430259 :     bool operator<(const ELFSymbolData &RHS) const {
     124     5430259 :       unsigned LHSType = Symbol->getType();
     125     5430260 :       unsigned RHSType = RHS.Symbol->getType();
     126     5430260 :       if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
     127             :         return false;
     128     5214061 :       if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
     129             :         return true;
     130     4895848 :       if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
     131      873569 :         return SectionIndex < RHS.SectionIndex;
     132     4022279 :       return Name < RHS.Name;
     133             :     }
     134             :   };
     135             : 
     136             :   /// @}
     137             :   /// @name Symbol Table Data
     138             :   /// @{
     139             : 
     140             :   StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
     141             : 
     142             :   /// @}
     143             : 
     144             :   // This holds the symbol table index of the last local symbol.
     145             :   unsigned LastLocalSymbolIndex;
     146             :   // This holds the .strtab section index.
     147             :   unsigned StringTableIndex;
     148             :   // This holds the .symtab section index.
     149             :   unsigned SymbolTableIndex;
     150             : 
     151             :   // Sections in the order they are to be output in the section table.
     152             :   std::vector<const MCSectionELF *> SectionTable;
     153             :   unsigned addToSectionTable(const MCSectionELF *Sec);
     154             : 
     155             :   // TargetObjectWriter wrappers.
     156             :   bool is64Bit() const;
     157             :   bool hasRelocationAddend() const;
     158             : 
     159             :   void align(unsigned Alignment);
     160             : 
     161             :   bool maybeWriteCompression(uint64_t Size,
     162             :                              SmallVectorImpl<char> &CompressedContents,
     163             :                              bool ZLibStyle, unsigned Alignment);
     164             : 
     165             : public:
     166             :   ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
     167             :             bool IsLittleEndian, DwoMode Mode)
     168       11149 :       : OWriter(OWriter),
     169       22298 :         W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
     170             : 
     171     6788605 :   void WriteWord(uint64_t Word) {
     172    13577210 :     if (is64Bit())
     173     6601939 :       W.write<uint64_t>(Word);
     174             :     else
     175      186666 :       W.write<uint32_t>(Word);
     176     6788598 :   }
     177             : 
     178             :   template <typename T> void write(T Val) {
     179     5068455 :     W.write(Val);
     180             :   }
     181             : 
     182             :   void writeHeader(const MCAssembler &Asm);
     183             : 
     184             :   void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
     185             :                    ELFSymbolData &MSD, const MCAsmLayout &Layout);
     186             : 
     187             :   // Start and end offset of each section
     188             :   using SectionOffsetsTy =
     189             :       std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
     190             : 
     191             :   // Map from a signature symbol to the group section index
     192             :   using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
     193             : 
     194             :   /// Compute the symbol table data
     195             :   ///
     196             :   /// \param Asm - The assembler.
     197             :   /// \param SectionIndexMap - Maps a section to its index.
     198             :   /// \param RevGroupMap - Maps a signature symbol to the group section.
     199             :   void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
     200             :                           const SectionIndexMapTy &SectionIndexMap,
     201             :                           const RevGroupMapTy &RevGroupMap,
     202             :                           SectionOffsetsTy &SectionOffsets);
     203             : 
     204             :   void writeAddrsigSection();
     205             : 
     206             :   MCSectionELF *createRelocationSection(MCContext &Ctx,
     207             :                                         const MCSectionELF &Sec);
     208             : 
     209             :   const MCSectionELF *createStringTable(MCContext &Ctx);
     210             : 
     211             :   void writeSectionHeader(const MCAsmLayout &Layout,
     212             :                           const SectionIndexMapTy &SectionIndexMap,
     213             :                           const SectionOffsetsTy &SectionOffsets);
     214             : 
     215             :   void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
     216             :                         const MCAsmLayout &Layout);
     217             : 
     218             :   void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
     219             :                         uint64_t Address, uint64_t Offset, uint64_t Size,
     220             :                         uint32_t Link, uint32_t Info, uint64_t Alignment,
     221             :                         uint64_t EntrySize);
     222             : 
     223             :   void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
     224             : 
     225             :   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
     226             :   void writeSection(const SectionIndexMapTy &SectionIndexMap,
     227             :                     uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
     228             :                     const MCSectionELF &Section);
     229             : };
     230             : 
     231             : class ELFObjectWriter : public MCObjectWriter {
     232             :   /// The target specific ELF writer instance.
     233             :   std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
     234             : 
     235             :   DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
     236             : 
     237             :   DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
     238             : 
     239             :   bool EmitAddrsigSection = false;
     240             :   std::vector<const MCSymbol *> AddrsigSyms;
     241             : 
     242             :   bool hasRelocationAddend() const;
     243             : 
     244             :   bool shouldRelocateWithSymbol(const MCAssembler &Asm,
     245             :                                 const MCSymbolRefExpr *RefA,
     246             :                                 const MCSymbolELF *Sym, uint64_t C,
     247             :                                 unsigned Type) const;
     248             : 
     249             : public:
     250       30760 :   ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
     251       61520 :       : TargetObjectWriter(std::move(MOTW)) {}
     252             : 
     253        7496 :   void reset() override {
     254        7496 :     Relocations.clear();
     255        7497 :     Renames.clear();
     256             :     MCObjectWriter::reset();
     257        7497 :   }
     258             : 
     259             :   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
     260             :                                               const MCSymbol &SymA,
     261             :                                               const MCFragment &FB, bool InSet,
     262             :                                               bool IsPCRel) const override;
     263             : 
     264     5068327 :   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
     265             :                                const MCSectionELF *From,
     266             :                                const MCSectionELF *To) {
     267     5068327 :     return true;
     268             :   }
     269             : 
     270             :   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
     271             :                         const MCFragment *Fragment, const MCFixup &Fixup,
     272             :                         MCValue Target, uint64_t &FixedValue) override;
     273             : 
     274             :   void executePostLayoutBinding(MCAssembler &Asm,
     275             :                                 const MCAsmLayout &Layout) override;
     276             : 
     277        6190 :   void emitAddrsigSection() override { EmitAddrsigSection = true; }
     278      237776 :   void addAddrsigSymbol(const MCSymbol *Sym) override {
     279      237776 :     AddrsigSyms.push_back(Sym);
     280      237776 :   }
     281             : 
     282             :   friend struct ELFWriter;
     283             : };
     284             : 
     285           0 : class ELFSingleObjectWriter : public ELFObjectWriter {
     286             :   raw_pwrite_stream &OS;
     287             :   bool IsLittleEndian;
     288             : 
     289             : public:
     290       30746 :   ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
     291             :                         raw_pwrite_stream &OS, bool IsLittleEndian)
     292       30746 :       : ELFObjectWriter(std::move(MOTW)), OS(OS),
     293       61492 :         IsLittleEndian(IsLittleEndian) {}
     294             : 
     295       11121 :   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
     296       22240 :     return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
     297       11121 :         .writeObject(Asm, Layout);
     298             :   }
     299             : 
     300             :   friend struct ELFWriter;
     301             : };
     302             : 
     303           0 : class ELFDwoObjectWriter : public ELFObjectWriter {
     304             :   raw_pwrite_stream &OS, &DwoOS;
     305             :   bool IsLittleEndian;
     306             : 
     307             : public:
     308          14 :   ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
     309             :                      raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
     310             :                      bool IsLittleEndian)
     311          14 :       : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
     312          28 :         IsLittleEndian(IsLittleEndian) {}
     313             : 
     314          92 :   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
     315             :                                const MCSectionELF *From,
     316             :                                const MCSectionELF *To) override {
     317          92 :     if (isDwoSection(*From)) {
     318           2 :       Ctx.reportError(Loc, "A dwo section may not contain relocations");
     319           2 :       return false;
     320             :     }
     321          90 :     if (To && isDwoSection(*To)) {
     322           1 :       Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
     323           1 :       return false;
     324             :     }
     325             :     return true;
     326             :   }
     327             : 
     328          14 :   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
     329          28 :     uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
     330          14 :                         .writeObject(Asm, Layout);
     331          28 :     Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
     332          14 :                 .writeObject(Asm, Layout);
     333          14 :     return Size;
     334             :   }
     335             : };
     336             : 
     337             : } // end anonymous namespace
     338             : 
     339           0 : void ELFWriter::align(unsigned Alignment) {
     340           0 :   uint64_t Padding = OffsetToAlignment(W.OS.tell(), Alignment);
     341           0 :   W.OS.write_zeros(Padding);
     342           0 : }
     343             : 
     344     1114717 : unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
     345     1114717 :   SectionTable.push_back(Sec);
     346     1114717 :   StrTabBuilder.add(Sec->getSectionName());
     347     2229434 :   return SectionTable.size();
     348             : }
     349             : 
     350             : void SymbolTableWriter::createSymtabShndx() {
     351        4480 :   if (!ShndxIndexes.empty())
     352             :     return;
     353             : 
     354           6 :   ShndxIndexes.resize(NumWritten);
     355             : }
     356             : 
     357           0 : template <typename T> void SymbolTableWriter::write(T Value) {
     358      713827 :   EWriter.write(Value);
     359           0 : }
     360           0 : 
     361           0 : SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
     362           0 :     : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
     363           0 : 
     364           0 : void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
     365           0 :                                     uint64_t size, uint8_t other,
     366             :                                     uint32_t shndx, bool Reserved) {
     367       11133 :   bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
     368       11133 : 
     369             :   if (LargeIndex)
     370      713827 :     createSymtabShndx();
     371             : 
     372             :   if (!ShndxIndexes.empty()) {
     373      713827 :     if (LargeIndex)
     374             :       ShndxIndexes.push_back(shndx);
     375      713827 :     else
     376             :       ShndxIndexes.push_back(0);
     377             :   }
     378      713827 : 
     379       26477 :   uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
     380        4480 : 
     381             :   if (Is64Bit) {
     382       21997 :     write(name);  // st_name
     383             :     write(info);  // st_info
     384             :     write(other); // st_other
     385      713827 :     write(Index); // st_shndx
     386             :     write(value); // st_value
     387      713827 :     write(size);  // st_size
     388      653190 :   } else {
     389             :     write(name);            // st_name
     390             :     write(uint32_t(value)); // st_value
     391      653190 :     write(uint32_t(size));  // st_size
     392      653190 :     write(info);            // st_info
     393      653190 :     write(other);           // st_other
     394             :     write(Index);           // st_shndx
     395       60637 :   }
     396       60637 : 
     397       60637 :   ++NumWritten;
     398             : }
     399             : 
     400       60637 : bool ELFWriter::is64Bit() const {
     401             :   return OWriter.TargetObjectWriter->is64Bit();
     402             : }
     403      713827 : 
     404      713827 : bool ELFWriter::hasRelocationAddend() const {
     405             :   return OWriter.hasRelocationAddend();
     406           0 : }
     407           0 : 
     408             : // Emit the ELF header.
     409             : void ELFWriter::writeHeader(const MCAssembler &Asm) {
     410           0 :   // ELF Header
     411           0 :   // ----------
     412             :   //
     413             :   // Note
     414             :   // ----
     415       11149 :   // emitWord method behaves differently for ELF32 and ELF64, writing
     416             :   // 4 bytes in the former and 8 in the latter.
     417             : 
     418             :   W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
     419             : 
     420             :   W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
     421             : 
     422             :   // e_ident[EI_DATA]
     423             :   W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
     424       11149 :                                            : ELF::ELFDATA2MSB);
     425             : 
     426       22298 :   W.OS << char(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
     427             :   // e_ident[EI_OSABI]
     428             :   W.OS << char(OWriter.TargetObjectWriter->getOSABI());
     429       11149 :   W.OS << char(0);                  // e_ident[EI_ABIVERSION]
     430       11149 : 
     431             :   W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
     432       11149 : 
     433             :   W.write<uint16_t>(ELF::ET_REL);             // e_type
     434       22296 : 
     435       11149 :   W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
     436             : 
     437       11149 :   W.write<uint32_t>(ELF::EV_CURRENT);         // e_version
     438             :   WriteWord(0);                    // e_entry, no entry point in .o file
     439             :   WriteWord(0);                    // e_phoff, no program header for .o
     440             :   WriteWord(0);                     // e_shoff = sec hdr table off in bytes
     441       22298 : 
     442             :   // e_flags = whatever the target wants
     443       11149 :   W.write<uint32_t>(Asm.getELFHeaderEFlags());
     444       11149 : 
     445       11148 :   // e_ehsize = ELF header size
     446       11149 :   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
     447             :                               : sizeof(ELF::Elf32_Ehdr));
     448             : 
     449       11149 :   W.write<uint16_t>(0);                  // e_phentsize = prog header entry size
     450             :   W.write<uint16_t>(0);                  // e_phnum = # prog header entries = 0
     451             : 
     452       23542 :   // e_shentsize = Section header entry size
     453             :   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
     454             :                               : sizeof(ELF::Elf32_Shdr));
     455             : 
     456             :   // e_shnum     = # of section header ents
     457             :   W.write<uint16_t>(0);
     458             : 
     459       23544 :   // e_shstrndx  = Section # of '.shstrtab'
     460             :   assert(StringTableIndex < ELF::SHN_LORESERVE);
     461             :   W.write<uint16_t>(StringTableIndex);
     462             : }
     463             : 
     464             : uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
     465             :                                 const MCAsmLayout &Layout) {
     466             :   if (Sym.isCommon() && Sym.isExternal())
     467       11149 :     return Sym.getCommonAlignment();
     468       11149 : 
     469             :   uint64_t Res;
     470      695222 :   if (!Layout.getSymbolOffset(Sym, Res))
     471             :     return 0;
     472      695222 : 
     473         593 :   if (Layout.getAssembler().isThumbFunc(&Sym))
     474             :     Res |= 1;
     475             : 
     476      694629 :   return Res;
     477             : }
     478             : 
     479      609296 : static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
     480         589 :   uint8_t Type = newType;
     481             : 
     482      609296 :   // Propagation rules:
     483             :   // IFUNC > FUNC > OBJECT > NOTYPE
     484             :   // TLS_OBJECT > OBJECT > NOTYPE
     485      695001 :   //
     486             :   // dont let the new type degrade the old type
     487             :   switch (origType) {
     488             :   default:
     489             :     break;
     490             :   case ELF::STT_GNU_IFUNC:
     491             :     if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
     492             :         Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
     493      695001 :       Type = ELF::STT_GNU_IFUNC;
     494             :     break;
     495             :   case ELF::STT_FUNC:
     496         333 :     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
     497         666 :         Type == ELF::STT_TLS)
     498         333 :       Type = ELF::STT_FUNC;
     499             :     break;
     500             :   case ELF::STT_OBJECT:
     501      213016 :     if (Type == ELF::STT_NOTYPE)
     502      426032 :       Type = ELF::STT_OBJECT;
     503      213016 :     break;
     504             :   case ELF::STT_TLS:
     505             :     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
     506       82843 :         Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
     507       82843 :       Type = ELF::STT_TLS;
     508             :     break;
     509             :   }
     510         401 : 
     511         802 :   return Type;
     512         401 : }
     513             : 
     514             : void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
     515             :                             ELFSymbolData &MSD, const MCAsmLayout &Layout) {
     516             :   const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
     517      695001 :   const MCSymbolELF *Base =
     518             :       cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
     519             : 
     520           0 :   // This has to be in sync with when computeSymbolTable uses SHN_ABS or
     521             :   // SHN_COMMON.
     522           0 :   bool IsReserved = !Base || Symbol.isCommon();
     523             : 
     524           0 :   // Binding and Type share the same byte as upper and lower nibbles
     525             :   uint8_t Binding = Symbol.getBinding();
     526             :   uint8_t Type = Symbol.getType();
     527             :   if (Base) {
     528           0 :     Type = mergeTypeForSet(Type, Base->getType());
     529             :   }
     530             :   uint8_t Info = (Binding << 4) | Type;
     531           0 : 
     532           0 :   // Other and Visibility share the same byte with Visibility using the lower
     533           0 :   // 2 bits
     534           0 :   uint8_t Visibility = Symbol.getVisibility();
     535             :   uint8_t Other = Symbol.getOther() | Visibility;
     536           0 : 
     537             :   uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
     538             :   uint64_t Size = 0;
     539             : 
     540           0 :   const MCExpr *ESize = MSD.Symbol->getSize();
     541           0 :   if (!ESize && Base)
     542             :     ESize = Base->getSize();
     543           0 : 
     544             :   if (ESize) {
     545             :     int64_t Res;
     546           0 :     if (!ESize->evaluateKnownAbsolute(Res, Layout))
     547           0 :       report_fatal_error("Size expression must be absolute.");
     548           0 :     Size = Res;
     549             :   }
     550           0 : 
     551             :   // Write out the symbol table entry
     552           0 :   Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
     553           0 :                      IsReserved);
     554           0 : }
     555             : 
     556             : // True if the assembler knows nothing about the final value of the symbol.
     557             : // This doesn't cover the comdat issues, since in those cases the assembler
     558           0 : // can at least know that all symbols in the section will move together.
     559             : static bool isWeak(const MCSymbolELF &Sym) {
     560           0 :   if (Sym.getType() == ELF::STT_GNU_IFUNC)
     561             :     return true;
     562             : 
     563             :   switch (Sym.getBinding()) {
     564             :   default:
     565     4062112 :     llvm_unreachable("Unknown binding");
     566     4062112 :   case ELF::STB_LOCAL:
     567             :     return false;
     568             :   case ELF::STB_GLOBAL:
     569     4062080 :     return false;
     570           0 :   case ELF::STB_WEAK:
     571           0 :   case ELF::STB_GNU_UNIQUE:
     572             :     return true;
     573             :   }
     574             : }
     575             : 
     576     1249370 : bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
     577             :                            bool Used, bool Renamed) {
     578     1249370 :   if (Symbol.isVariable()) {
     579             :     const MCExpr *Expr = Symbol.getVariableValue();
     580             :     if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
     581             :       if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
     582     8546251 :         return false;
     583             :     }
     584     8546251 :   }
     585             : 
     586             :   if (Used)
     587         686 :     return true;
     588             : 
     589             :   if (Renamed)
     590             :     return false;
     591             : 
     592     8546237 :   if (Symbol.isVariable() && Symbol.isUndefined()) {
     593             :     // FIXME: this is here just to diagnose the case of a var = commmon_sym.
     594             :     Layout.getBaseSymbol(Symbol);
     595     7921511 :     return false;
     596             :   }
     597             : 
     598     7921499 :   if (Symbol.isUndefined() && !Symbol.isBindingSet())
     599             :     return false;
     600          15 : 
     601          15 :   if (Symbol.isTemporary())
     602             :     return false;
     603             : 
     604     7921484 :   if (Symbol.getType() == ELF::STT_SECTION)
     605             :     return false;
     606             : 
     607     7921436 :   return true;
     608             : }
     609             : 
     610      528180 : void ELFWriter::computeSymbolTable(
     611      457682 :     MCAssembler &Asm, const MCAsmLayout &Layout,
     612             :     const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
     613             :     SectionOffsetsTy &SectionOffsets) {
     614             :   MCContext &Ctx = Asm.getContext();
     615             :   SymbolTableWriter Writer(*this, is64Bit());
     616       11133 : 
     617             :   // Symbol table
     618             :   unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
     619             :   MCSectionELF *SymtabSection =
     620       11133 :       Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
     621       11133 :   SymtabSection->setAlignment(is64Bit() ? 8 : 4);
     622             :   SymbolTableIndex = addToSectionTable(SymtabSection);
     623             : 
     624       11133 :   align(SymtabSection->getAlignment());
     625             :   uint64_t SecStart = W.OS.tell();
     626       11133 : 
     627       22266 :   // The first entry is the undefined symbol entry.
     628       11133 :   Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
     629             : 
     630       11133 :   std::vector<ELFSymbolData> LocalSymbolData;
     631       11133 :   std::vector<ELFSymbolData> ExternalSymbolData;
     632             : 
     633             :   // Add the data for the symbols.
     634       11133 :   bool HasLargeSectionIndex = false;
     635             :   for (const MCSymbol &S : Asm.symbols()) {
     636             :     const auto &Symbol = cast<MCSymbolELF>(S);
     637             :     bool Used = Symbol.isUsedInReloc();
     638             :     bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
     639             :     bool isSignature = Symbol.isSignature();
     640             : 
     641     8557384 :     if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
     642             :                     OWriter.Renames.count(&Symbol)))
     643             :       continue;
     644     8546251 : 
     645     8546248 :     if (Symbol.isTemporary() && Symbol.isUndefined()) {
     646             :       Ctx.reportError(SMLoc(), "Undefined temporary symbol");
     647    16467776 :       continue;
     648    17092503 :     }
     649     7851029 : 
     650             :     ELFSymbolData MSD;
     651      695224 :     MSD.Symbol = cast<MCSymbolELF>(&Symbol);
     652           2 : 
     653           2 :     bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
     654             :     assert(Local || !Symbol.isTemporary());
     655             : 
     656             :     if (Symbol.isAbsolute()) {
     657      695222 :       MSD.SectionIndex = ELF::SHN_ABS;
     658             :     } else if (Symbol.isCommon()) {
     659      695222 :       assert(!Local);
     660             :       MSD.SectionIndex = ELF::SHN_COMMON;
     661             :     } else if (Symbol.isUndefined()) {
     662      695222 :       if (isSignature && !Used) {
     663         221 :         MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
     664      695001 :         if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
     665             :           HasLargeSectionIndex = true;
     666         593 :       } else {
     667      694408 :         MSD.SectionIndex = ELF::SHN_UNDEF;
     668       85331 :       }
     669         207 :     } else {
     670         207 :       const MCSectionELF &Section =
     671             :           static_cast<const MCSectionELF &>(Symbol.getSection());
     672             :       if (Mode == NonDwoOnly && isDwoSection(Section))
     673       85124 :         continue;
     674             :       MSD.SectionIndex = SectionIndexMap.lookup(&Section);
     675             :       assert(MSD.SectionIndex && "Invalid section index!");
     676             :       if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
     677             :         HasLargeSectionIndex = true;
     678      609077 :     }
     679             : 
     680      609076 :     StringRef Name = Symbol.getName();
     681             : 
     682      609076 :     // Sections have their own string table
     683             :     if (Symbol.getType() != ELF::STT_SECTION) {
     684             :       MSD.Name = Name;
     685             :       StrTabBuilder.add(Name);
     686             :     }
     687             : 
     688             :     if (Local)
     689      695221 :       LocalSymbolData.push_back(MSD);
     690      459715 :     else
     691      459715 :       ExternalSymbolData.push_back(MSD);
     692             :   }
     693             : 
     694      695222 :   // This holds the .symtab_shndx section index.
     695      359280 :   unsigned SymtabShndxSectionIndex = 0;
     696             : 
     697      335942 :   if (HasLargeSectionIndex) {
     698             :     MCSectionELF *SymtabShndxSection =
     699             :         Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
     700             :     SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
     701             :     SymtabShndxSection->setAlignment(4);
     702             :   }
     703       11133 : 
     704             :   ArrayRef<std::string> FileNames = Asm.getFileNames();
     705           6 :   for (const std::string &Name : FileNames)
     706           6 :     StrTabBuilder.add(Name);
     707             : 
     708             :   StrTabBuilder.finalize();
     709             : 
     710             :   // File symbols are emitted first and handled separately from normal symbols,
     711       18605 :   // i.e. a non-STT_FILE symbol with the same name may appear.
     712       14944 :   for (const std::string &Name : FileNames)
     713             :     Writer.writeSymbol(StrTabBuilder.getOffset(Name),
     714       11133 :                        ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
     715             :                        ELF::SHN_ABS, true);
     716             : 
     717             :   // Symbols are required to be in lexicographic order.
     718       18605 :   array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
     719        7472 :   array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
     720             : 
     721             :   // Set the symbol indices. Local symbols must come before all other
     722             :   // symbols with non-local bindings.
     723             :   unsigned Index = FileNames.size() + 1;
     724             : 
     725             :   for (ELFSymbolData &MSD : LocalSymbolData) {
     726             :     unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
     727             :                                ? 0
     728             :                                : StrTabBuilder.getOffset(MSD.Name);
     729       11133 :     MSD.Symbol->setIndex(Index++);
     730             :     writeSymbol(Writer, StringIndex, MSD, Layout);
     731      370413 :   }
     732      359280 : 
     733             :   // Write the symbol table entries.
     734      359280 :   LastLocalSymbolIndex = Index;
     735      359280 : 
     736      359280 :   for (ELFSymbolData &MSD : ExternalSymbolData) {
     737             :     unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
     738             :     MSD.Symbol->setIndex(Index++);
     739             :     writeSymbol(Writer, StringIndex, MSD, Layout);
     740       11133 :     assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
     741             :   }
     742      347075 : 
     743      335942 :   uint64_t SecEnd = W.OS.tell();
     744      335942 :   SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
     745      335942 : 
     746             :   ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
     747             :   if (ShndxIndexes.empty()) {
     748             :     assert(SymtabShndxSectionIndex == 0);
     749       11133 :     return;
     750       11133 :   }
     751             :   assert(SymtabShndxSectionIndex != 0);
     752             : 
     753       11133 :   SecStart = W.OS.tell();
     754             :   const MCSectionELF *SymtabShndxSection =
     755             :       SectionTable[SymtabShndxSectionIndex - 1];
     756             :   for (uint32_t Index : ShndxIndexes)
     757             :     write(Index);
     758             :   SecEnd = W.OS.tell();
     759           6 :   SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
     760             : }
     761          12 : 
     762       58825 : void ELFWriter::writeAddrsigSection() {
     763             :   for (const MCSymbol *Sym : OWriter.AddrsigSyms)
     764           6 :     encodeULEB128(Sym->getIndex(), W.OS);
     765           6 : }
     766             : 
     767             : MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
     768           0 :                                                  const MCSectionELF &Sec) {
     769           0 :   if (OWriter.Relocations[&Sec].empty())
     770           0 :     return nullptr;
     771           0 : 
     772             :   const StringRef SectionName = Sec.getSectionName();
     773      694156 :   std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
     774             :   RelaSectionName += SectionName;
     775      694156 : 
     776             :   unsigned EntrySize;
     777             :   if (hasRelocationAddend())
     778             :     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
     779      360441 :   else
     780             :     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
     781             : 
     782             :   unsigned Flags = 0;
     783      356204 :   if (Sec.getFlags() & ELF::SHF_GROUP)
     784      173865 :     Flags = ELF::SHF_GROUP;
     785             : 
     786        4237 :   MCSectionELF *RelaSection = Ctx.createELFRelSection(
     787             :       RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
     788             :       Flags, EntrySize, Sec.getGroup(), &Sec);
     789      178102 :   RelaSection->setAlignment(is64Bit() ? 8 : 4);
     790             :   return RelaSection;
     791             : }
     792      182339 : 
     793             : // Include the debug info compression header.
     794             : bool ELFWriter::maybeWriteCompression(
     795      356204 :     uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
     796             :     unsigned Alignment) {
     797             :   if (ZLibStyle) {
     798             :     uint64_t HdrSize =
     799             :         is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
     800          26 :     if (Size <= HdrSize + CompressedContents.size())
     801             :       return false;
     802             :     // Platform specific header is followed by compressed data.
     803          26 :     if (is64Bit()) {
     804             :       // Write Elf64_Chdr header.
     805          24 :       write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
     806          24 :       write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
     807             :       write(static_cast<ELF::Elf64_Xword>(Size));
     808             :       write(static_cast<ELF::Elf64_Xword>(Alignment));
     809           5 :     } else {
     810             :       // Write Elf32_Chdr header otherwise.
     811             :       write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
     812             :       write(static_cast<ELF::Elf32_Word>(Size));
     813             :       write(static_cast<ELF::Elf32_Word>(Alignment));
     814           3 :     }
     815             :     return true;
     816             :   }
     817             : 
     818           2 :   // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
     819             :   // useful for consumers to preallocate a buffer to decompress into.
     820             :   const StringRef Magic = "ZLIB";
     821           5 :   if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
     822             :     return false;
     823             :   W.OS << Magic;
     824             :   support::endian::write(W.OS, Size, support::big);
     825             :   return true;
     826             : }
     827          28 : 
     828             : void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
     829           7 :                                  const MCAsmLayout &Layout) {
     830           7 :   MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
     831           7 :   StringRef SectionName = Section.getSectionName();
     832             : 
     833             :   auto &MC = Asm.getContext();
     834      694157 :   const auto &MAI = MC.getAsmInfo();
     835             : 
     836             :   // Compressing debug_frame requires handling alignment fragments which is
     837      694157 :   // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
     838             :   // for writing to arbitrary buffers) for little benefit.
     839      694157 :   bool CompressionEnabled =
     840      694157 :       MAI->compressDebugSections() != DebugCompressionType::None;
     841             :   if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
     842             :       SectionName == ".debug_frame") {
     843             :     Asm.writeSectionData(W.OS, &Section, Layout);
     844             :     return;
     845             :   }
     846      694157 : 
     847      694157 :   assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
     848             :           MAI->compressDebugSections() == DebugCompressionType::GNU) &&
     849      694131 :          "expected zlib or zlib-gnu style compression");
     850      694144 : 
     851             :   SmallVector<char, 128> UncompressedData;
     852             :   raw_svector_ostream VecOS(UncompressedData);
     853             :   Asm.writeSectionData(VecOS, &Section, Layout);
     854             : 
     855             :   SmallVector<char, 128> CompressedContents;
     856             :   if (Error E = zlib::compress(
     857             :           StringRef(UncompressedData.data(), UncompressedData.size()),
     858             :           CompressedContents)) {
     859          26 :     consumeError(std::move(E));
     860             :     W.OS << UncompressedData;
     861             :     return;
     862          26 :   }
     863          26 : 
     864          52 :   bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
     865           0 :   if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
     866           0 :                              ZlibStyle, Sec.getAlignment())) {
     867             :     W.OS << UncompressedData;
     868             :     return;
     869             :   }
     870          26 : 
     871          52 :   if (ZlibStyle)
     872             :     // Set the compressed flag. That is zlib style.
     873          14 :     Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
     874          14 :   else
     875             :     // Add "z" prefix to section name. This is zlib-gnu style.
     876             :     MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
     877          12 :   W.OS << CompressedContents;
     878             : }
     879           5 : 
     880             : void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
     881             :                                  uint64_t Address, uint64_t Offset,
     882          15 :                                  uint64_t Size, uint32_t Link, uint32_t Info,
     883          12 :                                  uint64_t Alignment, uint64_t EntrySize) {
     884             :   W.write<uint32_t>(Name);        // sh_name: index into string table
     885             :   W.write<uint32_t>(Type);        // sh_type
     886     1125860 :   WriteWord(Flags);     // sh_flags
     887             :   WriteWord(Address);   // sh_addr
     888             :   WriteWord(Offset);    // sh_offset
     889             :   WriteWord(Size);      // sh_size
     890     1125860 :   W.write<uint32_t>(Link);        // sh_link
     891     1125860 :   W.write<uint32_t>(Info);        // sh_info
     892     1125860 :   WriteWord(Alignment); // sh_addralign
     893     1125860 :   WriteWord(EntrySize); // sh_entsize
     894     1125860 : }
     895     1125860 : 
     896     1125860 : void ELFWriter::writeRelocations(const MCAssembler &Asm,
     897     1125860 :                                        const MCSectionELF &Sec) {
     898     1125860 :   std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
     899     1125860 : 
     900     1125860 :   // We record relocations by pushing to the end of a vector. Reverse the vector
     901             :   // to get the relocations in the order they were created.
     902      178102 :   // In most cases that is not important, but it can be for special sections
     903             :   // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
     904      178102 :   std::reverse(Relocs.begin(), Relocs.end());
     905             : 
     906             :   // Sort the relocation entries. MIPS needs this.
     907             :   OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
     908             : 
     909             :   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
     910      178102 :     const ELFRelocationEntry &Entry = Relocs[e - i - 1];
     911             :     unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
     912             : 
     913      356204 :     if (is64Bit()) {
     914             :       write(Entry.Offset);
     915     5424618 :       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
     916     5068414 :         write(uint32_t(Index));
     917     5068414 : 
     918             :         write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
     919    10136828 :         write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
     920     4271924 :         write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
     921     8543846 :         write(OWriter.TargetObjectWriter->getRType(Entry.Type));
     922             :       } else {
     923             :         struct ELF::Elf64_Rela ERE64;
     924       10346 :         ERE64.setSymbolAndType(Index, Entry.Type);
     925       10346 :         write(ERE64.r_info);
     926       10346 :       }
     927       10346 :       if (hasRelocationAddend())
     928             :         write(Entry.Addend);
     929             :     } else {
     930     4261577 :       write(uint32_t(Entry.Offset));
     931             : 
     932             :       struct ELF::Elf32_Rela ERE32;
     933     8543848 :       ERE32.setSymbolAndType(Index, Entry.Type);
     934     4271837 :       write(ERE32.r_info);
     935             : 
     936      796490 :       if (hasRelocationAddend())
     937             :         write(uint32_t(Entry.Addend));
     938             : 
     939      796490 :       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
     940             :         if (uint32_t RType =
     941             :                 OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
     942     1592978 :           write(uint32_t(Entry.Offset));
     943         637 : 
     944             :           ERE32.setSymbolAndType(0, RType);
     945     1592978 :           write(ERE32.r_info);
     946        1101 :           write(uint32_t(0));
     947        1101 :         }
     948           8 :         if (uint32_t RType =
     949             :                 OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
     950             :           write(uint32_t(Entry.Offset));
     951             : 
     952             :           ERE32.setSymbolAndType(0, RType);
     953             :           write(ERE32.r_info);
     954        1101 :           write(uint32_t(0));
     955        1101 :         }
     956           8 :       }
     957             :     }
     958             :   }
     959             : }
     960             : 
     961             : const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) {
     962             :   const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
     963             :   StrTabBuilder.write(W.OS);
     964             :   return StrtabSection;
     965      178102 : }
     966             : 
     967           0 : void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
     968       11147 :                              uint32_t GroupSymbolIndex, uint64_t Offset,
     969       11147 :                              uint64_t Size, const MCSectionELF &Section) {
     970           0 :   uint64_t sh_link = 0;
     971             :   uint64_t sh_info = 0;
     972             : 
     973     1114712 :   switch(Section.getType()) {
     974             :   default:
     975             :     // Nothing to do.
     976             :     break;
     977             : 
     978             :   case ELF::SHT_DYNAMIC:
     979     1114712 :     llvm_unreachable("SHT_DYNAMIC in a relocatable object");
     980             : 
     981             :   case ELF::SHT_REL:
     982             :   case ELF::SHT_RELA: {
     983             :     sh_link = SymbolTableIndex;
     984             :     assert(sh_link && ".symtab not found");
     985             :     const MCSection *InfoSection = Section.getAssociatedSection();
     986             :     sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
     987      178102 :     break;
     988             :   }
     989      178102 : 
     990             :   case ELF::SHT_SYMTAB:
     991      178102 :     sh_link = StringTableIndex;
     992      178102 :     sh_info = LastLocalSymbolIndex;
     993      178102 :     break;
     994             : 
     995             :   case ELF::SHT_SYMTAB_SHNDX:
     996       11133 :   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
     997       11133 :   case ELF::SHT_LLVM_ADDRSIG:
     998       11133 :     sh_link = SymbolTableIndex;
     999       11133 :     break;
    1000             : 
    1001        6203 :   case ELF::SHT_GROUP:
    1002             :     sh_link = SymbolTableIndex;
    1003             :     sh_info = GroupSymbolIndex;
    1004        6203 :     break;
    1005        6203 :   }
    1006             : 
    1007      213974 :   if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
    1008      213974 :     const MCSymbol *Sym = Section.getAssociatedSymbol();
    1009      213974 :     const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
    1010      213974 :     sh_link = SectionIndexMap.lookup(Sec);
    1011             :   }
    1012             : 
    1013     1114712 :   WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
    1014         233 :                    Section.getType(), Section.getFlags(), 0, Offset, Size,
    1015             :                    sh_link, sh_info, Section.getAlignment(),
    1016         233 :                    Section.getEntrySize());
    1017             : }
    1018             : 
    1019     1114713 : void ELFWriter::writeSectionHeader(
    1020     1114712 :     const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
    1021     1114712 :     const SectionOffsetsTy &SectionOffsets) {
    1022     1114712 :   const unsigned NumSections = SectionTable.size();
    1023     1114713 : 
    1024             :   // Null section first.
    1025       11147 :   uint64_t FirstSectionSize =
    1026             :       (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
    1027             :   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
    1028       11147 : 
    1029             :   for (const MCSectionELF *Section : SectionTable) {
    1030             :     uint32_t GroupSymbolIndex;
    1031             :     unsigned Type = Section->getType();
    1032       11147 :     if (Type != ELF::SHT_GROUP)
    1033       11147 :       GroupSymbolIndex = 0;
    1034             :     else
    1035     1125860 :       GroupSymbolIndex = Section->getGroup()->getIndex();
    1036             : 
    1037     1114713 :     const std::pair<uint64_t, uint64_t> &Offsets =
    1038     1114713 :         SectionOffsets.find(Section)->second;
    1039             :     uint64_t Size;
    1040             :     if (Type == ELF::SHT_NOBITS)
    1041      213974 :       Size = Layout.getSectionAddressSize(Section);
    1042             :     else
    1043             :       Size = Offsets.second - Offsets.first;
    1044             : 
    1045             :     writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
    1046     1114712 :                  *Section);
    1047       29866 :   }
    1048             : }
    1049     1084846 : 
    1050             : uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
    1051     1114712 :   uint64_t StartOffset = W.OS.tell();
    1052             : 
    1053             :   MCContext &Ctx = Asm.getContext();
    1054       11147 :   MCSectionELF *StrtabSection =
    1055             :       Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
    1056       11149 :   StringTableIndex = addToSectionTable(StrtabSection);
    1057       11149 : 
    1058             :   RevGroupMapTy RevGroupMap;
    1059       11149 :   SectionIndexMapTy SectionIndexMap;
    1060             : 
    1061       11149 :   std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
    1062       11149 : 
    1063             :   // Write out the ELF header ...
    1064             :   writeHeader(Asm);
    1065             : 
    1066             :   // ... then the sections ...
    1067             :   SectionOffsetsTy SectionOffsets;
    1068             :   std::vector<MCSectionELF *> Groups;
    1069             :   std::vector<MCSectionELF *> Relocations;
    1070       11149 :   for (MCSection &Sec : Asm) {
    1071             :     MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
    1072             :     if (Mode == NonDwoOnly && isDwoSection(Section))
    1073             :       continue;
    1074             :     if (Mode == DwoOnly && !isDwoSection(Section))
    1075             :       continue;
    1076      705465 : 
    1077             :     align(Section.getAlignment());
    1078      694318 : 
    1079         160 :     // Remember the offset into the file for this section.
    1080      694268 :     uint64_t SecStart = W.OS.tell();
    1081             : 
    1082             :     const MCSymbolELF *SignatureSymbol = Section.getGroup();
    1083      694158 :     writeSectionData(Asm, Section, Layout);
    1084             : 
    1085             :     uint64_t SecEnd = W.OS.tell();
    1086      694158 :     SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
    1087             : 
    1088      694158 :     MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
    1089      694158 : 
    1090             :     if (SignatureSymbol) {
    1091      694156 :       Asm.registerSymbol(*SignatureSymbol);
    1092      694156 :       unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
    1093             :       if (!GroupIdx) {
    1094      694156 :         MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
    1095             :         GroupIdx = addToSectionTable(Group);
    1096      694156 :         Group->setAlignment(4);
    1097      215529 :         Groups.push_back(Group);
    1098      215529 :       }
    1099      215529 :       std::vector<const MCSectionELF *> &Members =
    1100      213974 :           GroupMembers[SignatureSymbol];
    1101      213974 :       Members.push_back(&Section);
    1102      213974 :       if (RelSection)
    1103      213974 :         Members.push_back(RelSection);
    1104             :     }
    1105             : 
    1106      215529 :     SectionIndexMap[&Section] = addToSectionTable(&Section);
    1107      215529 :     if (RelSection) {
    1108      215529 :       SectionIndexMap[RelSection] = addToSectionTable(RelSection);
    1109      158108 :       Relocations.push_back(RelSection);
    1110             :     }
    1111             : 
    1112      694156 :     OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
    1113      694156 :   }
    1114      178102 : 
    1115      178102 :   MCSectionELF *CGProfileSection = nullptr;
    1116             :   if (!Asm.CGProfile.empty()) {
    1117             :     CGProfileSection = Ctx.getELFSection(".llvm.call-graph-profile",
    1118     1388312 :                                          ELF::SHT_LLVM_CALL_GRAPH_PROFILE,
    1119             :                                          ELF::SHF_EXCLUDE, 16, "");
    1120             :     SectionIndexMap[CGProfileSection] = addToSectionTable(CGProfileSection);
    1121             :   }
    1122       11147 : 
    1123           7 :   for (MCSectionELF *Group : Groups) {
    1124             :     align(Group->getAlignment());
    1125             : 
    1126           7 :     // Remember the offset into the file for this section.
    1127             :     uint64_t SecStart = W.OS.tell();
    1128             : 
    1129      225121 :     const MCSymbol *SignatureSymbol = Group->getGroup();
    1130      213974 :     assert(SignatureSymbol);
    1131             :     write(uint32_t(ELF::GRP_COMDAT));
    1132             :     for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
    1133      213974 :       uint32_t SecIndex = SectionIndexMap.lookup(Member);
    1134             :       write(SecIndex);
    1135      213974 :     }
    1136             : 
    1137             :     uint64_t SecEnd = W.OS.tell();
    1138      801585 :     SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
    1139      373637 :   }
    1140             : 
    1141             :   if (Mode == DwoOnly) {
    1142             :     // dwo files don't have symbol tables or relocations, but they do have
    1143      213974 :     // string tables.
    1144      213974 :     StrTabBuilder.finalize();
    1145             :   } else {
    1146             :     MCSectionELF *AddrsigSection;
    1147       11147 :     if (OWriter.EmitAddrsigSection) {
    1148             :       AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
    1149             :                                          ELF::SHF_EXCLUDE);
    1150          14 :       addToSectionTable(AddrsigSection);
    1151             :     }
    1152             : 
    1153       11133 :     // Compute symbol table information.
    1154        6190 :     computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
    1155             :                        SectionOffsets);
    1156        6190 : 
    1157             :     for (MCSectionELF *RelSection : Relocations) {
    1158             :       align(RelSection->getAlignment());
    1159             : 
    1160       11133 :       // Remember the offset into the file for this section.
    1161             :       uint64_t SecStart = W.OS.tell();
    1162             : 
    1163      189235 :       writeRelocations(Asm,
    1164      178102 :                        cast<MCSectionELF>(*RelSection->getAssociatedSection()));
    1165             : 
    1166             :       uint64_t SecEnd = W.OS.tell();
    1167      178102 :       SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
    1168             :     }
    1169      178102 : 
    1170      178102 :     if (OWriter.EmitAddrsigSection) {
    1171             :       uint64_t SecStart = W.OS.tell();
    1172      178102 :       writeAddrsigSection();
    1173      178102 :       uint64_t SecEnd = W.OS.tell();
    1174             :       SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
    1175             :     }
    1176       11133 :   }
    1177        6190 : 
    1178        6190 :   if (CGProfileSection) {
    1179        6190 :     uint64_t SecStart = W.OS.tell();
    1180        6190 :     for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
    1181             :       W.write<uint32_t>(CGPE.From->getSymbol().getIndex());
    1182             :       W.write<uint32_t>(CGPE.To->getSymbol().getIndex());
    1183             :       W.write<uint64_t>(CGPE.Count);
    1184       11147 :     }
    1185           7 :     uint64_t SecEnd = W.OS.tell();
    1186          29 :     SectionOffsets[CGProfileSection] = std::make_pair(SecStart, SecEnd);
    1187          22 :   }
    1188          22 : 
    1189          22 :   {
    1190             :     uint64_t SecStart = W.OS.tell();
    1191           7 :     const MCSectionELF *Sec = createStringTable(Ctx);
    1192           7 :     uint64_t SecEnd = W.OS.tell();
    1193             :     SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
    1194             :   }
    1195             : 
    1196       11147 :   uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
    1197       11146 :   align(NaturalAlignment);
    1198       11146 : 
    1199       11146 :   const uint64_t SectionHeaderOffset = W.OS.tell();
    1200             : 
    1201             :   // ... then the section header table ...
    1202       22294 :   writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
    1203       11147 : 
    1204             :   uint16_t NumSections = support::endian::byte_swap<uint16_t>(
    1205       11147 :       (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
    1206             :                                                       : SectionTable.size() + 1,
    1207             :       W.Endian);
    1208       11147 :   unsigned NumSectionsOffset;
    1209             : 
    1210       33434 :   auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
    1211       22294 :   if (is64Bit()) {
    1212       11140 :     uint64_t Val =
    1213       11147 :         support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
    1214             :     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
    1215             :                   offsetof(ELF::Elf64_Ehdr, e_shoff));
    1216       11147 :     NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
    1217       22294 :   } else {
    1218             :     uint32_t Val =
    1219        9902 :         support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
    1220             :     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
    1221             :                   offsetof(ELF::Elf32_Ehdr, e_shoff));
    1222             :     NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
    1223             :   }
    1224             :   Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
    1225        1525 :                 NumSectionsOffset);
    1226             : 
    1227             :   return W.OS.tell() - StartOffset;
    1228             : }
    1229             : 
    1230       11147 : bool ELFObjectWriter::hasRelocationAddend() const {
    1231             :   return TargetObjectWriter->hasRelocationAddend();
    1232             : }
    1233       11147 : 
    1234             : void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
    1235             :                                                const MCAsmLayout &Layout) {
    1236             :   // The presence of symbol versions causes undefined symbols and
    1237             :   // versions declared with @@@ to be renamed.
    1238             :   for (const std::pair<StringRef, const MCSymbol *> &P : Asm.Symvers) {
    1239             :     StringRef AliasName = P.first;
    1240       11143 :     const auto &Symbol = cast<MCSymbolELF>(*P.second);
    1241             :     size_t Pos = AliasName.find('@');
    1242             :     assert(Pos != StringRef::npos);
    1243             : 
    1244       11209 :     StringRef Prefix = AliasName.substr(0, Pos);
    1245          68 :     StringRef Rest = AliasName.substr(Pos);
    1246          68 :     StringRef Tail = Rest;
    1247          68 :     if (Rest.startswith("@@@"))
    1248             :       Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
    1249             : 
    1250          68 :     auto *Alias =
    1251          68 :         cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
    1252          68 :     Asm.registerSymbol(*Alias);
    1253             :     const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
    1254          18 :     Alias->setVariableValue(Value);
    1255             : 
    1256             :     // Aliases defined with .symvar copy the binding from the symbol they alias.
    1257          68 :     // This is the first place we are able to copy this information.
    1258          68 :     Alias->setExternal(Symbol.isExternal());
    1259          68 :     Alias->setBinding(Symbol.getBinding());
    1260          68 : 
    1261             :     if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
    1262             :       continue;
    1263             : 
    1264             :     // FIXME: produce a better error message.
    1265          68 :     if (Symbol.isUndefined() && Rest.startswith("@@") &&
    1266             :         !Rest.startswith("@@@"))
    1267          68 :       report_fatal_error("A @@ version cannot be undefined");
    1268          51 : 
    1269             :     if (Renames.count(&Symbol) && Renames[&Symbol] != Alias)
    1270             :       report_fatal_error(llvm::Twine("Multiple symbol versions defined for ") +
    1271          17 :                          Symbol.getName());
    1272             : 
    1273           1 :     Renames.insert(std::make_pair(&Symbol, Alias));
    1274             :   }
    1275          32 : 
    1276           1 :   for (const MCSymbol *&Sym : AddrsigSyms) {
    1277           1 :     if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
    1278             :       Sym = R;
    1279          15 :     if (Sym->isInSection() && Sym->getName().startswith(".L"))
    1280             :       Sym = Sym->getSection().getBeginSymbol();
    1281             :     Sym->setUsedInReloc();
    1282      248917 :   }
    1283      475552 : }
    1284           0 : 
    1285      237776 : // It is always valid to create a relocation with a symbol. It is preferable
    1286         400 : // to use a relocation with a section if that is possible. Using the section
    1287      237776 : // allows us to omit some local symbols from the symbol table.
    1288             : bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
    1289       11141 :                                                const MCSymbolRefExpr *RefA,
    1290             :                                                const MCSymbolELF *Sym,
    1291             :                                                uint64_t C,
    1292             :                                                unsigned Type) const {
    1293             :   // A PCRel relocation to an absolute value has no symbol (or section). We
    1294     5068419 :   // represent that with a relocation to a null section.
    1295             :   if (!RefA)
    1296             :     return false;
    1297             : 
    1298             :   MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
    1299             :   switch (Kind) {
    1300             :   default:
    1301     5068419 :     break;
    1302             :   // The .odp creation emits a relocation against the symbol ".TOC." which
    1303             :   // create a R_PPC64_TOC relocation. However the relocation symbol name
    1304     5068326 :   // in final object creation should be NULL, since the symbol does not
    1305             :   // really exist, it is just the reference to TOC base for the current
    1306             :   // object file. Since the symbol is undefined, returning false results
    1307             :   // in a relocation with a null section which is the desired result.
    1308             :   case MCSymbolRefExpr::VK_PPC_TOCBASE:
    1309             :     return false;
    1310             : 
    1311             :   // These VariantKind cause the relocation to refer to something other than
    1312             :   // the symbol itself, like a linker generated table. Since the address of
    1313             :   // symbol is not relevant, we cannot replace the symbol with the
    1314             :   // section and patch the difference in the addend.
    1315             :   case MCSymbolRefExpr::VK_GOT:
    1316             :   case MCSymbolRefExpr::VK_PLT:
    1317             :   case MCSymbolRefExpr::VK_GOTPCREL:
    1318             :   case MCSymbolRefExpr::VK_PPC_GOT_LO:
    1319             :   case MCSymbolRefExpr::VK_PPC_GOT_HI:
    1320             :   case MCSymbolRefExpr::VK_PPC_GOT_HA:
    1321             :     return true;
    1322             :   }
    1323             : 
    1324             :   // An undefined symbol is not in any section, so the relocation has to point
    1325             :   // to the symbol itself.
    1326             :   assert(Sym && "Expected a symbol");
    1327             :   if (Sym->isUndefined())
    1328             :     return true;
    1329             : 
    1330             :   unsigned Binding = Sym->getBinding();
    1331             :   switch(Binding) {
    1332             :   default:
    1333     5000231 :     llvm_unreachable("Invalid Binding");
    1334             :   case ELF::STB_LOCAL:
    1335             :     break;
    1336     4201401 :   case ELF::STB_WEAK:
    1337     4201401 :     // If the symbol is weak, it might be overridden by a symbol in another
    1338           0 :     // file. The relocation has to point to the symbol so that the linker
    1339           0 :     // can update it.
    1340             :     return true;
    1341             :   case ELF::STB_GLOBAL:
    1342             :     // Global ELF symbols can be preempted by the dynamic linker. The relocation
    1343             :     // has to point to the symbol for a reason analogous to the STB_WEAK case.
    1344             :     return true;
    1345             :   }
    1346             : 
    1347             :   // If a relocation points to a mergeable section, we have to be careful.
    1348             :   // If the offset is zero, a relocation with the section will encode the
    1349             :   // same information. With a non-zero offset, the situation is different.
    1350             :   // For example, a relocation can point 42 bytes past the end of a string.
    1351             :   // If we change such a relocation to use the section, the linker would think
    1352             :   // that it pointed to another string and subtracting 42 at runtime will
    1353             :   // produce the wrong value.
    1354             :   if (Sym->isInSection()) {
    1355             :     auto &Sec = cast<MCSectionELF>(Sym->getSection());
    1356             :     unsigned Flags = Sec.getFlags();
    1357             :     if (Flags & ELF::SHF_MERGE) {
    1358             :       if (C != 0)
    1359             :         return true;
    1360     2768296 : 
    1361             :       // It looks like gold has a bug (http://sourceware.org/PR16794) and can
    1362     2768295 :       // only handle section relocations to mergeable sections if using RELA.
    1363     2768295 :       if (!hasRelocationAddend())
    1364      905730 :         return true;
    1365             :     }
    1366             : 
    1367             :     // Most TLS relocations use a got, so they need the symbol. Even those that
    1368             :     // are just an offset (@tpoff), require a symbol in gold versions before
    1369      871797 :     // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
    1370             :     // http://sourceware.org/PR16773.
    1371             :     if (Flags & ELF::SHF_TLS)
    1372             :       return true;
    1373             :   }
    1374             : 
    1375             :   // If the symbol is a thumb function the final relocation must set the lowest
    1376             :   // bit. With a symbol that is done by just having the symbol have that bit
    1377     2703368 :   // set, so we would lose the bit if we relocated with the section.
    1378             :   // FIXME: We could use the section but add the bit to the relocation value.
    1379             :   if (Asm.isThumbFunc(Sym))
    1380             :     return true;
    1381             : 
    1382             :   if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
    1383             :     return true;
    1384             :   return false;
    1385     2703034 : }
    1386             : 
    1387             : void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
    1388     2703011 :                                        const MCAsmLayout &Layout,
    1389         138 :                                        const MCFragment *Fragment,
    1390             :                                        const MCFixup &Fixup, MCValue Target,
    1391             :                                        uint64_t &FixedValue) {
    1392             :   MCAsmBackend &Backend = Asm.getBackend();
    1393     5068443 :   bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
    1394             :                  MCFixupKindInfo::FKF_IsPCRel;
    1395             :   const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
    1396             :   uint64_t C = Target.getConstant();
    1397             :   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
    1398             :   MCContext &Ctx = Asm.getContext();
    1399     5068443 : 
    1400     5068443 :   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
    1401     5068443 :     // Let A, B and C being the components of Target and R be the location of
    1402     5068443 :     // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
    1403     5068443 :     // If it is pcrel, we want to compute (A - B + C - R).
    1404     5068443 : 
    1405             :     // In general, ELF has no relocations for -B. It can only represent (A + C)
    1406     5068443 :     // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
    1407             :     // replace B to implement it: (A - R - K + C)
    1408             :     if (IsPCRel) {
    1409             :       Ctx.reportError(
    1410             :           Fixup.getLoc(),
    1411             :           "No relocation available to represent this relative expression");
    1412             :       return;
    1413             :     }
    1414      212952 : 
    1415           5 :     const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
    1416             : 
    1417             :     if (SymB.isUndefined()) {
    1418     2703023 :       Ctx.reportError(Fixup.getLoc(),
    1419             :                       Twine("symbol '") + SymB.getName() +
    1420             :                           "' can not be undefined in a subtraction expression");
    1421      212947 :       return;
    1422             :     }
    1423      212947 : 
    1424           6 :     assert(!SymB.isAbsolute() && "Should have been folded");
    1425           6 :     const MCSection &SecB = SymB.getSection();
    1426           6 :     if (&SecB != &FixupSection) {
    1427           6 :       Ctx.reportError(Fixup.getLoc(),
    1428             :                       "Cannot represent a difference across sections");
    1429             :       return;
    1430             :     }
    1431             : 
    1432      212941 :     uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
    1433           7 :     uint64_t K = SymBOffset - FixupOffset;
    1434             :     IsPCRel = true;
    1435           7 :     C -= K;
    1436             :   }
    1437             : 
    1438      212934 :   // We either rejected the fixup or folded B into C at this point.
    1439             :   const MCSymbolRefExpr *RefA = Target.getSymA();
    1440             :   const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
    1441      212934 : 
    1442             :   bool ViaWeakRef = false;
    1443             :   if (SymA && SymA->isVariable()) {
    1444             :     const MCExpr *Expr = SymA->getVariableValue();
    1445     5068425 :     if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
    1446     5068425 :       if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
    1447             :         SymA = cast<MCSymbolELF>(&Inner->getSymbol());
    1448             :         ViaWeakRef = true;
    1449     5068332 :       }
    1450             :     }
    1451             :   }
    1452          62 : 
    1453          14 :   unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
    1454             :   uint64_t OriginalC = C;
    1455             :   bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
    1456             :   if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
    1457             :     C += Layout.getSymbolOffset(*SymA);
    1458             : 
    1459     5068425 :   uint64_t Addend = 0;
    1460             :   if (hasRelocationAddend()) {
    1461     5068419 :     Addend = C;
    1462     5068419 :     C = 0;
    1463     2702873 :   }
    1464             : 
    1465             :   FixedValue = C;
    1466     5068419 : 
    1467             :   const MCSectionELF *SecA = (SymA && SymA->isInSection())
    1468             :                                  ? cast<MCSectionELF>(&SymA->getSection())
    1469             :                                  : nullptr;
    1470             :   if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
    1471     5068419 :     return;
    1472             : 
    1473     5068326 :   if (!RelocateWithSymbol) {
    1474     5068419 :     const auto *SectionSymbol =
    1475             :         SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
    1476     5068418 :     if (SectionSymbol)
    1477             :       SectionSymbol->setUsedInReloc();
    1478             :     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA,
    1479     5068416 :                            OriginalC);
    1480             :     Relocations[&FixupSection].push_back(Rec);
    1481     2703002 :     return;
    1482     2702870 :   }
    1483             : 
    1484             :   const auto *RenamedSymA = SymA;
    1485             :   if (SymA) {
    1486     2703002 :     if (const MCSymbolELF *R = Renames.lookup(SymA))
    1487             :       RenamedSymA = R;
    1488             : 
    1489             :     if (ViaWeakRef)
    1490             :       RenamedSymA->setIsWeakrefUsedInReloc();
    1491     2365414 :     else
    1492     4730828 :       RenamedSymA->setUsedInReloc();
    1493             :   }
    1494             :   ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA,
    1495     2365414 :                          OriginalC);
    1496          12 :   Relocations[&FixupSection].push_back(Rec);
    1497             : }
    1498             : 
    1499             : bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
    1500             :     const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
    1501             :     bool InSet, bool IsPCRel) const {
    1502     2365414 :   const auto &SymA = cast<MCSymbolELF>(SA);
    1503             :   if (IsPCRel) {
    1504             :     assert(!InSet);
    1505    11126655 :     if (isWeak(SymA))
    1506             :       return false;
    1507             :   }
    1508             :   return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
    1509    11126655 :                                                                 InSet, IsPCRel);
    1510             : }
    1511     4062112 : 
    1512             : std::unique_ptr<MCObjectWriter>
    1513             : llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
    1514     9877253 :                             raw_pwrite_stream &OS, bool IsLittleEndian) {
    1515     9877253 :   return llvm::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
    1516             :                                                   IsLittleEndian);
    1517             : }
    1518             : 
    1519       30746 : std::unique_ptr<MCObjectWriter>
    1520             : llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
    1521       30746 :                                raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
    1522       30746 :                                bool IsLittleEndian) {
    1523             :   return llvm::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
    1524             :                                                IsLittleEndian);
    1525             : }

Generated by: LCOV version 1.13