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