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