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