防止 gcc 将 printf 优化为 put

0xF*_*0D3 1 c gcc compilation compiler-optimization

这是我的测试代码:

#include <stdio.h>

int main(void)
{
    printf("Hello, world!\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

非常简单,我用它编译了它gcc -o helloworld helloworld.c(我也尝试过-g)。

但是,当我时objdump -tT helloworld,输出是:


helloworld:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000              abi-note.c
000000000000039c l     O .note.ABI-tag  0000000000000020              __abi_tag
0000000000000000 l    df *ABS*  0000000000000000              init.c
0000000000000000 l    df *ABS*  0000000000000000              crtstuff.c
0000000000001070 l     F .text  0000000000000000              deregister_tm_clones
00000000000010a0 l     F .text  0000000000000000              register_tm_clones
00000000000010e0 l     F .text  0000000000000000              __do_global_dtors_aux
0000000000004030 l     O .bss   0000000000000001              completed.0
0000000000003df0 l     O .fini_array    0000000000000000              __do_global_dtors_aux_fini_array_entry
0000000000001130 l     F .text  0000000000000000              frame_dummy
0000000000003de8 l     O .init_array    0000000000000000              __frame_dummy_init_array_entry
0000000000000000 l    df *ABS*  0000000000000000              helloworld.c
0000000000000000 l    df *ABS*  0000000000000000              crtstuff.c
000000000000211c l     O .eh_frame  0000000000000000              __FRAME_END__
0000000000000000 l    df *ABS*  0000000000000000              
0000000000003df0 l       .init_array    0000000000000000              __init_array_end
0000000000003df8 l     O .dynamic   0000000000000000              _DYNAMIC
0000000000003de8 l       .init_array    0000000000000000              __init_array_start
0000000000002014 l       .eh_frame_hdr  0000000000000000              __GNU_EH_FRAME_HDR
0000000000004000 l     O .got.plt   0000000000000000              _GLOBAL_OFFSET_TABLE_
0000000000001000 l     F .init  0000000000000000              _init
00000000000011d0 g     F .text  0000000000000005              __libc_csu_fini
0000000000000000  w      *UND*  0000000000000000              _ITM_deregisterTMCloneTable
0000000000004020  w      .data  0000000000000000              data_start
0000000000000000       F *UND*  0000000000000000              puts@GLIBC_2.2.5
0000000000004030 g       .data  0000000000000000              _edata
00000000000011d8 g     F .fini  0000000000000000              .hidden _fini
0000000000000000       F *UND*  0000000000000000              __libc_start_main@GLIBC_2.2.5
0000000000004020 g       .data  0000000000000000              __data_start
0000000000000000  w      *UND*  0000000000000000              __gmon_start__
0000000000004028 g     O .data  0000000000000000              .hidden __dso_handle
0000000000002000 g     O .rodata    0000000000000004              _IO_stdin_used
0000000000001160 g     F .text  0000000000000065              __libc_csu_init
0000000000004038 g       .bss   0000000000000000              _end
0000000000001040 g     F .text  000000000000002f              _start
0000000000004030 g       .bss   0000000000000000              __bss_start
0000000000001139 g     F .text  000000000000001a              main
0000000000004030 g     O .data  0000000000000000              .hidden __TMC_END__
0000000000000000  w      *UND*  0000000000000000              _ITM_registerTMCloneTable
0000000000000000  w    F *UND*  0000000000000000              __cxa_finalize@GLIBC_2.2.5


DYNAMIC SYMBOL TABLE:
0000000000000000  w   D  *UND*  0000000000000000              _ITM_deregisterTMCloneTable
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 puts
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 __libc_start_main
0000000000000000  w   D  *UND*  0000000000000000              __gmon_start__
0000000000000000  w   D  *UND*  0000000000000000              _ITM_registerTMCloneTable
0000000000000000  w   DF *UND*  0000000000000000  GLIBC_2.2.5 __cxa_finalize
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,没有printf符号......出了什么问题......?有趣的是,虽然我没有使用puts,但我们可以puts在那里看到。

然后我发现了这个

好吧,如果我将 printf 与文字字符串一起使用,我会得到 put 而不是 printf,因为不需要使用它。所以我再次编译了它,-O0但 gcc 仍然优化了它。

我应该怎么做才能阻止 gcc 优化它?

And*_*zel 12

使用gcc,您可以使用命令行选项

-fno-builtin-printf

以便编译器不会将该函数识别printf内置函数(这将允许进一​​步优化,例如将其重定向到puts)。

或者你可以使用

-fno-builtin

以便编译器不会将任何函数识别为内置函数,除了以__builtin_前缀开头的函数。

但是,我通常不建议这样做,因为这可能会对性能产生负面影响。仅当您有特殊原因时才应这样做。