Skip to content

Commit e93e130

Browse files
committed
Rework readmeta to always return a collection of handles.
1 parent 7397b6d commit e93e130

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

src/COFF/COFFHandle.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct COFFHandle{T<:IO} <: ObjectHandle
1313

1414
# The parsed-out header of the COFF object
1515
header::COFFHeader
16-
16+
1717
# The location of the header (because of MZ confusion, we must store this)
1818
header_offset::UInt32
1919

@@ -63,7 +63,7 @@ function readmeta(io::IO, ::Type{H}) where {H <: COFFHandle}
6363
opt_header = read(io, COFFOptionalHeader)
6464

6565
# Construct our COFFHandle, pilfering the filename from the IOStream
66-
return COFFHandle(io, Int64(start), header, header_offset, opt_header, path(io))
66+
return [COFFHandle(io, Int64(start), header, header_offset, opt_header, path(io))]
6767
end
6868

6969

src/ELF/ELFHandle.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function readmeta(io::IO, ::Type{H}) where {H <: ELFHandle}
3535
start = position(io)
3636

3737
# Check for magic bytes
38-
magic = [read(io, UInt8) for idx in 1:4]
38+
magic = [read(io, UInt8) for idx in 1:4]
3939
if any(magic .!= elven_magic)
4040
msg = """
4141
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected ELF
@@ -56,7 +56,7 @@ function readmeta(io::IO, ::Type{H}) where {H <: ELFHandle}
5656
seek(io, start)
5757

5858
# Construct our ELFHandle, pilfering the filename from the IOStream
59-
return ELFHandle(io, Int64(start), ei, header, path(io))
59+
return [ELFHandle(io, Int64(start), ei, header, path(io))]
6060
end
6161

6262

src/MachO/MachOFat.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ length(h::FatMachOHandle) = length(h.header.archs)
7070
eltype(::Type{S}) where {S <: FatMachOHandle} = MachOLoadCmdRef
7171
function getindex(h::FatMachOHandle, idx)
7272
seek(h.io, h.start + h.header.archs[idx].offset)
73-
readmeta(h.io, MachOHandle)
73+
only(readmeta(h.io, MachOHandle))
7474
end
7575

7676
function show(io::IO, oh::FatMachOHandle)

src/MachO/MachOHandle.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function readmeta(io::IO,::Type{MachOHandle})
3535

3636
# Unpack the header
3737
header = unpack(io, header_type, endianness)
38-
return MachOHandle(io, Int64(start), header, path(io))
38+
return [MachOHandle(io, Int64(start), header, path(io))]
3939
end
4040

4141
## IOStream-like operations:

test/runtests.jl

+25-19
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ using Test
66
mktempdir() do dir
77
fpath = joinpath(dir, "empty")
88
touch(fpath)
9-
@test_throws MagicMismatch readmeta(fpath) do oh
9+
@test_throws MagicMismatch readmeta(fpath) do ohs
1010
@test false
1111
end
1212
end
1313
end
1414

1515
function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
1616
# Actually read it in
17-
oh_exe = readmeta(open(fooifier_path, "r"))
18-
oh_lib = readmeta(open(libfoo_path, "r"))
17+
oh_exe = only(readmeta(open(fooifier_path, "r")))
18+
oh_lib = only(readmeta(open(libfoo_path, "r")))
1919

2020
# Tease out some information from the containing folder name
2121
dir_path = basename(dirname(libfoo_path))
@@ -158,28 +158,31 @@ function test_libfoo_and_fooifier(fooifier_path, libfoo_path)
158158
end
159159

160160
function test_fat_libfoo(file)
161-
oh = readmeta(open(file, "r"))
162-
@test isa(oh, FatMachOHandle)
163-
local (ntotal, n64) = (0, 0)
164-
for coh in oh
161+
ohs = readmeta(open(file, "r"))
162+
@test isa(ohs, FatMachOHandle)
163+
@test length(ohs) == 2
164+
ntotal, n64 = 0, 0
165+
for oh in ohs
165166
ntotal += 1
166-
n64 += is64bit(coh)
167+
n64 += is64bit(oh)
167168
end
168169
@test ntotal == 2
169170
@test n64 == 1
170171
end
171172

172173
function test_metal(file)
173-
oh = readmeta(open(file, "r"))
174-
@test isa(oh, FatMachOHandle)
175-
@test length(oh) == 2
174+
ohs = readmeta(open(file, "r"))
175+
@test isa(ohs, FatMachOHandle)
176+
@test length(ohs) == 2
176177

177-
arch = oh[1]
178-
@test arch.header isa MachO.MachOHeader64
179-
@test findfirst(Sections(arch), "__TEXT,__compute") !== nothing
178+
let oh = ohs[1]
179+
@test oh.header isa MachO.MachOHeader64
180+
@test findfirst(Sections(oh), "__TEXT,__compute") !== nothing
181+
end
180182

181-
arch = oh[2]
182-
@test arch.header isa MachO.MetallibHeader
183+
let oh = ohs[2]
184+
@test oh.header isa MachO.MetallibHeader
185+
end
183186
end
184187

185188
# Run ELF tests
@@ -202,7 +205,8 @@ test_libfoo_and_fooifier("./win64/fooifier.exe", "./win64/libfoo.dll")
202205

203206
# Extract all pieces of `.gnu.version_d` from libstdc++.so, find the `GLIBCXX_*`
204207
# symbols, and use the maximum version of that to find the GLIBCXX ABI version number
205-
version_symbols = readmeta(libstdcxx_path) do oh
208+
version_symbols = readmeta(libstdcxx_path) do ohs
209+
oh = only(ohs)
206210
unique(vcat((x -> x.names).(ObjectFile.ELF.ELFVersionData(oh))...))
207211
end
208212
version_symbols = filter(x -> startswith(x, "GLIBCXX_"), version_symbols)
@@ -216,7 +220,8 @@ end
216220
# Test that 6a66694a8dd5ca85bd96fe6236f21d5b183e7de6 fix worked
217221
libmsobj_path = "./win32/msobj140.dll"
218222

219-
dynamic_links = readmeta(libmsobj_path) do oh
223+
dynamic_links = readmeta(libmsobj_path) do ohs
224+
oh = only(ohs)
220225
path.(DynamicLinks(oh))
221226
end
222227

@@ -226,7 +231,8 @@ end
226231
@test "api-ms-win-crt-runtime-l1-1-0.dll" in dynamic_links
227232

228233
whouses_exe = "./win32/WhoUses.exe"
229-
dynamic_links = readmeta(whouses_exe) do oh
234+
dynamic_links = readmeta(whouses_exe) do ohs
235+
oh = only(ohs)
230236
path.(DynamicLinks(oh))
231237
end
232238

0 commit comments

Comments
 (0)