Skip to content

Fix #8908 (Program export does not export globals' data)#9293

Open
andrey-starodubtsev wants to merge 3 commits into
NationalSecurityAgency:masterfrom
andrey-starodubtsev:GP-8908_export_global_variables
Open

Fix #8908 (Program export does not export globals' data)#9293
andrey-starodubtsev wants to merge 3 commits into
NationalSecurityAgency:masterfrom
andrey-starodubtsev:GP-8908_export_global_variables

Conversation

@andrey-starodubtsev

Copy link
Copy Markdown

This MR addresses #8908 (Program export does not export globals' data).

A new "Emit Global Data Initializers" exporter option was added (enabled by default). When enabled, it exports initializers for global variables.

Also, it fixes number of issues discovered when the globals with initialization are exported in C source:

  • scalars were formatted with Ghidra's h-suffix notation (FFFFFFFFh); 0x prefix is used for C sources now.
  • ASCII character was appended to the hex value (e.g. 78h x) for char/undefined
  • globals without initialization were exported as type var = ??; instead of plain type var;
  • global arrays used Java-style type declarations (i.e. byte[32] var; instead of byte var[32])
  • some of the globals contained incorrect symbols in their names (e.g. PTR_LAB_0001576c+1_000e0e3c)

Also, globals were not sorted by address which made their analysis more cubersome

This MR contains three commits which can be squashed after the review (or some functionality can be split to separate MR if necessary).

andrey-starodubtsev and others added 3 commits June 20, 2026 10:12
Add 'Emit Global Data Initializers' option to the C/C++ exporter that,
when enabled (default: on), emits global variable definitions with C
initializers instead of bare declarations.

For each referenced global, the exporter now looks up the typed Data at
the symbol's memory address and renders it as a C initializer:
- Scalars (int, float, char, enum, undefined bytes): use Ghidra's
  default value representation
- Strings: use the string representation
- Composites (arrays, structs, unions): recurse into components,
  producing braced initializers like {1, 2, 3} or {0x1, 0x2}
- Pointers and function definitions: skip initializer (fall back to
  plain declaration) since their address is not portable C syntax

Resolves: NationalSecurityAgency#8908

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Four bugs are fixed in buildDataInitializer and the globals pipeline:

1. h-suffix hex format: scalars (integers, undefined types) previously
   used Ghidra's display-oriented representation (e.g. 'FFFFFFFFh').
   Now they use getValue() -> Scalar/BigInteger and format as 0x-prefixed
   hex padded to the natural byte width (e.g. '0xFFFFFFFF').

2. Undefined type char appendage: Undefined1DataType.getRepresentation()
   returns '78h    x' (hex value + ASCII char). Fixed by bypassing
   getDefaultValueRepresentation() for scalar types entirely and using
   getValue() instead.

3. Global variable sort order: globals from different decompilation
   results were emitted in function-address order rather than variable-
   address order. writeResults() now accumulates new globals into a
   TreeMap<Address,String> per batch and writes them sorted by address.
   CPPResult.globals() is changed from List<String> to List<GlobalDecl>
   (a new record carrying Address + declaration) to enable address-based
   deduplication and sorting. processedGlobals is changed from
   Set<String> to Set<Address>.

4. Uninitialized memory initializer: when getValue() returns null or
   getDefaultValueRepresentation() returns '??' (inaccessible memory),
   buildDataInitializer() now returns null so a plain declaration is
   emitted instead of 'var = ??;'.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Arrays use C-style declaration now: `byte name[32]` instead of
  `byte[32] name` (via new buildGlobalDeclaration helper that unwraps
  nested Array layers and appends dimensions after the variable name)
- Symbol names containing characters invalid in C identifiers (e.g. +)
  are sanitized by replacing such characters with _; the original
  name is appended as a comment so information is not lost

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant