I just a bit confused by this previous discussion about .init in https://groups.google.com/forum/#!topic/gnu.gcc.help/Fit5UOU9UNs Because, from all the discussion I can get from internet shows that:
using "extern int hello_init() attribute ((constructor));" will not add code into .init section but put it into a function array in .ctors, right? But If I compile the example code:
#include <stdio.h>
extern int hello_init() __attribute__ ((constructor));
int hello_init()
{
printf("init hello\n");
return 0;
}
<<<<< if I compile it with "gcc -fPIC -shared -o libender.so test.c", using objdump -x or -d I can't see .ctors section and I can't even see any calls to __do_global_ctors_aux anywhere in the file including .init section. Did recent version of GCC has change the behavior of it? Is there anyway I can see the function with attribute of constructor from the ELF files?
- If i don't use constructor attribute and just compile it using "Wl,-init,hello_init". When I use objdump or readelf to dump the so file, I still can't see the code of hello_init in .init section. Does this flag really put the function code into .init section? if so is there any way I can use objdump or readelf to observe it?
my .init section dump is always be like this(no matter compiled with Wl, -init or using constructor attribute):
Disassembly of section .init:
00000000000005d0 <_init>:
5d0: 48 83 ec 08 sub $0x8,%rsp
5d4: 48 8b 05 05 0a 20 00 mov 0x200a05(%rip),%rax # 200fe0 <_DYNAMIC+0x1c8>
5db: 48 85 c0 test %rax,%rax
5de: 74 05 je 5e5 <_init+0x15>
5e0: e8 2b 00 00 00 callq 610 <__gmon_start__@plt>
5e5: 48 83 c4 08 add $0x8,%rsp
5e9: c3 retq
If I both used the attribute in the C file and compiled with Wl,init. I do see hello_init called twice before main. However, how I can observed them in ELF files, where didn't GCC put those function pointers?