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