Commit | Line | Data |
---|---|---|
157569d9 | 1 | static char *sccsid = "@(#)symorder.c 4.2 (Berkeley) %G%"; |
faa0bd68 BJ |
2 | /* |
3 | * symorder - reorder symbol table | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <pagsiz.h> | |
7 | #include <sys/types.h> | |
8 | #include <stat.h> | |
13257757 | 9 | #include <a.out.h> |
faa0bd68 BJ |
10 | |
11 | #define SPACE 100 | |
12 | ||
13 | struct nlist order[SPACE]; | |
14 | ||
15 | char *savestr(); | |
16 | struct nlist nl1, nl2; | |
17 | struct exec exec; | |
18 | FILE *strf; | |
19 | off_t sa, ss; | |
20 | struct stat stb; | |
21 | int nsym = 0; | |
22 | int symfound = 0; | |
23 | char asym[BUFSIZ]; | |
24 | ||
25 | main(argc, argv) | |
26 | char **argv; | |
27 | { | |
28 | register struct nlist *p, *q; | |
29 | register FILE *f; | |
30 | register int na, i, j; | |
31 | int maxlen; | |
32 | int n, o; | |
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 | perror(argv[1]); | |
40 | exit(1); | |
41 | } | |
42 | maxlen = 0; | |
43 | for(p = order; fgets(asym, sizeof asym, f) != NULL; p++, nsym++) { | |
44 | for(i = 0; asym[i] && asym[i] != '\n'; i++) | |
45 | continue; | |
46 | if (asym[i] == '\n') | |
47 | asym[i] = 0; | |
48 | p->n_un.n_name = savestr(asym); | |
49 | if (maxlen < strlen(p->n_un.n_name)) | |
50 | maxlen = strlen(p->n_un.n_name); | |
51 | } | |
52 | fclose(f); | |
53 | if((f = fopen(argv[2], "r")) == NULL) | |
54 | perror(argv[2]), exit(1); | |
55 | if((strf = fopen(argv[2], "r")) == NULL) | |
56 | perror(argv[2]), exit(1); | |
57 | if((o = open(argv[2], 1)) < 0) | |
58 | perror(argv[2]), exit(1); | |
59 | if((fread(&exec, sizeof exec, 1, f)) != 1 || N_BADMAG(exec)) { | |
60 | fprintf(stderr, "symorder: %s: bad format\n", argv[2]); | |
61 | exit(1); | |
62 | } | |
63 | if (exec.a_syms == 0) { | |
64 | fprintf(stderr, "symorder: %s is stripped\n", argv[2]); | |
65 | exit(1); | |
66 | } | |
67 | fstat(fileno(f), &stb); | |
68 | if (stb.st_size < N_STROFF(exec)+sizeof(off_t)) { | |
69 | fprintf(stderr, "symorder: %s is in old format or truncated\n", argv[2]); | |
70 | exit(1); | |
71 | } | |
72 | sa = N_SYMOFF(exec); | |
73 | na = sa; | |
74 | ss = sa + exec.a_syms; | |
75 | fseek(f, sa, 0); | |
76 | n = exec.a_syms; | |
77 | while(n && symfound < nsym) { | |
78 | if(fread(&nl1, sizeof nl1, 1, f) != 1) { | |
79 | fprintf(stderr, "Short file "); perror(argv[2]); | |
80 | exit(1); | |
81 | } | |
82 | na += sizeof nl1; | |
83 | n -= sizeof nl1; | |
84 | if (nl1.n_un.n_strx == 0 || nl1.n_type & N_STAB) | |
85 | continue; | |
86 | fseek(strf, ss+nl1.n_un.n_strx, 0); | |
87 | fread(asym, maxlen+1, 1, strf); | |
88 | for(j = 0; j < nsym; j++) { | |
89 | for(i = 0; asym[i]; i++) | |
90 | if(asym[i] != order[j].n_un.n_name[i]) | |
91 | goto cont; | |
92 | if (order[j].n_un.n_name[i]) | |
93 | goto cont; | |
94 | if (order[j].n_value) | |
95 | goto cont; | |
96 | order[j].n_value = 1; | |
97 | fseek(f, (i = (sa+(j * sizeof nl1))), 0); | |
98 | if(fread(&nl2, sizeof nl2, 1, f) != 1) | |
99 | printf("Read err on 2nd asym\n"); | |
100 | lseek(o, i, 0); | |
101 | if(write(o, &nl1, sizeof nl1) == -1) | |
102 | perror("write1"); | |
103 | lseek(o, na-sizeof nl1, 0); | |
104 | if(write(o, &nl2, sizeof nl2) == -1) | |
105 | perror("write2"); | |
106 | fseek(f, 0, 0); | |
107 | fseek(f, na, 0); | |
108 | symfound++; | |
109 | break; | |
110 | cont: ; | |
111 | ||
112 | } | |
113 | } | |
114 | if(symfound < nsym) { | |
115 | fprintf(stderr, "%d symbol(s) not found:\n", nsym - symfound); | |
116 | for (i = 0; i < nsym; i++) { | |
117 | if (order[i].n_value == 0) | |
118 | printf("%s\n", order[i].n_un.n_name); | |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | #define NSAVETAB 4096 | |
124 | char *savetab; | |
125 | int saveleft; | |
126 | ||
127 | char * | |
128 | savestr(cp) | |
129 | register char *cp; | |
130 | { | |
131 | register int len; | |
132 | ||
133 | len = strlen(cp) + 1; | |
134 | if (len > saveleft) { | |
135 | saveleft = NSAVETAB; | |
136 | if (len > saveleft) | |
137 | saveleft = len; | |
138 | savetab = (char *)malloc(saveleft); | |
139 | if (savetab == 0) { | |
140 | fprintf(stderr, | |
141 | "symorder: ran out of memory (savestr)\n"); | |
142 | exit(1); | |
143 | } | |
144 | } | |
145 | strncpy(savetab, cp, len); | |
146 | cp = savetab; | |
147 | savetab += len; | |
148 | saveleft -= len; | |
149 | return (cp); | |
150 | } |