2

我有一个“大”问题。我目前正在尝试在 Linux 上的程序(它是一个虚拟机)中获取我的 dtor 列表的地址。显然有一个析构函数,但是当我使用 nm 并显示所有地址及其名称时,我找不到它;唯一与之相关的是do_global_dtors_aux。此外,当代码运行时,它按预期工作得非常好。这是我的一段代码:

  #include <stdlib.h>
  #include <stdio.h>

  static void cleanup(void) __attribute__ ((destructor));

  int main() {
      printf("in the main function...");
      exit(0);
  }

  void cleanup(void){
      printf("in the cleanup");
  }

这是我使用的时候nm

0000000000600808 d _DYNAMIC
00000000006009f0 d _GLOBAL_OFFSET_TABLE_
0000000000400678 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses
00000000004007e0 r __FRAME_END__
0000000000600800 d __JCR_END__
0000000000600800 d __JCR_LIST__
0000000000600a38 D __TMC_END__
0000000000600a38 A __bss_start
0000000000600a28 D __data_start
0000000000400550 t __do_global_dtors_aux
00000000006007f0 t __do_global_dtors_aux_fini_array_entry
0000000000600a30 D __dso_handle
00000000006007e8 t __frame_dummy_init_array_entry
                 w __gmon_start__
00000000006007f0 t __init_array_end
00000000006007e8 t __init_array_start
00000000004005d0 T __libc_csu_fini
00000000004005e0 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000600a38 A _edata
0000000000600a40 A _end
000000000040066c T _fini
0000000000400430 T _init
0000000000400490 T _start
00000000004004bc t call_gmon_start
00000000004005b4 t cleanup
0000000000600a38 b completed.6092
0000000000600a28 W data_start
00000000004004e0 t deregister_tm_clones
                 U exit@@GLIBC_2.2.5
0000000000400570 t frame_dummy
000000000040059c T main
                 U printf@@GLIBC_2.2.5
                 U puts@@GLIBC_2.2.5
0000000000400510 t register_tm_clones
4

1 回答 1

1

gcc 似乎发生了一些变化。ctor 和 dtor 列表已被 init_array 和 fini_array 替换。

我不确定这是否发生在您身上,但是当我看到(具有析构函数、构造函数属性声明的程序)和(没有它们的程序)之间的 objdump 差异时,最突出显示的部分是 .init_array 和 .fini_array

这可能指的是 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46770

我不确定如何从列表中获取函数地址,但希望您可以从这里获取。

他们(在 gcc bugzilla 上)引用的一个简单示例可能会对您有所帮助。

#include <stdio.h>

static void
init ()
{
  printf ("init_array\n");
}

static void (*const init_array []) ()
      __attribute__ ((section (".init_array"), aligned (sizeof (void *))))
        = { init };

static void
fini ()
{
  printf ("fini_array\n");
}

static void (*const fini_array []) ()
      __attribute__ ((section (".fini_array"), aligned (sizeof (void *))))
        = { fini };

static void
ctor ()
{
  printf ("ctor\n");
}

static void (*const ctors []) ()
      __attribute__ ((section (".ctors"), aligned (sizeof (void *))))
        = { ctor };

static void
dtor ()
{
  printf ("dtor\n");
}

static void (*const dtors []) ()
      __attribute__ ((section (".dtors"), aligned (sizeof (void *))))
        = { dtor };

int
main ()
{
  printf ("main\n");
}
于 2015-08-08T14:18:18.723 回答