1

This is my test code:

#include <stdio.h> int main(void) { printf("Hello, world!\n"); return 0; } 

Pretty simple, and I compiled it with gcc -o helloworld helloworld.c (and I also tried -g too).

However when I objdump -tT helloworld, the output is:

 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 

as you can see, there's no printf symbol... what went go wrong..? and interestingly, I didn't use puts though, we can see puts there.

and then I found this

ok if I used printf with literal string, I got puts instead of printf because there's no need to use it. so I compiled this again with -O0 but gcc still optimizes it.

what should I do for keeping gcc from optimizing it?

2
  • 2
    Why do you care? Commented Oct 2, 2021 at 4:59
  • @Barmar just curiosity Commented Oct 2, 2021 at 5:00

2 Answers 2

14

With gcc, you can use the command-line option

-fno-builtin-printf

so that the compiler doesn't recognize the function printf as a built-in function (which would allow further optimizations, such as redirecting it to puts).

Or you can use

-fno-builtin

so that the compiler doesn't recognize any functions as built-in, except for functions starting with the __builtin_ prefix.

However, I generally don't recommend doing this, because this will probably have a negative performance impact. You should only do this when you have a special reason to do so.

Sign up to request clarification or add additional context in comments.

Comments

1

Add a trivial specifier:

printf("Hello, world%c", '\n'); 

1 Comment

is there another way that doesn't change code itself? I wanna achieve that without changing code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.