Search code examples
gdbarmdebug-symbolsnm

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


I have two source files (in C) which have globals with the same name. The globals are static. If I use nm to dump the symbols from the object files, I can see that debug info is included:

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

However, if I dump the symbols from the ELF file after linking is finished, it appears that the debug info got stripped out for these duplicate symbols. Is this normal behavior--i.e. is what's described here happening automatically?

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

Notice that not all debug info is stripped--it's still there for symbols like app_gpiote_user_enable. If I try to print one of the duplicates, like m_user_array_size, gdb tells me, No symbol "m_user_array_size" in current context. However, if I print app_gpiote_user_enable, gdb is happy with that.

  1. How should I print duplicate symbols in gdb? Do I have to use addresses instead of symbols?
  2. What are the .5782, etc. numbers on the end of the duplicate symbols? Will that help me map symbols back to object files?

Note: I rather not just rename the variables--they're both defined in a 3rd party library.


Solution

  • Duplicate symbols are printed like this: p 'f2.c'::x. It is explained in this section of GDB manual.

    Printing by using address can be done like this (assuming 0x60103c is the address of integer variable):

    print *(int*)0x60103c
    

    I have seen numbers at the end of symbols only when -flto flag was used to build executable. This (for default Ubuntu 14.04 toolchain) also breaks gdb's ability to print symbols from other files (it prints the wrong one). Kind of workaround for this would be to build without -flto when debugging.