Skip to content

Commit bf386b9

Browse files
authored
Merge pull request #41 from staticfloat/sf/platform_extraction
Add `Platform(oh::ObjectHandle)`
2 parents b00ab22 + 7150c9f commit bf386b9

File tree

12 files changed

+75
-4
lines changed

12 files changed

+75
-4
lines changed

src/Abstract/ObjectHandle.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ where `oh <: COFFHandle`).
4444
- unpack()
4545
4646
### Format-specific properties
47+
- *Platform()*
4748
- *endianness()*
4849
- *is64bit()*
4950
- *isrelocatable()*
@@ -189,6 +190,12 @@ for f in [:skip, :seekstart, :eof, :read, :readuntil, :readbytes, :write]
189190
@eval $(f)(oh::H, args...) where {H<:ObjectHandle} = $(f)(iostream(oh), args...)
190191
end
191192

193+
"""
194+
Platform(oh::ObjectHandle)
195+
196+
Returns a `Platform` object representing the binary platform this object is built for.
197+
"""
198+
@mustimplement Platform(oh::ObjectHandle)
192199

193200
"""
194201
endianness(oh::ObjectHandle)

src/COFF/COFF.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module COFF
22

3-
using StructIO
3+
using StructIO, Base.BinaryPlatforms
4+
import Base.BinaryPlatforms: Platform
45

56
# Bring in ObjectFile definitions
67
using ObjectFile

src/COFF/COFFHandle.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ iostream(oh::COFFHandle) = oh.io
7373

7474
## Format-specific properties:
7575
header(oh::COFFHandle) = oh.header
76+
Platform(oh::COFFHandle) = Platform(coff_machine_to_arch(oh.header.Machine), "windows")
7677
endianness(oh::COFFHandle) = coff_header_endianness(header(oh))
7778
is64bit(oh::COFFHandle) = coff_header_is64bit(header(oh))
7879
isrelocatable(oh::COFFHandle) = isrelocatable(header(oh))

src/COFF/constants.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
const IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169 # MIPS little-endian WCE v2
2525
end
2626

27+
function coff_machine_to_arch(machine::UInt16)
28+
if machine (IMAGE_FILE_MACHINE_I386,)
29+
return "i686"
30+
elseif machine (IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_IA64)
31+
return "x86_64"
32+
elseif machine (IMAGE_FILE_MACHINE_ARM, IMAGE_FILE_MACHINE_ARMNT, IMAGE_FILE_MACHINE_THUMB)
33+
return "armv7l"
34+
elseif machine (IMAGE_FILE_MACHINE_ARM64,)
35+
return "aarch64"
36+
elseif machine (IMAGE_FILE_MACHINE_POWERPC,)
37+
return "ppc64le"
38+
end
39+
end
40+
2741
# # # Characteristics
2842

2943
@constants IMAGE_FILE_CHARACTERISTICS "IMAGE_FILE_" begin

src/ELF/ELF.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ELF
22

3-
using StructIO
3+
using StructIO, Base.BinaryPlatforms
4+
import Base.BinaryPlatforms: Platform
45

56
# Bring in ObjectFile definitions
67
using ObjectFile

src/ELF/ELFHandle.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ end
6464
startaddr(oh::ELFHandle) = oh.start
6565
iostream(oh::ELFHandle) = oh.io
6666

67+
# We don't try to inspect dynamic libraries to figure out if this is a glibc or musl dynamic object
68+
function strip_libc_tag(p::Platform)
69+
delete!(tags(p), "libc")
70+
return p
71+
end
6772

6873
## Format-specific properties:
6974
header(oh::ELFHandle) = oh.header
75+
Platform(oh::ELFHandle) = strip_libc_tag(Platform(elf_machine_to_arch(oh.header.e_machine), "linux"))
7076
endianness(oh::ELFHandle) = elf_internal_endianness(oh.ei)
7177
is64bit(oh::ELFHandle) = elf_internal_is64bit(oh.ei)
7278
isrelocatable(oh::ELFHandle) = header(oh).e_type == ET_REL

src/ELF/ELFHeader.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ function machinetype(e_machine)
5656
return string("Unknown (0x",string(e_machine, base=16),")")
5757
end
5858

59+
function elf_machine_to_arch(machine::UInt16)
60+
if machine (EM_386,)
61+
return "i686"
62+
elseif machine (EM_IA_64, EM_X86_64)
63+
return "x86_64"
64+
elseif machine (EM_ARM,)
65+
return "armv7l"
66+
elseif machine (EM_AARCH64,)
67+
return "aarch64"
68+
elseif machine (EM_PPC64,)
69+
return "ppc64le"
70+
end
71+
end
72+
5973

6074
function show(io::IO, header::ELFHeader)
6175
println(io, "ELF Header")

src/MachO/MachO.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module MachO
22

3-
using StructIO
3+
using StructIO, Base.BinaryPlatforms
4+
import Base.BinaryPlatforms: Platform
45

56
# Bring in ObjectFile definitions
67
using ObjectFile

src/MachO/MachOHandle.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ iostream(oh::AbstractMachOHandle) = oh.io
4646
## Format-specific properties:
4747
header(oh::AbstractMachOHandle) = oh.header
4848
endianness(oh::AbstractMachOHandle) = macho_endianness(header(oh).magic)
49+
Platform(oh::MachOHandle) = Platform(macho_cpu_to_arch(header(oh).cputype), "macos")
4950
is64bit(oh::MachOHandle) = macho_is64bit(header(oh).magic)
5051
isrelocatable(oh::MachOHandle) = header(oh).filetype == MH_OBJECT
5152
isexecutable(oh::MachOHandle) = header(oh).filetype == MH_EXECUTE

src/MachO/constants.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ const CPU_ARCH_ABI64_32 = 0x02000000
4848
const CPU_TYPE_ARM64_32 = CPU_TYPE_ARM | CPU_ARCH_ABI64_32
4949
end
5050

51+
function macho_cpu_to_arch(cputype::UInt32)
52+
if cputype (CPU_TYPE_X86,)
53+
return "i686"
54+
elseif cputype (CPU_TYPE_X86_64,)
55+
return "x86_64"
56+
elseif cputype (CPU_TYPE_ARM,)
57+
return "armv7l"
58+
elseif cputype (CPU_TYPE_POWERPC64,)
59+
return "ppc64le"
60+
elseif cputype (CPU_TYPE_ARM64,)
61+
return "aarch64"
62+
end
63+
end
64+
65+
5166
# TODO subtype constants
5267
@constants NLISTTYPES "N_" begin
5368
const N_STAB = 0xe0

0 commit comments

Comments
 (0)