Skip to content

COFF: Add undefined externals to self.imports - #761

Open
zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-coff-imports
Open

zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-coff-imports

Conversation

@zardus

@zardus zardus commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

COFF undefined externals do not appear in self.imports. The
binaries/tests/x86_64/fauxware.obj fixture reports:

obj.imports: 0 entries

Project._register_object uses only self.imports to choose symbols to
hook. Calls to strcmp or exit therefore go to zero-filled addresses in
the extern object. angr then decodes those addresses as instructions. All ten
undefined externals in this object are reachable from its code.

Root cause

A COFF symbol with section number zero is an undefined external. cle allocates
an extern address for each of these symbols and relocates against it. However,
the symbol is not marked as an import, so self.imports stays empty.

A COFF symbol type of 0x20 means function. A type of 0 means that no type
information is available. GCC-family toolchains emit 0, but cle treats it as
data. cle lists a data import but does not hook it.

Fix

Mark each undefined external symbol as an import. The generic relocation
machinery then handles it. Map an untyped COFF symbol to TYPE_NONE, as cle
does for a relocatable ELF STT_NOTYPE symbol, instead of mapping it to data.

obj.imports: 10 entries
    ?_OptionsStorage@?1??__local_stdio_printf_options@@9@9 TYPE_NONE      -> 0x500000 in ExternObject
    _RTC_CheckStackVars                                  TYPE_FUNCTION  -> 0x500028 in ExternObject
    _RTC_InitBase                                        TYPE_FUNCTION  -> 0x500040 in ExternObject
    _RTC_Shutdown                                        TYPE_FUNCTION  -> 0x500048 in ExternObject
    __imp___acrt_iob_func                                TYPE_NONE      -> 0x500030 in ExternObject
    __imp___stdio_common_vfprintf                        TYPE_NONE      -> 0x500008 in ExternObject
    __imp__open                                          TYPE_NONE      -> 0x500018 in ExternObject
    __imp__read                                          TYPE_NONE      -> 0x500020 in ExternObject
    __imp_exit                                           TYPE_NONE      -> 0x500038 in ExternObject
    strcmp                                               TYPE_FUNCTION  -> 0x500010 in ExternObject

Fixes #746. Evidence: #761 (comment)

session: sharpen

@zardus

zardus commented Aug 17, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Head 7acefe5f44dacbd6afa8db0f2bc26733dcd4502f uses baseline
46a37333f4f59b0facf8774ee743ebc4cc074e9b.

The public reproducer is
angr.Project("binaries/tests/x86_64/fauxware.obj", main_opts={"backend": "COFF"}).
On the baseline, loader.main_object.imports is {}. No extern symbol is
hooked. CFGFast treats the eight zero bytes at 0x500010 (strcmp) and
0x500028 (_RTC_CheckStackVars) as functions. On the head, both addresses
contain ReturnUnconstrained stubs, and CFGFast does not decode either address.

The corpus comparison used 402 COFF objects. The objects were sampled 25 per
(collection, architecture) from a private corpus of 25,051. Each object ran in
a separate child process. Each side ran twice.

baseline head
Objects scored 249 249
CFG blocks 69,939 68,906
Blocks inside a CLE pseudo-object 1,033 0
Functions 15,039 15,082
Extern symbols hooked 644 2,270
Entries in imports 0 1,626
  • Block counts changed for 73 objects. Each object lost exactly the blocks that
    were inside a pseudo-object. No object gained a block.
  • The other 172 scored objects were identical.
  • Repeating either side produced the same block list and function count for all
    402 objects.
  • The same 153 objects failed on all four runs. Of these, 150 ARM or AArch64
    COFF objects raised NotImplementedError: Unsupported machine type. Issue
    Load ARM64 and ARMNT COFF objects #724 addresses that case. The other three raised
    ValueError: Address ... is already backed!.
  • The whole sample took 127 seconds on the baseline and 120 seconds on the
    head. Peak RSS was 292 MB on the baseline and 293 MB on the head.

The sample cannot be shared. Corpus objects are identified by architecture,
container, and digest. The public reproducer above uses a fixture in
angr/binaries.

These figures were measured at
461bb7323ac6f1d0914ac61fcee5189d2a53e947 on baseline
45c6509c753d07f740099035cd41f7f473dc6f31. The branch was later re-keyed to
7acefe5f44dacbd6afa8db0f2bc26733dcd4502f on
46a37333f4f59b0facf8774ee743ebc4cc074e9b.

git range-diff 45c6509c753d07f740099035cd41f7f473dc6f31..461bb7323ac6f1d0914ac61fcee5189d2a53e947 46a37333f4f59b0facf8774ee743ebc4cc074e9b..7acefe5f44dacbd6afa8db0f2bc26733dcd4502f
reports that every commit is unchanged.
git diff 461bb7323ac6f1d0914ac61fcee5189d2a53e947 7acefe5f44dacbd6afa8db0f2bc26733dcd4502f
contains only changes from the baseline advance: 12 files changed, with 353
insertions and 44 deletions. The baseline advance did not touch any file changed
by this PR. The figures therefore still describe the current head.

@angr-bot

Copy link
Copy Markdown
Member

Corpus decompilation diffs can be found at angr/dec-snapshots@master...angr/cle_761

A COFF object names what it needs from elsewhere by giving the symbol section
number zero. The loader already allocates each of those an extern address and
relocates against it, but the symbols never say they are imports, so
Relocation.__init__ does not record them and self.imports stays empty.

angr's Project._register_object iterates obj.imports and nothing else, so a COFF
object gets nothing hooked: a call to an undefined external lands in the extern
object's zero fill and is decoded as instructions, and under symbolic execution
it executes that zero fill instead of a stub.

Say so on the symbol; the generic relocation machinery does the rest, __imp_*
entries included, which are data and so are listed without being hooked. A COFF
symbol's Type field also says 0x20 for a function and 0 for nothing at all,
which is what every GCC-family toolchain emits, and reading 0 as data meant
those imports were listed and then skipped rather than hooked. TYPE_NONE is how
CLE spells "the file did not say", and it is already what a relocatable ELF's
STT_NOTYPE symbols become.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@zardus
zardus force-pushed the feature/fix-cle-coff-imports branch from 22f6feb to 7acefe5 Compare August 26, 2026 22:46
@zardus

zardus commented Aug 28, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

The following reproducer prints the x86-64 COFF fixture's imports. It also
prints each symbol type and resolved extern address.

Reproducer
import logging, os
logging.getLogger("cle").setLevel(logging.CRITICAL)
import cle

BIN = os.environ["BINARIES"]   # a checkout of angr/binaries
T = lambda *p: os.path.join(BIN, "tests", *p)

path = T("x86_64", "fauxware.obj")
ld = cle.Loader(path, auto_load_libs=False)
obj = ld.main_object
print(f"obj.imports: {len(obj.imports)} entries")
for name in sorted(obj.imports):
    reloc = obj.imports[name]
    sym = reloc.resolvedby
    where = ld.find_object_containing(sym.rebased_addr) if sym is not None else None
    kind = reloc.symbol.type.name if reloc.symbol is not None else "?"
    print(f"    {name:<52} {kind:<14} -> {sym.rebased_addr:#x} in {type(where).__name__}"
          if sym is not None else f"    {name:<52} {kind:<14} unresolved")

Before this change, the object's undefined externals do not appear in
self.imports. angr does not hook them.

cle master at 46a37333f4f59b0facf8774ee743ebc4cc074e9b
obj.imports: 0 entries

After this change, all ten undefined externals appear as imports. Each import
resolves into the extern object.

with this change, at 7acefe5f44dacbd6afa8db0f2bc26733dcd4502f
obj.imports: 10 entries
    ?_OptionsStorage@?1??__local_stdio_printf_options@@9@9 TYPE_NONE      -> 0x500000 in ExternObject
    _RTC_CheckStackVars                                  TYPE_FUNCTION  -> 0x500028 in ExternObject
    _RTC_InitBase                                        TYPE_FUNCTION  -> 0x500040 in ExternObject
    _RTC_Shutdown                                        TYPE_FUNCTION  -> 0x500048 in ExternObject
    __imp___acrt_iob_func                                TYPE_NONE      -> 0x500030 in ExternObject
    __imp___stdio_common_vfprintf                        TYPE_NONE      -> 0x500008 in ExternObject
    __imp__open                                          TYPE_NONE      -> 0x500018 in ExternObject
    __imp__read                                          TYPE_NONE      -> 0x500020 in ExternObject
    __imp_exit                                           TYPE_NONE      -> 0x500038 in ExternObject
    strcmp                                               TYPE_FUNCTION  -> 0x500010 in ExternObject

@zardus

zardus commented Aug 29, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This change replaces a # FIXME with import data. It does not change whether
an object loads. A load-depth probe therefore reports no change.

Sample. A seeded permutation selected 12,000 objects uniformly from an
internal corpus of 624,920 compiler- and vendor-produced binaries. Of these,
11,989 were available and were probed. The sample included 468 COFF objects.
On master, 432 of those objects load. The other 36 raise
NotImplementedError: Unsupported machine type. Issue #724 addresses that
case, which is unchanged here.

Method. The comparison loaded all 468 objects with the catalogue's declared
recipe. It used master at eac0e5540516b9199dd6a91933e80dc774ea3eac and
this branch at 7acefe5f44dacbd6afa8db0f2bc26733dcd4502f in one environment.
The comparison inspected each loaded object directly. It also ran
CFGFast(normalize=True, resolve_indirect_jumps=True) to completion for each
object on both commits.

Imports. On master, self.imports is empty for all 432 loaded COFF
objects. On this branch, 35 objects expose 566 imports. One object exposes 83
imports. These 35 objects are exactly the objects that reference undefined
externals. They include 18 x86 and 17 x86-64 relocatable objects. Of these, 33
come from a MinGW/MSYS2 collection and two come from an MSVC collection. No
object without undefined externals gains an import.

Analysis. Across 23 objects, recovered functions increase from 15,457 to
15,479, and CFG nodes increase from 39,225 to 39,229. All 23 objects are among
the 35 that gain imports. No object outside that group changes. Eighteen of the
23 objects gain exactly one function and one block because the extern stub
becomes a function. Four objects lose two to eight blocks because an extern is
no longer scanned as local code. The changes are limited to objects affected by
this fix.

Other COFF changes. Pull requests #724, #764, and #775 also change
coff.py. No pair among these four PRs merges cleanly. Each pair conflicts in
cle/backends/coff.py or tests/test_coff.py. Measured separately, #724
fixes the 36 machine-type failures. Pull request #764 moves a section with no
file bytes away from the image header in 398 of the 432 objects. Pull request
#775 extends the mapped span in all 432 objects.

The sample cannot be shared. Its objects are described by architecture, format,
and OS.

session: sharpen

@zardus zardus changed the title COFF: Expose undefined externals through self.imports COFF: Add undefined externals to self.imports Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

COFF undefined externals never reach self.imports, so angr never hooks them

2 participants