Skip to content

Commit 71e4f81

Browse files
committed
Misc useful interfaces
1 parent 1aec417 commit 71e4f81

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

src/ObjFileBase.jl

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module ObjFileBase
22

3-
export ObjectHandle, SectionRef, SymbolRef
3+
export ObjectHandle, SectionRef, SymbolRef, debugsections
4+
5+
export printfield, printentry, printfield_with_color, deref,
6+
sectionaddress, sectionoffset, sectionsize, sectionname
47

58
import Base: read, seek, readbytes, position, show
69

@@ -97,6 +100,12 @@ abstract Section{T<:ObjectHandle}
97100
# The offset of the section in the file
98101
@mustimplement sectionoffset(section::Section)
99102

103+
# The address of the section in virtual memory
104+
@mustimplement sectionaddress(section::Section)
105+
106+
# The name of the section
107+
@mustimplement sectionname(section::SectionRef)
108+
100109
# Retrieving the actual section datastructure
101110
@mustimplement deref(section::SectionRef)
102111

@@ -106,9 +115,17 @@ abstract Section{T<:ObjectHandle}
106115
abstract SymbolRef{T<:ObjectHandle}
107116
abstract SymtabEntry{T<:ObjectHandle}
108117

118+
sectionsize(x::SectionRef) = sectionsize(deref(x))
119+
sectionaddress(x::SectionRef) = sectionaddress(deref(x))
120+
sectionoffset(x::SectionRef) = sectionoffset(deref(x))
121+
109122
handleT{T}(::Union(Type{SectionRef{T}}, Type{Section{T}}, Type{SymbolRef{T}},
110123
Type{SymtabEntry{T}})) = T
111124

125+
abstract StrTab
126+
127+
@mustimplement strtab_lookup(s::StrTab, offset)
128+
112129
################################# Utilities ####################################
113130

114131
typealias SectionOrRef{T} Union(Section{T},SectionRef{T})
@@ -217,7 +234,7 @@ function DebugSections{T}(oh::T; debug_abbrev = nothing, debug_aranges = nothing
217234
debug_ranges = nothing, debug_str = nothing, debug_types = nothing)
218235
DebugSections(oh, debug_abbrev, debug_aranges, debug_frame, debug_info,
219236
debug_line, debug_loc, debug_macinfo, debug_pubnames, debug_ranges,
220-
debug_pubnames, debug_ranges)
237+
debug_str, debug_types)
221238
end
222239

223240
function DebugSections{T<:ObjectHandle}(oh::T, sections::Dict)
@@ -299,15 +316,50 @@ function finddietreebyname(x::DebugSections, name;
299316
cu,DWARF.DIETree)
300317
end
301318

319+
function read(x::DebugSections, ::Type{DWARF.ARTableSet})
320+
read(x.oh, deref(x.debug_aranges), DWARF.ARTableSet)
321+
end
322+
323+
# Utils
324+
# JIT Utils
325+
326+
function is_jit_section(s::Section)
327+
sectionaddress(s) > 0x100000
328+
end
329+
is_jit_section(s::SectionRef) = is_jit_section(deref(s))
330+
331+
@mustimplement replace_sections_from_memory(h::ObjectHandle, buffer)
332+
333+
# Printing utils
334+
function printfield(io::IO,string,fieldlength; align = :right)
335+
(align == :right) && print(io," "^max(fieldlength-length(string),0))
336+
print(io,string)
337+
(align == :left) && print(io," "^max(fieldlength-length(string),0))
338+
end
339+
function printfield_with_color(color,io::IO,string,fieldlength; align = :right)
340+
(align == :right) && print(io," "^max(fieldlength-length(string),0))
341+
print_with_color(color,io,string)
342+
(align == :left) && print(io," "^max(fieldlength-length(string),0))
343+
end
344+
printentry(io::IO,header,values...) = (printfield(io,header,21);println(io," ",values...))
302345

303346
# User facing interfaces
304347

348+
immutable MagicMismatch;
349+
message
350+
end
351+
305352
function readmeta(io::IO)
306353
ts = subtypes(ObjFileBase.ObjectHandle)
354+
pos = position(io)
307355
for T in ts
356+
seek(io,pos)
308357
try
309358
return readmeta(io, T)
310-
catch
359+
catch e
360+
if !isa(e,MagicMismatch)
361+
rethrow(e)
362+
end
311363
end
312364
end
313365
error("""
@@ -321,4 +373,9 @@ end
321373

322374
readmeta(file::String) = readmeta(open(file,"r"))
323375

376+
# Others
377+
378+
function getSectionLoadAddress
379+
end
380+
324381
end # module

0 commit comments

Comments
 (0)