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