Nick Lewycky | 4288f9e | 2013-03-09 09:32:16 +0000 | [diff] [blame] | 1 | //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===// |
Matt Fleming | 6c1ad48 | 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 | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ArrayRef.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 21 | #include "llvm/BinaryFormat/ELF.h" |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmBackend.h" |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmLayout.h" |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCAssembler.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCContext.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCELFObjectWriter.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCExpr.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCFixup.h" |
Rafael Espindola | ceecfe5b | 2017-07-11 23:56:10 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCFixupKindInfo.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCFragment.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCObjectWriter.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSection.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSectionELF.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCSymbolELF.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCValue.h" |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 38 | #include "llvm/MC/StringTableBuilder.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Allocator.h" |
| 40 | #include "llvm/Support/Casting.h" |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Compression.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Endian.h" |
George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Error.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Host.h" |
| 46 | #include "llvm/Support/MathExtras.h" |
| 47 | #include "llvm/Support/SMLoc.h" |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 48 | #include "llvm/Support/StringSaver.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 49 | #include "llvm/Support/SwapByteOrder.h" |
| 50 | #include "llvm/Support/raw_ostream.h" |
| 51 | #include <algorithm> |
| 52 | #include <cassert> |
| 53 | #include <cstddef> |
| 54 | #include <cstdint> |
| 55 | #include <map> |
| 56 | #include <memory> |
| 57 | #include <string> |
| 58 | #include <utility> |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 59 | #include <vector> |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 60 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 61 | using namespace llvm; |
| 62 | |
Jason W Kim | c09e726 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 63 | #undef DEBUG_TYPE |
| 64 | #define DEBUG_TYPE "reloc-info" |
| 65 | |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 66 | namespace { |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 67 | |
Eugene Zelenko | 7975b99 | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 68 | using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>; |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 69 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 70 | class ELFObjectWriter; |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 71 | struct ELFWriter; |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 72 | |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 73 | class SymbolTableWriter { |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 74 | ELFWriter &EWriter; |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 75 | bool Is64Bit; |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 76 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 77 | // indexes we are going to write to .symtab_shndx. |
| 78 | std::vector<uint32_t> ShndxIndexes; |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 79 | |
| 80 | // The numbel of symbols written so far. |
| 81 | unsigned NumWritten; |
| 82 | |
| 83 | void createSymtabShndx(); |
| 84 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 85 | template <typename T> void write(T Value); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 86 | |
| 87 | public: |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 88 | SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 89 | |
| 90 | void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size, |
| 91 | uint8_t other, uint32_t shndx, bool Reserved); |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 92 | |
| 93 | ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; } |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 96 | struct ELFWriter { |
| 97 | ELFObjectWriter &OWriter; |
| 98 | support::endian::Writer W; |
| 99 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 100 | static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout); |
| 101 | static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol, |
| 102 | bool Used, bool Renamed); |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 103 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 104 | /// Helper struct for containing some precomputed information on symbols. |
| 105 | struct ELFSymbolData { |
| 106 | const MCSymbolELF *Symbol; |
| 107 | uint32_t SectionIndex; |
| 108 | StringRef Name; |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 109 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 110 | // Support lexicographic sorting. |
| 111 | bool operator<(const ELFSymbolData &RHS) const { |
| 112 | unsigned LHSType = Symbol->getType(); |
| 113 | unsigned RHSType = RHS.Symbol->getType(); |
| 114 | if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION) |
| 115 | return false; |
| 116 | if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
| 117 | return true; |
| 118 | if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION) |
| 119 | return SectionIndex < RHS.SectionIndex; |
| 120 | return Name < RHS.Name; |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 121 | } |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 122 | }; |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 123 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 124 | /// @} |
| 125 | /// @name Symbol Table Data |
| 126 | /// @{ |
| 127 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 128 | StringTableBuilder StrTabBuilder{StringTableBuilder::ELF}; |
| 129 | |
| 130 | /// @} |
| 131 | |
| 132 | // This holds the symbol table index of the last local symbol. |
| 133 | unsigned LastLocalSymbolIndex; |
| 134 | // This holds the .strtab section index. |
| 135 | unsigned StringTableIndex; |
| 136 | // This holds the .symtab section index. |
| 137 | unsigned SymbolTableIndex; |
| 138 | |
| 139 | // Sections in the order they are to be output in the section table. |
| 140 | std::vector<const MCSectionELF *> SectionTable; |
| 141 | unsigned addToSectionTable(const MCSectionELF *Sec); |
| 142 | |
| 143 | // TargetObjectWriter wrappers. |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 144 | bool is64Bit() const; |
| 145 | bool hasRelocationAddend() const; |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 146 | |
| 147 | void align(unsigned Alignment); |
| 148 | |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 149 | bool maybeWriteCompression(uint64_t Size, |
| 150 | SmallVectorImpl<char> &CompressedContents, |
| 151 | bool ZLibStyle, unsigned Alignment); |
| 152 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 153 | public: |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 154 | ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS, |
| 155 | bool IsLittleEndian) |
| 156 | : OWriter(OWriter), |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 157 | W(OS, IsLittleEndian ? support::little : support::big) {} |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 158 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 159 | void WriteWord(uint64_t Word) { |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 160 | if (is64Bit()) |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 161 | W.write<uint64_t>(Word); |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 162 | else |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 163 | W.write<uint32_t>(Word); |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | template <typename T> void write(T Val) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 167 | W.write(Val); |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void writeHeader(const MCAssembler &Asm); |
| 171 | |
| 172 | void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, |
| 173 | ELFSymbolData &MSD, const MCAsmLayout &Layout); |
| 174 | |
| 175 | // Start and end offset of each section |
Eugene Zelenko | 7975b99 | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 176 | using SectionOffsetsTy = |
| 177 | std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>; |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 178 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 179 | // Map from a signature symbol to the group section index |
Eugene Zelenko | 7975b99 | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 180 | using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>; |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 181 | |
| 182 | /// Compute the symbol table data |
| 183 | /// |
| 184 | /// \param Asm - The assembler. |
| 185 | /// \param SectionIndexMap - Maps a section to its index. |
| 186 | /// \param RevGroupMap - Maps a signature symbol to the group section. |
| 187 | void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 188 | const SectionIndexMapTy &SectionIndexMap, |
| 189 | const RevGroupMapTy &RevGroupMap, |
| 190 | SectionOffsetsTy &SectionOffsets); |
| 191 | |
| 192 | MCSectionELF *createRelocationSection(MCContext &Ctx, |
| 193 | const MCSectionELF &Sec); |
| 194 | |
| 195 | const MCSectionELF *createStringTable(MCContext &Ctx); |
| 196 | |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 197 | void writeSectionHeader(const MCAsmLayout &Layout, |
| 198 | const SectionIndexMapTy &SectionIndexMap, |
| 199 | const SectionOffsetsTy &SectionOffsets); |
| 200 | |
| 201 | void writeSectionData(const MCAssembler &Asm, MCSection &Sec, |
| 202 | const MCAsmLayout &Layout); |
| 203 | |
| 204 | void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 205 | uint64_t Address, uint64_t Offset, uint64_t Size, |
| 206 | uint32_t Link, uint32_t Info, uint64_t Alignment, |
| 207 | uint64_t EntrySize); |
| 208 | |
| 209 | void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec); |
| 210 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 211 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout); |
David Blaikie | a0fa262 | 2016-04-12 21:45:53 +0000 | [diff] [blame] | 212 | void writeSection(const SectionIndexMapTy &SectionIndexMap, |
| 213 | uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size, |
| 214 | const MCSectionELF &Section); |
| 215 | }; |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 216 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 217 | class ELFObjectWriter : public MCObjectWriter { |
| 218 | raw_pwrite_stream &OS; |
| 219 | bool IsLittleEndian; |
| 220 | |
| 221 | /// The target specific ELF writer instance. |
| 222 | std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter; |
| 223 | |
| 224 | DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations; |
| 225 | |
| 226 | DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames; |
| 227 | |
| 228 | bool hasRelocationAddend() const; |
| 229 | |
| 230 | bool shouldRelocateWithSymbol(const MCAssembler &Asm, |
| 231 | const MCSymbolRefExpr *RefA, |
| 232 | const MCSymbolELF *Sym, uint64_t C, |
| 233 | unsigned Type) const; |
| 234 | |
| 235 | public: |
| 236 | ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 237 | raw_pwrite_stream &OS, bool IsLittleEndian) |
| 238 | : OS(OS), IsLittleEndian(IsLittleEndian), |
| 239 | TargetObjectWriter(std::move(MOTW)) {} |
| 240 | |
| 241 | void reset() override { |
| 242 | Relocations.clear(); |
| 243 | Renames.clear(); |
| 244 | MCObjectWriter::reset(); |
| 245 | } |
| 246 | |
| 247 | bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 248 | const MCSymbol &SymA, |
| 249 | const MCFragment &FB, bool InSet, |
| 250 | bool IsPCRel) const override; |
| 251 | |
| 252 | void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, |
| 253 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 254 | MCValue Target, uint64_t &FixedValue) override; |
| 255 | |
| 256 | uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override { |
| 257 | return ELFWriter(*this, OS, IsLittleEndian).writeObject(Asm, Layout); |
| 258 | } |
| 259 | |
| 260 | void executePostLayoutBinding(MCAssembler &Asm, |
| 261 | const MCAsmLayout &Layout) override; |
| 262 | |
| 263 | friend struct ELFWriter; |
| 264 | }; |
| 265 | |
Eugene Zelenko | ecefe5a | 2016-02-02 18:20:45 +0000 | [diff] [blame] | 266 | } // end anonymous namespace |
Rafael Espindola | 29abd97 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 267 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 268 | void ELFWriter::align(unsigned Alignment) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 269 | uint64_t Padding = OffsetToAlignment(W.OS.tell(), Alignment); |
| 270 | W.OS.write_zeros(Padding); |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 273 | unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) { |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 274 | SectionTable.push_back(Sec); |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 275 | StrTabBuilder.add(Sec->getSectionName()); |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 276 | return SectionTable.size(); |
| 277 | } |
| 278 | |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 279 | void SymbolTableWriter::createSymtabShndx() { |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 280 | if (!ShndxIndexes.empty()) |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 281 | return; |
| 282 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 283 | ShndxIndexes.resize(NumWritten); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 286 | template <typename T> void SymbolTableWriter::write(T Value) { |
| 287 | EWriter.write(Value); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 290 | SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit) |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 291 | : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {} |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 292 | |
| 293 | void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value, |
| 294 | uint64_t size, uint8_t other, |
| 295 | uint32_t shndx, bool Reserved) { |
| 296 | bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved; |
| 297 | |
| 298 | if (LargeIndex) |
| 299 | createSymtabShndx(); |
| 300 | |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 301 | if (!ShndxIndexes.empty()) { |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 302 | if (LargeIndex) |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 303 | ShndxIndexes.push_back(shndx); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 304 | else |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 305 | ShndxIndexes.push_back(0); |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx; |
| 309 | |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 310 | if (Is64Bit) { |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 311 | write(name); // st_name |
| 312 | write(info); // st_info |
| 313 | write(other); // st_other |
| 314 | write(Index); // st_shndx |
| 315 | write(value); // st_value |
| 316 | write(size); // st_size |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 317 | } else { |
Rafael Espindola | 91fd277 | 2015-04-29 21:09:32 +0000 | [diff] [blame] | 318 | write(name); // st_name |
| 319 | write(uint32_t(value)); // st_value |
| 320 | write(uint32_t(size)); // st_size |
| 321 | write(info); // st_info |
| 322 | write(other); // st_other |
| 323 | write(Index); // st_shndx |
Rafael Espindola | 10be083 | 2014-03-25 23:44:25 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | ++NumWritten; |
| 327 | } |
| 328 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 329 | bool ELFWriter::is64Bit() const { |
| 330 | return OWriter.TargetObjectWriter->is64Bit(); |
| 331 | } |
| 332 | |
| 333 | bool ELFWriter::hasRelocationAddend() const { |
| 334 | return OWriter.hasRelocationAddend(); |
| 335 | } |
| 336 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 337 | // Emit the ELF header. |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 338 | void ELFWriter::writeHeader(const MCAssembler &Asm) { |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 339 | // ELF Header |
| 340 | // ---------- |
| 341 | // |
| 342 | // Note |
| 343 | // ---- |
| 344 | // emitWord method behaves differently for ELF32 and ELF64, writing |
| 345 | // 4 bytes in the former and 8 in the latter. |
| 346 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 347 | W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3] |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 348 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 349 | W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 350 | |
| 351 | // e_ident[EI_DATA] |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 352 | W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB |
| 353 | : ELF::ELFDATA2MSB); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 354 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 355 | W.OS << char(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 3b727f5 | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 356 | // e_ident[EI_OSABI] |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 357 | W.OS << char(OWriter.TargetObjectWriter->getOSABI()); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 358 | W.OS << char(0); // e_ident[EI_ABIVERSION] |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 359 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 360 | W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 361 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 362 | W.write<uint16_t>(ELF::ET_REL); // e_type |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 363 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 364 | W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 365 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 366 | W.write<uint32_t>(ELF::EV_CURRENT); // e_version |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 367 | WriteWord(0); // e_entry, no entry point in .o file |
| 368 | WriteWord(0); // e_phoff, no program header for .o |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 369 | WriteWord(0); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 370 | |
Jason W Kim | 4761fba | 2011-02-04 21:41:11 +0000 | [diff] [blame] | 371 | // e_flags = whatever the target wants |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 372 | W.write<uint32_t>(Asm.getELFHeaderEFlags()); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 373 | |
| 374 | // e_ehsize = ELF header size |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 375 | W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr) |
| 376 | : sizeof(ELF::Elf32_Ehdr)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 377 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 378 | W.write<uint16_t>(0); // e_phentsize = prog header entry size |
| 379 | W.write<uint16_t>(0); // e_phnum = # prog header entries = 0 |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 380 | |
| 381 | // e_shentsize = Section header entry size |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 382 | W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr) |
| 383 | : sizeof(ELF::Elf32_Shdr)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 384 | |
| 385 | // e_shnum = # of section header ents |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 386 | W.write<uint16_t>(0); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 387 | |
| 388 | // e_shstrndx = Section # of '.shstrtab' |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 389 | assert(StringTableIndex < ELF::SHN_LORESERVE); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 390 | W.write<uint16_t>(StringTableIndex); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 393 | uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym, |
| 394 | const MCAsmLayout &Layout) { |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 395 | if (Sym.isCommon() && Sym.isExternal()) |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 396 | return Sym.getCommonAlignment(); |
Rafael Espindola | 9735f4f | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 397 | |
Rafael Espindola | fee224f | 2014-04-30 21:51:13 +0000 | [diff] [blame] | 398 | uint64_t Res; |
Duncan P. N. Exon Smith | 469f9db | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 399 | if (!Layout.getSymbolOffset(Sym, Res)) |
Rafael Espindola | 553e5eb | 2014-04-30 16:59:35 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | |
Duncan P. N. Exon Smith | 469f9db | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 402 | if (Layout.getAssembler().isThumbFunc(&Sym)) |
Rafael Espindola | 66f96fe | 2014-03-21 22:00:29 +0000 | [diff] [blame] | 403 | Res |= 1; |
Rafael Espindola | 9735f4f | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 404 | |
Rafael Espindola | 66f96fe | 2014-03-21 22:00:29 +0000 | [diff] [blame] | 405 | return Res; |
Rafael Espindola | 9735f4f | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Roman Divacky | 5a1c549 | 2014-01-07 20:17:03 +0000 | [diff] [blame] | 408 | static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) { |
| 409 | uint8_t Type = newType; |
| 410 | |
| 411 | // Propagation rules: |
| 412 | // IFUNC > FUNC > OBJECT > NOTYPE |
| 413 | // TLS_OBJECT > OBJECT > NOTYPE |
| 414 | // |
| 415 | // dont let the new type degrade the old type |
| 416 | switch (origType) { |
| 417 | default: |
| 418 | break; |
| 419 | case ELF::STT_GNU_IFUNC: |
| 420 | if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT || |
| 421 | Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS) |
| 422 | Type = ELF::STT_GNU_IFUNC; |
| 423 | break; |
| 424 | case ELF::STT_FUNC: |
| 425 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
| 426 | Type == ELF::STT_TLS) |
| 427 | Type = ELF::STT_FUNC; |
| 428 | break; |
| 429 | case ELF::STT_OBJECT: |
| 430 | if (Type == ELF::STT_NOTYPE) |
| 431 | Type = ELF::STT_OBJECT; |
| 432 | break; |
| 433 | case ELF::STT_TLS: |
| 434 | if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE || |
| 435 | Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC) |
| 436 | Type = ELF::STT_TLS; |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | return Type; |
| 441 | } |
| 442 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 443 | void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, |
| 444 | ELFSymbolData &MSD, const MCAsmLayout &Layout) { |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 445 | const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol); |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 446 | const MCSymbolELF *Base = |
| 447 | cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol)); |
Rafael Espindola | 85a8491 | 2014-03-26 00:16:43 +0000 | [diff] [blame] | 448 | |
| 449 | // This has to be in sync with when computeSymbolTable uses SHN_ABS or |
| 450 | // SHN_COMMON. |
Rafael Espindola | 4d37b2a | 2015-05-29 21:45:01 +0000 | [diff] [blame] | 451 | bool IsReserved = !Base || Symbol.isCommon(); |
Rafael Espindola | 3fe87a1 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 452 | |
Jack Carter | 2f8d9d9 | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 453 | // Binding and Type share the same byte as upper and lower nibbles |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 454 | uint8_t Binding = Symbol.getBinding(); |
| 455 | uint8_t Type = Symbol.getType(); |
Rafael Espindola | a6e3a59 | 2014-03-23 03:33:20 +0000 | [diff] [blame] | 456 | if (Base) { |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 457 | Type = mergeTypeForSet(Type, Base->getType()); |
Rafael Espindola | a6e3a59 | 2014-03-23 03:33:20 +0000 | [diff] [blame] | 458 | } |
Rafael Espindola | f8794ff | 2015-06-04 00:47:43 +0000 | [diff] [blame] | 459 | uint8_t Info = (Binding << 4) | Type; |
Jack Carter | 2f8d9d9 | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 460 | |
Joerg Sonnenberger | fc18473 | 2013-10-29 01:06:17 +0000 | [diff] [blame] | 461 | // Other and Visibility share the same byte with Visibility using the lower |
Jack Carter | 2f8d9d9 | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 462 | // 2 bits |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 463 | uint8_t Visibility = Symbol.getVisibility(); |
Rafael Espindola | 8c006ee | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 464 | uint8_t Other = Symbol.getOther() | Visibility; |
Rafael Espindola | 5f2d6a5 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 465 | |
Duncan P. N. Exon Smith | 469f9db | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 466 | uint64_t Value = SymbolValue(*MSD.Symbol, Layout); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 467 | uint64_t Size = 0; |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 468 | |
Rafael Espindola | 7c23cba | 2015-05-29 17:24:52 +0000 | [diff] [blame] | 469 | const MCExpr *ESize = MSD.Symbol->getSize(); |
Rafael Espindola | a041ef1 | 2014-03-27 00:28:24 +0000 | [diff] [blame] | 470 | if (!ESize && Base) |
Rafael Espindola | 7c23cba | 2015-05-29 17:24:52 +0000 | [diff] [blame] | 471 | ESize = Base->getSize(); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 472 | |
Rafael Espindola | 73c0ae7 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 473 | if (ESize) { |
| 474 | int64_t Res; |
Rafael Espindola | 94a88d7 | 2015-04-06 15:27:57 +0000 | [diff] [blame] | 475 | if (!ESize->evaluateKnownAbsolute(Res, Layout)) |
Rafael Espindola | 73c0ae7 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 476 | report_fatal_error("Size expression must be absolute."); |
| 477 | Size = Res; |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // Write out the symbol table entry |
Rafael Espindola | e48421f | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 481 | Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex, |
| 482 | IsReserved); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Rafael Espindola | db8a586 | 2015-04-17 20:05:17 +0000 | [diff] [blame] | 485 | // True if the assembler knows nothing about the final value of the symbol. |
| 486 | // This doesn't cover the comdat issues, since in those cases the assembler |
| 487 | // can at least know that all symbols in the section will move together. |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 488 | static bool isWeak(const MCSymbolELF &Sym) { |
| 489 | if (Sym.getType() == ELF::STT_GNU_IFUNC) |
Rafael Espindola | a635d83 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 490 | return true; |
| 491 | |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 492 | switch (Sym.getBinding()) { |
Rafael Espindola | a635d83 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 493 | default: |
| 494 | llvm_unreachable("Unknown binding"); |
| 495 | case ELF::STB_LOCAL: |
| 496 | return false; |
| 497 | case ELF::STB_GLOBAL: |
Rafael Espindola | db8a586 | 2015-04-17 20:05:17 +0000 | [diff] [blame] | 498 | return false; |
Rafael Espindola | a635d83 | 2015-04-17 08:46:11 +0000 | [diff] [blame] | 499 | case ELF::STB_WEAK: |
| 500 | case ELF::STB_GNU_UNIQUE: |
| 501 | return true; |
| 502 | } |
Rafael Espindola | c9e7068 | 2015-03-24 23:48:44 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 505 | bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol, |
| 506 | bool Used, bool Renamed) { |
Rafael Espindola | 7fadc0e | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 507 | if (Symbol.isVariable()) { |
| 508 | const MCExpr *Expr = Symbol.getVariableValue(); |
| 509 | if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) { |
| 510 | if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF) |
| 511 | return false; |
| 512 | } |
| 513 | } |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 514 | |
Rafael Espindola | ee8d151 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 515 | if (Used) |
| 516 | return true; |
| 517 | |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 518 | if (Renamed) |
| 519 | return false; |
| 520 | |
Rafael Espindola | ef1e863 | 2015-06-03 21:23:21 +0000 | [diff] [blame] | 521 | if (Symbol.isVariable() && Symbol.isUndefined()) { |
| 522 | // FIXME: this is here just to diagnose the case of a var = commmon_sym. |
| 523 | Layout.getBaseSymbol(Symbol); |
| 524 | return false; |
Rafael Espindola | 3b5ee55 | 2014-04-28 13:39:57 +0000 | [diff] [blame] | 525 | } |
Rafael Espindola | 9e18e96 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 526 | |
Rafael Espindola | ef1e863 | 2015-06-03 21:23:21 +0000 | [diff] [blame] | 527 | if (Symbol.isUndefined() && !Symbol.isBindingSet()) |
Rafael Espindola | a5efd6a | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 528 | return false; |
| 529 | |
Rafael Espindola | ee8d151 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 530 | if (Symbol.isTemporary()) |
Rafael Espindola | b1d0789 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 531 | return false; |
| 532 | |
Rafael Espindola | a401eee | 2015-06-04 15:33:30 +0000 | [diff] [blame] | 533 | if (Symbol.getType() == ELF::STT_SECTION) |
| 534 | return false; |
| 535 | |
Rafael Espindola | b1d0789 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 536 | return true; |
| 537 | } |
| 538 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 539 | void ELFWriter::computeSymbolTable( |
Rafael Espindola | ea1b394 | 2015-04-07 19:00:17 +0000 | [diff] [blame] | 540 | MCAssembler &Asm, const MCAsmLayout &Layout, |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 541 | const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap, |
| 542 | SectionOffsetsTy &SectionOffsets) { |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 543 | MCContext &Ctx = Asm.getContext(); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 544 | SymbolTableWriter Writer(*this, is64Bit()); |
| 545 | |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 546 | // Symbol table |
| 547 | unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
| 548 | MCSectionELF *SymtabSection = |
| 549 | Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, ""); |
| 550 | SymtabSection->setAlignment(is64Bit() ? 8 : 4); |
| 551 | SymbolTableIndex = addToSectionTable(SymtabSection); |
| 552 | |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 553 | align(SymtabSection->getAlignment()); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 554 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 555 | |
| 556 | // The first entry is the undefined symbol entry. |
| 557 | Writer.writeSymbol(0, 0, 0, 0, 0, 0, false); |
| 558 | |
Rafael Espindola | cfbd35c | 2015-05-28 20:11:34 +0000 | [diff] [blame] | 559 | std::vector<ELFSymbolData> LocalSymbolData; |
| 560 | std::vector<ELFSymbolData> ExternalSymbolData; |
Rafael Espindola | cfbd35c | 2015-05-28 20:11:34 +0000 | [diff] [blame] | 561 | |
Rafael Espindola | bee6e9f | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 562 | // Add the data for the symbols. |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 563 | bool HasLargeSectionIndex = false; |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 564 | for (const MCSymbol &S : Asm.symbols()) { |
| 565 | const auto &Symbol = cast<MCSymbolELF>(S); |
Rafael Espindola | ada43f6 | 2015-06-03 21:30:10 +0000 | [diff] [blame] | 566 | bool Used = Symbol.isUsedInReloc(); |
Rafael Espindola | 212fdde | 2015-06-03 21:52:06 +0000 | [diff] [blame] | 567 | bool WeakrefUsed = Symbol.isWeakrefUsedInReloc(); |
Rafael Espindola | 8c52a9b | 2015-06-03 21:41:59 +0000 | [diff] [blame] | 568 | bool isSignature = Symbol.isSignature(); |
Rafael Espindola | 7d0ba34 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 569 | |
Duncan P. N. Exon Smith | 469f9db | 2015-05-20 04:39:01 +0000 | [diff] [blame] | 570 | if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature, |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 571 | OWriter.Renames.count(&Symbol))) |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 572 | continue; |
| 573 | |
Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 574 | if (Symbol.isTemporary() && Symbol.isUndefined()) { |
| 575 | Ctx.reportError(SMLoc(), "Undefined temporary symbol"); |
| 576 | continue; |
| 577 | } |
Rafael Espindola | 6dff814 | 2015-06-25 20:10:45 +0000 | [diff] [blame] | 578 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 579 | ELFSymbolData MSD; |
Rafael Espindola | a869576 | 2015-06-02 00:25:12 +0000 | [diff] [blame] | 580 | MSD.Symbol = cast<MCSymbolELF>(&Symbol); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 581 | |
Rafael Espindola | 8c52a9b | 2015-06-03 21:41:59 +0000 | [diff] [blame] | 582 | bool Local = Symbol.getBinding() == ELF::STB_LOCAL; |
Rafael Espindola | 6dff814 | 2015-06-25 20:10:45 +0000 | [diff] [blame] | 583 | assert(Local || !Symbol.isTemporary()); |
| 584 | |
Rafael Espindola | f4b4430 | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 585 | if (Symbol.isAbsolute()) { |
Rafael Espindola | 022bb76 | 2014-03-24 03:43:21 +0000 | [diff] [blame] | 586 | MSD.SectionIndex = ELF::SHN_ABS; |
Rafael Espindola | 1467250 | 2015-05-29 17:48:04 +0000 | [diff] [blame] | 587 | } else if (Symbol.isCommon()) { |
Rafael Espindola | bee6e9f | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 588 | assert(!Local); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 589 | MSD.SectionIndex = ELF::SHN_COMMON; |
Rafael Espindola | f4b4430 | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 590 | } else if (Symbol.isUndefined()) { |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 591 | if (isSignature && !Used) { |
Rafael Espindola | 89feff3 | 2015-04-28 22:59:58 +0000 | [diff] [blame] | 592 | MSD.SectionIndex = RevGroupMap.lookup(&Symbol); |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 593 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 594 | HasLargeSectionIndex = true; |
| 595 | } else { |
Rafael Espindola | 7d0ba34 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 596 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 597 | } |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 598 | } else { |
Rafael Espindola | d634003 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 599 | const MCSectionELF &Section = |
Rafael Espindola | f4b4430 | 2015-05-29 15:07:27 +0000 | [diff] [blame] | 600 | static_cast<const MCSectionELF &>(Symbol.getSection()); |
Rafael Espindola | d634003 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 601 | MSD.SectionIndex = SectionIndexMap.lookup(&Section); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 602 | assert(MSD.SectionIndex && "Invalid section index!"); |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 603 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 604 | HasLargeSectionIndex = true; |
Rafael Espindola | fbcf0db | 2010-10-15 15:39:06 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 607 | StringRef Name = Symbol.getName(); |
Rafael Espindola | b661302 | 2014-10-17 01:48:58 +0000 | [diff] [blame] | 608 | |
| 609 | // Sections have their own string table |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 610 | if (Symbol.getType() != ELF::STT_SECTION) { |
| 611 | MSD.Name = Name; |
| 612 | StrTabBuilder.add(Name); |
| 613 | } |
Rafael Espindola | eb0c2c1 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 614 | |
Rafael Espindola | 10d2387 | 2015-05-29 14:20:40 +0000 | [diff] [blame] | 615 | if (Local) |
Rafael Espindola | a5efd6a | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 616 | LocalSymbolData.push_back(MSD); |
| 617 | else |
| 618 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Rafael Espindola | 2cd1951 | 2015-07-02 16:59:57 +0000 | [diff] [blame] | 621 | // This holds the .symtab_shndx section index. |
| 622 | unsigned SymtabShndxSectionIndex = 0; |
| 623 | |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 624 | if (HasLargeSectionIndex) { |
| 625 | MCSectionELF *SymtabShndxSection = |
| 626 | Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, ""); |
| 627 | SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection); |
| 628 | SymtabShndxSection->setAlignment(4); |
| 629 | } |
| 630 | |
Rafael Espindola | 1fd3627 | 2015-05-28 19:29:15 +0000 | [diff] [blame] | 631 | ArrayRef<std::string> FileNames = Asm.getFileNames(); |
| 632 | for (const std::string &Name : FileNames) |
Rafael Espindola | 66f3c9c | 2015-05-28 18:03:20 +0000 | [diff] [blame] | 633 | StrTabBuilder.add(Name); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 634 | |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 635 | StrTabBuilder.finalize(); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 636 | |
Peter Collingbourne | 5b75fd9 | 2017-03-03 21:22:06 +0000 | [diff] [blame] | 637 | // File symbols are emitted first and handled separately from normal symbols, |
| 638 | // i.e. a non-STT_FILE symbol with the same name may appear. |
Rafael Espindola | 0cbea29 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 639 | for (const std::string &Name : FileNames) |
| 640 | Writer.writeSymbol(StrTabBuilder.getOffset(Name), |
| 641 | ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT, |
| 642 | ELF::SHN_ABS, true); |
| 643 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 644 | // Symbols are required to be in lexicographic order. |
| 645 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 646 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 647 | |
| 648 | // Set the symbol indices. Local symbols must come before all other |
| 649 | // symbols with non-local bindings. |
Rafael Espindola | 1fd3627 | 2015-05-28 19:29:15 +0000 | [diff] [blame] | 650 | unsigned Index = FileNames.size() + 1; |
Rafael Espindola | 0e3decf | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 651 | |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 652 | for (ELFSymbolData &MSD : LocalSymbolData) { |
Daniel Jasper | 41de802 | 2015-06-23 11:31:32 +0000 | [diff] [blame] | 653 | unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION |
| 654 | ? 0 |
| 655 | : StrTabBuilder.getOffset(MSD.Name); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 656 | MSD.Symbol->setIndex(Index++); |
Rafael Espindola | e48421f | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 657 | writeSymbol(Writer, StringIndex, MSD, Layout); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 658 | } |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 659 | |
| 660 | // Write the symbol table entries. |
Rafael Espindola | 0cbea29 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 661 | LastLocalSymbolIndex = Index; |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 662 | |
Rafael Espindola | dcda997 | 2015-05-28 19:43:20 +0000 | [diff] [blame] | 663 | for (ELFSymbolData &MSD : ExternalSymbolData) { |
Rafael Espindola | e48421f | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 664 | unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name); |
Rafael Espindola | 0cbea29 | 2015-05-28 20:00:13 +0000 | [diff] [blame] | 665 | MSD.Symbol->setIndex(Index++); |
Rafael Espindola | e48421f | 2015-05-28 20:25:29 +0000 | [diff] [blame] | 666 | writeSymbol(Writer, StringIndex, MSD, Layout); |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 667 | assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 668 | } |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 669 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 670 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 671 | SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd); |
| 672 | |
| 673 | ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes(); |
| 674 | if (ShndxIndexes.empty()) { |
| 675 | assert(SymtabShndxSectionIndex == 0); |
| 676 | return; |
| 677 | } |
| 678 | assert(SymtabShndxSectionIndex != 0); |
| 679 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 680 | SecStart = W.OS.tell(); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 681 | const MCSectionELF *SymtabShndxSection = |
| 682 | SectionTable[SymtabShndxSectionIndex - 1]; |
| 683 | for (uint32_t Index : ShndxIndexes) |
| 684 | write(Index); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 685 | SecEnd = W.OS.tell(); |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 686 | SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 689 | MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx, |
| 690 | const MCSectionELF &Sec) { |
| 691 | if (OWriter.Relocations[&Sec].empty()) |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 692 | return nullptr; |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 693 | |
Rafael Espindola | 34948e5 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 694 | const StringRef SectionName = Sec.getSectionName(); |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 695 | std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; |
| 696 | RelaSectionName += SectionName; |
Benjamin Kramer | fd05415 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 697 | |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 698 | unsigned EntrySize; |
| 699 | if (hasRelocationAddend()) |
| 700 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
| 701 | else |
| 702 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 703 | |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 704 | unsigned Flags = 0; |
Rafael Espindola | 34948e5 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 705 | if (Sec.getFlags() & ELF::SHF_GROUP) |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 706 | Flags = ELF::SHF_GROUP; |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 707 | |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 708 | MCSectionELF *RelaSection = Ctx.createELFRelSection( |
Rafael Espindola | ead8549 | 2015-02-17 20:31:13 +0000 | [diff] [blame] | 709 | RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL, |
Rafael Espindola | 34948e5 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 710 | Flags, EntrySize, Sec.getGroup(), &Sec); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 711 | RelaSection->setAlignment(is64Bit() ? 8 : 4); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 712 | return RelaSection; |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 713 | } |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 714 | |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 715 | // Include the debug info compression header. |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 716 | bool ELFWriter::maybeWriteCompression( |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 717 | uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle, |
| 718 | unsigned Alignment) { |
| 719 | if (ZLibStyle) { |
| 720 | uint64_t HdrSize = |
| 721 | is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr); |
| 722 | if (Size <= HdrSize + CompressedContents.size()) |
| 723 | return false; |
| 724 | // Platform specific header is followed by compressed data. |
| 725 | if (is64Bit()) { |
| 726 | // Write Elf64_Chdr header. |
| 727 | write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB)); |
| 728 | write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field. |
| 729 | write(static_cast<ELF::Elf64_Xword>(Size)); |
| 730 | write(static_cast<ELF::Elf64_Xword>(Alignment)); |
| 731 | } else { |
| 732 | // Write Elf32_Chdr header otherwise. |
| 733 | write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB)); |
| 734 | write(static_cast<ELF::Elf32_Word>(Size)); |
| 735 | write(static_cast<ELF::Elf32_Word>(Alignment)); |
| 736 | } |
| 737 | return true; |
| 738 | } |
| 739 | |
| 740 | // "ZLIB" followed by 8 bytes representing the uncompressed size of the section, |
| 741 | // useful for consumers to preallocate a buffer to decompress into. |
Richard Smith | b910e56 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 742 | const StringRef Magic = "ZLIB"; |
| 743 | if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size()) |
| 744 | return false; |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 745 | W.OS << Magic; |
| 746 | support::endian::write(W.OS, Size, support::big); |
Richard Smith | b910e56 | 2016-05-25 00:14:12 +0000 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 750 | void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec, |
| 751 | const MCAsmLayout &Layout) { |
Rafael Espindola | e15b1b7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 752 | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 753 | StringRef SectionName = Section.getSectionName(); |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 754 | |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 755 | auto &MC = Asm.getContext(); |
| 756 | const auto &MAI = MC.getAsmInfo(); |
| 757 | |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 758 | // Compressing debug_frame requires handling alignment fragments which is |
| 759 | // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow |
| 760 | // for writing to arbitrary buffers) for little benefit. |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 761 | bool CompressionEnabled = |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 762 | MAI->compressDebugSections() != DebugCompressionType::None; |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 763 | if (!CompressionEnabled || !SectionName.startswith(".debug_") || |
| 764 | SectionName == ".debug_frame") { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 765 | Asm.writeSectionData(W.OS, &Section, Layout); |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 766 | return; |
| 767 | } |
| 768 | |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 769 | assert((MAI->compressDebugSections() == DebugCompressionType::Z || |
| 770 | MAI->compressDebugSections() == DebugCompressionType::GNU) && |
| 771 | "expected zlib or zlib-gnu style compression"); |
| 772 | |
David Majnemer | abdb2d2a | 2015-09-01 16:19:03 +0000 | [diff] [blame] | 773 | SmallVector<char, 128> UncompressedData; |
| 774 | raw_svector_ostream VecOS(UncompressedData); |
Peter Collingbourne | 147db3e | 2018-05-21 18:11:35 +0000 | [diff] [blame] | 775 | Asm.writeSectionData(VecOS, &Section, Layout); |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 776 | |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 777 | SmallVector<char, 128> CompressedContents; |
George Rimar | 167ca4a | 2017-01-17 15:45:07 +0000 | [diff] [blame] | 778 | if (Error E = zlib::compress( |
| 779 | StringRef(UncompressedData.data(), UncompressedData.size()), |
| 780 | CompressedContents)) { |
| 781 | consumeError(std::move(E)); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 782 | W.OS << UncompressedData; |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 783 | return; |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 784 | } |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 785 | |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 786 | bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z; |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 787 | if (!maybeWriteCompression(UncompressedData.size(), CompressedContents, |
| 788 | ZlibStyle, Sec.getAlignment())) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 789 | W.OS << UncompressedData; |
Rafael Espindola | 868b3f4 | 2015-04-30 21:51:58 +0000 | [diff] [blame] | 790 | return; |
| 791 | } |
George Rimar | c91e38c | 2016-05-27 12:27:32 +0000 | [diff] [blame] | 792 | |
| 793 | if (ZlibStyle) |
| 794 | // Set the compressed flag. That is zlib style. |
| 795 | Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED); |
| 796 | else |
| 797 | // Add "z" prefix to section name. This is zlib-gnu style. |
Saleem Abdulrasool | 1f62f57 | 2017-06-09 00:40:19 +0000 | [diff] [blame] | 798 | MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str()); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 799 | W.OS << CompressedContents; |
David Blaikie | 8019bf8 | 2014-04-10 21:53:53 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 802 | void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 803 | uint64_t Address, uint64_t Offset, |
| 804 | uint64_t Size, uint32_t Link, uint32_t Info, |
| 805 | uint64_t Alignment, uint64_t EntrySize) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 806 | W.write<uint32_t>(Name); // sh_name: index into string table |
| 807 | W.write<uint32_t>(Type); // sh_type |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 808 | WriteWord(Flags); // sh_flags |
| 809 | WriteWord(Address); // sh_addr |
| 810 | WriteWord(Offset); // sh_offset |
| 811 | WriteWord(Size); // sh_size |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 812 | W.write<uint32_t>(Link); // sh_link |
| 813 | W.write<uint32_t>(Info); // sh_info |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 814 | WriteWord(Alignment); // sh_addralign |
| 815 | WriteWord(EntrySize); // sh_entsize |
| 816 | } |
| 817 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 818 | void ELFWriter::writeRelocations(const MCAssembler &Asm, |
Rafael Espindola | 34948e5 | 2015-04-30 00:45:46 +0000 | [diff] [blame] | 819 | const MCSectionELF &Sec) { |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 820 | std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec]; |
Akira Hatanaka | 64ad2cf | 2012-03-23 23:06:45 +0000 | [diff] [blame] | 821 | |
Rafael Espindola | f44db24 | 2015-12-17 16:22:06 +0000 | [diff] [blame] | 822 | // We record relocations by pushing to the end of a vector. Reverse the vector |
| 823 | // to get the relocations in the order they were created. |
| 824 | // In most cases that is not important, but it can be for special sections |
| 825 | // (.eh_frame) or specific relocations (TLS optimizations on SystemZ). |
| 826 | std::reverse(Relocs.begin(), Relocs.end()); |
Rafael Espindola | d0e1652 | 2015-12-17 15:08:24 +0000 | [diff] [blame] | 827 | |
Rafael Espindola | f44db24 | 2015-12-17 16:22:06 +0000 | [diff] [blame] | 828 | // Sort the relocation entries. MIPS needs this. |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 829 | OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 830 | |
| 831 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 832 | const ELFRelocationEntry &Entry = Relocs[e - i - 1]; |
Rafael Espindola | 5e9ed90 | 2015-05-28 20:53:09 +0000 | [diff] [blame] | 833 | unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 834 | |
Rafael Espindola | fdaae0d | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 835 | if (is64Bit()) { |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 836 | write(Entry.Offset); |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 837 | if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) { |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 838 | write(uint32_t(Index)); |
Benjamin Kramer | 6c3c349 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 839 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 840 | write(OWriter.TargetObjectWriter->getRSsym(Entry.Type)); |
| 841 | write(OWriter.TargetObjectWriter->getRType3(Entry.Type)); |
| 842 | write(OWriter.TargetObjectWriter->getRType2(Entry.Type)); |
| 843 | write(OWriter.TargetObjectWriter->getRType(Entry.Type)); |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 844 | } else { |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 845 | struct ELF::Elf64_Rela ERE64; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 846 | ERE64.setSymbolAndType(Index, Entry.Type); |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 847 | write(ERE64.r_info); |
Jack Carter | 8ad0c27 | 2012-06-27 22:28:30 +0000 | [diff] [blame] | 848 | } |
Rafael Espindola | fdaae0d | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 849 | if (hasRelocationAddend()) |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 850 | write(Entry.Addend); |
Benjamin Kramer | 6c3c349 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 851 | } else { |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 852 | write(uint32_t(Entry.Offset)); |
Benjamin Kramer | 6c3c349 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 853 | |
Rafael Espindola | bce26a1 | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 854 | struct ELF::Elf32_Rela ERE32; |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 855 | ERE32.setSymbolAndType(Index, Entry.Type); |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 856 | write(ERE32.r_info); |
Benjamin Kramer | 6c3c349 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 857 | |
Rafael Espindola | fdaae0d | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 858 | if (hasRelocationAddend()) |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 859 | write(uint32_t(Entry.Addend)); |
Simon Atanasyan | ede43b7 | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 860 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 861 | if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) { |
| 862 | if (uint32_t RType = |
| 863 | OWriter.TargetObjectWriter->getRType2(Entry.Type)) { |
Simon Atanasyan | ede43b7 | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 864 | write(uint32_t(Entry.Offset)); |
| 865 | |
| 866 | ERE32.setSymbolAndType(0, RType); |
| 867 | write(ERE32.r_info); |
| 868 | write(uint32_t(0)); |
| 869 | } |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 870 | if (uint32_t RType = |
| 871 | OWriter.TargetObjectWriter->getRType3(Entry.Type)) { |
Simon Atanasyan | ede43b7 | 2017-09-21 14:04:53 +0000 | [diff] [blame] | 872 | write(uint32_t(Entry.Offset)); |
| 873 | |
| 874 | ERE32.setSymbolAndType(0, RType); |
| 875 | write(ERE32.r_info); |
| 876 | write(uint32_t(0)); |
| 877 | } |
| 878 | } |
Benjamin Kramer | 6c3c349 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 879 | } |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 883 | const MCSectionELF *ELFWriter::createStringTable(MCContext &Ctx) { |
Rafael Espindola | 08850b3 | 2015-05-25 21:56:55 +0000 | [diff] [blame] | 884 | const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1]; |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 885 | StrTabBuilder.write(W.OS); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 886 | return StrtabSection; |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 889 | void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap, |
| 890 | uint32_t GroupSymbolIndex, uint64_t Offset, |
| 891 | uint64_t Size, const MCSectionELF &Section) { |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 892 | uint64_t sh_link = 0; |
| 893 | uint64_t sh_info = 0; |
| 894 | |
| 895 | switch(Section.getType()) { |
Rafael Espindola | 86fe2fe | 2015-04-06 03:16:51 +0000 | [diff] [blame] | 896 | default: |
| 897 | // Nothing to do. |
| 898 | break; |
| 899 | |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 900 | case ELF::SHT_DYNAMIC: |
Rafael Espindola | 74ef480 | 2015-04-30 21:20:06 +0000 | [diff] [blame] | 901 | llvm_unreachable("SHT_DYNAMIC in a relocatable object"); |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 902 | |
| 903 | case ELF::SHT_REL: |
| 904 | case ELF::SHT_RELA: { |
Rafael Espindola | 3a7c0eb | 2015-02-17 20:37:50 +0000 | [diff] [blame] | 905 | sh_link = SymbolTableIndex; |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 906 | assert(sh_link && ".symtab not found"); |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 907 | const MCSection *InfoSection = Section.getAssociatedSection(); |
| 908 | sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection)); |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 909 | break; |
| 910 | } |
| 911 | |
| 912 | case ELF::SHT_SYMTAB: |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 913 | sh_link = StringTableIndex; |
| 914 | sh_info = LastLocalSymbolIndex; |
| 915 | break; |
| 916 | |
| 917 | case ELF::SHT_SYMTAB_SHNDX: |
| 918 | sh_link = SymbolTableIndex; |
| 919 | break; |
| 920 | |
Nick Lewycky | c6ac5f7 | 2011-10-07 23:28:32 +0000 | [diff] [blame] | 921 | case ELF::SHT_GROUP: |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 922 | sh_link = SymbolTableIndex; |
| 923 | sh_info = GroupSymbolIndex; |
| 924 | break; |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 927 | if (Section.getFlags() & ELF::SHF_LINK_ORDER) { |
| 928 | const MCSymbol *Sym = Section.getAssociatedSymbol(); |
| 929 | const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection()); |
| 930 | sh_link = SectionIndexMap.lookup(Sec); |
| 931 | } |
Logan Chien | 4b72442 | 2013-02-05 14:18:59 +0000 | [diff] [blame] | 932 | |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 933 | WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()), |
Rafael Espindola | 2868758 | 2015-05-21 19:36:43 +0000 | [diff] [blame] | 934 | Section.getType(), Section.getFlags(), 0, Offset, Size, |
| 935 | sh_link, sh_info, Section.getAlignment(), |
| 936 | Section.getEntrySize()); |
Rafael Espindola | 5a8d781 | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 939 | void ELFWriter::writeSectionHeader( |
Rafael Espindola | 57c8083 | 2015-06-04 20:55:49 +0000 | [diff] [blame] | 940 | const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap, |
Rafael Espindola | a820169 | 2015-04-28 15:26:21 +0000 | [diff] [blame] | 941 | const SectionOffsetsTy &SectionOffsets) { |
Rafael Espindola | b186391 | 2015-04-30 21:10:06 +0000 | [diff] [blame] | 942 | const unsigned NumSections = SectionTable.size(); |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 943 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 944 | // Null section first. |
Rafael Espindola | 3fe87a1 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 945 | uint64_t FirstSectionSize = |
Rafael Espindola | bf0db6c | 2015-04-15 13:07:47 +0000 | [diff] [blame] | 946 | (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0; |
Rafael Espindola | 88d1f63 | 2015-04-29 20:25:24 +0000 | [diff] [blame] | 947 | WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 948 | |
Rafael Espindola | 08850b3 | 2015-05-25 21:56:55 +0000 | [diff] [blame] | 949 | for (const MCSectionELF *Section : SectionTable) { |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 950 | uint32_t GroupSymbolIndex; |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 951 | unsigned Type = Section->getType(); |
| 952 | if (Type != ELF::SHT_GROUP) |
Rafael Espindola | a3e9a22 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 953 | GroupSymbolIndex = 0; |
| 954 | else |
Rafael Espindola | 5e9ed90 | 2015-05-28 20:53:09 +0000 | [diff] [blame] | 955 | GroupSymbolIndex = Section->getGroup()->getIndex(); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 956 | |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 957 | const std::pair<uint64_t, uint64_t> &Offsets = |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 958 | SectionOffsets.find(Section)->second; |
Rafael Espindola | 1aa20fc | 2015-05-21 19:46:39 +0000 | [diff] [blame] | 959 | uint64_t Size; |
Rafael Espindola | 5a1e80b | 2015-05-26 02:00:36 +0000 | [diff] [blame] | 960 | if (Type == ELF::SHT_NOBITS) |
| 961 | Size = Layout.getSectionAddressSize(Section); |
| 962 | else |
Rafael Espindola | 1aa20fc | 2015-05-21 19:46:39 +0000 | [diff] [blame] | 963 | Size = Offsets.second - Offsets.first; |
Rafael Espindola | 60ebca9 | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 964 | |
Rafael Espindola | e92c1bf | 2015-05-21 19:42:35 +0000 | [diff] [blame] | 965 | writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size, |
Rafael Espindola | 2868758 | 2015-05-21 19:36:43 +0000 | [diff] [blame] | 966 | *Section); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 970 | uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) { |
Peter Collingbourne | 438390f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 971 | uint64_t StartOffset = W.OS.tell(); |
| 972 | |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 973 | MCContext &Ctx = Asm.getContext(); |
Rafael Espindola | 5960cee | 2015-05-22 23:58:30 +0000 | [diff] [blame] | 974 | MCSectionELF *StrtabSection = |
| 975 | Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0); |
| 976 | StringTableIndex = addToSectionTable(StrtabSection); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 977 | |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 978 | RevGroupMapTy RevGroupMap; |
| 979 | SectionIndexMapTy SectionIndexMap; |
| 980 | |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 981 | std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers; |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 982 | |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 983 | // Write out the ELF header ... |
Rafael Espindola | 8c7829b | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 984 | writeHeader(Asm); |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 985 | |
Rafael Espindola | 7230f80 | 2015-04-08 11:41:24 +0000 | [diff] [blame] | 986 | // ... then the sections ... |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 987 | SectionOffsetsTy SectionOffsets; |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 988 | std::vector<MCSectionELF *> Groups; |
| 989 | std::vector<MCSectionELF *> Relocations; |
Rafael Espindola | e15b1b7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 990 | for (MCSection &Sec : Asm) { |
| 991 | MCSectionELF &Section = static_cast<MCSectionELF &>(Sec); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 992 | |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 993 | align(Section.getAlignment()); |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 994 | |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 995 | // Remember the offset into the file for this section. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 996 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 997 | |
Rafael Espindola | 0ccf9b7 | 2015-06-02 21:30:13 +0000 | [diff] [blame] | 998 | const MCSymbolELF *SignatureSymbol = Section.getGroup(); |
Rafael Espindola | e15b1b7 | 2015-05-27 13:30:50 +0000 | [diff] [blame] | 999 | writeSectionData(Asm, Section, Layout); |
Rafael Espindola | b8cbb26 | 2015-04-30 00:30:40 +0000 | [diff] [blame] | 1000 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1001 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1002 | SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd); |
| 1003 | |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1004 | MCSectionELF *RelSection = createRelocationSection(Ctx, Section); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (SignatureSymbol) { |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 1007 | Asm.registerSymbol(*SignatureSymbol); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1008 | unsigned &GroupIdx = RevGroupMap[SignatureSymbol]; |
| 1009 | if (!GroupIdx) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1010 | MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol); |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1011 | GroupIdx = addToSectionTable(Group); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1012 | Group->setAlignment(4); |
| 1013 | Groups.push_back(Group); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1014 | } |
Rafael Espindola | 7830fc8 | 2015-06-05 17:54:25 +0000 | [diff] [blame] | 1015 | std::vector<const MCSectionELF *> &Members = |
| 1016 | GroupMembers[SignatureSymbol]; |
| 1017 | Members.push_back(&Section); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1018 | if (RelSection) |
Rafael Espindola | 7830fc8 | 2015-06-05 17:54:25 +0000 | [diff] [blame] | 1019 | Members.push_back(RelSection); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1022 | SectionIndexMap[&Section] = addToSectionTable(&Section); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1023 | if (RelSection) { |
Rafael Espindola | 03d7abb | 2015-04-30 20:53:27 +0000 | [diff] [blame] | 1024 | SectionIndexMap[RelSection] = addToSectionTable(RelSection); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1025 | Relocations.push_back(RelSection); |
| 1026 | } |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1029 | for (MCSectionELF *Group : Groups) { |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1030 | align(Group->getAlignment()); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1031 | |
| 1032 | // Remember the offset into the file for this section. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1033 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1034 | |
| 1035 | const MCSymbol *SignatureSymbol = Group->getGroup(); |
| 1036 | assert(SignatureSymbol); |
| 1037 | write(uint32_t(ELF::GRP_COMDAT)); |
| 1038 | for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) { |
| 1039 | uint32_t SecIndex = SectionIndexMap.lookup(Member); |
| 1040 | write(SecIndex); |
| 1041 | } |
| 1042 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1043 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1044 | SectionOffsets[Group] = std::make_pair(SecStart, SecEnd); |
| 1045 | } |
| 1046 | |
| 1047 | // Compute symbol table information. |
Rafael Espindola | aa486e9 | 2015-05-28 17:54:01 +0000 | [diff] [blame] | 1048 | computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1049 | |
| 1050 | for (MCSectionELF *RelSection : Relocations) { |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1051 | align(RelSection->getAlignment()); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1052 | |
| 1053 | // Remember the offset into the file for this section. |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1054 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1055 | |
Evgeniy Stepanov | 43dcf4d | 2017-03-14 19:28:51 +0000 | [diff] [blame] | 1056 | writeRelocations(Asm, |
| 1057 | cast<MCSectionELF>(*RelSection->getAssociatedSection())); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1058 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1059 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | 0a82ad7 | 2015-05-21 20:43:13 +0000 | [diff] [blame] | 1060 | SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Rafael Espindola | 88d1f63 | 2015-04-29 20:25:24 +0000 | [diff] [blame] | 1063 | { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1064 | uint64_t SecStart = W.OS.tell(); |
Rafael Espindola | b186391 | 2015-04-30 21:10:06 +0000 | [diff] [blame] | 1065 | const MCSectionELF *Sec = createStringTable(Ctx); |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1066 | uint64_t SecEnd = W.OS.tell(); |
Rafael Espindola | bda1980 | 2015-04-30 14:21:49 +0000 | [diff] [blame] | 1067 | SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd); |
Rafael Espindola | 88abc39 | 2015-04-29 20:34:31 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1070 | uint64_t NaturalAlignment = is64Bit() ? 8 : 4; |
Rafael Espindola | b20fbb8 | 2015-06-05 18:21:00 +0000 | [diff] [blame] | 1071 | align(NaturalAlignment); |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1072 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1073 | const uint64_t SectionHeaderOffset = W.OS.tell(); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1074 | |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1075 | // ... then the section header table ... |
Rafael Espindola | 57c8083 | 2015-06-04 20:55:49 +0000 | [diff] [blame] | 1076 | writeSectionHeader(Layout, SectionIndexMap, SectionOffsets); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1077 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1078 | uint16_t NumSections = support::endian::byte_swap<uint16_t>( |
| 1079 | (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF |
| 1080 | : SectionTable.size() + 1, |
| 1081 | W.Endian); |
Rafael Espindola | 8c7829b | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1082 | unsigned NumSectionsOffset; |
| 1083 | |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1084 | auto &Stream = static_cast<raw_pwrite_stream &>(W.OS); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1085 | if (is64Bit()) { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1086 | uint64_t Val = |
| 1087 | support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian); |
| 1088 | Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
| 1089 | offsetof(ELF::Elf64_Ehdr, e_shoff)); |
Rafael Espindola | 8c7829b | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1090 | NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1091 | } else { |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1092 | uint32_t Val = |
| 1093 | support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian); |
| 1094 | Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val), |
| 1095 | offsetof(ELF::Elf32_Ehdr, e_shoff)); |
Rafael Espindola | 8c7829b | 2015-04-29 20:39:37 +0000 | [diff] [blame] | 1096 | NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum); |
Rafael Espindola | 642a221 | 2015-04-14 22:54:16 +0000 | [diff] [blame] | 1097 | } |
Peter Collingbourne | f17b149 | 2018-05-21 18:17:42 +0000 | [diff] [blame] | 1098 | Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections), |
| 1099 | NumSectionsOffset); |
Peter Collingbourne | 438390f | 2018-05-21 18:23:50 +0000 | [diff] [blame] | 1100 | |
| 1101 | return W.OS.tell() - StartOffset; |
Rafael Espindola | 1557fd6 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Peter Collingbourne | a29fe57 | 2018-05-21 19:18:28 +0000 | [diff] [blame^] | 1104 | bool ELFObjectWriter::hasRelocationAddend() const { |
| 1105 | return TargetObjectWriter->hasRelocationAddend(); |
| 1106 | } |
| 1107 | |
| 1108 | void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
| 1109 | const MCAsmLayout &Layout) { |
| 1110 | // The presence of symbol versions causes undefined symbols and |
| 1111 | // versions declared with @@@ to be renamed. |
| 1112 | for (const std::pair<StringRef, const MCSymbol *> &P : Asm.Symvers) { |
| 1113 | StringRef AliasName = P.first; |
| 1114 | const auto &Symbol = cast<MCSymbolELF>(*P.second); |
| 1115 | size_t Pos = AliasName.find('@'); |
| 1116 | assert(Pos != StringRef::npos); |
| 1117 | |
| 1118 | StringRef Prefix = AliasName.substr(0, Pos); |
| 1119 | StringRef Rest = AliasName.substr(Pos); |
| 1120 | StringRef Tail = Rest; |
| 1121 | if (Rest.startswith("@@@")) |
| 1122 | Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1); |
| 1123 | |
| 1124 | auto *Alias = |
| 1125 | cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail)); |
| 1126 | Asm.registerSymbol(*Alias); |
| 1127 | const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext()); |
| 1128 | Alias->setVariableValue(Value); |
| 1129 | |
| 1130 | // Aliases defined with .symvar copy the binding from the symbol they alias. |
| 1131 | // This is the first place we are able to copy this information. |
| 1132 | Alias->setExternal(Symbol.isExternal()); |
| 1133 | Alias->setBinding(Symbol.getBinding()); |
| 1134 | |
| 1135 | if (!Symbol.isUndefined() && !Rest.startswith("@@@")) |
| 1136 | continue; |
| 1137 | |
| 1138 | // FIXME: produce a better error message. |
| 1139 | if (Symbol.isUndefined() && Rest.startswith("@@") && |
| 1140 | !Rest.startswith("@@@")) |
| 1141 | report_fatal_error("A @@ version cannot be undefined"); |
| 1142 | |
| 1143 | if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) |
| 1144 | report_fatal_error(llvm::Twine("Multiple symbol versions defined for ") + |
| 1145 | Symbol.getName()); |
| 1146 | |
| 1147 | Renames.insert(std::make_pair(&Symbol, Alias)); |
| 1148 | } |
| 1149 | } |
| 1150 | |
| 1151 | // It is always valid to create a relocation with a symbol. It is preferable |
| 1152 | // to use a relocation with a section if that is possible. Using the section |
| 1153 | // allows us to omit some local symbols from the symbol table. |
| 1154 | bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm, |
| 1155 | const MCSymbolRefExpr *RefA, |
| 1156 | const MCSymbolELF *Sym, |
| 1157 | uint64_t C, |
| 1158 | unsigned Type) const { |
| 1159 | // A PCRel relocation to an absolute value has no symbol (or section). We |
| 1160 | // represent that with a relocation to a null section. |
| 1161 | if (!RefA) |
| 1162 | return false; |
| 1163 | |
| 1164 | MCSymbolRefExpr::VariantKind Kind = RefA->getKind(); |
| 1165 | switch (Kind) { |
| 1166 | default: |
| 1167 | break; |
| 1168 | // The .odp creation emits a relocation against the symbol ".TOC." which |
| 1169 | // create a R_PPC64_TOC relocation. However the relocation symbol name |
| 1170 | // in final object creation should be NULL, since the symbol does not |
| 1171 | // really exist, it is just the reference to TOC base for the current |
| 1172 | // object file. Since the symbol is undefined, returning false results |
| 1173 | // in a relocation with a null section which is the desired result. |
| 1174 | case MCSymbolRefExpr::VK_PPC_TOCBASE: |
| 1175 | return false; |
| 1176 | |
| 1177 | // These VariantKind cause the relocation to refer to something other than |
| 1178 | // the symbol itself, like a linker generated table. Since the address of |
| 1179 | // symbol is not relevant, we cannot replace the symbol with the |
| 1180 | // section and patch the difference in the addend. |
| 1181 | case MCSymbolRefExpr::VK_GOT: |
| 1182 | case MCSymbolRefExpr::VK_PLT: |
| 1183 | case MCSymbolRefExpr::VK_GOTPCREL: |
| 1184 | case MCSymbolRefExpr::VK_PPC_GOT_LO: |
| 1185 | case MCSymbolRefExpr::VK_PPC_GOT_HI: |
| 1186 | case MCSymbolRefExpr::VK_PPC_GOT_HA: |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
| 1190 | // An undefined symbol is not in any section, so the relocation has to point |
| 1191 | // to the symbol itself. |
| 1192 | assert(Sym && "Expected a symbol"); |
| 1193 | if (Sym->isUndefined()) |
| 1194 | return true; |
| 1195 | |
| 1196 | unsigned Binding = Sym->getBinding(); |
| 1197 | switch(Binding) { |
| 1198 | default: |
| 1199 | llvm_unreachable("Invalid Binding"); |
| 1200 | case ELF::STB_LOCAL: |
| 1201 | break; |
| 1202 | case ELF::STB_WEAK: |
| 1203 | // If the symbol is weak, it might be overridden by a symbol in another |
| 1204 | // file. The relocation has to point to the symbol so that the linker |
| 1205 | // can update it. |
| 1206 | return true; |
| 1207 | case ELF::STB_GLOBAL: |
| 1208 | // Global ELF symbols can be preempted by the dynamic linker. The relocation |
| 1209 | // has to point to the symbol for a reason analogous to the STB_WEAK case. |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
| 1213 | // If a relocation points to a mergeable section, we have to be careful. |
| 1214 | // If the offset is zero, a relocation with the section will encode the |
| 1215 | // same information. With a non-zero offset, the situation is different. |
| 1216 | // For example, a relocation can point 42 bytes past the end of a string. |
| 1217 | // If we change such a relocation to use the section, the linker would think |
| 1218 | // that it pointed to another string and subtracting 42 at runtime will |
| 1219 | // produce the wrong value. |
| 1220 | if (Sym->isInSection()) { |
| 1221 | auto &Sec = cast<MCSectionELF>(Sym->getSection()); |
| 1222 | unsigned Flags = Sec.getFlags(); |
| 1223 | if (Flags & ELF::SHF_MERGE) { |
| 1224 | if (C != 0) |
| 1225 | return true; |
| 1226 | |
| 1227 | // It looks like gold has a bug (http://sourceware.org/PR16794) and can |
| 1228 | // only handle section relocations to mergeable sections if using RELA. |
| 1229 | if (!hasRelocationAddend()) |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
| 1233 | // Most TLS relocations use a got, so they need the symbol. Even those that |
| 1234 | // are just an offset (@tpoff), require a symbol in gold versions before |
| 1235 | // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed |
| 1236 | // http://sourceware.org/PR16773. |
| 1237 | if (Flags & ELF::SHF_TLS) |
| 1238 | return true; |
| 1239 | } |
| 1240 | |
| 1241 | // If the symbol is a thumb function the final relocation must set the lowest |
| 1242 | // bit. With a symbol that is done by just having the symbol have that bit |
| 1243 | // set, so we would lose the bit if we relocated with the section. |
| 1244 | // FIXME: We could use the section but add the bit to the relocation value. |
| 1245 | if (Asm.isThumbFunc(Sym)) |
| 1246 | return true; |
| 1247 | |
| 1248 | if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type)) |
| 1249 | return true; |
| 1250 | return false; |
| 1251 | } |
| 1252 | |
| 1253 | void ELFObjectWriter::recordRelocation(MCAssembler &Asm, |
| 1254 | const MCAsmLayout &Layout, |
| 1255 | const MCFragment *Fragment, |
| 1256 | const MCFixup &Fixup, MCValue Target, |
| 1257 | uint64_t &FixedValue) { |
| 1258 | MCAsmBackend &Backend = Asm.getBackend(); |
| 1259 | bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags & |
| 1260 | MCFixupKindInfo::FKF_IsPCRel; |
| 1261 | const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent()); |
| 1262 | uint64_t C = Target.getConstant(); |
| 1263 | uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 1264 | MCContext &Ctx = Asm.getContext(); |
| 1265 | |
| 1266 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 1267 | // Let A, B and C being the components of Target and R be the location of |
| 1268 | // the fixup. If the fixup is not pcrel, we want to compute (A - B + C). |
| 1269 | // If it is pcrel, we want to compute (A - B + C - R). |
| 1270 | |
| 1271 | // In general, ELF has no relocations for -B. It can only represent (A + C) |
| 1272 | // or (A + C - R). If B = R + K and the relocation is not pcrel, we can |
| 1273 | // replace B to implement it: (A - R - K + C) |
| 1274 | if (IsPCRel) { |
| 1275 | Ctx.reportError( |
| 1276 | Fixup.getLoc(), |
| 1277 | "No relocation available to represent this relative expression"); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
| 1281 | const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol()); |
| 1282 | |
| 1283 | if (SymB.isUndefined()) { |
| 1284 | Ctx.reportError(Fixup.getLoc(), |
| 1285 | Twine("symbol '") + SymB.getName() + |
| 1286 | "' can not be undefined in a subtraction expression"); |
| 1287 | return; |
| 1288 | } |
| 1289 | |
| 1290 | assert(!SymB.isAbsolute() && "Should have been folded"); |
| 1291 | const MCSection &SecB = SymB.getSection(); |
| 1292 | if (&SecB != &FixupSection) { |
| 1293 | Ctx.reportError(Fixup.getLoc(), |
| 1294 | "Cannot represent a difference across sections"); |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | uint64_t SymBOffset = Layout.getSymbolOffset(SymB); |
| 1299 | uint64_t K = SymBOffset - FixupOffset; |
| 1300 | IsPCRel = true; |
| 1301 | C -= K; |
| 1302 | } |
| 1303 | |
| 1304 | // We either rejected the fixup or folded B into C at this point. |
| 1305 | const MCSymbolRefExpr *RefA = Target.getSymA(); |
| 1306 | const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr; |
| 1307 | |
| 1308 | bool ViaWeakRef = false; |
| 1309 | if (SymA && SymA->isVariable()) { |
| 1310 | const MCExpr *Expr = SymA->getVariableValue(); |
| 1311 | if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) { |
| 1312 | if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) { |
| 1313 | SymA = cast<MCSymbolELF>(&Inner->getSymbol()); |
| 1314 | ViaWeakRef = true; |
| 1315 | } |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel); |
| 1320 | uint64_t OriginalC = C; |
| 1321 | bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type); |
| 1322 | if (!RelocateWithSymbol && SymA && !SymA->isUndefined()) |
| 1323 | C += Layout.getSymbolOffset(*SymA); |
| 1324 | |
| 1325 | uint64_t Addend = 0; |
| 1326 | if (hasRelocationAddend()) { |
| 1327 | Addend = C; |
| 1328 | C = 0; |
| 1329 | } |
| 1330 | |
| 1331 | FixedValue = C; |
| 1332 | |
| 1333 | if (!RelocateWithSymbol) { |
| 1334 | const MCSection *SecA = |
| 1335 | (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr; |
| 1336 | const auto *SectionSymbol = |
| 1337 | SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr; |
| 1338 | if (SectionSymbol) |
| 1339 | SectionSymbol->setUsedInReloc(); |
| 1340 | ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, |
| 1341 | OriginalC); |
| 1342 | Relocations[&FixupSection].push_back(Rec); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
| 1346 | const auto *RenamedSymA = SymA; |
| 1347 | if (SymA) { |
| 1348 | if (const MCSymbolELF *R = Renames.lookup(SymA)) |
| 1349 | RenamedSymA = R; |
| 1350 | |
| 1351 | if (ViaWeakRef) |
| 1352 | RenamedSymA->setIsWeakrefUsedInReloc(); |
| 1353 | else |
| 1354 | RenamedSymA->setUsedInReloc(); |
| 1355 | } |
| 1356 | ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, |
| 1357 | OriginalC); |
| 1358 | Relocations[&FixupSection].push_back(Rec); |
| 1359 | } |
| 1360 | |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 1361 | bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 1362 | const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB, |
Rafael Espindola | 35d6189 | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1363 | bool InSet, bool IsPCRel) const { |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 1364 | const auto &SymA = cast<MCSymbolELF>(SA); |
Rafael Espindola | 35d6189 | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1365 | if (IsPCRel) { |
| 1366 | assert(!InSet); |
Peter Collingbourne | 3600305 | 2017-04-08 23:35:49 +0000 | [diff] [blame] | 1367 | if (isWeak(SymA)) |
Rafael Espindola | 35d6189 | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1368 | return false; |
| 1369 | } |
Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 1370 | return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, |
Rafael Espindola | 35d6189 | 2015-04-17 21:15:17 +0000 | [diff] [blame] | 1371 | InSet, IsPCRel); |
Eli Friedman | d92d17b | 2011-03-03 07:24:36 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 1374 | std::unique_ptr<MCObjectWriter> |
Lang Hames | dcb312b | 2017-10-09 23:53:15 +0000 | [diff] [blame] | 1375 | llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW, |
| 1376 | raw_pwrite_stream &OS, bool IsLittleEndian) { |
Lang Hames | 60fbc7c | 2017-10-10 16:28:07 +0000 | [diff] [blame] | 1377 | return llvm::make_unique<ELFObjectWriter>(std::move(MOTW), OS, |
| 1378 | IsLittleEndian); |
Rafael Espindola | b264d33 | 2011-12-21 17:30:17 +0000 | [diff] [blame] | 1379 | } |