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