www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/25/05:28:17

Date: Tue, 25 Feb 1997 12:14:08 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: "A.Appleyard" <A DOT APPLEYARD AT fs2 DOT mt DOT umist DOT ac DOT uk>
cc: djgpp AT delorie DOT com
Subject: Re: How to find all references?
In-Reply-To: <336072B37D7@fs2.mt.umist.ac.uk>
Message-ID: <Pine.SUN.3.91.970225121250.7156A-100000@is>
MIME-Version: 1.0

On Mon, 24 Feb 1997, A.Appleyard wrote:

> > Btw, I still think that NM is your answer.  Can you tell why you couldn't
> > make it help you?
> 
>   I just tried this simple example:-
> 
> int qwertyuiop(){return 1;}
> int asdfghjkl(){return 1;};
> main(){qwertyuiop();}
> 
> compiled with `gcc _.cc -Wall -g', and `NM -C -l -s' called on the resulting
> A.OUT gave an info list which said this about the three symbols visible here:-
> 
> 00001568 T asdfghjkl(void)	crt0.s:2
> ...
> 00001580 T main	crt0.s:3
> ...
> 00001550 T qwertyuiop(void)	crt0.s:1
> 
>   and nothing to show that qwertyuiop has been used and asdfghjkl
> has not.

Using `nm', you can limit the problem to only those cases where a
symbol is defined and used in the same source file.  To this end, run
`nm' on all your *.o files, then sort them by the symbol name (the 3rd
column of `nm' output).  Every symbol that has both `U' and one of
`T', `D', `C', `B', `G', or `S', is both defined *and* referenced (in
another module), and so can be eliminated from the list.  (If you need
such a procedure a lot, you can devise a Sed script, or an Awk
program, or even to write a small C program to do this elimination.)

What remains are only those symbols that are potentially defined and
used on the same file.  Those of them that aren't used can be found by
declaring them `static'.  You can use the pre-processor for this, like
so:

	#ifndef EXTERNAL
	#define EXTERNAL
	#endif
	...
	EXTERNAL int asdfghjkl(){return 1;};

Then compiling this with -DEXTERNAL=static and -O will make gcc yell
bloody murder for functions and variables that aren't used, while the
normal compilation without -DEXTERNAL effectively defines EXTERNAL
away.

You can also add the `--cref' option to the `ld' command line: it
causes the linker to print a cross-reference of symbols in the
program, where the first reference is the definition and the rest are
its uses.  Again, you can use that info to limit your problem to
symbols defined and used in the same .o file.  The `-y SYMBOL' option
is also worth exploring: for every symbol you need to know about, it
will print all the .o files where SYMBOL appears.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019