Skip to content

Commit

Permalink
Merge pull request #145 from p-x9/feature/symbols-random-access-colle…
Browse files Browse the repository at this point in the history
…ction

Fix to use `RandomAccessCollection` of symbols
  • Loading branch information
p-x9 authored Nov 18, 2024
2 parents b4b5132 + 911695f commit 94fe4fa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Sources/MachOKit/Protocol/MachORepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ extension MachORepresentable {
inSection sectionNumber: Int = 0,
isGlobalOnly: Bool = false
) -> [Symbol] {
let symbols = Array(self.symbols)
let symbols = self.symbols
var bestOffset: Int?
var bestSymbols: [Symbol] = []

Expand All @@ -401,7 +401,7 @@ extension MachORepresentable {
let globalStart: Int = numericCast(dysym.iextdefsym)
let globalCount: Int = numericCast(dysym.nextdefsym)
for i in globalStart ..< globalStart + globalCount {
let symbol = symbols[i]
let symbol = symbols[AnyIndex(i)]
let nlist = symbol.nlist
let symbolSectionNumber = symbol.nlist.sectionNumber

Expand All @@ -418,7 +418,7 @@ extension MachORepresentable {
let localStart: Int = numericCast(dysym.ilocalsym)
let localCount: Int = numericCast(dysym.nlocalsym)
for i in localStart ..< localStart + localCount {
let symbol = symbols[i]
let symbol = symbols[AnyIndex(i)]
let nlist = symbol.nlist
let symbolSectionNumber = symbol.nlist.sectionNumber

Expand Down Expand Up @@ -552,15 +552,15 @@ extension MachORepresentable {
return nil
}

let symbols = Array(self.symbols)
let symbols = self.symbols
var bestSymbol: Symbol?

if let dysym = loadCommands.dysymtab {
// find closest match in globals
let globalStart: Int = numericCast(dysym.iextdefsym)
let globalCount: Int = numericCast(dysym.nextdefsym)
for i in globalStart ..< globalStart + globalCount {
let symbol = symbols[i]
let symbol = symbols[AnyIndex(i)]
let nlist = symbol.nlist
guard nlist.flags?.type == .sect,
matchesName(nameC, symbol) else {
Expand All @@ -574,7 +574,7 @@ extension MachORepresentable {
let localStart: Int = numericCast(dysym.ilocalsym)
let localCount: Int = numericCast(dysym.nlocalsym)
for i in localStart ..< localStart + localCount {
let symbol = symbols[i]
let symbol = symbols[AnyIndex(i)]
let nlist = symbol.nlist
guard nlist.flags?.type == .sect,
nlist.flags?.stab == nil,
Expand Down
8 changes: 4 additions & 4 deletions Tests/MachOKitTests/MachOFilePrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ final class MachOFilePrintTests: XCTestCase {
}

func testSectionRelocationInfos() {
let symbols = Array(machO.symbols)
let symbols = machO.symbols
for section in machO.sections32 where section.nreloc > 0 {
print("----")
print("Name:", "\(section.segmentName).\(section.sectionName)"
Expand All @@ -118,7 +118,7 @@ final class MachOFilePrintTests: XCTestCase {
print("pcRelative:", info.isRelocatedPCRelative)
if let symbolIndex = info.symbolIndex {
print("SymbolIndex:", symbolIndex)
print("SymbolName:", symbols[symbolIndex].name)
print("SymbolName:", symbols[AnyIndex(symbolIndex)].name)
}
if let sectionOrdinal = info.sectionOrdinal {
print("SectionOrdinal:", sectionOrdinal)
Expand Down Expand Up @@ -239,7 +239,7 @@ final class MachOFilePrintTests: XCTestCase {

func testIndirectSymbols() throws {
guard let _indirectSymbols = machO.indirectSymbols else { return }
let symbols = Array(machO.symbols)
let symbols = machO.symbols
let indirectSymbols = Array(_indirectSymbols)

for section in machO.sections {
Expand All @@ -253,7 +253,7 @@ final class MachOFilePrintTests: XCTestCase {
for symbol in indirectSymbols {
print(" ", symbol._value, terminator: " ")
if let index = symbol.index {
print(symbols[index].name)
print(symbols[AnyIndex(index)].name)
} else {
print(symbol)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/MachOKitTests/MachOPrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ final class MachOPrintTests: XCTestCase {

func testIndirectSymbols() throws {
guard let _indirectSymbols = machO.indirectSymbols else { return }
let symbols = Array(machO.symbols)
let symbols = machO.symbols
let indirectSymbols = Array(_indirectSymbols)

for section in machO.sections {
Expand All @@ -184,7 +184,7 @@ final class MachOPrintTests: XCTestCase {
for symbol in indirectSymbols {
print(" ", symbol._value, terminator: " ")
if let index = symbol.index {
print(symbols[index].name)
print(symbols[AnyIndex(index)].name)
} else {
print(symbol)
}
Expand Down

0 comments on commit 94fe4fa

Please sign in to comment.