mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Use the new module structure rather the old SwiftShims header. This is much cleaner and lets us include operating system headers to get the relevant definitions where possible. Add code to support ELF and DWARF, including decompression using zlib, zstd and liblzma if those turn out to be required and available. rdar://110261712
71 lines
1.8 KiB
Swift
71 lines
1.8 KiB
Swift
//===--- FileImageSource.swift - An image source that reads from a file ---===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2023 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Defines FileImageSource, an image source that reads data from a file.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Swift
|
|
|
|
@_implementationOnly import OS.Libc
|
|
|
|
enum FileImageSourceError: Error {
|
|
case posixError(Int32)
|
|
case truncatedRead
|
|
}
|
|
|
|
class FileImageSource: ImageSource {
|
|
private var fd: Int32
|
|
|
|
public var isMappedImage: Bool { return false }
|
|
|
|
private var _path: String
|
|
public var path: String? { return _path }
|
|
|
|
public lazy var bounds: Bounds? = {
|
|
let size = lseek(fd, 0, SEEK_END)
|
|
if size < 0 {
|
|
return nil
|
|
}
|
|
return Bounds(base: 0, size: Size(size))
|
|
}()
|
|
|
|
public init(path: String) throws {
|
|
_path = path
|
|
fd = _swift_open(path, O_RDONLY, 0)
|
|
if fd < 0 {
|
|
throw FileImageSourceError.posixError(_swift_get_errno())
|
|
}
|
|
}
|
|
|
|
deinit {
|
|
close(fd)
|
|
}
|
|
|
|
public func fetch<T>(from addr: Address,
|
|
into buffer: UnsafeMutableBufferPointer<T>) throws {
|
|
while true {
|
|
let size = MemoryLayout<T>.stride * buffer.count
|
|
let result = pread(fd, buffer.baseAddress, size, off_t(addr))
|
|
|
|
if result < 0 {
|
|
throw FileImageSourceError.posixError(_swift_get_errno())
|
|
}
|
|
|
|
if result != size {
|
|
throw FileImageSourceError.truncatedRead
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|