Skip to content

Commit 8f6862c

Browse files
committed
Some rearrangement. Fix #2
1 parent a35f4ce commit 8f6862c

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/ObjFileBase.jl

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
__precompile__()
12
module ObjFileBase
23

34
export ObjectHandle, SectionRef, SymbolRef, debugsections
45

56
export printfield, printentry, printfield_with_color, deref,
67
sectionaddress, sectionoffset, sectionsize, sectionname,
7-
load_strtab, readmeta, StrTab, symname
8+
load_strtab, readmeta, StrTab, symname, Sections, symbolvalue
89

9-
import Base: read, seek, readbytes, position, show
10+
import Base: read, seek, readbytes, position, show, showcompact
1011

1112
########## ObjFileBase.jl - Basic shared functionality for all object files ####
1213
#
@@ -80,6 +81,18 @@ readmeta{T<:ObjectHandle}(io::IO, ::Type{T}) =
8081
@mustimplement seek(oh::ObjectHandle, args...)
8182
@mustimplement position(oh::ObjectHandle)
8283

84+
"""
85+
Determine whether an object file is a relocatable (.o) object file.
86+
"""
87+
function isrelocatable
88+
end
89+
90+
"""
91+
Turn a section name into the object-file specific naming convention.
92+
"""
93+
function mangle_sname
94+
end
95+
8396
##
8497
# These are parameterized on the type of Object Handle. An imaginary Foo
8598
# fileformat might declare:
@@ -91,21 +104,12 @@ readmeta{T<:ObjectHandle}(io::IO, ::Type{T}) =
91104
# end
92105
##
93106

107+
abstract Sections
94108
abstract SectionRef{T<:ObjectHandle}
95109
abstract Section{T<:ObjectHandle}
96110
abstract Relocation{T<:ObjectHandle}
97111
abstract RelocationRef{T<:ObjectHandle}
98112

99-
# The size of the actual data contained in the section. This should exclude any
100-
# padding mandated by the file format e.g. due to alignment rules
101-
@mustimplement sectionsize(section::Section)
102-
103-
# The offset of the section in the file
104-
@mustimplement sectionoffset(section::Section)
105-
106-
# The address of the section in virtual memory
107-
@mustimplement sectionaddress(section::Section)
108-
109113
# The name of the section
110114
@mustimplement sectionname(section::SectionRef)
111115

@@ -121,9 +125,25 @@ abstract SymtabEntry{T<:ObjectHandle}
121125

122126
function symname
123127
end
128+
function symbolvalue
129+
end
124130

131+
symbolnum(x::SymbolRef) = symbolnum(deref(x))
132+
133+
"""
134+
The size of the actual data contained in the section. This should exclude any
135+
padding mandated by the file format e.g. due to alignment rules
136+
"""
125137
sectionsize(x::SectionRef) = sectionsize(deref(x))
138+
139+
"""
140+
The address of the section in virtual memory.
141+
"""
126142
sectionaddress(x::SectionRef) = sectionaddress(deref(x))
143+
144+
"""
145+
The offset of the section in the file.
146+
"""
127147
sectionoffset(x::SectionRef) = sectionoffset(deref(x))
128148

129149
handleT{T}(::Union{Type{SectionRef{T}}, Type{Section{T}}, Type{SymbolRef{T}},
@@ -141,9 +161,6 @@ end
141161

142162
typealias SectionOrRef{T} Union{Section{T},SectionRef{T}}
143163

144-
sectionsize(sect::SectionRef) = sectionsize(deref(sect))
145-
sectionoffset(sect::SectionRef) = sectionoffset(deref(sect))
146-
147164
seek{T<:ObjectHandle,S}(oh::T, section::Section{S}) =
148165
(@assert T <: S; seek(oh,sectionoffset(section)))
149166

@@ -157,6 +174,7 @@ function readbytes{T<:ObjectHandle,S}(oh::T,sec::Section{S})
157174
readbytes(oh, sectionsize(sec))
158175
end
159176
readbytes(sec::SectionRef) = readbytes(handle(sec),deref(sec))
177+
seek(x::SectionRef, off) = seek(handle(x), sectionoffset(x)+off)
160178

161179
typealias Maybe{T} Union{T,Void}
162180

@@ -181,7 +199,10 @@ function DebugSections{T}(oh::T; debug_abbrev = nothing, debug_aranges = nothing
181199
debug_frame = nothing, debug_info = nothing, debug_line = nothing,
182200
debug_macinfo = nothing, debug_pubnames = nothing, debug_loc= nothing,
183201
debug_ranges = nothing, debug_str = nothing, debug_types = nothing)
184-
DebugSections(oh, debug_abbrev, debug_aranges, debug_frame, debug_info,
202+
Sect = Union{map(typeof, [debug_abbrev, debug_aranges, debug_frame, debug_info,
203+
debug_line, debug_loc, debug_macinfo, debug_pubnames, debug_ranges,
204+
debug_str, debug_types])...}
205+
DebugSections{T,Sect}(oh, debug_abbrev, debug_aranges, debug_frame, debug_info,
185206
debug_line, debug_loc, debug_macinfo, debug_pubnames, debug_ranges,
186207
debug_str, debug_types)
187208
end
@@ -310,4 +331,12 @@ read{T<:ObjectHandle}(oh::T, args...) = read(oh.io,args...)
310331
function getSectionLoadAddress
311332
end
312333

334+
immutable LOIByName
335+
addrs::Dict{Symbol, UInt64}
336+
end
337+
getSectionLoadAddress(LOI::LOIByName, x::Union{Symbol, AbstractString}) =
338+
LOI.addrs[symbol(x)]
339+
getSectionLoadAddress(LOI::LOIByName, sec) =
340+
getSectionLoadAddress(LOI, bytestring(sectionname(sec)))
341+
313342
end # module

0 commit comments

Comments
 (0)