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