4

I found the following post (How to generate gcc debug symbol outside the build target?) on how to split a the compiled file and the debugging symbols.

However, I cannot find any useful information in the debugging file.

For example,

My helloWorld code is:

#include<stdio.h>

int main(void) {

    int a;
    a = 5;
    printf("The memory address of a is: %p\n", (void*) &a);
    return 0;
}

I ran gcc -g -o hello hello.c

objcopy --only-keep-debug hello hello.debug
gdb -s main.debug -e main

In gdb, anything I tried won't give me any information on a, I cannot find its address, I cannot find the main function address

For example :

(gdb) info variables
All defined variables:

Non-debugging symbols:
0x0000000000400618  _IO_stdin_used
0x0000000000400710  __FRAME_END__
0x0000000000600e3c  __init_array_end
0x0000000000600e3c  __init_array_start
0x0000000000600e40  __CTOR_LIST__
0x0000000000600e48  __CTOR_END__
0x0000000000600e50  __DTOR_LIST__
0x0000000000600e58  __DTOR_END__
0x0000000000600e60  __JCR_END__
0x0000000000600e60  __JCR_LIST__
0x0000000000600e68  _DYNAMIC
0x0000000000601000  _GLOBAL_OFFSET_TABLE_
0x0000000000601028  __data_start
0x0000000000601028  data_start
0x0000000000601030  __dso_handle
0x0000000000601038  __bss_start
0x0000000000601038  _edata
0x0000000000601038  completed.6603
0x0000000000601040  dtor_idx.6605
0x0000000000601048  _end

Am I doing something wrong? Am I understanding the debug file incorrectly? Is there even a way to find out an address of compiled variable/function from a saved debugging information?

Community
  • 1
  • 1
Quantico
  • 2,398
  • 7
  • 35
  • 59
  • You forgot to strip your `main`, all other things you do in correct way. Do you compare result of gdb before `objcopy` and after? – fghj Apr 19 '16 at 19:44
  • I don't, what do you mean by that? – Quantico Apr 19 '16 at 19:49
  • I mean you say that after extract debug information to separate file something not work in `gdb`, I ask do you try this `something` that do not work in gdb before operation of separating debug info. – fghj Apr 19 '16 at 19:52

1 Answers1

3

int a is a stack variable so it does not have a fixed address unless you are in a call to that specific function. Furthermore, each call to that function will allocate its own variable.

When we say "debugging symbols" we usually mean functions and global variables. A local variable is not a "symbol" in this context. In fact, if you compile with optimisations enabled int a would almost certainly be optimised to a register variable so it would not have an address at all, unless you forced it to be written to memory by doing some_function(&a) or similar.

You can find the address of main just by writing print main in GDB. This is because functions are implicitly converted to pointers in C when they appear in value context, and GDB's print uses C semantics.

jforberg
  • 6,537
  • 3
  • 29
  • 47
  • I see. Thank you very much for this detailed explanation. I am new to C and trying to figure things out. What would be a good testing example for code that will have a as a variable I can extract its address ? – Quantico Apr 19 '16 at 19:48
  • When you are in a function call, you can get the address of local variables of that specific call instance. You could also declare it as a global variable and then it would get its own symbol. Try using ```nm``` to list all symbols in a binary. Also if my answer was helpful to you, please consider upvoting and accepting it. – jforberg Apr 19 '16 at 19:51
  • upvoted. I am still trying to figure few things before I accept it. What do you mean by nm? So for a local variable I need a break point withing a function call and catch it during a run to find the address of a? – Quantico Apr 19 '16 at 19:53
  • ```nm``` is the standard unix tool for viewing symbols. If you're on Linux it would be in the binutils package. Check the docs for details. Re local variables: yes, sort of. Remember that functions can call each other so you could have a function A which calls B which calls A again. Then there would be two differente instances of A's local variables with different addresses. Perhaps this isn't obvious to you; if so then I recommend you read up on *call conventions* and *stack frames*. There is a lot of good info here on this site and elsewhere. Good luck. – jforberg Apr 19 '16 at 20:59