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