ack/util/amisc/anm.c

337 lines
6.3 KiB
C
Raw Permalink Normal View History

1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1994-06-24 11:31:16 +00:00
/* $Id$ */
1985-04-12 14:55:00 +00:00
/*
** print symbol tables for
** ACK object files
**
** anm [-gopruns] [name ...]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "object.h"
1985-04-12 14:55:00 +00:00
#include "out.h"
1991-11-18 09:46:53 +00:00
#include "arch.h"
#include "ranlib.h"
1985-04-12 14:55:00 +00:00
int numsort_flg;
int sectsort_flg;
int undef_flg;
int revsort_flg = 1;
int globl_flg;
int nosort_flg;
int arch_flg;
int prep_flg;
1987-01-15 22:06:16 +00:00
int read_error;
1985-04-12 14:55:00 +00:00
struct outhead hbuf;
struct outsect sbuf;
long off;
long s_base[S_MAX]; /* for specially encoded bases */
1987-01-15 22:06:16 +00:00
char *filename;
1991-11-18 09:46:53 +00:00
int narg;
1985-04-12 14:55:00 +00:00
2019-03-24 15:53:13 +00:00
static void process(FILE *);
static void do_file(FILE *);
2019-03-24 15:53:13 +00:00
int main(int argc, char **argv)
1985-04-12 14:55:00 +00:00
{
if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
argv++;
while (*++*argv) switch (**argv) {
case 'n': /* sort numerically */
numsort_flg++;
continue;
case 's': /* sort in section order */
sectsort_flg++;
continue;
case 'g': /* globl symbols only */
globl_flg++;
continue;
case 'u': /* undefined symbols only */
undef_flg++;
continue;
case 'r': /* sort in reverse order */
revsort_flg = -1;
continue;
case 'p': /* don't sort -- symbol table order */
nosort_flg++;
continue;
case 'o': /* prepend a name to each line */
prep_flg++;
continue;
default: /* oops */
fprintf(stderr, "anm: invalid argument -%c\n", *argv[0]);
exit(1);
}
argc--;
}
if (argc == 0) {
argc = 1;
argv[1] = "a.out";
}
narg = argc;
while(argc--) {
2019-03-24 15:53:13 +00:00
FILE *fd;
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
filename = *++argv;
2019-03-24 15:53:13 +00:00
if ((fd = fopen(filename, "rb")) == NULL) {
1991-11-18 09:46:53 +00:00
fprintf(stderr, "anm: cannot open %s\n", filename);
1985-04-12 14:55:00 +00:00
continue;
}
1991-11-18 09:46:53 +00:00
process(fd);
2019-03-24 15:53:13 +00:00
fclose(fd);
1991-11-18 09:46:53 +00:00
}
2019-03-24 15:53:13 +00:00
exit(EXIT_SUCCESS);
1991-11-18 09:46:53 +00:00
}
1985-04-12 14:55:00 +00:00
2019-03-24 15:53:13 +00:00
static void process(FILE *fd)
1991-11-18 09:46:53 +00:00
{
unsigned int magic;
long nextpos;
struct ar_hdr archive_header;
static char buf[sizeof(archive_header.ar_name)+1];
if (narg > 1) printf("\n%s:\n", filename);
magic = rd_unsigned2(fd);
switch(magic) {
case O_MAGIC:
2019-03-24 15:53:13 +00:00
fseek(fd, 0L, SEEK_SET);
1991-11-18 09:46:53 +00:00
do_file(fd);
break;
case ARMAG:
case AALMAG:
while (rd_arhdr(fd, &archive_header)) {
2019-03-24 15:53:13 +00:00
nextpos = ftell(fd) + archive_header.ar_size;
1991-11-18 09:46:53 +00:00
if (nextpos & 1) nextpos++;
strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
filename = buf;
if ( strcmp(filename, SYMDEF)) {
printf("\n%s:\n", filename);
do_file(fd);
}
2019-03-24 15:53:13 +00:00
fseek(fd, nextpos, SEEK_SET);
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
break;
default:
fprintf(stderr, "anm: %s -- bad format\n", filename);
break;
}
}
1985-04-12 14:55:00 +00:00
2019-03-24 15:53:13 +00:00
static void do_file(FILE *fd)
1991-11-18 09:46:53 +00:00
{
struct outname *nbufp = NULL;
struct outname nbuf;
char *cbufp;
long fi_to_co;
long n;
unsigned readcount;
int i,j;
int compare();
read_error = 0;
rd_fdopen(fd);
rd_ohead(&hbuf);
if (read_error) {
return;
}
if (BADMAGIC(hbuf)) {
return;
}
n = hbuf.oh_nname;
if (n == 0) {
fprintf(stderr, "anm: %s -- no name list\n", filename);
return;
}
if (hbuf.oh_nchar == 0) {
fprintf(stderr, "anm: %s -- no names\n", filename);
return;
}
if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar) {
fprintf(stderr, "anm: string area too big in %s\n", filename);
exit(2);
}
/* store special section bases ??? */
if (hbuf.oh_flags & HF_8086) {
rd_sect(&sbuf, hbuf.oh_nsect);
if (read_error) {
return;
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
for (i=0; i<hbuf.oh_nsect; i++) {
s_base[i+S_MIN] =
(sbuf.os_base>>12) & 03777760;
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
}
if ((cbufp = (char *)malloc(readcount)) == NULL) {
fprintf(stderr, "anm: out of memory on %s\n", filename);
exit(2);
}
rd_string(cbufp, hbuf.oh_nchar);
if (read_error) {
free(cbufp);
return;
}
fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
i = 0;
while (--n >= 0) {
rd_name(&nbuf, 1);
1987-01-15 22:06:16 +00:00
if (read_error) {
1991-11-18 09:46:53 +00:00
break;
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
if (globl_flg && (nbuf.on_type&S_EXT)==0)
continue;
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
if (undef_flg
&&
((nbuf.on_type&S_TYP)!=S_UND || (nbuf.on_type&S_ETC)!=0))
continue;
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
if (nbuf.on_foff == 0) nbuf.on_mptr = 0;
else nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
/* adjust value for specially encoded bases */
if (hbuf.oh_flags & HF_8086) {
if (((nbuf.on_type&S_ETC) == 0) ||
((nbuf.on_type&S_ETC) == S_SCT)) {
j = nbuf.on_type&S_TYP;
if ((j>=S_MIN) && (j<=S_MAX))
nbuf.on_valu += s_base[j];
}
}
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
if (nbufp == NULL)
nbufp = (struct outname *)malloc(sizeof(struct outname));
else
nbufp = (struct outname *)realloc(nbufp, (i+1)*sizeof(struct outname));
if (nbufp == NULL) {
fprintf(stderr, "anm: out of memory on %s\n", filename);
exit(2);
}
nbufp[i++] = nbuf;
}
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
if (nbufp && nosort_flg==0)
qsort(nbufp, i, sizeof(struct outname), compare);
for (n=0; n<i; n++) {
char cs1[4];
char cs2[4];
if (prep_flg)
printf("%s:", filename);
switch(nbufp[n].on_type&S_ETC) {
case S_SCT:
sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
sprintf(cs2, " S");
break;
case S_FIL:
sprintf(cs1, " -");
sprintf(cs2, " F");
break;
case S_MOD:
sprintf(cs1, " -");
sprintf(cs2, " M");
break;
case S_COM:
sprintf(cs1, " C");
if (nbufp[n].on_type&S_EXT)
sprintf(cs2, " E");
else
sprintf(cs2, " -");
break;
case 0:
if (nbufp[n].on_type&S_EXT)
sprintf(cs2, " E");
else
sprintf(cs2, " -");
1985-04-12 14:55:00 +00:00
1991-11-18 09:46:53 +00:00
switch(nbufp[n].on_type&S_TYP) {
case S_UND:
sprintf(cs1, " U");
1985-04-12 14:55:00 +00:00
break;
1991-11-18 09:46:53 +00:00
case S_ABS:
sprintf(cs1, " A");
1985-04-12 14:55:00 +00:00
break;
default:
1991-11-18 09:46:53 +00:00
sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
break;
default:
sprintf(cs1, "??");
sprintf(cs2, " ?");
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
printf("%8lx %s %s %s\n",nbufp[n].on_valu,cs1,cs2,nbufp[n].on_mptr ? nbufp[n].on_mptr : "(NULL)");
1985-04-12 14:55:00 +00:00
}
1991-11-18 09:46:53 +00:00
if (nbufp)
free((char *)nbufp);
if (cbufp)
free((char *)cbufp);
1985-04-12 14:55:00 +00:00
}
2019-03-24 15:53:13 +00:00
int compare(struct outname *p1, struct outname *p2)
1985-04-12 14:55:00 +00:00
{
int i;
if (sectsort_flg) {
if ((p1->on_type&S_TYP) > (p2->on_type&S_TYP))
return(revsort_flg);
if ((p1->on_type&S_TYP) < (p2->on_type&S_TYP))
return(-revsort_flg);
}
if (numsort_flg) {
if (p1->on_valu > p2->on_valu)
return(revsort_flg);
if (p1->on_valu < p2->on_valu)
return(-revsort_flg);
}
1991-08-27 09:55:25 +00:00
if (! p1->on_mptr) {
if (! p2->on_mptr) return 0;
return -revsort_flg;
}
if (! p2->on_mptr) return revsort_flg;
1985-04-12 14:55:00 +00:00
i = strcmp(p1->on_mptr, p2->on_mptr);
if (i > 0)
return(revsort_flg);
if (i < 0)
return(-revsort_flg);
return(0);
}
2019-03-24 15:53:13 +00:00
void rd_fatal(void)
1985-04-12 14:55:00 +00:00
{
1987-01-15 22:06:16 +00:00
fprintf(stderr,"read error on %s\n", filename);
read_error = 1;
1985-04-12 14:55:00 +00:00
}