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