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