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