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