Function with __attribute__ ((constructor)) not placed in .ctors section

msciboor msciboor@googlemail.com
Sat Jul 28 15:11:00 GMT 2012


Hello,

I read through Internet that .ctors section is used to hold pointers
to functions with __attribute__ ((constructor)). However when I
compile my program with function with __attribute__ ((constructor)),
then .ctors section is empty and this function is invoked directly
from __libc_csu_init function instead of __do_global_ctors_aux. Was it
changed lately?

Source:

$ cat main.cpp
int x;
void __attribute__ ((constructor)) fun() {
    x = 55;
}

int main() {
   return 0;
}
$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ main.cpp -o main
$ objdump -s -j .ctors ./main

./main:     file format elf32-i386

Contents of section .ctors:
 8049f04 ffffffff 00000000                    ........
$ gdb ./main
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/user/test/main...(no debugging symbols found)...done.
(gdb) break fun
Breakpoint 1 at 0x80483b7
(gdb) run
Starting program: /home/user/test/main

Breakpoint 1, 0x080483b7 in fun() ()
(gdb) bt
#0  0x080483b7 in fun() ()
#1  0x08048422 in __libc_csu_init ()
#2  0x0014c46a in __libc_start_main (main=0x80483c3 <main>, argc=1,
ubp_av=0xbffff224, init=0x80483d0 <__libc_csu_init>, fini=0x8048440
<__libc_csu_fini>,
    rtld_fini=0x11f270 <_dl_fini>, stack_end=0xbffff21c) at libc-start.c:185
#3  0x08048321 in _start ()



More information about the Gcc-help mailing list