【问题标题】:When I use GNU nm it doesn't display __DTOR_LIST__当我使用 GNU nm 时,它不显示 __DTOR_LIST__
【发布时间】:2015-10-31 19:28:17
【问题描述】:

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

【问题讨论】:

  • printf() 格式字符串的末尾包含一个换行符是个好主意。或者,对于简单的“输出字符串”调用,请改用 puts()
  • 我在文档 (gcc.gnu.org/onlinedocs/gccint/Initialization.html) 中没有看到它声明 __DTOR_LIST__ 将是可执行文件中的实际符号。
  • 您也许可以使用可移植的 'atexit()' 函数作为 'attribute ((destructor));'不便携。
  • 我明白了,但这里的问题是要知道 .dtor 在哪里,并且显然它已经消失了....但是谢谢您提供的信息,接下来我将使用 atexit()

标签: c linux destructor nm


【解决方案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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    相关资源
    最近更新 更多