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