Cannot find .dtors and .ctors in binary format.

I read the book Hacking, the art of exploitation. The book has a section explaining the use of .dtors and .ctors .

I am trying to reproduce one of the exercises in a book, but these sections are not in my executable file. At first I thought that the problem was that I compiled for the 64-bit version, but now I am compiling for the 32-bit version, and .dtors and .ctors still do not appear in the partition table. Here is the code:

 #include <stdio.h> #include <stdlib.h> static void miConstructor(void) __attribute__ ((constructor)); static void miDestructor(void) __attribute__ ((destructor)); int main(void) { printf("En main() \n"); return 0; } void miConstructor(void) { printf("En el constructor\n"); } void miDestructor(void) { printf("En el destructor\n"); } 

I compile with:

  gcc -m32 -o a.out dtors_example.c 

This is nm output:

 080495f0 d _DYNAMIC 080496e4 d _GLOBAL_OFFSET_TABLE_ 080484dc R _IO_stdin_used w _ITM_deregisterTMCloneTable w _ITM_registerTMCloneTable w _Jv_RegisterClasses 080485d8 r __FRAME_END__ 080495ec d __JCR_END__ 080495ec d __JCR_LIST__ 08049704 D __TMC_END__ 08049704 A __bss_start 080496fc D __data_start 080483c0 t __do_global_dtors_aux 080495e4 t __do_global_dtors_aux_fini_array_entry 08049700 D __dso_handle 080495dc t __frame_dummy_init_array_entry w __gmon_start__ 080484ba T __i686.get_pc_thunk.bx 080495e4 t __init_array_end 080495dc t __init_array_start 08048450 T __libc_csu_fini 08048460 T __libc_csu_init U __libc_start_main@@GLIBC_2.0 08049704 A _edata 08049708 A _end 080484c0 T _fini 080484d8 R _fp_hw 080482b8 T _init 08048320 T _start 08049704 b completed.5730 080496fc W data_start 08048350 t deregister_tm_clones 080483e0 t frame_dummy 0804840c T main 08048428 t miConstructor 0804843c t miDestructor U puts@@GLIBC_2.0 08048380 t register_tm_clones 

.dtors output doesn't show .dtors or .ctors

Maybe the sections __init_array_end , __init_array_start or __do_global_dtors_aux are related to the behavior of .ctors and .dtors ?

+4
c constructor destructor objdump nm
source share
2 answers

The problem is most likely gcc. under gcc 4.7, you can generate .ctors sections, but gcc 4.7 uses .init_array instead of .ctors. You can confirm this by running the command listed below. objdump -dr -j.ctors a.out.if no sections were found, try objdump -dr -j.init_array a.out or you can do this readelf -S a.out to list all sections. then you will find .ctors or (and) .init_array.

+9
source share

Use the objdump command with the -x option to view the full available header information, character table, and move entries.

 objdump -x ./yourcommand 
+1
source share

All Articles