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