1 /* $Id: btfixupprep.c,v 1.6 2001/08/22 15:27:47 davem Exp $
2 Simple utility to prepare vmlinux image for sparc.
3 Resolves all BTFIXUP uses and settings and creates
4 a special .s object to link to the image.
5
6 Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29
30 #define MAXSYMS 1024
31
32 static char *symtab = "SYMBOL TABLE:";
33 static char *relrec = "RELOCATION RECORDS FOR [";
34 static int rellen;
35 static int symlen;
36 int mode;
37
38 struct _btfixup;
39
40 typedef struct _btfixuprel {
41 char *sect;
42 unsigned long offset;
43 struct _btfixup *f;
44 int frel;
45 struct _btfixuprel *next;
46 } btfixuprel;
47
48 typedef struct _btfixup {
49 int type;
50 int setinitval;
51 unsigned int initval;
52 char *initvalstr;
53 char *name;
54 btfixuprel *rel;
55 } btfixup;
56
57 btfixup array[MAXSYMS];
58 int last = 0;
59 char buffer[1024];
60 unsigned long lastfoffset = -1;
61 unsigned long lastfrelno;
62 btfixup *lastf;
63
64 void fatal(void) __attribute__((noreturn));
fatal(void)65 void fatal(void)
66 {
67 fprintf(stderr, "Malformed output from objdump\n%s\n", buffer);
68 exit(1);
69 }
70
find(int type,char * name)71 btfixup *find(int type, char *name)
72 {
73 int i;
74 for (i = 0; i < last; i++) {
75 if (array[i].type == type && !strcmp(array[i].name, name))
76 return array + i;
77 }
78 array[last].type = type;
79 array[last].name = strdup(name);
80 array[last].setinitval = 0;
81 if (!array[last].name) fatal();
82 array[last].rel = NULL;
83 last++;
84 if (last >= MAXSYMS) {
85 fprintf(stderr, "Ugh. Something strange. More than %d different BTFIXUP symbols\n", MAXSYMS);
86 exit(1);
87 }
88 return array + last - 1;
89 }
90
set_mode(char * buffer)91 void set_mode (char *buffer)
92 {
93 for (mode = 0;; mode++)
94 if (buffer[mode] < '0' || buffer[mode] > '9')
95 break;
96 if (mode != 8 && mode != 16)
97 fatal();
98 }
99
100
main(int argc,char ** argv)101 int main(int argc,char **argv)
102 {
103 char *p, *q;
104 char *sect;
105 int i, j, k;
106 unsigned int initval;
107 int shift;
108 btfixup *f;
109 btfixuprel *r, **rr;
110 unsigned long offset;
111 char *initvalstr;
112
113 symlen = strlen(symtab);
114 while (fgets (buffer, 1024, stdin) != NULL)
115 if (!strncmp (buffer, symtab, symlen))
116 goto main0;
117 fatal();
118 main0:
119 rellen = strlen(relrec);
120 while (fgets (buffer, 1024, stdin) != NULL)
121 if (!strncmp (buffer, relrec, rellen))
122 goto main1;
123 fatal();
124 main1:
125 sect = malloc(strlen (buffer + rellen) + 1);
126 if (!sect) fatal();
127 strcpy (sect, buffer + rellen);
128 p = strchr (sect, ']');
129 if (!p) fatal();
130 *p = 0;
131 if (fgets (buffer, 1024, stdin) == NULL)
132 fatal();
133 while (fgets (buffer, 1024, stdin) != NULL) {
134 int nbase;
135 if (!strncmp (buffer, relrec, rellen))
136 goto main1;
137 if (mode == 0)
138 set_mode (buffer);
139 p = strchr (buffer, '\n');
140 if (p) *p = 0;
141 if (strlen (buffer) < 22+mode)
142 continue;
143 if (strncmp (buffer + mode, " R_SPARC_", 9))
144 continue;
145 nbase = 27 - 8 + mode;
146 if (buffer[nbase] != '_' || buffer[nbase+1] != '_' || buffer[nbase+2] != '_')
147 continue;
148 switch (buffer[nbase+3]) {
149 case 'f': /* CALL */
150 case 'b': /* BLACKBOX */
151 case 's': /* SIMM13 */
152 case 'a': /* HALF */
153 case 'h': /* SETHI */
154 case 'i': /* INT */
155 break;
156 default:
157 continue;
158 }
159 p = strchr (buffer + nbase+5, '+');
160 if (p) *p = 0;
161 shift = nbase + 5;
162 if (buffer[nbase+4] == 's' && buffer[nbase+5] == '_') {
163 shift = nbase + 6;
164 if (strcmp (sect, ".text.init")) {
165 fprintf(stderr, "Wrong use of '%s' BTFIXUPSET.\nBTFIXUPSET_CALL can be used only in __init sections\n", buffer+shift);
166 exit(1);
167 }
168 } else if (buffer[nbase+4] != '_')
169 continue;
170 if (!strcmp (sect, ".text.exit"))
171 continue;
172 if (strcmp (sect, ".text") && strcmp (sect, ".text.init") && strcmp (sect, ".fixup") && (strcmp (sect, "__ksymtab") || buffer[nbase+3] != 'f')) {
173 if (buffer[nbase+3] == 'f')
174 fprintf(stderr, "Wrong use of '%s' in '%s' section. It can be only used in .text, .text.init, .fixup and __ksymtab\n", buffer + shift, sect);
175 else
176 fprintf(stderr, "Wrong use of '%s' in '%s' section. It can be only used in .text, .fixup and .text.init\n", buffer + shift, sect);
177 exit(1);
178 }
179 p = strstr (buffer + shift, "__btset_");
180 if (p && buffer[nbase+4] == 's') {
181 fprintf(stderr, "__btset_ in BTFIXUP name can only be used when defining the variable, not for setting\n%s\n", buffer);
182 exit(1);
183 }
184 initval = 0;
185 initvalstr = NULL;
186 if (p) {
187 if (p[8] != '0' || p[9] != 'x') {
188 fprintf(stderr, "Pre-initialized values can be only initialized with hexadecimal constants starting 0x\n%s\n", buffer);
189 exit(1);
190 }
191 initval = strtoul(p + 10, &q, 16);
192 if (*q || !initval) {
193 fprintf(stderr, "Pre-initialized values can be only in the form name__btset_0xXXXXXXXX where X are hex digits.\nThey cannot be name__btset_0x00000000 though. Use BTFIXUPDEF_XX instead of BTFIXUPDEF_XX_INIT then.\n%s\n", buffer);
194 exit(1);
195 }
196 initvalstr = p + 10;
197 *p = 0;
198 }
199 f = find(buffer[nbase+3], buffer + shift);
200 if (buffer[nbase+4] == 's')
201 continue;
202 switch (buffer[nbase+3]) {
203 case 'f':
204 if (initval) {
205 fprintf(stderr, "Cannot use pre-initalized fixups for calls\n%s\n", buffer);
206 exit(1);
207 }
208 if (!strcmp (sect, "__ksymtab")) {
209 if (strncmp (buffer + mode+9, "32 ", 10)) {
210 fprintf(stderr, "BTFIXUP_CALL in EXPORT_SYMBOL results in relocation other than R_SPARC_32\n\%s\n", buffer);
211 exit(1);
212 }
213 } else if (strncmp (buffer + mode+9, "WDISP30 ", 10) &&
214 strncmp (buffer + mode+9, "HI22 ", 10) &&
215 strncmp (buffer + mode+9, "LO10 ", 10)) {
216 fprintf(stderr, "BTFIXUP_CALL results in relocation other than R_SPARC_WDISP30, R_SPARC_HI22 or R_SPARC_LO10\n%s\n", buffer);
217 exit(1);
218 }
219 break;
220 case 'b':
221 if (initval) {
222 fprintf(stderr, "Cannot use pre-initialized fixups for blackboxes\n%s\n", buffer);
223 exit(1);
224 }
225 if (strncmp (buffer + mode+9, "HI22 ", 10)) {
226 fprintf(stderr, "BTFIXUP_BLACKBOX results in relocation other than R_SPARC_HI22\n%s\n", buffer);
227 exit(1);
228 }
229 break;
230 case 's':
231 if (initval + 0x1000 >= 0x2000) {
232 fprintf(stderr, "Wrong initializer for SIMM13. Has to be from $fffff000 to $00000fff\n%s\n", buffer);
233 exit(1);
234 }
235 if (strncmp (buffer + mode+9, "13 ", 10)) {
236 fprintf(stderr, "BTFIXUP_SIMM13 results in relocation other than R_SPARC_13\n%s\n", buffer);
237 exit(1);
238 }
239 break;
240 case 'a':
241 if (initval + 0x1000 >= 0x2000 && (initval & 0x3ff)) {
242 fprintf(stderr, "Wrong initializer for HALF.\n%s\n", buffer);
243 exit(1);
244 }
245 if (strncmp (buffer + mode+9, "13 ", 10)) {
246 fprintf(stderr, "BTFIXUP_HALF results in relocation other than R_SPARC_13\n%s\n", buffer);
247 exit(1);
248 }
249 break;
250 case 'h':
251 if (initval & 0x3ff) {
252 fprintf(stderr, "Wrong initializer for SETHI. Cannot have set low 10 bits\n%s\n", buffer);
253 exit(1);
254 }
255 if (strncmp (buffer + mode+9, "HI22 ", 10)) {
256 fprintf(stderr, "BTFIXUP_SETHI results in relocation other than R_SPARC_HI22\n%s\n", buffer);
257 exit(1);
258 }
259 break;
260 case 'i':
261 if (initval) {
262 fprintf(stderr, "Cannot use pre-initalized fixups for INT\n%s\n", buffer);
263 exit(1);
264 }
265 if (strncmp (buffer + mode+9, "HI22 ", 10) && strncmp (buffer + mode+9, "LO10 ", 10)) {
266 fprintf(stderr, "BTFIXUP_INT results in relocation other than R_SPARC_HI22 and R_SPARC_LO10\n%s\n", buffer);
267 exit(1);
268 }
269 break;
270 }
271 if (!f->setinitval) {
272 f->initval = initval;
273 if (initvalstr) {
274 f->initvalstr = strdup(initvalstr);
275 if (!f->initvalstr) fatal();
276 }
277 f->setinitval = 1;
278 } else if (f->initval != initval) {
279 fprintf(stderr, "Btfixup %s previously used with initializer %s which doesn't match with current initializer\n%s\n",
280 f->name, f->initvalstr ? : "0x00000000", buffer);
281 exit(1);
282 } else if (initval && strcmp(f->initvalstr, initvalstr)) {
283 fprintf(stderr, "Btfixup %s previously used with initializer %s which doesn't match with current initializer.\n"
284 "Initializers have to match literally as well.\n%s\n",
285 f->name, f->initvalstr, buffer);
286 exit(1);
287 }
288 offset = strtoul(buffer, &q, 16);
289 if (q != buffer + mode || (!offset && (mode == 8 ? strncmp (buffer, "00000000 ", 9) : strncmp (buffer, "0000000000000000 ", 17)))) {
290 fprintf(stderr, "Malformed relocation address in\n%s\n", buffer);
291 exit(1);
292 }
293 for (k = 0, r = f->rel, rr = &f->rel; r; rr = &r->next, r = r->next, k++)
294 if (r->offset == offset && !strcmp(r->sect, sect)) {
295 fprintf(stderr, "Ugh. One address has two relocation records\n");
296 exit(1);
297 }
298 *rr = malloc(sizeof(btfixuprel));
299 if (!*rr) fatal();
300 (*rr)->offset = offset;
301 (*rr)->f = NULL;
302 if (buffer[nbase+3] == 'f') {
303 lastf = f;
304 lastfoffset = offset;
305 lastfrelno = k;
306 } else if (lastfoffset + 4 == offset) {
307 (*rr)->f = lastf;
308 (*rr)->frel = lastfrelno;
309 }
310 (*rr)->sect = sect;
311 (*rr)->next = NULL;
312 }
313 printf("! Generated by btfixupprep. Do not edit.\n\n");
314 printf("\t.section\t\".data.init\",#alloc,#write\n\t.align\t4\n\n");
315 printf("\t.global\t___btfixup_start\n___btfixup_start:\n\n");
316 for (i = 0; i < last; i++) {
317 f = array + i;
318 printf("\t.global\t___%cs_%s\n", f->type, f->name);
319 if (f->type == 'f')
320 printf("___%cs_%s:\n\t.word 0x%08x,0,0,", f->type, f->name, f->type << 24);
321 else
322 printf("___%cs_%s:\n\t.word 0x%08x,0,", f->type, f->name, f->type << 24);
323 for (j = 0, r = f->rel; r != NULL; j++, r = r->next);
324 if (j)
325 printf("%d\n\t.word\t", j * 2);
326 else
327 printf("0\n");
328 for (r = f->rel, j--; r != NULL; j--, r = r->next) {
329 if (!strcmp (r->sect, ".text"))
330 printf ("_stext+0x%08lx", r->offset);
331 else if (!strcmp (r->sect, ".text.init"))
332 printf ("__init_begin+0x%08lx", r->offset);
333 else if (!strcmp (r->sect, "__ksymtab"))
334 printf ("__start___ksymtab+0x%08lx", r->offset);
335 else if (!strcmp (r->sect, ".fixup"))
336 printf ("__start___fixup+0x%08lx", r->offset);
337 else
338 fatal();
339 if (f->type == 'f' || !r->f)
340 printf (",0");
341 else
342 printf (",___fs_%s+0x%08x", r->f->name, (4 + r->frel*2)*4 + 4);
343 if (j) printf (",");
344 else printf ("\n");
345 }
346 printf("\n");
347 }
348 printf("\n\t.global\t___btfixup_end\n___btfixup_end:\n");
349 printf("\n\n! Define undefined references\n\n");
350 for (i = 0; i < last; i++) {
351 f = array + i;
352 if (f->type == 'f') {
353 printf("\t.global\t___f_%s\n", f->name);
354 printf("___f_%s:\n", f->name);
355 }
356 }
357 printf("\tretl\n\t nop\n\n");
358 for (i = 0; i < last; i++) {
359 f = array + i;
360 if (f->type != 'f') {
361 if (!f->initval) {
362 printf("\t.global\t___%c_%s\n", f->type, f->name);
363 printf("___%c_%s = 0\n", f->type, f->name);
364 } else {
365 printf("\t.global\t___%c_%s__btset_0x%s\n", f->type, f->name, f->initvalstr);
366 printf("___%c_%s__btset_0x%s = 0x%08x\n", f->type, f->name, f->initvalstr, f->initval);
367 }
368 }
369 }
370 printf("\n\n");
371 exit(0);
372 }
373