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