BSD 3 development
[unix-history] / usr / src / cmd / symorder.c
CommitLineData
c4a78bea
BJ
1/* symorder orderlist symbolfile
2 * orderlist is a file containing symbols to be found in symbolfile,
3 * 1 symbol per line.
4 * symbolfile is updated in place to put the requested symbols first
5 * in the symbol table, in the order specified. This is done
6 * by swapping the old symbols in the required spots with the
7 * new ones. If all of the order symbols are not found, an
8 * error is generated.
9 *
10 * Modelled after nlist.c the nlist subroutine, which has been
11 * modified to succeed as soon as all sought symbols are found.
12 *
13 * This program was specifically designed to cut down on the read
14 * overhead of systat(ss) when getting symbols from /unix.
15 */
16
17#include <stdio.h>
18#include <a.out.h>
19int a_magic[] = {A_MAGIC1, A_MAGIC2, A_MAGIC3, A_MAGIC4, 0};
20#define SPACE 100
21
22main(argc, argv)
23char *argv[];
24{
25 register struct nlist *p, *q;
26 register FILE *f;
27 register int sa, na, i, j;
28 int nsym = 0, symfound = 0, n, o;
29 struct nlist nl1, nl2;
30 char buf[20];
31 struct nlist order[SPACE];
32 struct exec exec;
33
34 if(argc != 3) {
35 fprintf(stderr, "Usage: symorder orderlist file\n");
36 exit(1);
37 }
38 if((f = fopen(argv[1], "r")) == NULL) {
39 fprintf(stderr, "Can't open "); perror(argv[1]);
40 exit(1);
41 }
42 for(p = order; fgets(buf, sizeof buf, f) != NULL; p++, nsym++)
43 for(i = 0; i < 8 && buf[i] != '\n'; i++)
44 p->n_name[i] = buf[i];
45 fclose(f);
46/*** for(i = 0; i < nsym; i++) ***/
47/*** printf("\"%.8s\"\n", order[i].n_name); ***/
48/*** printf("--------\n"); ***/
49 if((f = fopen(argv[2], "r")) == NULL) {
50 fprintf(stderr, "Can't open "); perror(argv[2]);
51 exit(1);
52 }
53 if((o = open(argv[2], 1)) < 0) {
54 fprintf(stderr, "Can't update "); perror(argv[2]);
55 exit(1);
56 }
57 if((fread(&exec, sizeof exec, 1, f)) != 1) {
58 fprintf(stderr, "Can't read "); perror(argv[2]);
59 exit(1);
60 }
61 for(i=0; a_magic[i]; i++)
62 if(a_magic[i] == exec.a_magic) break;
63 if(a_magic[i] == 0){
64 fprintf(stderr, "Bad Header on %s\n", argv[2]);
65 exit(1);
66 }
67 sa = exec.a_text + exec.a_data;
68 sa += exec.a_trsize + exec.a_drsize;
69 sa += sizeof exec;
70 na = sa;
71 fseek(f, sa, 0);
72 n = exec.a_syms;
73
74 while(n && symfound < nsym) {
75 if(fread(&nl1, sizeof nl1, 1, f) != 1) {
76 fprintf(stderr, "Short file "); perror(argv[2]);
77 exit(1);
78 }
79/*** printf("\"%.8s\"\n", nl1.n_name); ***/
80 na += sizeof nl1;
81 n -= sizeof nl1;
82/*** printf("Trying "); ***/
83 for(j = 0; j < nsym; j++) {
84/*** printf("%s ", order[j].n_name); ***/
85 for(i = 0; i < 8; i++)
86 if(nl1.n_name[i] != order[j].n_name[i])
87 goto cont;
88/*** printf("Found: %.8s\n", nl1.n_name); ***/
89 if (order[j].n_value)
90 goto cont;
91 order[j].n_value = 1;
92 fseek(f, (i = (sa+(j * sizeof nl1))), 0);
93 if(fread(&nl2, sizeof nl2, 1, f) != 1)
94 printf("Read err on 2nd sym\n");
95 lseek(o, i, 0);
96 if(write(o, &nl1, sizeof nl1) == -1)
97 perror("write1");
98 lseek(o, na-sizeof nl1, 0);
99 if(write(o, &nl2, sizeof nl2) == -1)
100 perror("write2");
101 fseek(f, 0, 0);
102 fseek(f, na, 0);
103 symfound++;
104 break;
105 cont: ;
106
107 }
108/*** printf("\n"); ***/
109 }
110 if(symfound < nsym) {
111 fprintf(stderr, "%d Syms not found:\n", nsym - symfound);
112 for (i = 0; i < nsym; i++) {
113 if (order[i].n_value == 0)
114 printf("%.8s\n", order[i].n_name);
115 }
116 }
117}