|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 | +class ElfFile { |
| 17 | + public enum Error: Swift.Error { |
| 18 | + case FileOpenFailure(_ filePath: String) |
| 19 | + case FileReadFailure(_ filePath: String, offset: UInt64, size: UInt64) |
| 20 | + case FileNotElfFormat(_ filePath: String) |
| 21 | + case MalformedElfFile(_ filePath: String, description: String = "") |
| 22 | + } |
| 23 | + |
| 24 | + public typealias SymbolMap = [String: (start: UInt64, end: UInt64)] |
| 25 | + |
| 26 | + let filePath: String |
| 27 | + let file: FileHandle |
| 28 | + let ehdr: ElfEhdr |
| 29 | + let isElf64: Bool |
| 30 | + |
| 31 | + public init(filePath: String) throws { |
| 32 | + self.filePath = filePath |
| 33 | + |
| 34 | + guard let file = try? FileHandle(forReadingFrom: URL(fileURLWithPath: filePath)) else { |
| 35 | + throw Error.FileOpenFailure(filePath) |
| 36 | + } |
| 37 | + self.file = file |
| 38 | + |
| 39 | + let identLen = Int(EI_NIDENT) |
| 40 | + file.seek(toFileOffset: 0) |
| 41 | + guard let identData = try file.read(upToCount: identLen), identData.count == identLen else { |
| 42 | + file.closeFile() |
| 43 | + throw Error.FileReadFailure(filePath, offset: 0, size: UInt64(identLen)) |
| 44 | + } |
| 45 | + |
| 46 | + let identMagic = String(bytes: identData.prefix(Int(SELFMAG)), encoding: .utf8) |
| 47 | + guard identMagic == ELFMAG else { |
| 48 | + file.closeFile() |
| 49 | + throw Error.FileNotElfFormat(filePath) |
| 50 | + } |
| 51 | + |
| 52 | + let identClass = identData[Int(EI_CLASS)] |
| 53 | + let isElf64 = identClass == ELFCLASS64 |
| 54 | + guard isElf64 || identClass == ELFCLASS32 else { |
| 55 | + file.closeFile() |
| 56 | + throw Error.MalformedElfFile(filePath, description: "unsupported ELFCLASS: \(identClass)") |
| 57 | + } |
| 58 | + self.isElf64 = isElf64 |
| 59 | + |
| 60 | + let ehdrSize = isElf64 ? Elf64_Ehdr.symbolSize : Elf32_Ehdr.symbolSize |
| 61 | + file.seek(toFileOffset: 0) |
| 62 | + guard let ehdrData = try file.read(upToCount: ehdrSize), ehdrData.count == ehdrSize else { |
| 63 | + file.closeFile() |
| 64 | + throw Error.FileReadFailure(filePath, offset: 0, size: UInt64(ehdrSize)) |
| 65 | + } |
| 66 | + |
| 67 | + if isElf64 { |
| 68 | + self.ehdr = ehdrData.withUnsafeBytes { $0.load(as: Elf64_Ehdr.self) as ElfEhdr } |
| 69 | + } else { |
| 70 | + self.ehdr = ehdrData.withUnsafeBytes { $0.load(as: Elf32_Ehdr.self) as ElfEhdr } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + deinit { file.closeFile() } |
| 75 | + |
| 76 | + // returns a map of symbol names to their offset range in file (+ baseAddress) |
| 77 | + public func loadSymbols(baseAddress: UInt64 = 0) throws -> SymbolMap { |
| 78 | + guard let sectionCount = UInt(exactly: self.ehdr.shnum) else { |
| 79 | + throw Error.MalformedElfFile( |
| 80 | + self.filePath, description: "invalid ehdr.shnum: \(self.ehdr.shnum)") |
| 81 | + } |
| 82 | + |
| 83 | + var symbols: SymbolMap = [:] |
| 84 | + for sectionIndex in 0..<sectionCount { |
| 85 | + let shdr: ElfShdr = |
| 86 | + isElf64 |
| 87 | + ? try self.readShdr(index: sectionIndex) as Elf64_Shdr |
| 88 | + : try self.readShdr(index: sectionIndex) as Elf32_Shdr |
| 89 | + |
| 90 | + guard shdr.type == SHT_SYMTAB || shdr.type == SHT_DYNSYM else { continue } |
| 91 | + |
| 92 | + let sectionData: Data = try self.readSection(shdr: shdr) |
| 93 | + let symTable: [ElfSym] = |
| 94 | + self.isElf64 |
| 95 | + ? sectionData.withUnsafeBytes { Array($0.bindMemory(to: Elf64_Sym.self)) } |
| 96 | + : sectionData.withUnsafeBytes { Array($0.bindMemory(to: Elf32_Sym.self)) } |
| 97 | + |
| 98 | + guard shdr.entsize == (self.isElf64 ? Elf64_Sym.symbolSize : Elf32_Sym.symbolSize) else { |
| 99 | + throw Error.MalformedElfFile(self.filePath, description: "invalid ElfShdr.sh_entsize") |
| 100 | + } |
| 101 | + |
| 102 | + // the link field in the section header for a symbol table section refers |
| 103 | + // to the index of the string table section containing the symbol names |
| 104 | + guard let linkIndex = UInt(exactly: shdr.link) else { |
| 105 | + throw Error.MalformedElfFile( |
| 106 | + self.filePath, description: "invalid ElfShdr.sh_link: \(shdr.link)") |
| 107 | + } |
| 108 | + |
| 109 | + let shdrLink: ElfShdr = |
| 110 | + isElf64 |
| 111 | + ? try self.readShdr(index: UInt(linkIndex)) as Elf64_Shdr |
| 112 | + : try self.readShdr(index: UInt(linkIndex)) as Elf32_Shdr |
| 113 | + |
| 114 | + guard shdrLink.type == SHT_STRTAB else { |
| 115 | + throw Error.MalformedElfFile(self.filePath, description: "linked section not SHT_STRTAB") |
| 116 | + } |
| 117 | + |
| 118 | + // load the entire contents of the string table into memory |
| 119 | + let strTable: Data = try self.readSection(shdr: shdrLink) |
| 120 | + |
| 121 | + let symCount = Int(shdr.size / shdr.entsize) |
| 122 | + for symIndex in 0..<symCount { |
| 123 | + let sym = symTable[symIndex] |
| 124 | + guard sym.shndx != SHN_UNDEF, sym.value != 0, sym.size != 0 else { continue } |
| 125 | + |
| 126 | + // sym.name is a byte offset into the string table |
| 127 | + guard let strStart = Int(exactly: sym.name), strStart < strTable.count else { |
| 128 | + throw Error.MalformedElfFile( |
| 129 | + self.filePath, description: "invalid string table offset: \(sym.name)") |
| 130 | + } |
| 131 | + |
| 132 | + guard let strEnd = strTable[strStart...].firstIndex(of: 0), |
| 133 | + let symName = String(data: strTable[strStart..<strEnd], encoding: .utf8) |
| 134 | + else { |
| 135 | + throw Error.MalformedElfFile( |
| 136 | + self.filePath, description: "invalid string @ offset \(strStart)") |
| 137 | + } |
| 138 | + |
| 139 | + // rebase the symbol value on the base address provided by the caller |
| 140 | + let symStart = sym.value + baseAddress |
| 141 | + symbols[symName] = (start: symStart, end: symStart + sym.size) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return symbols |
| 146 | + } |
| 147 | + |
| 148 | + // reads and returns the Elf32_Shdr or Elf64_Shdr at the specified index |
| 149 | + internal func readShdr<T: ElfShdr>(index: UInt) throws -> T { |
| 150 | + guard index < self.ehdr.shnum else { |
| 151 | + throw Error.MalformedElfFile( |
| 152 | + self.filePath, description: "section index \(index) >= ElfEhdr.e_shnum \(self.ehdr.shnum))") |
| 153 | + } |
| 154 | + |
| 155 | + let shdrSize = T.symbolSize |
| 156 | + guard shdrSize == self.ehdr.shentsize else { |
| 157 | + throw Error.MalformedElfFile(self.filePath, description: "ElfEhdr.e_shentsize != \(shdrSize)") |
| 158 | + } |
| 159 | + |
| 160 | + let shdrOffset: UInt64 = self.ehdr.shoff + UInt64(index) * UInt64(shdrSize) |
| 161 | + self.file.seek(toFileOffset: shdrOffset) |
| 162 | + guard let shdrData = try self.file.read(upToCount: shdrSize), shdrData.count == shdrSize else { |
| 163 | + throw Error.FileReadFailure(self.filePath, offset: shdrOffset, size: UInt64(shdrSize)) |
| 164 | + } |
| 165 | + |
| 166 | + return shdrData.withUnsafeBytes { $0.load(as: T.self) as T } |
| 167 | + } |
| 168 | + |
| 169 | + // reads and returns all data in the specified section |
| 170 | + internal func readSection(shdr: ElfShdr) throws -> Data { |
| 171 | + guard let sectionSize = Int(exactly: shdr.size) else { |
| 172 | + throw Error.MalformedElfFile( |
| 173 | + self.filePath, description: "ElfShdr.sh_size too large \(shdr.size)") |
| 174 | + } |
| 175 | + |
| 176 | + let fileOffset = shdr.offset |
| 177 | + self.file.seek(toFileOffset: fileOffset) |
| 178 | + guard let data = try self.file.read(upToCount: sectionSize), data.count == sectionSize else { |
| 179 | + throw Error.FileReadFailure(self.filePath, offset: fileOffset, size: UInt64(sectionSize)) |
| 180 | + } |
| 181 | + |
| 182 | + return data |
| 183 | + } |
| 184 | +} |
0 commit comments