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" |
Evan Cheng | a7cfc08 | 2011-07-23 00:45:41 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 29 | |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 30 | #include "../Target/Mips/MCTargetDesc/MipsFixupKinds.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 31 | |
| 32 | #include <vector> |
| 33 | using namespace llvm; |
| 34 | |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 35 | #undef DEBUG_TYPE |
| 36 | #define DEBUG_TYPE "reloc-info" |
| 37 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 38 | bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { |
| 39 | const MCFixupKindInfo &FKI = |
| 40 | Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind); |
| 41 | |
| 42 | return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; |
| 43 | } |
| 44 | |
| 45 | bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) { |
| 46 | switch (Variant) { |
| 47 | default: |
| 48 | return false; |
| 49 | case MCSymbolRefExpr::VK_GOT: |
| 50 | case MCSymbolRefExpr::VK_PLT: |
| 51 | case MCSymbolRefExpr::VK_GOTPCREL: |
Rafael Espindola | 378e0ec | 2011-06-05 01:20:06 +0000 | [diff] [blame] | 52 | case MCSymbolRefExpr::VK_GOTOFF: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 53 | case MCSymbolRefExpr::VK_TPOFF: |
| 54 | case MCSymbolRefExpr::VK_TLSGD: |
| 55 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 56 | case MCSymbolRefExpr::VK_INDNTPOFF: |
| 57 | case MCSymbolRefExpr::VK_NTPOFF: |
| 58 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 59 | case MCSymbolRefExpr::VK_TLSLDM: |
| 60 | case MCSymbolRefExpr::VK_DTPOFF: |
| 61 | case MCSymbolRefExpr::VK_TLSLD: |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 66 | ELFObjectWriter::~ELFObjectWriter() |
| 67 | {} |
| 68 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 69 | // Emit the ELF header. |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 70 | void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize, |
| 71 | unsigned NumberOfSections) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 72 | // ELF Header |
| 73 | // ---------- |
| 74 | // |
| 75 | // Note |
| 76 | // ---- |
| 77 | // emitWord method behaves differently for ELF32 and ELF64, writing |
| 78 | // 4 bytes in the former and 8 in the latter. |
| 79 | |
| 80 | Write8(0x7f); // e_ident[EI_MAG0] |
| 81 | Write8('E'); // e_ident[EI_MAG1] |
| 82 | Write8('L'); // e_ident[EI_MAG2] |
| 83 | Write8('F'); // e_ident[EI_MAG3] |
| 84 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 85 | Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 86 | |
| 87 | // e_ident[EI_DATA] |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 88 | Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 89 | |
| 90 | Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 91 | // e_ident[EI_OSABI] |
Rafael Espindola | dc9a8a3 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 92 | Write8(TargetObjectWriter->getOSABI()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 93 | Write8(0); // e_ident[EI_ABIVERSION] |
| 94 | |
| 95 | WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD); |
| 96 | |
| 97 | Write16(ELF::ET_REL); // e_type |
| 98 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 99 | Write16(TargetObjectWriter->getEMachine()); // e_machine = target |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 100 | |
| 101 | Write32(ELF::EV_CURRENT); // e_version |
| 102 | WriteWord(0); // e_entry, no entry point in .o file |
| 103 | WriteWord(0); // e_phoff, no program header for .o |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 104 | WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) : |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 105 | sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 106 | |
Jason W Kim | 2d7a53a | 2011-02-04 21:41:11 +0000 | [diff] [blame] | 107 | // e_flags = whatever the target wants |
Rafael Espindola | e8526d0 | 2011-12-21 20:09:46 +0000 | [diff] [blame] | 108 | Write32(getEFlags()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 109 | |
| 110 | // e_ehsize = ELF header size |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 111 | Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 112 | |
| 113 | Write16(0); // e_phentsize = prog header entry size |
| 114 | Write16(0); // e_phnum = # prog header entries = 0 |
| 115 | |
| 116 | // e_shentsize = Section header entry size |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 117 | Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 118 | |
| 119 | // e_shnum = # of section header ents |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 120 | if (NumberOfSections >= ELF::SHN_LORESERVE) |
Nick Lewycky | aaae3f6 | 2011-10-07 20:56:23 +0000 | [diff] [blame] | 121 | Write16(ELF::SHN_UNDEF); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 122 | else |
| 123 | Write16(NumberOfSections); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 124 | |
| 125 | // e_shstrndx = Section # of '.shstrtab' |
Nick Lewycky | b3429d3 | 2011-10-07 20:58:24 +0000 | [diff] [blame] | 126 | if (ShstrtabIndex >= ELF::SHN_LORESERVE) |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 127 | Write16(ELF::SHN_XINDEX); |
| 128 | else |
| 129 | Write16(ShstrtabIndex); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 132 | void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF, |
| 133 | MCDataFragment *ShndxF, |
| 134 | uint64_t name, |
| 135 | uint8_t info, uint64_t value, |
| 136 | uint64_t size, uint8_t other, |
| 137 | uint32_t shndx, |
| 138 | bool Reserved) { |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 139 | if (ShndxF) { |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 140 | if (shndx >= ELF::SHN_LORESERVE && !Reserved) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 141 | String32(*ShndxF, shndx); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 142 | else |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 143 | String32(*ShndxF, 0); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 146 | uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ? |
| 147 | uint16_t(ELF::SHN_XINDEX) : shndx; |
| 148 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 149 | if (is64Bit()) { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 150 | String32(*SymtabF, name); // st_name |
| 151 | String8(*SymtabF, info); // st_info |
| 152 | String8(*SymtabF, other); // st_other |
| 153 | String16(*SymtabF, Index); // st_shndx |
| 154 | String64(*SymtabF, value); // st_value |
| 155 | String64(*SymtabF, size); // st_size |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 156 | } else { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 157 | String32(*SymtabF, name); // st_name |
| 158 | String32(*SymtabF, value); // st_value |
| 159 | String32(*SymtabF, size); // st_size |
| 160 | String8(*SymtabF, info); // st_info |
| 161 | String8(*SymtabF, other); // st_other |
| 162 | String16(*SymtabF, Index); // st_shndx |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 166 | uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data, |
| 167 | const MCAsmLayout &Layout) { |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 168 | if (Data.isCommon() && Data.isExternal()) |
| 169 | return Data.getCommonAlignment(); |
| 170 | |
| 171 | const MCSymbol &Symbol = Data.getSymbol(); |
Roman Divacky | d149186 | 2010-12-20 21:14:39 +0000 | [diff] [blame] | 172 | |
| 173 | if (Symbol.isAbsolute() && Symbol.isVariable()) { |
| 174 | if (const MCExpr *Value = Symbol.getVariableValue()) { |
| 175 | int64_t IntValue; |
| 176 | if (Value->EvaluateAsAbsolute(IntValue, Layout)) |
Jim Grosbach | f68a26b | 2011-12-06 00:13:09 +0000 | [diff] [blame] | 177 | return (uint64_t)IntValue; |
Roman Divacky | d149186 | 2010-12-20 21:14:39 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 181 | if (!Symbol.isInSection()) |
| 182 | return 0; |
| 183 | |
Rafael Espindola | 6469540 | 2011-05-16 16:17:21 +0000 | [diff] [blame] | 184 | |
| 185 | if (Data.getFragment()) { |
| 186 | if (Data.getFlags() & ELF_Other_ThumbFunc) |
| 187 | return Layout.getSymbolOffset(&Data)+1; |
| 188 | else |
| 189 | return Layout.getSymbolOffset(&Data); |
| 190 | } |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 195 | void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm, |
| 196 | const MCAsmLayout &Layout) { |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 197 | // The presence of symbol versions causes undefined symbols and |
| 198 | // versions declared with @@@ to be renamed. |
| 199 | |
| 200 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 201 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 202 | const MCSymbol &Alias = it->getSymbol(); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 203 | const MCSymbol &Symbol = Alias.AliasedSymbol(); |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 204 | MCSymbolData &SD = Asm.getSymbolData(Symbol); |
| 205 | |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 206 | // Not an alias. |
| 207 | if (&Symbol == &Alias) |
| 208 | continue; |
| 209 | |
Benjamin Kramer | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 210 | StringRef AliasName = Alias.getName(); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 211 | size_t Pos = AliasName.find('@'); |
| 212 | if (Pos == StringRef::npos) |
| 213 | continue; |
| 214 | |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 215 | // Aliases defined with .symvar copy the binding from the symbol they alias. |
| 216 | // This is the first place we are able to copy this information. |
| 217 | it->setExternal(SD.isExternal()); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 218 | MCELF::SetBinding(*it, MCELF::GetBinding(SD)); |
Rafael Espindola | f571f9a | 2010-10-28 18:33:03 +0000 | [diff] [blame] | 219 | |
Benjamin Kramer | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 220 | StringRef Rest = AliasName.substr(Pos); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 221 | if (!Symbol.isUndefined() && !Rest.startswith("@@@")) |
| 222 | continue; |
| 223 | |
Rafael Espindola | 83ff4d2 | 2010-10-27 17:56:18 +0000 | [diff] [blame] | 224 | // FIXME: produce a better error message. |
| 225 | if (Symbol.isUndefined() && Rest.startswith("@@") && |
| 226 | !Rest.startswith("@@@")) |
| 227 | report_fatal_error("A @@ version cannot be undefined"); |
| 228 | |
Benjamin Kramer | 07ee632 | 2010-10-27 19:53:52 +0000 | [diff] [blame] | 229 | Renames.insert(std::make_pair(&Symbol, &Alias)); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 233 | void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF, |
| 234 | MCDataFragment *ShndxF, |
| 235 | ELFSymbolData &MSD, |
| 236 | const MCAsmLayout &Layout) { |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 237 | MCSymbolData &OrigData = *MSD.SymbolData; |
Rafael Espindola | de89b01 | 2010-10-15 18:25:33 +0000 | [diff] [blame] | 238 | MCSymbolData &Data = |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 239 | Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol()); |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 240 | |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 241 | bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() || |
| 242 | Data.getSymbol().isVariable(); |
| 243 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 244 | uint8_t Binding = MCELF::GetBinding(OrigData); |
| 245 | uint8_t Visibility = MCELF::GetVisibility(OrigData); |
| 246 | uint8_t Type = MCELF::GetType(Data); |
Rafael Espindola | 152c106 | 2010-10-06 21:02:29 +0000 | [diff] [blame] | 247 | |
| 248 | uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift); |
| 249 | uint8_t Other = Visibility; |
| 250 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 251 | uint64_t Value = SymbolValue(Data, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 252 | uint64_t Size = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 253 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 254 | assert(!(Data.isCommon() && !Data.isExternal())); |
| 255 | |
Rafael Espindola | f012124 | 2010-12-22 16:03:00 +0000 | [diff] [blame] | 256 | const MCExpr *ESize = Data.getSize(); |
| 257 | if (ESize) { |
| 258 | int64_t Res; |
| 259 | if (!ESize->EvaluateAsAbsolute(Res, Layout)) |
| 260 | report_fatal_error("Size expression must be absolute."); |
| 261 | Size = Res; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Write out the symbol table entry |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 265 | WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value, |
| 266 | Size, Other, MSD.SectionIndex, IsReserved); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 269 | void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF, |
| 270 | MCDataFragment *ShndxF, |
| 271 | const MCAssembler &Asm, |
| 272 | const MCAsmLayout &Layout, |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 273 | const SectionIndexMapTy &SectionIndexMap) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 274 | // The string table must be emitted first because we need the index |
| 275 | // into the string table for all the symbol names. |
| 276 | assert(StringTable.size() && "Missing string table"); |
| 277 | |
| 278 | // FIXME: Make sure the start of the symbol table is aligned. |
| 279 | |
| 280 | // The first entry is the undefined symbol entry. |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 281 | WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 282 | |
| 283 | // Write the symbol table entries. |
| 284 | LastLocalSymbolIndex = LocalSymbolData.size() + 1; |
| 285 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { |
| 286 | ELFSymbolData &MSD = LocalSymbolData[i]; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 287 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 290 | // Write out a symbol table entry for each regular section. |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 291 | for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e; |
| 292 | ++i) { |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 293 | const MCSectionELF &Section = |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 294 | static_cast<const MCSectionELF&>(i->getSection()); |
| 295 | if (Section.getType() == ELF::SHT_RELA || |
| 296 | Section.getType() == ELF::SHT_REL || |
| 297 | Section.getType() == ELF::SHT_STRTAB || |
Nick Lewycky | d2fdb4a | 2011-10-07 23:29:53 +0000 | [diff] [blame] | 298 | Section.getType() == ELF::SHT_SYMTAB || |
| 299 | Section.getType() == ELF::SHT_SYMTAB_SHNDX) |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 300 | continue; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 301 | WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0, |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 302 | ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), |
| 303 | false); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 304 | LastLocalSymbolIndex++; |
| 305 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 306 | |
| 307 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { |
| 308 | ELFSymbolData &MSD = ExternalSymbolData[i]; |
| 309 | MCSymbolData &Data = *MSD.SymbolData; |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 310 | assert(((Data.getFlags() & ELF_STB_Global) || |
| 311 | (Data.getFlags() & ELF_STB_Weak)) && |
| 312 | "External symbol requires STB_GLOBAL or STB_WEAK flag"); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 313 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 314 | if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 315 | LastLocalSymbolIndex++; |
| 316 | } |
| 317 | |
| 318 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { |
| 319 | ELFSymbolData &MSD = UndefinedSymbolData[i]; |
| 320 | MCSymbolData &Data = *MSD.SymbolData; |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 321 | WriteSymbol(SymtabF, ShndxF, MSD, Layout); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 322 | if (MCELF::GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 323 | LastLocalSymbolIndex++; |
| 324 | } |
| 325 | } |
| 326 | |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 327 | const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm, |
| 328 | const MCValue &Target, |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 329 | const MCFragment &F, |
| 330 | const MCFixup &Fixup, |
| 331 | bool IsPCRel) const { |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 332 | const MCSymbol &Symbol = Target.getSymA()->getSymbol(); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 333 | const MCSymbol &ASymbol = Symbol.AliasedSymbol(); |
| 334 | const MCSymbol *Renamed = Renames.lookup(&Symbol); |
| 335 | const MCSymbolData &SD = Asm.getSymbolData(Symbol); |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 336 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 337 | if (ASymbol.isUndefined()) { |
| 338 | if (Renamed) |
| 339 | return Renamed; |
| 340 | return &ASymbol; |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 341 | } |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 342 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 343 | if (SD.isExternal()) { |
| 344 | if (Renamed) |
| 345 | return Renamed; |
| 346 | return &Symbol; |
| 347 | } |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 348 | |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 349 | const MCSectionELF &Section = |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 350 | static_cast<const MCSectionELF&>(ASymbol.getSection()); |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 351 | const SectionKind secKind = Section.getKind(); |
Rafael Espindola | 7eae36b | 2010-09-30 20:18:35 +0000 | [diff] [blame] | 352 | |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 353 | if (secKind.isBSS()) |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 354 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
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.isThreadLocal()) { |
| 357 | if (Renamed) |
| 358 | return Renamed; |
| 359 | return &Symbol; |
| 360 | } |
| 361 | |
Rafael Espindola | 8cecf25 | 2010-10-06 16:23:36 +0000 | [diff] [blame] | 362 | MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind(); |
Rafael Espindola | 3729d00 | 2010-10-05 23:57:26 +0000 | [diff] [blame] | 363 | const MCSectionELF &Sec2 = |
| 364 | static_cast<const MCSectionELF&>(F.getParent()->getSection()); |
| 365 | |
Rafael Espindola | 8cecf25 | 2010-10-06 16:23:36 +0000 | [diff] [blame] | 366 | if (&Sec2 != &Section && |
Rafael Espindola | c97f80e | 2010-10-18 16:38:04 +0000 | [diff] [blame] | 367 | (Kind == MCSymbolRefExpr::VK_PLT || |
| 368 | Kind == MCSymbolRefExpr::VK_GOTPCREL || |
Rafael Espindola | 2595873 | 2010-11-24 21:57:39 +0000 | [diff] [blame] | 369 | Kind == MCSymbolRefExpr::VK_GOTOFF)) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 370 | if (Renamed) |
| 371 | return Renamed; |
| 372 | return &Symbol; |
| 373 | } |
Rafael Espindola | 3729d00 | 2010-10-05 23:57:26 +0000 | [diff] [blame] | 374 | |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 375 | if (Section.getFlags() & ELF::SHF_MERGE) { |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 376 | if (Target.getConstant() == 0) |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 377 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 378 | if (Renamed) |
| 379 | return Renamed; |
| 380 | return &Symbol; |
Rafael Espindola | 1f52dfe | 2010-11-14 23:53:26 +0000 | [diff] [blame] | 381 | } |
Rafael Espindola | c97f80e | 2010-10-18 16:38:04 +0000 | [diff] [blame] | 382 | |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 383 | return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel); |
| 384 | |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 387 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 388 | void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, |
| 389 | const MCAsmLayout &Layout, |
| 390 | const MCFragment *Fragment, |
| 391 | const MCFixup &Fixup, |
| 392 | MCValue Target, |
| 393 | uint64_t &FixedValue) { |
| 394 | int64_t Addend = 0; |
| 395 | int Index = 0; |
| 396 | int64_t Value = Target.getConstant(); |
| 397 | const MCSymbol *RelocSymbol = NULL; |
| 398 | |
Rafael Espindola | 127a6a4 | 2010-12-17 07:28:17 +0000 | [diff] [blame] | 399 | bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind()); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 400 | if (!Target.isAbsolute()) { |
| 401 | const MCSymbol &Symbol = Target.getSymA()->getSymbol(); |
| 402 | const MCSymbol &ASymbol = Symbol.AliasedSymbol(); |
Jason W Kim | e964d11 | 2011-05-11 22:53:06 +0000 | [diff] [blame] | 403 | RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 404 | |
| 405 | if (const MCSymbolRefExpr *RefB = Target.getSymB()) { |
| 406 | const MCSymbol &SymbolB = RefB->getSymbol(); |
| 407 | MCSymbolData &SDB = Asm.getSymbolData(SymbolB); |
| 408 | IsPCRel = true; |
| 409 | |
| 410 | // Offset of the symbol in the section |
| 411 | int64_t a = Layout.getSymbolOffset(&SDB); |
| 412 | |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 413 | // Offset of the relocation in the section |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 414 | int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
| 415 | Value += b - a; |
| 416 | } |
| 417 | |
| 418 | if (!RelocSymbol) { |
| 419 | MCSymbolData &SD = Asm.getSymbolData(ASymbol); |
| 420 | MCFragment *F = SD.getFragment(); |
| 421 | |
| 422 | Index = F->getParent()->getOrdinal() + 1; |
| 423 | |
| 424 | // Offset of the symbol in the section |
| 425 | Value += Layout.getSymbolOffset(&SD); |
| 426 | } else { |
| 427 | if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref) |
| 428 | WeakrefUsedInReloc.insert(RelocSymbol); |
| 429 | else |
| 430 | UsedInReloc.insert(RelocSymbol); |
| 431 | Index = -1; |
| 432 | } |
| 433 | Addend = Value; |
| 434 | // Compensate for the addend on i386. |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 435 | if (is64Bit()) |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 436 | Value = 0; |
| 437 | } |
| 438 | |
| 439 | FixedValue = Value; |
| 440 | unsigned Type = GetRelocType(Target, Fixup, IsPCRel, |
| 441 | (RelocSymbol != 0), Addend); |
Rafael Espindola | c677e79 | 2011-12-21 14:26:29 +0000 | [diff] [blame] | 442 | MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ? |
| 443 | MCSymbolRefExpr::VK_None : Target.getSymA()->getKind(); |
| 444 | if (RelocNeedsGOT(Modifier)) |
| 445 | NeedsGOT = true; |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 446 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 447 | uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) + |
| 448 | Fixup.getOffset(); |
Roman Divacky | 2a66cea | 2011-08-04 19:08:19 +0000 | [diff] [blame] | 449 | |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame^] | 450 | // FIXME: no tests cover this. Is adjustFixupOffset dead code? |
| 451 | TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset); |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 452 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 453 | if (!hasRelocationAddend()) |
| 454 | Addend = 0; |
Rafael Espindola | bbf9c4a | 2011-08-04 13:05:26 +0000 | [diff] [blame] | 455 | |
| 456 | if (is64Bit()) |
| 457 | assert(isInt<64>(Addend)); |
| 458 | else |
| 459 | assert(isInt<32>(Addend)); |
| 460 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 461 | ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend); |
| 462 | Relocations[Fragment->getParent()].push_back(ERE); |
| 463 | } |
| 464 | |
| 465 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 466 | uint64_t |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 467 | ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 468 | const MCSymbol *S) { |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 469 | MCSymbolData &SD = Asm.getSymbolData(*S); |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 470 | return SD.getIndex(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 473 | bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm, |
| 474 | const MCSymbolData &Data, |
| 475 | bool Used, bool Renamed) { |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 476 | if (Data.getFlags() & ELF_Other_Weakref) |
| 477 | return false; |
| 478 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 479 | if (Used) |
| 480 | return true; |
| 481 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 482 | if (Renamed) |
| 483 | return false; |
| 484 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 485 | const MCSymbol &Symbol = Data.getSymbol(); |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 486 | |
Rafael Espindola | d179886 | 2010-10-29 23:09:31 +0000 | [diff] [blame] | 487 | if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_") |
| 488 | return true; |
| 489 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 490 | const MCSymbol &A = Symbol.AliasedSymbol(); |
Rafael Espindola | 21451e5 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 491 | if (Symbol.isVariable() && !A.isVariable() && A.isUndefined()) |
| 492 | return false; |
| 493 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 494 | bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL; |
Rafael Espindola | 21451e5 | 2011-02-23 20:22:07 +0000 | [diff] [blame] | 495 | if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal) |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 496 | return false; |
| 497 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 498 | if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined()) |
| 499 | return false; |
| 500 | |
Rafael Espindola | bd70118 | 2010-10-19 19:31:37 +0000 | [diff] [blame] | 501 | if (Symbol.isTemporary()) |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 502 | return false; |
| 503 | |
| 504 | return true; |
| 505 | } |
| 506 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 507 | bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature, |
| 508 | bool isUsedInReloc) { |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 509 | if (Data.isExternal()) |
| 510 | return false; |
| 511 | |
| 512 | const MCSymbol &Symbol = Data.getSymbol(); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 513 | const MCSymbol &RefSymbol = Symbol.AliasedSymbol(); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 514 | |
| 515 | if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) { |
| 516 | if (isSignature && !isUsedInReloc) |
| 517 | return true; |
| 518 | |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 519 | return false; |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 520 | } |
Rafael Espindola | 737cd21 | 2010-10-05 18:01:23 +0000 | [diff] [blame] | 521 | |
| 522 | return true; |
| 523 | } |
| 524 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 525 | void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 526 | SectionIndexMapTy &SectionIndexMap, |
| 527 | const RelMapTy &RelMap) { |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 528 | unsigned Index = 1; |
| 529 | for (MCAssembler::iterator it = Asm.begin(), |
| 530 | ie = Asm.end(); it != ie; ++it) { |
| 531 | const MCSectionELF &Section = |
| 532 | static_cast<const MCSectionELF &>(it->getSection()); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 533 | if (Section.getType() != ELF::SHT_GROUP) |
| 534 | continue; |
| 535 | SectionIndexMap[&Section] = Index++; |
| 536 | } |
| 537 | |
| 538 | for (MCAssembler::iterator it = Asm.begin(), |
| 539 | ie = Asm.end(); it != ie; ++it) { |
| 540 | const MCSectionELF &Section = |
| 541 | static_cast<const MCSectionELF &>(it->getSection()); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 542 | if (Section.getType() == ELF::SHT_GROUP || |
| 543 | Section.getType() == ELF::SHT_REL || |
| 544 | Section.getType() == ELF::SHT_RELA) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 545 | continue; |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 546 | SectionIndexMap[&Section] = Index++; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 547 | const MCSectionELF *RelSection = RelMap.lookup(&Section); |
| 548 | if (RelSection) |
| 549 | SectionIndexMap[RelSection] = Index++; |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 553 | void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm, |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 554 | const SectionIndexMapTy &SectionIndexMap, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 555 | RevGroupMapTy RevGroupMap, |
| 556 | unsigned NumRegularSections) { |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 557 | // FIXME: Is this the correct place to do this? |
Rafael Espindola | 378e0ec | 2011-06-05 01:20:06 +0000 | [diff] [blame] | 558 | // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed? |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 559 | if (NeedsGOT) { |
| 560 | llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_"; |
| 561 | MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name); |
| 562 | MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym); |
| 563 | Data.setExternal(true); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 564 | MCELF::SetBinding(Data, ELF::STB_GLOBAL); |
Rafael Espindola | 5c77c16 | 2010-10-05 15:48:37 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 567 | // Index 0 is always the empty string. |
| 568 | StringMap<uint64_t> StringIndexMap; |
| 569 | StringTable += '\x00'; |
| 570 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 571 | // FIXME: We could optimize suffixes in strtab in the same way we |
| 572 | // optimize them in shstrtab. |
| 573 | |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 574 | // Add the data for the symbols. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 575 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 576 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 577 | const MCSymbol &Symbol = it->getSymbol(); |
| 578 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 579 | bool Used = UsedInReloc.count(&Symbol); |
| 580 | bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 581 | bool isSignature = RevGroupMap.count(&Symbol); |
| 582 | |
| 583 | if (!isInSymtab(Asm, *it, |
| 584 | Used || WeakrefUsed || isSignature, |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 585 | Renames.count(&Symbol))) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 586 | continue; |
| 587 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 588 | ELFSymbolData MSD; |
| 589 | MSD.SymbolData = it; |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 590 | const MCSymbol &RefSymbol = Symbol.AliasedSymbol(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 591 | |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 592 | // Undefined symbols are global, but this is the first place we |
| 593 | // are able to set it. |
| 594 | bool Local = isLocal(*it, isSignature, Used); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 595 | if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 596 | MCSymbolData &SD = Asm.getSymbolData(RefSymbol); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 597 | MCELF::SetBinding(*it, ELF::STB_GLOBAL); |
| 598 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 601 | if (RefSymbol.isUndefined() && !Used && WeakrefUsed) |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 602 | MCELF::SetBinding(*it, ELF::STB_WEAK); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 603 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 604 | if (it->isCommon()) { |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 605 | assert(!Local); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 606 | MSD.SectionIndex = ELF::SHN_COMMON; |
Rafael Espindola | bf052ac | 2010-10-27 16:04:30 +0000 | [diff] [blame] | 607 | } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) { |
Rafael Espindola | a0949b5 | 2010-10-14 16:34:44 +0000 | [diff] [blame] | 608 | MSD.SectionIndex = ELF::SHN_ABS; |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 609 | } else if (RefSymbol.isUndefined()) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 610 | if (isSignature && !Used) |
| 611 | MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]); |
| 612 | else |
| 613 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 614 | } else { |
Rafael Espindola | bab2a80 | 2010-11-10 21:51:05 +0000 | [diff] [blame] | 615 | const MCSectionELF &Section = |
| 616 | static_cast<const MCSectionELF&>(RefSymbol.getSection()); |
| 617 | MSD.SectionIndex = SectionIndexMap.lookup(&Section); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 618 | if (MSD.SectionIndex >= ELF::SHN_LORESERVE) |
| 619 | NeedsSymtabShndx = true; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 620 | assert(MSD.SectionIndex && "Invalid section index!"); |
Rafael Espindola | 5df0b65 | 2010-10-15 15:39:06 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 623 | // The @@@ in symbol version is replaced with @ in undefined symbols and |
| 624 | // @@ in defined ones. |
| 625 | StringRef Name = Symbol.getName(); |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 626 | SmallString<32> Buf; |
| 627 | |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 628 | size_t Pos = Name.find("@@@"); |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 629 | if (Pos != StringRef::npos) { |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 630 | Buf += Name.substr(0, Pos); |
| 631 | unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1; |
| 632 | Buf += Name.substr(Pos + Skip); |
| 633 | Name = Buf; |
Rafael Espindola | 8818213 | 2010-10-27 15:18:17 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 636 | uint64_t &Entry = StringIndexMap[Name]; |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 637 | if (!Entry) { |
| 638 | Entry = StringTable.size(); |
Benjamin Kramer | 1261a2f | 2010-11-12 19:26:04 +0000 | [diff] [blame] | 639 | StringTable += Name; |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 640 | StringTable += '\x00'; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 641 | } |
Rafael Espindola | a686696 | 2010-10-27 14:44:52 +0000 | [diff] [blame] | 642 | MSD.StringIndex = Entry; |
| 643 | if (MSD.SectionIndex == ELF::SHN_UNDEF) |
| 644 | UndefinedSymbolData.push_back(MSD); |
| 645 | else if (Local) |
| 646 | LocalSymbolData.push_back(MSD); |
| 647 | else |
| 648 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // Symbols are required to be in lexicographic order. |
| 652 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 653 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 654 | array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 655 | |
| 656 | // Set the symbol indices. Local symbols must come before all other |
| 657 | // symbols with non-local bindings. |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 658 | unsigned Index = 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 659 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 660 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
Rafael Espindola | ab4a7af | 2010-11-14 03:12:24 +0000 | [diff] [blame] | 661 | |
| 662 | Index += NumRegularSections; |
| 663 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 664 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 665 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 666 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 667 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
Nick Lewycky | 7aabcb1 | 2011-10-11 03:54:50 +0000 | [diff] [blame] | 668 | |
| 669 | if (NumRegularSections > ELF::SHN_LORESERVE) |
| 670 | NeedsSymtabShndx = true; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 673 | void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm, |
| 674 | MCAsmLayout &Layout, |
| 675 | RelMapTy &RelMap) { |
| 676 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 677 | ie = Asm.end(); it != ie; ++it) { |
| 678 | const MCSectionData &SD = *it; |
| 679 | if (Relocations[&SD].empty()) |
| 680 | continue; |
| 681 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 682 | MCContext &Ctx = Asm.getContext(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 683 | const MCSectionELF &Section = |
| 684 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 685 | |
| 686 | const StringRef SectionName = Section.getSectionName(); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 687 | std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel"; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 688 | RelaSectionName += SectionName; |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 689 | |
| 690 | unsigned EntrySize; |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 691 | if (hasRelocationAddend()) |
| 692 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 693 | else |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 694 | EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 695 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 696 | const MCSectionELF *RelaSection = |
| 697 | Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ? |
| 698 | ELF::SHT_RELA : ELF::SHT_REL, 0, |
| 699 | SectionKind::getReadOnly(), |
| 700 | EntrySize, ""); |
| 701 | RelMap[&Section] = RelaSection; |
| 702 | Asm.getOrCreateSectionData(*RelaSection); |
| 703 | } |
| 704 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 705 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 706 | void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout, |
| 707 | const RelMapTy &RelMap) { |
| 708 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 709 | ie = Asm.end(); it != ie; ++it) { |
| 710 | const MCSectionData &SD = *it; |
| 711 | const MCSectionELF &Section = |
| 712 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 713 | |
| 714 | const MCSectionELF *RelaSection = RelMap.lookup(&Section); |
| 715 | if (!RelaSection) |
| 716 | continue; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 717 | MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 718 | RelaSD.setAlignment(is64Bit() ? 8 : 4); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 719 | |
| 720 | MCDataFragment *F = new MCDataFragment(&RelaSD); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 721 | WriteRelocationsFragment(Asm, F, &*it); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 725 | void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, |
| 726 | uint64_t Flags, uint64_t Address, |
| 727 | uint64_t Offset, uint64_t Size, |
| 728 | uint32_t Link, uint32_t Info, |
| 729 | uint64_t Alignment, |
| 730 | uint64_t EntrySize) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 731 | Write32(Name); // sh_name: index into string table |
| 732 | Write32(Type); // sh_type |
| 733 | WriteWord(Flags); // sh_flags |
| 734 | WriteWord(Address); // sh_addr |
| 735 | WriteWord(Offset); // sh_offset |
| 736 | WriteWord(Size); // sh_size |
| 737 | Write32(Link); // sh_link |
| 738 | Write32(Info); // sh_info |
| 739 | WriteWord(Alignment); // sh_addralign |
| 740 | WriteWord(EntrySize); // sh_entsize |
| 741 | } |
| 742 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 743 | void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm, |
| 744 | MCDataFragment *F, |
| 745 | const MCSectionData *SD) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 746 | std::vector<ELFRelocationEntry> &Relocs = Relocations[SD]; |
| 747 | // sort by the r_offset just like gnu as does |
| 748 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 749 | |
| 750 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 751 | ELFRelocationEntry entry = Relocs[e - i - 1]; |
| 752 | |
Rafael Espindola | 12203cc | 2010-11-21 00:48:25 +0000 | [diff] [blame] | 753 | if (!entry.Index) |
| 754 | ; |
| 755 | else if (entry.Index < 0) |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 756 | entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol); |
| 757 | else |
Rafael Espindola | 12203cc | 2010-11-21 00:48:25 +0000 | [diff] [blame] | 758 | entry.Index += LocalSymbolData.size(); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 759 | if (is64Bit()) { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 760 | String64(*F, entry.r_offset); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 761 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 762 | struct ELF::Elf64_Rela ERE64; |
| 763 | ERE64.setSymbolAndType(entry.Index, entry.Type); |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 764 | String64(*F, ERE64.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 765 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 766 | if (hasRelocationAddend()) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 767 | String64(*F, entry.r_addend); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 768 | } else { |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 769 | String32(*F, entry.r_offset); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 770 | |
Rafael Espindola | 8f413fa | 2010-10-05 15:11:03 +0000 | [diff] [blame] | 771 | struct ELF::Elf32_Rela ERE32; |
| 772 | ERE32.setSymbolAndType(entry.Index, entry.Type); |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 773 | String32(*F, ERE32.r_info); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 774 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 775 | if (hasRelocationAddend()) |
Rafael Espindola | af3d38f | 2010-11-10 20:02:59 +0000 | [diff] [blame] | 776 | String32(*F, entry.r_addend); |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 777 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
| 780 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 781 | static int compareBySuffix(const void *a, const void *b) { |
| 782 | const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a); |
| 783 | const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b); |
| 784 | const StringRef &NameA = secA->getSectionName(); |
| 785 | const StringRef &NameB = secB->getSectionName(); |
| 786 | const unsigned sizeA = NameA.size(); |
| 787 | const unsigned sizeB = NameB.size(); |
| 788 | const unsigned len = std::min(sizeA, sizeB); |
| 789 | for (unsigned int i = 0; i < len; ++i) { |
| 790 | char ca = NameA[sizeA - i - 1]; |
| 791 | char cb = NameB[sizeB - i - 1]; |
| 792 | if (ca != cb) |
| 793 | return cb - ca; |
| 794 | } |
| 795 | |
| 796 | return sizeB - sizeA; |
| 797 | } |
| 798 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 799 | void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm, |
| 800 | MCAsmLayout &Layout, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 801 | SectionIndexMapTy &SectionIndexMap, |
| 802 | const RelMapTy &RelMap) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 803 | MCContext &Ctx = Asm.getContext(); |
| 804 | MCDataFragment *F; |
| 805 | |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 806 | unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 807 | |
Rafael Espindola | 38738bf | 2010-09-22 19:04:41 +0000 | [diff] [blame] | 808 | // 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] | 809 | const MCSectionELF *ShstrtabSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 810 | Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 811 | SectionKind::getReadOnly()); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 812 | MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); |
| 813 | ShstrtabSD.setAlignment(1); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 814 | |
Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 815 | const MCSectionELF *SymtabSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 816 | Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 817 | SectionKind::getReadOnly(), |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 818 | EntrySize, ""); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 819 | MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 820 | SymtabSD.setAlignment(is64Bit() ? 8 : 4); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 821 | |
| 822 | MCSectionData *SymtabShndxSD = NULL; |
| 823 | |
| 824 | if (NeedsSymtabShndx) { |
Rafael Espindola | 4283f4b | 2010-11-10 19:05:07 +0000 | [diff] [blame] | 825 | const MCSectionELF *SymtabShndxSection = |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 826 | Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 827 | SectionKind::getReadOnly(), 4, ""); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 828 | SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection); |
| 829 | SymtabShndxSD->setAlignment(4); |
| 830 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 831 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 832 | const MCSectionELF *StrtabSection; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 833 | StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 834 | SectionKind::getReadOnly()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 835 | MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); |
| 836 | StrtabSD.setAlignment(1); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 837 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 838 | ComputeIndexMap(Asm, SectionIndexMap, RelMap); |
| 839 | |
| 840 | ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection); |
| 841 | SymbolTableIndex = SectionIndexMap.lookup(SymtabSection); |
| 842 | StringTableIndex = SectionIndexMap.lookup(StrtabSection); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 843 | |
| 844 | // Symbol table |
| 845 | F = new MCDataFragment(&SymtabSD); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 846 | MCDataFragment *ShndxF = NULL; |
| 847 | if (NeedsSymtabShndx) { |
| 848 | ShndxF = new MCDataFragment(SymtabShndxSD); |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 849 | } |
Rafael Espindola | 4beee3d | 2010-11-10 22:16:43 +0000 | [diff] [blame] | 850 | WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 851 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 852 | F = new MCDataFragment(&StrtabSD); |
| 853 | F->getContents().append(StringTable.begin(), StringTable.end()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 854 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 855 | F = new MCDataFragment(&ShstrtabSD); |
| 856 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 857 | std::vector<const MCSectionELF*> Sections; |
| 858 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 859 | ie = Asm.end(); it != ie; ++it) { |
| 860 | const MCSectionELF &Section = |
| 861 | static_cast<const MCSectionELF&>(it->getSection()); |
| 862 | Sections.push_back(&Section); |
| 863 | } |
| 864 | array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix); |
| 865 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 866 | // Section header string table. |
| 867 | // |
| 868 | // The first entry of a string table holds a null character so skip |
| 869 | // section 0. |
| 870 | uint64_t Index = 1; |
| 871 | F->getContents() += '\x00'; |
| 872 | |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 873 | for (unsigned int I = 0, E = Sections.size(); I != E; ++I) { |
| 874 | const MCSectionELF &Section = *Sections[I]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 875 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 876 | StringRef Name = Section.getSectionName(); |
Rafael Espindola | d5321da | 2011-04-07 23:21:52 +0000 | [diff] [blame] | 877 | if (I != 0) { |
| 878 | StringRef PreviousName = Sections[I - 1]->getSectionName(); |
| 879 | if (PreviousName.endswith(Name)) { |
| 880 | SectionStringTableIndex[&Section] = Index - Name.size() - 1; |
| 881 | continue; |
| 882 | } |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 883 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 884 | // Remember the index into the string table so we can write it |
| 885 | // into the sh_name field of the section header table. |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 886 | SectionStringTableIndex[&Section] = Index; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 887 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 888 | Index += Name.size() + 1; |
| 889 | F->getContents() += Name; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 890 | F->getContents() += '\x00'; |
| 891 | } |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 894 | void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm, |
| 895 | MCAsmLayout &Layout, |
| 896 | GroupMapTy &GroupMap, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 897 | RevGroupMapTy &RevGroupMap, |
| 898 | SectionIndexMapTy &SectionIndexMap, |
| 899 | const RelMapTy &RelMap) { |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 900 | // Create the .note.GNU-stack section if needed. |
| 901 | MCContext &Ctx = Asm.getContext(); |
| 902 | if (Asm.getNoExecStack()) { |
| 903 | const MCSectionELF *GnuStackSection = |
| 904 | Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0, |
| 905 | SectionKind::getReadOnly()); |
| 906 | Asm.getOrCreateSectionData(*GnuStackSection); |
| 907 | } |
| 908 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 909 | // Build the groups |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 910 | for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); |
| 911 | it != ie; ++it) { |
| 912 | const MCSectionELF &Section = |
| 913 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 914 | if (!(Section.getFlags() & ELF::SHF_GROUP)) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 915 | continue; |
| 916 | |
| 917 | const MCSymbol *SignatureSymbol = Section.getGroup(); |
| 918 | Asm.getOrCreateSymbolData(*SignatureSymbol); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 919 | const MCSectionELF *&Group = RevGroupMap[SignatureSymbol]; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 920 | if (!Group) { |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 921 | Group = Ctx.CreateELFGroupSection(); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 922 | MCSectionData &Data = Asm.getOrCreateSectionData(*Group); |
| 923 | Data.setAlignment(4); |
| 924 | MCDataFragment *F = new MCDataFragment(&Data); |
| 925 | String32(*F, ELF::GRP_COMDAT); |
| 926 | } |
| 927 | GroupMap[Group] = SignatureSymbol; |
| 928 | } |
| 929 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 930 | ComputeIndexMap(Asm, SectionIndexMap, RelMap); |
| 931 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 932 | // Add sections to the groups |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 933 | for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 934 | it != ie; ++it) { |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 935 | const MCSectionELF &Section = |
| 936 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 937 | if (!(Section.getFlags() & ELF::SHF_GROUP)) |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 938 | continue; |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 939 | const MCSectionELF *Group = RevGroupMap[Section.getGroup()]; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 940 | MCSectionData &Data = Asm.getOrCreateSectionData(*Group); |
| 941 | // FIXME: we could use the previous fragment |
| 942 | MCDataFragment *F = new MCDataFragment(&Data); |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 943 | unsigned Index = SectionIndexMap.lookup(&Section); |
| 944 | String32(*F, Index); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 945 | } |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 948 | void ELFObjectWriter::WriteSection(MCAssembler &Asm, |
| 949 | const SectionIndexMapTy &SectionIndexMap, |
| 950 | uint32_t GroupSymbolIndex, |
| 951 | uint64_t Offset, uint64_t Size, |
| 952 | uint64_t Alignment, |
| 953 | const MCSectionELF &Section) { |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 954 | uint64_t sh_link = 0; |
| 955 | uint64_t sh_info = 0; |
| 956 | |
| 957 | switch(Section.getType()) { |
| 958 | case ELF::SHT_DYNAMIC: |
| 959 | sh_link = SectionStringTableIndex[&Section]; |
| 960 | sh_info = 0; |
| 961 | break; |
| 962 | |
| 963 | case ELF::SHT_REL: |
| 964 | case ELF::SHT_RELA: { |
| 965 | const MCSectionELF *SymtabSection; |
| 966 | const MCSectionELF *InfoSection; |
| 967 | SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, |
| 968 | 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 969 | SectionKind::getReadOnly()); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 970 | sh_link = SectionIndexMap.lookup(SymtabSection); |
| 971 | assert(sh_link && ".symtab not found"); |
| 972 | |
| 973 | // Remove ".rel" and ".rela" prefixes. |
| 974 | unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; |
| 975 | StringRef SectionName = Section.getSectionName().substr(SecNameLen); |
| 976 | |
| 977 | InfoSection = Asm.getContext().getELFSection(SectionName, |
| 978 | ELF::SHT_PROGBITS, 0, |
Rafael Espindola | 3f2d13c | 2010-11-11 03:40:25 +0000 | [diff] [blame] | 979 | SectionKind::getReadOnly()); |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 980 | sh_info = SectionIndexMap.lookup(InfoSection); |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | case ELF::SHT_SYMTAB: |
| 985 | case ELF::SHT_DYNSYM: |
| 986 | sh_link = StringTableIndex; |
| 987 | sh_info = LastLocalSymbolIndex; |
| 988 | break; |
| 989 | |
| 990 | case ELF::SHT_SYMTAB_SHNDX: |
| 991 | sh_link = SymbolTableIndex; |
| 992 | break; |
| 993 | |
| 994 | case ELF::SHT_PROGBITS: |
| 995 | case ELF::SHT_STRTAB: |
| 996 | case ELF::SHT_NOBITS: |
Rafael Espindola | 9897661 | 2010-12-26 21:30:59 +0000 | [diff] [blame] | 997 | case ELF::SHT_NOTE: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 998 | case ELF::SHT_NULL: |
| 999 | case ELF::SHT_ARM_ATTRIBUTES: |
Jason W Kim | 86a97f2 | 2011-01-12 00:19:25 +0000 | [diff] [blame] | 1000 | case ELF::SHT_INIT_ARRAY: |
| 1001 | case ELF::SHT_FINI_ARRAY: |
| 1002 | case ELF::SHT_PREINIT_ARRAY: |
Rafael Espindola | 0cf5e3d | 2011-01-23 05:43:40 +0000 | [diff] [blame] | 1003 | case ELF::SHT_X86_64_UNWIND: |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1004 | // Nothing to do. |
| 1005 | break; |
| 1006 | |
Nick Lewycky | 4a8d43e | 2011-10-07 23:28:32 +0000 | [diff] [blame] | 1007 | case ELF::SHT_GROUP: |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1008 | sh_link = SymbolTableIndex; |
| 1009 | sh_info = GroupSymbolIndex; |
| 1010 | break; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1011 | |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1012 | default: |
| 1013 | assert(0 && "FIXME: sh_type value not supported!"); |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(), |
| 1018 | Section.getFlags(), 0, Offset, Size, sh_link, sh_info, |
| 1019 | Alignment, Section.getEntrySize()); |
| 1020 | } |
| 1021 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1022 | bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) { |
Rafael Espindola | f8803fe | 2010-12-06 03:48:09 +0000 | [diff] [blame] | 1023 | return SD.getOrdinal() == ~UINT32_C(0) && |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1024 | !SD.getSection().isVirtualSection(); |
| 1025 | } |
| 1026 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1027 | uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1028 | uint64_t Ret = 0; |
| 1029 | for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; |
| 1030 | ++i) { |
| 1031 | const MCFragment &F = *i; |
| 1032 | assert(F.getKind() == MCFragment::FT_Data); |
| 1033 | Ret += cast<MCDataFragment>(F).getContents().size(); |
| 1034 | } |
| 1035 | return Ret; |
| 1036 | } |
| 1037 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1038 | uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout, |
| 1039 | const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1040 | if (IsELFMetaDataSection(SD)) |
| 1041 | return DataSectionSize(SD); |
| 1042 | return Layout.getSectionFileSize(&SD); |
| 1043 | } |
| 1044 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 1045 | uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout, |
| 1046 | const MCSectionData &SD) { |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1047 | if (IsELFMetaDataSection(SD)) |
| 1048 | return DataSectionSize(SD); |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1049 | return Layout.getSectionAddressSize(&SD); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1052 | void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm, |
| 1053 | const MCAsmLayout &Layout, |
| 1054 | const MCSectionELF &Section) { |
| 1055 | uint64_t FileOff = OS.tell(); |
| 1056 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1057 | |
| 1058 | uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment()); |
| 1059 | WriteZeros(Padding); |
| 1060 | FileOff += Padding; |
| 1061 | |
| 1062 | FileOff += GetSectionFileSize(Layout, SD); |
| 1063 | |
| 1064 | if (IsELFMetaDataSection(SD)) { |
| 1065 | for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e; |
| 1066 | ++i) { |
| 1067 | const MCFragment &F = *i; |
| 1068 | assert(F.getKind() == MCFragment::FT_Data); |
| 1069 | WriteBytes(cast<MCDataFragment>(F).getContents().str()); |
| 1070 | } |
| 1071 | } else { |
Jim Grosbach | f77d5b1 | 2011-12-06 00:03:48 +0000 | [diff] [blame] | 1072 | Asm.writeSectionData(&SD, Layout); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1076 | void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm, |
| 1077 | const GroupMapTy &GroupMap, |
| 1078 | const MCAsmLayout &Layout, |
| 1079 | const SectionIndexMapTy &SectionIndexMap, |
| 1080 | const SectionOffsetMapTy &SectionOffsetMap) { |
| 1081 | const unsigned NumSections = Asm.size() + 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1082 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1083 | std::vector<const MCSectionELF*> Sections; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1084 | Sections.resize(NumSections - 1); |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1085 | |
| 1086 | for (SectionIndexMapTy::const_iterator i= |
| 1087 | SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) { |
| 1088 | const std::pair<const MCSectionELF*, uint32_t> &p = *i; |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1089 | Sections[p.second - 1] = p.first; |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1092 | // Null section first. |
Rafael Espindola | 7be2c33 | 2010-10-31 00:16:26 +0000 | [diff] [blame] | 1093 | uint64_t FirstSectionSize = |
| 1094 | NumSections >= ELF::SHN_LORESERVE ? NumSections : 0; |
| 1095 | uint32_t FirstSectionLink = |
| 1096 | ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0; |
| 1097 | WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1098 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1099 | for (unsigned i = 0; i < NumSections - 1; ++i) { |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1100 | const MCSectionELF &Section = *Sections[i]; |
| 1101 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1102 | uint32_t GroupSymbolIndex; |
| 1103 | if (Section.getType() != ELF::SHT_GROUP) |
| 1104 | GroupSymbolIndex = 0; |
| 1105 | else |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1106 | GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, |
| 1107 | GroupMap.lookup(&Section)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1108 | |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 1109 | uint64_t Size = GetSectionAddressSize(Layout, SD); |
Rafael Espindola | 6db8a9f | 2010-12-02 03:09:06 +0000 | [diff] [blame] | 1110 | |
Rafael Espindola | 2ff9e83 | 2010-11-11 18:13:52 +0000 | [diff] [blame] | 1111 | WriteSection(Asm, SectionIndexMap, GroupSymbolIndex, |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1112 | SectionOffsetMap.lookup(&Section), Size, |
Rafael Espindola | c87a94a | 2010-11-10 23:36:59 +0000 | [diff] [blame] | 1113 | SD.getAlignment(), Section); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1117 | void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm, |
| 1118 | std::vector<const MCSectionELF*> &Sections) { |
| 1119 | for (MCAssembler::iterator it = Asm.begin(), |
| 1120 | ie = Asm.end(); it != ie; ++it) { |
| 1121 | const MCSectionELF &Section = |
| 1122 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1123 | if (Section.getType() == ELF::SHT_GROUP) |
| 1124 | Sections.push_back(&Section); |
| 1125 | } |
| 1126 | |
| 1127 | for (MCAssembler::iterator it = Asm.begin(), |
| 1128 | ie = Asm.end(); it != ie; ++it) { |
| 1129 | const MCSectionELF &Section = |
| 1130 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1131 | if (Section.getType() != ELF::SHT_GROUP && |
| 1132 | Section.getType() != ELF::SHT_REL && |
| 1133 | Section.getType() != ELF::SHT_RELA) |
| 1134 | Sections.push_back(&Section); |
| 1135 | } |
| 1136 | |
| 1137 | for (MCAssembler::iterator it = Asm.begin(), |
| 1138 | ie = Asm.end(); it != ie; ++it) { |
| 1139 | const MCSectionELF &Section = |
| 1140 | static_cast<const MCSectionELF &>(it->getSection()); |
| 1141 | if (Section.getType() == ELF::SHT_REL || |
| 1142 | Section.getType() == ELF::SHT_RELA) |
| 1143 | Sections.push_back(&Section); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | void ELFObjectWriter::WriteObject(MCAssembler &Asm, |
| 1148 | const MCAsmLayout &Layout) { |
| 1149 | GroupMapTy GroupMap; |
| 1150 | RevGroupMapTy RevGroupMap; |
| 1151 | SectionIndexMapTy SectionIndexMap; |
| 1152 | |
| 1153 | unsigned NumUserSections = Asm.size(); |
| 1154 | |
| 1155 | DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap; |
| 1156 | CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); |
| 1157 | |
| 1158 | const unsigned NumUserAndRelocSections = Asm.size(); |
| 1159 | CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap, |
| 1160 | RevGroupMap, SectionIndexMap, RelMap); |
| 1161 | const unsigned AllSections = Asm.size(); |
| 1162 | const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections; |
| 1163 | |
| 1164 | unsigned NumRegularSections = NumUserSections + NumIndexedSections; |
| 1165 | |
| 1166 | // Compute symbol table information. |
| 1167 | ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections); |
| 1168 | |
| 1169 | |
| 1170 | WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap); |
| 1171 | |
| 1172 | CreateMetadataSections(const_cast<MCAssembler&>(Asm), |
| 1173 | const_cast<MCAsmLayout&>(Layout), |
| 1174 | SectionIndexMap, |
| 1175 | RelMap); |
| 1176 | |
| 1177 | uint64_t NaturalAlignment = is64Bit() ? 8 : 4; |
| 1178 | uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) : |
| 1179 | sizeof(ELF::Elf32_Ehdr); |
| 1180 | uint64_t FileOff = HeaderSize; |
| 1181 | |
| 1182 | std::vector<const MCSectionELF*> Sections; |
| 1183 | ComputeSectionOrder(Asm, Sections); |
| 1184 | unsigned NumSections = Sections.size(); |
| 1185 | SectionOffsetMapTy SectionOffsetMap; |
| 1186 | for (unsigned i = 0; i < NumRegularSections + 1; ++i) { |
| 1187 | const MCSectionELF &Section = *Sections[i]; |
| 1188 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1189 | |
| 1190 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 1191 | |
| 1192 | // Remember the offset into the file for this section. |
| 1193 | SectionOffsetMap[&Section] = FileOff; |
| 1194 | |
| 1195 | // Get the size of the section in the output file (including padding). |
| 1196 | FileOff += GetSectionFileSize(Layout, SD); |
| 1197 | } |
| 1198 | |
| 1199 | FileOff = RoundUpToAlignment(FileOff, NaturalAlignment); |
| 1200 | |
| 1201 | const unsigned SectionHeaderOffset = FileOff - HeaderSize; |
| 1202 | |
| 1203 | uint64_t SectionHeaderEntrySize = is64Bit() ? |
| 1204 | sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr); |
| 1205 | FileOff += (NumSections + 1) * SectionHeaderEntrySize; |
| 1206 | |
| 1207 | for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) { |
| 1208 | const MCSectionELF &Section = *Sections[i]; |
| 1209 | const MCSectionData &SD = Asm.getOrCreateSectionData(Section); |
| 1210 | |
| 1211 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 1212 | |
| 1213 | // Remember the offset into the file for this section. |
| 1214 | SectionOffsetMap[&Section] = FileOff; |
| 1215 | |
| 1216 | // Get the size of the section in the output file (including padding). |
| 1217 | FileOff += GetSectionFileSize(Layout, SD); |
| 1218 | } |
| 1219 | |
| 1220 | // Write out the ELF header ... |
| 1221 | WriteHeader(SectionHeaderOffset, NumSections + 1); |
| 1222 | |
| 1223 | // ... then the regular sections ... |
| 1224 | // + because of .shstrtab |
| 1225 | for (unsigned i = 0; i < NumRegularSections + 1; ++i) |
| 1226 | WriteDataSectionData(Asm, Layout, *Sections[i]); |
| 1227 | |
| 1228 | FileOff = OS.tell(); |
| 1229 | uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment); |
| 1230 | WriteZeros(Padding); |
| 1231 | |
| 1232 | // ... then the section header table ... |
| 1233 | WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, |
| 1234 | SectionOffsetMap); |
| 1235 | |
| 1236 | FileOff = OS.tell(); |
| 1237 | |
Nick Lewycky | aaae3f6 | 2011-10-07 20:56:23 +0000 | [diff] [blame] | 1238 | // ... and then the remaining sections ... |
Rafael Espindola | 7c18fa8 | 2011-03-20 18:44:20 +0000 | [diff] [blame] | 1239 | for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) |
| 1240 | WriteDataSectionData(Asm, Layout, *Sections[i]); |
| 1241 | } |
| 1242 | |
Eli Friedman | 78c1e17 | 2011-03-03 07:24:36 +0000 | [diff] [blame] | 1243 | bool |
| 1244 | ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, |
| 1245 | const MCSymbolData &DataA, |
| 1246 | const MCFragment &FB, |
| 1247 | bool InSet, |
| 1248 | bool IsPCRel) const { |
| 1249 | if (DataA.getFlags() & ELF_STB_Weak) |
| 1250 | return false; |
| 1251 | return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl( |
| 1252 | Asm, DataA, FB,InSet, IsPCRel); |
| 1253 | } |
| 1254 | |
Rafael Espindola | 6024c97 | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 1255 | MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
| 1256 | raw_ostream &OS, |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1257 | bool IsLittleEndian) { |
| 1258 | switch (MOTW->getEMachine()) { |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1259 | case ELF::EM_386: |
| 1260 | case ELF::EM_X86_64: |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1261 | case ELF::EM_ARM: |
Rafael Espindola | f3a86fb | 2011-12-22 01:57:09 +0000 | [diff] [blame^] | 1262 | case ELF::EM_PPC: |
| 1263 | case ELF::EM_PPC64: |
Rafael Espindola | 69bbda0 | 2011-12-22 00:37:50 +0000 | [diff] [blame] | 1264 | return new ELFObjectWriter(MOTW, OS, IsLittleEndian); break; |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1265 | case ELF::EM_MBLAZE: |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1266 | return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break; |
Akira Hatanaka | 291512f | 2011-09-30 21:55:40 +0000 | [diff] [blame] | 1267 | case ELF::EM_MIPS: |
| 1268 | return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break; |
Benjamin Kramer | 3285877 | 2010-11-15 19:20:50 +0000 | [diff] [blame] | 1269 | default: llvm_unreachable("Unsupported architecture"); break; |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
Rafael Espindola | edae8e1 | 2011-12-21 17:30:17 +0000 | [diff] [blame] | 1273 | unsigned ELFObjectWriter::GetRelocType(const MCValue &Target, |
| 1274 | const MCFixup &Fixup, |
| 1275 | bool IsPCRel, |
| 1276 | bool IsRelocWithSymbol, |
| 1277 | int64_t Addend) const { |
| 1278 | return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel, |
| 1279 | IsRelocWithSymbol, Addend); |
| 1280 | } |
| 1281 | |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1282 | /// START OF SUBCLASSES for ELFObjectWriter |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1283 | //===- MBlazeELFObjectWriter -------------------------------------------===// |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1284 | |
Rafael Espindola | 31f3578 | 2010-12-17 18:01:31 +0000 | [diff] [blame] | 1285 | MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 1286 | raw_ostream &_OS, |
| 1287 | bool IsLittleEndian) |
| 1288 | : ELFObjectWriter(MOTW, _OS, IsLittleEndian) { |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | MBlazeELFObjectWriter::~MBlazeELFObjectWriter() { |
| 1292 | } |
| 1293 | |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 1294 | unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target, |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1295 | const MCFixup &Fixup, |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 1296 | bool IsPCRel, |
| 1297 | bool IsRelocWithSymbol, |
Rafael Espindola | c677e79 | 2011-12-21 14:26:29 +0000 | [diff] [blame] | 1298 | int64_t Addend) const { |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1299 | // determine the type of the relocation |
| 1300 | unsigned Type; |
| 1301 | if (IsPCRel) { |
| 1302 | switch ((unsigned)Fixup.getKind()) { |
| 1303 | default: |
| 1304 | llvm_unreachable("Unimplemented"); |
Rafael Espindola | e04ed7e | 2010-11-28 14:17:56 +0000 | [diff] [blame] | 1305 | case FK_PCRel_4: |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1306 | Type = ELF::R_MICROBLAZE_64_PCREL; |
| 1307 | break; |
Rafael Espindola | e04ed7e | 2010-11-28 14:17:56 +0000 | [diff] [blame] | 1308 | case FK_PCRel_2: |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1309 | Type = ELF::R_MICROBLAZE_32_PCREL; |
| 1310 | break; |
| 1311 | } |
| 1312 | } else { |
| 1313 | switch ((unsigned)Fixup.getKind()) { |
| 1314 | default: llvm_unreachable("invalid fixup kind!"); |
| 1315 | case FK_Data_4: |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 1316 | Type = ((IsRelocWithSymbol || Addend !=0) |
| 1317 | ? ELF::R_MICROBLAZE_32 |
| 1318 | : ELF::R_MICROBLAZE_64); |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1319 | break; |
| 1320 | case FK_Data_2: |
| 1321 | Type = ELF::R_MICROBLAZE_32; |
| 1322 | break; |
| 1323 | } |
| 1324 | } |
Jason W Kim | 56a3990 | 2010-12-06 21:57:34 +0000 | [diff] [blame] | 1325 | return Type; |
Wesley Peck | 4b04713 | 2010-11-21 22:06:28 +0000 | [diff] [blame] | 1326 | } |
Jason W Kim | d3443e9 | 2010-11-15 16:18:39 +0000 | [diff] [blame] | 1327 | |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 1328 | //===- MipsELFObjectWriter -------------------------------------------===// |
| 1329 | |
Akira Hatanaka | 291512f | 2011-09-30 21:55:40 +0000 | [diff] [blame] | 1330 | MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW, |
| 1331 | raw_ostream &_OS, |
| 1332 | bool IsLittleEndian) |
| 1333 | : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {} |
| 1334 | |
| 1335 | MipsELFObjectWriter::~MipsELFObjectWriter() {} |
| 1336 | |
Akira Hatanaka | 84bfc2f | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 1337 | // FIXME: get the real EABI Version from the Triple. |
Rafael Espindola | e99183d | 2011-12-22 00:21:50 +0000 | [diff] [blame] | 1338 | unsigned MipsELFObjectWriter::getEFlags() const { |
Rafael Espindola | e8526d0 | 2011-12-21 20:09:46 +0000 | [diff] [blame] | 1339 | return ELF::EF_MIPS_NOREORDER | ELF::EF_MIPS_ARCH_32R2; |
Akira Hatanaka | 84bfc2f | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Bruno Cardoso Lopes | a00a62a | 2011-12-06 03:34:42 +0000 | [diff] [blame] | 1342 | const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm, |
| 1343 | const MCValue &Target, |
| 1344 | const MCFragment &F, |
| 1345 | const MCFixup &Fixup, |
| 1346 | bool IsPCRel) const { |
| 1347 | assert(Target.getSymA() && "SymA cannot be 0."); |
| 1348 | const MCSymbol &Sym = Target.getSymA()->getSymbol(); |
| 1349 | |
Akira Hatanaka | f3315cf | 2011-12-13 02:27:40 +0000 | [diff] [blame] | 1350 | if (Sym.getSection().getKind().isMergeableCString() || |
| 1351 | Sym.getSection().getKind().isMergeableConst()) |
Bruno Cardoso Lopes | a00a62a | 2011-12-06 03:34:42 +0000 | [diff] [blame] | 1352 | return &Sym; |
| 1353 | |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
Akira Hatanaka | 291512f | 2011-09-30 21:55:40 +0000 | [diff] [blame] | 1357 | unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target, |
| 1358 | const MCFixup &Fixup, |
| 1359 | bool IsPCRel, |
| 1360 | bool IsRelocWithSymbol, |
Rafael Espindola | c677e79 | 2011-12-21 14:26:29 +0000 | [diff] [blame] | 1361 | int64_t Addend) const { |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 1362 | // determine the type of the relocation |
| 1363 | unsigned Type = (unsigned)ELF::R_MIPS_NONE; |
| 1364 | unsigned Kind = (unsigned)Fixup.getKind(); |
| 1365 | |
| 1366 | switch (Kind) { |
| 1367 | default: |
| 1368 | llvm_unreachable("invalid fixup kind!"); |
| 1369 | case FK_Data_4: |
| 1370 | Type = ELF::R_MIPS_32; |
| 1371 | break; |
Akira Hatanaka | 84bfc2f | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 1372 | case FK_GPRel_4: |
| 1373 | Type = ELF::R_MIPS_GPREL32; |
| 1374 | break; |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 1375 | case Mips::fixup_Mips_GPREL16: |
| 1376 | Type = ELF::R_MIPS_GPREL16; |
| 1377 | break; |
| 1378 | case Mips::fixup_Mips_26: |
| 1379 | Type = ELF::R_MIPS_26; |
| 1380 | break; |
| 1381 | case Mips::fixup_Mips_CALL16: |
| 1382 | Type = ELF::R_MIPS_CALL16; |
| 1383 | break; |
Bruno Cardoso Lopes | e3d3572 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 1384 | case Mips::fixup_Mips_GOT_Global: |
| 1385 | case Mips::fixup_Mips_GOT_Local: |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 1386 | Type = ELF::R_MIPS_GOT16; |
| 1387 | break; |
| 1388 | case Mips::fixup_Mips_HI16: |
| 1389 | Type = ELF::R_MIPS_HI16; |
| 1390 | break; |
| 1391 | case Mips::fixup_Mips_LO16: |
| 1392 | Type = ELF::R_MIPS_LO16; |
| 1393 | break; |
| 1394 | case Mips::fixup_Mips_TLSGD: |
| 1395 | Type = ELF::R_MIPS_TLS_GD; |
| 1396 | break; |
| 1397 | case Mips::fixup_Mips_GOTTPREL: |
| 1398 | Type = ELF::R_MIPS_TLS_GOTTPREL; |
| 1399 | break; |
| 1400 | case Mips::fixup_Mips_TPREL_HI: |
| 1401 | Type = ELF::R_MIPS_TLS_TPREL_HI16; |
| 1402 | break; |
| 1403 | case Mips::fixup_Mips_TPREL_LO: |
| 1404 | Type = ELF::R_MIPS_TLS_TPREL_LO16; |
| 1405 | break; |
Akira Hatanaka | bc24985 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 1406 | case Mips::fixup_Mips_TLSLDM: |
| 1407 | Type = ELF::R_MIPS_TLS_LDM; |
| 1408 | break; |
| 1409 | case Mips::fixup_Mips_DTPREL_HI: |
| 1410 | Type = ELF::R_MIPS_TLS_DTPREL_HI16; |
| 1411 | break; |
| 1412 | case Mips::fixup_Mips_DTPREL_LO: |
| 1413 | Type = ELF::R_MIPS_TLS_DTPREL_LO16; |
| 1414 | break; |
Bruno Cardoso Lopes | a0dd4cb | 2011-11-04 22:24:36 +0000 | [diff] [blame] | 1415 | case Mips::fixup_Mips_Branch_PCRel: |
| 1416 | case Mips::fixup_Mips_PC16: |
| 1417 | Type = ELF::R_MIPS_PC16; |
| 1418 | break; |
| 1419 | } |
| 1420 | |
| 1421 | return Type; |
Akira Hatanaka | 291512f | 2011-09-30 21:55:40 +0000 | [diff] [blame] | 1422 | } |