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