|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import LinuxSystemHeaders |
| 15 | + |
| 16 | +// TODO: replace this implementation with general purpose ELF parsing support |
| 17 | +// currently private to swift/stdlib/public/Backtrace. |
| 18 | +class ElfFile { |
| 19 | + public enum ELFError: Error { |
| 20 | + case notELF64(_ filePath: String, _ description: String = "") |
| 21 | + case malformedFile(_ filePath: String, _ description: String = "") |
| 22 | + } |
| 23 | + |
| 24 | + public typealias SymbolMap = [String: (start: UInt64, end: UInt64)] |
| 25 | + |
| 26 | + let filePath: String |
| 27 | + let fileData: Data |
| 28 | + let ehdr: Elf64_Ehdr |
| 29 | + |
| 30 | + public init(filePath: String) throws { |
| 31 | + self.filePath = filePath |
| 32 | + |
| 33 | + self.fileData = try Data(contentsOf: URL(fileURLWithPath: filePath), options: .alwaysMapped) |
| 34 | + |
| 35 | + let ident = fileData.prefix(upTo: Int(EI_NIDENT)) |
| 36 | + |
| 37 | + guard String(bytes: ident.prefix(Int(SELFMAG)), encoding: .utf8) == ELFMAG else { |
| 38 | + throw ELFError.notELF64(filePath, "\(ident.prefix(Int(SELFMAG))) != ELFMAG") |
| 39 | + } |
| 40 | + |
| 41 | + guard ident[Int(EI_CLASS)] == ELFCLASS64 else { |
| 42 | + throw ELFError.notELF64(filePath, "\(ident[Int(EI_CLASS)]) != ELFCLASS64") |
| 43 | + } |
| 44 | + |
| 45 | + let ehdrSize = MemoryLayout<Elf64_Ehdr>.size |
| 46 | + self.ehdr = fileData[0..<ehdrSize].withUnsafeBytes { $0.load(as: Elf64_Ehdr.self) } |
| 47 | + } |
| 48 | + |
| 49 | + // returns a map of symbol names to their offset range in file (+ baseAddress) |
| 50 | + public func loadSymbols(baseAddress: UInt64 = 0) throws -> SymbolMap { |
| 51 | + guard let sectionCount = UInt(exactly: self.ehdr.e_shnum) else { |
| 52 | + throw ELFError.malformedFile( |
| 53 | + self.filePath, "invalid Elf64_Ehdr.e_shnum: \(self.ehdr.e_shnum)") |
| 54 | + } |
| 55 | + |
| 56 | + var symbols: SymbolMap = [:] |
| 57 | + for sectionIndex in 0..<sectionCount { |
| 58 | + let shdr: Elf64_Shdr = try self.loadShdr(index: sectionIndex) |
| 59 | + guard shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM else { continue } |
| 60 | + |
| 61 | + let symTableData: Data = try self.loadSection(shdr) |
| 62 | + let symTable: [Elf64_Sym] = symTableData.withUnsafeBytes { |
| 63 | + Array($0.bindMemory(to: Elf64_Sym.self)) |
| 64 | + } |
| 65 | + |
| 66 | + guard shdr.sh_entsize == MemoryLayout<Elf64_Sym>.size else { |
| 67 | + throw ELFError.malformedFile(self.filePath, "invalid Elf64_Shdr.sh_entsize") |
| 68 | + } |
| 69 | + |
| 70 | + // the link field in the section header for a symbol table section refers |
| 71 | + // to the index of the string table section containing the symbol names |
| 72 | + guard let linkIndex = UInt(exactly: shdr.sh_link) else { |
| 73 | + throw ELFError.malformedFile(self.filePath, "invalid Elf64_Shdr.sh_link: \(shdr.sh_link)") |
| 74 | + } |
| 75 | + |
| 76 | + let shdrLink: Elf64_Shdr = try self.loadShdr(index: UInt(linkIndex)) |
| 77 | + guard shdrLink.sh_type == SHT_STRTAB else { |
| 78 | + throw ELFError.malformedFile(self.filePath, "linked section not SHT_STRTAB") |
| 79 | + } |
| 80 | + |
| 81 | + // load the entire contents of the string table into memory |
| 82 | + let strTableData: Data = try self.loadSection(shdrLink) |
| 83 | + let strTable: [UInt8] = strTableData.withUnsafeBytes { |
| 84 | + Array($0.bindMemory(to: UInt8.self)) |
| 85 | + } |
| 86 | + |
| 87 | + let symCount = Int(shdr.sh_size / shdr.sh_entsize) |
| 88 | + for symIndex in 0..<symCount { |
| 89 | + let sym = symTable[symIndex] |
| 90 | + guard sym.st_shndx != SHN_UNDEF, sym.st_value != 0, sym.st_size != 0 else { continue } |
| 91 | + |
| 92 | + // sym.st_name is a byte offset into the string table |
| 93 | + guard let strStart = Int(exactly: sym.st_name), strStart < strTable.count else { |
| 94 | + throw ELFError.malformedFile(self.filePath, "invalid string table offset: \(sym.st_name)") |
| 95 | + } |
| 96 | + |
| 97 | + guard let strEnd = strTable[strStart...].firstIndex(of: 0), |
| 98 | + let symName = String(bytes: strTable[strStart..<strEnd], encoding: .utf8) |
| 99 | + else { |
| 100 | + throw ELFError.malformedFile(self.filePath, "invalid string @ offset \(strStart)") |
| 101 | + } |
| 102 | + |
| 103 | + // rebase the symbol value on the base address provided by the caller |
| 104 | + let symStart = sym.st_value + baseAddress |
| 105 | + symbols[symName] = (start: symStart, end: symStart + sym.st_size) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return symbols |
| 110 | + } |
| 111 | + |
| 112 | + // returns the Elf64_Shdr at the specified index |
| 113 | + internal func loadShdr(index: UInt) throws -> Elf64_Shdr { |
| 114 | + guard index < self.ehdr.e_shnum else { |
| 115 | + throw ELFError.malformedFile( |
| 116 | + self.filePath, "section index \(index) >= Elf64_Ehdr.e_shnum \(self.ehdr.e_shnum))") |
| 117 | + } |
| 118 | + |
| 119 | + let shdrSize = MemoryLayout<Elf64_Shdr>.size |
| 120 | + guard shdrSize == self.ehdr.e_shentsize else { |
| 121 | + throw ELFError.malformedFile(self.filePath, "Elf64_Ehdr.e_shentsize != \(shdrSize)") |
| 122 | + } |
| 123 | + |
| 124 | + let shdrOffset = Int(self.ehdr.e_shoff) + Int(index) * shdrSize |
| 125 | + let shdrData = self.fileData[shdrOffset..<(shdrOffset + shdrSize)] |
| 126 | + return shdrData.withUnsafeBytes { $0.load(as: Elf64_Shdr.self) } |
| 127 | + } |
| 128 | + |
| 129 | + // returns all data in the specified section |
| 130 | + internal func loadSection(_ shdr: Elf64_Shdr) throws -> Data { |
| 131 | + guard let sectionSize = Int(exactly: shdr.sh_size) else { |
| 132 | + throw ELFError.malformedFile(self.filePath, "Elf64_Shdr.sh_size too large \(shdr.sh_size)") |
| 133 | + } |
| 134 | + |
| 135 | + guard let fileOffset = Int(exactly: shdr.sh_offset) else { |
| 136 | + throw ELFError.malformedFile( |
| 137 | + self.filePath, "Elf64_Shdr.sh_offset too large \(shdr.sh_offset)") |
| 138 | + } |
| 139 | + |
| 140 | + return self.fileData[fileOffset..<(fileOffset + sectionSize)] |
| 141 | + } |
| 142 | +} |
0 commit comments