From: Keith Owens <kaos@ocs.com.au>
To: Chris Wedgwood <cw@f00f.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Re: crc32 and lib.a (was Re: [PATCH] nbd in 2.5.3 does
Date: Sat, 02 Feb 2002 19:08:38 +1100 [thread overview]
Message-ID: <25095.1012637318@ocs3.intra.ocs.com.au> (raw)
In-Reply-To: Your message of "Fri, 01 Feb 2002 23:30:20 -0800." <20020202073020.GA7014@tapu.f00f.org>
[-- Attachment #1: Type: text/plain, Size: 2323 bytes --]
cc: list trimmed.
On Fri, 1 Feb 2002 23:30:20 -0800,
Chris Wedgwood <cw@f00f.org> wrote:
>compile and link it, have the linker know main or some part of crt0 is
>special, build a graph of the final ELF object, see that bar and blem
>are not connected to 'main' and discard them?
>
>I'm pretty sure (but not 100% certain) that oher compilers can do
>this, maybe someone can test on other platforms?
>
>A really smart linker (if given enough compiler help) could build a
>directional graph and still remove this code even if blem called foo.
AFAICT it has to be done in two stages. Identify extern symbols that
are not referenced and convert them to static, then compile and let gcc
warn about unused statics and remove them.
The two stage approach is required because ld does not have enough
information to identify all function calls. ld views an object as
opaque blobs of text and data with relocation entries that tie them
together. When a function is called from the same source file, gcc/gas
tend to use PC relative branches for the inter-source call. PC
relative local branches do not usually generate relocation entries.
Without a relocation entry, ld cannot detect the inter-source call.
There is also a problem with exported symbols. To ld, EXPORT_SYMBOL
looks like a reference to the symbol, but the export entry is
irrelevant, what really matters is if any module refers to those
symbols. ld cannot distinguish between used and unused exported
symbols. To make matters worse, if you define foo as static and
EXPORT_SYMBOL(foo) in the same source then gcc thinks that the symbol
is used, because of the EXPORT_SYMBOL() reference. Again what really
matters is if any module uses the exported symbol.
The script below runs the .o files for the kernel and identifies extern
symbols with no extern references, i.e. the first part of the problem.
It knows about exported symbols and modules and handles them correctly.
I have not run the script for years, the list of conglomerates and the
list starting with mod_use_count_ will need updating.
Beware that you get a lot of false positives because of the kernel
config mechanism. A symbol may be listed as unused but changing the
config may cause it to be used. This is especially true for exported
symbols.
<davem>What, no initc**l reference?</davem>
[-- Attachment #2: namespace.pl --]
[-- Type: text/plain , Size: 7714 bytes --]
#!/usr/bin/perl -w
#
# namespace.pl. Mon Jan 27 22:25:47 EST 1997
#
# Perform a name space analysis on the linux kernel.
#
# Copyright Keith Owens <kaos@ocs.com.au>. GPL.
#
# Invoke by changing directory to the top of the kernel source
# tree then namespace.pl, no parameters.
#
# Tuned for 2.1.x kernels with the new module handling, it will
# work with 2.0 kernels as well. Last change 2.1.81.
#
# The source must be compiled/assembled first, the object files
# are the primary input to this script. Incomplete or missing
# objects will result in a flawed analysis.
#
# Even with complete objects, treat the result of the analysis
# with caution. Some external references are only used by
# certain architectures, others with certain combinations of
# configuration parameters. Ideally the source should include
# something like
#
# #ifndef CONFIG_...
# static
# #endif
# symbol_definition;
#
# so the symbols are defined as static unless a particular
# CONFIG_... requires it to be external.
#
require 5; # at least perl 5
use strict;
use File::Find;
my $nm = "/usr/bin/nm -p"; # in case somebody moves nm
if ($#ARGV != -1) {
print STDERR "usage: $0 takes no parameters\n";
die("giving up\n");
}
my %nmdata = (); # nm data for each object
my %def = (); # all definitions for each name
my %ksymtab = (); # names that appear in __ksymtab_
my %ref = (); # $ref{$name} exists if there is a true external reference to $name
my %export = (); # $export{$name} exists if there is an EXPORT_... of $name
&find(\&linux_objects, '.'); # find the objects and do_nm on them
list_multiply_defined();
resolve_external_references();
list_extra_externals();
exit(0);
sub linux_objects
{
# Select objects, ignoring objects which are only created by
# merging other objects. Also ignore all of modules, scripts
# and compressed.
my $basename = $_;
$_ = $File::Find::name;
s:^\./::;
if (/.*\.o$/ && ! (
m:/fs.o$: || m:/isofs.o$: || m:/nfs.o$:
|| m:/xiafs.o$: || m:/umsdos.o$: || m:/hpfs.o$:
|| m:/smbfs.o$: || m:/ncpfs.o$: || m:/ufs.o$:
|| m:/affs.o$: || m:/romfs.o$: || m:/kernel.o$:
|| m:/mm.o$: || m:/ipc.o$: || m:/ext.o$:
|| m:/msdos.o$: || m:/proc/proc.o$: || m:/minix.o$:
|| m:/ext2.o$: || m:/sysv.o$: || m:/fat.o$:
|| m:/vfat.o$: || m:/unix.o$: || m:/802.o$:
|| m:/appletalk.o$: || m:/ax25.o$: || m:/core.o$:
|| m:/ethernet.o$: || m:/ipv4.o$: || m:/ipx.o$:
|| m:/netrom.o$: || m:/ipv6.o$: || m:/x25.o$:
|| m:/rose.o$: || m:/bridge.o$: || m:/lapb.o$:
|| m:/sock_n_syms.o$: || m:/teles.o$: || m:/pcbit.o$:
|| m:/isdn.o$: || m:/ftape.o$: || m:/scsi_mod.o$:
|| m:/sd_mod.o$: || m:/sr_mod.o$: || m:/lowlevel.o$:
|| m:/sound.o$: || m:/piggy.o$: || m:/bootsect.o$:
|| m:/boot/setup.o$: || m:^modules/: || m:^scripts/:
|| m:/compressed/:
|| m:/autofs.o$: || m:/lockd/lockd.o$: || m:/nfsd.o$:
|| m:/sunrpc.o$: || m:/scsi_n_syms.o$:
|| m:/boot/bbootsect.o$: || m:/boot/bsetup.o$:
|| m:/misc/parport.o$: || m:/nls/nls.o$:
|| m:/debug/debug.o$: || m:/netlink/netlink.o$:
|| m:/sched/sched.o$: || m:/sound/sb.o$:
|| m:/sound/soundcore.o$: || m:/pci/pci_syms.o$:
|| m:/devpts/devpts.o$: || m:/video/fbdev.o$:
)
) {
do_nm($basename, $_);
}
$_ = $basename; # File::Find expects $_ untouched (undocumented)
}
sub do_nm
{
my ($basename, $fullname) = @_;
my ($source, $type, $name);
if (! -e $basename) {
printf STDERR "$basename does not exist\n";
return;
}
if ($fullname !~ /\.o$/) {
printf STDERR "$fullname is not an object file\n";
return;
}
$source = $basename;
$source =~ s/\.o$//;
if (! -e "$source.c" && ! -e "$source.S") {
printf STDERR "No source file found for $fullname\n";
return;
}
if (! open(NMDATA, "$nm $basename|")) {
printf STDERR "$nm $fullname failed $!\n";
return;
}
my @nmdata;
while (<NMDATA>) {
chop;
($type, $name) = (split(/ +/, $_, 3))[1..2];
# Expected types
# B weak external reference to data that has been resolved
# C global variable, uninitialised
# D global variable, initialised
# R global array, initialised
# T global label/procedure
# U external reference
# W weak external reference to text that has been resolved
# a assembler equate
# b static variable, uninitialised
# d static variable, initialised
# r static array, initialised
# t static label/procedures
# ? undefined type, used a lot by modules
if ($type !~ /^[BCDRTUWabdrt?]$/) {
printf STDERR "nm output for $fullname contains unknown type '$_'\n";
}
elsif ($name =~ /\./) {
# name with '.' is local static
}
else {
$name =~ s/_R[a-f0-9]{8}$//; # module versions adds this
if ($type =~ /[BCDRTW]/ &&
$name ne 'init_module' &&
$name ne 'cleanup_module' &&
$name ne 'Using_Versions' &&
$name !~ /^Version_[0-9]+$/ &&
$name ne 'kernel_version') {
if (!exists($def{$name})) {
$def{$name} = [];
}
push(@{$def{$name}}, $fullname);
}
push(@nmdata, "$type $name");
if ($name =~ /^__ksymtab_/) {
$name = substr($name, 10);
if (!exists($ksymtab{$name})) {
$ksymtab{$name} = [];
}
push(@{$ksymtab{$name}}, $fullname);
}
}
}
close(NMDATA);
if ($#nmdata < 0) {
printf "No nm data for $fullname\n";
return;
}
$nmdata{$fullname} = \@nmdata;
}
sub list_multiply_defined
{
my ($name, $module);
foreach $name (keys(%def)) {
if ($#{$def{$name}} > 0) {
printf "$name is multiply defined in :-\n";
foreach $module (@{$def{$name}}) {
printf "\t$module\n";
}
}
}
}
sub resolve_external_references
{
my ($object, $type, $name, $i, $j, $kstrtab, $ksymtab, $export);
printf "\n";
foreach $object (keys(%nmdata)) {
my $nmdata = $nmdata{$object};
for ($i = 0; $i <= $#{$nmdata}; ++$i) {
($type, $name) = split(' ', $nmdata->[$i], 2);
if ($type eq "U") {
if (exists($def{$name}) || exists($ksymtab{$name})) {
# add the owning object to the nmdata
$nmdata->[$i] = "$type $name $object";
# only count as a reference if it is not EXPORT_...
$kstrtab = "? __kstrtab_$name";
$ksymtab = "? __ksymtab_$name";
$export = 0;
for ($j = 0; $j <= $#{$nmdata}; ++$j) {
if ($nmdata->[$j] eq $kstrtab ||
$nmdata->[$j] eq $ksymtab) {
$export = 1;
last;
}
}
if ($export) {
$export{$name} = "";
}
else {
$ref{$name} = ""
}
}
elsif ($name ne "mod_use_count_" &&
$name ne "__this_module" &&
$name ne "_etext" &&
$name ne "_edata" &&
$name ne "_end" &&
$name ne "__start___ksymtab" &&
$name ne "__start___ex_table" &&
$name ne "__stop___ksymtab" &&
$name ne "__stop___ex_table" &&
$name ne "__stop___ex_table" &&
$name ne "__bss_start" &&
$name ne "_text" &&
$name ne "__init_begin" &&
$name ne "__init_end") {
printf "Cannot resolve reference to $name from $object\n";
}
}
}
}
}
sub list_extra_externals
{
my %noref = ();
my ($name, @module, $module, $export);
foreach $name (keys(%def)) {
if (! exists($ref{$name})) {
@module = @{$def{$name}};
foreach $module (@module) {
if (! exists($noref{$module})) {
$noref{$module} = [];
}
push(@{$noref{$module}}, $name);
}
}
}
if (%noref) {
printf "\nExternally defined symbols with no external references\n";
foreach $module (sort(keys(%noref))) {
printf " $module\n";
foreach (sort(@{$noref{$module}})) {
if (exists($export{$_})) {
$export = " (export only)";
}
else {
$export = "";
}
printf " $_$export\n";
}
}
}
}
next prev parent reply other threads:[~2002-02-02 8:09 UTC|newest]
Thread overview: 67+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-01-31 20:27 crc32 and lib.a (was Re: [PATCH] nbd in 2.5.3 does not Petr Vandrovec
2002-01-31 20:31 ` Jeff Garzik
2002-01-31 22:53 ` [PATCH] " Petr Vandrovec
2002-01-31 22:59 ` David S. Miller
2002-01-31 23:08 ` Jeff Garzik
2002-01-31 23:43 ` David Lang
2002-01-31 23:24 ` [PATCH] Re: crc32 and lib.a (was Re: [PATCH] nbd in 2.5.3 does Alan Cox
2002-01-31 23:21 ` Arnaldo Carvalho de Melo
2002-02-02 16:32 ` Denis Vlasenko
2002-02-02 12:57 ` Jens Axboe
2002-02-02 13:16 ` arjan
2002-02-02 13:52 ` Jens Axboe
2002-02-03 11:37 ` David Woodhouse
2002-01-31 23:43 ` Jeff Garzik
2002-02-01 8:14 ` David Woodhouse
2002-02-02 2:12 ` Chris Wedgwood
2002-02-02 3:01 ` Andrew Morton
2002-02-02 7:30 ` Chris Wedgwood
2002-02-02 7:42 ` Daniel Jacobowitz
2002-02-02 8:08 ` Jeff Garzik
2002-02-02 19:20 ` Daniel Jacobowitz
2002-02-02 8:06 ` Jeff Garzik
2002-02-02 8:08 ` Keith Owens [this message]
2002-02-02 8:40 ` David Woodhouse
2002-02-02 8:59 ` Keith Owens
2002-02-02 9:14 ` David Woodhouse
2002-02-03 4:14 ` Eric W. Biederman
2002-02-03 7:01 ` Ralf Baechle
2002-02-03 9:13 ` Chris Wedgwood
2002-02-03 12:16 ` David Woodhouse
2002-02-03 12:33 ` Chris Wedgwood
2002-02-03 12:47 ` David Woodhouse
2002-02-03 13:40 ` Alan Cox
2002-01-31 23:45 ` David S. Miller
2002-02-01 0:32 ` Alan Cox
2002-02-01 10:07 ` Horst von Brand
2002-02-01 10:28 ` Keith Owens
2002-02-01 11:03 ` David S. Miller
2002-02-01 11:25 ` Keith Owens
2002-02-01 14:56 ` Jeff Garzik
2002-02-01 0:42 Alan Cox
[not found] ` <20020131.162549.74750188.davem@redhat.com>
2002-02-01 0:30 ` David S. Miller
2002-02-01 3:46 ` Jeff Garzik
2002-02-01 4:18 ` H. Peter Anvin
2002-02-01 4:35 ` Jeff Garzik
2002-02-01 4:25 ` David S. Miller
2002-02-01 4:48 ` Jeff Garzik
2002-02-01 5:10 ` Keith Owens
2002-02-01 5:12 ` Jeff Garzik
2002-02-01 5:18 ` Keith Owens
2002-02-01 13:42 ` Horst von Brand
2002-02-03 23:34 ` Keith Owens
2002-02-04 20:14 ` Horst von Brand
2002-02-01 5:59 ` David S. Miller
2002-02-01 6:01 ` David S. Miller
2002-02-01 6:11 ` Keith Owens
2002-02-01 6:26 ` David S. Miller
2002-02-01 6:43 ` Keith Owens
2002-02-01 15:03 ` Alan Cox
2002-02-01 14:55 ` Jeff Garzik
2002-02-01 15:12 ` Petr Vandrovec
2002-02-01 16:08 ` David Woodhouse
2002-02-04 13:24 ` Horst von Brand
2002-02-05 7:51 ` Jeff Garzik
2002-02-01 15:19 ` Alan Cox
2002-02-01 19:37 ` Rob Landley
2002-02-01 19:50 ` Jeff Garzik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=25095.1012637318@ocs3.intra.ocs.com.au \
--to=kaos@ocs.com.au \
--cc=cw@f00f.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®