How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Pan ruochen
Hi All,

I use objdump to disassemble the ELF files. It can list symbolic names
for functions if referenced in branch and link instructions. But it
can show  symbolic names neither for global variables nor for
functions if used as points in the context of assignments, argument
passing and etc. As shown as below.
  ...
  70016c: 24050002 li a1,2
  700170: 0c1c002a jal 7000a8 <RegBitRead>
  700174: 24060002 li a2,2
  ...
  0070a064 l     O .bss 00000004 is_video
  ...
  703050: 3c020071 lui v0,0x71
  703054: 8c42a064 lw v0,-24476(v0) <-- It is a reference to the
global variable is_video in fact,

    but objdump doesn't show these messages
So, can I make objdump show all reference messages for functions and
global variables?

Best Regards
---------------
PRC
Dec 2, 2008
Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Brian Dessent
Pan ruochen wrote:

> So, can I make objdump show all reference messages for functions and
> global variables?

It sounds like you need to use the -r option to display relocs.

Brian
Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Nick Clifton
In reply to this post by Pan ruochen
Hi Pan,

>   703054: 8c42a064 lw v0,-24476(v0) <-- It is a reference to the
> global variable is_video in fact,
>
>     but objdump doesn't show these messages
 >
> So, can I make objdump show all reference messages for functions and
> global variables?

Brian mentioned displaying relocs alongside the disassembly, which will
help you with object files, but I suspect will not provide you with all
of the information you are hoping to see.

In essence though the answer is "no", because it is just too difficult
to recognise all references to global variables or functions.  A global
variable might be accessed directly or via a base address plus an
offset, or loaded into one register and then copied into another
register, or aliased via a union or type punning.  Functions can have
multiple entry points, or can be overlayed or even dynamically
constructed at run-time.

Cheers
   Nick




Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Pan ruochen
> In essence though the answer is "no", because it is just too difficult to
> recognise all references to global variables or functions.  A global
> variable might be accessed directly or via a base address plus an offset, or
> loaded into one register and then copied into another register, or aliased
> via a union or type punning.  Functions can have multiple entry points, or
> can be overlayed or even dynamically constructed at run-time.
>
> Cheers
>  Nick
>

It does be difficult to recoginse all direct or inredirect references
in all cases. But I just want objdump to recoginse all direct
references, like variable assignments and argument passing. I think
gcc keeps a reference table for each symbol during compilation since
gcc can give warnings about unused variables and functions. Maybe
these messages are discarded on linking.
My goal is to implement support for .init sections in my system. So I
want to design a tiny tool to find out all init functions
automatically. I think this can be achieved by the following approach:
1. The entry function is an init function.
2. A function is called only by init functions is an init function
Without accurate reference information, the results will not be exact.
It will be good if objdump can recoginse all direct references or
there exists some other tool which can parse the source and show all
references.


Best Regards
---------------
PRC
Dec 3, 2008
Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Nick Clifton
Hi Pan,

> It does be difficult to recoginse all direct or inredirect references
> in all cases. But I just want objdump to recoginse all direct
> references, like variable assignments and argument passing.

Please feel free to have a go at implementing this then.  For just
simple direct memory references it should not be too hard.

> I think
> gcc keeps a reference table for each symbol during compilation since
> gcc can give warnings about unused variables and functions. Maybe
> these messages are discarded on linking.

Yes.  In fact a lot of information that gcc holds about a program is
lost when it emits the assembler output.

> My goal is to implement support for .init sections in my system. So I
> want to design a tiny tool to find out all init functions
> automatically. I think this can be achieved by the following approach:
> 1. The entry function is an init function.
> 2. A function is called only by init functions is an init function

Well 2. might not necessarily be true.  For example if an init function
calls malloc() that does not necessarily mean that malloc() is an init
function.

What exactly do you mean by "support for .init sections" anyway ?  Are
you trying to perform some kind of garbage collection like the linkers
--gc-sections option ?

Cheers
   Nick


Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Pan ruochen
> Well 2. might not necessarily be true.  For example if an init function
> calls malloc() that does not necessarily mean that malloc() is an init
> function.
Well, maybe 2 can be fixed as:
2. A internal function within the program is called only by init
functions is an init function
Thus it can handle the case of malloc in init functions.

> What exactly do you mean by "support for .init sections" anyway ?  Are you
> trying to perform some kind of garbage collection like the linkers
> --gc-sections option ?

Support for .init sections means the kernel allocates pages from the
init sections when the programs are loaded and being initialized. And
after initialization is finished, the kernel reclaims the pages for
init sections. This can reduce memory requirements significantly for
an embedded system.

Best Regards
--------------
PRC
Dec 4, 2008
Reply | Threaded
Open this post in threaded view
|

Re: How to list all symbolic names for references to global varibables and functions with objdump (for MIPS arch)?

Nick Clifton
Hi Pan,

> Support for .init sections means the kernel allocates pages from the
> init sections when the programs are loaded and being initialized. And
> after initialization is finished, the kernel reclaims the pages for
> init sections. This can reduce memory requirements significantly for
> an embedded system.

Ah - OK - I understand now.  It sounds like what you really want to do
is to hook into the linker's section-garbage-collection mechanism.  This
currently allows the linker to find functions which are never used and
throw them away, (providing that the program has been compiled with
gcc's -ffunction-section command line option).

What you want to do is to augment the current code so that any function
which is used by which is not referenced via the program's entry
point(*) is placed into a special segment which the loader knows it can
discard after the initialization routines have been run.

Cheers
   Nick

(*) Actually you will also have to consider termination routines as well
(C++ deconstructors, .fini sections etc).  These may also invoke
functions in the program which are not referenced via the entry point,
but which need to be present when the program exits.  Possibly your
kernel will have to reload the segment containing the init routines.
Although if the program is exiting because it has run out of memory this
might prove difficult to implement...


You know it might be a whole lot easier to abandon trying to make this
an automatic mechanism and instead require that the program's creator
annotate any only-used-during-init functions via some kind of attribute.
Eg if you specified that any such function had to be given a section
name attribute of .init.text say, then you could arrange for your linker
script to keep all code in this section separate from the normal .text
section and then your kernel could know to discard this section once it
is ready to invoke the program's entry point.