pe_utils
flavor.psp.format_2025.pe_utils
¶
Windows PE Executable Utilities.
Provides utilities for manipulating Windows PE (Portable Executable) files to ensure compatibility with PSPF format when data is appended after the executable.
Functions¶
expand_dos_stub
¶
Expand the DOS stub of a PE executable to match Rust/MSVC binary size.
This fixes Windows PE loader rejection of Go binaries when PSPF data is appended. The DOS stub is expanded from 128 bytes (0x80) to 240 bytes (0xF0) to match Rust binaries.
Process: 1. Extract MZ header (first 64 bytes) 2. Extract DOS stub code (bytes 64 to current PE offset) 3. Extract PE header and remainder 4. Insert padding to expand stub to target size 5. Update e_lfanew pointer to new PE offset
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Original PE executable data |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Modified PE executable with expanded DOS stub |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is not a valid PE executable |
Source code in flavor/psp/format_2025/pe_utils.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
get_launcher_type
¶
Detect launcher type from PE characteristics.
Go and Rust compilers produce PE files with different characteristics: - Go: Minimal DOS stub (PE offset 0x80 / 128 bytes) - Rust: Larger DOS stub (PE offset 0xE8 / 232 bytes or more)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
launcher_data
|
bytes
|
Launcher binary data |
required |
Returns:
| Type | Description |
|---|---|
str
|
"go", "rust", or "unknown" |
Source code in flavor/psp/format_2025/pe_utils.py
get_pe_header_offset
¶
Read the PE header offset from the DOS header.
The offset is stored at position 0x3C (e_lfanew field) as a 4-byte little-endian integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
PE executable data |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
PE header offset, or None if invalid |
Source code in flavor/psp/format_2025/pe_utils.py
is_pe_executable
¶
needs_dos_stub_expansion
¶
Check if a PE executable needs DOS stub expansion.
Go binaries use minimal DOS stub (128 bytes / 0x80) which is incompatible with Windows PE loader when PSPF data is appended. This function detects such binaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
PE executable data |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if DOS stub needs expansion (Go binary with 0x80 stub) |
Source code in flavor/psp/format_2025/pe_utils.py
process_launcher_for_pspf
¶
Process launcher binary for PSPF embedding compatibility.
This is the main entry point for PE manipulation. It uses a hybrid approach: - Go launchers: Use PE overlay (no modifications, PSPF appended after sections) - Rust launchers: Use DOS stub expansion (PSPF at fixed 0xF0 offset)
Phase 29: Go binaries are fundamentally incompatible with DOS stub expansion due to their PE structure (15 sections, unusual section names, missing data directories). The PE overlay approach is the industry standard and preserves 100% PE structure integrity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
launcher_data
|
bytes
|
Original launcher binary |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Processed launcher binary (expanded if Rust, unchanged if Go/Unix) |