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

标签 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

最佳答案

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");
}

关于c - 当我使用 GNU nm 时,它不显示 __DTOR_LIST__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893194/

相关文章:

c - 枚举数据类型内存分配

c++ - 用于调试打印的 MS 标志中的奇怪值

c - 程序将 int 转换为 ASCII,我不知道为什么?

python - 使用 __del__ 方法清理对象

python - 在 Python 中,每个对象可以调用 `__del__` 多少次?

c++将相同的对象返回给调用者

c - 局部指针的段错误?

PYTHONPATH 不包含在 Ubuntu 16.04 的 sys.path 中

linux - 如何在没有第一个参数 fn 的情况下调用带有标志的 fork() 或 clone()?

c++ - 加载共享库 : cv2. 时出错,因此对 C++ 应用程序使用 php exec()。在网络应用程序上