Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ version = "0.4.4"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
StructIO = "53d494c1-5632-5724-8f4c-31dff12d585f"


[compat]
Reexport = "0.2, 1.0"
StructIO = "0.3"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"

[targets]
test = ["Test"]
test = ["Test", "Mmap"]
15 changes: 14 additions & 1 deletion src/Abstract/Symbol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export Symbols,

# Export SymtabEntry API
export SymtabEntry,
deref, symbol_name, symbol_value, isundef, isglobal, islocal, isweak
deref, symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_section, symbol_offset

# Export SymbolRef API
export SymbolRef,
Expand Down Expand Up @@ -161,6 +161,19 @@ Return the value of the given symbol
"""
@mustimplement symbol_value(sym::SymtabEntry)

"""
symbol_section(sym::SymtabEntry)
Return the section that this symbol refers to
"""
@mustimplement symbol_section(sym::SymtabEntry)

"""
symbol_offset(sym::SymtabEntry)
Return the offset into the file that this symbol refers to.
"""
@mustimplement symbol_offset(sym::SymtabEntry)


"""
isundef(sym::SymtabEntry)

Expand Down
2 changes: 1 addition & 1 deletion src/COFF/COFF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sect
findfirst, deref, section_name, section_size,
section_offset, section_address, section_number, segment_name, segment_offset,
segment_file_size, segment_memory_size, segment_address, strtab_lookup,
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number, symbol_section, symbol_offset
# Load in imported C #define constants
include("constants.jl")

Expand Down
8 changes: 6 additions & 2 deletions src/COFF/COFFSymbol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ function symbol_name(sym::COFFSymtabEntry)
end

symbol_value(sym::COFFSymtabEntry) = sym.Value
symbol_section(sym::COFFSymtabEntry) = sym.SectionNumber
symbol_type(sym::COFFSymtabEntry) = sym.Type
function isundef(sym::COFFSymtabEntry)
sym.StorageClass in (
Expand Down Expand Up @@ -112,6 +111,7 @@ end
deref(sym::COFFSymbolRef) = sym.entry
Symbols(sym::COFFSymbolRef) = sym.syms
symbol_number(sym::COFFSymbolRef) = sym.idx
symbol_section(sym::COFFSymbolRef) = Sections(handle(sym))[deref(sym).SectionNumber]
@derefmethod symbol_type(sym::COFFSymbolRef)
function symbol_name(sym::COFFSymbolRef)
# COFF Symbols set the first four bytes of the name to zero to signify a
Expand All @@ -127,6 +127,10 @@ function symbol_name(sym::COFFSymbolRef)
return unsafe_string(name)
end

function symbol_offset(sym::COFFSymbolRef)
# Return the offset into the file that this symbol refers to
return symbol_value(sym) + section_offset(symbol_section(sym))
end

# Symbol printing stuff
"""
Expand Down Expand Up @@ -161,7 +165,7 @@ function symbol_type_string(sym::COFFSymtabEntry)
if isempty(type_string)
string("Unknown Symbol Type (0x", string(sym.Type, base=16), ")")
end

return type_string
end
@derefmethod symbol_type_string(s::COFFSymbolRef)
2 changes: 1 addition & 1 deletion src/ELF/ELF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sect
findfirst, deref, section_name, section_size,
section_offset, section_address, section_number, segment_name, segment_offset,
segment_file_size, segment_memory_size, segment_address, strtab_lookup,
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number, symbol_section, symbol_offset
# Load in imported C #define constants
include("constants.jl")

Expand Down
9 changes: 8 additions & 1 deletion src/ELF/ELFSymbol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Symbols(oh::H) where {H <: ELFHandle}
if !isempty(dyn_sections)
return ELFSymbols(first(dyn_sections))
end

dyn_sections = findall(sections, ".dynsym")
if !isempty(dyn_sections)
return ELFSymbols(first(dyn_sections))
Expand Down Expand Up @@ -176,3 +176,10 @@ function symbol_value(sym::ELFSymbolRef)
# Return our ill-gotten goods
return value
end

function symbol_offset(sym::ELFSymbolRef)
addr = symbol_value(sym)
sect = symbol_section(sym)
virtual_offset = section_address(sect) - section_offset(sect)
return addr - virtual_offset
end
2 changes: 1 addition & 1 deletion src/MachO/MachO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sect
findfirst, deref, section_name, section_size,
section_offset, section_address, section_number, segment_name, segment_offset,
segment_file_size, segment_memory_size, segment_address, strtab_lookup,
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number
symbol_name, symbol_value, isundef, isglobal, islocal, isweak, symbol_number, symbol_section, symbol_offset
# Load in imported C #define constants
include("constants.jl")

Expand Down
22 changes: 19 additions & 3 deletions src/MachO/MachOSymbol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
symbol_name(sym::MachOSymtabEntry) = string("strtab@", sym.n_strx)
symbol_value(sym::MachOSymtabEntry) = sym.n_value
symbol_type(sym::MachOSymtabEntry) = sym.n_type
symbol_section(sym::MachOSymtabEntry) = sym.n_sect
symbol_description(sym::MachOSymtabEntry) = sym.n_desc

function isglobal(sym::MachOSymtabEntry)
Expand All @@ -39,7 +38,7 @@
end
function isundef(sym::MachOSymtabEntry)
return (symbol_type(sym) == N_UNDF) ||
(isglobal(sym) && symbol_section(sym) == NO_SECT)
(isglobal(sym) && sym.n_sect == NO_SECT)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the comments below, should this be checking for 0 instead of NO_SECT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that was me being inconsistent. NO_SECT is just 0

end


Expand Down Expand Up @@ -115,6 +114,23 @@
deref(sym::MachOSymbolRef) = sym.entry
Symbols(sym::MachOSymbolRef) = sym.syms
symbol_number(sym::MachOSymbolRef) = sym.idx

function symbol_section(sym::MachOSymbolRef)
# MachO uses 0 to indicate no section, so we use NO_SECT
if deref(sym).n_sect == 0
return NO_SECT

Check warning on line 121 in src/MachO/MachOSymbol.jl

View check run for this annotation

Codecov / codecov/patch

src/MachO/MachOSymbol.jl#L121

Added line #L121 was not covered by tests
end
return Sections(handle(sym))[deref(sym).n_sect]
end

function symbol_name(sym::MachOSymbolRef)
return strtab_lookup(StrTab(Symbols(sym)), sym.entry.n_strx)
end
end

function symbol_offset(sym::MachOSymbolRef)
addr = symbol_value(sym)
sect = symbol_section(sym)
virtual_offset = section_address(sect) - section_offset(sect)
return addr - virtual_offset
end

Binary file added test/libjulias/libjulia.1.10.10.dylib
Binary file not shown.
Binary file added test/libjulias/libjulia.dll
Binary file not shown.
Binary file added test/libjulias/libjulia.so
Binary file not shown.
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,23 @@ end
@test "KERNEL32.dll" in dynamic_links
@test "libstdc++-6.dll" in dynamic_links
end

using Mmap
@testset "Finding dep_libs" begin
function find_dep_libs(file)
obj = only(readmeta(open(file, "r")))
syms = collect(Symbols(obj))
syms_names = symbol_name.(syms)
sym = syms[findfirst(syms_names .== mangle_symbol_name(obj, "dep_libs"))]
offset = symbol_offset(sym)
filem = mmap(file)
data = String(filem[offset: (offset + 255)])
@test contains(data, "libjulia-internal")
@test contains(data, "libjulia-codegen")
@test contains(data, "libopenlibm")
end
for file in readdir("./libjulias")
find_dep_libs(joinpath("./libjulias", file))
end
end

Loading