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