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