Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/ELF/ELFDynEntry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ function ELFDynEntries(oh::ELFHandle)
# Figure out what kind of DynEntry objects we're going to read in
DT = dyn_entry_type(oh)

# Split-debuginfo files keep the `.dynamic` header, but not its contents
if !isnothing(dyn_section) && !section_has_contents(dyn_section)
# Relocatable objects have no `.dynamic` section, and split-debuginfo
# files keep its header but not its contents
if isnothing(dyn_section) || !section_has_contents(dyn_section)
return ELFDynEntryRef{typeof(oh)}[]
end

Expand Down
32 changes: 23 additions & 9 deletions src/ELF/ELFDynamicLink.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,31 @@ import Base: getindex, show

ELF dynamic linkage table, contains the list dynamic loader commands, see the
`ELFDynEntry` type for an opaque and unhelpful detailing of these commands.
`section_ref` is `nothing` for objects without a `.dynamic` section (e.g.
relocatable `.o` files), in which case `links` is empty.
"""
struct ELFDynamicLinks{H <: ELFHandle} <: DynamicLinks{H}
section_ref::SectionRef{H}
section_ref::Union{Nothing,SectionRef{H}}
links::Vector
handle::H
end
ELFDynamicLinks(section_ref::SectionRef, links::Vector) =
ELFDynamicLinks(section_ref, links, handle(section_ref))

"""
DynamicLinks(oh::ELFHandle)

Return the `ELFDynamicLinks` object that is contained within the first
`.dynamic` section within the ELF object.
`.dynamic` section within the ELF object. If there is no `.dynamic` section,
the result is empty.
"""
function DynamicLinks(oh::ELFHandle)
sections = Sections(oh)
dyn_section = findfirst(sections, ".dynamic")

# Create half-initialized `DynamicLinks` object, so we have it around to
# pass to the `DynamicLink()` constructor below
dls = ELFDynamicLinks(dyn_section, DynamicLink[])
dls = ELFDynamicLinks(dyn_section, DynamicLink[], oh)

# Find all our DT_NEEDED DynEntries
des = ELFDynEntries(oh, [DT_NEEDED])
Expand All @@ -44,7 +50,7 @@ function DynamicLinks(oh::ELFHandle)
return dls
end
Section(dls::ELFDynamicLinks) = dls.section_ref
handle(dls::ELFDynamicLinks) = handle(Section(dls))
handle(dls::ELFDynamicLinks) = dls.handle
lastindex(dls::ELFDynamicLinks) = lastindex(dls.links)
getindex(dls::ELFDynamicLinks, idx) = getindex(dls.links, idx)

Expand All @@ -55,6 +61,9 @@ Given an ELF `DynamicLinks` container, construct an `ELFStrTab` that refers to
the associated dynamic string table.
"""
function StrTab(dls::ELFDynamicLinks{H}) where {H <: ELFHandle}
if Section(dls) === nothing
error("$(path(handle(dls))) has no .dynamic section")
end
# The `sh_link` field in a ".dynamic" section always points to the
# dynamic string table (usually called ".dynstr")
section_idx = deref(Section(dls)).sh_link+1
Expand Down Expand Up @@ -85,18 +94,23 @@ end
"""
ELFRPath

Stores the RPath entries from an ELF object
Stores the RPath entries from an ELF object. `section_ref` is `nothing` for
objects without a `.dynamic` section, in which case `rpath` is empty.
"""
struct ELFRPath{H <: ELFHandle} <: RPath{H}
section_ref::SectionRef{H}
section_ref::Union{Nothing,SectionRef{H}}
rpath::Vector{<:AbstractString}
handle::H
end
ELFRPath(section_ref::SectionRef, rpath::Vector{<:AbstractString}) =
ELFRPath(section_ref, rpath, handle(section_ref))

"""
RPath(oh::ELFHandle)

Return the `ELFRPath` object that is contained within the first `.dynamic`
section within the ELF object.
section within the ELF object. If there is no `.dynamic` section, the result
is empty.
"""
function RPath(oh::ELFHandle)
sections = Sections(oh)
Expand All @@ -115,9 +129,9 @@ function RPath(oh::ELFHandle)
end

# Return our RPath object
return ELFRPath(dyn_section, paths)
return ELFRPath(dyn_section, paths, oh)
end

Section(rpath::ELFRPath) = rpath.section_ref
handle(rpath::ELFRPath) = handle(Section(rpath))
handle(rpath::ELFRPath) = rpath.handle
rpaths(rpath::ELFRPath) = rpath.rpath
9 changes: 9 additions & 0 deletions test/elfobj/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# Regenerates the ELF relocatable object fixture in this directory. Requires
# `gcc`. `foo.o` is an `ET_REL` object: it has `.symtab`, `.strtab` and
# relocation sections, but no program headers and none of the dynamic-linking
# sections (`.dynamic`, `.dynsym`, `.dynstr`, `.gnu.version*`).
set -euo pipefail
cd "$(dirname "$0")"
echo 'extern int bar(void); int foo(void) { return bar() + 1; }' | \
gcc -O2 -fPIC -c -x c -o foo.o -
Binary file added test/elfobj/foo.o
Binary file not shown.
36 changes: 36 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,42 @@ end
end
end

# A relocatable object has no `.dynamic` section (and no other dynamic-linking
# sections or program headers), so its dynamic linkage info is empty.
@testset "ELF relocatable object" begin
using ObjectFile.ELF

readmeta("./elfobj/foo.o") do ohs
oh = only(ohs)
@test oh isa ELFHandle
@test isrelocatable(oh)
@test !isdynamic(oh)
@test findfirst(Sections(oh), ".dynamic") === nothing

@test isempty(ELF.ELFDynEntries(oh))
dls = DynamicLinks(oh)
@test isempty(dls)
@test isempty(path.(dls))
@test Section(dls) === nothing
@test handle(dls) === oh
@test_throws ErrorException StrTab(dls)
rpath = RPath(oh)
@test isempty(rpaths(rpath))
@test isempty(canonical_rpaths(rpath))
@test Section(rpath) === nothing
@test handle(rpath) === oh
@test isempty(find_libraries(oh))
@test sprint(show, dls) == "ELF DynamicLinks"
@test sprint(show, rpath) == "ELF RPath"

@test isempty(ELFVersionData(oh))
@test isempty(ELFVersionNeededData(oh))
@test isempty(Segments(oh))
@test "foo" in symbol_name.(Symbols(oh))
@test "bar" in symbol_name.(Symbols(oh))
end
end

# Ensure that these tricksy win32 files work
@testset "git win32 problems" begin
# Test that 6a66694a8dd5ca85bd96fe6236f21d5b183e7de6 fix worked
Expand Down
Loading