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