From 8cf58c2f6bb9304e14367beb9eecac324ec8c569 Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Fri, 8 Mar 2019 14:02:36 -0800 Subject: [PATCH 1/4] Add `keys()` API to collections for usage with things like `findfirst()` --- src/Abstract/DynamicLink.jl | 7 +++++-- src/Abstract/Section.jl | 6 ++++-- src/Abstract/Segment.jl | 4 +++- src/COFF/COFF.jl | 2 +- src/ELF/ELFRelocation.jl | 1 + src/MachO/MachO.jl | 2 +- src/MachO/MachOLoadCmd.jl | 2 ++ 7 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Abstract/DynamicLink.jl b/src/Abstract/DynamicLink.jl index 5ce7fd7..e3f3831 100644 --- a/src/Abstract/DynamicLink.jl +++ b/src/Abstract/DynamicLink.jl @@ -1,6 +1,6 @@ # Export DynamicLinks API export DynamicLinks, - getindex, length, iterate, lastindex, eltype, + getindex, length, iterate, keys, lastindex, eltype, handle, header # Export Dynamic Link API @@ -12,7 +12,7 @@ export RPath, handle, rpaths, canonical_rpaths, find_library # Import iteration protocol -import Base: iterate, length, lastindex +import Base: iterate, keys, length, lastindex """ DynamicLinks @@ -28,6 +28,7 @@ given below, with methods that subclasses must implement marked in emphasis: - *getindex()* - *lastindex()* - iterate() + - keys() - eltype() ### Misc. @@ -36,6 +37,7 @@ given below, with methods that subclasses must implement marked in emphasis: abstract type DynamicLinks{H <: ObjectHandle} end @mustimplement lastindex(dls::DynamicLinks) +keys(dls::DynamicLinks) = 1:length(dls) iterate(dls::DynamicLinks, idx=1) = idx > length(dls) ? nothing : (dls[idx], idx+1) length(dls::DynamicLinks) = lastindex(dls) eltype(::Type{D}) where {D <: DynamicLinks} = DynamicLink @@ -107,6 +109,7 @@ Return the list of paths that will be searched for shared libraries. @mustimplement rpaths(rpath::RPath) lastindex(rpath::RPath) = lastindex(rpaths(rpath)) +keys(rpath::RPath) = 1:length(rpath) iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1) length(rpath::RPath) = lastindex(rpath) eltype(::Type{D}) where {D <: RPath} = String diff --git a/src/Abstract/Section.jl b/src/Abstract/Section.jl index 0c5c7fd..89d72c4 100644 --- a/src/Abstract/Section.jl +++ b/src/Abstract/Section.jl @@ -1,6 +1,6 @@ # Export Sections API export Sections, - getindex, length, lastindex, iterate, eltype, + getindex, length, lastindex, iterate, keys, eltype, findall, findfirst, handle, header, format_string @@ -15,7 +15,7 @@ export SectionRef, # Import Base methods for extension import Base: read, seek, seekstart, eof, length, eltype, findall, - findfirst, iterate, lastindex + findfirst, iterate, keys, lastindex """ Sections @@ -35,6 +35,7 @@ in emphasis: - *lastindex()* - length() - iterate() + - keys() - eltype() ### Search @@ -49,6 +50,7 @@ abstract type Sections{H<:ObjectHandle} end # Fairly simple iteration interface specification @mustimplement lastindex(sections::Sections) length(sections::Sections) = lastindex(sections) +keys(sections::Sections) = 1:length(sections) iterate(sections::Sections, idx=1) = idx > length(sections) ? nothing : (sections[idx], idx+1) eltype(::Type{S}) where {S <: Sections} = SectionRef diff --git a/src/Abstract/Segment.jl b/src/Abstract/Segment.jl index 4589f72..80d4075 100644 --- a/src/Abstract/Segment.jl +++ b/src/Abstract/Segment.jl @@ -8,7 +8,7 @@ export Segment, deref, segment_name, segment_offset, segment_file_size, export SegmentRef, segment_number # Import Base stuff for extension -import Base: getindex, length, eltype, findfirst, findall, iterate, lastindex +import Base: getindex, length, eltype, findfirst, findall, iterate, keys, lastindex """ Segments @@ -28,6 +28,7 @@ in emphasis: - *lastindex()* - length() - iterate() + - keys() - eltype() ### Search @@ -42,6 +43,7 @@ abstract type Segments{H<:ObjectHandle} end # Fairly simple iteration interface specification @mustimplement lastindex(segs::Segments) length(segs::Segments) = lastindex(segs) +keys(segs::Segments) = 1:length(segs) iterate(segs::Segments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1) eltype(::Type{S}) where {S <: Segments} = SegmentRef diff --git a/src/COFF/COFF.jl b/src/COFF/COFF.jl index 3c52aff..8b6edd3 100644 --- a/src/COFF/COFF.jl +++ b/src/COFF/COFF.jl @@ -6,7 +6,7 @@ using StructIO using ObjectFile import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef, Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef, - getindex, length, lastindex, iterate, eltype, handle, header, path, + getindex, length, lastindex, iterate, keys, eltype, handle, header, path, rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip, iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable, isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name, diff --git a/src/ELF/ELFRelocation.jl b/src/ELF/ELFRelocation.jl index dde6ea0..ebc867a 100644 --- a/src/ELF/ELFRelocation.jl +++ b/src/ELF/ELFRelocation.jl @@ -53,6 +53,7 @@ function getindex{T}(s::Relocations{T},n) RelocationRef{T}(s.sec.handle,unpack(s.sec.handle, T)) end +keys(s::Relocations) = 1:length(s) iterate(s::Relocations, idx=1) = idx > length(s) ? nothing : (s[idx], idx+1) # Utilities for extracting information from relocation entries diff --git a/src/MachO/MachO.jl b/src/MachO/MachO.jl index 16d975d..df45da3 100644 --- a/src/MachO/MachO.jl +++ b/src/MachO/MachO.jl @@ -6,7 +6,7 @@ using StructIO using ObjectFile import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef, Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef, - getindex, length, iterate, lastindex, eltype, handle, header, path, + getindex, length, iterate, keys, lastindex, eltype, handle, header, path, rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip, iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable, isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name, diff --git a/src/MachO/MachOLoadCmd.jl b/src/MachO/MachOLoadCmd.jl index d7a49f7..96a228b 100644 --- a/src/MachO/MachOLoadCmd.jl +++ b/src/MachO/MachOLoadCmd.jl @@ -54,6 +54,7 @@ Allows iteration over the LoadCmds within a Mach-O file. - getindex() - lastindex() - iterate() + - keys() - length() - eltype() @@ -144,6 +145,7 @@ handle(lcs::MachOLoadCmds) = lcs.handle header(lcs::MachOLoadCmds) = header(handle(lcs)) # Iteration +keys(lcs::MachOLoadCmds) = 1:length(lcs) iterate(lcs::MachOLoadCmds, idx=1) = idx > length(lcs) ? nothing : (lcs.cmds[idx], idx+1) lastindex(lcs::MachOLoadCmds) = lastindex(lcs.cmds) length(lcs::MachOLoadCmds) = length(lcs.cmds) From 51ebf13811235d3259a5f6d48ce61b90c569f5d4 Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Fri, 8 Mar 2019 14:03:16 -0800 Subject: [PATCH 2/4] Fix some typos --- src/Abstract/DynamicLink.jl | 2 +- src/Abstract/Segment.jl | 2 +- src/MachO/MachOLoadCmd.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Abstract/DynamicLink.jl b/src/Abstract/DynamicLink.jl index e3f3831..cdf7bb7 100644 --- a/src/Abstract/DynamicLink.jl +++ b/src/Abstract/DynamicLink.jl @@ -110,7 +110,7 @@ Return the list of paths that will be searched for shared libraries. lastindex(rpath::RPath) = lastindex(rpaths(rpath)) keys(rpath::RPath) = 1:length(rpath) -iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1) +iterate(rpath::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1) length(rpath::RPath) = lastindex(rpath) eltype(::Type{D}) where {D <: RPath} = String getindex(rpath::RPath, idx) = rpaths(rpath)[idx] diff --git a/src/Abstract/Segment.jl b/src/Abstract/Segment.jl index 80d4075..4d3ee4e 100644 --- a/src/Abstract/Segment.jl +++ b/src/Abstract/Segment.jl @@ -192,4 +192,4 @@ Return the index of the referred segment. ## Printing -show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H) \ No newline at end of file +show(io::IO, segs::Segments{H}) where {H <: ObjectHandle} = show_collection(io, segs, H) diff --git a/src/MachO/MachOLoadCmd.jl b/src/MachO/MachOLoadCmd.jl index 96a228b..f29757e 100644 --- a/src/MachO/MachOLoadCmd.jl +++ b/src/MachO/MachOLoadCmd.jl @@ -3,7 +3,7 @@ export MachOLoadCmd, MachOLoadCmdHeader, MachOLoadCmds, MachOLoadCmdRef """ -MachOLoadCmdHeader + MachOLoadCmdHeader All MachO Load Commands have a common header, containing information about what kind of Load Command it is, and how large the load command is. From 64a19153d4a0a8b63c00cea6bdf3ced4ffddfa4f Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Fri, 8 Mar 2019 14:03:20 -0800 Subject: [PATCH 3/4] `ELFSegments` should inherit from `Segments` --- src/ELF/ELFSegment.jl | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/ELF/ELFSegment.jl b/src/ELF/ELFSegment.jl index 0dbe133..e6cec82 100644 --- a/src/ELF/ELFSegment.jl +++ b/src/ELF/ELFSegment.jl @@ -6,30 +6,12 @@ export ELFSegments, ELFSegment, ELFSegment32, ELFSegment64 ELF segment header table type, containing information about the number of segments within the ELF object, the location of the segment headers, etc... """ -struct ELFSegments{H <: ELFHandle} +struct ELFSegments{H <: ELFHandle} <: Segments{H} handle::H end Segments(oh::ELFHandle) = ELFSegments(oh) handle(segs::ELFSegments) = segs.handle - -# Iteration -iterate(segs::ELFSegments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1) lastindex(segs::ELFSegments) = header(handle(segs)).e_phnum -length(segs::ELFSegments) = lastindex(segs) -eltype(::Type{S}) where {S <: ELFSegments} = ELFSegmentRef - -function getindex(segs::ELFSegments{H}, idx) where {H <: ELFHandle} - # Punt off to `getindex_ref` - oh = handle(segs) - return getindex_ref( - segs, - segment_header_offset(oh), - segment_header_size(oh), - segment_header_type(oh), - SegmentRef, - idx - ) -end """ From 64b00cc6585bb77fd5cb6455482f2789f814f965 Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Fri, 8 Mar 2019 14:20:40 -0800 Subject: [PATCH 4/4] Fix type signatures for `MachOLoadCmdRef{}` usage --- src/MachO/LoadCmds/DylibCmds.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MachO/LoadCmds/DylibCmds.jl b/src/MachO/LoadCmds/DylibCmds.jl index dadfc16..6e21dd3 100644 --- a/src/MachO/LoadCmds/DylibCmds.jl +++ b/src/MachO/LoadCmds/DylibCmds.jl @@ -121,10 +121,10 @@ Command. """ dylib_compatibilty(cmd::DylibCmd) = cmd.stub.compatibilty -@derefmethod dylib_name(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd} -@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd} -@derefmethod dylib_version(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd} -@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{T}) where {T <: DylibCmd} +@derefmethod dylib_name(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd} +@derefmethod dylib_timestamp(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd} +@derefmethod dylib_version(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd} +@derefmethod dylib_compatibilty(cmd::MachOLoadCmdRef{H, T}) where {H <: MachOHandle} where {T <: DylibCmd} """