objdump -r a.o not giving static function call

objdump -r a.o not giving static function call

Post by jeniffe » Thu, 30 Mar 2006 19:57:46



$ cat a.c
#include<stdio.h>

static void abc()
        {
        printf("hi");
        }

extern void pqr()
        {
        printf("hello");
        }
void f_called()
        {
        printf("df");
        }
void f_not_called()
        {
        printf("\nnt");
        }

 void Func_calling_static()
        {
        abc();
        }

int main()
        {
        abc();
        pqr();
        f_called();
        return 0;
        }
-------------
$ objdump -r a.o

a.o:     file format elf32-i386

RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE
0000000a R_386_32          .rodata
0000000f R_386_PC32        printf
00000022 R_386_32          .rodata
00000027 R_386_PC32        printf
0000003a R_386_32          .rodata
0000003f R_386_PC32        printf
00000052 R_386_32          .rodata
00000057 R_386_PC32        printf
0000008f R_386_PC32        pqr
00000094 R_386_PC32        f_called

abc() being a static function is not included in the ouptut of objdump
...I need to find out all the functions that are called..included
static ones..Is there any way?

 
 
 

objdump -r a.o not giving static function call

Post by Icarus Sparr » Fri, 31 Mar 2006 01:37:55



> $ cat a.c
> #include<stdio.h>

> static void abc()
>         {
>         printf("hi");
>         }

> extern void pqr()
>         {
>         printf("hello");
>         }
> void f_called()
>         {
>         printf("df");
>         }
> void f_not_called()
>         {
>         printf("\nnt");
>         }

>  void Func_calling_static()
>         {
>         abc();
>         }

> int main()
>         {
>         abc();
>         pqr();
>         f_called();
>         return 0;
>         }
> -------------
> $ objdump -r a.o

> a.o:     file format elf32-i386

> RELOCATION RECORDS FOR [.text]:
> OFFSET   TYPE              VALUE
> 0000000a R_386_32          .rodata
> 0000000f R_386_PC32        printf
> 00000022 R_386_32          .rodata
> 00000027 R_386_PC32        printf
> 0000003a R_386_32          .rodata
> 0000003f R_386_PC32        printf
> 00000052 R_386_32          .rodata
> 00000057 R_386_PC32        printf
> 0000008f R_386_PC32        pqr
> 00000094 R_386_PC32        f_called

> abc() being a static function is not included in the ouptut of objdump
> ...I need to find out all the functions that are called..included static
> ones..Is there any way?

What you are asking for when you use 'objdump -r' is the relocation
information. This has almost nothing to do with function calls. It is just
that a function call to an external function will require the "call"
instruction to be adjusted at link time (either the explicit link step or
the runtime linker) and so has a relocation element associated with it.

When you call a function in the same file you *may* be able to do the call
as a program counter relative call, and if so you will not need a
relocation record.

It is also possible that the compiler has just automatically inlined your
function so it no longer exists. If a function is "static" and only used
in one place then this is a sensible thing for it to do, it will never be
slower or bigger, and usually will be smaller and faster.

objdump -r is the wrong tool for this job.

I have already suggested that you look at 'cflow', which is a lot closer
to doing what you want.

If you want to pursue using 'objdump, then "objdump -d" or "objdump -t" is
a better choice.

 
 
 

objdump -r a.o not giving static function call

Post by jeniffe » Fri, 31 Mar 2006 11:46:25


Icarus Sparry wrote:
> On Wed, 29 Mar 2006 02:57:46 -0800, jeniffer wrote:

> > $ cat a.c
> > #include<stdio.h>

> > static void abc()
> >         {
> >         printf("hi");
> >         }

> > extern void pqr()
> >         {
> >         printf("hello");
> >         }
> > void f_called()
> >         {
> >         printf("df");
> >         }
> > void f_not_called()
> >         {
> >         printf("\nnt");
> >         }

> >  void Func_calling_static()
> >         {
> >         abc();
> >         }

> > int main()
> >         {
> >         abc();
> >         pqr();
> >         f_called();
> >         return 0;
> >         }
> > -------------
> > $ objdump -r a.o

> > a.o:     file format elf32-i386

> > RELOCATION RECORDS FOR [.text]:
> > OFFSET   TYPE              VALUE
> > 0000000a R_386_32          .rodata
> > 0000000f R_386_PC32        printf
> > 00000022 R_386_32          .rodata
> > 00000027 R_386_PC32        printf
> > 0000003a R_386_32          .rodata
> > 0000003f R_386_PC32        printf
> > 00000052 R_386_32          .rodata
> > 00000057 R_386_PC32        printf
> > 0000008f R_386_PC32        pqr
> > 00000094 R_386_PC32        f_called

> > abc() being a static function is not included in the ouptut of objdump
> > ...I need to find out all the functions that are called..included static
> > ones..Is there any way?

> What you are asking for when you use 'objdump -r' is the relocation
> information. This has almost nothing to do with function calls. It is just
> that a function call to an external function will require the "call"
> instruction to be adjusted at link time (either the explicit link step or
> the runtime linker) and so has a relocation element associated with it.

> When you call a function in the same file you *may* be able to do the call
> as a program counter relative call, and if so you will not need a
> relocation record.

> It is also possible that the compiler has just automatically inlined your
> function so it no longer exists. If a function is "static" and only used
> in one place then this is a sensible thing for it to do, it will never be
> slower or bigger, and usually will be smaller and faster.

> objdump -r is the wrong tool for this job.

> I have already suggested that you look at 'cflow', which is a lot closer
> to doing what you want.

> If you want to pursue using 'objdump, then "objdump -d" or "objdump -t" is
> a better choice.

Thanks i understood that.
nm tool with the -r option gives functions that are defined in that
file or defined in the library/external file [ if we grep T,t,U ]....Is
is an appropriate method or has some problem?
my aim :From a set of .o files I need to find out those functions that
are
defined but not called from anywhere. I had firstly use nm tool (-r
option)
to get all functions [T,t,U] ,then used objdump -r (this was wrong) to
get all functions that are called so as to get required functions .
cflow is not working in the environment i work and I am not
understanding how will objdump - t or objdump -d help me to get the
required functions:-
$ objdump -t a.o|more

a.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 a.c
00000000 l    d  .text  00000000
00000000 l    d  .data  00000000
00000000 l    d  .bss   00000000
00000000 l    d  .rodata        00000000
00000000 l     F .text  00000018 abc
00000000 l    d  .note.GNU-stack        00000000
00000000 l    d  .comment       00000000
00000000         *UND*  00000000 printf
00000018 g     F .text  00000018 pqr
00000030 g     F .text  00000018 f_called
00000048 g     F .text  00000018 f_not_called
00000060 g     F .text  0000000d Func_calling_static
0000006d g     F .text  00000026 main

-----------
objdump -d a.o :-

a.o:     file format elf32-i386

Disassembly of section .text:

00000000 <abc>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 08                sub    $0x8,%esp
   6:   83 ec 0c                sub    $0xc,%esp
   9:   68 00 00 00 00          push   $0x0
   e:   e8 fc ff ff ff          call   f <abc+0xf>
  13:   83 c4 10                add    $0x10,%esp
  16:   c9                      leave
  17:   c3                      ret

00000018 <pqr>:
  18:   55                      push   %ebp
  19:   89 e5                   mov    %esp,%ebp
  1b:   83 ec 08                sub    $0x8,%esp
  1e:   83 ec 0c                sub    $0xc,%esp
  21:   68 03 00 00 00          push   $0x3
  26:   e8 fc ff ff ff          call   27 <pqr+0xf>
  2b:   83 c4 10                add    $0x10,%esp
  2e:   c9                      leave
  2f:   c3                      ret

00000030 <f_called>:
  30:   55                      push   %ebp
  31:   89 e5                   mov    %esp,%ebp
  33:   83 ec 08                sub    $0x8,%esp
  36:   83 ec 0c                sub    $0xc,%esp
  39:   68 09 00 00 00          push   $0x9
  3e:   e8 fc ff ff ff          call   3f <f_called+0xf>
  43:   83 c4 10                add    $0x10,%esp
  46:   c9                      leave
  47:   c3                      ret

00000048 <f_not_called>:
  48:   55                      push   %ebp
  49:   89 e5                   mov    %esp,%ebp
  4b:   83 ec 08                sub    $0x8,%esp
  4e:   83 ec 0c                sub    $0xc,%esp
  51:   68 0c 00 00 00          push   $0xc
  56:   e8 fc ff ff ff          call   57 <f_not_called+0xf>
  5b:   83 c4 10                add    $0x10,%esp
  5e:   c9                      leave
  5f:   c3                      ret

00000060 <Func_calling_static>:
  60:   55                      push   %ebp
  61:   89 e5                   mov    %esp,%ebp
  63:   83 ec 08                sub    $0x8,%esp
  66:   e8 95 ff ff ff          call   0 <abc>
  6b:   c9                      leave
  6c:   c3                      ret

0000006d <main>:
  6d:   55                      push   %ebp
  6e:   89 e5                   mov    %esp,%ebp
  70:   83 ec 08                sub    $0x8,%esp
  73:   83 e4 f0                and    $0xfffffff0,%esp
  76:   b8 00 00 00 00          mov    $0x0,%eax
  7b:   29 c4                   sub    %eax,%esp
  7d:   e8 7e ff ff ff          call   0 <abc>
  82:   e8 fc ff ff ff          call   83 <main+0x16>
  87:   e8 fc ff ff ff          call   88 <main+0x1b>
  8c:   b8 00 00 00 00          mov    $0x0,%eax
  91:   c9                      leave
  92:   c3                      ret

 
 
 

objdump -r a.o not giving static function call

Post by Icarus Sparr » Fri, 31 Mar 2006 15:29:43





>> > $ cat a.c
>> > #include<stdio.h>

>> > static void abc()
>> >         {
>> >         printf("hi");
>> >         }

>> > extern void pqr()
>> >         {
>> >         printf("hello");
>> >         }
>> > void f_called()
>> >         {
>> >         printf("df");
>> >         }
>> > void f_not_called()
>> >         {
>> >         printf("\nnt");
>> >         }

>> >  void Func_calling_static()
>> >         {
>> >         abc();
>> >         }

>> > int main()
>> >         {
>> >         abc();
>> >         pqr();
>> >         f_called();
>> >         return 0;
>> >         }
>> > -------------
>> > $ objdump -r a.o

>> > a.o:     file format elf32-i386

>> > RELOCATION RECORDS FOR [.text]:
>> > OFFSET   TYPE              VALUE
>> > 0000000a R_386_32          .rodata
>> > 0000000f R_386_PC32        printf
>> > 00000022 R_386_32          .rodata
>> > 00000027 R_386_PC32        printf
>> > 0000003a R_386_32          .rodata
>> > 0000003f R_386_PC32        printf
>> > 00000052 R_386_32          .rodata
>> > 00000057 R_386_PC32        printf
>> > 0000008f R_386_PC32        pqr
>> > 00000094 R_386_PC32        f_called

>> > abc() being a static function is not included in the ouptut of
>> > objdump ...I need to find out all the functions that are
>> > called..included static ones..Is there any way?

>> What you are asking for when you use 'objdump -r' is the relocation
>> information. This has almost nothing to do with function calls. It is
>> just that a function call to an external function will require the
>> "call" instruction to be adjusted at link time (either the explicit
>> link step or the runtime linker) and so has a relocation element
>> associated with it.

>> When you call a function in the same file you *may* be able to do the
>> call as a program counter relative call, and if so you will not need a
>> relocation record.

>> It is also possible that the compiler has just automatically inlined
>> your function so it no longer exists. If a function is "static" and
>> only used in one place then this is a sensible thing for it to do, it
>> will never be slower or bigger, and usually will be smaller and faster.

>> objdump -r is the wrong tool for this job.

>> I have already suggested that you look at 'cflow', which is a lot
>> closer to doing what you want.

>> If you want to pursue using 'objdump, then "objdump -d" or "objdump -t"
>> is a better choice.

> Thanks i understood that.
> nm tool with the -r option gives functions that are defined in that file
> or defined in the library/external file [ if we grep T,t,U ]....Is is an
> appropriate method or has some problem?

nm -r is not a suitable command to run. I am not sure what you expect the
'-r' to do, but I am confident that it is not to 'reverse the sort'.

Quote:> my aim :From a set of .o files I need to find out those functions that
> are
> defined but not called from anywhere. I had firstly use nm tool (-r
> option)
> to get all functions [T,t,U] ,then used objdump -r (this was wrong) to
> get all functions that are called so as to get required functions .
> cflow is not working in the environment i work and I am not
> understanding how will objdump - t or objdump -d help me to get the
> required functions:-

You have two choices with this approach, either you accept that there are
some functions that are never called but you don't notice this, or else
you wrongly conclude that a function is not called when in fact it is.

If you are using this as a tool to tell you what to remove from a file,
then you probably want to remove too little rather than too much.

An "nm" based approach will tend to tell you that a function can be called
when in fact it cannot. Consider a file with 4 functions main, a, b, and
c. main calls a. b & c are mutually recursive. However an 'nm' based
approach will deduce that a, b, and c are all called. 'main' is called
(almost by definition) so an nm based approach will report that all 4
functions are called. However b & c are never called.

You would be much better off by getting cflow working than trying to build
something like cflow out of objdump and/or nm.

If you want to follow an objdump approach, then you need to see for each
function what it calls. For direct calls this is easy, but it is hard for
indirect calls (those which go via a function pointer). Once you have this
information, then you can start at 'main' and add all the functions which
main calls, and then add all the functions that they call, and so on
recursively. When you get to the stage of not adding any more functions
then any function that is not in the reachable set is never called.

For an "nm" based approach you need to see for a set of files which
symbols are "T". For each of these see if any other file reports the same
symbol as "U". If it doesn't, then the function can be made "static".
Change it, then recompile with at least "-O1" optimization to gcc. If it
removes the function then it is not needed.

 
 
 

1. how to know the instruction address of calling function within called function?

Suppose function A call B, are there any ways to know the address of last instruction in A just
before calling B from within B? I would like to do some profiling by substituting B by a routine with record
the time spent on B. I also would like to know the time spent on B in different locations of A
where B is called. If I can know the address of last instruction in A just before calling B, from
debuging info of the excutables, I can find its source code address, therefore know the
time spent on B in differnet locations of A.

I don't want to make any changes to A. "-pg" compiler option for profiling is of no use either,
because there is no source code for B, and there is no object code for B with profiling codes in,
so you can't know the total time spent on B, but only the self time spent on B.

Thanks very much.
--



Kingston, Ontario K7L 3N6        Tel: (613) 545-2723 (o)
Canada                           Fax: (613) 545-6463
Home address:
58 1/2 Chatham St.               Tel: (613) 547-1353 (h)
Kingston, ON K7K 4G8

2. Guidance needed about my SS20

3. Browser calls CGI C function which sets an env var and call a c function crashes

4. seting up a external modem ans ppp

5. gettimeofday system function giving bad system call coredump

6. X is dead

7. "static declaration for `function' follows non-static"????

8. Help with Solstice X.25

9. static functions in library matching global functions

10. static constructor from shared library not getting called on aix when loaded by Apache Web Server

11. Static constructors not called for C++ shared library

12. problem compiling kernel, objdump gives error!! please help

13. Constructors of static objects not called (VAC++5.0)