Skip to content

Commit 6a66694

Browse files
committed
Fix COFF import table parsing when import table is not at the beginning of the section
1 parent 24babf5 commit 6a66694

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/COFF/COFFDynamicLink.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,38 @@ function import_name(oh::COFFHandle, idata::COFFSectionRef, iid)
2424
return strip(readuntil(oh, '\0'), '\0')
2525
end
2626

27+
function find_section_for_rva(oh::H, rva) where {H <: COFFHandle}
28+
for s in Sections(oh)
29+
if section_address(s) < rva && section_address(s) + section_size(s) > rva
30+
return s
31+
end
32+
end
33+
error("Unable to find section for RVA $(rva)")
34+
end
35+
2736
function COFFDynamicLinks(oh::H) where {H <: COFFHandle}
28-
# Find the import table section
29-
idata = findfirst(Sections(oh), ".idata")
37+
# Start by finding the virtual address of the import table
38+
import_table_rva = oh.opt_header.directories.ImportTable.VirtualAddress
39+
40+
# Next figure out which section that belongs to:
41+
s = find_section_for_rva(oh, import_table_rva)
3042

3143
# We'll load in all the ImageImportDescriptors we can
3244
iids = COFFImageImportDescriptor[]
3345

3446
# Read in ImageImportDescriptors until it's all NULL
35-
seek(oh, section_offset(idata))
47+
seek(oh, (import_table_rva - section_address(s)) + section_offset(s))
3648
while true
3749
iid = unpack(oh, COFFImageImportDescriptor)
38-
if iid.Name == 0 && iid.FirstThunk == 0
50+
if iid.Name == 0 && iid.FirstThunk == 0 && iid.Characteristics == 0 && iid.ForwarderChain == 0 && iid.TimeDateStamp == 0
3951
break
4052
else
4153
push!(iids, iid)
4254
end
4355
end
4456

4557
# Now, jump around and get all the strings
46-
links = [COFFDynamicLink{H}(import_name(oh, idata, iid)) for iid in iids]
58+
links = [COFFDynamicLink{H}(import_name(oh, s, iid)) for iid in iids]
4759
return COFFDynamicLinks(oh, links)
4860
end
4961
DynamicLinks(oh::COFFHandle) = COFFDynamicLinks(oh)

0 commit comments

Comments
 (0)