arch/arm: Add the macro definitions for CFI instructions for ghs #18099
+49
−0
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.
Add the macro definitions for CFI instructions for greenhills compiler
Summary
This code provides compatibility adaptation for CFI (Call Frame Information) instructions in the Green Hills Compiler (GHS). When the ghs macro is detected (indicating a Green Hills compiler environment), it defines a series of empty assembly macros that map GCC-style CFI instructions to no-ops.
Core Functionality:
Conditional Compilation: Only activates when using the Green Hills compiler
Macro Redefinition: Defines 11 CFI-related instructions as empty macros (.endm)
Compatibility Handling: Prevents syntax errors when compiling GCC-style assembly code in GHS
Impact
Positive Impacts:
Impact Dimension: Specific Manifestation
Compilation Compatibility: Allows the same assembly code to compile under both GCC and Green Hills
Code Portability: Supports cross-compiler development, especially in embedded domains
Development Efficiency: Eliminates the need to maintain multiple code versions for different compilers
Debug Information Compatibility: Prevents CFI instructions from being misinterpreted in GHS
Potential Risks:
Risk Dimension: Potential Issues
Debugging Capability: May lose stack unwinding information in Green Hills
Exception Handling: C++ exceptions might not unwind call stacks correctly
Performance Analysis: Some profiling tools may rely on CFI information
Maintenance Complexity: Must ensure all CFI instructions are properly handled
Testing
Test 1: Compiler Compatibility Test
// test_compiler_compatibility.c
#ifdef ghs
#pragma message "Compiling with Green Hills Compiler"
#else
#pragma message "Compiling with non-GHS compiler"
#endif
// Inline assembly test
void test_function(void) {
asm volatile (
".cfi_startproc\n\t"
"nop\n\t"
".cfi_def_cfa_offset 16\n\t"
"nop\n\t"
".cfi_endproc"
);
}
int main() {
test_function();
return 0;
}
Results:
GHS Compiler: Compilation succeeds, no CFI-related errors
GCC Compiler: Compilation succeeds, generates complete CFI information
Test 2: Macro Expansion Verification Test
// test_macro_expansion.s
// Test if macros are correctly defined as empty
#ifdef ghs
// Verify each macro expands to no-op
.macro .cfi_startproc
.endm
#endif
.global test_func
test_func:
.cfi_startproc // Should expand to empty
push %rbp
.cfi_def_cfa_offset 16 // Should expand to empty
mov %rsp, %rbp
.cfi_endproc // Should expand to empty
ret
Verification Method:
Use GHS assembler preprocessing and examine expansion results
Check if .cfi_ instructions remain in preprocessed code
Test 3: Functional Impact Test
// test_functional_impact.cpp
#include
#include
void level3() {
asm volatile (".cfi_remember_state");
throw std::runtime_error("Test exception");
asm volatile (".cfi_restore_state");
}
void level2() {
asm volatile (".cfi_def_cfa rsp, 8");
level3();
}
void level1() {
asm volatile (".cfi_startproc");
level2();
asm volatile (".cfi_endproc");
}
int main() {
try {
level1();
} catch (const std::exception& e) {
std::cout << "Exception caught: " << e.what() << std::endl;
// In GHS, stack unwinding may be affected due to missing CFI
}
return 0;
}
Testing Focus:
Whether exceptions are correctly caught
Whether the program crashes
Whether debuggers can correctly display call stacks
Test 4: Performance Comparison Test
// test_performance.c
#define ITERATIONS 1000000
// Function with CFI
attribute((noinline))
void with_cfi() {
asm volatile (
".cfi_startproc\n\t"
".cfi_def_cfa_offset 16\n\t"
"nop\n\t"
".cfi_endproc"
);
}
// Function without CFI (control group)
attribute((noinline))
void without_cfi() {
asm volatile ("nop");
}
int main() {
for (int i = 0; i < ITERATIONS; i++) {
with_cfi(); // Test group
without_cfi(); // Control group
}
return 0;
}
Measurement Metrics:
Code size differences
Execution time differences
Memory usage differences