Getting memory section information

Posted by Basilevs on Stack Overflow See other posts from Stack Overflow or by Basilevs
Published on 2010-04-15T06:54:12Z Indexed on 2010/04/15 7:03 UTC
Read the original article Hit count: 345

Filed under:
|
|
|

Can somebody explain me how the following code works?

# if   defined(__ELF__)
#  define __SECTION_FLAGS   ", \"aw\" , @progbits"
    /* writable flag needed for ld ".[cd]tors" sections bug workaround) */
# elif defined(__COFF__)
#  define __SECTION_FLAGS   ", \"dr\""
    /* untested, may be writable flag needed */
# endif

asm
(
    ".section .ctors" __SECTION_FLAGS "\n"
    ".globl __ctors_begin__\n"
    "__ctors_begin__:\n"
    ".previous\n"
);
asm /* ld ".[cd]tors" sections bug workaround */
(
    ".section .ctors0" __SECTION_FLAGS "\n"
    ".globl __ctors0_begin__\n"
    "__ctors0_begin__:\n"
    ".previous\n"
);

Similarly we are getting __ctors_end__ , __ctors0_end__ and destructors location is also obtained this way. After some ld bug workarounds all functions pointed by pointers from __ctors_begin__ to __ctors_end__ are executed. I don't know assembler and this code is impossible for me to interpret.

BTW: I know that invoking C++ contructors/destructors from C is not a task to be considered safe or easy.

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++