在GDB中如何区分来自不同目标文件的同名符号?

How do I differentiate symbols with the same name from different object files in GDB?

我有两个源文件(在 C 中),它们具有同名的全局变量。全局变量是静态的。如果我使用 nm 从目标文件中转储符号,我可以看到包含调试信息:

hostname:ble_app_hrs username$ find Debug -name '*.o' -exec /usr/local/gcc-arm-none-eabi-4_8-2014q2/bin/arm-none-eabi-nm -o -l {} \; | grep m_user_array_size
Debug/components/libraries/gpiote/app_gpiote.o:00000000 b m_user_array_size GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/gpiote/app_gpiote.c:36
Debug/components/libraries/timer/app_timer.o:00000000 b m_user_array_size   GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/timer/app_timer.c:131

但是,如果我在链接完成后转储 ELF 文件中的符号,似乎这些重复符号的调试信息已被删除。这是正常行为吗? what's described here 是自动发生的吗?

hostname:ble_app_hrs username$ /usr/local/gcc-arm-none-eabi-4_8-2014q2/bin/arm-none-eabi-nm -o -l Debug/ble_app_hrs.elf | grep user
Debug/ble_app_hrs.elf:0001cb50 T app_gpiote_user_enable GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/gpiote/app_gpiote.c:224
Debug/ble_app_hrs.elf:0001ca88 T app_gpiote_user_register   GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/gpiote/app_gpiote.c:190
Debug/ble_app_hrs.elf:200020a4 B m_enabled_users_mask.6444
Debug/ble_app_hrs.elf:200020c0 B m_gpiote_user_id.6603
Debug/ble_app_hrs.elf:20002080 B m_user_array_size.5782
Debug/ble_app_hrs.elf:200020a8 B m_user_array_size.6446
Debug/ble_app_hrs.elf:200020a9 B m_user_count.6445
Debug/ble_app_hrs.elf:20002084 B mp_users.5783
Debug/ble_app_hrs.elf:200020ac B mp_users.6443
Debug/ble_app_hrs.elf:0001d688 t user_id_get.5752.4484  GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/timer/app_timer.c:1056
Debug/ble_app_hrs.elf:0001d2cc t user_op_alloc.5716.4521    GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/timer/app_timer.c:794
Debug/ble_app_hrs.elf:0001d2b4 t user_op_enque.5691.4546    GNU_ARM_Eclipse/nRF51_SDK_7/components/libraries/timer/app_timer.c:781

请注意,并不是所有的调试信息都被删除了——对于像 app_gpiote_user_enable 这样的符号,它仍然存在。如果我尝试打印其中一个副本,例如 m_user_array_size,gdb 会告诉我,No symbol "m_user_array_size" in current context. 但是,如果我打印 app_gpiote_user_enable,gdb 会对此感到满意。

  1. 我应该如何在 gdb 中打印重复的符号?我必须使用地址而不是符号吗?
  2. 重复符号末尾的.5782等数字是什么?这会帮助我将符号映射回目标文件吗?

注意:我不想只是重命名变量——它们都是在第 3 方库中定义的。

重复符号打印如下:p 'f2.c'::x。解释 in this section of GDB manual.

使用address打印可以这样(假设0x60103c是整型变量的地址):

print *(int*)0x60103c

我只在使用 -flto 标志构建可执行文件时才在符号末尾看到数字。这(对于默认 Ubuntu 14.04 工具链)也破坏了 gdb 从其他文件打印符号的能力(它打印了错误的文件)。一种解决方法是在调试时不使用 -flto 进行构建。