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