C/C++: Enable user-defined stack protection guard symbols.
This change allows users to define the stack protection guard as a global symbol, and merges it if it matches the compiler's internal symbol.
This commit introduces a new target hook, stack_protect_guard_symbol_p, which allows users to define their own stack protection guard as a global symbol. If the hook returns true, the compiler declares __stack_chk_guard as a global uintptr_t variable and merges it with the user-declared variable if they match, including its visibility attribute. This enables more flexible stack protection schemes.
In Details
The commit introduces the TARGET_STACK_PROTECT_GUARD_SYMBOL_P target hook, enabling target-specific control over stack protection guard symbols. When this hook returns true, the compiler declares __stack_chk_guard as a global uintptr_t. The existing default_stack_protect_guard function is modified to use uintptr_t when the hook is enabled. The ix86_stack_protect_guard_symbol_p is provided as an example implementation. The C frontend is modified to declare a global symbol for the stack protection guard based on the hook's return value.
For Context
Stack smashing is a common security vulnerability where attackers overwrite memory on the stack to inject malicious code. Stack protection is a compiler-based defense that places a "canary" value before the return address on the stack, and verifies that it hasn't been changed before returning from a function. If the canary has been modified, the program terminates, preventing the attacker from hijacking control flow. Traditionally, the compiler manages the canary value itself. This commit enables developers to define their own global symbol for this stack protection canary, allowing for greater customization and potentially integration with other security mechanisms.