Pergunta

Is there a tool in Linux which maps the different variable in an executable to it corresponding memory segments.

For example, if i have a initialized global variable int x = 10 in my executable, the tool should show that the variable belongs to .data segment of the executable in a format similar to what is shown below.

x .data

Foi útil?

Solução

If your executable is not stripped, then the nm command (see also Linux manpage for nm) will do what you want - it prints a table of symbol names / addresses / types. It's got multiple output options; on Linux, the "sysv"-style formatting comes close to giving you what you want. Sample:

$ nm -f sysv /bin/perl

Symbols from /bin/perl:

Name Value Class Type Size Line Section

Bases.3 |000000000813019c| d | OBJECT|0000000000000014| |.data F0convert |00000000080c6905| t | FUNC|00000000000000c6| |.text PL_AMG_names |0000000008137c80| D | OBJECT|0000000000000108| |.data PL_No |0000000008137904| D | OBJECT|0000000000000004| |.data PL_Yes |0000000008137900| D | OBJECT|0000000000000004| |.data [ ... ] PL_curinterp |0000000008138e88| B | OBJECT|0000000000000004| |.bss PL_do_undump |0000000008137910| D | OBJECT|0000000000000001| |.data PL_dollarzero_mutex |0000000008138e58| B | OBJECT|0000000000000018| |.bss PL_fold |000000000812c020| R | OBJECT|0000000000000100| |.rodata PL_fold_locale |0000000008135c80| D | OBJECT|0000000000000100| |.data PL_force_link_funcs |0000000008137da0| D | OBJECT|00000000000006c8| |.data PL_freq |000000000812c120| R | OBJECT|0000000000000100| |.rodata PL_hexdigit |0000000008137908| D | OBJECT|0000000000000004| |.data PL_memory_wrap |000000000812bfa3| R | OBJECT|0000000000000013| |.rodata [ ... ] PerlIO_setpos |0000000008112134| T | FUNC|00000000000000a6| |.text PerlIO_sprintf |00000000081122ee| T | FUNC|000000000000002a| |.text PerlIO_stdio |0000000008138720| D | OBJECT|0000000000000070| |.data PerlIO_stdoutf |0000000008112066| T | FUNC|000000000000003b| |.text [ ... ]

If you do create an object file from the C sourcecode:

    int x = 10;

and run that through nm, it gives:

$ nm -f sysv xxxx.o


Symbols from xxxx.o:

Name         Value            Class        Type         Size     Line  Section

x           |0000000000000000|   D  |    OBJECT|0000000000000004|     |.data

Note the Value field reported by it is the address of said symbol, not the contents of the variable.

If you run strip over it, this table is removed though and all nm tells you is "no symbols".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top