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