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 | |
| 14 | #include "llvm/MC/ELFObjectWriter.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/Twine.h" |
| 18 | #include "llvm/MC/MCAssembler.h" |
| 19 | #include "llvm/MC/MCAsmLayout.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 22 | #include "llvm/MC/MCExpr.h" |
| 23 | #include "llvm/MC/MCObjectWriter.h" |
| 24 | #include "llvm/MC/MCSectionELF.h" |
| 25 | #include "llvm/MC/MCSymbol.h" |
| 26 | #include "llvm/MC/MCValue.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include "llvm/Support/ELF.h" |
| 30 | #include "llvm/Target/TargetAsmBackend.h" |
| 31 | |
| 32 | #include "../Target/X86/X86FixupKinds.h" |
| 33 | |
| 34 | #include <vector> |
| 35 | using namespace llvm; |
| 36 | |
Rafael Espindola | ad49cf5 | 2010-09-18 15:03:21 +0000 | [diff] [blame] | 37 | static unsigned GetType(const MCSymbolData &SD) { |
| 38 | uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift; |
| 39 | assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT || |
| 40 | Type == ELF::STT_FUNC || Type == ELF::STT_SECTION || |
| 41 | Type == ELF::STT_FILE || Type == ELF::STT_COMMON || |
| 42 | Type == ELF::STT_TLS); |
| 43 | return Type; |
| 44 | } |
| 45 | |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 46 | static unsigned GetBinding(const MCSymbolData &SD) { |
| 47 | uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift; |
| 48 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 49 | Binding == ELF::STB_WEAK); |
| 50 | return Binding; |
| 51 | } |
| 52 | |
| 53 | static void SetBinding(MCSymbolData &SD, unsigned Binding) { |
| 54 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 55 | Binding == ELF::STB_WEAK); |
| 56 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift); |
| 57 | SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift)); |
| 58 | } |
| 59 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 60 | namespace { |
| 61 | |
| 62 | class ELFObjectWriterImpl { |
| 63 | static bool isFixupKindX86PCRel(unsigned Kind) { |
| 64 | switch (Kind) { |
| 65 | default: |
| 66 | return false; |
| 67 | case X86::reloc_pcrel_1byte: |
| 68 | case X86::reloc_pcrel_4byte: |
| 69 | case X86::reloc_riprel_4byte: |
| 70 | case X86::reloc_riprel_4byte_movq_load: |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 75 | /*static bool isFixupKindX86RIPRel(unsigned Kind) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 76 | return Kind == X86::reloc_riprel_4byte || |
| 77 | Kind == X86::reloc_riprel_4byte_movq_load; |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 78 | }*/ |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | /// ELFSymbolData - Helper struct for containing some precomputed information |
| 82 | /// on symbols. |
| 83 | struct ELFSymbolData { |
| 84 | MCSymbolData *SymbolData; |
| 85 | uint64_t StringIndex; |
| 86 | uint32_t SectionIndex; |
| 87 | |
| 88 | // Support lexicographic sorting. |
| 89 | bool operator<(const ELFSymbolData &RHS) const { |
Rafael Espindola | ad49cf5 | 2010-09-18 15:03:21 +0000 | [diff] [blame] | 90 | if (GetType(*SymbolData) == ELF::STT_FILE) |
| 91 | return true; |
| 92 | if (GetType(*RHS.SymbolData) == ELF::STT_FILE) |
| 93 | return false; |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 94 | return SymbolData->getSymbol().getName() < |
| 95 | RHS.SymbolData->getSymbol().getName(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 96 | } |
| 97 | }; |
| 98 | |
| 99 | /// @name Relocation Data |
| 100 | /// @{ |
| 101 | |
| 102 | struct ELFRelocationEntry { |
| 103 | // Make these big enough for both 32-bit and 64-bit |
| 104 | uint64_t r_offset; |
| 105 | uint64_t r_info; |
| 106 | uint64_t r_addend; |
| 107 | |
| 108 | // Support lexicographic sorting. |
| 109 | bool operator<(const ELFRelocationEntry &RE) const { |
| 110 | return RE.r_offset < r_offset; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | llvm::DenseMap<const MCSectionData*, |
| 115 | std::vector<ELFRelocationEntry> > Relocations; |
| 116 | DenseMap<const MCSection*, uint64_t> SectionStringTableIndex; |
| 117 | |
| 118 | /// @} |
| 119 | /// @name Symbol Table Data |
| 120 | /// @{ |
| 121 | |
| 122 | SmallString<256> StringTable; |
| 123 | std::vector<ELFSymbolData> LocalSymbolData; |
| 124 | std::vector<ELFSymbolData> ExternalSymbolData; |
| 125 | std::vector<ELFSymbolData> UndefinedSymbolData; |
| 126 | |
| 127 | /// @} |
| 128 | |
| 129 | ELFObjectWriter *Writer; |
| 130 | |
| 131 | raw_ostream &OS; |
| 132 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 133 | unsigned Is64Bit : 1; |
| 134 | |
| 135 | bool HasRelocationAddend; |
| 136 | |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 137 | Triple::OSType OSType; |
| 138 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 139 | // This holds the symbol table index of the last local symbol. |
| 140 | unsigned LastLocalSymbolIndex; |
| 141 | // This holds the .strtab section index. |
| 142 | unsigned StringTableIndex; |
| 143 | |
| 144 | unsigned ShstrtabIndex; |
| 145 | |
| 146 | public: |
| 147 | ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit, |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 148 | bool _HasRelAddend, Triple::OSType _OSType) |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 149 | : Writer(_Writer), OS(Writer->getStream()), |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 150 | Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend), |
| 151 | OSType(_OSType) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void Write8(uint8_t Value) { Writer->Write8(Value); } |
| 155 | void Write16(uint16_t Value) { Writer->Write16(Value); } |
| 156 | void Write32(uint32_t Value) { Writer->Write32(Value); } |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 157 | //void Write64(uint64_t Value) { Writer->Write64(Value); } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 158 | void WriteZeros(unsigned N) { Writer->WriteZeros(N); } |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 159 | //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) { |
| 160 | // Writer->WriteBytes(Str, ZeroFillSize); |
| 161 | //} |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 162 | |
| 163 | void WriteWord(uint64_t W) { |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 164 | if (Is64Bit) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 165 | Writer->Write64(W); |
Chris Lattner | b188a37 | 2010-08-28 03:21:03 +0000 | [diff] [blame] | 166 | else |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 167 | Writer->Write32(W); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void String8(char *buf, uint8_t Value) { |
| 171 | buf[0] = Value; |
| 172 | } |
| 173 | |
| 174 | void StringLE16(char *buf, uint16_t Value) { |
| 175 | buf[0] = char(Value >> 0); |
| 176 | buf[1] = char(Value >> 8); |
| 177 | } |
| 178 | |
| 179 | void StringLE32(char *buf, uint32_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 180 | StringLE16(buf, uint16_t(Value >> 0)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 181 | StringLE16(buf + 2, uint16_t(Value >> 16)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void StringLE64(char *buf, uint64_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 185 | StringLE32(buf, uint32_t(Value >> 0)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 186 | StringLE32(buf + 4, uint32_t(Value >> 32)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void StringBE16(char *buf ,uint16_t Value) { |
| 190 | buf[0] = char(Value >> 8); |
| 191 | buf[1] = char(Value >> 0); |
| 192 | } |
| 193 | |
| 194 | void StringBE32(char *buf, uint32_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 195 | StringBE16(buf, uint16_t(Value >> 16)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 196 | StringBE16(buf + 2, uint16_t(Value >> 0)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void StringBE64(char *buf, uint64_t Value) { |
Benjamin Kramer | 36c6dc2 | 2010-08-23 21:23:52 +0000 | [diff] [blame] | 200 | StringBE32(buf, uint32_t(Value >> 32)); |
Benjamin Kramer | c522f6e | 2010-08-23 21:32:00 +0000 | [diff] [blame] | 201 | StringBE32(buf + 4, uint32_t(Value >> 0)); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void String16(char *buf, uint16_t Value) { |
| 205 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 206 | StringLE16(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 207 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 208 | StringBE16(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void String32(char *buf, uint32_t Value) { |
| 212 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 213 | StringLE32(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 214 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 215 | StringBE32(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void String64(char *buf, uint64_t Value) { |
| 219 | if (Writer->isLittleEndian()) |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 220 | StringLE64(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 221 | else |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 222 | StringBE64(buf, Value); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections); |
| 226 | |
| 227 | void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info, |
| 228 | uint64_t value, uint64_t size, |
| 229 | uint8_t other, uint16_t shndx); |
| 230 | |
| 231 | void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD, |
| 232 | const MCAsmLayout &Layout); |
| 233 | |
| 234 | void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm, |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 235 | const MCAsmLayout &Layout, |
| 236 | unsigned NumRegularSections); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 237 | |
| 238 | void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout, |
| 239 | const MCFragment *Fragment, const MCFixup &Fixup, |
| 240 | MCValue Target, uint64_t &FixedValue); |
| 241 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 242 | uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 243 | const MCSymbol *S); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 244 | |
| 245 | /// ComputeSymbolTable - Compute the symbol table data |
| 246 | /// |
| 247 | /// \param StringTable [out] - The string table data. |
| 248 | /// \param StringIndexMap [out] - Map from symbol names to offsets in the |
| 249 | /// string table. |
| 250 | void ComputeSymbolTable(MCAssembler &Asm); |
| 251 | |
| 252 | void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout, |
| 253 | const MCSectionData &SD); |
| 254 | |
| 255 | void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) { |
| 256 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 257 | ie = Asm.end(); it != ie; ++it) { |
| 258 | WriteRelocation(Asm, Layout, *it); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout); |
| 263 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 264 | void ExecutePostLayoutBinding(MCAssembler &Asm) { |
| 265 | // Compute symbol table information. |
| 266 | ComputeSymbolTable(Asm); |
| 267 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 268 | |
| 269 | void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags, |
| 270 | uint64_t Address, uint64_t Offset, |
| 271 | uint64_t Size, uint32_t Link, uint32_t Info, |
| 272 | uint64_t Alignment, uint64_t EntrySize); |
| 273 | |
| 274 | void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F, |
| 275 | const MCSectionData *SD); |
| 276 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 277 | bool IsFixupFullyResolved(const MCAssembler &Asm, |
| 278 | const MCValue Target, |
| 279 | bool IsPCRel, |
| 280 | const MCFragment *DF) const; |
| 281 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 282 | void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout); |
| 283 | }; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | // Emit the ELF header. |
| 288 | void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize, |
| 289 | unsigned NumberOfSections) { |
| 290 | // ELF Header |
| 291 | // ---------- |
| 292 | // |
| 293 | // Note |
| 294 | // ---- |
| 295 | // emitWord method behaves differently for ELF32 and ELF64, writing |
| 296 | // 4 bytes in the former and 8 in the latter. |
| 297 | |
| 298 | Write8(0x7f); // e_ident[EI_MAG0] |
| 299 | Write8('E'); // e_ident[EI_MAG1] |
| 300 | Write8('L'); // e_ident[EI_MAG2] |
| 301 | Write8('F'); // e_ident[EI_MAG3] |
| 302 | |
| 303 | Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS] |
| 304 | |
| 305 | // e_ident[EI_DATA] |
| 306 | Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB); |
| 307 | |
| 308 | Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION] |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 309 | // e_ident[EI_OSABI] |
| 310 | switch (OSType) { |
| 311 | case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break; |
| 312 | case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break; |
| 313 | default: Write8(ELF::ELFOSABI_NONE); break; |
| 314 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 315 | Write8(0); // e_ident[EI_ABIVERSION] |
| 316 | |
| 317 | WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD); |
| 318 | |
| 319 | Write16(ELF::ET_REL); // e_type |
| 320 | |
| 321 | // FIXME: Make this configurable |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 322 | Write16(Is64Bit ? ELF::EM_X86_64 : ELF::EM_386); // e_machine = target |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 323 | |
| 324 | Write32(ELF::EV_CURRENT); // e_version |
| 325 | WriteWord(0); // e_entry, no entry point in .o file |
| 326 | WriteWord(0); // e_phoff, no program header for .o |
Benjamin Kramer | eb97677 | 2010-08-17 17:02:29 +0000 | [diff] [blame] | 327 | WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) : |
| 328 | sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 329 | |
| 330 | // FIXME: Make this configurable. |
| 331 | Write32(0); // e_flags = whatever the target wants |
| 332 | |
| 333 | // e_ehsize = ELF header size |
| 334 | Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr)); |
| 335 | |
| 336 | Write16(0); // e_phentsize = prog header entry size |
| 337 | Write16(0); // e_phnum = # prog header entries = 0 |
| 338 | |
| 339 | // e_shentsize = Section header entry size |
| 340 | Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr)); |
| 341 | |
| 342 | // e_shnum = # of section header ents |
| 343 | Write16(NumberOfSections); |
| 344 | |
| 345 | // e_shstrndx = Section # of '.shstrtab' |
| 346 | Write16(ShstrtabIndex); |
| 347 | } |
| 348 | |
| 349 | void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name, |
| 350 | uint8_t info, uint64_t value, |
| 351 | uint64_t size, uint8_t other, |
| 352 | uint16_t shndx) { |
| 353 | if (Is64Bit) { |
| 354 | char buf[8]; |
| 355 | |
| 356 | String32(buf, name); |
| 357 | F->getContents() += StringRef(buf, 4); // st_name |
| 358 | |
| 359 | String8(buf, info); |
| 360 | F->getContents() += StringRef(buf, 1); // st_info |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 361 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 362 | String8(buf, other); |
| 363 | F->getContents() += StringRef(buf, 1); // st_other |
| 364 | |
| 365 | String16(buf, shndx); |
| 366 | F->getContents() += StringRef(buf, 2); // st_shndx |
| 367 | |
| 368 | String64(buf, value); |
| 369 | F->getContents() += StringRef(buf, 8); // st_value |
| 370 | |
| 371 | String64(buf, size); |
| 372 | F->getContents() += StringRef(buf, 8); // st_size |
| 373 | } else { |
| 374 | char buf[4]; |
| 375 | |
| 376 | String32(buf, name); |
| 377 | F->getContents() += StringRef(buf, 4); // st_name |
| 378 | |
| 379 | String32(buf, value); |
| 380 | F->getContents() += StringRef(buf, 4); // st_value |
| 381 | |
| 382 | String32(buf, size); |
| 383 | F->getContents() += StringRef(buf, 4); // st_size |
| 384 | |
| 385 | String8(buf, info); |
| 386 | F->getContents() += StringRef(buf, 1); // st_info |
| 387 | |
| 388 | String8(buf, other); |
| 389 | F->getContents() += StringRef(buf, 1); // st_other |
| 390 | |
| 391 | String16(buf, shndx); |
| 392 | F->getContents() += StringRef(buf, 2); // st_shndx |
| 393 | } |
| 394 | } |
| 395 | |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 396 | static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) { |
| 397 | if (Data.isCommon() && Data.isExternal()) |
| 398 | return Data.getCommonAlignment(); |
| 399 | |
| 400 | const MCSymbol &Symbol = Data.getSymbol(); |
| 401 | if (!Symbol.isInSection()) |
| 402 | return 0; |
| 403 | |
| 404 | if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak)) |
| 405 | if (MCFragment *FF = Data.getFragment()) |
| 406 | return Layout.getSymbolAddress(&Data) - |
| 407 | Layout.getSectionAddress(FF->getParent()); |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 412 | void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD, |
| 413 | const MCAsmLayout &Layout) { |
| 414 | MCSymbolData &Data = *MSD.SymbolData; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 415 | uint8_t Info = (Data.getFlags() & 0xff); |
| 416 | uint8_t Other = ((Data.getFlags() & 0xf00) >> ELF_STV_Shift); |
Rafael Espindola | 2c6ec31 | 2010-09-27 21:23:02 +0000 | [diff] [blame] | 417 | uint64_t Value = SymbolValue(Data, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 418 | uint64_t Size = 0; |
| 419 | const MCExpr *ESize; |
| 420 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 421 | assert(!(Data.isCommon() && !Data.isExternal())); |
| 422 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 423 | ESize = Data.getSize(); |
| 424 | if (Data.getSize()) { |
| 425 | MCValue Res; |
| 426 | if (ESize->getKind() == MCExpr::Binary) { |
| 427 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize); |
| 428 | |
| 429 | if (BE->EvaluateAsRelocatable(Res, &Layout)) { |
| 430 | MCSymbolData &A = |
| 431 | Layout.getAssembler().getSymbolData(Res.getSymA()->getSymbol()); |
| 432 | MCSymbolData &B = |
| 433 | Layout.getAssembler().getSymbolData(Res.getSymB()->getSymbol()); |
| 434 | |
| 435 | Size = Layout.getSymbolAddress(&A) - Layout.getSymbolAddress(&B); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 436 | } |
| 437 | } else if (ESize->getKind() == MCExpr::Constant) { |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 438 | Size = static_cast<const MCConstantExpr *>(ESize)->getValue(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 439 | } else { |
| 440 | assert(0 && "Unsupported size expression"); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Write out the symbol table entry |
| 445 | WriteSymbolEntry(F, MSD.StringIndex, Info, Value, |
| 446 | Size, Other, MSD.SectionIndex); |
| 447 | } |
| 448 | |
| 449 | void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F, |
| 450 | const MCAssembler &Asm, |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 451 | const MCAsmLayout &Layout, |
| 452 | unsigned NumRegularSections) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 453 | // The string table must be emitted first because we need the index |
| 454 | // into the string table for all the symbol names. |
| 455 | assert(StringTable.size() && "Missing string table"); |
| 456 | |
| 457 | // FIXME: Make sure the start of the symbol table is aligned. |
| 458 | |
| 459 | // The first entry is the undefined symbol entry. |
| 460 | unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 461 | F->getContents().append(EntrySize, '\x00'); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 462 | |
| 463 | // Write the symbol table entries. |
| 464 | LastLocalSymbolIndex = LocalSymbolData.size() + 1; |
| 465 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) { |
| 466 | ELFSymbolData &MSD = LocalSymbolData[i]; |
| 467 | WriteSymbol(F, MSD, Layout); |
| 468 | } |
| 469 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 470 | // Write out a symbol table entry for each regular section. |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 471 | unsigned Index = 1; |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 472 | for (MCAssembler::const_iterator it = Asm.begin(); |
| 473 | Index <= NumRegularSections; ++it, ++Index) { |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 474 | const MCSectionELF &Section = |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 475 | static_cast<const MCSectionELF&>(it->getSection()); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 476 | // Leave out relocations so we don't have indexes within |
| 477 | // the relocations messed up |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 478 | if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL) |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 479 | continue; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 480 | WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index); |
Eli Friedman | a44fa24 | 2010-08-16 21:17:09 +0000 | [diff] [blame] | 481 | LastLocalSymbolIndex++; |
| 482 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 483 | |
| 484 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) { |
| 485 | ELFSymbolData &MSD = ExternalSymbolData[i]; |
| 486 | MCSymbolData &Data = *MSD.SymbolData; |
| 487 | assert((Data.getFlags() & ELF_STB_Global) && |
| 488 | "External symbol requires STB_GLOBAL flag"); |
| 489 | WriteSymbol(F, MSD, Layout); |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 490 | if (GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 491 | LastLocalSymbolIndex++; |
| 492 | } |
| 493 | |
| 494 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) { |
| 495 | ELFSymbolData &MSD = UndefinedSymbolData[i]; |
| 496 | MCSymbolData &Data = *MSD.SymbolData; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 497 | WriteSymbol(F, MSD, Layout); |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 498 | if (GetBinding(Data) == ELF::STB_LOCAL) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 499 | LastLocalSymbolIndex++; |
| 500 | } |
| 501 | } |
| 502 | |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 503 | static const MCSymbolData *getAtom(const MCSymbolData &SD) { |
| 504 | if (!SD.getFragment()) |
| 505 | return 0; |
| 506 | |
| 507 | return SD.getFragment()->getAtom(); |
| 508 | } |
| 509 | |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 510 | // FIXME: this is currently X86/X86_64 only |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 511 | void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm, |
| 512 | const MCAsmLayout &Layout, |
| 513 | const MCFragment *Fragment, |
| 514 | const MCFixup &Fixup, |
| 515 | MCValue Target, |
| 516 | uint64_t &FixedValue) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 517 | int64_t Addend = 0; |
| 518 | unsigned Index = 0; |
Benjamin Kramer | 95c602a | 2010-08-27 10:38:39 +0000 | [diff] [blame] | 519 | int64_t Value = Target.getConstant(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 520 | |
Rafael Espindola | 0007489 | 2010-09-17 22:34:41 +0000 | [diff] [blame] | 521 | bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind()); |
Benjamin Kramer | 81cfb85 | 2010-08-17 19:45:05 +0000 | [diff] [blame] | 522 | if (!Target.isAbsolute()) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 523 | const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); |
| 524 | MCSymbolData &SD = Asm.getSymbolData(*Symbol); |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 525 | const MCSymbolData *Base = getAtom(SD); |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 526 | MCFragment *F = SD.getFragment(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 527 | |
Rafael Espindola | 0007489 | 2010-09-17 22:34:41 +0000 | [diff] [blame] | 528 | // Avoid relocations for cases like jumps and calls in the same file. |
| 529 | if (Symbol->isDefined() && !SD.isExternal() && |
| 530 | IsPCRel && |
| 531 | &Fragment->getParent()->getSection() == &Symbol->getSection()) { |
| 532 | uint64_t FixupAddr = Layout.getFragmentAddress(Fragment) + Fixup.getOffset(); |
| 533 | FixedValue = Layout.getSymbolAddress(&SD) + Target.getConstant() - FixupAddr; |
| 534 | return; |
| 535 | } |
| 536 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 537 | if (Base) { |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 538 | if (Base != &SD) { |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 539 | Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1; |
Rafael Espindola | a648918 | 2010-09-24 21:19:03 +0000 | [diff] [blame] | 540 | |
| 541 | MCSectionData *FSD = F->getParent(); |
| 542 | // Offset of the symbol in the section |
| 543 | Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD); |
Benjamin Kramer | bcf2db6 | 2010-08-23 19:05:46 +0000 | [diff] [blame] | 544 | } else |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 545 | Index = getSymbolIndexInSymbolTable(Asm, Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 546 | Addend = Value; |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 547 | // Compensate for the addend on i386. |
Benjamin Kramer | 4ba1b30 | 2010-08-26 18:12:04 +0000 | [diff] [blame] | 548 | if (Is64Bit) |
| 549 | Value = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 550 | } else { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 551 | if (F) { |
| 552 | // Index of the section in .symtab against this symbol |
| 553 | // is being relocated + 2 (empty section + abs. symbols). |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 554 | Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 555 | |
| 556 | MCSectionData *FSD = F->getParent(); |
| 557 | // Offset of the symbol in the section |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 558 | Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 559 | } else { |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 560 | Index = getSymbolIndexInSymbolTable(Asm, Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 561 | } |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 562 | Addend = Value; |
| 563 | // Compensate for the addend on i386. |
| 564 | if (Is64Bit) |
| 565 | Value = 0; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Benjamin Kramer | 95c602a | 2010-08-27 10:38:39 +0000 | [diff] [blame] | 569 | FixedValue = Value; |
| 570 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 571 | // determine the type of the relocation |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 572 | unsigned Type; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 573 | if (Is64Bit) { |
| 574 | if (IsPCRel) { |
| 575 | Type = ELF::R_X86_64_PC32; |
| 576 | } else { |
| 577 | switch ((unsigned)Fixup.getKind()) { |
| 578 | default: llvm_unreachable("invalid fixup kind!"); |
| 579 | case FK_Data_8: Type = ELF::R_X86_64_64; break; |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 580 | case X86::reloc_signed_4byte: |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 581 | case X86::reloc_pcrel_4byte: |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 582 | assert(isInt<32>(Target.getConstant())); |
| 583 | Type = ELF::R_X86_64_32S; |
| 584 | break; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 585 | case FK_Data_4: |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 586 | Type = ELF::R_X86_64_32; |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 587 | break; |
| 588 | case FK_Data_2: Type = ELF::R_X86_64_16; break; |
| 589 | case X86::reloc_pcrel_1byte: |
| 590 | case FK_Data_1: Type = ELF::R_X86_64_8; break; |
| 591 | } |
| 592 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 593 | } else { |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 594 | if (IsPCRel) { |
| 595 | Type = ELF::R_386_PC32; |
| 596 | } else { |
| 597 | switch ((unsigned)Fixup.getKind()) { |
| 598 | default: llvm_unreachable("invalid fixup kind!"); |
Rafael Espindola | a8c02c3 | 2010-09-30 03:11:42 +0000 | [diff] [blame] | 599 | |
| 600 | // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode |
| 601 | // instead? |
| 602 | case X86::reloc_signed_4byte: |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 603 | case X86::reloc_pcrel_4byte: |
| 604 | case FK_Data_4: Type = ELF::R_386_32; break; |
| 605 | case FK_Data_2: Type = ELF::R_386_16; break; |
| 606 | case X86::reloc_pcrel_1byte: |
| 607 | case FK_Data_1: Type = ELF::R_386_8; break; |
| 608 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 612 | ELFRelocationEntry ERE; |
| 613 | |
| 614 | if (Is64Bit) { |
| 615 | struct ELF::Elf64_Rela ERE64; |
| 616 | ERE64.setSymbolAndType(Index, Type); |
| 617 | ERE.r_info = ERE64.r_info; |
| 618 | } else { |
| 619 | struct ELF::Elf32_Rela ERE32; |
| 620 | ERE32.setSymbolAndType(Index, Type); |
| 621 | ERE.r_info = ERE32.r_info; |
| 622 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 623 | |
Benjamin Kramer | 63d37b9 | 2010-08-26 17:23:02 +0000 | [diff] [blame] | 624 | ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); |
Benjamin Kramer | e5b5734 | 2010-08-17 18:20:28 +0000 | [diff] [blame] | 625 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 626 | if (HasRelocationAddend) |
| 627 | ERE.r_addend = Addend; |
Benjamin Kramer | 172d7d6 | 2010-08-17 00:33:24 +0000 | [diff] [blame] | 628 | else |
| 629 | ERE.r_addend = 0; // Silence compiler warning. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 630 | |
| 631 | Relocations[Fragment->getParent()].push_back(ERE); |
| 632 | } |
| 633 | |
Benjamin Kramer | 0b6cbfe | 2010-08-23 21:19:37 +0000 | [diff] [blame] | 634 | uint64_t |
| 635 | ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm, |
| 636 | const MCSymbol *S) { |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 637 | MCSymbolData &SD = Asm.getSymbolData(*S); |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 638 | |
Benjamin Kramer | 7b83c26 | 2010-08-25 20:09:43 +0000 | [diff] [blame] | 639 | // Local symbol. |
| 640 | if (!SD.isExternal() && !S->isUndefined()) |
| 641 | return SD.getIndex() + /* empty symbol */ 1; |
| 642 | |
| 643 | // External or undefined symbol. |
| 644 | return SD.getIndex() + Asm.size() + /* empty symbol */ 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) { |
| 648 | // Build section lookup table. |
| 649 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 650 | unsigned Index = 1; |
| 651 | for (MCAssembler::iterator it = Asm.begin(), |
| 652 | ie = Asm.end(); it != ie; ++it, ++Index) |
| 653 | SectionIndexMap[&it->getSection()] = Index; |
| 654 | |
| 655 | // Index 0 is always the empty string. |
| 656 | StringMap<uint64_t> StringIndexMap; |
| 657 | StringTable += '\x00'; |
| 658 | |
| 659 | // Add the data for local symbols. |
| 660 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 661 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 662 | const MCSymbol &Symbol = it->getSymbol(); |
| 663 | |
| 664 | // Ignore non-linker visible symbols. |
| 665 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
| 666 | continue; |
| 667 | |
| 668 | if (it->isExternal() || Symbol.isUndefined()) |
| 669 | continue; |
| 670 | |
| 671 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 672 | if (!Entry) { |
| 673 | Entry = StringTable.size(); |
| 674 | StringTable += Symbol.getName(); |
| 675 | StringTable += '\x00'; |
| 676 | } |
| 677 | |
| 678 | ELFSymbolData MSD; |
| 679 | MSD.SymbolData = it; |
| 680 | MSD.StringIndex = Entry; |
| 681 | |
| 682 | if (Symbol.isAbsolute()) { |
| 683 | MSD.SectionIndex = ELF::SHN_ABS; |
| 684 | LocalSymbolData.push_back(MSD); |
| 685 | } else { |
| 686 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 687 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 688 | LocalSymbolData.push_back(MSD); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // Now add non-local symbols. |
| 693 | for (MCAssembler::symbol_iterator it = Asm.symbol_begin(), |
| 694 | ie = Asm.symbol_end(); it != ie; ++it) { |
| 695 | const MCSymbol &Symbol = it->getSymbol(); |
| 696 | |
| 697 | // Ignore non-linker visible symbols. |
Rafael Espindola | 53725bc | 2010-09-28 16:19:11 +0000 | [diff] [blame] | 698 | if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined()) |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 699 | continue; |
| 700 | |
| 701 | if (!it->isExternal() && !Symbol.isUndefined()) |
| 702 | continue; |
| 703 | |
Rafael Espindola | 53725bc | 2010-09-28 16:19:11 +0000 | [diff] [blame] | 704 | if (Symbol.isVariable()) |
| 705 | continue; |
| 706 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 707 | uint64_t &Entry = StringIndexMap[Symbol.getName()]; |
| 708 | if (!Entry) { |
| 709 | Entry = StringTable.size(); |
| 710 | StringTable += Symbol.getName(); |
| 711 | StringTable += '\x00'; |
| 712 | } |
| 713 | |
| 714 | ELFSymbolData MSD; |
| 715 | MSD.SymbolData = it; |
| 716 | MSD.StringIndex = Entry; |
| 717 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 718 | if (it->isCommon()) { |
| 719 | MSD.SectionIndex = ELF::SHN_COMMON; |
| 720 | ExternalSymbolData.push_back(MSD); |
| 721 | } else if (Symbol.isUndefined()) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 722 | MSD.SectionIndex = ELF::SHN_UNDEF; |
Rafael Espindola | e15eb4e | 2010-09-23 19:55:14 +0000 | [diff] [blame] | 723 | // FIXME: Undefined symbols are global, but this is the first place we |
| 724 | // are able to set it. |
| 725 | if (GetBinding(*it) == ELF::STB_LOCAL) |
| 726 | SetBinding(*it, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 727 | UndefinedSymbolData.push_back(MSD); |
| 728 | } else if (Symbol.isAbsolute()) { |
| 729 | MSD.SectionIndex = ELF::SHN_ABS; |
| 730 | ExternalSymbolData.push_back(MSD); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 731 | } else { |
| 732 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
| 733 | assert(MSD.SectionIndex && "Invalid section index!"); |
| 734 | ExternalSymbolData.push_back(MSD); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // Symbols are required to be in lexicographic order. |
| 739 | array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end()); |
| 740 | array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end()); |
| 741 | array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end()); |
| 742 | |
| 743 | // Set the symbol indices. Local symbols must come before all other |
| 744 | // symbols with non-local bindings. |
| 745 | Index = 0; |
| 746 | for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) |
| 747 | LocalSymbolData[i].SymbolData->setIndex(Index++); |
| 748 | for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) |
| 749 | ExternalSymbolData[i].SymbolData->setIndex(Index++); |
| 750 | for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) |
| 751 | UndefinedSymbolData[i].SymbolData->setIndex(Index++); |
| 752 | } |
| 753 | |
| 754 | void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout, |
| 755 | const MCSectionData &SD) { |
| 756 | if (!Relocations[&SD].empty()) { |
| 757 | MCContext &Ctx = Asm.getContext(); |
| 758 | const MCSection *RelaSection; |
| 759 | const MCSectionELF &Section = |
| 760 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 761 | |
| 762 | const StringRef SectionName = Section.getSectionName(); |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 763 | std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel"; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 764 | RelaSectionName += SectionName; |
Benjamin Kramer | 299fbe3 | 2010-08-17 17:56:13 +0000 | [diff] [blame] | 765 | |
| 766 | unsigned EntrySize; |
| 767 | if (HasRelocationAddend) |
| 768 | EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela); |
| 769 | else |
| 770 | EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 771 | |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 772 | RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ? |
| 773 | ELF::SHT_RELA : ELF::SHT_REL, 0, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 774 | SectionKind::getReadOnly(), |
| 775 | false, EntrySize); |
| 776 | |
| 777 | MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection); |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 778 | RelaSD.setAlignment(Is64Bit ? 8 : 4); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 779 | |
| 780 | MCDataFragment *F = new MCDataFragment(&RelaSD); |
| 781 | |
| 782 | WriteRelocationsFragment(Asm, F, &SD); |
| 783 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 784 | Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type, |
| 789 | uint64_t Flags, uint64_t Address, |
| 790 | uint64_t Offset, uint64_t Size, |
| 791 | uint32_t Link, uint32_t Info, |
| 792 | uint64_t Alignment, |
| 793 | uint64_t EntrySize) { |
| 794 | Write32(Name); // sh_name: index into string table |
| 795 | Write32(Type); // sh_type |
| 796 | WriteWord(Flags); // sh_flags |
| 797 | WriteWord(Address); // sh_addr |
| 798 | WriteWord(Offset); // sh_offset |
| 799 | WriteWord(Size); // sh_size |
| 800 | Write32(Link); // sh_link |
| 801 | Write32(Info); // sh_info |
| 802 | WriteWord(Alignment); // sh_addralign |
| 803 | WriteWord(EntrySize); // sh_entsize |
| 804 | } |
| 805 | |
| 806 | void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm, |
| 807 | MCDataFragment *F, |
| 808 | const MCSectionData *SD) { |
| 809 | std::vector<ELFRelocationEntry> &Relocs = Relocations[SD]; |
| 810 | // sort by the r_offset just like gnu as does |
| 811 | array_pod_sort(Relocs.begin(), Relocs.end()); |
| 812 | |
| 813 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 814 | ELFRelocationEntry entry = Relocs[e - i - 1]; |
| 815 | |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 816 | if (Is64Bit) { |
| 817 | char buf[8]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 818 | |
Benjamin Kramer | 5e492e8 | 2010-09-09 18:01:29 +0000 | [diff] [blame] | 819 | String64(buf, entry.r_offset); |
| 820 | F->getContents() += StringRef(buf, 8); |
| 821 | |
| 822 | String64(buf, entry.r_info); |
| 823 | F->getContents() += StringRef(buf, 8); |
| 824 | |
| 825 | if (HasRelocationAddend) { |
| 826 | String64(buf, entry.r_addend); |
| 827 | F->getContents() += StringRef(buf, 8); |
| 828 | } |
| 829 | } else { |
| 830 | char buf[4]; |
| 831 | |
| 832 | String32(buf, entry.r_offset); |
| 833 | F->getContents() += StringRef(buf, 4); |
| 834 | |
| 835 | String32(buf, entry.r_info); |
| 836 | F->getContents() += StringRef(buf, 4); |
| 837 | |
| 838 | if (HasRelocationAddend) { |
| 839 | String32(buf, entry.r_addend); |
| 840 | F->getContents() += StringRef(buf, 4); |
| 841 | } |
| 842 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
| 846 | void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm, |
| 847 | MCAsmLayout &Layout) { |
| 848 | MCContext &Ctx = Asm.getContext(); |
| 849 | MCDataFragment *F; |
| 850 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 851 | const MCSection *SymtabSection; |
| 852 | unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32; |
| 853 | |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 854 | unsigned NumRegularSections = Asm.size(); |
| 855 | |
Rafael Espindola | 38738bf | 2010-09-22 19:04:41 +0000 | [diff] [blame] | 856 | // We construct .shstrtab, .symtab and .strtab in this order to match gnu as. |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 857 | const MCSection *ShstrtabSection; |
| 858 | ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0, |
| 859 | SectionKind::getReadOnly(), false); |
| 860 | MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection); |
| 861 | ShstrtabSD.setAlignment(1); |
| 862 | ShstrtabIndex = Asm.size(); |
| 863 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 864 | SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 865 | SectionKind::getReadOnly(), |
| 866 | false, EntrySize); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 867 | MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 868 | SymtabSD.setAlignment(Is64Bit ? 8 : 4); |
| 869 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 870 | const MCSection *StrtabSection; |
| 871 | StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0, |
| 872 | SectionKind::getReadOnly(), false); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 873 | MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection); |
| 874 | StrtabSD.setAlignment(1); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 875 | StringTableIndex = Asm.size(); |
| 876 | |
Rafael Espindola | c3c413f | 2010-09-27 22:04:54 +0000 | [diff] [blame] | 877 | WriteRelocations(Asm, Layout); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 878 | |
| 879 | // Symbol table |
| 880 | F = new MCDataFragment(&SymtabSD); |
| 881 | WriteSymbolTable(F, Asm, Layout, NumRegularSections); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 882 | Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout); |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 883 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 884 | F = new MCDataFragment(&StrtabSD); |
| 885 | F->getContents().append(StringTable.begin(), StringTable.end()); |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 886 | Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 887 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 888 | F = new MCDataFragment(&ShstrtabSD); |
| 889 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 890 | // Section header string table. |
| 891 | // |
| 892 | // The first entry of a string table holds a null character so skip |
| 893 | // section 0. |
| 894 | uint64_t Index = 1; |
| 895 | F->getContents() += '\x00'; |
| 896 | |
| 897 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 898 | ie = Asm.end(); it != ie; ++it) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 899 | const MCSectionELF &Section = |
Benjamin Kramer | 368ae7e | 2010-08-17 00:00:46 +0000 | [diff] [blame] | 900 | static_cast<const MCSectionELF&>(it->getSection()); |
Rafael Espindola | 51efe7a | 2010-09-23 14:14:56 +0000 | [diff] [blame] | 901 | // FIXME: We could merge suffixes like in .text and .rela.text. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 902 | |
| 903 | // Remember the index into the string table so we can write it |
| 904 | // into the sh_name field of the section header table. |
| 905 | SectionStringTableIndex[&it->getSection()] = Index; |
| 906 | |
| 907 | Index += Section.getSectionName().size() + 1; |
| 908 | F->getContents() += Section.getSectionName(); |
| 909 | F->getContents() += '\x00'; |
| 910 | } |
| 911 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 912 | Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout); |
| 913 | } |
| 914 | |
| 915 | bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm, |
| 916 | const MCValue Target, |
| 917 | bool IsPCRel, |
| 918 | const MCFragment *DF) const { |
| 919 | // If this is a PCrel relocation, find the section this fixup value is |
| 920 | // relative to. |
| 921 | const MCSection *BaseSection = 0; |
| 922 | if (IsPCRel) { |
| 923 | BaseSection = &DF->getParent()->getSection(); |
| 924 | assert(BaseSection); |
| 925 | } |
| 926 | |
| 927 | const MCSection *SectionA = 0; |
| 928 | const MCSymbol *SymbolA = 0; |
| 929 | if (const MCSymbolRefExpr *A = Target.getSymA()) { |
| 930 | SymbolA = &A->getSymbol(); |
| 931 | SectionA = &SymbolA->getSection(); |
| 932 | } |
| 933 | |
| 934 | const MCSection *SectionB = 0; |
| 935 | if (const MCSymbolRefExpr *B = Target.getSymB()) { |
| 936 | SectionB = &B->getSymbol().getSection(); |
| 937 | } |
| 938 | |
| 939 | if (!BaseSection) |
| 940 | return SectionA == SectionB; |
| 941 | |
| 942 | const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA); |
| 943 | if (DataA.isExternal()) |
| 944 | return false; |
| 945 | |
| 946 | return !SectionB && BaseSection == SectionA; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm, |
| 950 | const MCAsmLayout &Layout) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 951 | CreateMetadataSections(const_cast<MCAssembler&>(Asm), |
| 952 | const_cast<MCAsmLayout&>(Layout)); |
| 953 | |
| 954 | // Add 1 for the null section. |
| 955 | unsigned NumSections = Asm.size() + 1; |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 956 | uint64_t NaturalAlignment = Is64Bit ? 8 : 4; |
| 957 | uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr); |
| 958 | uint64_t FileOff = HeaderSize; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 959 | |
| 960 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 961 | ie = Asm.end(); it != ie; ++it) { |
| 962 | const MCSectionData &SD = *it; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 963 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 964 | FileOff = RoundUpToAlignment(FileOff, SD.getAlignment()); |
| 965 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 966 | // Get the size of the section in the output file (including padding). |
| 967 | uint64_t Size = Layout.getSectionFileSize(&SD); |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 968 | |
| 969 | FileOff += Size; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 972 | FileOff = RoundUpToAlignment(FileOff, NaturalAlignment); |
| 973 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 974 | // Write out the ELF header ... |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 975 | WriteHeader(FileOff - HeaderSize, NumSections); |
| 976 | |
| 977 | FileOff = HeaderSize; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 978 | |
| 979 | // ... then all of the sections ... |
| 980 | DenseMap<const MCSection*, uint64_t> SectionOffsetMap; |
| 981 | |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 982 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
| 983 | |
| 984 | unsigned Index = 1; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 985 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 986 | ie = Asm.end(); it != ie; ++it) { |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 987 | const MCSectionData &SD = *it; |
| 988 | |
| 989 | uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment()); |
| 990 | WriteZeros(Padding); |
| 991 | FileOff += Padding; |
| 992 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 993 | // Remember the offset into the file for this section. |
| 994 | SectionOffsetMap[&it->getSection()] = FileOff; |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 995 | SectionIndexMap[&it->getSection()] = Index++; |
| 996 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 997 | FileOff += Layout.getSectionFileSize(&SD); |
| 998 | |
| 999 | Asm.WriteSectionData(it, Layout, Writer); |
| 1000 | } |
| 1001 | |
Benjamin Kramer | a9eadca | 2010-09-06 16:11:52 +0000 | [diff] [blame] | 1002 | uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment); |
| 1003 | WriteZeros(Padding); |
| 1004 | FileOff += Padding; |
| 1005 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1006 | // ... and then the section header table. |
| 1007 | // Should we align the section header table? |
| 1008 | // |
| 1009 | // Null section first. |
| 1010 | WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 1011 | |
| 1012 | for (MCAssembler::const_iterator it = Asm.begin(), |
| 1013 | ie = Asm.end(); it != ie; ++it) { |
| 1014 | const MCSectionData &SD = *it; |
| 1015 | const MCSectionELF &Section = |
| 1016 | static_cast<const MCSectionELF&>(SD.getSection()); |
| 1017 | |
| 1018 | uint64_t sh_link = 0; |
| 1019 | uint64_t sh_info = 0; |
| 1020 | |
| 1021 | switch(Section.getType()) { |
| 1022 | case ELF::SHT_DYNAMIC: |
| 1023 | sh_link = SectionStringTableIndex[&it->getSection()]; |
| 1024 | sh_info = 0; |
| 1025 | break; |
| 1026 | |
| 1027 | case ELF::SHT_REL: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1028 | case ELF::SHT_RELA: { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1029 | const MCSection *SymtabSection; |
| 1030 | const MCSection *InfoSection; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1031 | |
| 1032 | SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0, |
| 1033 | SectionKind::getReadOnly(), |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1034 | false); |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1035 | sh_link = SectionIndexMap[SymtabSection]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1036 | |
Benjamin Kramer | 377a572 | 2010-08-17 17:30:07 +0000 | [diff] [blame] | 1037 | // Remove ".rel" and ".rela" prefixes. |
| 1038 | unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5; |
| 1039 | StringRef SectionName = Section.getSectionName().substr(SecNameLen); |
| 1040 | |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1041 | InfoSection = Asm.getContext().getELFSection(SectionName, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1042 | ELF::SHT_PROGBITS, 0, |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1043 | SectionKind::getReadOnly(), |
| 1044 | false); |
Benjamin Kramer | 44cbde8 | 2010-08-19 13:44:49 +0000 | [diff] [blame] | 1045 | sh_info = SectionIndexMap[InfoSection]; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1046 | break; |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 1047 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1048 | |
| 1049 | case ELF::SHT_SYMTAB: |
| 1050 | case ELF::SHT_DYNSYM: |
| 1051 | sh_link = StringTableIndex; |
| 1052 | sh_info = LastLocalSymbolIndex; |
| 1053 | break; |
| 1054 | |
| 1055 | case ELF::SHT_PROGBITS: |
| 1056 | case ELF::SHT_STRTAB: |
| 1057 | case ELF::SHT_NOBITS: |
Benjamin Kramer | 19dc7fa | 2010-08-31 17:03:33 +0000 | [diff] [blame] | 1058 | case ELF::SHT_NULL: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1059 | // Nothing to do. |
| 1060 | break; |
| 1061 | |
| 1062 | case ELF::SHT_HASH: |
| 1063 | case ELF::SHT_GROUP: |
| 1064 | case ELF::SHT_SYMTAB_SHNDX: |
| 1065 | default: |
| 1066 | assert(0 && "FIXME: sh_type value not supported!"); |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()], |
| 1071 | Section.getType(), Section.getFlags(), |
Rafael Espindola | 71859c6 | 2010-09-16 19:46:31 +0000 | [diff] [blame] | 1072 | 0, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1073 | SectionOffsetMap.lookup(&SD.getSection()), |
| 1074 | Layout.getSectionSize(&SD), sh_link, |
| 1075 | sh_info, SD.getAlignment(), |
| 1076 | Section.getEntrySize()); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | ELFObjectWriter::ELFObjectWriter(raw_ostream &OS, |
| 1081 | bool Is64Bit, |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 1082 | Triple::OSType OSType, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1083 | bool IsLittleEndian, |
| 1084 | bool HasRelocationAddend) |
| 1085 | : MCObjectWriter(OS, IsLittleEndian) |
| 1086 | { |
Roman Divacky | 5baf79e | 2010-09-09 17:57:50 +0000 | [diff] [blame] | 1087 | Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend, OSType); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | ELFObjectWriter::~ELFObjectWriter() { |
| 1091 | delete (ELFObjectWriterImpl*) Impl; |
| 1092 | } |
| 1093 | |
| 1094 | void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) { |
| 1095 | ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm); |
| 1096 | } |
| 1097 | |
| 1098 | void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm, |
| 1099 | const MCAsmLayout &Layout, |
| 1100 | const MCFragment *Fragment, |
| 1101 | const MCFixup &Fixup, MCValue Target, |
| 1102 | uint64_t &FixedValue) { |
| 1103 | ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup, |
| 1104 | Target, FixedValue); |
| 1105 | } |
| 1106 | |
Rafael Espindola | 7070387 | 2010-09-30 02:22:20 +0000 | [diff] [blame] | 1107 | bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm, |
| 1108 | const MCValue Target, |
| 1109 | bool IsPCRel, |
| 1110 | const MCFragment *DF) const { |
| 1111 | return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target, |
| 1112 | IsPCRel, DF); |
| 1113 | } |
| 1114 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1115 | void ELFObjectWriter::WriteObject(const MCAssembler &Asm, |
| 1116 | const MCAsmLayout &Layout) { |
| 1117 | ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout); |
| 1118 | } |