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