Nick Lewycky | 7394c5a | 2013-03-09 09:32:16 +0000 | [diff] [blame] | 1 | //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===// |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 21 | #include "llvm/BinaryFormat/ELF.h" |
Rafael Espindola | 210f522 | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmBackend.h" |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmLayout.h" |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAssembler.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCELFObjectWriter.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCExpr.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCFixup.h" |
Rafael Espindola | 210f522 | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCFixupKindInfo.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCFragment.h" |
Eric Christopher | 42eb082 | 2018-09-06 22:09:31 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCObjectFileInfo.h" |
Craig Topper | f1d0f77 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCObjectWriter.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSection.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCSectionELF.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCSymbolELF.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCValue.h" |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 39 | #include "llvm/MC/StringTableBuilder.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Allocator.h" |
| 41 | #include "llvm/Support/Casting.h" |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Compression.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Endian.h" |
George Rimar | ef935cd | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Error.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Host.h" |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 47 | #include "llvm/Support/LEB128.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 48 | #include "llvm/Support/MathExtras.h" |
| 49 | #include "llvm/Support/SMLoc.h" |
Rafael Espindola | d43c001 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 50 | #include "llvm/Support/StringSaver.h" |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 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> |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 61 | #include <vector> |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 62 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 63 | using namespace llvm; |
| 64 | |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 65 | #undef DEBUG_TYPE |
| 66 | #define DEBUG_TYPE "reloc-info" |
| 67 | |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 68 | namespace { |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 69 | |
Eugene Zelenko | f49f90e | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 70 | using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>; |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 71 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 72 | class ELFObjectWriter; |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 73 | struct ELFWriter; |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 74 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 75 | bool isDwoSection(const MCSectionELF &Sec) { |
| 76 | return Sec.getSectionName().endswith(".dwo"); |
| 77 | } |
| 78 | |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 79 | class SymbolTableWriter { |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 80 | ELFWriter &EWriter; |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 81 | bool Is64Bit; |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 82 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 83 | // indexes we are going to write to .symtab_shndx. |
| 84 | std::vector<uint32_t> ShndxIndexes; |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 85 | |
| 86 | // The numbel of symbols written so far. |
| 87 | unsigned NumWritten; |
| 88 | |
| 89 | void createSymtabShndx(); |
| 90 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 91 | template <typename T> void write(T Value); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 92 | |
| 93 | public: |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 94 | SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 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); |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 98 | |
| 99 | ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; } |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 102 | struct ELFWriter { |
| 103 | ELFObjectWriter &OWriter; |
| 104 | support::endian::Writer W; |
| 105 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 106 | enum DwoMode { |
| 107 | AllSections, |
| 108 | NonDwoOnly, |
| 109 | DwoOnly, |
| 110 | } Mode; |
| 111 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 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); |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 115 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 116 | /// Helper struct for containing some precomputed information on symbols. |
| 117 | struct ELFSymbolData { |
| 118 | const MCSymbolELF *Symbol; |
| 119 | uint32_t SectionIndex; |
| 120 | StringRef Name; |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 121 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 122 | // Support lexicographic sorting. |
| 123 | bool operator<(const ELFSymbolData &RHS) const { |
| 124 | unsigned LHSType = Symbol->getType(); |
| 125 | unsigned RHSType = RHS.Symbol->getType(); |
| 126 | if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION) |
| 127 | return false; |
| 128 | if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
| 129 | return true; |
| 130 | if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
| 131 | return SectionIndex < RHS.SectionIndex; |
| 132 | return Name < RHS.Name; |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 133 | } |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 134 | }; |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 135 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 136 | /// @} |
| 137 | /// @name Symbol Table Data |
| 138 | /// @{ |
| 139 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 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. |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 156 | bool is64Bit() const; |
| 157 | bool hasRelocationAddend() const; |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 158 | |
| 159 | void align(unsigned Alignment); |
| 160 | |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 161 | bool maybeWriteCompression(uint64_t Size, |
| 162 | SmallVectorImpl<char> &CompressedContents, |
| 163 | bool ZLibStyle, unsigned Alignment); |
| 164 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 165 | public: |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 166 | ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS, |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 167 | bool IsLittleEndian, DwoMode Mode) |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 168 | : OWriter(OWriter), |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 169 | W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {} |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 170 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 171 | void WriteWord(uint64_t Word) { |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 172 | if (is64Bit()) |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 173 | W.write<uint64_t>(Word); |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 174 | else |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 175 | W.write<uint32_t>(Word); |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | template <typename T> void write(T Val) { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 179 | W.write(Val); |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 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 |
Eugene Zelenko | f49f90e | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 188 | using SectionOffsetsTy = |
| 189 | std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>; |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 190 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 191 | // Map from a signature symbol to the group section index |
Eugene Zelenko | f49f90e | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 192 | using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>; |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 204 | void writeAddrsigSection(); |
| 205 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 206 | MCSectionELF *createRelocationSection(MCContext &Ctx, |
| 207 | const MCSectionELF &Sec); |
| 208 | |
| 209 | const MCSectionELF *createStringTable(MCContext &Ctx); |
| 210 | |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 225 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout); |
David Blaikie | 23d0579 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 226 | void writeSection(const SectionIndexMapTy &SectionIndexMap, |
| 227 | uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, |
| 228 | const MCSectionELF &Section); |
| 229 | }; |
Eugene Zelenko | a700a60 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 230 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 231 | class ELFObjectWriter : public MCObjectWriter { |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 239 | bool EmitAddrsigSection = false; |
| 240 | std::vector<const MCSymbol *> AddrsigSyms; |
| 241 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 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: |
Peter Collingbourne | 4ef1841 | 2018-05-21 19:30:59 +0000 | [diff] [blame] | 250 | ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW) |
| 251 | : TargetObjectWriter(std::move(MOTW)) {} |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 252 | |
| 253 | void reset() override { |
| 254 | Relocations.clear(); |
| 255 | Renames.clear(); |
| 256 | MCObjectWriter::reset(); |
| 257 | } |
| 258 | |
| 259 | bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 260 | const MCSymbol &SymA, |
| 261 | const MCFragment &FB, bool InSet, |
| 262 | bool IsPCRel) const override; |
| 263 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 264 | virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc, |
| 265 | const MCSectionELF *From, |
| 266 | const MCSectionELF *To) { |
| 267 | return true; |
| 268 | } |
| 269 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 270 | void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 271 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 272 | MCValue Target, uint64_t &FixedValue) override; |
| 273 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 274 | void executePostLayoutBinding(MCAssembler &Asm, |
| 275 | const MCAsmLayout &Layout) override; |
| 276 | |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 277 | void emitAddrsigSection() override { EmitAddrsigSection = true; } |
| 278 | void addAddrsigSymbol(const MCSymbol *Sym) override { |
| 279 | AddrsigSyms.push_back(Sym); |
| 280 | } |
| 281 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 282 | friend struct ELFWriter; |
| 283 | }; |
| 284 | |
Peter Collingbourne | 4ef1841 | 2018-05-21 19:30:59 +0000 | [diff] [blame] | 285 | class ELFSingleObjectWriter : public ELFObjectWriter { |
| 286 | raw_pwrite_stream &OS; |
| 287 | bool IsLittleEndian; |
| 288 | |
| 289 | public: |
| 290 | ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 291 | raw_pwrite_stream &OS, bool IsLittleEndian) |
| 292 | : ELFObjectWriter(std::move(MOTW)), OS(OS), |
| 293 | IsLittleEndian(IsLittleEndian) {} |
| 294 | |
| 295 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override { |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 296 | return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections) |
| 297 | .writeObject(Asm, Layout); |
| 298 | } |
| 299 | |
| 300 | friend struct ELFWriter; |
| 301 | }; |
| 302 | |
| 303 | class ELFDwoObjectWriter : public ELFObjectWriter { |
| 304 | raw_pwrite_stream &OS, &DwoOS; |
| 305 | bool IsLittleEndian; |
| 306 | |
| 307 | public: |
| 308 | ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 309 | raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, |
| 310 | bool IsLittleEndian) |
| 311 | : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS), |
| 312 | IsLittleEndian(IsLittleEndian) {} |
| 313 | |
| 314 | virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc, |
| 315 | const MCSectionELF *From, |
| 316 | const MCSectionELF *To) override { |
| 317 | if (isDwoSection(*From)) { |
| 318 | Ctx.reportError(Loc, "A dwo section may not contain relocations"); |
| 319 | return false; |
| 320 | } |
| 321 | if (To && isDwoSection(*To)) { |
| 322 | Ctx.reportError(Loc, "A relocation may not refer to a dwo section"); |
| 323 | return false; |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override { |
| 329 | uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly) |
| 330 | .writeObject(Asm, Layout); |
| 331 | Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly) |
| 332 | .writeObject(Asm, Layout); |
| 333 | return Size; |
Peter Collingbourne | 4ef1841 | 2018-05-21 19:30:59 +0000 | [diff] [blame] | 334 | } |
| 335 | }; |
| 336 | |
Eugene Zelenko | 380d47d | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 337 | } // end anonymous namespace |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 338 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 339 | void ELFWriter::align(unsigned Alignment) { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 340 | uint64_t Padding = OffsetToAlignment(W.OS.tell(), Alignment); |
| 341 | W.OS.write_zeros(Padding); |
Rafael Espindola | aa4466d | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 344 | unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) { |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 345 | SectionTable.push_back(Sec); |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 346 | StrTabBuilder.add(Sec->getSectionName()); |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 347 | return SectionTable.size(); |
| 348 | } |
| 349 | |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 350 | void SymbolTableWriter::createSymtabShndx() { |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 351 | if (!ShndxIndexes.empty()) |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 352 | return; |
| 353 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 354 | ShndxIndexes.resize(NumWritten); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 357 | template <typename T> void SymbolTableWriter::write(T Value) { |
| 358 | EWriter.write(Value); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 361 | SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit) |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 362 | : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {} |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 363 | |
| 364 | void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value, |
| 365 | uint64_t size, uint8_t other, |
| 366 | uint32_t shndx, bool Reserved) { |
| 367 | bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved; |
| 368 | |
| 369 | if (LargeIndex) |
| 370 | createSymtabShndx(); |
| 371 | |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 372 | if (!ShndxIndexes.empty()) { |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 373 | if (LargeIndex) |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 374 | ShndxIndexes.push_back(shndx); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 375 | else |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 376 | ShndxIndexes.push_back(0); |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx; |
| 380 | |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 381 | if (Is64Bit) { |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 382 | write(name); // st_name |
| 383 | write(info); // st_info |
| 384 | write(other); // st_other |
| 385 | write(Index); // st_shndx |
| 386 | write(value); // st_value |
| 387 | write(size); // st_size |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 388 | } else { |
Rafael Espindola | 5fc3769 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 389 | write(name); // st_name |
| 390 | write(uint32_t(value)); // st_value |
| 391 | write(uint32_t(size)); // st_size |
| 392 | write(info); // st_info |
| 393 | write(other); // st_other |
| 394 | write(Index); // st_shndx |
Rafael Espindola | 81c66bc | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | ++NumWritten; |
| 398 | } |
| 399 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 400 | bool ELFWriter::is64Bit() const { |
| 401 | return OWriter.TargetObjectWriter->is64Bit(); |
| 402 | } |
| 403 | |
| 404 | bool ELFWriter::hasRelocationAddend() const { |
| 405 | return OWriter.hasRelocationAddend(); |
| 406 | } |
| 407 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 408 | // Emit the ELF header. |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 409 | void ELFWriter::writeHeader(const MCAssembler &Asm) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 410 | // ELF Header |
| 411 | // ---------- |
| 412 | // |
| 413 | // Note |
| 414 | // ---- |
| 415 | // emitWord method behaves differently for ELF32 and ELF64, writing |
| 416 | // 4 bytes in the former and 8 in the latter. |
| 417 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 418 | W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3] |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 419 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 420 | W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 421 | |
| 422 | // e_ident[EI_DATA] |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 423 | W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB |
| 424 | : ELF::ELFDATA2MSB); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 425 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 426 | W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 427 | // e_ident[EI_OSABI] |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 428 | W.OS << char(OWriter.TargetObjectWriter->getOSABI()); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 429 | W.OS << char(0); // e_ident[EI_ABIVERSION] |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 430 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 431 | W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 432 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 433 | W.write<uint16_t>(ELF::ET_REL); // e_type |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 434 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 435 | W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 436 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 437 | W.write<uint32_t>(ELF::EV_CURRENT); // e_version |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 438 | WriteWord(0); // e_entry, no entry point in .o file |
| 439 | WriteWord(0); // e_phoff, no program header for .o |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 440 | WriteWord(0); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 441 | |
Jason W Kim | 2d7a53a | 2011-02-04 21:41:11 +0000 | [diff] [blame] | 442 | // e_flags = whatever the target wants |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 443 | W.write<uint32_t>(Asm.getELFHeaderEFlags()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 444 | |
| 445 | // e_ehsize = ELF header size |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 446 | W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr) |
| 447 | : sizeof(ELF::Elf32_Ehdr)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 448 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 449 | W.write<uint16_t>(0); // e_phentsize = prog header entry size |
| 450 | W.write<uint16_t>(0); // e_phnum = # prog header entries = 0 |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 451 | |
| 452 | // e_shentsize = Section header entry size |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 453 | W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr) |
| 454 | : sizeof(ELF::Elf32_Shdr)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 455 | |
| 456 | // e_shnum = # of section header ents |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 457 | W.write<uint16_t>(0); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 458 | |
| 459 | // e_shstrndx = Section # of '.shstrtab' |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 460 | assert(StringTableIndex < ELF::SHN_LORESERVE); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 461 | W.write<uint16_t>(StringTableIndex); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 464 | uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym, |
| 465 | const MCAsmLayout &Layout) { |
Rafael Espindola | cfac75a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 466 | if (Sym.isCommon() && Sym.isExternal()) |
Rafael Espindola | 82fdcc0 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 467 | return Sym.getCommonAlignment(); |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 468 | |
Rafael Espindola | 593cb79 | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 469 | uint64_t Res; |
Duncan P. N. Exon Smith | 37e9aba | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 470 | if (!Layout.getSymbolOffset(Sym, Res)) |
Rafael Espindola | 1c37cbd | 2014-04-30 16:59:35 +0000 | [diff] [blame] | 471 | return 0; |
| 472 | |
Duncan P. N. Exon Smith | 37e9aba | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 473 | if (Layout.getAssembler().isThumbFunc(&Sym)) |
Rafael Espindola | a60f719 | 2014-03-21 22:00:29 +0000 | [diff] [blame] | 474 | Res |= 1; |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 475 | |
Rafael Espindola | a60f719 | 2014-03-21 22:00:29 +0000 | [diff] [blame] | 476 | return Res; |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Roman Divacky | 7e889af | 2014-01-07 20:17:03 +0000 | [diff] [blame] | 479 | static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) { |
| 480 | uint8_t Type = newType; |
| 481 | |
| 482 | // Propagation rules: |
| 483 | // IFUNC > FUNC > OBJECT > NOTYPE |
| 484 | // TLS_OBJECT > OBJECT > NOTYPE |
| 485 | // |
| 486 | // dont let the new type degrade the old type |
| 487 | switch (origType) { |
| 488 | default: |
| 489 | break; |
| 490 | case ELF::STT_GNU_IFUNC: |
| 491 | if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT || |
| 492 | Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS) |
| 493 | Type = ELF::STT_GNU_IFUNC; |
| 494 | break; |
| 495 | case ELF::STT_FUNC: |
| 496 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
| 497 | Type == ELF::STT_TLS) |
| 498 | Type = ELF::STT_FUNC; |
| 499 | break; |
| 500 | case ELF::STT_OBJECT: |
| 501 | if (Type == ELF::STT_NOTYPE) |
| 502 | Type = ELF::STT_OBJECT; |
| 503 | break; |
| 504 | case ELF::STT_TLS: |
| 505 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
| 506 | Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC) |
| 507 | Type = ELF::STT_TLS; |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | return Type; |
| 512 | } |
| 513 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 514 | void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, |
| 515 | ELFSymbolData &MSD, const MCAsmLayout &Layout) { |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 516 | const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol); |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 517 | const MCSymbolELF *Base = |
| 518 | cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol)); |
Rafael Espindola | 3008f80 | 2014-03-26 00:16:43 +0000 | [diff] [blame] | 519 | |
| 520 | // This has to be in sync with when computeSymbolTable uses SHN_ABS or |
| 521 | // SHN_COMMON. |
Rafael Espindola | cfac75a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 522 | bool IsReserved = !Base || Symbol.isCommon(); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 523 | |
Jack Carter | 77afbdc | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 524 | // Binding and Type share the same byte as upper and lower nibbles |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 525 | uint8_t Binding = Symbol.getBinding(); |
| 526 | uint8_t Type = Symbol.getType(); |
Rafael Espindola | 4ff2dad | 2014-03-23 03:33:20 +0000 | [diff] [blame] | 527 | if (Base) { |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 528 | Type = mergeTypeForSet(Type, Base->getType()); |
Rafael Espindola | 4ff2dad | 2014-03-23 03:33:20 +0000 | [diff] [blame] | 529 | } |
Rafael Espindola | c212856 | 2015-06-04 00:47:43 +0000 | [diff] [blame] | 530 | uint8_t Info = (Binding << 4) | Type; |
Jack Carter | 77afbdc | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 531 | |
Joerg Sonnenberger | 7258078 | 2013-10-29 01:06:17 +0000 | [diff] [blame] | 532 | // Other and Visibility share the same byte with Visibility using the lower |
Jack Carter | 77afbdc | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 533 | // 2 bits |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 534 | uint8_t Visibility = Symbol.getVisibility(); |
Rafael Espindola | cddcaba | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 535 | uint8_t Other = Symbol.getOther() | Visibility; |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 536 | |
Duncan P. N. Exon Smith | 37e9aba | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 537 | uint64_t Value = SymbolValue(*MSD.Symbol, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 538 | uint64_t Size = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 539 | |
Rafael Espindola | 28afbf24 | 2015-05-29 17:24:52 +0000 | [diff] [blame] | 540 | const MCExpr *ESize = MSD.Symbol->getSize(); |
Rafael Espindola | e459f72 | 2014-03-27 00:28:24 +0000 | [diff] [blame] | 541 | if (!ESize && Base) |
Rafael Espindola | 28afbf24 | 2015-05-29 17:24:52 +0000 | [diff] [blame] | 542 | ESize = Base->getSize(); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 543 | |
Rafael Espindola | f012124 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 544 | if (ESize) { |
| 545 | int64_t Res; |
Rafael Espindola | 9428f18 | 2015-04-06 15:27:57 +0000 | [diff] [blame] | 546 | if (!ESize->evaluateKnownAbsolute(Res, Layout)) |
Rafael Espindola | f012124 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 547 | report_fatal_error("Size expression must be absolute."); |
| 548 | Size = Res; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | // Write out the symbol table entry |
Rafael Espindola | c66c8e7 | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 552 | Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex, |
| 553 | IsReserved); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Rafael Espindola | 560d73a | 2015-04-17 20:05:17 +0000 | [diff] [blame] | 556 | // True if the assembler knows nothing about the final value of the symbol. |
| 557 | // This doesn't cover the comdat issues, since in those cases the assembler |
| 558 | // can at least know that all symbols in the section will move together. |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 559 | static bool isWeak(const MCSymbolELF &Sym) { |
| 560 | if (Sym.getType() == ELF::STT_GNU_IFUNC) |
Rafael Espindola | 2298ce3 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 561 | return true; |
| 562 | |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 563 | switch (Sym.getBinding()) { |
Rafael Espindola | 2298ce3 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 564 | default: |
| 565 | llvm_unreachable("Unknown binding"); |
| 566 | case ELF::STB_LOCAL: |
| 567 | return false; |
| 568 | case ELF::STB_GLOBAL: |
Rafael Espindola | 560d73a | 2015-04-17 20:05:17 +0000 | [diff] [blame] | 569 | return false; |
Rafael Espindola | 2298ce3 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 570 | case ELF::STB_WEAK: |
| 571 | case ELF::STB_GNU_UNIQUE: |
| 572 | return true; |
| 573 | } |
Rafael Espindola | 4e86f54 | 2015-03-24 23:48:44 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 576 | bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol, |
| 577 | bool Used, bool Renamed) { |
Rafael Espindola | 0a70f9b | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 578 | 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 | return false; |
| 583 | } |
| 584 | } |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 585 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 586 | if (Used) |
| 587 | return true; |
| 588 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 589 | if (Renamed) |
| 590 | return false; |
| 591 | |
Rafael Espindola | a41438c | 2015-06-03 21:23:21 +0000 | [diff] [blame] | 592 | 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 | return false; |
Rafael Espindola | 0af058f | 2014-04-28 13:39:57 +0000 | [diff] [blame] | 596 | } |
Rafael Espindola | 21451e5 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 597 | |
Rafael Espindola | a41438c | 2015-06-03 21:23:21 +0000 | [diff] [blame] | 598 | if (Symbol.isUndefined() && !Symbol.isBindingSet()) |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 599 | return false; |
| 600 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 601 | if (Symbol.isTemporary()) |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 602 | return false; |
| 603 | |
Rafael Espindola | 9f299ab | 2015-06-04 15:33:30 +0000 | [diff] [blame] | 604 | if (Symbol.getType() == ELF::STT_SECTION) |
| 605 | return false; |
| 606 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 610 | void ELFWriter::computeSymbolTable( |
Rafael Espindola | c78f07a | 2015-04-07 19:00:17 +0000 | [diff] [blame] | 611 | MCAssembler &Asm, const MCAsmLayout &Layout, |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 612 | const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap, |
| 613 | SectionOffsetsTy &SectionOffsets) { |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 614 | MCContext &Ctx = Asm.getContext(); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 615 | SymbolTableWriter Writer(*this, is64Bit()); |
| 616 | |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 617 | // Symbol table |
| 618 | unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
| 619 | MCSectionELF *SymtabSection = |
| 620 | Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, ""); |
| 621 | SymtabSection->setAlignment(is64Bit() ? 8 : 4); |
| 622 | SymbolTableIndex = addToSectionTable(SymtabSection); |
| 623 | |
Rafael Espindola | aa4466d | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 624 | align(SymtabSection->getAlignment()); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 625 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 626 | |
| 627 | // The first entry is the undefined symbol entry. |
| 628 | Writer.writeSymbol(0, 0, 0, 0, 0, 0, false); |
| 629 | |
Rafael Espindola | 7978ab3 | 2015-05-28 20:11:34 +0000 | [diff] [blame] | 630 | std::vector<ELFSymbolData> LocalSymbolData; |
| 631 | std::vector<ELFSymbolData> ExternalSymbolData; |
Rafael Espindola | 7978ab3 | 2015-05-28 20:11:34 +0000 | [diff] [blame] | 632 | |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 633 | // Add the data for the symbols. |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 634 | bool HasLargeSectionIndex = false; |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 635 | for (const MCSymbol &S : Asm.symbols()) { |
| 636 | const auto &Symbol = cast<MCSymbolELF>(S); |
Rafael Espindola | 2b9f4dc | 2015-06-03 21:30:10 +0000 | [diff] [blame] | 637 | bool Used = Symbol.isUsedInReloc(); |
Rafael Espindola | 5eb7481 | 2015-06-03 21:52:06 +0000 | [diff] [blame] | 638 | bool WeakrefUsed = Symbol.isWeakrefUsedInReloc(); |
Rafael Espindola | f3857c3 | 2015-06-03 21:41:59 +0000 | [diff] [blame] | 639 | bool isSignature = Symbol.isSignature(); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 640 | |
Duncan P. N. Exon Smith | 37e9aba | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 641 | if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature, |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 642 | OWriter.Renames.count(&Symbol))) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 643 | continue; |
| 644 | |
Oliver Stannard | 99ab112 | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 645 | if (Symbol.isTemporary() && Symbol.isUndefined()) { |
| 646 | Ctx.reportError(SMLoc(), "Undefined temporary symbol"); |
| 647 | continue; |
| 648 | } |
Rafael Espindola | 1de6f36 | 2015-06-25 20:10:45 +0000 | [diff] [blame] | 649 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 650 | ELFSymbolData MSD; |
Rafael Espindola | f7e0685 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 651 | MSD.Symbol = cast<MCSymbolELF>(&Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 652 | |
Rafael Espindola | f3857c3 | 2015-06-03 21:41:59 +0000 | [diff] [blame] | 653 | bool Local = Symbol.getBinding() == ELF::STB_LOCAL; |
Rafael Espindola | 1de6f36 | 2015-06-25 20:10:45 +0000 | [diff] [blame] | 654 | assert(Local || !Symbol.isTemporary()); |
| 655 | |
Rafael Espindola | 616c7af | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 656 | if (Symbol.isAbsolute()) { |
Rafael Espindola | 0bce334 | 2014-03-24 03:43:21 +0000 | [diff] [blame] | 657 | MSD.SectionIndex = ELF::SHN_ABS; |
Rafael Espindola | 82fdcc0 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 658 | } else if (Symbol.isCommon()) { |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 659 | assert(!Local); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 660 | MSD.SectionIndex = ELF::SHN_COMMON; |
Rafael Espindola | 616c7af | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 661 | } else if (Symbol.isUndefined()) { |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 662 | if (isSignature && !Used) { |
Rafael Espindola | 5acccb1 | 2015-04-28 22:59:58 +0000 | [diff] [blame] | 663 | MSD.SectionIndex = RevGroupMap.lookup(&Symbol); |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 664 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 665 | HasLargeSectionIndex = true; |
| 666 | } else { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 667 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 668 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 669 | } else { |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 670 | const MCSectionELF &Section = |
Rafael Espindola | 616c7af | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 671 | static_cast<const MCSectionELF &>(Symbol.getSection()); |
George Rimar | d437f3c | 2018-12-05 10:43:58 +0000 | [diff] [blame] | 672 | |
| 673 | // We may end up with a situation when section symbol is technically |
| 674 | // defined, but should not be. That happens because we explicitly |
| 675 | // pre-create few .debug_* sections to have accessors. |
| 676 | // And if these sections were not really defined in the code, but were |
| 677 | // referenced, we simply error out. |
| 678 | if (!Section.isRegistered()) { |
| 679 | assert(static_cast<const MCSymbolELF &>(Symbol).getType() == |
| 680 | ELF::STT_SECTION); |
| 681 | Ctx.reportError(SMLoc(), |
| 682 | "Undefined section reference: " + Symbol.getName()); |
| 683 | continue; |
| 684 | } |
| 685 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 686 | if (Mode == NonDwoOnly && isDwoSection(Section)) |
| 687 | continue; |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 688 | MSD.SectionIndex = SectionIndexMap.lookup(&Section); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 689 | assert(MSD.SectionIndex && "Invalid section index!"); |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 690 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 691 | HasLargeSectionIndex = true; |
Rafael Espindola | 5df0b65 | 2010-10-15 15:39:06 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 694 | StringRef Name = Symbol.getName(); |
Rafael Espindola | d8ee23f | 2014-10-17 01:48:58 +0000 | [diff] [blame] | 695 | |
| 696 | // Sections have their own string table |
Rafael Espindola | d43c001 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 697 | if (Symbol.getType() != ELF::STT_SECTION) { |
| 698 | MSD.Name = Name; |
| 699 | StrTabBuilder.add(Name); |
| 700 | } |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 701 | |
Rafael Espindola | 3b08750 | 2015-05-29 14:20:40 +0000 | [diff] [blame] | 702 | if (Local) |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 703 | LocalSymbolData.push_back(MSD); |
| 704 | else |
| 705 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Rafael Espindola | 9561506 | 2015-07-02 16:59:57 +0000 | [diff] [blame] | 708 | // This holds the .symtab_shndx section index. |
| 709 | unsigned SymtabShndxSectionIndex = 0; |
| 710 | |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 711 | if (HasLargeSectionIndex) { |
| 712 | MCSectionELF *SymtabShndxSection = |
| 713 | Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, ""); |
| 714 | SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection); |
| 715 | SymtabShndxSection->setAlignment(4); |
| 716 | } |
| 717 | |
Rafael Espindola | 2444141 | 2015-05-28 19:29:15 +0000 | [diff] [blame] | 718 | ArrayRef<std::string> FileNames = Asm.getFileNames(); |
| 719 | for (const std::string &Name : FileNames) |
Rafael Espindola | fd64e0f | 2015-05-28 18:03:20 +0000 | [diff] [blame] | 720 | StrTabBuilder.add(Name); |
Hans Wennborg | a8febf2 | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 721 | |
Rafael Espindola | 715bcca | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 722 | StrTabBuilder.finalize(); |
Hans Wennborg | a8febf2 | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 723 | |
Peter Collingbourne | 6387bda | 2017-03-03 21:22:06 +0000 | [diff] [blame] | 724 | // File symbols are emitted first and handled separately from normal symbols, |
| 725 | // i.e. a non-STT_FILE symbol with the same name may appear. |
Rafael Espindola | 86c0a10 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 726 | for (const std::string &Name : FileNames) |
| 727 | Writer.writeSymbol(StrTabBuilder.getOffset(Name), |
| 728 | ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT, |
| 729 | ELF::SHN_ABS, true); |
| 730 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 731 | // Symbols are required to be in lexicographic order. |
| 732 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 733 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 734 | |
| 735 | // Set the symbol indices. Local symbols must come before all other |
| 736 | // symbols with non-local bindings. |
Rafael Espindola | 2444141 | 2015-05-28 19:29:15 +0000 | [diff] [blame] | 737 | unsigned Index = FileNames.size() + 1; |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 738 | |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 739 | for (ELFSymbolData &MSD : LocalSymbolData) { |
Daniel Jasper | b7f5b8b | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 740 | unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION |
| 741 | ? 0 |
| 742 | : StrTabBuilder.getOffset(MSD.Name); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 743 | MSD.Symbol->setIndex(Index++); |
Rafael Espindola | c66c8e7 | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 744 | writeSymbol(Writer, StringIndex, MSD, Layout); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 745 | } |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 746 | |
| 747 | // Write the symbol table entries. |
Rafael Espindola | 86c0a10 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 748 | LastLocalSymbolIndex = Index; |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 749 | |
Rafael Espindola | b665c2b | 2015-05-28 19:43:20 +0000 | [diff] [blame] | 750 | for (ELFSymbolData &MSD : ExternalSymbolData) { |
Rafael Espindola | c66c8e7 | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 751 | unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name); |
Rafael Espindola | 86c0a10 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 752 | MSD.Symbol->setIndex(Index++); |
Rafael Espindola | c66c8e7 | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 753 | writeSymbol(Writer, StringIndex, MSD, Layout); |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 754 | assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 755 | } |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 756 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 757 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 758 | SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd); |
| 759 | |
| 760 | ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes(); |
| 761 | if (ShndxIndexes.empty()) { |
| 762 | assert(SymtabShndxSectionIndex == 0); |
| 763 | return; |
| 764 | } |
| 765 | assert(SymtabShndxSectionIndex != 0); |
| 766 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 767 | SecStart = W.OS.tell(); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 768 | const MCSectionELF *SymtabShndxSection = |
| 769 | SectionTable[SymtabShndxSectionIndex - 1]; |
| 770 | for (uint32_t Index : ShndxIndexes) |
| 771 | write(Index); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 772 | SecEnd = W.OS.tell(); |
Rafael Espindola | b9ffeb09 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 773 | SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 776 | void ELFWriter::writeAddrsigSection() { |
| 777 | for (const MCSymbol *Sym : OWriter.AddrsigSyms) |
| 778 | encodeULEB128(Sym->getIndex(), W.OS); |
| 779 | } |
| 780 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 781 | MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx, |
| 782 | const MCSectionELF &Sec) { |
| 783 | if (OWriter.Relocations[&Sec].empty()) |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 784 | return nullptr; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 785 | |
Rafael Espindola | 4689501 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 786 | const StringRef SectionName = Sec.getSectionName(); |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 787 | std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; |
| 788 | RelaSectionName += SectionName; |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 789 | |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 790 | unsigned EntrySize; |
| 791 | if (hasRelocationAddend()) |
| 792 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
| 793 | else |
| 794 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 795 | |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 796 | unsigned Flags = 0; |
Rafael Espindola | 4689501 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 797 | if (Sec.getFlags() & ELF::SHF_GROUP) |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 798 | Flags = ELF::SHF_GROUP; |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 799 | |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 800 | MCSectionELF *RelaSection = Ctx.createELFRelSection( |
Rafael Espindola | 0710289 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 801 | RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, |
Rafael Espindola | 4689501 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 802 | Flags, EntrySize, Sec.getGroup(), &Sec); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 803 | RelaSection->setAlignment(is64Bit() ? 8 : 4); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 804 | return RelaSection; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 805 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 806 | |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 807 | // Include the debug info compression header. |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 808 | bool ELFWriter::maybeWriteCompression( |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 809 | uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle, |
| 810 | unsigned Alignment) { |
| 811 | if (ZLibStyle) { |
| 812 | uint64_t HdrSize = |
| 813 | is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr); |
| 814 | if (Size <= HdrSize + CompressedContents.size()) |
| 815 | return false; |
| 816 | // Platform specific header is followed by compressed data. |
| 817 | if (is64Bit()) { |
| 818 | // Write Elf64_Chdr header. |
| 819 | write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB)); |
| 820 | write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field. |
| 821 | write(static_cast<ELF::Elf64_Xword>(Size)); |
| 822 | write(static_cast<ELF::Elf64_Xword>(Alignment)); |
| 823 | } else { |
| 824 | // Write Elf32_Chdr header otherwise. |
| 825 | write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB)); |
| 826 | write(static_cast<ELF::Elf32_Word>(Size)); |
| 827 | write(static_cast<ELF::Elf32_Word>(Alignment)); |
| 828 | } |
| 829 | return true; |
| 830 | } |
| 831 | |
| 832 | // "ZLIB" followed by 8 bytes representing the uncompressed size of the section, |
| 833 | // useful for consumers to preallocate a buffer to decompress into. |
Richard Smith | e89c000 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 834 | const StringRef Magic = "ZLIB"; |
| 835 | if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size()) |
| 836 | return false; |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 837 | W.OS << Magic; |
| 838 | support::endian::write(W.OS, Size, support::big); |
Richard Smith | e89c000 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 839 | return true; |
| 840 | } |
| 841 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 842 | void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec, |
| 843 | const MCAsmLayout &Layout) { |
Rafael Espindola | a287fe7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 844 | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 845 | StringRef SectionName = Section.getSectionName(); |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 846 | |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 847 | auto &MC = Asm.getContext(); |
| 848 | const auto &MAI = MC.getAsmInfo(); |
| 849 | |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 850 | // Compressing debug_frame requires handling alignment fragments which is |
| 851 | // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow |
| 852 | // for writing to arbitrary buffers) for little benefit. |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 853 | bool CompressionEnabled = |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 854 | MAI->compressDebugSections() != DebugCompressionType::None; |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 855 | if (!CompressionEnabled || !SectionName.startswith(".debug_") || |
| 856 | SectionName == ".debug_frame") { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 857 | Asm.writeSectionData(W.OS, &Section, Layout); |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 858 | return; |
| 859 | } |
| 860 | |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 861 | assert((MAI->compressDebugSections() == DebugCompressionType::Z || |
| 862 | MAI->compressDebugSections() == DebugCompressionType::GNU) && |
| 863 | "expected zlib or zlib-gnu style compression"); |
| 864 | |
David Majnemer | 919f1f4 | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 865 | SmallVector<char, 128> UncompressedData; |
| 866 | raw_svector_ostream VecOS(UncompressedData); |
Peter Collingbourne | 74bda0b | 2018-05-21 18:11:35 +0000 | [diff] [blame] | 867 | Asm.writeSectionData(VecOS, &Section, Layout); |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 868 | |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 869 | SmallVector<char, 128> CompressedContents; |
George Rimar | ef935cd | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 870 | if (Error E = zlib::compress( |
| 871 | StringRef(UncompressedData.data(), UncompressedData.size()), |
| 872 | CompressedContents)) { |
| 873 | consumeError(std::move(E)); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 874 | W.OS << UncompressedData; |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 875 | return; |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 876 | } |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 877 | |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 878 | bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z; |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 879 | if (!maybeWriteCompression(UncompressedData.size(), CompressedContents, |
| 880 | ZlibStyle, Sec.getAlignment())) { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 881 | W.OS << UncompressedData; |
Rafael Espindola | a55dd52 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 882 | return; |
| 883 | } |
George Rimar | ec285af | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 884 | |
| 885 | if (ZlibStyle) |
| 886 | // Set the compressed flag. That is zlib style. |
| 887 | Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED); |
| 888 | else |
| 889 | // Add "z" prefix to section name. This is zlib-gnu style. |
Saleem Abdulrasool | e6be41f | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 890 | MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str()); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 891 | W.OS << CompressedContents; |
David Blaikie | fe25521 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 894 | void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 895 | uint64_t Address, uint64_t Offset, |
| 896 | uint64_t Size, uint32_t Link, uint32_t Info, |
| 897 | uint64_t Alignment, uint64_t EntrySize) { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 898 | W.write<uint32_t>(Name); // sh_name: index into string table |
| 899 | W.write<uint32_t>(Type); // sh_type |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 900 | WriteWord(Flags); // sh_flags |
| 901 | WriteWord(Address); // sh_addr |
| 902 | WriteWord(Offset); // sh_offset |
| 903 | WriteWord(Size); // sh_size |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 904 | W.write<uint32_t>(Link); // sh_link |
| 905 | W.write<uint32_t>(Info); // sh_info |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 906 | WriteWord(Alignment); // sh_addralign |
| 907 | WriteWord(EntrySize); // sh_entsize |
| 908 | } |
| 909 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 910 | void ELFWriter::writeRelocations(const MCAssembler &Asm, |
Rafael Espindola | 4689501 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 911 | const MCSectionELF &Sec) { |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 912 | std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec]; |
Akira Hatanaka | 00ca888 | 2012-03-23 23:06:45 +0000 | [diff] [blame] | 913 | |
Rafael Espindola | cdfe790 | 2015-12-17 16:22:06 +0000 | [diff] [blame] | 914 | // We record relocations by pushing to the end of a vector. Reverse the vector |
| 915 | // to get the relocations in the order they were created. |
| 916 | // In most cases that is not important, but it can be for special sections |
| 917 | // (.eh_frame) or specific relocations (TLS optimizations on SystemZ). |
| 918 | std::reverse(Relocs.begin(), Relocs.end()); |
Rafael Espindola | f50f96b | 2015-12-17 15:08:24 +0000 | [diff] [blame] | 919 | |
Rafael Espindola | cdfe790 | 2015-12-17 16:22:06 +0000 | [diff] [blame] | 920 | // Sort the relocation entries. MIPS needs this. |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 921 | OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 922 | |
| 923 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
Rafael Espindola | 224dbf4 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 924 | const ELFRelocationEntry &Entry = Relocs[e - i - 1]; |
Rafael Espindola | db8eb52 | 2015-05-28 20:53:09 +0000 | [diff] [blame] | 925 | unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0; |
Rafael Espindola | 224dbf4 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 926 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 927 | if (is64Bit()) { |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 928 | write(Entry.Offset); |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 929 | if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) { |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 930 | write(uint32_t(Index)); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 931 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 932 | write(OWriter.TargetObjectWriter->getRSsym(Entry.Type)); |
| 933 | write(OWriter.TargetObjectWriter->getRType3(Entry.Type)); |
| 934 | write(OWriter.TargetObjectWriter->getRType2(Entry.Type)); |
| 935 | write(OWriter.TargetObjectWriter->getRType(Entry.Type)); |
Rafael Espindola | 224dbf4 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 936 | } else { |
Jack Carter | 93ee286 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 937 | struct ELF::Elf64_Rela ERE64; |
Rafael Espindola | 224dbf4 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 938 | ERE64.setSymbolAndType(Index, Entry.Type); |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 939 | write(ERE64.r_info); |
Jack Carter | 93ee286 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 940 | } |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 941 | if (hasRelocationAddend()) |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 942 | write(Entry.Addend); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 943 | } else { |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 944 | write(uint32_t(Entry.Offset)); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 945 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 946 | struct ELF::Elf32_Rela ERE32; |
Rafael Espindola | 224dbf4 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 947 | ERE32.setSymbolAndType(Index, Entry.Type); |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 948 | write(ERE32.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 949 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 950 | if (hasRelocationAddend()) |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 951 | write(uint32_t(Entry.Addend)); |
Simon Atanasyan | cff5a9f | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 952 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 953 | if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) { |
| 954 | if (uint32_t RType = |
| 955 | OWriter.TargetObjectWriter->getRType2(Entry.Type)) { |
Simon Atanasyan | cff5a9f | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 956 | write(uint32_t(Entry.Offset)); |
| 957 | |
| 958 | ERE32.setSymbolAndType(0, RType); |
| 959 | write(ERE32.r_info); |
| 960 | write(uint32_t(0)); |
| 961 | } |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 962 | if (uint32_t RType = |
| 963 | OWriter.TargetObjectWriter->getRType3(Entry.Type)) { |
Simon Atanasyan | cff5a9f | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 964 | write(uint32_t(Entry.Offset)); |
| 965 | |
| 966 | ERE32.setSymbolAndType(0, RType); |
| 967 | write(ERE32.r_info); |
| 968 | write(uint32_t(0)); |
| 969 | } |
| 970 | } |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 971 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 975 | const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) { |
Rafael Espindola | ab78d67 | 2015-05-25 21:56:55 +0000 | [diff] [blame] | 976 | const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1]; |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 977 | StrTabBuilder.write(W.OS); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 978 | return StrtabSection; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 981 | void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap, |
| 982 | uint32_t GroupSymbolIndex, uint64_t Offset, |
| 983 | uint64_t Size, const MCSectionELF &Section) { |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 984 | uint64_t sh_link = 0; |
| 985 | uint64_t sh_info = 0; |
| 986 | |
| 987 | switch(Section.getType()) { |
Rafael Espindola | 6254dcc | 2015-04-06 03:16:51 +0000 | [diff] [blame] | 988 | default: |
| 989 | // Nothing to do. |
| 990 | break; |
| 991 | |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 992 | case ELF::SHT_DYNAMIC: |
Rafael Espindola | 15953ff | 2015-04-30 21:20:06 +0000 | [diff] [blame] | 993 | llvm_unreachable("SHT_DYNAMIC in a relocatable object"); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 994 | |
| 995 | case ELF::SHT_REL: |
| 996 | case ELF::SHT_RELA: { |
Rafael Espindola | 8fd9fc3 | 2015-02-17 20:37:50 +0000 | [diff] [blame] | 997 | sh_link = SymbolTableIndex; |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 998 | assert(sh_link && ".symtab not found"); |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 999 | const MCSection *InfoSection = Section.getAssociatedSection(); |
| 1000 | sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection)); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | } |
| 1003 | |
| 1004 | case ELF::SHT_SYMTAB: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1005 | sh_link = StringTableIndex; |
| 1006 | sh_info = LastLocalSymbolIndex; |
| 1007 | break; |
| 1008 | |
| 1009 | case ELF::SHT_SYMTAB_SHNDX: |
Michael J. Spencer | 56500c7 | 2018-06-02 16:33:01 +0000 | [diff] [blame] | 1010 | case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 1011 | case ELF::SHT_LLVM_ADDRSIG: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1012 | sh_link = SymbolTableIndex; |
| 1013 | break; |
| 1014 | |
Nick Lewycky | 4a8d43e | 2011-10-07 23:28:32 +0000 | [diff] [blame] | 1015 | case ELF::SHT_GROUP: |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1016 | sh_link = SymbolTableIndex; |
| 1017 | sh_info = GroupSymbolIndex; |
| 1018 | break; |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Evgeniy Stepanov | c67a9ef | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 1021 | if (Section.getFlags() & ELF::SHF_LINK_ORDER) { |
| 1022 | const MCSymbol *Sym = Section.getAssociatedSymbol(); |
| 1023 | const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection()); |
| 1024 | sh_link = SectionIndexMap.lookup(Sec); |
| 1025 | } |
Logan Chien | b0c8996 | 2013-02-05 14:18:59 +0000 | [diff] [blame] | 1026 | |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 1027 | WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()), |
Rafael Espindola | ad7c117 | 2015-05-21 19:36:43 +0000 | [diff] [blame] | 1028 | Section.getType(), Section.getFlags(), 0, Offset, Size, |
| 1029 | sh_link, sh_info, Section.getAlignment(), |
| 1030 | Section.getEntrySize()); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1033 | void ELFWriter::writeSectionHeader( |
Rafael Espindola | 4dbd603 | 2015-06-04 20:55:49 +0000 | [diff] [blame] | 1034 | const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap, |
Rafael Espindola | a34b8b2 | 2015-04-28 15:26:21 +0000 | [diff] [blame] | 1035 | const SectionOffsetsTy &SectionOffsets) { |
Rafael Espindola | d80f516 | 2015-04-30 21:10:06 +0000 | [diff] [blame] | 1036 | const unsigned NumSections = SectionTable.size(); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1037 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1038 | // Null section first. |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1039 | uint64_t FirstSectionSize = |
Rafael Espindola | 50b9357 | 2015-04-15 13:07:47 +0000 | [diff] [blame] | 1040 | (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0; |
Rafael Espindola | 58a854d | 2015-04-29 20:25:24 +0000 | [diff] [blame] | 1041 | WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1042 | |
Rafael Espindola | ab78d67 | 2015-05-25 21:56:55 +0000 | [diff] [blame] | 1043 | for (const MCSectionELF *Section : SectionTable) { |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1044 | uint32_t GroupSymbolIndex; |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1045 | unsigned Type = Section->getType(); |
| 1046 | if (Type != ELF::SHT_GROUP) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1047 | GroupSymbolIndex = 0; |
| 1048 | else |
Rafael Espindola | db8eb52 | 2015-05-28 20:53:09 +0000 | [diff] [blame] | 1049 | GroupSymbolIndex = Section->getGroup()->getIndex(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1050 | |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1051 | const std::pair<uint64_t, uint64_t> &Offsets = |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1052 | SectionOffsets.find(Section)->second; |
Rafael Espindola | 9a9984d | 2015-05-21 19:46:39 +0000 | [diff] [blame] | 1053 | uint64_t Size; |
Rafael Espindola | ea3533b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 1054 | if (Type == ELF::SHT_NOBITS) |
| 1055 | Size = Layout.getSectionAddressSize(Section); |
| 1056 | else |
Rafael Espindola | 9a9984d | 2015-05-21 19:46:39 +0000 | [diff] [blame] | 1057 | Size = Offsets.second - Offsets.first; |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1058 | |
Rafael Espindola | a127bd0 | 2015-05-21 19:42:35 +0000 | [diff] [blame] | 1059 | writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size, |
Rafael Espindola | ad7c117 | 2015-05-21 19:36:43 +0000 | [diff] [blame] | 1060 | *Section); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1064 | uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) { |
Peter Collingbourne | 0d4292f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 1065 | uint64_t StartOffset = W.OS.tell(); |
| 1066 | |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1067 | MCContext &Ctx = Asm.getContext(); |
Rafael Espindola | 7c4f3dc | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 1068 | MCSectionELF *StrtabSection = |
| 1069 | Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0); |
| 1070 | StringTableIndex = addToSectionTable(StrtabSection); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1071 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1072 | RevGroupMapTy RevGroupMap; |
| 1073 | SectionIndexMapTy SectionIndexMap; |
| 1074 | |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1075 | std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1076 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1077 | // Write out the ELF header ... |
Rafael Espindola | 840bb0f | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1078 | writeHeader(Asm); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1079 | |
Rafael Espindola | 541279f | 2015-04-08 11:41:24 +0000 | [diff] [blame] | 1080 | // ... then the sections ... |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1081 | SectionOffsetsTy SectionOffsets; |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1082 | std::vector<MCSectionELF *> Groups; |
| 1083 | std::vector<MCSectionELF *> Relocations; |
Rafael Espindola | a287fe7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 1084 | for (MCSection &Sec : Asm) { |
| 1085 | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1086 | if (Mode == NonDwoOnly && isDwoSection(Section)) |
| 1087 | continue; |
| 1088 | if (Mode == DwoOnly && !isDwoSection(Section)) |
| 1089 | continue; |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1090 | |
Rafael Espindola | aa4466d | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1091 | align(Section.getAlignment()); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1092 | |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1093 | // Remember the offset into the file for this section. |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1094 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 1095 | |
Rafael Espindola | 4cc59a1 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 1096 | const MCSymbolELF *SignatureSymbol = Section.getGroup(); |
Rafael Espindola | a287fe7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 1097 | writeSectionData(Asm, Section, Layout); |
Rafael Espindola | 1e95f21 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 1098 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1099 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1100 | SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd); |
| 1101 | |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1102 | MCSectionELF *RelSection = createRelocationSection(Ctx, Section); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (SignatureSymbol) { |
Rafael Espindola | f00654b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 1105 | Asm.registerSymbol(*SignatureSymbol); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1106 | unsigned &GroupIdx = RevGroupMap[SignatureSymbol]; |
| 1107 | if (!GroupIdx) { |
Rafael Espindola | 7521964 | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1108 | MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol); |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1109 | GroupIdx = addToSectionTable(Group); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1110 | Group->setAlignment(4); |
| 1111 | Groups.push_back(Group); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1112 | } |
Rafael Espindola | c1233a8 | 2015-06-05 17:54:25 +0000 | [diff] [blame] | 1113 | std::vector<const MCSectionELF *> &Members = |
| 1114 | GroupMembers[SignatureSymbol]; |
| 1115 | Members.push_back(&Section); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1116 | if (RelSection) |
Rafael Espindola | c1233a8 | 2015-06-05 17:54:25 +0000 | [diff] [blame] | 1117 | Members.push_back(RelSection); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1120 | SectionIndexMap[&Section] = addToSectionTable(&Section); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1121 | if (RelSection) { |
Rafael Espindola | b65870f | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1122 | SectionIndexMap[RelSection] = addToSectionTable(RelSection); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1123 | Relocations.push_back(RelSection); |
| 1124 | } |
Eric Christopher | 42eb082 | 2018-09-06 22:09:31 +0000 | [diff] [blame] | 1125 | |
| 1126 | OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Michael J. Spencer | 56500c7 | 2018-06-02 16:33:01 +0000 | [diff] [blame] | 1129 | MCSectionELF *CGProfileSection = nullptr; |
| 1130 | if (!Asm.CGProfile.empty()) { |
| 1131 | CGProfileSection = Ctx.getELFSection(".llvm.call-graph-profile", |
| 1132 | ELF::SHT_LLVM_CALL_GRAPH_PROFILE, |
| 1133 | ELF::SHF_EXCLUDE, 16, ""); |
| 1134 | SectionIndexMap[CGProfileSection] = addToSectionTable(CGProfileSection); |
| 1135 | } |
| 1136 | |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1137 | for (MCSectionELF *Group : Groups) { |
Rafael Espindola | aa4466d | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1138 | align(Group->getAlignment()); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Remember the offset into the file for this section. |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1141 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1142 | |
| 1143 | const MCSymbol *SignatureSymbol = Group->getGroup(); |
| 1144 | assert(SignatureSymbol); |
| 1145 | write(uint32_t(ELF::GRP_COMDAT)); |
| 1146 | for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) { |
| 1147 | uint32_t SecIndex = SectionIndexMap.lookup(Member); |
| 1148 | write(SecIndex); |
| 1149 | } |
| 1150 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1151 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1152 | SectionOffsets[Group] = std::make_pair(SecStart, SecEnd); |
| 1153 | } |
| 1154 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1155 | if (Mode == DwoOnly) { |
| 1156 | // dwo files don't have symbol tables or relocations, but they do have |
| 1157 | // string tables. |
| 1158 | StrTabBuilder.finalize(); |
| 1159 | } else { |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 1160 | MCSectionELF *AddrsigSection; |
| 1161 | if (OWriter.EmitAddrsigSection) { |
| 1162 | AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG, |
| 1163 | ELF::SHF_EXCLUDE); |
| 1164 | addToSectionTable(AddrsigSection); |
| 1165 | } |
| 1166 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1167 | // Compute symbol table information. |
| 1168 | computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, |
| 1169 | SectionOffsets); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1170 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1171 | for (MCSectionELF *RelSection : Relocations) { |
| 1172 | align(RelSection->getAlignment()); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1173 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1174 | // Remember the offset into the file for this section. |
| 1175 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1176 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1177 | writeRelocations(Asm, |
| 1178 | cast<MCSectionELF>(*RelSection->getAssociatedSection())); |
Rafael Espindola | fabbc49 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1179 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1180 | uint64_t SecEnd = W.OS.tell(); |
| 1181 | SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd); |
| 1182 | } |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (OWriter.EmitAddrsigSection) { |
| 1185 | uint64_t SecStart = W.OS.tell(); |
| 1186 | writeAddrsigSection(); |
| 1187 | uint64_t SecEnd = W.OS.tell(); |
| 1188 | SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd); |
| 1189 | } |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Michael J. Spencer | 56500c7 | 2018-06-02 16:33:01 +0000 | [diff] [blame] | 1192 | if (CGProfileSection) { |
| 1193 | uint64_t SecStart = W.OS.tell(); |
| 1194 | for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) { |
| 1195 | W.write<uint32_t>(CGPE.From->getSymbol().getIndex()); |
| 1196 | W.write<uint32_t>(CGPE.To->getSymbol().getIndex()); |
| 1197 | W.write<uint64_t>(CGPE.Count); |
| 1198 | } |
| 1199 | uint64_t SecEnd = W.OS.tell(); |
| 1200 | SectionOffsets[CGProfileSection] = std::make_pair(SecStart, SecEnd); |
| 1201 | } |
| 1202 | |
Rafael Espindola | 58a854d | 2015-04-29 20:25:24 +0000 | [diff] [blame] | 1203 | { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1204 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | d80f516 | 2015-04-30 21:10:06 +0000 | [diff] [blame] | 1205 | const MCSectionELF *Sec = createStringTable(Ctx); |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1206 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | 76e71bd | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1207 | SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd); |
Rafael Espindola | 4fa3a17 | 2015-04-29 20:34:31 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1210 | uint64_t NaturalAlignment = is64Bit() ? 8 : 4; |
Rafael Espindola | aa4466d | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1211 | align(NaturalAlignment); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1212 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1213 | const uint64_t SectionHeaderOffset = W.OS.tell(); |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1214 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1215 | // ... then the section header table ... |
Rafael Espindola | 4dbd603 | 2015-06-04 20:55:49 +0000 | [diff] [blame] | 1216 | writeSectionHeader(Layout, SectionIndexMap, SectionOffsets); |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1217 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1218 | uint16_t NumSections = support::endian::byte_swap<uint16_t>( |
| 1219 | (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF |
| 1220 | : SectionTable.size() + 1, |
| 1221 | W.Endian); |
Rafael Espindola | 840bb0f | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1222 | unsigned NumSectionsOffset; |
| 1223 | |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1224 | auto &Stream = static_cast<raw_pwrite_stream &>(W.OS); |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1225 | if (is64Bit()) { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1226 | uint64_t Val = |
| 1227 | support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian); |
| 1228 | Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
| 1229 | offsetof(ELF::Elf64_Ehdr, e_shoff)); |
Rafael Espindola | 840bb0f | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1230 | NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum); |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1231 | } else { |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1232 | uint32_t Val = |
| 1233 | support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian); |
| 1234 | Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
| 1235 | offsetof(ELF::Elf32_Ehdr, e_shoff)); |
Rafael Espindola | 840bb0f | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1236 | NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum); |
Rafael Espindola | 83ed6b7 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1237 | } |
Peter Collingbourne | 8e93a95 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1238 | Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections), |
| 1239 | NumSectionsOffset); |
Peter Collingbourne | 0d4292f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 1240 | |
| 1241 | return W.OS.tell() - StartOffset; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1244 | bool ELFObjectWriter::hasRelocationAddend() const { |
| 1245 | return TargetObjectWriter->hasRelocationAddend(); |
| 1246 | } |
| 1247 | |
| 1248 | void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
| 1249 | const MCAsmLayout &Layout) { |
| 1250 | // The presence of symbol versions causes undefined symbols and |
| 1251 | // versions declared with @@@ to be renamed. |
| 1252 | for (const std::pair<StringRef, const MCSymbol *> &P : Asm.Symvers) { |
| 1253 | StringRef AliasName = P.first; |
| 1254 | const auto &Symbol = cast<MCSymbolELF>(*P.second); |
| 1255 | size_t Pos = AliasName.find('@'); |
| 1256 | assert(Pos != StringRef::npos); |
| 1257 | |
| 1258 | StringRef Prefix = AliasName.substr(0, Pos); |
| 1259 | StringRef Rest = AliasName.substr(Pos); |
| 1260 | StringRef Tail = Rest; |
| 1261 | if (Rest.startswith("@@@")) |
| 1262 | Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1); |
| 1263 | |
| 1264 | auto *Alias = |
| 1265 | cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail)); |
| 1266 | Asm.registerSymbol(*Alias); |
| 1267 | const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext()); |
| 1268 | Alias->setVariableValue(Value); |
| 1269 | |
| 1270 | // Aliases defined with .symvar copy the binding from the symbol they alias. |
| 1271 | // This is the first place we are able to copy this information. |
| 1272 | Alias->setExternal(Symbol.isExternal()); |
| 1273 | Alias->setBinding(Symbol.getBinding()); |
| 1274 | |
| 1275 | if (!Symbol.isUndefined() && !Rest.startswith("@@@")) |
| 1276 | continue; |
| 1277 | |
Hans Wennborg | 2a7ad00 | 2019-02-18 10:39:35 +0000 | [diff] [blame] | 1278 | // FIXME: Get source locations for these errors or diagnose them earlier. |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1279 | if (Symbol.isUndefined() && Rest.startswith("@@") && |
Hans Wennborg | 2a7ad00 | 2019-02-18 10:39:35 +0000 | [diff] [blame] | 1280 | !Rest.startswith("@@@")) { |
| 1281 | Asm.getContext().reportError(SMLoc(), "versioned symbol " + AliasName + |
| 1282 | " must be defined"); |
| 1283 | continue; |
| 1284 | } |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1285 | |
Hans Wennborg | 2a7ad00 | 2019-02-18 10:39:35 +0000 | [diff] [blame] | 1286 | if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) { |
| 1287 | Asm.getContext().reportError( |
| 1288 | SMLoc(), llvm::Twine("multiple symbol versions defined for ") + |
| 1289 | Symbol.getName()); |
| 1290 | continue; |
| 1291 | } |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1292 | |
| 1293 | Renames.insert(std::make_pair(&Symbol, Alias)); |
| 1294 | } |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 1295 | |
| 1296 | for (const MCSymbol *&Sym : AddrsigSyms) { |
| 1297 | if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym))) |
| 1298 | Sym = R; |
Peter Collingbourne | f06afc3 | 2018-08-06 21:59:58 +0000 | [diff] [blame] | 1299 | if (Sym->isInSection() && Sym->getName().startswith(".L")) |
| 1300 | Sym = Sym->getSection().getBeginSymbol(); |
Peter Collingbourne | a29ff2e | 2018-07-17 22:17:18 +0000 | [diff] [blame] | 1301 | Sym->setUsedInReloc(); |
| 1302 | } |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | // It is always valid to create a relocation with a symbol. It is preferable |
| 1306 | // to use a relocation with a section if that is possible. Using the section |
| 1307 | // allows us to omit some local symbols from the symbol table. |
| 1308 | bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm, |
| 1309 | const MCSymbolRefExpr *RefA, |
| 1310 | const MCSymbolELF *Sym, |
| 1311 | uint64_t C, |
| 1312 | unsigned Type) const { |
| 1313 | // A PCRel relocation to an absolute value has no symbol (or section). We |
| 1314 | // represent that with a relocation to a null section. |
| 1315 | if (!RefA) |
| 1316 | return false; |
| 1317 | |
| 1318 | MCSymbolRefExpr::VariantKind Kind = RefA->getKind(); |
| 1319 | switch (Kind) { |
| 1320 | default: |
| 1321 | break; |
| 1322 | // The .odp creation emits a relocation against the symbol ".TOC." which |
| 1323 | // create a R_PPC64_TOC relocation. However the relocation symbol name |
| 1324 | // in final object creation should be NULL, since the symbol does not |
| 1325 | // really exist, it is just the reference to TOC base for the current |
| 1326 | // object file. Since the symbol is undefined, returning false results |
| 1327 | // in a relocation with a null section which is the desired result. |
| 1328 | case MCSymbolRefExpr::VK_PPC_TOCBASE: |
| 1329 | return false; |
| 1330 | |
| 1331 | // These VariantKind cause the relocation to refer to something other than |
| 1332 | // the symbol itself, like a linker generated table. Since the address of |
| 1333 | // symbol is not relevant, we cannot replace the symbol with the |
| 1334 | // section and patch the difference in the addend. |
| 1335 | case MCSymbolRefExpr::VK_GOT: |
| 1336 | case MCSymbolRefExpr::VK_PLT: |
| 1337 | case MCSymbolRefExpr::VK_GOTPCREL: |
| 1338 | case MCSymbolRefExpr::VK_PPC_GOT_LO: |
| 1339 | case MCSymbolRefExpr::VK_PPC_GOT_HI: |
| 1340 | case MCSymbolRefExpr::VK_PPC_GOT_HA: |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
| 1344 | // An undefined symbol is not in any section, so the relocation has to point |
| 1345 | // to the symbol itself. |
| 1346 | assert(Sym && "Expected a symbol"); |
| 1347 | if (Sym->isUndefined()) |
| 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 | // Global ELF symbols can be preempted by the dynamic linker. The relocation |
| 1363 | // has to point to the symbol for a reason analogous to the STB_WEAK case. |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
| 1367 | // If a relocation points to a mergeable section, we have to be careful. |
| 1368 | // If the offset is zero, a relocation with the section will encode the |
| 1369 | // same information. With a non-zero offset, the situation is different. |
| 1370 | // For example, a relocation can point 42 bytes past the end of a string. |
| 1371 | // If we change such a relocation to use the section, the linker would think |
| 1372 | // that it pointed to another string and subtracting 42 at runtime will |
| 1373 | // produce the wrong value. |
| 1374 | if (Sym->isInSection()) { |
| 1375 | auto &Sec = cast<MCSectionELF>(Sym->getSection()); |
| 1376 | unsigned Flags = Sec.getFlags(); |
| 1377 | if (Flags & ELF::SHF_MERGE) { |
| 1378 | if (C != 0) |
| 1379 | return true; |
| 1380 | |
| 1381 | // It looks like gold has a bug (http://sourceware.org/PR16794) and can |
| 1382 | // only handle section relocations to mergeable sections if using RELA. |
| 1383 | if (!hasRelocationAddend()) |
| 1384 | return true; |
| 1385 | } |
| 1386 | |
| 1387 | // Most TLS relocations use a got, so they need the symbol. Even those that |
| 1388 | // are just an offset (@tpoff), require a symbol in gold versions before |
| 1389 | // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed |
| 1390 | // http://sourceware.org/PR16773. |
| 1391 | if (Flags & ELF::SHF_TLS) |
| 1392 | return true; |
| 1393 | } |
| 1394 | |
| 1395 | // If the symbol is a thumb function the final relocation must set the lowest |
| 1396 | // bit. With a symbol that is done by just having the symbol have that bit |
| 1397 | // set, so we would lose the bit if we relocated with the section. |
| 1398 | // FIXME: We could use the section but add the bit to the relocation value. |
| 1399 | if (Asm.isThumbFunc(Sym)) |
| 1400 | return true; |
| 1401 | |
| 1402 | if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type)) |
| 1403 | return true; |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | void ELFObjectWriter::recordRelocation(MCAssembler &Asm, |
| 1408 | const MCAsmLayout &Layout, |
| 1409 | const MCFragment *Fragment, |
| 1410 | const MCFixup &Fixup, MCValue Target, |
| 1411 | uint64_t &FixedValue) { |
| 1412 | MCAsmBackend &Backend = Asm.getBackend(); |
| 1413 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 1414 | MCFixupKindInfo::FKF_IsPCRel; |
| 1415 | const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent()); |
| 1416 | uint64_t C = Target.getConstant(); |
| 1417 | uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 1418 | MCContext &Ctx = Asm.getContext(); |
| 1419 | |
| 1420 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 1421 | // Let A, B and C being the components of Target and R be the location of |
| 1422 | // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). |
| 1423 | // If it is pcrel, we want to compute (A - B + C - R). |
| 1424 | |
| 1425 | // In general, ELF has no relocations for -B. It can only represent (A + C) |
| 1426 | // or (A + C - R). If B = R + K and the relocation is not pcrel, we can |
| 1427 | // replace B to implement it: (A - R - K + C) |
| 1428 | if (IsPCRel) { |
| 1429 | Ctx.reportError( |
| 1430 | Fixup.getLoc(), |
| 1431 | "No relocation available to represent this relative expression"); |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol()); |
| 1436 | |
| 1437 | if (SymB.isUndefined()) { |
| 1438 | Ctx.reportError(Fixup.getLoc(), |
| 1439 | Twine("symbol '") + SymB.getName() + |
| 1440 | "' can not be undefined in a subtraction expression"); |
| 1441 | return; |
| 1442 | } |
| 1443 | |
| 1444 | assert(!SymB.isAbsolute() && "Should have been folded"); |
| 1445 | const MCSection &SecB = SymB.getSection(); |
| 1446 | if (&SecB != &FixupSection) { |
| 1447 | Ctx.reportError(Fixup.getLoc(), |
| 1448 | "Cannot represent a difference across sections"); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | uint64_t SymBOffset = Layout.getSymbolOffset(SymB); |
| 1453 | uint64_t K = SymBOffset - FixupOffset; |
| 1454 | IsPCRel = true; |
| 1455 | C -= K; |
| 1456 | } |
| 1457 | |
| 1458 | // We either rejected the fixup or folded B into C at this point. |
| 1459 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 1460 | const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr; |
| 1461 | |
| 1462 | bool ViaWeakRef = false; |
| 1463 | if (SymA && SymA->isVariable()) { |
| 1464 | const MCExpr *Expr = SymA->getVariableValue(); |
| 1465 | if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { |
| 1466 | if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) { |
| 1467 | SymA = cast<MCSymbolELF>(&Inner->getSymbol()); |
| 1468 | ViaWeakRef = true; |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel); |
| 1474 | uint64_t OriginalC = C; |
| 1475 | bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type); |
| 1476 | if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) |
| 1477 | C += Layout.getSymbolOffset(*SymA); |
| 1478 | |
| 1479 | uint64_t Addend = 0; |
| 1480 | if (hasRelocationAddend()) { |
| 1481 | Addend = C; |
| 1482 | C = 0; |
| 1483 | } |
| 1484 | |
| 1485 | FixedValue = C; |
| 1486 | |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1487 | const MCSectionELF *SecA = (SymA && SymA->isInSection()) |
| 1488 | ? cast<MCSectionELF>(&SymA->getSection()) |
| 1489 | : nullptr; |
| 1490 | if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA)) |
| 1491 | return; |
| 1492 | |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1493 | if (!RelocateWithSymbol) { |
Peter Collingbourne | 815bbef | 2018-05-21 19:18:28 +0000 | [diff] [blame] | 1494 | const auto *SectionSymbol = |
| 1495 | SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr; |
| 1496 | if (SectionSymbol) |
| 1497 | SectionSymbol->setUsedInReloc(); |
| 1498 | ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, |
| 1499 | OriginalC); |
| 1500 | Relocations[&FixupSection].push_back(Rec); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | const auto *RenamedSymA = SymA; |
| 1505 | if (SymA) { |
| 1506 | if (const MCSymbolELF *R = Renames.lookup(SymA)) |
| 1507 | RenamedSymA = R; |
| 1508 | |
| 1509 | if (ViaWeakRef) |
| 1510 | RenamedSymA->setIsWeakrefUsedInReloc(); |
| 1511 | else |
| 1512 | RenamedSymA->setUsedInReloc(); |
| 1513 | } |
| 1514 | ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, |
| 1515 | OriginalC); |
| 1516 | Relocations[&FixupSection].push_back(Rec); |
| 1517 | } |
| 1518 | |
Jim Grosbach | bc81286 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 1519 | bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 1520 | const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB, |
Rafael Espindola | fb118bd | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1521 | bool InSet, bool IsPCRel) const { |
Rafael Espindola | 569f382 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 1522 | const auto &SymA = cast<MCSymbolELF>(SA); |
Rafael Espindola | fb118bd | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1523 | if (IsPCRel) { |
| 1524 | assert(!InSet); |
Peter Collingbourne | b06c394 | 2017-04-08 23:35:49 +0000 | [diff] [blame] | 1525 | if (isWeak(SymA)) |
Rafael Espindola | fb118bd | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1526 | return false; |
| 1527 | } |
Jim Grosbach | bc81286 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 1528 | return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, |
Rafael Espindola | fb118bd | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1529 | InSet, IsPCRel); |
Eli Friedman | 78c1e17 | 2011-03-03 07:24:36 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Lang Hames | e471346 | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 1532 | std::unique_ptr<MCObjectWriter> |
Lang Hames | 37437af | 2017-10-09 23:53:15 +0000 | [diff] [blame] | 1533 | llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 1534 | raw_pwrite_stream &OS, bool IsLittleEndian) { |
Peter Collingbourne | 4ef1841 | 2018-05-21 19:30:59 +0000 | [diff] [blame] | 1535 | return llvm::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS, |
| 1536 | IsLittleEndian); |
Rafael Espindola | edae8e1 | 2011-12-21 17:30:17 +0000 | [diff] [blame] | 1537 | } |
Peter Collingbourne | 60fd99d | 2018-05-21 19:44:54 +0000 | [diff] [blame] | 1538 | |
| 1539 | std::unique_ptr<MCObjectWriter> |
| 1540 | llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 1541 | raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, |
| 1542 | bool IsLittleEndian) { |
| 1543 | return llvm::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS, |
| 1544 | IsLittleEndian); |
| 1545 | } |