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