【问题标题】:List global variables in executable列出可执行文件中的全局变量
【发布时间】:2012-06-12 03:54:29
【问题描述】:

如何转储可执行文件中的所有全局变量和地址偏移量?

这是在 os x 上,使用 gcc 编译的 xcode 开发的应用程序。

谢谢

【问题讨论】:

    标签: c xcode macos gcc otool


    【解决方案1】:

    如果编译成 Mach-O

    使用otoolcctools

    如果编译为 ELF

    您应该可以使用objdump 和/或readelf 来执行此操作。

    我手头没有 *nix 系统,但objdump -s -j .data 应该可以让你离你很近。

    【讨论】:

    • 这是一个 OS X 应用程序。它没有编译为 ELF。除非您安装 libelf 和 elftools,否则没有 objdump。
    • 如何使用otools获取全局变量?
    【解决方案2】:

    从 macOS 上的可执行文件中获取全局变量列表及其偏移量的简单方法(原始问题)是使用 nm 工具。没有任何额外的键,它给出了变量和它们的偏移量。

    例子:

    $ nm <some binary>
    ...
                     U __ZTVN10__cxxabiv117__class_type_infoE
                     U __ZTVN10__cxxabiv120__si_class_type_infoE
    000000010006e248 s __ZTVN7testing17TestEventListenerE
    000000010006e1c0 s __ZTVN7testing22EmptyTestEventListenerE
    000000010006dfa8 s __ZTVN7testing31TestPartResultReporterInterfaceE
    000000010006d860 S __ZTVN7testing32ScopedFakeTestPartResultReporterE
    000000010006d8d8 S __ZTVN7testing4TestE
    ...
    

    查看man nm了解UsS等代码的解释。


    如果您还想查找字符串常量,还有另一个工具strings

    $ strings <some binary> -o -t x
    

    将为您提供字符串文字及其偏移量的列表。

    更多详情请见man strings

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多