Skip to content

PE: Build base relocations for every resolved architecture - #755

Open
zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-pe-reloc-arches
Open

zardus wants to merge 1 commit into
masterfrom
feature/fix-cle-pe-reloc-arches

Conversation

@zardus

@zardus zardus commented Aug 17, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

CLE discards the full .reloc directory for ARM64, ARMNT, MIPS, and RISC-V PE files. For example, loading tests/aarch64/windows/pe_reloc_arm64.exe at its linked base of 0x140000000 keeps only the padding entry. Rebasing the image does not move any pointer:

=== aarch64: tests/aarch64/windows/pe_reloc_arm64.exe
  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1)]
  total relocs: 1
    +0x2000  linked=0x1400011b8    rebased=0x1400011b8    delta=0x0

The preferred base hides this problem. At any other base, each absolute pointer keeps its link-time value. This also affects an image whose ImageBase is 0.

Cause

PE._make_reloc looks up the table by self.arch.name. However, ALL_RELOCATIONS used names that archinfo does not produce. It also had no AArch64 entry:

ALL_RELOCATIONS = {
    "AMD64": relocation_table_generic,
    "arm": relocation_table_generic | relocation_table_arm,
    "X86": relocation_table_generic,
    "mips": relocation_table_generic | relocation_table_mips,
    "RISCV": relocation_table_generic | relocation_table_riscv,
}

archinfo uses AARCH64, ARMEL, ARMHF, ARMCortexM, MIPS32, and RISCV64. As a result, get_relocation returned None for every type. The loader logged Unknown reloc 10 on AARCH64.

Changing the keys alone is not enough. PEReloc.value used self.resolvedby.rebased_addr, but a base relocation has no resolved symbol. A recognized type could still be dropped without a warning.

Fix

This change keys the table by arch.name. It adds a value for IMAGE_REL_BASED_THUMB_MOV32. The value reads and rewrites the imm4:i:imm3:imm8 halves of the MOVW.W/MOVT.W pair.

The base class now returns None for a symbol-less relocation and warns once per type. A recognized but unimplemented fixup is now reported. IMAGE_REL_BASED_ARM64_MOV32A and MOV32T remain unimplemented and now report that state.

After a rebase by 0x10000, every fixup moves:

  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1), ('IMAGE_REL_BASED_HIGHLOW', 3), ('IMAGE_REL_BASED_THUMB_MOV32', 2)]
    +0x2000  linked=0x40113f       rebased=0x41113f       delta=0x10000
    MOVW/MOVT +0x104c  linked=0x402000     rebased=0x412000     delta=0x10000

Fixes #752. The fixtures require angr/binaries#183.

Details: #755 (comment)

sync: angr/binaries#183

session: sharpen

@zardus

zardus commented Aug 17, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This report compares head 04d58745ecc829d52624c44bc96f397a55abada2 with baseline 0e77ade3c39a3cee05f65051e57955675e1ac21b.

Corpus comparison

The corpus contains 1,557 PE files. Each side loads one file per process. The expected values come from each file's base relocation directory, not from CLE. The counts exclude IMAGE_REL_BASED_ABSOLUTE padding entries.

machine objects base relocations in the files built on baseline built on head
I386 458 1,188,958 1,188,930 1,188,930
AMD64 469 213,518 213,518 213,518
ARM64 306 221,282 0 221,282
ARMNT 176 138,748 0 138,718
RISCV64 58 13,431 0 13,271
THUMB 43 88,722 0 88,722
AMD64^linux 37 138,411 0 0
LOONGARCH64 10 441 0 0

This change cannot help the last two rows. The backend does not resolve the 37 AMD64^linux files without #757. archinfo has no architecture for the 10 LOONGARCH64 files.

Each file was loaded at its nominal base and at 0x10000 above that base. Each fixup site was compared with the file value plus the loader's shift.

fixup sites, over the 1,520 objects both sides load Baseline Head
HIGHLOW carrying the shift 1,188,911 1,293,526
HIGHLOW left unrelocated 193,365 28
DIR64 carrying the shift 186,496 363,769
DIR64 left unrelocated 234,713 160
THUMB_MOV32 carrying the shift 0 34,103
THUMB_MOV32 left unrelocated 34,133 30
carrying a value that is neither 0 0

The head adds base relocations to 388 files. Of those files, 242 load at their preferred base. Their fixups rewrite the same bytes, so their loaded memory matches the baseline. The other 146 files have an ImageBase of 0 and map at 0x400000. Their bytes change. All 345,830 fixup sites in those files carry the exact shift at both load addresses.

The head leaves 218 sites unrelocated. They are in 29 files whose .reloc blocks pefile rejects because the page RVA is outside the image or the block is empty. CLE never receives those entries. The 28 I386 sites in this group remain unchanged on both sides.

Loaded memory, relocations, symbols, entry points, and function hints remain identical for all 927 x86 and x86-64 files.

Both sides have the same 11 load errors. Ten are ArchNotFound errors for LOONGARCH64 files that pefile resolves but archinfo does not. One is an IndexError in _meta_iat that #732 fixes. The change adds no error, timeout, or memory regression.

All 34,133 Thumb MOVW/MOVT sites were decoded independently. Every site contains a MOVW.W followed by a MOVT.W. Every immediate points inside the linked image range. Re-encoding reproduces the original bytes.

CFG changes

Applying a base relocation changes bytes only when the image moves. The 242 files that load at their preferred base cannot change a CFG. The files that CLE maps at 0x400000 because their ImageBase is 0 can change a CFG.

CFGFast used its default settings and one process per file.

Baseline Head
objects 132 132
objects whose block set changed — 58
blocks 431,052 435,819
functions 32,788 36,229
bytes covered 5,796,484 6,023,938
errors 0 0
timeouts 0 0

Fifteen files cover 252,544 more bytes. Forty-two files cover 25,090 fewer bytes. The largest gains are these THUMB files:

  • 91af04b8a587edaf — The sample cannot be shared. CFGFast covers 70,026 more bytes.
  • b3dcdbe21277b1df — The sample cannot be shared. CFGFast covers 61,080 more bytes.
  • 11ca64ae318f51e9 — The sample cannot be shared. CFGFast covers 37,574 more bytes.

The lower coverage is expected. Before the fix, pointers still named the old base. CFGFast followed those pointers into data and decoded it. The two largest reductions are these THUMB files:

  • ac5738b8add1a987 — The sample cannot be shared. The head no longer recovers a 0123456789ABCDEF table followed by zeros.
  • 7d77b3ba0e815b96 — The sample cannot be shared. Of the 4,592 bytes that disappear, 4,562 are the image's own string literals, including <null time>, Block number, and Failed to cr....

Two independent CFGFast runs on the same revision agreed for all 247 files. They produced the same block addresses, function addresses, block counts, and byte counts.

Limits

The corpus is private, so this report uses machine types and counts instead of paths. The fixtures in angr/binaries#183 reproduce both architectures publicly.

IMAGE_REL_BASED_ARM_MOV32 and the MIPS and RISC-V type tables are now reachable but remain unimplemented. They warn once and leave the fixup at its linked value. No file in the corpus uses them.

The corpus figures were first measured at 2161f5b74330b5333a63c5ffb0211f4683cfd1db on baseline 45c6509c753d07f740099035cd41f7f473dc6f31. The branch was replayed unchanged at 988c00868f584e716c44aa66fed8408f069e5c3e on 46a37333f4f59b0facf8774ee743ebc4cc074e9b, then at the current head on 0e77ade3c39a3cee05f65051e57955675e1ac21b. Both range comparisons show the same commits. The branch's added and removed lines are byte-identical. Master did not change any file touched by this patch. These figures therefore describe the current head.

@angr-bot

Copy link
Copy Markdown
Member

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

@zardus
zardus force-pushed the feature/fix-cle-pe-reloc-arches branch 2 times, most recently from 0775fe8 to 58d16f7 Compare August 22, 2026 15:23
@zardus
zardus force-pushed the feature/fix-cle-pe-reloc-arches branch from 58d16f7 to 988c008 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

This report compares the ARM64 and ARMNT PE fixtures from angr/binaries#183. It loads each image at its linked base and at 0x10000 above that base. It uses tests/x86_64/windows/sioctl.sys as an unchanged control.

linked is the pointer at the fixup site when the image loads at its ImageBase. rebased is the pointer at the same site after cle.Loader(..., main_opts={"base_addr": ...}).

Before

At the merge base, CLE drops every base relocation in both ARM images. Rebasing leaves each pointer unchanged with delta=0x0.

cle at the merge base, 46a3733
Unknown reloc 10 on AARCH64
Unknown reloc 7 on ARMEL
Unknown reloc 3 on ARMEL
cle: <cle at the merge base>/cle/__init__.py
ALL_RELOCATIONS keys: ['AMD64', 'RISCV', 'X86', 'arm', 'mips']
=== aarch64: tests/aarch64/windows/pe_reloc_arm64.exe
  arch.name: AARCH64  linked_base=0x140000000
  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1)]
  total relocs: 1
    +0x2000  linked=0x1400011b8    rebased=0x1400011b8    delta=0x0
    +0x2008  linked=0x1400011c0    rebased=0x1400011c0    delta=0x0
    +0x2010  linked=0x1400011cc    rebased=0x1400011cc    delta=0x0
=== armnt: tests/armel/windows/pe_reloc_armnt.exe
  arch.name: ARMEL  linked_base=0x400000
  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1)]
  total relocs: 1
    +0x2000  linked=0x40113f       rebased=0x40113f       delta=0x0
    +0x2004  linked=0x40114b       rebased=0x40114b       delta=0x0
    +0x2008  linked=0x40115b       rebased=0x40115b       delta=0x0
    MOVW/MOVT +0x104c  linked=0x402000     rebased=0x402000     delta=0x0
    MOVW/MOVT +0x1130  linked=0x403000     rebased=0x403000     delta=0x0
=== amd64 control: tests/x86_64/windows/sioctl.sys
  arch.name: AMD64 reloc classes: [('DllImport', 15), ('IMAGE_REL_BASED_ABSOLUTE', 1), ('IMAGE_REL_BASED_DIR64', 13)]

After

With this change, CLE recognizes and applies each type. Every fixup moves by the rebase delta.

with this change, 988c008
cle: <cle with this change>/cle/__init__.py
ALL_RELOCATIONS keys: ['AARCH64', 'AMD64', 'ARMCortexM', 'ARMEL', 'ARMHF', 'MIPS32', 'PPC32', 'RISCV64', 'X86']
=== aarch64: tests/aarch64/windows/pe_reloc_arm64.exe
  arch.name: AARCH64  linked_base=0x140000000
  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1), ('IMAGE_REL_BASED_DIR64', 3)]
  total relocs: 4
    +0x2000  linked=0x1400011b8    rebased=0x1400111b8    delta=0x10000
    +0x2008  linked=0x1400011c0    rebased=0x1400111c0    delta=0x10000
    +0x2010  linked=0x1400011cc    rebased=0x1400111cc    delta=0x10000
=== armnt: tests/armel/windows/pe_reloc_armnt.exe
  arch.name: ARMEL  linked_base=0x400000
  reloc classes: [('IMAGE_REL_BASED_ABSOLUTE', 1), ('IMAGE_REL_BASED_HIGHLOW', 3), ('IMAGE_REL_BASED_THUMB_MOV32', 2)]
  total relocs: 6
    +0x2000  linked=0x40113f       rebased=0x41113f       delta=0x10000
    +0x2004  linked=0x40114b       rebased=0x41114b       delta=0x10000
    +0x2008  linked=0x40115b       rebased=0x41115b       delta=0x10000
    MOVW/MOVT +0x104c  linked=0x402000     rebased=0x412000     delta=0x10000
    MOVW/MOVT +0x1130  linked=0x403000     rebased=0x413000     delta=0x10000
=== amd64 control: tests/x86_64/windows/sioctl.sys
  arch.name: AMD64 reloc classes: [('DllImport', 15), ('IMAGE_REL_BASED_ABSOLUTE', 1), ('IMAGE_REL_BASED_DIR64', 13)]

ALL_RELOCATIONS was keyed on "arm", "mips" and "RISCV", which archinfo does not
produce, and had no AARCH64 entry, so get_relocation returned None for every type
on those architectures and the whole .reloc directory was discarded. Key the table
on arch.name instead.

That alone is not safe. PEReloc.value falls through to self.resolvedby.rebased_addr,
which is None for a base relocation, so making the ARM table reachable turns every
IMAGE_REL_BASED_THUMB_MOV32 into an AttributeError during loading. Implement that
relocation, and let the base class skip a recognised but unimplemented type with a
warning rather than raising.
@zardus
zardus force-pushed the feature/fix-cle-pe-reloc-arches branch from 988c008 to 04d5874 Compare September 4, 2026 15:45
@zardus zardus changed the title PE: Build base relocations on every architecture the backend resolves PE: Build base relocations for every resolved architecture 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.

PE base relocations are dropped on every architecture but x86 and x86-64: the relocation tables are keyed on names archinfo does not produce

2 participants