Date: Tue, 25 Feb 1997 12:14:08 +0200 (IST) From: Eli Zaretskii To: "A.Appleyard" cc: djgpp AT delorie DOT com Subject: Re: How to find all references? In-Reply-To: <336072B37D7@fs2.mt.umist.ac.uk> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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.