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