optimizing out string functions when the string contains a NUL
ohav chochmah
philomath868@gmail.com
Wed Jun 20 15:33:00 GMT 2012
Hi,
consider the following program:
#include <stdio.h>
#define HIDDEN "\0this string starts with a NUL"
int main(void)
{
puts(HIDDEN);
printf(HIDDEN);
return 0;
}
AFAICT, the puts and printf are both no-ops, as the C-string stops
before it starts. yet when doing 'gcc -S tst.c -O2 -march=native' or
even 'gcc -S tst.c -O3 -march=native', GCC generates the following:
.file "tst.c"
.section .rodata
.align 8
.LC0:
.string ""
.string "this string starts with a NUL"
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $.LC0, %edi
call puts
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE11:
.size main, .-main
.ident "GCC: (GNU) 4.7.1"
.section .note.GNU-stack,"",@progbits
in short, the printf was removed but not the puts. which left me wondering why?
thanks.
More information about the Gcc-help
mailing list