Skip to content

refactor(MachOSwiftSection): extract _sectionOffsetAndSize helper#81

Merged
Mx-Iris merged 2 commits intomainfrom
refactor/section-offset-size-helper
Apr 26, 2026
Merged

refactor(MachOSwiftSection): extract _sectionOffsetAndSize helper#81
Mx-Iris merged 2 commits intomainfrom
refactor/section-offset-size-helper

Conversation

@Mx-Iris
Copy link
Copy Markdown
Member

@Mx-Iris Mx-Iris commented Apr 23, 2026

Summary

  • Extract _sectionOffsetAndSize(of:in:) in both MachOFile.Swift and MachOImage.Swift to compute a section's offset (cache-aware for MachOFile, slide-aware for MachOImage) and size once.
  • Rewrite _readDescriptors, _readRelativeDescriptors, _readTypeMetadataRecords, and _readProtocolRecords to call the new helper instead of repeating the offset/size computation inline.
  • No behavior change; pure deduplication.

Test plan

  • swift build
  • swift test --filter MachOSwiftSectionTests
  • swift test --filter SwiftDumpTests

Dedupe repeated section offset/size computation across
_readDescriptors, _readRelativeDescriptors, _readTypeMetadataRecords,
and _readProtocolRecords in both MachOFile.Swift and MachOImage.Swift.
Copilot AI review requested due to automatic review settings April 23, 2026 10:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors MachOSwiftSection’s Mach-O Swift section readers by extracting a shared _sectionOffsetAndSize(of:in:) helper for both MachOImage (slide-aware) and MachOFile (dyld-cache-aware), reducing duplicated offset/size calculations while keeping behavior the same.

Changes:

  • Add _sectionOffsetAndSize(of:in:) helper in MachOImage.Swift and MachOFile.Swift.
  • Update _readDescriptors, _readRelativeDescriptors, _readTypeMetadataRecords, and _readProtocolRecords to use the helper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
Sources/MachOSwiftSection/MachOImage+Swift.swift Deduplicates section offset/size computation for in-memory images and updates section-reading helpers to reuse it.
Sources/MachOSwiftSection/MachOFile+Swift.swift Deduplicates section offset/size computation for file-backed Mach-Os (including cache-aware offsets) and updates section-reading helpers to reuse it.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors MachOFile.Swift and MachOImage.Swift by extracting the logic for calculating section offsets and sizes into a new private helper method, _sectionOffsetAndSize. This change reduces code duplication within each extension. The review feedback suggests further improvements, including removing redundant method parameters, using the more idiomatic distance(to:) method for pointer calculations, and exploring a shared protocol to unify the now-identical implementations across both files.


extension MachOFile.Swift {
private func _readDescriptors<Descriptor: TopLevelDescriptor>(from swiftMachOSection: MachOSwiftSectionName, in machO: MachOFile) throws -> [Descriptor] {
private func _sectionOffsetAndSize(of swiftMachOSection: MachOSwiftSectionName, in machO: MachOFile) throws -> (offset: Int, size: Int) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The machO parameter is redundant in this and other private helper methods because it is already available as a property of the Swift struct (self.machO). Removing it would simplify the method signatures and call sites across both MachOFile.Swift and MachOImage.Swift extensions.

Suggested change
private func _sectionOffsetAndSize(of swiftMachOSection: MachOSwiftSectionName, in machO: MachOFile) throws -> (offset: Int, size: Int) {
private func _sectionOffsetAndSize(of swiftMachOSection: MachOSwiftSectionName) throws -> (offset: Int, size: Int) {

return (offset, section.size)
}

private func _readDescriptors<Descriptor: TopLevelDescriptor>(from swiftMachOSection: MachOSwiftSectionName, in machO: MachOFile) throws -> [Descriptor] {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The implementation of _readDescriptors (and the other _read... methods below) is now identical to the implementation in MachOImage+Swift.swift. To improve maintainability and avoid future divergence, consider extracting these into a shared protocol extension. You could define an internal protocol that provides the _sectionOffsetAndSize and reading methods, and have both MachOFile and MachOImage (or their Swift wrappers) conform to it.

var descriptors: [Descriptor] = []
let vmaddrSlide = try required(machO.vmaddrSlide)
let start = try required(UnsafeRawPointer(bitPattern: section.address + vmaddrSlide))
let offset = start.bitPattern.int - machO.ptr.bitPattern.int
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Use distance(to:) for a more idiomatic and readable way to calculate the offset between two pointers in Swift. This avoids manual bit pattern manipulation and integer conversions.

Suggested change
let offset = start.bitPattern.int - machO.ptr.bitPattern.int
let offset = machO.ptr.distance(to: start)

- Drop redundant machO parameter from private _read* and _sectionOffsetAndSize helpers since self.machO is already available.
- Use UnsafeRawPointer.distance(to:) in place of manual bitPattern subtraction in MachOImage variant.
- Move `extension RelativeDirectPointer: LayoutProtocol` into a dedicated file under MachOPointers, alongside the pointer definition.
@Mx-Iris Mx-Iris merged commit 3e43e77 into main Apr 26, 2026
2 checks passed
@Mx-Iris Mx-Iris deleted the refactor/section-offset-size-helper branch April 26, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants