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