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