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