UsernameFodder / pmdsky-debug

Debug info for reverse engineering PMD: Explorers of Sky

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Include global variables in headers

tech-ticks opened this issue · comments

Is your feature request related to a problem? Please describe.
The type of global variables is not always clear (see e.g. FRACTIONAL_TURN_SEQUENCE in overlay29, which is an array of 16-bit integers according to Ghidra, but the description doesn't mention that).

Describe the solution you'd like
Adding global variables to the headers would solve two problems/inconveniences:

  • The type of all globals would be clearly defined
  • Global variables wouldn't have to be added manually to C injection projects with extern struct ... and show up in auto-complete

Describe alternatives you've considered
Adding types to the globals in YAML files in a structured way would also be helpful (and a header file could be generated from them), but I don't really see a benefit over directly adding the fields to the headers.

This seems like a pretty natural extension to the function signatures. I'll just need to double-check that it doesn't cause any issues with Ghidra imports (it shouldn't...)

Update: Ghidra has no issues with global declarations.

@tech-ticks To clarify, you wanted extern global declarations in the headers (extern int foo;), not linkable global definitions (int foo;), right?

I think it shouldn't matter, but I'm not really sure? A linker script maps each symbol to its location, which has been working for functions that aren't extern. I'd have to test whether linkable global definitions behave fine, I've already used extern declarations on structs.

In that case, can you test that #73 works for you? Since the globals are technically already defined in the EoS binaries (or by the linker script in c-of-time's case), extern declarations are the "proper" thing to do, so ideally we would stick with doing that.

Function prototypes (int foo(int x);) and extern variable declarations are analogous and behave the same way: they declare symbols so the compiler knows they exist and doesn't complain when you reference them in your own code, but they don't actually act as definitions to make them "real objects". This is opposed to function definitions (int foo(int x) { return x; }) and variable definitions, which are also analogous in that they define a new symbol for the linker.

Typically prototypes/declarations are the only things you want in header files, because there's no problem with duplicating them. I don't think it's a problem for c-of-time, but generally including definitions in header files can lead to multiple-definition linker errors if you use the headers to build multiple different object files, then try to link them together.

It works fine, thanks. And yeah, extern declarations are what we want here, thanks for clarifying.