Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize ELF Sections API #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions src/ELF/ELFHandle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function readmeta(io::IO, ::Type{H}) where {H <: ELFHandle}
start = position(io)

# Check for magic bytes
magic = [read(io, UInt8) for idx in 1:4]
magic = [read(io, UInt8) for idx in 1:4]
if any(magic .!= elven_magic)
msg = """
Magic Number 0x$(join(string.(magic, base=16),"")) does not match expected ELF
Expand Down Expand Up @@ -78,7 +78,11 @@ mangle_symbol_name(oh::ELFHandle, name::AbstractString) = name
format_string(::Type{H}) where {H <: ELFHandle} = "ELF"

# Section information
section_header_offset(oh::ELFHandle) = header(oh).e_shoff

# The section entry at index 0 is SHN_UNDEF of section type SHT_NULL. It is not
# a real section and may actually contain extension data. Do not return it to
# avoid confusion clients
staticfloat marked this conversation as resolved.
Show resolved Hide resolved
section_header_offset(oh::ELFHandle) = header(oh).e_shoff + section_header_size(oh)
section_header_size(oh::ELFHandle) = header(oh).e_shentsize
function section_header_type(oh::H) where {H <: ELFHandle}
if is64bit(oh)
Expand Down
14 changes: 12 additions & 2 deletions src/ELF/ELFSection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,18 @@ Sections(oh::ELFHandle) = ELFSections(oh)

# Implement Sections API
handle(sections::ELFSections) = sections.handle
lastindex(sections::ELFSections) = header(handle(sections)).e_shnum

function lastindex(sections::ELFSections)
head = header(handle(sections))
head.e_shoff == 0 && return 0
if head.e_shnum == 0
# Large section extension. Number of sections is actually stored in the
# sh_size field of the undef section
seek(handle(sections), head.e_shoff)
return unpack(handle(sections), section_header_type(handle(sections))).sh_size - 1
end
# Exclude the SHT_NULL section
return head.e_shnum - 1
end

"""
ELFSection
Expand Down
13 changes: 10 additions & 3 deletions src/ELF/ELFStrTab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct ELFStrTab{H <: ELFHandle} <: StrTab{H}
""")
@warn(replace(msg, "\n" => " "), key=(handle(section), section_idx))
end

return new(section)
end
end
Expand Down Expand Up @@ -59,10 +59,17 @@ construct an `StrTab` object from. The returned value is an index (e.g. it is
1-based, not zero-based as the value within the ELF object itself).
"""
function section_header_strtab_index(oh::ELFHandle)
return header(oh).e_shstrndx + 1
head = header(oh)
if head.e_shoff != 0 && head.e_shnum == 0
# ELF Large Sections extension. Actual strtab index is the sh_link
# field of the SHN_UNDEF section
seek(oh, head.e_shoff)
return unpack(oh, section_header_type(oh)).sh_link
end
return head.e_shstrndx
end

function strtab_lookup(strtab::ELFStrTab, index)
seek(strtab.section_ref, index)
return strip(readuntil(handle(strtab), '\0'), '\0')
end
end
Loading