当我使用GNU nm时,它不会显示__DTOR_LIST__

时间:2015-08-08 12:35:14

标签: c linux destructor nm

我有一个“大”的问题。 我目前正在尝试在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

1 个答案:

答案 0 :(得分:1)

gcc似乎有一些变化。 ctor和dtor列表已被init_array和fini_array取代。

我不确定你是否发生了什么事,但是当我看到(带有析构函数的程序,构造函数属性声明)和(没有它们的程序)之间的objdumps差异时,主要突出显示的部分是.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");
}