1

As I understand it, for each function symbol in the program there is a corresponding range of memory in the address space. The call assembly instruction pushes ip and some other info on the stack and jumps to the beginning of that range.

I need to get that range while debugging with GDB. I can easily get a name of a function corresponding to a given address or line using info symbol. I need the opposite: symbol -> address range. In fact, I can even use TUI mode, scroll assembly code down to the end of the function and look at the "<symbol_name>+<offset>" annotation near the last instruction but that's definitely not the right way to do it (it's even worse because I'm using GDB/MI).

How can I match a symbol to its memory range using GDB?

1 Answer 1

1

As I understand it, for each function symbol in the program there is a corresponding range of memory in the address space.

A given "source" function may occupy 0 (in case the function is never called and the linker garbage-collects it) or more address ranges in the linked ELF image:

  1. The compiler is free to split a function into multiple parts, each with its own address range, and the linker is not obligated to put these parts next to each other. GCC will do that in order to e.g. keep frequently executed parts of foo (foo.hot section) and infrequently executed parts (foo.cold section) on separate pages, in order to minimize TLB flushes.

  2. Inverse is also possible: when foo is frequently called with a fixed value of a parameter, GCC may make a copy of the function (foo.clone.123) and optimize that copy for this value.

How can I match a symbol to its memory range using GDB?

To find the start of the range, just print &foo.

There doesn't seem to be a way to find the end of the range in GDB (besides the disas command you've already discovered). On ELF platforms, you can trivially find the length of the function from e.g. readelf -Ws | grep foo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.