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