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