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