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 | |
| 14 | #include "llvm/MC/ELFObjectWriter.h" |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/MC/MCAssembler.h" |
| 20 | #include "llvm/MC/MCAsmLayout.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCObjectWriter.h" |
| 25 | #include "llvm/MC/MCSectionELF.h" |
| 26 | #include "llvm/MC/MCSymbol.h" |
| 27 | #include "llvm/MC/MCValue.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/ELF.h" |
| 31 | #include "llvm/Target/TargetAsmBackend.h" |
| 32 | |
| 33 | #include "../Target/X86/X86FixupKinds.h" |
| 34 | |
| 35 | #include <vector> |
| 36 | using namespace llvm; |
| 37 | |
Rafael Espindola | ad49cf5 | 2010-09-18 15:03:21 +0000 | [diff] [blame] | 38 | static unsigned GetType(const MCSymbolData &SD) { |
| 39 | uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift; |
| 40 | assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT || |
| 41 | Type == ELF::STT_FUNC || Type == ELF::STT_SECTION || |
| 42 | Type == ELF::STT_FILE || Type == ELF::STT_COMMON || |
| 43 | Type == ELF::STT_TLS); |
| 44 | return Type; |
| 45 | } |
| 46 | |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 47 | static unsigned GetBinding(const MCSymbolData &SD) { |
| 48 | uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift; |
| 49 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 50 | Binding == ELF::STB_WEAK); |
| 51 | return Binding; |
| 52 | } |
| 53 | |
| 54 | static void SetBinding(MCSymbolData &SD, unsigned Binding) { |
| 55 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 56 | Binding == ELF::STB_WEAK); |
| 57 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift); |
| 58 | SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift)); |
| 59 | } |
| 60 | |
Rafael Espindola | cebdc01 | 2010-10-04 19:46:28 +0000 | [diff] [blame] | 61 | static bool isFixupKindX86PCRel(unsigned Kind) { |
| 62 | switch (Kind) { |
| 63 | default: |
| 64 | return false; |
| 65 | case X86::reloc_pcrel_1byte: |
| 66 | case X86::reloc_pcrel_4byte: |
| 67 | case X86::reloc_riprel_4byte: |
| 68 | case X86::reloc_riprel_4byte_movq_load: |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 73 | static bool RelocNeedsGOT(unsigned Type) { |
| 74 | switch (Type) { |
| 75 | default: |
| 76 | return false; |
| 77 | case ELF::R_X86_64_GOT32: |
| 78 | case ELF::R_X86_64_PLT32: |
| 79 | case ELF::R_X86_64_GOTPCREL: |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 84 | namespace { |
| 85 | |
| 86 | class ELFObjectWriterImpl { |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 87 | /*static bool isFixupKindX86RIPRel(unsigned Kind) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 88 | return Kind == X86::reloc_riprel_4byte || |
| 89 | Kind == X86::reloc_riprel_4byte_movq_load; |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 90 | }*/ |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 91 | |
| 92 | |
| 93 | /// ELFSymbolData - Helper struct for containing some precomputed information |
| 94 | /// on symbols. |
| 95 | struct ELFSymbolData { |
| 96 | MCSymbolData *SymbolData; |
| 97 | uint64_t StringIndex; |
| 98 | uint32_t SectionIndex; |
| 99 | |
| 100 | // Support lexicographic sorting. |
| 101 | bool operator<(const ELFSymbolData &RHS) const { |
Rafael Espindola | ad49cf5 | 2010-09-18 15:03:21 +0000 | [diff] [blame] | 102 | if (GetType(*SymbolData) == ELF::STT_FILE) |
| 103 | return true; |
| 104 | if (GetType(*RHS.SymbolData) == ELF::STT_FILE) |
| 105 | return false; |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 106 | return SymbolData->getSymbol().getName() < |
| 107 | RHS.SymbolData->getSymbol().getName(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
| 111 | /// @name Relocation Data |
| 112 | /// @{ |
| 113 | |
| 114 | struct ELFRelocationEntry { |
| 115 | // Make these big enough for both 32-bit and 64-bit |
| 116 | uint64_t r_offset; |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 117 | int Index; |
| 118 | unsigned Type; |
| 119 | const MCSymbol *Symbol; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 120 | uint64_t r_addend; |
| 121 | |
| 122 | // Support lexicographic sorting. |
| 123 | bool operator<(const ELFRelocationEntry &RE) const { |
| 124 | return RE.r_offset < r_offset; |
| 125 | } |
| 126 | }; |
| 127 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 128 | SmallPtrSet<const MCSymbol *, 16> UsedInReloc; |
| 129 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 130 | llvm::DenseMap<const MCSectionData*, |
| 131 | std::vector<ELFRelocationEntry> > Relocations; |
| 132 | DenseMap<const MCSection*, uint64_t> SectionStringTableIndex; |
| 133 | |
| 134 | /// @} |
| 135 | /// @name Symbol Table Data |
| 136 | /// @{ |
| 137 | |
| 138 | SmallString<256> StringTable; |
| 139 | std::vector<ELFSymbolData> LocalSymbolData; |
| 140 | std::vector<ELFSymbolData> ExternalSymbolData; |
| 141 | std::vector<ELFSymbolData> UndefinedSymbolData; |
| 142 | |
| 143 | /// @} |
| 144 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 145 | int NumRegularSections; |
| 146 | |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 147 | bool NeedsGOT; |
| 148 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 149 | ELFObjectWriter *Writer; |
| 150 | |
| 151 | raw_ostream &OS; |
| 152 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 153 | unsigned Is64Bit : 1; |
| 154 | |
| 155 | bool HasRelocationAddend; |
| 156 | |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 157 | Triple::OSType OSType; |
| 158 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 159 | // This holds the symbol table index of the last local symbol. |
| 160 | unsigned LastLocalSymbolIndex; |
| 161 | // This holds the .strtab section index. |
| 162 | unsigned StringTableIndex; |
| 163 | |
| 164 | unsigned ShstrtabIndex; |
| 165 | |
| 166 | public: |
| 167 | ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit, |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 168 | bool _HasRelAddend, Triple::OSType _OSType) |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 169 | : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()), |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 170 | Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend), |
| 171 | OSType(_OSType) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void Write8(uint8_t Value) { Writer->Write8(Value); } |
| 175 | void Write16(uint16_t Value) { Writer->Write16(Value); } |
| 176 | void Write32(uint32_t Value) { Writer->Write32(Value); } |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 177 | //void Write64(uint64_t Value) { Writer->Write64(Value); } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 178 | void WriteZeros(unsigned N) { Writer->WriteZeros(N); } |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 179 | //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) { |
| 180 | // Writer->WriteBytes(Str, ZeroFillSize); |
| 181 | //} |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 182 | |
| 183 | void WriteWord(uint64_t W) { |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 184 | if (Is64Bit) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 185 | Writer->Write64(W); |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 186 | else |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 187 | Writer->Write32(W); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void String8(char *buf, uint8_t Value) { |
| 191 | buf[0] = Value; |
| 192 | } |
| 193 | |
| 194 | void StringLE16(char *buf, uint16_t Value) { |
| 195 | buf[0] = char(Value >> 0); |
| 196 | buf[1] = char(Value >> 8); |
| 197 | } |
| 198 | |
| 199 | void StringLE32(char *buf, uint32_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 200 | StringLE16(buf, uint16_t(Value >> 0)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 201 | StringLE16(buf + 2, uint16_t(Value >> 16)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void StringLE64(char *buf, uint64_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 205 | StringLE32(buf, uint32_t(Value >> 0)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 206 | StringLE32(buf + 4, uint32_t(Value >> 32)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void StringBE16(char *buf ,uint16_t Value) { |
| 210 | buf[0] = char(Value >> 8); |
| 211 | buf[1] = char(Value >> 0); |
| 212 | } |
| 213 | |
| 214 | void StringBE32(char *buf, uint32_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 215 | StringBE16(buf, uint16_t(Value >> 16)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 216 | StringBE16(buf + 2, uint16_t(Value >> 0)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void StringBE64(char *buf, uint64_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 220 | StringBE32(buf, uint32_t(Value >> 32)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 221 | StringBE32(buf + 4, uint32_t(Value >> 0)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void String16(char *buf, uint16_t Value) { |
| 225 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 226 | StringLE16(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 227 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 228 | StringBE16(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void String32(char *buf, uint32_t Value) { |
| 232 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 233 | StringLE32(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 234 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 235 | StringBE32(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void String64(char *buf, uint64_t Value) { |
| 239 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 240 | StringLE64(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 241 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 242 | StringBE64(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections); |
| 246 | |
| 247 | void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info, |
| 248 | uint64_t value, uint64_t size, |
| 249 | uint8_t other, uint16_t shndx); |
| 250 | |
| 251 | void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD, |
| 252 | const MCAsmLayout &Layout); |
| 253 | |
| 254 | void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm, |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 255 | const MCAsmLayout &Layout, |
| 256 | unsigned NumRegularSections); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 257 | |
| 258 | void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 259 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 260 | MCValue Target, uint64_t &FixedValue); |
| 261 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 262 | uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 263 | const MCSymbol *S); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 264 | |
| 265 | /// ComputeSymbolTable - Compute the symbol table data |
| 266 | /// |
| 267 | /// \param StringTable [out] - The string table data. |
| 268 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 269 | /// string table. |
| 270 | void ComputeSymbolTable(MCAssembler &Asm); |
| 271 | |
| 272 | void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout, |
| 273 | const MCSectionData &SD); |
| 274 | |
| 275 | void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) { |
| 276 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 277 | ie = Asm.end(); it != ie; ++it) { |
| 278 | WriteRelocation(Asm, Layout, *it); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout); |
| 283 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 284 | void ExecutePostLayoutBinding(MCAssembler &Asm) { |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 285 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 286 | |
| 287 | void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 288 | uint64_t Address, uint64_t Offset, |
| 289 | uint64_t Size, uint32_t Link, uint32_t Info, |
| 290 | uint64_t Alignment, uint64_t EntrySize); |
| 291 | |
| 292 | void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F, |
| 293 | const MCSectionData *SD); |
| 294 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 295 | bool IsFixupFullyResolved(const MCAssembler &Asm, |
| 296 | const MCValue Target, |
| 297 | bool IsPCRel, |
| 298 | const MCFragment *DF) const; |
| 299 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 300 | void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | } |
| 304 | |
| 305 | // Emit the ELF header. |
| 306 | void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize, |
| 307 | unsigned NumberOfSections) { |
| 308 | // ELF Header |
| 309 | // ---------- |
| 310 | // |
| 311 | // Note |
| 312 | // ---- |
| 313 | // emitWord method behaves differently for ELF32 and ELF64, writing |
| 314 | // 4 bytes in the former and 8 in the latter. |
| 315 | |
| 316 | Write8(0x7f); // e_ident[EI_MAG0] |
| 317 | Write8('E'); // e_ident[EI_MAG1] |
| 318 | Write8('L'); // e_ident[EI_MAG2] |
| 319 | Write8('F'); // e_ident[EI_MAG3] |
| 320 | |
| 321 | Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
| 322 | |
| 323 | // e_ident[EI_DATA] |
| 324 | Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB); |
| 325 | |
| 326 | Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 327 | // e_ident[EI_OSABI] |
| 328 | switch (OSType) { |
| 329 | case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break; |
| 330 | case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break; |
| 331 | default: Write8(ELF::ELFOSABI_NONE); break; |
| 332 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 333 | Write8(0); // e_ident[EI_ABIVERSION] |
| 334 | |
| 335 | WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD); |
| 336 | |
| 337 | Write16(ELF::ET_REL); // e_type |
| 338 | |
| 339 | // FIXME: Make this configurable |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 340 | Write16(Is64Bit ? ELF::EM_X86_64 : ELF::EM_386); // e_machine = target |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 341 | |
| 342 | Write32(ELF::EV_CURRENT); // e_version |
| 343 | WriteWord(0); // e_entry, no entry point in .o file |
| 344 | WriteWord(0); // e_phoff, no program header for .o |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 345 | WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) : |
| 346 | sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 347 | |
| 348 | // FIXME: Make this configurable. |
| 349 | Write32(0); // e_flags = whatever the target wants |
| 350 | |
| 351 | // e_ehsize = ELF header size |
| 352 | Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr)); |
| 353 | |
| 354 | Write16(0); // e_phentsize = prog header entry size |
| 355 | Write16(0); // e_phnum = # prog header entries = 0 |
| 356 | |
| 357 | // e_shentsize = Section header entry size |
| 358 | Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr)); |
| 359 | |
| 360 | // e_shnum = # of section header ents |
| 361 | Write16(NumberOfSections); |
| 362 | |
| 363 | // e_shstrndx = Section # of '.shstrtab' |
| 364 | Write16(ShstrtabIndex); |
| 365 | } |
| 366 | |
| 367 | void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name, |
| 368 | uint8_t info, uint64_t value, |
| 369 | uint64_t size, uint8_t other, |
| 370 | uint16_t shndx) { |
| 371 | if (Is64Bit) { |
| 372 | char buf[8]; |
| 373 | |
| 374 | String32(buf, name); |
| 375 | F->getContents() += StringRef(buf, 4); // st_name |
| 376 | |
| 377 | String8(buf, info); |
| 378 | F->getContents() += StringRef(buf, 1); // st_info |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 379 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 380 | String8(buf, other); |
| 381 | F->getContents() += StringRef(buf, 1); // st_other |
| 382 | |
| 383 | String16(buf, shndx); |
| 384 | F->getContents() += StringRef(buf, 2); // st_shndx |
| 385 | |
| 386 | String64(buf, value); |
| 387 | F->getContents() += StringRef(buf, 8); // st_value |
| 388 | |
| 389 | String64(buf, size); |
| 390 | F->getContents() += StringRef(buf, 8); // st_size |
| 391 | } else { |
| 392 | char buf[4]; |
| 393 | |
| 394 | String32(buf, name); |
| 395 | F->getContents() += StringRef(buf, 4); // st_name |
| 396 | |
| 397 | String32(buf, value); |
| 398 | F->getContents() += StringRef(buf, 4); // st_value |
| 399 | |
| 400 | String32(buf, size); |
| 401 | F->getContents() += StringRef(buf, 4); // st_size |
| 402 | |
| 403 | String8(buf, info); |
| 404 | F->getContents() += StringRef(buf, 1); // st_info |
| 405 | |
| 406 | String8(buf, other); |
| 407 | F->getContents() += StringRef(buf, 1); // st_other |
| 408 | |
| 409 | String16(buf, shndx); |
| 410 | F->getContents() += StringRef(buf, 2); // st_shndx |
| 411 | } |
| 412 | } |
| 413 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 414 | static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) { |
| 415 | if (Data.isCommon() && Data.isExternal()) |
| 416 | return Data.getCommonAlignment(); |
| 417 | |
| 418 | const MCSymbol &Symbol = Data.getSymbol(); |
| 419 | if (!Symbol.isInSection()) |
| 420 | return 0; |
| 421 | |
| 422 | if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak)) |
| 423 | if (MCFragment *FF = Data.getFragment()) |
| 424 | return Layout.getSymbolAddress(&Data) - |
| 425 | Layout.getSectionAddress(FF->getParent()); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 430 | void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD, |
| 431 | const MCAsmLayout &Layout) { |
| 432 | MCSymbolData &Data = *MSD.SymbolData; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 433 | uint8_t Info = (Data.getFlags() & 0xff); |
| 434 | uint8_t Other = ((Data.getFlags() & 0xf00) >> ELF_STV_Shift); |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 435 | uint64_t Value = SymbolValue(Data, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 436 | uint64_t Size = 0; |
| 437 | const MCExpr *ESize; |
| 438 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 439 | assert(!(Data.isCommon() && !Data.isExternal())); |
| 440 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 441 | ESize = Data.getSize(); |
| 442 | if (Data.getSize()) { |
| 443 | MCValue Res; |
| 444 | if (ESize->getKind() == MCExpr::Binary) { |
| 445 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize); |
| 446 | |
| 447 | if (BE->EvaluateAsRelocatable(Res, &Layout)) { |
Rafael Espindola | 62fed8b | 2010-10-05 21:02:45 +0000 | [diff] [blame] | 448 | uint64_t AddressA = 0; |
| 449 | uint64_t AddressB = 0; |
| 450 | const MCSymbol &SymA = Res.getSymA()->getSymbol(); |
| 451 | const MCSymbol &SymB = Res.getSymB()->getSymbol(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 452 | |
Rafael Espindola | 62fed8b | 2010-10-05 21:02:45 +0000 | [diff] [blame] | 453 | if (SymA.isDefined()) { |
| 454 | MCSymbolData &A = Layout.getAssembler().getSymbolData(SymA); |
| 455 | AddressA = Layout.getSymbolAddress(&A); |
| 456 | } |
| 457 | |
| 458 | if (SymB.isDefined()) { |
| 459 | MCSymbolData &B = Layout.getAssembler().getSymbolData(SymB); |
| 460 | AddressB = Layout.getSymbolAddress(&B); |
| 461 | } |
| 462 | |
| 463 | Size = AddressA - AddressB; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 464 | } |
| 465 | } else if (ESize->getKind() == MCExpr::Constant) { |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 466 | Size = static_cast<const MCConstantExpr *>(ESize)->getValue(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 467 | } else { |
| 468 | assert(0 && "Unsupported size expression"); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Write out the symbol table entry |
| 473 | WriteSymbolEntry(F, MSD.StringIndex, Info, Value, |
| 474 | Size, Other, MSD.SectionIndex); |
| 475 | } |
| 476 | |
| 477 | void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F, |
| 478 | const MCAssembler &Asm, |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 479 | const MCAsmLayout &Layout, |
| 480 | unsigned NumRegularSections) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 481 | // The string table must be emitted first because we need the index |
| 482 | // into the string table for all the symbol names. |
| 483 | assert(StringTable.size() && "Missing string table"); |
| 484 | |
| 485 | // FIXME: Make sure the start of the symbol table is aligned. |
| 486 | |
| 487 | // The first entry is the undefined symbol entry. |
| 488 | unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 489 | F->getContents().append(EntrySize, '\x00'); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 490 | |
| 491 | // Write the symbol table entries. |
| 492 | LastLocalSymbolIndex = LocalSymbolData.size() + 1; |
| 493 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { |
| 494 | ELFSymbolData &MSD = LocalSymbolData[i]; |
| 495 | WriteSymbol(F, MSD, Layout); |
| 496 | } |
| 497 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 498 | // Write out a symbol table entry for each regular section. |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 499 | unsigned Index = 1; |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 500 | for (MCAssembler::const_iterator it = Asm.begin(); |
| 501 | Index <= NumRegularSections; ++it, ++Index) { |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 502 | const MCSectionELF &Section = |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 503 | static_cast<const MCSectionELF&>(it->getSection()); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 504 | // Leave out relocations so we don't have indexes within |
| 505 | // the relocations messed up |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 506 | if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL) |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 507 | continue; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 508 | WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 509 | LastLocalSymbolIndex++; |
| 510 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 511 | |
| 512 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { |
| 513 | ELFSymbolData &MSD = ExternalSymbolData[i]; |
| 514 | MCSymbolData &Data = *MSD.SymbolData; |
| 515 | assert((Data.getFlags() & ELF_STB_Global) && |
| 516 | "External symbol requires STB_GLOBAL flag"); |
| 517 | WriteSymbol(F, MSD, Layout); |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 518 | if (GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 519 | LastLocalSymbolIndex++; |
| 520 | } |
| 521 | |
| 522 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { |
| 523 | ELFSymbolData &MSD = UndefinedSymbolData[i]; |
| 524 | MCSymbolData &Data = *MSD.SymbolData; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 525 | WriteSymbol(F, MSD, Layout); |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 526 | if (GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 527 | LastLocalSymbolIndex++; |
| 528 | } |
| 529 | } |
| 530 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 531 | static bool ShouldRelocOnSymbol(const MCSymbolData &SD, |
| 532 | const MCValue &Target) { |
| 533 | const MCSymbol &Symbol = SD.getSymbol(); |
| 534 | if (Symbol.isUndefined()) |
| 535 | return true; |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 536 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 537 | const MCSectionELF &Section = |
| 538 | static_cast<const MCSectionELF&>(Symbol.getSection()); |
| 539 | |
| 540 | if (Section.getFlags() & MCSectionELF::SHF_MERGE) |
| 541 | return Target.getConstant() != 0; |
| 542 | |
| 543 | if (SD.isExternal()) |
| 544 | return true; |
| 545 | |
| 546 | return false; |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 549 | // FIXME: this is currently X86/X86_64 only |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 550 | void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm, |
| 551 | const MCAsmLayout &Layout, |
| 552 | const MCFragment *Fragment, |
| 553 | const MCFixup &Fixup, |
| 554 | MCValue Target, |
| 555 | uint64_t &FixedValue) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 556 | int64_t Addend = 0; |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 557 | int Index = 0; |
Benjamin Kramer | 95c602a | 2010-08-27 10:38:39 +0000 | [diff] [blame] | 558 | int64_t Value = Target.getConstant(); |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 559 | const MCSymbol *Symbol = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 560 | |
Rafael Espindola | 0007489 | 2010-09-17 22:34:41 +0000 | [diff] [blame] | 561 | bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind()); |
Benjamin Kramer | 81cfb85 | 2010-08-17 19:45:05 +0000 | [diff] [blame] | 562 | if (!Target.isAbsolute()) { |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 563 | Symbol = &Target.getSymA()->getSymbol(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 564 | MCSymbolData &SD = Asm.getSymbolData(*Symbol); |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 565 | MCFragment *F = SD.getFragment(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 566 | |
Rafael Espindola | 9d8b755 | 2010-10-03 00:46:57 +0000 | [diff] [blame] | 567 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 568 | const MCSymbol &SymbolB = RefB->getSymbol(); |
| 569 | MCSymbolData &SDB = Asm.getSymbolData(SymbolB); |
| 570 | IsPCRel = true; |
Rafael Espindola | 55fb102 | 2010-10-04 15:59:01 +0000 | [diff] [blame] | 571 | MCSectionData *Sec = Fragment->getParent(); |
| 572 | |
| 573 | // Offset of the symbol in the section |
| 574 | int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec); |
| 575 | |
| 576 | // Ofeset of the relocation in the section |
| 577 | int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 578 | Value += b - a; |
Rafael Espindola | 9d8b755 | 2010-10-03 00:46:57 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 581 | // Check that this case has already been fully resolved before we get |
| 582 | // here. |
Rafael Espindola | 0007489 | 2010-09-17 22:34:41 +0000 | [diff] [blame] | 583 | if (Symbol->isDefined() && !SD.isExternal() && |
| 584 | IsPCRel && |
| 585 | &Fragment->getParent()->getSection() == &Symbol->getSection()) { |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 586 | llvm_unreachable("We don't need a relocation in this case."); |
Rafael Espindola | 0007489 | 2010-09-17 22:34:41 +0000 | [diff] [blame] | 587 | return; |
| 588 | } |
| 589 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 590 | bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target); |
| 591 | if (!RelocOnSymbol) { |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 592 | Index = F->getParent()->getOrdinal(); |
Rafael Espindola | a648918 | 2010-09-24 21:19:03 +0000 | [diff] [blame] | 593 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 594 | MCSectionData *FSD = F->getParent(); |
| 595 | // Offset of the symbol in the section |
| 596 | Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD); |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 597 | } else { |
| 598 | UsedInReloc.insert(Symbol); |
| 599 | Index = -1; |
| 600 | } |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 601 | Addend = Value; |
| 602 | // Compensate for the addend on i386. |
| 603 | if (Is64Bit) |
| 604 | Value = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Benjamin Kramer | 95c602a | 2010-08-27 10:38:39 +0000 | [diff] [blame] | 607 | FixedValue = Value; |
| 608 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 609 | // determine the type of the relocation |
Rafael Espindola | 28f9ac8 | 2010-10-04 18:44:25 +0000 | [diff] [blame] | 610 | |
| 611 | MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 612 | unsigned Type; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 613 | if (Is64Bit) { |
| 614 | if (IsPCRel) { |
Rafael Espindola | 92bf668 | 2010-10-04 19:04:13 +0000 | [diff] [blame] | 615 | switch (Modifier) { |
| 616 | case MCSymbolRefExpr::VK_None: |
| 617 | Type = ELF::R_X86_64_PC32; |
| 618 | break; |
| 619 | case MCSymbolRefExpr::VK_PLT: |
| 620 | Type = ELF::R_X86_64_PLT32; |
| 621 | break; |
Rafael Espindola | 607d1f6 | 2010-10-04 19:51:39 +0000 | [diff] [blame] | 622 | case llvm::MCSymbolRefExpr::VK_GOTPCREL: |
| 623 | Type = ELF::R_X86_64_GOTPCREL; |
| 624 | break; |
Rafael Espindola | 92bf668 | 2010-10-04 19:04:13 +0000 | [diff] [blame] | 625 | default: |
| 626 | llvm_unreachable("Unimplemented"); |
| 627 | } |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 628 | } else { |
| 629 | switch ((unsigned)Fixup.getKind()) { |
| 630 | default: llvm_unreachable("invalid fixup kind!"); |
| 631 | case FK_Data_8: Type = ELF::R_X86_64_64; break; |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 632 | case X86::reloc_signed_4byte: |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 633 | case X86::reloc_pcrel_4byte: |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 634 | assert(isInt<32>(Target.getConstant())); |
Rafael Espindola | 28f9ac8 | 2010-10-04 18:44:25 +0000 | [diff] [blame] | 635 | switch (Modifier) { |
| 636 | case MCSymbolRefExpr::VK_None: |
| 637 | Type = ELF::R_X86_64_32S; |
| 638 | break; |
| 639 | case MCSymbolRefExpr::VK_GOT: |
| 640 | Type = ELF::R_X86_64_GOT32; |
| 641 | break; |
| 642 | default: |
| 643 | llvm_unreachable("Unimplemented"); |
| 644 | } |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 645 | break; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 646 | case FK_Data_4: |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 647 | Type = ELF::R_X86_64_32; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 648 | break; |
| 649 | case FK_Data_2: Type = ELF::R_X86_64_16; break; |
| 650 | case X86::reloc_pcrel_1byte: |
| 651 | case FK_Data_1: Type = ELF::R_X86_64_8; break; |
| 652 | } |
| 653 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 654 | } else { |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 655 | if (IsPCRel) { |
| 656 | Type = ELF::R_386_PC32; |
| 657 | } else { |
| 658 | switch ((unsigned)Fixup.getKind()) { |
| 659 | default: llvm_unreachable("invalid fixup kind!"); |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 660 | |
| 661 | // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode |
| 662 | // instead? |
| 663 | case X86::reloc_signed_4byte: |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 664 | case X86::reloc_pcrel_4byte: |
| 665 | case FK_Data_4: Type = ELF::R_386_32; break; |
| 666 | case FK_Data_2: Type = ELF::R_386_16; break; |
| 667 | case X86::reloc_pcrel_1byte: |
| 668 | case FK_Data_1: Type = ELF::R_386_8; break; |
| 669 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 673 | if (RelocNeedsGOT(Type)) |
| 674 | NeedsGOT = true; |
| 675 | |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 676 | ELFRelocationEntry ERE; |
| 677 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 678 | ERE.Index = Index; |
| 679 | ERE.Type = Type; |
| 680 | ERE.Symbol = Symbol; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 681 | |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 682 | ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 683 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 684 | if (HasRelocationAddend) |
| 685 | ERE.r_addend = Addend; |
Benjamin Kramer | 172d7d6 | 2010-08-17 00:33:24 +0000 | [diff] [blame] | 686 | else |
| 687 | ERE.r_addend = 0; // Silence compiler warning. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 688 | |
| 689 | Relocations[Fragment->getParent()].push_back(ERE); |
| 690 | } |
| 691 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 692 | uint64_t |
| 693 | ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 694 | const MCSymbol *S) { |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 695 | MCSymbolData &SD = Asm.getSymbolData(*S); |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 696 | |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 697 | // Local symbol. |
| 698 | if (!SD.isExternal() && !S->isUndefined()) |
| 699 | return SD.getIndex() + /* empty symbol */ 1; |
| 700 | |
| 701 | // External or undefined symbol. |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 702 | return SD.getIndex() + NumRegularSections + /* empty symbol */ 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 705 | static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data, |
| 706 | bool Used) { |
| 707 | const MCSymbol &Symbol = Data.getSymbol(); |
| 708 | if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined()) |
| 709 | return false; |
| 710 | |
| 711 | if (!Used && Symbol.isTemporary()) |
| 712 | return false; |
| 713 | |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | static bool isLocal(const MCSymbolData &Data) { |
| 718 | if (Data.isExternal()) |
| 719 | return false; |
| 720 | |
| 721 | const MCSymbol &Symbol = Data.getSymbol(); |
| 722 | if (Symbol.isUndefined() && !Symbol.isVariable()) |
| 723 | return false; |
| 724 | |
| 725 | return true; |
| 726 | } |
| 727 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 728 | void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) { |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 729 | // FIXME: Is this the correct place to do this? |
| 730 | if (NeedsGOT) { |
| 731 | llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_"; |
| 732 | MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name); |
| 733 | MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym); |
| 734 | Data.setExternal(true); |
| 735 | } |
| 736 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 737 | // Build section lookup table. |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 738 | NumRegularSections = Asm.size(); |
Rafael Espindola | f5c347d | 2010-10-05 21:20:07 +0000 | [diff] [blame] | 739 | DenseMap<const MCSection*, uint32_t> SectionIndexMap; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 740 | unsigned Index = 1; |
| 741 | for (MCAssembler::iterator it = Asm.begin(), |
| 742 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 743 | SectionIndexMap[&it->getSection()] = Index; |
| 744 | |
| 745 | // Index 0 is always the empty string. |
| 746 | StringMap<uint64_t> StringIndexMap; |
| 747 | StringTable += '\x00'; |
| 748 | |
| 749 | // Add the data for local symbols. |
| 750 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 751 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 752 | const MCSymbol &Symbol = it->getSymbol(); |
| 753 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 754 | if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol))) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 755 | continue; |
| 756 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 757 | if (!isLocal(*it)) |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 758 | continue; |
| 759 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 760 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 761 | if (!Entry) { |
| 762 | Entry = StringTable.size(); |
| 763 | StringTable += Symbol.getName(); |
| 764 | StringTable += '\x00'; |
| 765 | } |
| 766 | |
| 767 | ELFSymbolData MSD; |
| 768 | MSD.SymbolData = it; |
| 769 | MSD.StringIndex = Entry; |
| 770 | |
| 771 | if (Symbol.isAbsolute()) { |
| 772 | MSD.SectionIndex = ELF::SHN_ABS; |
| 773 | LocalSymbolData.push_back(MSD); |
| 774 | } else { |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 775 | const MCSymbol *SymbolP = &Symbol; |
| 776 | if (Symbol.isVariable()) { |
| 777 | const MCExpr *Value = Symbol.getVariableValue(); |
| 778 | assert (Value->getKind() == MCExpr::SymbolRef && "Unimplemented"); |
| 779 | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value); |
| 780 | SymbolP = &Ref->getSymbol(); |
| 781 | } |
| 782 | MSD.SectionIndex = SectionIndexMap.lookup(&SymbolP->getSection()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 783 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 784 | LocalSymbolData.push_back(MSD); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // Now add non-local symbols. |
| 789 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 790 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 791 | const MCSymbol &Symbol = it->getSymbol(); |
| 792 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 793 | if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol))) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 794 | continue; |
| 795 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 796 | if (isLocal(*it)) |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 797 | continue; |
| 798 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 799 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 800 | if (!Entry) { |
| 801 | Entry = StringTable.size(); |
| 802 | StringTable += Symbol.getName(); |
| 803 | StringTable += '\x00'; |
| 804 | } |
| 805 | |
| 806 | ELFSymbolData MSD; |
| 807 | MSD.SymbolData = it; |
| 808 | MSD.StringIndex = Entry; |
| 809 | |
Rafael Espindola | 01f9ea3 | 2010-10-05 22:26:43 +0000 | [diff] [blame^] | 810 | // FIXME: There is duplicated code with the local case. |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 811 | if (it->isCommon()) { |
| 812 | MSD.SectionIndex = ELF::SHN_COMMON; |
| 813 | ExternalSymbolData.push_back(MSD); |
Rafael Espindola | 01f9ea3 | 2010-10-05 22:26:43 +0000 | [diff] [blame^] | 814 | } else if (Symbol.isVariable()) { |
| 815 | const MCExpr *Value = Symbol.getVariableValue(); |
| 816 | assert (Value->getKind() == MCExpr::SymbolRef && "Unimplemented"); |
| 817 | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value); |
| 818 | const MCSymbol &RefSymbol = Ref->getSymbol(); |
| 819 | if (RefSymbol.isDefined()) { |
| 820 | MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection()); |
| 821 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 822 | ExternalSymbolData.push_back(MSD); |
| 823 | } |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 824 | } else if (Symbol.isUndefined()) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 825 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 826 | // FIXME: Undefined symbols are global, but this is the first place we |
| 827 | // are able to set it. |
| 828 | if (GetBinding(*it) == ELF::STB_LOCAL) |
| 829 | SetBinding(*it, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 830 | UndefinedSymbolData.push_back(MSD); |
| 831 | } else if (Symbol.isAbsolute()) { |
| 832 | MSD.SectionIndex = ELF::SHN_ABS; |
| 833 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 834 | } else { |
| 835 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 836 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 837 | ExternalSymbolData.push_back(MSD); |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // Symbols are required to be in lexicographic order. |
| 842 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 843 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 844 | array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 845 | |
| 846 | // Set the symbol indices. Local symbols must come before all other |
| 847 | // symbols with non-local bindings. |
| 848 | Index = 0; |
| 849 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 850 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
| 851 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 852 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 853 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 854 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
| 855 | } |
| 856 | |
| 857 | void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout, |
| 858 | const MCSectionData &SD) { |
| 859 | if (!Relocations[&SD].empty()) { |
| 860 | MCContext &Ctx = Asm.getContext(); |
| 861 | const MCSection *RelaSection; |
| 862 | const MCSectionELF &Section = |
| 863 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 864 | |
| 865 | const StringRef SectionName = Section.getSectionName(); |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 866 | std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel"; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 867 | RelaSectionName += SectionName; |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 868 | |
| 869 | unsigned EntrySize; |
| 870 | if (HasRelocationAddend) |
| 871 | EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
| 872 | else |
| 873 | EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 874 | |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 875 | RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ? |
| 876 | ELF::SHT_RELA : ELF::SHT_REL, 0, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 877 | SectionKind::getReadOnly(), |
| 878 | false, EntrySize); |
| 879 | |
| 880 | MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 881 | RelaSD.setAlignment(Is64Bit ? 8 : 4); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 882 | |
| 883 | MCDataFragment *F = new MCDataFragment(&RelaSD); |
| 884 | |
| 885 | WriteRelocationsFragment(Asm, F, &SD); |
| 886 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 887 | Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type, |
| 892 | uint64_t Flags, uint64_t Address, |
| 893 | uint64_t Offset, uint64_t Size, |
| 894 | uint32_t Link, uint32_t Info, |
| 895 | uint64_t Alignment, |
| 896 | uint64_t EntrySize) { |
| 897 | Write32(Name); // sh_name: index into string table |
| 898 | Write32(Type); // sh_type |
| 899 | WriteWord(Flags); // sh_flags |
| 900 | WriteWord(Address); // sh_addr |
| 901 | WriteWord(Offset); // sh_offset |
| 902 | WriteWord(Size); // sh_size |
| 903 | Write32(Link); // sh_link |
| 904 | Write32(Info); // sh_info |
| 905 | WriteWord(Alignment); // sh_addralign |
| 906 | WriteWord(EntrySize); // sh_entsize |
| 907 | } |
| 908 | |
| 909 | void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm, |
| 910 | MCDataFragment *F, |
| 911 | const MCSectionData *SD) { |
| 912 | std::vector<ELFRelocationEntry> &Relocs = Relocations[SD]; |
| 913 | // sort by the r_offset just like gnu as does |
| 914 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 915 | |
| 916 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 917 | ELFRelocationEntry entry = Relocs[e - i - 1]; |
| 918 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 919 | if (entry.Index < 0) |
| 920 | entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol); |
| 921 | else |
| 922 | entry.Index += LocalSymbolData.size() + 1; |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 923 | if (Is64Bit) { |
| 924 | char buf[8]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 925 | |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 926 | String64(buf, entry.r_offset); |
| 927 | F->getContents() += StringRef(buf, 8); |
| 928 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 929 | struct ELF::Elf64_Rela ERE64; |
| 930 | ERE64.setSymbolAndType(entry.Index, entry.Type); |
| 931 | String64(buf, ERE64.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 932 | F->getContents() += StringRef(buf, 8); |
| 933 | |
| 934 | if (HasRelocationAddend) { |
| 935 | String64(buf, entry.r_addend); |
| 936 | F->getContents() += StringRef(buf, 8); |
| 937 | } |
| 938 | } else { |
| 939 | char buf[4]; |
| 940 | |
| 941 | String32(buf, entry.r_offset); |
| 942 | F->getContents() += StringRef(buf, 4); |
| 943 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 944 | struct ELF::Elf32_Rela ERE32; |
| 945 | ERE32.setSymbolAndType(entry.Index, entry.Type); |
| 946 | String32(buf, ERE32.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 947 | F->getContents() += StringRef(buf, 4); |
| 948 | |
| 949 | if (HasRelocationAddend) { |
| 950 | String32(buf, entry.r_addend); |
| 951 | F->getContents() += StringRef(buf, 4); |
| 952 | } |
| 953 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm, |
| 958 | MCAsmLayout &Layout) { |
| 959 | MCContext &Ctx = Asm.getContext(); |
| 960 | MCDataFragment *F; |
| 961 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 962 | const MCSection *SymtabSection; |
| 963 | unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
| 964 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 965 | unsigned NumRegularSections = Asm.size(); |
| 966 | |
Rafael Espindola | 38738bf | 2010-09-22 19:04:41 +0000 | [diff] [blame] | 967 | // We construct .shstrtab, .symtab and .strtab in this order to match gnu as. |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 968 | const MCSection *ShstrtabSection; |
| 969 | ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, |
| 970 | SectionKind::getReadOnly(), false); |
| 971 | MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); |
| 972 | ShstrtabSD.setAlignment(1); |
| 973 | ShstrtabIndex = Asm.size(); |
| 974 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 975 | SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 976 | SectionKind::getReadOnly(), |
| 977 | false, EntrySize); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 978 | MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 979 | SymtabSD.setAlignment(Is64Bit ? 8 : 4); |
| 980 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 981 | const MCSection *StrtabSection; |
| 982 | StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, |
| 983 | SectionKind::getReadOnly(), false); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 984 | MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); |
| 985 | StrtabSD.setAlignment(1); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 986 | StringTableIndex = Asm.size(); |
| 987 | |
Rafael Espindola | c3c413f | 2010-09-27 22:04:54 +0000 | [diff] [blame] | 988 | WriteRelocations(Asm, Layout); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 989 | |
| 990 | // Symbol table |
| 991 | F = new MCDataFragment(&SymtabSD); |
| 992 | WriteSymbolTable(F, Asm, Layout, NumRegularSections); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 993 | Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 994 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 995 | F = new MCDataFragment(&StrtabSD); |
| 996 | F->getContents().append(StringTable.begin(), StringTable.end()); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 997 | Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 998 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 999 | F = new MCDataFragment(&ShstrtabSD); |
| 1000 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1001 | // Section header string table. |
| 1002 | // |
| 1003 | // The first entry of a string table holds a null character so skip |
| 1004 | // section 0. |
| 1005 | uint64_t Index = 1; |
| 1006 | F->getContents() += '\x00'; |
| 1007 | |
| 1008 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1009 | ie = Asm.end(); it != ie; ++it) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1010 | const MCSectionELF &Section = |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 1011 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 51efe7a | 2010-09-23 14:14:56 +0000 | [diff] [blame] | 1012 | // FIXME: We could merge suffixes like in .text and .rela.text. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Remember the index into the string table so we can write it |
| 1015 | // into the sh_name field of the section header table. |
| 1016 | SectionStringTableIndex[&it->getSection()] = Index; |
| 1017 | |
| 1018 | Index += Section.getSectionName().size() + 1; |
| 1019 | F->getContents() += Section.getSectionName(); |
| 1020 | F->getContents() += '\x00'; |
| 1021 | } |
| 1022 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1023 | Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout); |
| 1024 | } |
| 1025 | |
| 1026 | bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm, |
| 1027 | const MCValue Target, |
| 1028 | bool IsPCRel, |
| 1029 | const MCFragment *DF) const { |
| 1030 | // If this is a PCrel relocation, find the section this fixup value is |
| 1031 | // relative to. |
| 1032 | const MCSection *BaseSection = 0; |
| 1033 | if (IsPCRel) { |
| 1034 | BaseSection = &DF->getParent()->getSection(); |
| 1035 | assert(BaseSection); |
| 1036 | } |
| 1037 | |
| 1038 | const MCSection *SectionA = 0; |
| 1039 | const MCSymbol *SymbolA = 0; |
| 1040 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 1041 | SymbolA = &A->getSymbol(); |
| 1042 | SectionA = &SymbolA->getSection(); |
| 1043 | } |
| 1044 | |
| 1045 | const MCSection *SectionB = 0; |
| 1046 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 1047 | SectionB = &B->getSymbol().getSection(); |
| 1048 | } |
| 1049 | |
| 1050 | if (!BaseSection) |
| 1051 | return SectionA == SectionB; |
| 1052 | |
| 1053 | const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA); |
| 1054 | if (DataA.isExternal()) |
| 1055 | return false; |
| 1056 | |
| 1057 | return !SectionB && BaseSection == SectionA; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1060 | void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1061 | const MCAsmLayout &Layout) { |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1062 | // Compute symbol table information. |
| 1063 | ComputeSymbolTable(Asm); |
| 1064 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1065 | CreateMetadataSections(const_cast<MCAssembler&>(Asm), |
| 1066 | const_cast<MCAsmLayout&>(Layout)); |
| 1067 | |
| 1068 | // Add 1 for the null section. |
| 1069 | unsigned NumSections = Asm.size() + 1; |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1070 | uint64_t NaturalAlignment = Is64Bit ? 8 : 4; |
| 1071 | uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr); |
| 1072 | uint64_t FileOff = HeaderSize; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1073 | |
| 1074 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1075 | ie = Asm.end(); it != ie; ++it) { |
| 1076 | const MCSectionData &SD = *it; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1077 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1078 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 1079 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1080 | // Get the size of the section in the output file (including padding). |
| 1081 | uint64_t Size = Layout.getSectionFileSize(&SD); |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1082 | |
| 1083 | FileOff += Size; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1086 | FileOff = RoundUpToAlignment(FileOff, NaturalAlignment); |
| 1087 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1088 | // Write out the ELF header ... |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1089 | WriteHeader(FileOff - HeaderSize, NumSections); |
| 1090 | |
| 1091 | FileOff = HeaderSize; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1092 | |
| 1093 | // ... then all of the sections ... |
| 1094 | DenseMap<const MCSection*, uint64_t> SectionOffsetMap; |
| 1095 | |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1096 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 1097 | |
| 1098 | unsigned Index = 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1099 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1100 | ie = Asm.end(); it != ie; ++it) { |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1101 | const MCSectionData &SD = *it; |
| 1102 | |
| 1103 | uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment()); |
| 1104 | WriteZeros(Padding); |
| 1105 | FileOff += Padding; |
| 1106 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1107 | // Remember the offset into the file for this section. |
| 1108 | SectionOffsetMap[&it->getSection()] = FileOff; |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1109 | SectionIndexMap[&it->getSection()] = Index++; |
| 1110 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1111 | FileOff += Layout.getSectionFileSize(&SD); |
| 1112 | |
| 1113 | Asm.WriteSectionData(it, Layout, Writer); |
| 1114 | } |
| 1115 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1116 | uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment); |
| 1117 | WriteZeros(Padding); |
| 1118 | FileOff += Padding; |
| 1119 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1120 | // ... and then the section header table. |
| 1121 | // Should we align the section header table? |
| 1122 | // |
| 1123 | // Null section first. |
| 1124 | WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 1125 | |
| 1126 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1127 | ie = Asm.end(); it != ie; ++it) { |
| 1128 | const MCSectionData &SD = *it; |
| 1129 | const MCSectionELF &Section = |
| 1130 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 1131 | |
| 1132 | uint64_t sh_link = 0; |
| 1133 | uint64_t sh_info = 0; |
| 1134 | |
| 1135 | switch(Section.getType()) { |
| 1136 | case ELF::SHT_DYNAMIC: |
| 1137 | sh_link = SectionStringTableIndex[&it->getSection()]; |
| 1138 | sh_info = 0; |
| 1139 | break; |
| 1140 | |
| 1141 | case ELF::SHT_REL: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1142 | case ELF::SHT_RELA: { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1143 | const MCSection *SymtabSection; |
| 1144 | const MCSection *InfoSection; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1145 | |
| 1146 | SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 1147 | SectionKind::getReadOnly(), |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1148 | false); |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1149 | sh_link = SectionIndexMap[SymtabSection]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1150 | |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 1151 | // Remove ".rel" and ".rela" prefixes. |
| 1152 | unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; |
| 1153 | StringRef SectionName = Section.getSectionName().substr(SecNameLen); |
| 1154 | |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1155 | InfoSection = Asm.getContext().getELFSection(SectionName, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1156 | ELF::SHT_PROGBITS, 0, |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1157 | SectionKind::getReadOnly(), |
| 1158 | false); |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1159 | sh_info = SectionIndexMap[InfoSection]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1160 | break; |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1161 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1162 | |
| 1163 | case ELF::SHT_SYMTAB: |
| 1164 | case ELF::SHT_DYNSYM: |
| 1165 | sh_link = StringTableIndex; |
| 1166 | sh_info = LastLocalSymbolIndex; |
| 1167 | break; |
| 1168 | |
| 1169 | case ELF::SHT_PROGBITS: |
| 1170 | case ELF::SHT_STRTAB: |
| 1171 | case ELF::SHT_NOBITS: |
Benjamin Kramer | 19dc7fa | 2010-08-31 17:03:33 +0000 | [diff] [blame] | 1172 | case ELF::SHT_NULL: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1173 | // Nothing to do. |
| 1174 | break; |
| 1175 | |
| 1176 | case ELF::SHT_HASH: |
| 1177 | case ELF::SHT_GROUP: |
| 1178 | case ELF::SHT_SYMTAB_SHNDX: |
| 1179 | default: |
| 1180 | assert(0 && "FIXME: sh_type value not supported!"); |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()], |
| 1185 | Section.getType(), Section.getFlags(), |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1186 | 0, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1187 | SectionOffsetMap.lookup(&SD.getSection()), |
| 1188 | Layout.getSectionSize(&SD), sh_link, |
| 1189 | sh_info, SD.getAlignment(), |
| 1190 | Section.getEntrySize()); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | ELFObjectWriter::ELFObjectWriter(raw_ostream &OS, |
| 1195 | bool Is64Bit, |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 1196 | Triple::OSType OSType, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1197 | bool IsLittleEndian, |
| 1198 | bool HasRelocationAddend) |
| 1199 | : MCObjectWriter(OS, IsLittleEndian) |
| 1200 | { |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 1201 | Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend, OSType); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | ELFObjectWriter::~ELFObjectWriter() { |
| 1205 | delete (ELFObjectWriterImpl*) Impl; |
| 1206 | } |
| 1207 | |
| 1208 | void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) { |
| 1209 | ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm); |
| 1210 | } |
| 1211 | |
| 1212 | void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, |
| 1213 | const MCAsmLayout &Layout, |
| 1214 | const MCFragment *Fragment, |
| 1215 | const MCFixup &Fixup, MCValue Target, |
| 1216 | uint64_t &FixedValue) { |
| 1217 | ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup, |
| 1218 | Target, FixedValue); |
| 1219 | } |
| 1220 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1221 | bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm, |
| 1222 | const MCValue Target, |
| 1223 | bool IsPCRel, |
| 1224 | const MCFragment *DF) const { |
| 1225 | return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target, |
| 1226 | IsPCRel, DF); |
| 1227 | } |
| 1228 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 1229 | void ELFObjectWriter::WriteObject(MCAssembler &Asm, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1230 | const MCAsmLayout &Layout) { |
| 1231 | ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout); |
| 1232 | } |