NetBSD/usr.bin/size/size.c

339 lines
8.0 KiB
C
Raw Normal View History

/* $NetBSD: size.c,v 1.10 1997/10/19 23:10:35 lukem Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
The Regents of the University of California. All rights reserved.\n");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)size.c 8.2 (Berkeley) 12/9/93";
#endif
__RCSID("$NetBSD: size.c,v 1.10 1997/10/19 23:10:35 lukem Exp $");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/file.h>
1993-03-21 12:45:37 +03:00
#include <a.out.h>
#include <ar.h>
#include <ctype.h>
#include <err.h>
#include <ranlib.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
1997-01-17 01:23:13 +03:00
unsigned long total_text, total_data, total_bss, total_total;
int ignore_bad_archive_entries = 1;
1997-01-17 01:23:13 +03:00
int print_totals = 0;
int main __P((int, char**));
int process_file __P((int, char *));
int show_archive __P((int, char *, FILE *));
int show_objfile __P((int, char *, FILE *));
void *emalloc __P((size_t));
void *erealloc __P((void *, size_t));
1993-08-07 08:35:30 +04:00
void usage __P((void));
1993-08-07 08:35:30 +04:00
int
main(argc, argv)
int argc;
char *argv[];
{
1993-08-07 08:35:30 +04:00
int ch, eval;
while ((ch = getopt(argc, argv, "wt")) != -1)
1993-08-07 08:35:30 +04:00
switch(ch) {
case 'w':
ignore_bad_archive_entries = 0;
break;
1997-01-17 01:23:13 +03:00
case 't':
print_totals = 1;
break;
1993-08-07 08:35:30 +04:00
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
1993-08-07 08:35:30 +04:00
eval = 0;
if (*argv)
do {
eval |= process_file(argc, *argv);
1993-08-07 08:35:30 +04:00
} while (*++argv);
else
eval |= process_file(1, "a.out");
1997-01-17 01:23:13 +03:00
if (print_totals)
(void)printf("\n%lu\t%lu\t%lu\t%lu\t%lx\tTOTAL\n",
total_text, total_data, total_bss,
total_total, total_total);
1993-08-07 08:35:30 +04:00
exit(eval);
}
/*
* process_file()
* show symbols in the file given as an argument. Accepts archive and
* object files as input.
*/
int
process_file(count, fname)
int count;
char *fname;
{
struct exec exec_head;
FILE *fp;
int retval;
char magic[SARMAG];
1997-01-17 01:23:13 +03:00
if (!(fp = fopen(fname, "r"))) {
warnx("cannot read %s", fname);
return(1);
}
/*
* first check whether this is an object file - read a object
* header, and skip back to the beginning
*/
if (fread((char *)&exec_head, sizeof(exec_head), (size_t)1, fp) != 1) {
warnx("%s: bad format", fname);
(void)fclose(fp);
return(1);
}
rewind(fp);
/* this could be an archive */
if (N_BADMAG(exec_head)) {
if (fread(magic, sizeof(magic), (size_t)1, fp) != 1 ||
strncmp(magic, ARMAG, SARMAG)) {
warnx("%s: not object file or archive", fname);
(void)fclose(fp);
return(1);
}
retval = show_archive(count, fname, fp);
} else
retval = show_objfile(count, fname, fp);
(void)fclose(fp);
return(retval);
}
/*
* show_archive()
* show symbols in the given archive file
*/
int
show_archive(count, fname, fp)
int count;
char *fname;
FILE *fp;
{
struct ar_hdr ar_head;
struct exec exec_head;
int i, rval;
long last_ar_off;
char *p, *name;
int baselen, namelen;
baselen = strlen(fname) + 3;
namelen = sizeof(ar_head.ar_name);
name = emalloc(baselen + namelen);
rval = 0;
/* while there are more entries in the archive */
while (fread((char *)&ar_head, sizeof(ar_head), (size_t)1, fp) == 1) {
/* bad archive entry - stop processing this archive */
if (strncmp(ar_head.ar_fmag, ARFMAG, sizeof(ar_head.ar_fmag))) {
warnx("%s: bad format archive header", fname);
(void)free(name);
return(1);
}
/* remember start position of current archive object */
last_ar_off = ftell(fp);
/* skip ranlib entries */
if (!strncmp(ar_head.ar_name, RANLIBMAG, sizeof(RANLIBMAG) - 1))
goto skip;
/*
* construct a name of the form "archive.a:obj.o:" for the
* current archive entry if the object name is to be printed
* on each output line
*/
p = name;
if (count > 1)
p += sprintf(p, "%s:", fname);
#ifdef AR_EFMT1
/*
* BSD 4.4 extended AR format: #1/<namelen>, with name as the
* first <namelen> bytes of the file
*/
if ( (ar_head.ar_name[0] == '#') &&
(ar_head.ar_name[1] == '1') &&
(ar_head.ar_name[2] == '/') &&
(isdigit(ar_head.ar_name[3]))) {
int len = atoi(&ar_head.ar_name[3]);
if (len > namelen) {
p -= (long)name;
name = (char *)erealloc(name, baselen+len);
namelen = len;
p += (long)name;
}
if (fread(p, len, 1, fp) != 1) {
(void)fprintf(stderr,
"nm: %s: premature EOF.\n", name);
(void)free(name);
return 1;
}
p += len;
} else
#endif
for (i = 0; i < sizeof(ar_head.ar_name); ++i)
if (ar_head.ar_name[i] && ar_head.ar_name[i] != ' ')
*p++ = ar_head.ar_name[i];
*p++ = '\0';
/* get and check current object's header */
if (fread((char *)&exec_head, sizeof(exec_head),
(size_t)1, fp) != 1) {
warnx("%s: premature EOF", name);
(void)free(name);
return(1);
}
if (N_BADMAG(exec_head)) {
if (!ignore_bad_archive_entries) {
warnx("%s: bad format", name);
rval = 1;
}
} else {
(void)fseek(fp, (long)-sizeof(exec_head),
SEEK_CUR);
rval |= show_objfile(2, name, fp);
}
/*
* skip to next archive object - it starts at the next
* even byte boundary
*/
#define even(x) (((x) + 1) & ~1)
skip: if (fseek(fp, last_ar_off + even(atol(ar_head.ar_size)),
SEEK_SET)) {
warn("%s", fname);
(void)free(name);
return(1);
}
}
(void)free(name);
return(rval);
}
1993-08-07 08:35:30 +04:00
int
show_objfile(count, name, fp)
1993-08-07 08:35:30 +04:00
int count;
char *name;
FILE *fp;
1993-03-21 12:45:37 +03:00
{
static int first = 1;
1993-03-21 12:45:37 +03:00
struct exec head;
u_long total;
if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
warnx("%s: cannot read header", name);
return(1);
1993-03-21 12:45:37 +03:00
}
if (N_BADMAG(head)) {
warnx("%s: bad format", name);
return(1);
}
1993-08-07 08:35:30 +04:00
if (first) {
first = 0;
1993-08-07 08:35:30 +04:00
(void)printf("text\tdata\tbss\tdec\thex\n");
}
total = head.a_text + head.a_data + head.a_bss;
1993-08-07 08:35:30 +04:00
(void)printf("%lu\t%lu\t%lu\t%lu\t%lx", head.a_text, head.a_data,
head.a_bss, total, total);
1993-08-07 08:35:30 +04:00
if (count > 1)
(void)printf("\t%s", name);
1997-01-17 01:23:13 +03:00
total_text += head.a_text;
total_data += head.a_data;
total_bss += head.a_bss;
total_total += total;
1993-08-07 08:35:30 +04:00
(void)printf("\n");
return (0);
}
void *
emalloc(size)
size_t size;
{
char *p;
/* NOSTRICT */
if ((p = malloc(size)) != NULL)
return(p);
err(1, "malloc");
}
void *
erealloc(p, size)
void *p;
size_t size;
{
/* NOSTRICT */
if ((p = realloc(p, size)) != NULL)
return(p);
err(1, "realloc");
}
1993-08-07 08:35:30 +04:00
void
usage()
{
(void)fprintf(stderr, "usage: size [-tw] file ...\n");
1993-08-07 08:35:30 +04:00
exit(1);
}