简体   繁体   English

如何查看 .bss 中静态变量的内存位置?

[英]How do I see the memory locations of static variables within .bss?

Supposing I have a static variable declared in gps_anetenova_m10478.c as follows:假设我在gps_anetenova_m10478.c声明了一个静态变量,如下所示:

static app_timer_id_t m_gps_response_timeout_timer_id;

I have some sort of buffer overrun bug in my code and at some point a write to the variable right before m_gps_response_timeout_timer_id in memory is overwriting it.我的代码中有某种缓冲区溢出错误,并且在内存中的m_gps_response_timeout_timer_id之前写入变量正在覆盖它。

I can find out where m_gps_response_timeout_timer_id is in memory using the 'Expressions' view in Eclipse's GDB client.我可以使用 Eclipse 的 GDB 客户端中的“表达式”视图找出m_gps_response_timeout_timer_id在内存中的位置。 Just enter &m_gps_response_timeout_timer_id .只需输入&m_gps_response_timeout_timer_id But how do I tell which variable is immediately before it in memory?但是我怎么知道哪个变量在内存中紧接在它之前呢?

Is there a way to get this info into the .map file that ld produces?有没有办法将这些信息放入ld生成的 .map 文件中? At the moment I only see source files:目前我只看到源文件:

 .bss           0x000000002000011c        0x0 _build/debug_leds.o
 .bss           0x000000002000011c     0x11f8 _build/gps_antenova_m10478.o
 .bss           0x0000000020001314     0x161c _build/gsm_ublox_sara.o

I'll be honest, I don't know enough about Eclipse to give an easy way within Eclipse to get this.老实说,我对 Eclipse 的了解还不够多,无法在 Eclipse 中提供一种简单的方法来获得它。 The tool you're probably looking for is either objdump or nm .您可能正在寻找的工具是objdumpnm An example with objdump is to simply run objdump -x <myELF> . objdump一个例子是简单地运行objdump -x <myELF> This will then return all symbols in the file, which section they're in, and their addresses.这将返回文件中的所有符号、它们所在的部分以及它们的地址。 You'll then have to manually search for the variable in which you're interested based on the addresses.然后,您必须根据地址手动搜索您感兴趣的变量。

objdump -x <ELFfile> will give output along the lines of the following: objdump -x <ELFfile>将给出如下输出:

000120d8 g     F .text  0000033c bit_string_copy
00015ea4 g     O .bss   00000004 overflow_bit
00015e24 g       .bss   00000000 __bss_start
00011ce4 g     F .text  0000003c main
00014b6c g     F .text  0000008c integer_and

The first column is the address, the fourth the section and the fifth the length of that field.第一列是地址,第四列是部分,第五列是该字段的长度。

nm <ELFfile> gives the following: nm <ELFfile>给出以下内容:

00015ea8 B __bss_end
00015e24 B __bss_start
0000c000 T _start
00015e20 D zero_constant
00015e24 b zero_constant_itself

The first column is the address and the second the section.第一列是地址,第二列是部分。 D/d is data, B/b is BSS and T/t is text. D/d 是数据,B/b 是 BSS,T/t 是文本。 The rest can be found in the manpage.其余的可以在联机帮助页中找到。 nm also accepts the -n flag to sort the lines by their numeric address. nm还接受-n标志以按数字地址对行进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM