Skip to content
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

Modern GCC and GDB #624

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Modern GCC and GDB #624

wants to merge 20 commits into from

Conversation

dnasdw
Copy link
Contributor

@dnasdw dnasdw commented Jun 9, 2024

Support compilation with modern GCC and enable source-level debugging using GDB (e.g., in VSCode with mGBA).

Copy link
Contributor

@Eebit Eebit left a comment

Choose a reason for hiding this comment

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

These are some good changes and much appreciated to have some of these things cleaned up! I've left my preliminary thoughts about some of the fixes in the actual decompiled code.

For the changes related to the build system, I think it would be best to have them reviewed by @laqieer, @CT075, @StanHash, or @MokhaLeee (who each have a better grasp on the various aspects of it than me).

@@ -1000,7 +1000,7 @@ void ItemSubMenuEnd(struct MenuProc* menu) {
return;
}

u8 MenuCommand_SelectNo(struct MenuProc* menu, struct MenuItemProc* menuItem) {
u8 MenuCommand_SelectNo(struct MenuProc* menu/*, struct MenuItemProc* menuItem*/) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be good to annotate this with a further comment about why the second argument is commented out -- the comment at the latter call site (L1027-L1028) is now no longer obvious as to what it was pointing out

src/bmmenu.c Outdated
Comment on lines 2282 to 2285
// This is really caused by implicit declaration
isBallista = ((s32 (*)(struct Trap*))IsBallista)(trap); // TODO: FIXME: UB
isBallista = IsBallista(trap);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here - I think it becomes unclear as to what this comment is indicating about the implicit declaration. Would be good to either remove the comment or make a comment about what the bug was, and how/why we fixed it

@@ -989,7 +989,7 @@ void UnitChangeFaction(struct Unit* unit, int faction) {
GetUnit(newUnit->rescue)->rescue = newUnit->index;
}

inline s8 CanUnitCrossTerrain(struct Unit* unit, int terrain) {
inline s32 CanUnitCrossTerrain(struct Unit* unit, int terrain) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it at all possible to avoid this change? I'm a bit worried since it pollutes a lot of places in the code where we now have to cast to s8.

It seems pretty clear that this function is intended to return s8/bool, but there is just the one place (in mapanim_summon.c) where it returns s32 due to implicit declaration...

If it cannot be helped then it's okay, but if there is a less invasive approach I'd prefer we take that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is likely another bug in the official code. Theoretically, there should only be a version of the function with a return type of s8. However, under the previous approach—where the function was not declared in the header file (.h) but individually declared in each source file (.c)—modern gcc would fail to link, unable to find the symbol for this function.

The current modification, while not ideal, ensures both the binary consistency after compilation with agbcc and resolves the linking issue with modern gcc.

AS := $(PREFIX)as$(EXE)
LD := $(PREFIX)ld$(EXE)
OBJCOPY := $(PREFIX)objcopy$(EXE)
STRIP := $(PREFIX)strip$(EXE)

CC1 := tools/agbcc/bin/agbcc$(EXE)
CC1_OLD := tools/agbcc/bin/old_agbcc$(EXE)
# note: the makefile must be set up so MODERNCC is never called
Copy link
Member

Choose a reason for hiding this comment

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

Can you give more details on what this is accomplishing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

What parts of that repo is this intended to imitate? Should I believe that this is a good thing solely because emerald does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is mainly inspired by the Makefile section, which allows for the compilation of different versions of the code. Intermediate files and .o files are generated in separate directories to ensure that switching between different versions during compilation does not cause interference.

@laqieer
Copy link
Contributor

laqieer commented Jun 10, 2024

Thanks for your contribution. Could you please provide these to help us review this PR:

  • resolve the conflicts with target branch first
  • pipeline result to prove it passes and breaks nothing
  • information on your testing environment (platform, OS, toolchain)
  • result of make command to prove it builds with agbcc and matches the vanilla ROM
  • result of command to build with modern gcc such as make modern and prove it works on hardware/emulator
  • result of command to build with debug info such as make modern DINFO=1 and proof of source-level debugging (also gdb version and emulator version)
  • add related instructions to README

dnasdw added 20 commits June 10, 2024 13:12
Fix -Wimplicit-function-declaration
Fix -Werror=builtin-declaration-mismatch
…re incorrectly compiled into the .bss segment and discarded in the latest GCC version.
@dnasdw
Copy link
Contributor Author

dnasdw commented Jun 10, 2024

image
image

I performed a rebase and then made corresponding additions in ldscript_modern.txt based on the changes in ldscript.txt today. I removed two inappropriate comments and added two new comments elsewhere to explain why the changes were made.

There are no conflicts now, and I manually compiled both versions without any issues.
The testing environment includes MSYS2, WSL1 (Ubuntu-22.04), and the operating system is Windows11, with the toolchain being devkitARM.
To compile the modern version of gcc, you can use either make modern or make MODERN=1.
There's no need for parameters like DINFO=1 because the project's compilation parameters already include -g, so the compiled code is debuggable. However, when the -O2 optimization is enabled, the game freezes during the map and story background description, and removing -O2 solves the issue. Even pressing the START button to skip also leads to freezing, likely due to some animations.

As for README and similar files, it's best for your team to write them as I'm not particularly skilled in handling such textual content.

@dnasdw
Copy link
Contributor Author

dnasdw commented Jun 10, 2024

Thanks for your contribution. Could you please provide these to help us review this PR:

  • resolve the conflicts with target branch first
  • pipeline result to prove it passes and breaks nothing
  • information on your testing environment (platform, OS, toolchain)
  • result of make command to prove it builds with agbcc and matches the vanilla ROM
  • result of command to build with modern gcc such as make modern and prove it works on hardware/emulator
  • result of command to build with debug info such as make modern DINFO=1 and proof of source-level debugging (also gdb version and emulator version)
  • add related instructions to README

Forgot to quote this, I mentioned the relevant content in my reply above.

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.

4 participants