Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1 | //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===// |
| 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 | |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 14 | #include "MCELF.h" |
| 15 | #include "llvm/ADT/OwningPtr.h" |
| 16 | #include "llvm/ADT/SmallPtrSet.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/StringMap.h" |
| 20 | #include "llvm/ADT/Twine.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmBackend.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmLayout.h" |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAssembler.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCELFObjectWriter.h" |
| 26 | #include "llvm/MC/MCELFSymbolFlags.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCExpr.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSectionELF.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCValue.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/ErrorHandling.h" |
| 32 | #include "llvm/Support/ELF.h" |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | a7cfc08 | 2011-07-23 00:45:41 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringSwitch.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 35 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 36 | #include <vector> |
| 37 | using namespace llvm; |
| 38 | |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 39 | #undef DEBUG_TYPE |
| 40 | #define DEBUG_TYPE "reloc-info" |
| 41 | |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | class ELFObjectWriter : public MCObjectWriter { |
| 44 | protected: |
| 45 | |
| 46 | static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind); |
| 47 | static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant); |
| 48 | static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout); |
| 49 | static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data, |
| 50 | bool Used, bool Renamed); |
| 51 | static bool isLocal(const MCSymbolData &Data, bool isSignature, |
| 52 | bool isUsedInReloc); |
| 53 | static bool IsELFMetaDataSection(const MCSectionData &SD); |
| 54 | static uint64_t DataSectionSize(const MCSectionData &SD); |
| 55 | static uint64_t GetSectionFileSize(const MCAsmLayout &Layout, |
| 56 | const MCSectionData &SD); |
| 57 | static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout, |
| 58 | const MCSectionData &SD); |
| 59 | |
| 60 | void WriteDataSectionData(MCAssembler &Asm, |
| 61 | const MCAsmLayout &Layout, |
| 62 | const MCSectionELF &Section); |
| 63 | |
| 64 | /*static bool isFixupKindX86RIPRel(unsigned Kind) { |
| 65 | return Kind == X86::reloc_riprel_4byte || |
| 66 | Kind == X86::reloc_riprel_4byte_movq_load; |
| 67 | }*/ |
| 68 | |
| 69 | /// ELFSymbolData - Helper struct for containing some precomputed |
| 70 | /// information on symbols. |
| 71 | struct ELFSymbolData { |
| 72 | MCSymbolData *SymbolData; |
| 73 | uint64_t StringIndex; |
| 74 | uint32_t SectionIndex; |
| 75 | |
| 76 | // Support lexicographic sorting. |
| 77 | bool operator<(const ELFSymbolData &RHS) const { |
| 78 | if (MCELF::GetType(*SymbolData) == ELF::STT_FILE) |
| 79 | return true; |
| 80 | if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE) |
| 81 | return false; |
| 82 | return SymbolData->getSymbol().getName() < |
| 83 | RHS.SymbolData->getSymbol().getName(); |
| 84 | } |
| 85 | }; |
| 86 | |
Rafael Espindola | 3963d61 | 2011-12-22 03:38:00 +0000 | [diff] [blame] | 87 | /// The target specific ELF writer instance. |
| 88 | llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter; |
| 89 | |
| 90 | SmallPtrSet<const MCSymbol *, 16> UsedInReloc; |
| 91 | SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc; |
| 92 | DenseMap<const MCSymbol *, const MCSymbol *> Renames; |
| 93 | |
| 94 | llvm::DenseMap<const MCSectionData*, |
| 95 | std::vector<ELFRelocationEntry> > Relocations; |
| 96 | DenseMap<const MCSection*, uint64_t> SectionStringTableIndex; |
| 97 | |
| 98 | /// @} |
| 99 | /// @name Symbol Table Data |
| 100 | /// @{ |
| 101 | |
| 102 | SmallString<256> StringTable; |
| 103 | std::vector<ELFSymbolData> LocalSymbolData; |
| 104 | std::vector<ELFSymbolData> ExternalSymbolData; |
| 105 | std::vector<ELFSymbolData> UndefinedSymbolData; |
| 106 | |
| 107 | /// @} |
| 108 | |
| 109 | bool NeedsGOT; |
| 110 | |
| 111 | bool NeedsSymtabShndx; |
| 112 | |
| 113 | // This holds the symbol table index of the last local symbol. |
| 114 | unsigned LastLocalSymbolIndex; |
| 115 | // This holds the .strtab section index. |
| 116 | unsigned StringTableIndex; |
| 117 | // This holds the .symtab section index. |
| 118 | unsigned SymbolTableIndex; |
| 119 | |
| 120 | unsigned ShstrtabIndex; |
| 121 | |
| 122 | |
| 123 | const MCSymbol *SymbolToReloc(const MCAssembler &Asm, |
| 124 | const MCValue &Target, |
| 125 | const MCFragment &F, |
| 126 | const MCFixup &Fixup, |
| 127 | bool IsPCRel) const; |
| 128 | |
| 129 | // TargetObjectWriter wrappers. |
| 130 | const MCSymbol *ExplicitRelSym(const MCAssembler &Asm, |
| 131 | const MCValue &Target, |
| 132 | const MCFragment &F, |
| 133 | const MCFixup &Fixup, |
| 134 | bool IsPCRel) const { |
| 135 | return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
| 136 | } |
| 137 | |
| 138 | bool is64Bit() const { return TargetObjectWriter->is64Bit(); } |
| 139 | bool hasRelocationAddend() const { |
| 140 | return TargetObjectWriter->hasRelocationAddend(); |
| 141 | } |
| 142 | unsigned getEFlags() const { |
| 143 | return TargetObjectWriter->getEFlags(); |
| 144 | } |
| 145 | unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, |
| 146 | bool IsPCRel, bool IsRelocWithSymbol, |
| 147 | int64_t Addend) const { |
| 148 | return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel, |
| 149 | IsRelocWithSymbol, Addend); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | public: |
| 154 | ELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
| 155 | raw_ostream &_OS, bool IsLittleEndian) |
| 156 | : MCObjectWriter(_OS, IsLittleEndian), |
| 157 | TargetObjectWriter(MOTW), |
| 158 | NeedsGOT(false), NeedsSymtabShndx(false){ |
| 159 | } |
| 160 | |
| 161 | virtual ~ELFObjectWriter(); |
| 162 | |
| 163 | void WriteWord(uint64_t W) { |
| 164 | if (is64Bit()) |
| 165 | Write64(W); |
| 166 | else |
| 167 | Write32(W); |
| 168 | } |
| 169 | |
| 170 | void StringLE16(char *buf, uint16_t Value) { |
| 171 | buf[0] = char(Value >> 0); |
| 172 | buf[1] = char(Value >> 8); |
| 173 | } |
| 174 | |
| 175 | void StringLE32(char *buf, uint32_t Value) { |
| 176 | StringLE16(buf, uint16_t(Value >> 0)); |
| 177 | StringLE16(buf + 2, uint16_t(Value >> 16)); |
| 178 | } |
| 179 | |
| 180 | void StringLE64(char *buf, uint64_t Value) { |
| 181 | StringLE32(buf, uint32_t(Value >> 0)); |
| 182 | StringLE32(buf + 4, uint32_t(Value >> 32)); |
| 183 | } |
| 184 | |
| 185 | void StringBE16(char *buf ,uint16_t Value) { |
| 186 | buf[0] = char(Value >> 8); |
| 187 | buf[1] = char(Value >> 0); |
| 188 | } |
| 189 | |
| 190 | void StringBE32(char *buf, uint32_t Value) { |
| 191 | StringBE16(buf, uint16_t(Value >> 16)); |
| 192 | StringBE16(buf + 2, uint16_t(Value >> 0)); |
| 193 | } |
| 194 | |
| 195 | void StringBE64(char *buf, uint64_t Value) { |
| 196 | StringBE32(buf, uint32_t(Value >> 32)); |
| 197 | StringBE32(buf + 4, uint32_t(Value >> 0)); |
| 198 | } |
| 199 | |
| 200 | void String8(MCDataFragment &F, uint8_t Value) { |
| 201 | char buf[1]; |
| 202 | buf[0] = Value; |
| 203 | F.getContents() += StringRef(buf, 1); |
| 204 | } |
| 205 | |
| 206 | void String16(MCDataFragment &F, uint16_t Value) { |
| 207 | char buf[2]; |
| 208 | if (isLittleEndian()) |
| 209 | StringLE16(buf, Value); |
| 210 | else |
| 211 | StringBE16(buf, Value); |
| 212 | F.getContents() += StringRef(buf, 2); |
| 213 | } |
| 214 | |
| 215 | void String32(MCDataFragment &F, uint32_t Value) { |
| 216 | char buf[4]; |
| 217 | if (isLittleEndian()) |
| 218 | StringLE32(buf, Value); |
| 219 | else |
| 220 | StringBE32(buf, Value); |
| 221 | F.getContents() += StringRef(buf, 4); |
| 222 | } |
| 223 | |
| 224 | void String64(MCDataFragment &F, uint64_t Value) { |
| 225 | char buf[8]; |
| 226 | if (isLittleEndian()) |
| 227 | StringLE64(buf, Value); |
| 228 | else |
| 229 | StringBE64(buf, Value); |
| 230 | F.getContents() += StringRef(buf, 8); |
| 231 | } |
| 232 | |
| 233 | void WriteHeader(uint64_t SectionDataSize, |
| 234 | unsigned NumberOfSections); |
| 235 | |
| 236 | void WriteSymbolEntry(MCDataFragment *SymtabF, |
| 237 | MCDataFragment *ShndxF, |
| 238 | uint64_t name, uint8_t info, |
| 239 | uint64_t value, uint64_t size, |
| 240 | uint8_t other, uint32_t shndx, |
| 241 | bool Reserved); |
| 242 | |
| 243 | void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF, |
| 244 | ELFSymbolData &MSD, |
| 245 | const MCAsmLayout &Layout); |
| 246 | |
| 247 | typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy; |
| 248 | void WriteSymbolTable(MCDataFragment *SymtabF, |
| 249 | MCDataFragment *ShndxF, |
| 250 | const MCAssembler &Asm, |
| 251 | const MCAsmLayout &Layout, |
| 252 | const SectionIndexMapTy &SectionIndexMap); |
| 253 | |
| 254 | virtual void RecordRelocation(const MCAssembler &Asm, |
| 255 | const MCAsmLayout &Layout, |
| 256 | const MCFragment *Fragment, |
| 257 | const MCFixup &Fixup, |
| 258 | MCValue Target, uint64_t &FixedValue); |
| 259 | |
| 260 | uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 261 | const MCSymbol *S); |
| 262 | |
| 263 | // Map from a group section to the signature symbol |
| 264 | typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy; |
| 265 | // Map from a signature symbol to the group section |
| 266 | typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy; |
| 267 | // Map from a section to the section with the relocations |
| 268 | typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy; |
| 269 | // Map from a section to its offset |
| 270 | typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy; |
| 271 | |
| 272 | /// ComputeSymbolTable - Compute the symbol table data |
| 273 | /// |
| 274 | /// \param StringTable [out] - The string table data. |
| 275 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 276 | /// string table. |
| 277 | void ComputeSymbolTable(MCAssembler &Asm, |
| 278 | const SectionIndexMapTy &SectionIndexMap, |
| 279 | RevGroupMapTy RevGroupMap, |
| 280 | unsigned NumRegularSections); |
| 281 | |
| 282 | void ComputeIndexMap(MCAssembler &Asm, |
| 283 | SectionIndexMapTy &SectionIndexMap, |
| 284 | const RelMapTy &RelMap); |
| 285 | |
| 286 | void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout, |
| 287 | RelMapTy &RelMap); |
| 288 | |
| 289 | void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, |
| 290 | const RelMapTy &RelMap); |
| 291 | |
| 292 | void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout, |
| 293 | SectionIndexMapTy &SectionIndexMap, |
| 294 | const RelMapTy &RelMap); |
| 295 | |
| 296 | // Create the sections that show up in the symbol table. Currently |
| 297 | // those are the .note.GNU-stack section and the group sections. |
| 298 | void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout, |
| 299 | GroupMapTy &GroupMap, |
| 300 | RevGroupMapTy &RevGroupMap, |
| 301 | SectionIndexMapTy &SectionIndexMap, |
| 302 | const RelMapTy &RelMap); |
| 303 | |
| 304 | virtual void ExecutePostLayoutBinding(MCAssembler &Asm, |
| 305 | const MCAsmLayout &Layout); |
| 306 | |
| 307 | void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap, |
| 308 | const MCAsmLayout &Layout, |
| 309 | const SectionIndexMapTy &SectionIndexMap, |
| 310 | const SectionOffsetMapTy &SectionOffsetMap); |
| 311 | |
| 312 | void ComputeSectionOrder(MCAssembler &Asm, |
| 313 | std::vector<const MCSectionELF*> &Sections); |
| 314 | |
| 315 | void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 316 | uint64_t Address, uint64_t Offset, |
| 317 | uint64_t Size, uint32_t Link, uint32_t Info, |
| 318 | uint64_t Alignment, uint64_t EntrySize); |
| 319 | |
| 320 | void WriteRelocationsFragment(const MCAssembler &Asm, |
| 321 | MCDataFragment *F, |
| 322 | const MCSectionData *SD); |
| 323 | |
| 324 | virtual bool |
| 325 | IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 326 | const MCSymbolData &DataA, |
| 327 | const MCFragment &FB, |
| 328 | bool InSet, |
| 329 | bool IsPCRel) const; |
| 330 | |
| 331 | virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout); |
| 332 | void WriteSection(MCAssembler &Asm, |
| 333 | const SectionIndexMapTy &SectionIndexMap, |
| 334 | uint32_t GroupSymbolIndex, |
| 335 | uint64_t Offset, uint64_t Size, uint64_t Alignment, |
| 336 | const MCSectionELF &Section); |
| 337 | }; |
| 338 | } |
| 339 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 340 | bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { |
| 341 | const MCFixupKindInfo &FKI = |
| 342 | Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind); |
| 343 | |
| 344 | return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 345 | } |
| 346 | |
| 347 | bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) { |
| 348 | switch (Variant) { |
| 349 | default: |
| 350 | return false; |
| 351 | case MCSymbolRefExpr::VK_GOT: |
| 352 | case MCSymbolRefExpr::VK_PLT: |
| 353 | case MCSymbolRefExpr::VK_GOTPCREL: |
Rafael Espindola | 378e0ec | 2011-06-05 01:20:06 +0000 | [diff] [blame] | 354 | case MCSymbolRefExpr::VK_GOTOFF: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 355 | case MCSymbolRefExpr::VK_TPOFF: |
| 356 | case MCSymbolRefExpr::VK_TLSGD: |
| 357 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 358 | case MCSymbolRefExpr::VK_INDNTPOFF: |
| 359 | case MCSymbolRefExpr::VK_NTPOFF: |
| 360 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 361 | case MCSymbolRefExpr::VK_TLSLDM: |
| 362 | case MCSymbolRefExpr::VK_DTPOFF: |
| 363 | case MCSymbolRefExpr::VK_TLSLD: |
| 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 368 | ELFObjectWriter::~ELFObjectWriter() |
| 369 | {} |
| 370 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 371 | // Emit the ELF header. |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 372 | void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize, |
| 373 | unsigned NumberOfSections) { |
Matt Fleming | 3565a06 | 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 | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 387 | Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 388 | |
| 389 | // e_ident[EI_DATA] |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 390 | Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 391 | |
| 392 | Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 393 | // e_ident[EI_OSABI] |
Rafael Espindola | dc9a8a3 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 394 | Write8(TargetObjectWriter->getOSABI()); |
Matt Fleming | 3565a06 | 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 | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 401 | Write16(TargetObjectWriter->getEMachine()); // e_machine = target |
Matt Fleming | 3565a06 | 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 | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 406 | WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) : |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 407 | sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 408 | |
Jason W Kim | 2d7a53a | 2011-02-04 21:41:11 +0000 | [diff] [blame] | 409 | // e_flags = whatever the target wants |
Rafael Espindola | e8526d0 | 2011-12-21 20:09:46 +0000 | [diff] [blame] | 410 | Write32(getEFlags()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 411 | |
| 412 | // e_ehsize = ELF header size |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 413 | Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr)); |
Matt Fleming | 3565a06 | 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 | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 419 | Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 420 | |
| 421 | // e_shnum = # of section header ents |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 422 | if (NumberOfSections >= ELF::SHN_LORESERVE) |
Nick Lewycky | aaae3f6 | 2011-10-07 20:56:23 +0000 | [diff] [blame] | 423 | Write16(ELF::SHN_UNDEF); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 424 | else |
| 425 | Write16(NumberOfSections); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 426 | |
| 427 | // e_shstrndx = Section # of '.shstrtab' |
Nick Lewycky | b3429d3 | 2011-10-07 20:58:24 +0000 | [diff] [blame] | 428 | if (ShstrtabIndex >= ELF::SHN_LORESERVE) |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 429 | Write16(ELF::SHN_XINDEX); |
| 430 | else |
| 431 | Write16(ShstrtabIndex); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Daniel Dunbar | 115a3dd | 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 | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 441 | if (ShndxF) { |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 442 | if (shndx >= ELF::SHN_LORESERVE && !Reserved) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 443 | String32(*ShndxF, shndx); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 444 | else |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 445 | String32(*ShndxF, 0); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Rafael Espindola | af3d38f | 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 | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 451 | if (is64Bit()) { |
Rafael Espindola | af3d38f | 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 | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 458 | } else { |
Rafael Espindola | af3d38f | 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 | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 468 | uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data, |
| 469 | const MCAsmLayout &Layout) { |
Rafael Espindola | 2c6ec31 | 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 | d149186 | 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 | f68a26b | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 479 | return (uint64_t)IntValue; |
Roman Divacky | d149186 | 2010-12-20 21:14:39 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 483 | if (!Symbol.isInSection()) |
| 484 | return 0; |
| 485 | |
Rafael Espindola | 6469540 | 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 | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 497 | void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, |
| 498 | const MCAsmLayout &Layout) { |
Rafael Espindola | 8818213 | 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 | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 505 | const MCSymbol &Symbol = Alias.AliasedSymbol(); |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 506 | MCSymbolData &SD = Asm.getSymbolData(Symbol); |
| 507 | |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 508 | // Not an alias. |
| 509 | if (&Symbol == &Alias) |
| 510 | continue; |
| 511 | |
Benjamin Kramer | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 512 | StringRef AliasName = Alias.getName(); |
Rafael Espindola | 8818213 | 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 | f571f9a | 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 | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 520 | MCELF::SetBinding(*it, MCELF::GetBinding(SD)); |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 521 | |
Benjamin Kramer | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 522 | StringRef Rest = AliasName.substr(Pos); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 523 | if (!Symbol.isUndefined() && !Rest.startswith("@@@")) |
| 524 | continue; |
| 525 | |
Rafael Espindola | 83ff4d2 | 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 | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 531 | Renames.insert(std::make_pair(&Symbol, &Alias)); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 535 | void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF, |
| 536 | MCDataFragment *ShndxF, |
| 537 | ELFSymbolData &MSD, |
| 538 | const MCAsmLayout &Layout) { |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 539 | MCSymbolData &OrigData = *MSD.SymbolData; |
Rafael Espindola | de89b01 | 2010-10-15 18:25:33 +0000 | [diff] [blame] | 540 | MCSymbolData &Data = |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 541 | Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol()); |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 542 | |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 543 | bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() || |
| 544 | Data.getSymbol().isVariable(); |
| 545 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 546 | uint8_t Binding = MCELF::GetBinding(OrigData); |
| 547 | uint8_t Visibility = MCELF::GetVisibility(OrigData); |
| 548 | uint8_t Type = MCELF::GetType(Data); |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 549 | |
| 550 | uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift); |
| 551 | uint8_t Other = Visibility; |
| 552 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 553 | uint64_t Value = SymbolValue(Data, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 554 | uint64_t Size = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 555 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 556 | assert(!(Data.isCommon() && !Data.isExternal())); |
| 557 | |
Rafael Espindola | f012124 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 558 | const MCExpr *ESize = Data.getSize(); |
| 559 | if (ESize) { |
| 560 | int64_t Res; |
| 561 | if (!ESize->EvaluateAsAbsolute(Res, Layout)) |
| 562 | report_fatal_error("Size expression must be absolute."); |
| 563 | Size = Res; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Write out the symbol table entry |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 567 | WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value, |
| 568 | Size, Other, MSD.SectionIndex, IsReserved); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 571 | void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF, |
| 572 | MCDataFragment *ShndxF, |
| 573 | const MCAssembler &Asm, |
| 574 | const MCAsmLayout &Layout, |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 575 | const SectionIndexMapTy &SectionIndexMap) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 576 | // The string table must be emitted first because we need the index |
| 577 | // into the string table for all the symbol names. |
| 578 | assert(StringTable.size() && "Missing string table"); |
| 579 | |
| 580 | // FIXME: Make sure the start of the symbol table is aligned. |
| 581 | |
| 582 | // The first entry is the undefined symbol entry. |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 583 | WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 584 | |
| 585 | // Write the symbol table entries. |
| 586 | LastLocalSymbolIndex = LocalSymbolData.size() + 1; |
| 587 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { |
| 588 | ELFSymbolData &MSD = LocalSymbolData[i]; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 589 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 592 | // Write out a symbol table entry for each regular section. |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 593 | for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; |
| 594 | ++i) { |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 595 | const MCSectionELF &Section = |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 596 | static_cast<const MCSectionELF&>(i->getSection()); |
| 597 | if (Section.getType() == ELF::SHT_RELA || |
| 598 | Section.getType() == ELF::SHT_REL || |
| 599 | Section.getType() == ELF::SHT_STRTAB || |
Nick Lewycky | d2fdb4a | 2011-10-07 23:29:53 +0000 | [diff] [blame] | 600 | Section.getType() == ELF::SHT_SYMTAB || |
| 601 | Section.getType() == ELF::SHT_SYMTAB_SHNDX) |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 602 | continue; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 603 | WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0, |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 604 | ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), |
| 605 | false); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 606 | LastLocalSymbolIndex++; |
| 607 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 608 | |
| 609 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { |
| 610 | ELFSymbolData &MSD = ExternalSymbolData[i]; |
| 611 | MCSymbolData &Data = *MSD.SymbolData; |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 612 | assert(((Data.getFlags() & ELF_STB_Global) || |
| 613 | (Data.getFlags() & ELF_STB_Weak)) && |
| 614 | "External symbol requires STB_GLOBAL or STB_WEAK flag"); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 615 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 616 | if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 617 | LastLocalSymbolIndex++; |
| 618 | } |
| 619 | |
| 620 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { |
| 621 | ELFSymbolData &MSD = UndefinedSymbolData[i]; |
| 622 | MCSymbolData &Data = *MSD.SymbolData; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 623 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 624 | if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 625 | LastLocalSymbolIndex++; |
| 626 | } |
| 627 | } |
| 628 | |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 629 | const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm, |
| 630 | const MCValue &Target, |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 631 | const MCFragment &F, |
| 632 | const MCFixup &Fixup, |
| 633 | bool IsPCRel) const { |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 634 | const MCSymbol &Symbol = Target.getSymA()->getSymbol(); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 635 | const MCSymbol &ASymbol = Symbol.AliasedSymbol(); |
| 636 | const MCSymbol *Renamed = Renames.lookup(&Symbol); |
| 637 | const MCSymbolData &SD = Asm.getSymbolData(Symbol); |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 638 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 639 | if (ASymbol.isUndefined()) { |
| 640 | if (Renamed) |
| 641 | return Renamed; |
| 642 | return &ASymbol; |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 643 | } |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 644 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 645 | if (SD.isExternal()) { |
| 646 | if (Renamed) |
| 647 | return Renamed; |
| 648 | return &Symbol; |
| 649 | } |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 650 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 651 | const MCSectionELF &Section = |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 652 | static_cast<const MCSectionELF&>(ASymbol.getSection()); |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 653 | const SectionKind secKind = Section.getKind(); |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 654 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 655 | if (secKind.isBSS()) |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 656 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 657 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 658 | if (secKind.isThreadLocal()) { |
| 659 | if (Renamed) |
| 660 | return Renamed; |
| 661 | return &Symbol; |
| 662 | } |
| 663 | |
Rafael Espindola | 8cecf25 | 2010-10-06 16:23:36 +0000 | [diff] [blame] | 664 | MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind(); |
Rafael Espindola | 3729d00 | 2010-10-05 23:57:26 +0000 | [diff] [blame] | 665 | const MCSectionELF &Sec2 = |
| 666 | static_cast<const MCSectionELF&>(F.getParent()->getSection()); |
| 667 | |
Rafael Espindola | 8cecf25 | 2010-10-06 16:23:36 +0000 | [diff] [blame] | 668 | if (&Sec2 != &Section && |
Rafael Espindola | c97f80e | 2010-10-18 16:38:04 +0000 | [diff] [blame] | 669 | (Kind == MCSymbolRefExpr::VK_PLT || |
| 670 | Kind == MCSymbolRefExpr::VK_GOTPCREL || |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 671 | Kind == MCSymbolRefExpr::VK_GOTOFF)) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 672 | if (Renamed) |
| 673 | return Renamed; |
| 674 | return &Symbol; |
| 675 | } |
Rafael Espindola | 3729d00 | 2010-10-05 23:57:26 +0000 | [diff] [blame] | 676 | |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 677 | if (Section.getFlags() & ELF::SHF_MERGE) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 678 | if (Target.getConstant() == 0) |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 679 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 680 | if (Renamed) |
| 681 | return Renamed; |
| 682 | return &Symbol; |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 683 | } |
Rafael Espindola | c97f80e | 2010-10-18 16:38:04 +0000 | [diff] [blame] | 684 | |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 685 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
| 686 | |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 689 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 690 | void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, |
| 691 | const MCAsmLayout &Layout, |
| 692 | const MCFragment *Fragment, |
| 693 | const MCFixup &Fixup, |
| 694 | MCValue Target, |
| 695 | uint64_t &FixedValue) { |
| 696 | int64_t Addend = 0; |
| 697 | int Index = 0; |
| 698 | int64_t Value = Target.getConstant(); |
| 699 | const MCSymbol *RelocSymbol = NULL; |
| 700 | |
Rafael Espindola | 127a6a4 | 2010-12-17 07:28:17 +0000 | [diff] [blame] | 701 | bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 702 | if (!Target.isAbsolute()) { |
| 703 | const MCSymbol &Symbol = Target.getSymA()->getSymbol(); |
| 704 | const MCSymbol &ASymbol = Symbol.AliasedSymbol(); |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 705 | RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 706 | |
| 707 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 708 | const MCSymbol &SymbolB = RefB->getSymbol(); |
| 709 | MCSymbolData &SDB = Asm.getSymbolData(SymbolB); |
| 710 | IsPCRel = true; |
| 711 | |
| 712 | // Offset of the symbol in the section |
| 713 | int64_t a = Layout.getSymbolOffset(&SDB); |
| 714 | |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 715 | // Offset of the relocation in the section |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 716 | int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 717 | Value += b - a; |
| 718 | } |
| 719 | |
| 720 | if (!RelocSymbol) { |
| 721 | MCSymbolData &SD = Asm.getSymbolData(ASymbol); |
| 722 | MCFragment *F = SD.getFragment(); |
| 723 | |
| 724 | Index = F->getParent()->getOrdinal() + 1; |
| 725 | |
| 726 | // Offset of the symbol in the section |
| 727 | Value += Layout.getSymbolOffset(&SD); |
| 728 | } else { |
| 729 | if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref) |
| 730 | WeakrefUsedInReloc.insert(RelocSymbol); |
| 731 | else |
| 732 | UsedInReloc.insert(RelocSymbol); |
| 733 | Index = -1; |
| 734 | } |
| 735 | Addend = Value; |
| 736 | // Compensate for the addend on i386. |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 737 | if (is64Bit()) |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 738 | Value = 0; |
| 739 | } |
| 740 | |
| 741 | FixedValue = Value; |
| 742 | unsigned Type = GetRelocType(Target, Fixup, IsPCRel, |
| 743 | (RelocSymbol != 0), Addend); |
Rafael Espindola | c677e79 | 2011-12-21 14:26:29 +0000 | [diff] [blame] | 744 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 745 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 746 | if (RelocNeedsGOT(Modifier)) |
| 747 | NeedsGOT = true; |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 748 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 749 | uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) + |
| 750 | Fixup.getOffset(); |
Roman Divacky | 2a66cea | 2011-08-04 19:08:19 +0000 | [diff] [blame] | 751 | |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame] | 752 | // FIXME: no tests cover this. Is adjustFixupOffset dead code? |
| 753 | TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 754 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 755 | if (!hasRelocationAddend()) |
| 756 | Addend = 0; |
Rafael Espindola | bbf9c4a | 2011-08-04 13:05:26 +0000 | [diff] [blame] | 757 | |
| 758 | if (is64Bit()) |
| 759 | assert(isInt<64>(Addend)); |
| 760 | else |
| 761 | assert(isInt<32>(Addend)); |
| 762 | |
Akira Hatanaka | 00ca888 | 2012-03-23 23:06:45 +0000 | [diff] [blame^] | 763 | ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 764 | Relocations[Fragment->getParent()].push_back(ERE); |
| 765 | } |
| 766 | |
| 767 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 768 | uint64_t |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 769 | ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 770 | const MCSymbol *S) { |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 771 | MCSymbolData &SD = Asm.getSymbolData(*S); |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 772 | return SD.getIndex(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 775 | bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm, |
| 776 | const MCSymbolData &Data, |
| 777 | bool Used, bool Renamed) { |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 778 | if (Data.getFlags() & ELF_Other_Weakref) |
| 779 | return false; |
| 780 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 781 | if (Used) |
| 782 | return true; |
| 783 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 784 | if (Renamed) |
| 785 | return false; |
| 786 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 787 | const MCSymbol &Symbol = Data.getSymbol(); |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 788 | |
Rafael Espindola | d179886 | 2010-10-29 23:09:31 +0000 | [diff] [blame] | 789 | if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_") |
| 790 | return true; |
| 791 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 792 | const MCSymbol &A = Symbol.AliasedSymbol(); |
Rafael Espindola | 21451e5 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 793 | if (Symbol.isVariable() && !A.isVariable() && A.isUndefined()) |
| 794 | return false; |
| 795 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 796 | bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL; |
Rafael Espindola | 21451e5 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 797 | if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal) |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 798 | return false; |
| 799 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 800 | if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined()) |
| 801 | return false; |
| 802 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 803 | if (Symbol.isTemporary()) |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 804 | return false; |
| 805 | |
| 806 | return true; |
| 807 | } |
| 808 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 809 | bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature, |
| 810 | bool isUsedInReloc) { |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 811 | if (Data.isExternal()) |
| 812 | return false; |
| 813 | |
| 814 | const MCSymbol &Symbol = Data.getSymbol(); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 815 | const MCSymbol &RefSymbol = Symbol.AliasedSymbol(); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 816 | |
| 817 | if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) { |
| 818 | if (isSignature && !isUsedInReloc) |
| 819 | return true; |
| 820 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 821 | return false; |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 822 | } |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 827 | void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 828 | SectionIndexMapTy &SectionIndexMap, |
| 829 | const RelMapTy &RelMap) { |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 830 | unsigned Index = 1; |
| 831 | for (MCAssembler::iterator it = Asm.begin(), |
| 832 | ie = Asm.end(); it != ie; ++it) { |
| 833 | const MCSectionELF &Section = |
| 834 | static_cast<const MCSectionELF &>(it->getSection()); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 835 | if (Section.getType() != ELF::SHT_GROUP) |
| 836 | continue; |
| 837 | SectionIndexMap[&Section] = Index++; |
| 838 | } |
| 839 | |
| 840 | for (MCAssembler::iterator it = Asm.begin(), |
| 841 | ie = Asm.end(); it != ie; ++it) { |
| 842 | const MCSectionELF &Section = |
| 843 | static_cast<const MCSectionELF &>(it->getSection()); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 844 | if (Section.getType() == ELF::SHT_GROUP || |
| 845 | Section.getType() == ELF::SHT_REL || |
| 846 | Section.getType() == ELF::SHT_RELA) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 847 | continue; |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 848 | SectionIndexMap[&Section] = Index++; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 849 | const MCSectionELF *RelSection = RelMap.lookup(&Section); |
| 850 | if (RelSection) |
| 851 | SectionIndexMap[RelSection] = Index++; |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 855 | void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm, |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 856 | const SectionIndexMapTy &SectionIndexMap, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 857 | RevGroupMapTy RevGroupMap, |
| 858 | unsigned NumRegularSections) { |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 859 | // FIXME: Is this the correct place to do this? |
Rafael Espindola | 378e0ec | 2011-06-05 01:20:06 +0000 | [diff] [blame] | 860 | // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed? |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 861 | if (NeedsGOT) { |
| 862 | llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_"; |
| 863 | MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name); |
| 864 | MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym); |
| 865 | Data.setExternal(true); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 866 | MCELF::SetBinding(Data, ELF::STB_GLOBAL); |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 869 | // Index 0 is always the empty string. |
| 870 | StringMap<uint64_t> StringIndexMap; |
| 871 | StringTable += '\x00'; |
| 872 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 873 | // FIXME: We could optimize suffixes in strtab in the same way we |
| 874 | // optimize them in shstrtab. |
| 875 | |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 876 | // Add the data for the symbols. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 877 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 878 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 879 | const MCSymbol &Symbol = it->getSymbol(); |
| 880 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 881 | bool Used = UsedInReloc.count(&Symbol); |
| 882 | bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 883 | bool isSignature = RevGroupMap.count(&Symbol); |
| 884 | |
| 885 | if (!isInSymtab(Asm, *it, |
| 886 | Used || WeakrefUsed || isSignature, |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 887 | Renames.count(&Symbol))) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 888 | continue; |
| 889 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 890 | ELFSymbolData MSD; |
| 891 | MSD.SymbolData = it; |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 892 | const MCSymbol &RefSymbol = Symbol.AliasedSymbol(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 893 | |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 894 | // Undefined symbols are global, but this is the first place we |
| 895 | // are able to set it. |
| 896 | bool Local = isLocal(*it, isSignature, Used); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 897 | if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 898 | MCSymbolData &SD = Asm.getSymbolData(RefSymbol); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 899 | MCELF::SetBinding(*it, ELF::STB_GLOBAL); |
| 900 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 903 | if (RefSymbol.isUndefined() && !Used && WeakrefUsed) |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 904 | MCELF::SetBinding(*it, ELF::STB_WEAK); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 905 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 906 | if (it->isCommon()) { |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 907 | assert(!Local); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 908 | MSD.SectionIndex = ELF::SHN_COMMON; |
Rafael Espindola | bf052ac | 2010-10-27 16:04:30 +0000 | [diff] [blame] | 909 | } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) { |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 910 | MSD.SectionIndex = ELF::SHN_ABS; |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 911 | } else if (RefSymbol.isUndefined()) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 912 | if (isSignature && !Used) |
| 913 | MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]); |
| 914 | else |
| 915 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 916 | } else { |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 917 | const MCSectionELF &Section = |
| 918 | static_cast<const MCSectionELF&>(RefSymbol.getSection()); |
| 919 | MSD.SectionIndex = SectionIndexMap.lookup(&Section); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 920 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 921 | NeedsSymtabShndx = true; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 922 | assert(MSD.SectionIndex && "Invalid section index!"); |
Rafael Espindola | 5df0b65 | 2010-10-15 15:39:06 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 925 | // The @@@ in symbol version is replaced with @ in undefined symbols and |
| 926 | // @@ in defined ones. |
| 927 | StringRef Name = Symbol.getName(); |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 928 | SmallString<32> Buf; |
| 929 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 930 | size_t Pos = Name.find("@@@"); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 931 | if (Pos != StringRef::npos) { |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 932 | Buf += Name.substr(0, Pos); |
| 933 | unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; |
| 934 | Buf += Name.substr(Pos + Skip); |
| 935 | Name = Buf; |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 938 | uint64_t &Entry = StringIndexMap[Name]; |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 939 | if (!Entry) { |
| 940 | Entry = StringTable.size(); |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 941 | StringTable += Name; |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 942 | StringTable += '\x00'; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 943 | } |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 944 | MSD.StringIndex = Entry; |
| 945 | if (MSD.SectionIndex == ELF::SHN_UNDEF) |
| 946 | UndefinedSymbolData.push_back(MSD); |
| 947 | else if (Local) |
| 948 | LocalSymbolData.push_back(MSD); |
| 949 | else |
| 950 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | // Symbols are required to be in lexicographic order. |
| 954 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 955 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 956 | array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 957 | |
| 958 | // Set the symbol indices. Local symbols must come before all other |
| 959 | // symbols with non-local bindings. |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 960 | unsigned Index = 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 961 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 962 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 963 | |
| 964 | Index += NumRegularSections; |
| 965 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 966 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 967 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 968 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 969 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
Nick Lewycky | 7aabcb1 | 2011-10-11 03:54:50 +0000 | [diff] [blame] | 970 | |
| 971 | if (NumRegularSections > ELF::SHN_LORESERVE) |
| 972 | NeedsSymtabShndx = true; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 975 | void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm, |
| 976 | MCAsmLayout &Layout, |
| 977 | RelMapTy &RelMap) { |
| 978 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 979 | ie = Asm.end(); it != ie; ++it) { |
| 980 | const MCSectionData &SD = *it; |
| 981 | if (Relocations[&SD].empty()) |
| 982 | continue; |
| 983 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 984 | MCContext &Ctx = Asm.getContext(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 985 | const MCSectionELF &Section = |
| 986 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 987 | |
| 988 | const StringRef SectionName = Section.getSectionName(); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 989 | std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 990 | RelaSectionName += SectionName; |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 991 | |
| 992 | unsigned EntrySize; |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 993 | if (hasRelocationAddend()) |
| 994 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 995 | else |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 996 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 997 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 998 | const MCSectionELF *RelaSection = |
| 999 | Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ? |
| 1000 | ELF::SHT_RELA : ELF::SHT_REL, 0, |
| 1001 | SectionKind::getReadOnly(), |
| 1002 | EntrySize, ""); |
| 1003 | RelMap[&Section] = RelaSection; |
| 1004 | Asm.getOrCreateSectionData(*RelaSection); |
| 1005 | } |
| 1006 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1007 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1008 | void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, |
| 1009 | const RelMapTy &RelMap) { |
| 1010 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1011 | ie = Asm.end(); it != ie; ++it) { |
| 1012 | const MCSectionData &SD = *it; |
| 1013 | const MCSectionELF &Section = |
| 1014 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 1015 | |
| 1016 | const MCSectionELF *RelaSection = RelMap.lookup(&Section); |
| 1017 | if (!RelaSection) |
| 1018 | continue; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1019 | MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1020 | RelaSD.setAlignment(is64Bit() ? 8 : 4); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1021 | |
| 1022 | MCDataFragment *F = new MCDataFragment(&RelaSD); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1023 | WriteRelocationsFragment(Asm, F, &*it); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1027 | void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, |
| 1028 | uint64_t Flags, uint64_t Address, |
| 1029 | uint64_t Offset, uint64_t Size, |
| 1030 | uint32_t Link, uint32_t Info, |
| 1031 | uint64_t Alignment, |
| 1032 | uint64_t EntrySize) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1033 | Write32(Name); // sh_name: index into string table |
| 1034 | Write32(Type); // sh_type |
| 1035 | WriteWord(Flags); // sh_flags |
| 1036 | WriteWord(Address); // sh_addr |
| 1037 | WriteWord(Offset); // sh_offset |
| 1038 | WriteWord(Size); // sh_size |
| 1039 | Write32(Link); // sh_link |
| 1040 | Write32(Info); // sh_info |
| 1041 | WriteWord(Alignment); // sh_addralign |
| 1042 | WriteWord(EntrySize); // sh_entsize |
| 1043 | } |
| 1044 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1045 | void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm, |
| 1046 | MCDataFragment *F, |
| 1047 | const MCSectionData *SD) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1048 | std::vector<ELFRelocationEntry> &Relocs = Relocations[SD]; |
Akira Hatanaka | 00ca888 | 2012-03-23 23:06:45 +0000 | [diff] [blame^] | 1049 | |
| 1050 | // Sort the relocation entries. Most targets just sort by r_offset, but some |
| 1051 | // (e.g., MIPS) have additional constraints. |
| 1052 | TargetObjectWriter->sortRelocs(Asm, Relocs); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1053 | |
| 1054 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 1055 | ELFRelocationEntry entry = Relocs[e - i - 1]; |
| 1056 | |
Rafael Espindola | 12203cc | 2010-11-21 00:48:25 +0000 | [diff] [blame] | 1057 | if (!entry.Index) |
| 1058 | ; |
| 1059 | else if (entry.Index < 0) |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1060 | entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol); |
| 1061 | else |
Rafael Espindola | 12203cc | 2010-11-21 00:48:25 +0000 | [diff] [blame] | 1062 | entry.Index += LocalSymbolData.size(); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1063 | if (is64Bit()) { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1064 | String64(*F, entry.r_offset); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1065 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1066 | struct ELF::Elf64_Rela ERE64; |
| 1067 | ERE64.setSymbolAndType(entry.Index, entry.Type); |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1068 | String64(*F, ERE64.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1069 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1070 | if (hasRelocationAddend()) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1071 | String64(*F, entry.r_addend); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1072 | } else { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1073 | String32(*F, entry.r_offset); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1074 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1075 | struct ELF::Elf32_Rela ERE32; |
| 1076 | ERE32.setSymbolAndType(entry.Index, entry.Type); |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1077 | String32(*F, ERE32.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1078 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1079 | if (hasRelocationAddend()) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 1080 | String32(*F, entry.r_addend); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 1081 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 1085 | static int compareBySuffix(const void *a, const void *b) { |
| 1086 | const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a); |
| 1087 | const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b); |
| 1088 | const StringRef &NameA = secA->getSectionName(); |
| 1089 | const StringRef &NameB = secB->getSectionName(); |
| 1090 | const unsigned sizeA = NameA.size(); |
| 1091 | const unsigned sizeB = NameB.size(); |
| 1092 | const unsigned len = std::min(sizeA, sizeB); |
| 1093 | for (unsigned int i = 0; i < len; ++i) { |
| 1094 | char ca = NameA[sizeA - i - 1]; |
| 1095 | char cb = NameB[sizeB - i - 1]; |
| 1096 | if (ca != cb) |
| 1097 | return cb - ca; |
| 1098 | } |
| 1099 | |
| 1100 | return sizeB - sizeA; |
| 1101 | } |
| 1102 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1103 | void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, |
| 1104 | MCAsmLayout &Layout, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1105 | SectionIndexMapTy &SectionIndexMap, |
| 1106 | const RelMapTy &RelMap) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1107 | MCContext &Ctx = Asm.getContext(); |
| 1108 | MCDataFragment *F; |
| 1109 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1110 | unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1111 | |
Rafael Espindola | 38738bf | 2010-09-22 19:04:41 +0000 | [diff] [blame] | 1112 | // We construct .shstrtab, .symtab and .strtab in this order to match gnu as. |
Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 1113 | const MCSectionELF *ShstrtabSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1114 | Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 1115 | SectionKind::getReadOnly()); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1116 | MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); |
| 1117 | ShstrtabSD.setAlignment(1); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1118 | |
Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 1119 | const MCSectionELF *SymtabSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1120 | Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 1121 | SectionKind::getReadOnly(), |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1122 | EntrySize, ""); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1123 | MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1124 | SymtabSD.setAlignment(is64Bit() ? 8 : 4); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1125 | |
| 1126 | MCSectionData *SymtabShndxSD = NULL; |
| 1127 | |
| 1128 | if (NeedsSymtabShndx) { |
Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 1129 | const MCSectionELF *SymtabShndxSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1130 | Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1131 | SectionKind::getReadOnly(), 4, ""); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1132 | SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection); |
| 1133 | SymtabShndxSD->setAlignment(4); |
| 1134 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1135 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1136 | const MCSectionELF *StrtabSection; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1137 | StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 1138 | SectionKind::getReadOnly()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1139 | MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); |
| 1140 | StrtabSD.setAlignment(1); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1141 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1142 | ComputeIndexMap(Asm, SectionIndexMap, RelMap); |
| 1143 | |
| 1144 | ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection); |
| 1145 | SymbolTableIndex = SectionIndexMap.lookup(SymtabSection); |
| 1146 | StringTableIndex = SectionIndexMap.lookup(StrtabSection); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Symbol table |
| 1149 | F = new MCDataFragment(&SymtabSD); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1150 | MCDataFragment *ShndxF = NULL; |
| 1151 | if (NeedsSymtabShndx) { |
| 1152 | ShndxF = new MCDataFragment(SymtabShndxSD); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1153 | } |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 1154 | WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1155 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1156 | F = new MCDataFragment(&StrtabSD); |
| 1157 | F->getContents().append(StringTable.begin(), StringTable.end()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1158 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1159 | F = new MCDataFragment(&ShstrtabSD); |
| 1160 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 1161 | std::vector<const MCSectionELF*> Sections; |
| 1162 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1163 | ie = Asm.end(); it != ie; ++it) { |
| 1164 | const MCSectionELF &Section = |
| 1165 | static_cast<const MCSectionELF&>(it->getSection()); |
| 1166 | Sections.push_back(&Section); |
| 1167 | } |
| 1168 | array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix); |
| 1169 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1170 | // Section header string table. |
| 1171 | // |
| 1172 | // The first entry of a string table holds a null character so skip |
| 1173 | // section 0. |
| 1174 | uint64_t Index = 1; |
| 1175 | F->getContents() += '\x00'; |
| 1176 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 1177 | for (unsigned int I = 0, E = Sections.size(); I != E; ++I) { |
| 1178 | const MCSectionELF &Section = *Sections[I]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1179 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1180 | StringRef Name = Section.getSectionName(); |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 1181 | if (I != 0) { |
| 1182 | StringRef PreviousName = Sections[I - 1]->getSectionName(); |
| 1183 | if (PreviousName.endswith(Name)) { |
| 1184 | SectionStringTableIndex[&Section] = Index - Name.size() - 1; |
| 1185 | continue; |
| 1186 | } |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1187 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1188 | // Remember the index into the string table so we can write it |
| 1189 | // into the sh_name field of the section header table. |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1190 | SectionStringTableIndex[&Section] = Index; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1191 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1192 | Index += Name.size() + 1; |
| 1193 | F->getContents() += Name; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1194 | F->getContents() += '\x00'; |
| 1195 | } |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 1198 | void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, |
| 1199 | MCAsmLayout &Layout, |
| 1200 | GroupMapTy &GroupMap, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1201 | RevGroupMapTy &RevGroupMap, |
| 1202 | SectionIndexMapTy &SectionIndexMap, |
| 1203 | const RelMapTy &RelMap) { |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 1204 | // Create the .note.GNU-stack section if needed. |
| 1205 | MCContext &Ctx = Asm.getContext(); |
| 1206 | if (Asm.getNoExecStack()) { |
| 1207 | const MCSectionELF *GnuStackSection = |
| 1208 | Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0, |
| 1209 | SectionKind::getReadOnly()); |
| 1210 | Asm.getOrCreateSectionData(*GnuStackSection); |
| 1211 | } |
| 1212 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1213 | // Build the groups |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1214 | for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); |
| 1215 | it != ie; ++it) { |
| 1216 | const MCSectionELF &Section = |
| 1217 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 1218 | if (!(Section.getFlags() & ELF::SHF_GROUP)) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1219 | continue; |
| 1220 | |
| 1221 | const MCSymbol *SignatureSymbol = Section.getGroup(); |
| 1222 | Asm.getOrCreateSymbolData(*SignatureSymbol); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 1223 | const MCSectionELF *&Group = RevGroupMap[SignatureSymbol]; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1224 | if (!Group) { |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 1225 | Group = Ctx.CreateELFGroupSection(); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1226 | MCSectionData &Data = Asm.getOrCreateSectionData(*Group); |
| 1227 | Data.setAlignment(4); |
| 1228 | MCDataFragment *F = new MCDataFragment(&Data); |
| 1229 | String32(*F, ELF::GRP_COMDAT); |
| 1230 | } |
| 1231 | GroupMap[Group] = SignatureSymbol; |
| 1232 | } |
| 1233 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1234 | ComputeIndexMap(Asm, SectionIndexMap, RelMap); |
| 1235 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1236 | // Add sections to the groups |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1237 | for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1238 | it != ie; ++it) { |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1239 | const MCSectionELF &Section = |
| 1240 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 1241 | if (!(Section.getFlags() & ELF::SHF_GROUP)) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1242 | continue; |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 1243 | const MCSectionELF *Group = RevGroupMap[Section.getGroup()]; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1244 | MCSectionData &Data = Asm.getOrCreateSectionData(*Group); |
| 1245 | // FIXME: we could use the previous fragment |
| 1246 | MCDataFragment *F = new MCDataFragment(&Data); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1247 | unsigned Index = SectionIndexMap.lookup(&Section); |
| 1248 | String32(*F, Index); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1249 | } |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 1252 | void ELFObjectWriter::WriteSection(MCAssembler &Asm, |
| 1253 | const SectionIndexMapTy &SectionIndexMap, |
| 1254 | uint32_t GroupSymbolIndex, |
| 1255 | uint64_t Offset, uint64_t Size, |
| 1256 | uint64_t Alignment, |
| 1257 | const MCSectionELF &Section) { |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1258 | uint64_t sh_link = 0; |
| 1259 | uint64_t sh_info = 0; |
| 1260 | |
| 1261 | switch(Section.getType()) { |
| 1262 | case ELF::SHT_DYNAMIC: |
| 1263 | sh_link = SectionStringTableIndex[&Section]; |
| 1264 | sh_info = 0; |
| 1265 | break; |
| 1266 | |
| 1267 | case ELF::SHT_REL: |
| 1268 | case ELF::SHT_RELA: { |
| 1269 | const MCSectionELF *SymtabSection; |
| 1270 | const MCSectionELF *InfoSection; |
| 1271 | SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, |
| 1272 | 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 1273 | SectionKind::getReadOnly()); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1274 | sh_link = SectionIndexMap.lookup(SymtabSection); |
| 1275 | assert(sh_link && ".symtab not found"); |
| 1276 | |
| 1277 | // Remove ".rel" and ".rela" prefixes. |
| 1278 | unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; |
| 1279 | StringRef SectionName = Section.getSectionName().substr(SecNameLen); |
| 1280 | |
| 1281 | InfoSection = Asm.getContext().getELFSection(SectionName, |
| 1282 | ELF::SHT_PROGBITS, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 1283 | SectionKind::getReadOnly()); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1284 | sh_info = SectionIndexMap.lookup(InfoSection); |
| 1285 | break; |
| 1286 | } |
| 1287 | |
| 1288 | case ELF::SHT_SYMTAB: |
| 1289 | case ELF::SHT_DYNSYM: |
| 1290 | sh_link = StringTableIndex; |
| 1291 | sh_info = LastLocalSymbolIndex; |
| 1292 | break; |
| 1293 | |
| 1294 | case ELF::SHT_SYMTAB_SHNDX: |
| 1295 | sh_link = SymbolTableIndex; |
| 1296 | break; |
| 1297 | |
| 1298 | case ELF::SHT_PROGBITS: |
| 1299 | case ELF::SHT_STRTAB: |
| 1300 | case ELF::SHT_NOBITS: |
Rafael Espindola | 9897661 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 1301 | case ELF::SHT_NOTE: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1302 | case ELF::SHT_NULL: |
| 1303 | case ELF::SHT_ARM_ATTRIBUTES: |
Jason W Kim | 86a97f2 | 2011-01-12 00:19:25 +0000 | [diff] [blame] | 1304 | case ELF::SHT_INIT_ARRAY: |
| 1305 | case ELF::SHT_FINI_ARRAY: |
| 1306 | case ELF::SHT_PREINIT_ARRAY: |
Rafael Espindola | 0cf5e3d | 2011-01-23 05:43:40 +0000 | [diff] [blame] | 1307 | case ELF::SHT_X86_64_UNWIND: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1308 | // Nothing to do. |
| 1309 | break; |
| 1310 | |
Nick Lewycky | 4a8d43e | 2011-10-07 23:28:32 +0000 | [diff] [blame] | 1311 | case ELF::SHT_GROUP: |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1312 | sh_link = SymbolTableIndex; |
| 1313 | sh_info = GroupSymbolIndex; |
| 1314 | break; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1315 | |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1316 | default: |
| 1317 | assert(0 && "FIXME: sh_type value not supported!"); |
| 1318 | break; |
| 1319 | } |
| 1320 | |
| 1321 | WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(), |
| 1322 | Section.getFlags(), 0, Offset, Size, sh_link, sh_info, |
| 1323 | Alignment, Section.getEntrySize()); |
| 1324 | } |
| 1325 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1326 | bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) { |
Rafael Espindola | f8803fe | 2010-12-06 03:48:09 +0000 | [diff] [blame] | 1327 | return SD.getOrdinal() == ~UINT32_C(0) && |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1328 | !SD.getSection().isVirtualSection(); |
| 1329 | } |
| 1330 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1331 | uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1332 | uint64_t Ret = 0; |
| 1333 | for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; |
| 1334 | ++i) { |
| 1335 | const MCFragment &F = *i; |
| 1336 | assert(F.getKind() == MCFragment::FT_Data); |
| 1337 | Ret += cast<MCDataFragment>(F).getContents().size(); |
| 1338 | } |
| 1339 | return Ret; |
| 1340 | } |
| 1341 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1342 | uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout, |
| 1343 | const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1344 | if (IsELFMetaDataSection(SD)) |
| 1345 | return DataSectionSize(SD); |
| 1346 | return Layout.getSectionFileSize(&SD); |
| 1347 | } |
| 1348 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1349 | uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout, |
| 1350 | const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1351 | if (IsELFMetaDataSection(SD)) |
| 1352 | return DataSectionSize(SD); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1353 | return Layout.getSectionAddressSize(&SD); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1356 | void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm, |
| 1357 | const MCAsmLayout &Layout, |
| 1358 | const MCSectionELF &Section) { |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1359 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1360 | |
Benjamin Kramer | 263109d | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1361 | uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment()); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1362 | WriteZeros(Padding); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1363 | |
| 1364 | if (IsELFMetaDataSection(SD)) { |
| 1365 | for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; |
| 1366 | ++i) { |
| 1367 | const MCFragment &F = *i; |
| 1368 | assert(F.getKind() == MCFragment::FT_Data); |
| 1369 | WriteBytes(cast<MCDataFragment>(F).getContents().str()); |
| 1370 | } |
| 1371 | } else { |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1372 | Asm.writeSectionData(&SD, Layout); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1376 | void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm, |
| 1377 | const GroupMapTy &GroupMap, |
| 1378 | const MCAsmLayout &Layout, |
| 1379 | const SectionIndexMapTy &SectionIndexMap, |
| 1380 | const SectionOffsetMapTy &SectionOffsetMap) { |
| 1381 | const unsigned NumSections = Asm.size() + 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1382 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1383 | std::vector<const MCSectionELF*> Sections; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1384 | Sections.resize(NumSections - 1); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1385 | |
| 1386 | for (SectionIndexMapTy::const_iterator i= |
| 1387 | SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) { |
| 1388 | const std::pair<const MCSectionELF*, uint32_t> &p = *i; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1389 | Sections[p.second - 1] = p.first; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1392 | // Null section first. |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1393 | uint64_t FirstSectionSize = |
| 1394 | NumSections >= ELF::SHN_LORESERVE ? NumSections : 0; |
| 1395 | uint32_t FirstSectionLink = |
| 1396 | ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0; |
| 1397 | WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1398 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1399 | for (unsigned i = 0; i < NumSections - 1; ++i) { |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1400 | const MCSectionELF &Section = *Sections[i]; |
| 1401 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1402 | uint32_t GroupSymbolIndex; |
| 1403 | if (Section.getType() != ELF::SHT_GROUP) |
| 1404 | GroupSymbolIndex = 0; |
| 1405 | else |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1406 | GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, |
| 1407 | GroupMap.lookup(&Section)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1408 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1409 | uint64_t Size = GetSectionAddressSize(Layout, SD); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1410 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1411 | WriteSection(Asm, SectionIndexMap, GroupSymbolIndex, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1412 | SectionOffsetMap.lookup(&Section), Size, |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1413 | SD.getAlignment(), Section); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1417 | void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm, |
| 1418 | std::vector<const MCSectionELF*> &Sections) { |
| 1419 | for (MCAssembler::iterator it = Asm.begin(), |
| 1420 | ie = Asm.end(); it != ie; ++it) { |
| 1421 | const MCSectionELF &Section = |
| 1422 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1423 | if (Section.getType() == ELF::SHT_GROUP) |
| 1424 | Sections.push_back(&Section); |
| 1425 | } |
| 1426 | |
| 1427 | for (MCAssembler::iterator it = Asm.begin(), |
| 1428 | ie = Asm.end(); it != ie; ++it) { |
| 1429 | const MCSectionELF &Section = |
| 1430 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1431 | if (Section.getType() != ELF::SHT_GROUP && |
| 1432 | Section.getType() != ELF::SHT_REL && |
| 1433 | Section.getType() != ELF::SHT_RELA) |
| 1434 | Sections.push_back(&Section); |
| 1435 | } |
| 1436 | |
| 1437 | for (MCAssembler::iterator it = Asm.begin(), |
| 1438 | ie = Asm.end(); it != ie; ++it) { |
| 1439 | const MCSectionELF &Section = |
| 1440 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1441 | if (Section.getType() == ELF::SHT_REL || |
| 1442 | Section.getType() == ELF::SHT_RELA) |
| 1443 | Sections.push_back(&Section); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | void ELFObjectWriter::WriteObject(MCAssembler &Asm, |
| 1448 | const MCAsmLayout &Layout) { |
| 1449 | GroupMapTy GroupMap; |
| 1450 | RevGroupMapTy RevGroupMap; |
| 1451 | SectionIndexMapTy SectionIndexMap; |
| 1452 | |
| 1453 | unsigned NumUserSections = Asm.size(); |
| 1454 | |
| 1455 | DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap; |
| 1456 | CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); |
| 1457 | |
| 1458 | const unsigned NumUserAndRelocSections = Asm.size(); |
| 1459 | CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap, |
| 1460 | RevGroupMap, SectionIndexMap, RelMap); |
| 1461 | const unsigned AllSections = Asm.size(); |
| 1462 | const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections; |
| 1463 | |
| 1464 | unsigned NumRegularSections = NumUserSections + NumIndexedSections; |
| 1465 | |
| 1466 | // Compute symbol table information. |
| 1467 | ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections); |
| 1468 | |
| 1469 | |
| 1470 | WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); |
| 1471 | |
| 1472 | CreateMetadataSections(const_cast<MCAssembler&>(Asm), |
| 1473 | const_cast<MCAsmLayout&>(Layout), |
| 1474 | SectionIndexMap, |
| 1475 | RelMap); |
| 1476 | |
| 1477 | uint64_t NaturalAlignment = is64Bit() ? 8 : 4; |
| 1478 | uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) : |
| 1479 | sizeof(ELF::Elf32_Ehdr); |
| 1480 | uint64_t FileOff = HeaderSize; |
| 1481 | |
| 1482 | std::vector<const MCSectionELF*> Sections; |
| 1483 | ComputeSectionOrder(Asm, Sections); |
| 1484 | unsigned NumSections = Sections.size(); |
| 1485 | SectionOffsetMapTy SectionOffsetMap; |
| 1486 | for (unsigned i = 0; i < NumRegularSections + 1; ++i) { |
| 1487 | const MCSectionELF &Section = *Sections[i]; |
| 1488 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1489 | |
| 1490 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 1491 | |
| 1492 | // Remember the offset into the file for this section. |
| 1493 | SectionOffsetMap[&Section] = FileOff; |
| 1494 | |
| 1495 | // Get the size of the section in the output file (including padding). |
| 1496 | FileOff += GetSectionFileSize(Layout, SD); |
| 1497 | } |
| 1498 | |
| 1499 | FileOff = RoundUpToAlignment(FileOff, NaturalAlignment); |
| 1500 | |
| 1501 | const unsigned SectionHeaderOffset = FileOff - HeaderSize; |
| 1502 | |
| 1503 | uint64_t SectionHeaderEntrySize = is64Bit() ? |
| 1504 | sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr); |
| 1505 | FileOff += (NumSections + 1) * SectionHeaderEntrySize; |
| 1506 | |
| 1507 | for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) { |
| 1508 | const MCSectionELF &Section = *Sections[i]; |
| 1509 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1510 | |
| 1511 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 1512 | |
| 1513 | // Remember the offset into the file for this section. |
| 1514 | SectionOffsetMap[&Section] = FileOff; |
| 1515 | |
| 1516 | // Get the size of the section in the output file (including padding). |
| 1517 | FileOff += GetSectionFileSize(Layout, SD); |
| 1518 | } |
| 1519 | |
| 1520 | // Write out the ELF header ... |
| 1521 | WriteHeader(SectionHeaderOffset, NumSections + 1); |
| 1522 | |
| 1523 | // ... then the regular sections ... |
| 1524 | // + because of .shstrtab |
| 1525 | for (unsigned i = 0; i < NumRegularSections + 1; ++i) |
| 1526 | WriteDataSectionData(Asm, Layout, *Sections[i]); |
| 1527 | |
Benjamin Kramer | 263109d | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 1528 | uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1529 | WriteZeros(Padding); |
| 1530 | |
| 1531 | // ... then the section header table ... |
| 1532 | WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, |
| 1533 | SectionOffsetMap); |
| 1534 | |
Nick Lewycky | aaae3f6 | 2011-10-07 20:56:23 +0000 | [diff] [blame] | 1535 | // ... and then the remaining sections ... |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1536 | for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) |
| 1537 | WriteDataSectionData(Asm, Layout, *Sections[i]); |
| 1538 | } |
| 1539 | |
Eli Friedman | 78c1e17 | 2011-03-03 07:24:36 +0000 | [diff] [blame] | 1540 | bool |
| 1541 | ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 1542 | const MCSymbolData &DataA, |
| 1543 | const MCFragment &FB, |
| 1544 | bool InSet, |
| 1545 | bool IsPCRel) const { |
| 1546 | if (DataA.getFlags() & ELF_STB_Weak) |
| 1547 | return false; |
| 1548 | return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl( |
| 1549 | Asm, DataA, FB,InSet, IsPCRel); |
| 1550 | } |
| 1551 | |
Rafael Espindola | 6024c97 | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 1552 | MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
| 1553 | raw_ostream &OS, |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1554 | bool IsLittleEndian) { |
Rafael Espindola | 7bd2780 | 2011-12-22 03:24:43 +0000 | [diff] [blame] | 1555 | return new ELFObjectWriter(MOTW, OS, IsLittleEndian); |
Rafael Espindola | edae8e1 | 2011-12-21 17:30:17 +0000 | [diff] [blame] | 1556 | } |