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