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