https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71585
Bug ID: 71585 Summary: Cannot selectively disable stack protector with LTO Product: gcc Version: 5.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: js at alien8 dot de Target Milestone: --- The following code includes a function that is intended to not be compiled with a stack canary. This is a reduced example from an embedded project, where this function is the function that actually sets the __stack_chk_guard variable. The code is this: #pragma GCC push_options #pragma GCC optimize ("-fno-stack-protector") __attribute__((constructor)) void foo() { asm (""); } #pragma GCC pop_options int main() { return 0; } When compiled without LTO ( gcc -fstack-protector-all -O2 -o main lto-func-attr.c ), it correctly emits a non-stack-protected function foo(): 00000000004004c0 <foo>: 4004c0: c3 ret When compiled with LTO ( gcc -fstack-protector-all -flto -O2 -o main lto-func-attr.c ), it incorrectly emits a stack canary check: 00000000004004a0 <foo>: 4004a0: 48 83 ec 18 sub rsp,0x18 4004a4: 64 48 8b 04 25 28 00 mov rax,QWORD PTR fs:0x28 4004ab: 00 00 4004ad: 48 89 44 24 08 mov QWORD PTR [rsp+0x8],rax 4004b2: 31 c0 xor eax,eax 4004b4: 48 8b 44 24 08 mov rax,QWORD PTR [rsp+0x8] 4004b9: 64 48 33 04 25 28 00 xor rax,QWORD PTR fs:0x28 4004c0: 00 00 4004c2: 75 05 jne 4004c9 <foo+0x29> 4004c4: 48 83 c4 18 add rsp,0x18 4004c8: c3 ret 4004c9: e8 72 ff ff ff call 400440 <__stack_chk_fail@plt> The expected behavior is that even with LTO, no stack canary check is emitted. I am using gcc (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6) running on Fedora 23 x86_64.