Skip to content

Support gp relocations for Psy-Q AS 2.5x #1888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2025

Conversation

galaxyhaxz
Copy link
Contributor

@galaxyhaxz galaxyhaxz commented Mar 12, 2025

I'm not sure what version of object files the current tool targets (could be newer or older) but at least with 2.5x of the Psy-Q assembler (1996-1997), it generates gp relocations not supported. They are code 12 for big-endian and code 30 for little-endian. These relocations are handled slightly differently from the other GP type, in this case sdata uses an absolute value whereas sbss goes by symbol name, both reference the section start point.

This adds support for them so that the tool can convert their relevant objects for use on decomp.me. Also note that I'm not a C++ wizard so the code may not be the best, but it works in my testing.

Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

Walkthrough

This change introduces two new relocation types, GPREL16_BE and GPREL16_LE, into the Psyq object parser. The parser’s logic is updated in its parsing, display, and ELF generation methods to correctly handle these types, including their mappings to ELF MIPS relocation types. These modifications ensure that the new relocation types are recognized during file parsing and applied properly during ELF generation.

Changes

File Summary
tools/.../psyq-obj-parser.cc - Added enum values GPREL16_BE and GPREL16_LE to the PsyqRelocType enumeration.
- Updated the parse method to handle these new relocation types.
- Modified displayRelocs to include string representations for the new types.
- Enhanced generateElf logic for ELF output and updated type mappings.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Parser
    participant Relocation
    participant Display

    User->>Parser: Load object file
    Parser->>Parser: Identify relocation type (including GPREL16_BE/LE)
    Parser->>Relocation: Pass relocation info for ELF generation
    Relocation->>Relocation: Adjust offset & write bytes based on type
    Parser->>Display: Format relocation strings for output
    Display-->>User: Present parsed relocation details
Loading

Poem

Hopping through lines of code so bright,
Two fresh relocation types take flight.
GPREL16_BE and LE join the parade,
In parsing and ELF, their marks are laid.
I, the rabbit, celebrate this byte-filled delight!


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2b06c4c and 3f77d56.

📒 Files selected for processing (1)
  • tools/psyq-obj-parser/psyq-obj-parser.cc (8 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeScene Cloud Delta Analysis (main)
tools/psyq-obj-parser/psyq-obj-parser.cc

[warning] 344-351: ❌ Getting worse: Complex Method
PsyqLnkFile::parse increases in cyclomatic complexity from 55 to 57, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.


[warning] 1198-1463: ❌ Getting worse: Complex Method
PsyqLnkFile::Relocation::generateElf increases in cyclomatic complexity from 55 to 62, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.


[warning] 1198-1198: ❌ New issue: Complex Conditional
PsyqLnkFile::Relocation::generateElf has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.


[warning] 1198-1463: ❌ Getting worse: Complex Method
PsyqLnkFile::Relocation::generateElf increases in cyclomatic complexity from 55 to 62, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (7)
tools/psyq-obj-parser/psyq-obj-parser.cc (7)

73-75: Addition of new relocation types for Psy-Q AS 2.5x

The new enum values GPREL16_BE and GPREL16_LE have been correctly added to the PsyqRelocType enum with the appropriate values (12 and 30) as mentioned in the PR description. These values match the relocation codes generated by the Psy-Q assembler version 2.5x.


344-351: Handler for new relocation types in the parse method

The appropriate case statements have been added to handle the new relocation types during parsing. The implementation follows the pattern established for other relocation types in the codebase.

🧰 Tools
🪛 GitHub Check: CodeScene Cloud Delta Analysis (main)

[warning] 344-351: ❌ Getting worse: Complex Method
PsyqLnkFile::parse increases in cyclomatic complexity from 55 to 57, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.


779-780: Updated relocation type string mapping

The string mappings for the new relocation types have been correctly added to the typeStr map, ensuring proper display in debug/verbose output.


1008-1009: Updated ELF relocation type mapping

The new relocation types have been properly mapped to the appropriate ELF MIPS relocation type (R_MIPS_GPREL16).


1198-1200: Updated offset adjustment for big-endian relocations

The condition has been extended to include GPREL16_BE in the list of big-endian relocation types that require offset adjustment. This is consistent with the handling of other big-endian relocation types.

🧰 Tools
🪛 GitHub Check: CodeScene Cloud Delta Analysis (main)

[warning] 1198-1463: ❌ Getting worse: Complex Method
PsyqLnkFile::Relocation::generateElf increases in cyclomatic complexity from 55 to 62, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.


[warning] 1198-1198: ❌ New issue: Complex Conditional
PsyqLnkFile::Relocation::generateElf has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.


1246-1257: Implementation of new relocation handlers in generateElf

The implementation correctly handles both little-endian and big-endian GP relocations:

  • For GPREL16_LE, bytes are written in little-endian order (least significant byte first)
  • For GPREL16_BE, bytes are written in big-endian order (most significant byte first)

The code follows the established pattern for other relocation types and properly handles the 16-bit offset masking.


1454-1463: Handling for SECTION_START in SUB expression

The code adds support for subtractions involving SECTION_START, which is needed for GP relocations that reference the section start point as mentioned in the PR description.

🧰 Tools
🪛 GitHub Check: CodeScene Cloud Delta Analysis (main)

[warning] 1198-1463: ❌ Getting worse: Complex Method
PsyqLnkFile::Relocation::generateElf increases in cyclomatic complexity from 55 to 62, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@nicolasnoble nicolasnoble left a comment

Choose a reason for hiding this comment

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

Looks good to me :)

@nicolasnoble nicolasnoble enabled auto-merge March 12, 2025 02:54
@nicolasnoble nicolasnoble merged commit 689a296 into grumpycoders:main Mar 12, 2025
21 of 22 checks passed
@galaxyhaxz galaxyhaxz deleted the support_old_gp branch March 12, 2025 05:44
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