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