Skip to content

Conversation

@fjtrujy
Copy link
Member

@fjtrujy fjtrujy commented Jan 4, 2026

  • Fix gprof profiler to work correctly with PRX (relocatable) modules

    • Convert PC addresses to link-time format in __mcount for proper symbol resolution
    • Maintain backward compatibility with regular (non-PRX) executables

    Problem

    When using gprof with PRX modules loaded via psplink or PPSSPP, the profiler
    produced empty output. PRX modules are relocated at load time, so runtime PC
    addresses don't match the link-time addresses stored in ELF symbols.

    Solution

    Convert addresses to link-time format immediately in __mcount:

    frompc = (frompc & 0x0FFFFFFF) - gp.lowpc;  // link-time = runtime - base
    selfpc = (selfpc & 0x0FFFFFFF) - gp.lowpc;
    
    Since PSP text sections start at address 0 at link-time, this simple
    subtraction gives us the correct addresses for symbol matching.

  PRX modules are relocated at load time, causing runtime PC addresses
  to differ from link-time addresses in the ELF. This made gprof output
  empty because:
  1. Range checks failed (runtime PCs outside link-time range)
  2. Addresses in gmon.out didn't match ELF symbols

  Fix by converting addresses to link-time in __mcount immediately:
  - Runtime address - &_ftext = link-time address (since _ftext = 0)
  - Store link-time addresses in arcs and samples
  - Use link-time range for bounds checking

  This approach requires no new kernel API imports, ensuring
  compatibility with real hardware via psplink.
Copy link
Member

@sharkwouter sharkwouter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, thanks!

@sharkwouter sharkwouter merged commit 3dbfc57 into pspdev:master Jan 6, 2026
1 check passed
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.

2 participants