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