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