Fix #8908 (Program export does not export globals' data)#9293
Open
andrey-starodubtsev wants to merge 3 commits into
Open
Fix #8908 (Program export does not export globals' data)#9293andrey-starodubtsev wants to merge 3 commits into
andrey-starodubtsev wants to merge 3 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
0xprefix is used for C sources now.char/undefinedtype var = ??;instead of plaintype var;Java-styletype declarations (i.e.byte[32] var;instead ofbyte var[32])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).