Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD back-end for VERSAdos-E objects. |
4b95cf5c | 2 | Copyright (C) 1995-2014 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. |
4 | ||
5 | Versados is a Motorola trademark. | |
6 | ||
7 | This file is part of BFD, the Binary File Descriptor library. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 11 | the Free Software Foundation; either version 3 of the License, or |
252b5132 RH |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
21 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
22 | MA 02110-1301, USA. */ | |
252b5132 RH |
23 | |
24 | /* | |
25 | SUBSECTION | |
1049f94e | 26 | VERSAdos-E relocatable object file format |
252b5132 RH |
27 | |
28 | DESCRIPTION | |
29 | ||
1049f94e | 30 | This module supports reading of VERSAdos relocatable |
252b5132 RH |
31 | object files. |
32 | ||
33 | A VERSAdos file looks like contains | |
34 | ||
7dee875e | 35 | o Identification Record |
252b5132 | 36 | o External Symbol Definition Record |
7dee875e | 37 | o Object Text Record |
116c20d2 | 38 | o End Record. */ |
252b5132 | 39 | |
252b5132 | 40 | #include "sysdep.h" |
3db64b00 | 41 | #include "bfd.h" |
252b5132 RH |
42 | #include "libbfd.h" |
43 | #include "libiberty.h" | |
44 | ||
252b5132 | 45 | |
252b5132 RH |
46 | #define VHEADER '1' |
47 | #define VESTDEF '2' | |
48 | #define VOTR '3' | |
49 | #define VEND '4' | |
50 | ||
116c20d2 | 51 | #define ES_BASE 17 /* First symbol has esdid 17. */ |
252b5132 | 52 | |
116c20d2 | 53 | /* Per file target dependent information. */ |
252b5132 | 54 | |
116c20d2 | 55 | /* One for each section. */ |
252b5132 | 56 | struct esdid |
116c20d2 NC |
57 | { |
58 | asection *section; /* Ptr to bfd version. */ | |
59 | unsigned char *contents; /* Used to build image. */ | |
60 | int pc; | |
61 | int relocs; /* Reloc count, valid end of pass 1. */ | |
62 | int donerel; /* Have relocs been translated. */ | |
63 | }; | |
252b5132 RH |
64 | |
65 | typedef struct versados_data_struct | |
116c20d2 NC |
66 | { |
67 | int es_done; /* Count of symbol index, starts at ES_BASE. */ | |
68 | asymbol *symbols; /* Pointer to local symbols. */ | |
69 | char *strings; /* Strings of all the above. */ | |
70 | int stringlen; /* Len of string table (valid end of pass1). */ | |
71 | int nsecsyms; /* Number of sections. */ | |
252b5132 | 72 | |
116c20d2 NC |
73 | int ndefs; /* Number of exported symbols (they dont get esdids). */ |
74 | int nrefs; /* Number of imported symbols (valid end of pass1). */ | |
252b5132 | 75 | |
116c20d2 NC |
76 | int ref_idx; /* Current processed value of the above. */ |
77 | int def_idx; | |
252b5132 | 78 | |
116c20d2 | 79 | int pass_2_done; |
252b5132 | 80 | |
116c20d2 NC |
81 | struct esdid e[16]; /* Per section info. */ |
82 | int alert; /* To see if we're trampling. */ | |
83 | asymbol *rest[256 - 16]; /* Per symbol info. */ | |
84 | } | |
252b5132 RH |
85 | tdata_type; |
86 | ||
87 | #define VDATA(abfd) (abfd->tdata.versados_data) | |
a1165289 NC |
88 | #define EDATA(abfd, n) (abfd->tdata.versados_data->e[(n) < 16 ? (n) : 0]) |
89 | #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[(n) < 240 ? (n) : 0]) | |
252b5132 RH |
90 | |
91 | struct ext_otr | |
116c20d2 NC |
92 | { |
93 | unsigned char size; | |
94 | char type; | |
95 | unsigned char map[4]; | |
96 | unsigned char esdid; | |
97 | unsigned char data[200]; | |
98 | }; | |
252b5132 RH |
99 | |
100 | struct ext_vheader | |
116c20d2 NC |
101 | { |
102 | unsigned char size; | |
103 | char type; /* Record type. */ | |
104 | char name[10]; /* Module name. */ | |
105 | char rev; /* Module rev number. */ | |
106 | char lang; | |
107 | char vol[4]; | |
108 | char user[2]; | |
109 | char cat[8]; | |
110 | char fname[8]; | |
111 | char ext[2]; | |
112 | char time[3]; | |
113 | char date[3]; | |
114 | char rest[211]; | |
115 | }; | |
252b5132 RH |
116 | |
117 | struct ext_esd | |
116c20d2 NC |
118 | { |
119 | unsigned char size; | |
120 | char type; | |
121 | unsigned char esd_entries[1]; | |
122 | }; | |
123 | ||
124 | #define ESD_ABS 0 | |
125 | #define ESD_COMMON 1 | |
126 | #define ESD_STD_REL_SEC 2 | |
127 | #define ESD_SHRT_REL_SEC 3 | |
128 | #define ESD_XDEF_IN_SEC 4 | |
129 | #define ESD_XDEF_IN_ABS 5 | |
130 | #define ESD_XREF_SEC 6 | |
131 | #define ESD_XREF_SYM 7 | |
132 | ||
252b5132 | 133 | union ext_any |
116c20d2 NC |
134 | { |
135 | unsigned char size; | |
136 | struct ext_vheader header; | |
137 | struct ext_esd esd; | |
138 | struct ext_otr otr; | |
139 | }; | |
24a35864 | 140 | |
558e161f | 141 | /* Initialize by filling in the hex conversion array. */ |
252b5132 RH |
142 | |
143 | /* Set up the tdata information. */ | |
144 | ||
b34976b6 | 145 | static bfd_boolean |
116c20d2 | 146 | versados_mkobject (bfd *abfd) |
252b5132 RH |
147 | { |
148 | if (abfd->tdata.versados_data == NULL) | |
149 | { | |
dc810e39 | 150 | bfd_size_type amt = sizeof (tdata_type); |
116c20d2 NC |
151 | tdata_type *tdata = bfd_alloc (abfd, amt); |
152 | ||
252b5132 | 153 | if (tdata == NULL) |
b34976b6 | 154 | return FALSE; |
252b5132 RH |
155 | abfd->tdata.versados_data = tdata; |
156 | tdata->symbols = NULL; | |
157 | VDATA (abfd)->alert = 0x12345678; | |
158 | } | |
159 | ||
160 | bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0); | |
b34976b6 | 161 | return TRUE; |
252b5132 RH |
162 | } |
163 | ||
252b5132 RH |
164 | /* Report a problem in an S record file. FIXME: This probably should |
165 | not call fprintf, but we really do need some mechanism for printing | |
166 | error messages. */ | |
167 | ||
252b5132 | 168 | static asymbol * |
116c20d2 NC |
169 | versados_new_symbol (bfd *abfd, |
170 | int snum, | |
171 | const char *name, | |
172 | bfd_vma val, | |
173 | asection *sec) | |
252b5132 RH |
174 | { |
175 | asymbol *n = VDATA (abfd)->symbols + snum; | |
176 | n->name = name; | |
177 | n->value = val; | |
178 | n->section = sec; | |
179 | n->the_bfd = abfd; | |
180 | n->flags = 0; | |
181 | return n; | |
182 | } | |
183 | ||
a1165289 | 184 | static bfd_boolean |
116c20d2 | 185 | get_record (bfd *abfd, union ext_any *ptr) |
252b5132 | 186 | { |
dc810e39 AM |
187 | if (bfd_bread (&ptr->size, (bfd_size_type) 1, abfd) != 1 |
188 | || (bfd_bread ((char *) ptr + 1, (bfd_size_type) ptr->size, abfd) | |
189 | != ptr->size)) | |
a1165289 NC |
190 | return FALSE; |
191 | ||
192 | { | |
193 | bfd_size_type amt = ptr->size + 1; | |
194 | ||
195 | if (amt < sizeof (* ptr)) | |
196 | memset ((char *) ptr + amt, 0, sizeof (* ptr) - amt); | |
197 | } | |
198 | ||
199 | return TRUE; | |
252b5132 RH |
200 | } |
201 | ||
24a35864 | 202 | static int |
116c20d2 | 203 | get_4 (unsigned char **pp) |
252b5132 RH |
204 | { |
205 | unsigned char *p = *pp; | |
116c20d2 | 206 | |
252b5132 RH |
207 | *pp += 4; |
208 | return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0); | |
209 | } | |
210 | ||
24a35864 | 211 | static void |
116c20d2 | 212 | get_10 (unsigned char **pp, char *name) |
252b5132 RH |
213 | { |
214 | char *p = (char *) *pp; | |
215 | int len = 10; | |
116c20d2 | 216 | |
252b5132 | 217 | *pp += len; |
116c20d2 | 218 | while (*p != ' ' && len) |
252b5132 RH |
219 | { |
220 | *name++ = *p++; | |
221 | len--; | |
222 | } | |
223 | *name = 0; | |
224 | } | |
225 | ||
226 | static char * | |
116c20d2 | 227 | new_symbol_string (bfd *abfd, const char *name) |
252b5132 RH |
228 | { |
229 | char *n = VDATA (abfd)->strings; | |
116c20d2 | 230 | |
252b5132 RH |
231 | strcpy (VDATA (abfd)->strings, name); |
232 | VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1; | |
233 | return n; | |
234 | } | |
235 | ||
252b5132 | 236 | static void |
116c20d2 | 237 | process_esd (bfd *abfd, struct ext_esd *esd, int pass) |
252b5132 | 238 | { |
116c20d2 | 239 | /* Read through the ext def for the est entries. */ |
252b5132 RH |
240 | int togo = esd->size - 2; |
241 | bfd_vma size; | |
242 | bfd_vma start; | |
243 | asection *sec; | |
244 | char name[11]; | |
245 | unsigned char *ptr = esd->esd_entries; | |
246 | unsigned char *end = ptr + togo; | |
116c20d2 | 247 | |
252b5132 RH |
248 | while (ptr < end) |
249 | { | |
250 | int scn = *ptr & 0xf; | |
251 | int typ = (*ptr >> 4) & 0xf; | |
252 | ||
116c20d2 | 253 | /* Declare this section. */ |
252b5132 RH |
254 | sprintf (name, "%d", scn); |
255 | sec = bfd_make_section_old_way (abfd, strdup (name)); | |
256 | sec->target_index = scn; | |
257 | EDATA (abfd, scn).section = sec; | |
258 | ptr++; | |
116c20d2 | 259 | |
252b5132 RH |
260 | switch (typ) |
261 | { | |
262 | default: | |
263 | abort (); | |
264 | case ESD_XREF_SEC: | |
265 | case ESD_XREF_SYM: | |
266 | { | |
267 | int snum = VDATA (abfd)->ref_idx++; | |
268 | get_10 (&ptr, name); | |
269 | if (pass == 1) | |
116c20d2 | 270 | VDATA (abfd)->stringlen += strlen (name) + 1; |
252b5132 RH |
271 | else |
272 | { | |
273 | int esidx; | |
274 | asymbol *s; | |
275 | char *n = new_symbol_string (abfd, name); | |
116c20d2 | 276 | |
dc810e39 AM |
277 | s = versados_new_symbol (abfd, snum, n, (bfd_vma) 0, |
278 | bfd_und_section_ptr); | |
252b5132 RH |
279 | esidx = VDATA (abfd)->es_done++; |
280 | RDATA (abfd, esidx - ES_BASE) = s; | |
281 | } | |
282 | } | |
283 | break; | |
284 | ||
252b5132 RH |
285 | case ESD_ABS: |
286 | size = get_4 (&ptr); | |
c7e2358a | 287 | (void) size; |
252b5132 | 288 | start = get_4 (&ptr); |
c7e2358a | 289 | (void) start; |
252b5132 RH |
290 | break; |
291 | case ESD_STD_REL_SEC: | |
292 | case ESD_SHRT_REL_SEC: | |
116c20d2 NC |
293 | sec->size = get_4 (&ptr); |
294 | sec->flags |= SEC_ALLOC; | |
252b5132 RH |
295 | break; |
296 | case ESD_XDEF_IN_ABS: | |
45dfa85a | 297 | sec = bfd_abs_section_ptr; |
252b5132 RH |
298 | case ESD_XDEF_IN_SEC: |
299 | { | |
300 | int snum = VDATA (abfd)->def_idx++; | |
dc810e39 | 301 | bfd_vma val; |
116c20d2 | 302 | |
252b5132 RH |
303 | get_10 (&ptr, name); |
304 | val = get_4 (&ptr); | |
305 | if (pass == 1) | |
116c20d2 NC |
306 | /* Just remember the symbol. */ |
307 | VDATA (abfd)->stringlen += strlen (name) + 1; | |
252b5132 RH |
308 | else |
309 | { | |
310 | asymbol *s; | |
311 | char *n = new_symbol_string (abfd, name); | |
116c20d2 | 312 | |
dc810e39 AM |
313 | s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n, |
314 | val, sec); | |
252b5132 RH |
315 | s->flags |= BSF_GLOBAL; |
316 | } | |
317 | } | |
318 | break; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
116c20d2 NC |
323 | #define R_RELWORD 1 |
324 | #define R_RELLONG 2 | |
252b5132 RH |
325 | #define R_RELWORD_NEG 3 |
326 | #define R_RELLONG_NEG 4 | |
327 | ||
328 | reloc_howto_type versados_howto_table[] = | |
329 | { | |
b34976b6 | 330 | HOWTO (R_RELWORD, 0, 1, 16, FALSE, |
252b5132 | 331 | 0, complain_overflow_dont, 0, |
b34976b6 AM |
332 | "+v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE), |
333 | HOWTO (R_RELLONG, 0, 2, 32, FALSE, | |
252b5132 | 334 | 0, complain_overflow_dont, 0, |
b34976b6 | 335 | "+v32", TRUE, 0xffffffff, 0xffffffff, FALSE), |
252b5132 | 336 | |
b34976b6 | 337 | HOWTO (R_RELWORD_NEG, 0, -1, 16, FALSE, |
252b5132 | 338 | 0, complain_overflow_dont, 0, |
b34976b6 AM |
339 | "-v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE), |
340 | HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE, | |
252b5132 | 341 | 0, complain_overflow_dont, 0, |
b34976b6 | 342 | "-v32", TRUE, 0xffffffff, 0xffffffff, FALSE), |
252b5132 RH |
343 | }; |
344 | ||
252b5132 | 345 | static int |
116c20d2 | 346 | get_offset (int len, unsigned char *ptr) |
252b5132 RH |
347 | { |
348 | int val = 0; | |
116c20d2 | 349 | |
252b5132 RH |
350 | if (len) |
351 | { | |
352 | int i; | |
116c20d2 | 353 | |
252b5132 RH |
354 | val = *ptr++; |
355 | if (val & 0x80) | |
356 | val |= ~0xff; | |
357 | for (i = 1; i < len; i++) | |
358 | val = (val << 8) | *ptr++; | |
359 | } | |
360 | ||
361 | return val; | |
362 | } | |
363 | ||
364 | static void | |
116c20d2 | 365 | process_otr (bfd *abfd, struct ext_otr *otr, int pass) |
252b5132 RH |
366 | { |
367 | unsigned long shift; | |
368 | unsigned char *srcp = otr->data; | |
369 | unsigned char *endp = (unsigned char *) otr + otr->size; | |
370 | unsigned int bits = (otr->map[0] << 24) | |
371 | | (otr->map[1] << 16) | |
372 | | (otr->map[2] << 8) | |
373 | | (otr->map[3] << 0); | |
374 | ||
375 | struct esdid *esdid = &EDATA (abfd, otr->esdid - 1); | |
376 | unsigned char *contents = esdid->contents; | |
a1165289 | 377 | bfd_boolean need_contents = FALSE; |
252b5132 RH |
378 | unsigned int dst_idx = esdid->pc; |
379 | ||
dc810e39 | 380 | for (shift = ((unsigned long) 1 << 31); shift && srcp < endp; shift >>= 1) |
252b5132 RH |
381 | { |
382 | if (bits & shift) | |
383 | { | |
384 | int flag = *srcp++; | |
385 | int esdids = (flag >> 5) & 0x7; | |
386 | int sizeinwords = ((flag >> 3) & 1) ? 2 : 1; | |
387 | int offsetlen = flag & 0x7; | |
388 | int j; | |
389 | ||
252b5132 RH |
390 | if (esdids == 0) |
391 | { | |
116c20d2 | 392 | /* A zero esdid means the new pc is the offset given. */ |
252b5132 RH |
393 | dst_idx += get_offset (offsetlen, srcp); |
394 | srcp += offsetlen; | |
395 | } | |
396 | else | |
397 | { | |
398 | int val = get_offset (offsetlen, srcp + esdids); | |
116c20d2 | 399 | |
252b5132 | 400 | if (pass == 1) |
a1165289 NC |
401 | need_contents = TRUE; |
402 | else if (contents) | |
252b5132 RH |
403 | for (j = 0; j < sizeinwords * 2; j++) |
404 | { | |
405 | contents[dst_idx + (sizeinwords * 2) - j - 1] = val; | |
406 | val >>= 8; | |
407 | } | |
408 | ||
409 | for (j = 0; j < esdids; j++) | |
410 | { | |
91d6fa6a | 411 | int id = *srcp++; |
252b5132 | 412 | |
91d6fa6a | 413 | if (id) |
252b5132 RH |
414 | { |
415 | int rn = EDATA (abfd, otr->esdid - 1).relocs++; | |
116c20d2 | 416 | |
252b5132 RH |
417 | if (pass == 1) |
418 | { | |
116c20d2 NC |
419 | /* This is the first pass over the data, |
420 | just remember that we need a reloc. */ | |
252b5132 RH |
421 | } |
422 | else | |
423 | { | |
424 | arelent *n = | |
425 | EDATA (abfd, otr->esdid - 1).section->relocation + rn; | |
426 | n->address = dst_idx; | |
427 | ||
91d6fa6a | 428 | n->sym_ptr_ptr = (asymbol **) (size_t) id; |
252b5132 RH |
429 | n->addend = 0; |
430 | n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1); | |
431 | } | |
432 | } | |
433 | } | |
434 | srcp += offsetlen; | |
435 | dst_idx += sizeinwords * 2; | |
436 | } | |
437 | } | |
438 | else | |
439 | { | |
a1165289 NC |
440 | need_contents = TRUE; |
441 | ||
442 | if (esdid->section && contents && dst_idx < esdid->section->size) | |
252b5132 RH |
443 | if (pass == 2) |
444 | { | |
116c20d2 | 445 | /* Absolute code, comes in 16 bit lumps. */ |
252b5132 RH |
446 | contents[dst_idx] = srcp[0]; |
447 | contents[dst_idx + 1] = srcp[1]; | |
448 | } | |
a1165289 | 449 | |
252b5132 RH |
450 | dst_idx += 2; |
451 | srcp += 2; | |
452 | } | |
453 | } | |
a1165289 | 454 | |
252b5132 RH |
455 | EDATA (abfd, otr->esdid - 1).pc = dst_idx; |
456 | ||
457 | if (!contents && need_contents) | |
dc810e39 | 458 | { |
0a9d414a NC |
459 | if (esdid->section) |
460 | { | |
461 | bfd_size_type size; | |
462 | ||
463 | size = esdid->section->size; | |
464 | esdid->contents = bfd_alloc (abfd, size); | |
465 | } | |
466 | else | |
467 | esdid->contents = NULL; | |
dc810e39 | 468 | } |
252b5132 RH |
469 | } |
470 | ||
b34976b6 | 471 | static bfd_boolean |
116c20d2 | 472 | versados_scan (bfd *abfd) |
252b5132 | 473 | { |
a1165289 | 474 | bfd_boolean loop = TRUE; |
252b5132 RH |
475 | int i; |
476 | int j; | |
477 | int nsecs = 0; | |
dc810e39 | 478 | bfd_size_type amt; |
252b5132 | 479 | |
bfde9f99 | 480 | VDATA (abfd)->stringlen = 0; |
252b5132 RH |
481 | VDATA (abfd)->nrefs = 0; |
482 | VDATA (abfd)->ndefs = 0; | |
483 | VDATA (abfd)->ref_idx = 0; | |
484 | VDATA (abfd)->def_idx = 0; | |
bfde9f99 | 485 | VDATA (abfd)->pass_2_done = 0; |
252b5132 RH |
486 | |
487 | while (loop) | |
488 | { | |
489 | union ext_any any; | |
116c20d2 | 490 | |
252b5132 | 491 | if (!get_record (abfd, &any)) |
a1165289 | 492 | return FALSE; |
252b5132 RH |
493 | switch (any.header.type) |
494 | { | |
495 | case VHEADER: | |
496 | break; | |
497 | case VEND: | |
a1165289 | 498 | loop = FALSE; |
252b5132 RH |
499 | break; |
500 | case VESTDEF: | |
501 | process_esd (abfd, &any.esd, 1); | |
502 | break; | |
503 | case VOTR: | |
504 | process_otr (abfd, &any.otr, 1); | |
505 | break; | |
506 | } | |
507 | } | |
508 | ||
116c20d2 | 509 | /* Now allocate space for the relocs and sections. */ |
252b5132 RH |
510 | VDATA (abfd)->nrefs = VDATA (abfd)->ref_idx; |
511 | VDATA (abfd)->ndefs = VDATA (abfd)->def_idx; | |
512 | VDATA (abfd)->ref_idx = 0; | |
513 | VDATA (abfd)->def_idx = 0; | |
514 | ||
515 | abfd->symcount = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs; | |
516 | ||
517 | for (i = 0; i < 16; i++) | |
518 | { | |
519 | struct esdid *esdid = &EDATA (abfd, i); | |
116c20d2 | 520 | |
252b5132 RH |
521 | if (esdid->section) |
522 | { | |
dc810e39 | 523 | amt = (bfd_size_type) esdid->relocs * sizeof (arelent); |
116c20d2 | 524 | esdid->section->relocation = bfd_alloc (abfd, amt); |
252b5132 RH |
525 | esdid->pc = 0; |
526 | ||
527 | if (esdid->contents) | |
528 | esdid->section->flags |= SEC_HAS_CONTENTS | SEC_LOAD; | |
529 | ||
530 | esdid->section->reloc_count = esdid->relocs; | |
531 | if (esdid->relocs) | |
532 | esdid->section->flags |= SEC_RELOC; | |
533 | ||
534 | esdid->relocs = 0; | |
535 | ||
116c20d2 | 536 | /* Add an entry into the symbol table for it. */ |
252b5132 RH |
537 | nsecs++; |
538 | VDATA (abfd)->stringlen += strlen (esdid->section->name) + 1; | |
539 | } | |
540 | } | |
541 | ||
542 | abfd->symcount += nsecs; | |
543 | ||
dc810e39 AM |
544 | amt = abfd->symcount; |
545 | amt *= sizeof (asymbol); | |
116c20d2 | 546 | VDATA (abfd)->symbols = bfd_alloc (abfd, amt); |
252b5132 | 547 | |
dc810e39 AM |
548 | amt = VDATA (abfd)->stringlen; |
549 | VDATA (abfd)->strings = bfd_alloc (abfd, amt); | |
252b5132 RH |
550 | |
551 | if ((VDATA (abfd)->symbols == NULL && abfd->symcount > 0) | |
552 | || (VDATA (abfd)->strings == NULL && VDATA (abfd)->stringlen > 0)) | |
b34976b6 | 553 | return FALSE; |
252b5132 RH |
554 | |
555 | /* Actually fill in the section symbols, | |
116c20d2 | 556 | we stick them at the end of the table. */ |
252b5132 RH |
557 | for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++) |
558 | { | |
559 | struct esdid *esdid = &EDATA (abfd, i); | |
560 | asection *sec = esdid->section; | |
116c20d2 | 561 | |
252b5132 RH |
562 | if (sec) |
563 | { | |
564 | asymbol *s = VDATA (abfd)->symbols + j; | |
565 | s->name = new_symbol_string (abfd, sec->name); | |
566 | s->section = sec; | |
567 | s->flags = BSF_LOCAL; | |
568 | s->value = 0; | |
569 | s->the_bfd = abfd; | |
570 | j++; | |
571 | } | |
572 | } | |
116c20d2 | 573 | |
252b5132 RH |
574 | if (abfd->symcount) |
575 | abfd->flags |= HAS_SYMS; | |
576 | ||
577 | /* Set this to nsecs - since we've already planted the section | |
116c20d2 | 578 | symbols. */ |
252b5132 RH |
579 | VDATA (abfd)->nsecsyms = nsecs; |
580 | ||
581 | VDATA (abfd)->ref_idx = 0; | |
582 | ||
a1165289 | 583 | return TRUE; |
252b5132 RH |
584 | } |
585 | ||
252b5132 RH |
586 | /* Check whether an existing file is a versados file. */ |
587 | ||
588 | static const bfd_target * | |
116c20d2 | 589 | versados_object_p (bfd *abfd) |
252b5132 RH |
590 | { |
591 | struct ext_vheader ext; | |
592 | unsigned char len; | |
487e54f2 | 593 | tdata_type *tdata_save; |
252b5132 RH |
594 | |
595 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) | |
596 | return NULL; | |
597 | ||
dc810e39 | 598 | if (bfd_bread (&len, (bfd_size_type) 1, abfd) != 1) |
252b5132 RH |
599 | { |
600 | if (bfd_get_error () != bfd_error_system_call) | |
601 | bfd_set_error (bfd_error_wrong_format); | |
602 | return NULL; | |
603 | } | |
604 | ||
0a9d414a NC |
605 | /* PR 17512: file: 726-2128-0.004. */ |
606 | if (len < 13) | |
607 | { | |
608 | bfd_set_error (bfd_error_wrong_format); | |
609 | return NULL; | |
610 | } | |
611 | ||
dc810e39 | 612 | if (bfd_bread (&ext.type, (bfd_size_type) len, abfd) != len) |
252b5132 RH |
613 | { |
614 | if (bfd_get_error () != bfd_error_system_call) | |
615 | bfd_set_error (bfd_error_wrong_format); | |
616 | return NULL; | |
617 | } | |
618 | ||
619 | /* We guess that the language field will never be larger than 10. | |
620 | In sample files, it is always either 0 or 1. Checking for this | |
621 | prevents confusion with Intel Hex files. */ | |
622 | if (ext.type != VHEADER | |
623 | || ext.lang > 10) | |
624 | { | |
625 | bfd_set_error (bfd_error_wrong_format); | |
626 | return NULL; | |
627 | } | |
628 | ||
629 | /* OK, looks like a record, build the tdata and read in. */ | |
487e54f2 AM |
630 | tdata_save = abfd->tdata.versados_data; |
631 | if (!versados_mkobject (abfd) || !versados_scan (abfd)) | |
632 | { | |
633 | abfd->tdata.versados_data = tdata_save; | |
634 | return NULL; | |
635 | } | |
252b5132 RH |
636 | |
637 | return abfd->xvec; | |
638 | } | |
639 | ||
b34976b6 | 640 | static bfd_boolean |
116c20d2 | 641 | versados_pass_2 (bfd *abfd) |
252b5132 RH |
642 | { |
643 | union ext_any any; | |
644 | ||
645 | if (VDATA (abfd)->pass_2_done) | |
646 | return 1; | |
647 | ||
dc810e39 | 648 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
252b5132 RH |
649 | return 0; |
650 | ||
651 | VDATA (abfd)->es_done = ES_BASE; | |
652 | ||
116c20d2 | 653 | /* Read records till we get to where we want to be. */ |
252b5132 RH |
654 | while (1) |
655 | { | |
656 | get_record (abfd, &any); | |
657 | switch (any.header.type) | |
658 | { | |
659 | case VEND: | |
660 | VDATA (abfd)->pass_2_done = 1; | |
661 | return 1; | |
662 | case VESTDEF: | |
663 | process_esd (abfd, &any.esd, 2); | |
664 | break; | |
665 | case VOTR: | |
666 | process_otr (abfd, &any.otr, 2); | |
667 | break; | |
668 | } | |
669 | } | |
670 | } | |
671 | ||
b34976b6 | 672 | static bfd_boolean |
116c20d2 NC |
673 | versados_get_section_contents (bfd *abfd, |
674 | asection *section, | |
675 | void * location, | |
676 | file_ptr offset, | |
677 | bfd_size_type count) | |
252b5132 RH |
678 | { |
679 | if (!versados_pass_2 (abfd)) | |
b34976b6 | 680 | return FALSE; |
252b5132 RH |
681 | |
682 | memcpy (location, | |
683 | EDATA (abfd, section->target_index).contents + offset, | |
684 | (size_t) count); | |
685 | ||
b34976b6 | 686 | return TRUE; |
252b5132 RH |
687 | } |
688 | ||
689 | #define versados_get_section_contents_in_window \ | |
690 | _bfd_generic_get_section_contents_in_window | |
691 | ||
b34976b6 | 692 | static bfd_boolean |
116c20d2 NC |
693 | versados_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED, |
694 | sec_ptr section ATTRIBUTE_UNUSED, | |
695 | const void * location ATTRIBUTE_UNUSED, | |
696 | file_ptr offset ATTRIBUTE_UNUSED, | |
697 | bfd_size_type bytes_to_do ATTRIBUTE_UNUSED) | |
252b5132 | 698 | { |
b34976b6 | 699 | return FALSE; |
252b5132 RH |
700 | } |
701 | ||
252b5132 | 702 | static int |
116c20d2 | 703 | versados_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED, |
a6b96beb | 704 | struct bfd_link_info *info ATTRIBUTE_UNUSED) |
252b5132 RH |
705 | { |
706 | return 0; | |
707 | } | |
708 | ||
252b5132 RH |
709 | /* Return the amount of memory needed to read the symbol table. */ |
710 | ||
711 | static long | |
116c20d2 | 712 | versados_get_symtab_upper_bound (bfd *abfd) |
252b5132 RH |
713 | { |
714 | return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *); | |
715 | } | |
716 | ||
717 | /* Return the symbol table. */ | |
718 | ||
719 | static long | |
116c20d2 | 720 | versados_canonicalize_symtab (bfd *abfd, asymbol **alocation) |
252b5132 RH |
721 | { |
722 | unsigned int symcount = bfd_get_symcount (abfd); | |
723 | unsigned int i; | |
724 | asymbol *s; | |
725 | ||
726 | versados_pass_2 (abfd); | |
727 | ||
728 | for (i = 0, s = VDATA (abfd)->symbols; | |
729 | i < symcount; | |
730 | s++, i++) | |
116c20d2 | 731 | *alocation++ = s; |
252b5132 RH |
732 | |
733 | *alocation = NULL; | |
734 | ||
735 | return symcount; | |
736 | } | |
737 | ||
dc810e39 | 738 | static void |
116c20d2 NC |
739 | versados_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, |
740 | asymbol *symbol, | |
741 | symbol_info *ret) | |
252b5132 RH |
742 | { |
743 | bfd_symbol_info (symbol, ret); | |
744 | } | |
745 | ||
dc810e39 | 746 | static void |
116c20d2 NC |
747 | versados_print_symbol (bfd *abfd, |
748 | void * afile, | |
749 | asymbol *symbol, | |
750 | bfd_print_symbol_type how) | |
252b5132 RH |
751 | { |
752 | FILE *file = (FILE *) afile; | |
116c20d2 | 753 | |
252b5132 RH |
754 | switch (how) |
755 | { | |
756 | case bfd_print_symbol_name: | |
757 | fprintf (file, "%s", symbol->name); | |
758 | break; | |
759 | default: | |
116c20d2 | 760 | bfd_print_symbol_vandf (abfd, (void *) file, symbol); |
252b5132 RH |
761 | fprintf (file, " %-5s %s", |
762 | symbol->section->name, | |
763 | symbol->name); | |
252b5132 RH |
764 | } |
765 | } | |
766 | ||
dc810e39 | 767 | static long |
116c20d2 NC |
768 | versados_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, |
769 | sec_ptr asect) | |
252b5132 RH |
770 | { |
771 | return (asect->reloc_count + 1) * sizeof (arelent *); | |
772 | } | |
773 | ||
dc810e39 | 774 | static long |
116c20d2 NC |
775 | versados_canonicalize_reloc (bfd *abfd, |
776 | sec_ptr section, | |
777 | arelent **relptr, | |
778 | asymbol **symbols) | |
252b5132 RH |
779 | { |
780 | unsigned int count; | |
781 | arelent *src; | |
782 | ||
783 | versados_pass_2 (abfd); | |
784 | src = section->relocation; | |
a1165289 | 785 | |
252b5132 RH |
786 | if (!EDATA (abfd, section->target_index).donerel) |
787 | { | |
788 | EDATA (abfd, section->target_index).donerel = 1; | |
116c20d2 | 789 | /* Translate from indexes to symptr ptrs. */ |
252b5132 RH |
790 | for (count = 0; count < section->reloc_count; count++) |
791 | { | |
f60ca5e3 | 792 | int esdid = (int) (size_t) src[count].sym_ptr_ptr; |
252b5132 RH |
793 | |
794 | if (esdid == 0) | |
45dfa85a | 795 | src[count].sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; |
116c20d2 | 796 | else if (esdid < ES_BASE) |
252b5132 | 797 | { |
116c20d2 | 798 | /* Section relative thing. */ |
252b5132 | 799 | struct esdid *e = &EDATA (abfd, esdid - 1); |
116c20d2 | 800 | |
252b5132 RH |
801 | src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr; |
802 | } | |
a1165289 NC |
803 | /* PR 17512: file:3757-2936-0.004. */ |
804 | else if ((unsigned) (esdid - ES_BASE) >= bfd_get_symcount (abfd)) | |
805 | src[count].sym_ptr_ptr = bfd_und_section_ptr->symbol_ptr_ptr; | |
252b5132 | 806 | else |
116c20d2 | 807 | src[count].sym_ptr_ptr = symbols + esdid - ES_BASE; |
252b5132 RH |
808 | } |
809 | } | |
810 | ||
811 | for (count = 0; count < section->reloc_count; count++) | |
116c20d2 NC |
812 | *relptr++ = src++; |
813 | ||
252b5132 RH |
814 | *relptr = 0; |
815 | return section->reloc_count; | |
816 | } | |
817 | ||
116c20d2 NC |
818 | #define versados_close_and_cleanup _bfd_generic_close_and_cleanup |
819 | #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info | |
820 | #define versados_new_section_hook _bfd_generic_new_section_hook | |
821 | #define versados_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false) | |
822 | #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name | |
823 | #define versados_get_lineno _bfd_nosymbols_get_lineno | |
824 | #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line | |
9c461f7d | 825 | #define versados_find_line _bfd_nosymbols_find_line |
4ab527b0 | 826 | #define versados_find_inliner_info _bfd_nosymbols_find_inliner_info |
60bb06bc | 827 | #define versados_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string |
116c20d2 NC |
828 | #define versados_make_empty_symbol _bfd_generic_make_empty_symbol |
829 | #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol | |
830 | #define versados_read_minisymbols _bfd_generic_read_minisymbols | |
831 | #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol | |
832 | #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup | |
157090f7 | 833 | #define versados_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup |
116c20d2 NC |
834 | #define versados_set_arch_mach bfd_default_set_arch_mach |
835 | #define versados_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents | |
836 | #define versados_bfd_relax_section bfd_generic_relax_section | |
837 | #define versados_bfd_gc_sections bfd_generic_gc_sections | |
ae17ab41 | 838 | #define versados_bfd_lookup_section_flags bfd_generic_lookup_section_flags |
116c20d2 NC |
839 | #define versados_bfd_merge_sections bfd_generic_merge_sections |
840 | #define versados_bfd_is_group_section bfd_generic_is_group_section | |
841 | #define versados_bfd_discard_group bfd_generic_discard_group | |
842 | #define versados_section_already_linked _bfd_generic_section_already_linked | |
3023e3f6 | 843 | #define versados_bfd_define_common_symbol bfd_generic_define_common_symbol |
116c20d2 | 844 | #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
116c20d2 NC |
845 | #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols |
846 | #define versados_bfd_link_just_syms _bfd_generic_link_just_syms | |
1338dd10 PB |
847 | #define versados_bfd_copy_link_hash_symbol_type \ |
848 | _bfd_generic_copy_link_hash_symbol_type | |
116c20d2 NC |
849 | #define versados_bfd_final_link _bfd_generic_final_link |
850 | #define versados_bfd_link_split_section _bfd_generic_link_split_section | |
252b5132 | 851 | |
6d00b590 | 852 | const bfd_target m68k_versados_vec = |
252b5132 | 853 | { |
116c20d2 | 854 | "versados", /* Name. */ |
252b5132 | 855 | bfd_target_versados_flavour, |
116c20d2 NC |
856 | BFD_ENDIAN_BIG, /* Target byte order. */ |
857 | BFD_ENDIAN_BIG, /* Target headers byte order. */ | |
858 | (HAS_RELOC | EXEC_P | /* Object flags. */ | |
252b5132 RH |
859 | HAS_LINENO | HAS_DEBUG | |
860 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), | |
861 | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS | |
116c20d2 NC |
862 | | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */ |
863 | 0, /* Leading underscore. */ | |
864 | ' ', /* AR_pad_char. */ | |
865 | 16, /* AR_max_namelen. */ | |
0aabe54e | 866 | 0, /* match priority. */ |
252b5132 RH |
867 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
868 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
116c20d2 | 869 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */ |
252b5132 RH |
870 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
871 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, | |
116c20d2 | 872 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */ |
252b5132 RH |
873 | |
874 | { | |
875 | _bfd_dummy_target, | |
116c20d2 | 876 | versados_object_p, /* bfd_check_format. */ |
252b5132 RH |
877 | _bfd_dummy_target, |
878 | _bfd_dummy_target, | |
879 | }, | |
880 | { | |
881 | bfd_false, | |
882 | versados_mkobject, | |
883 | _bfd_generic_mkarchive, | |
884 | bfd_false, | |
885 | }, | |
116c20d2 | 886 | { /* bfd_write_contents. */ |
252b5132 RH |
887 | bfd_false, |
888 | bfd_false, | |
889 | _bfd_write_archive_contents, | |
890 | bfd_false, | |
891 | }, | |
892 | ||
893 | BFD_JUMP_TABLE_GENERIC (versados), | |
894 | BFD_JUMP_TABLE_COPY (_bfd_generic), | |
895 | BFD_JUMP_TABLE_CORE (_bfd_nocore), | |
896 | BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive), | |
897 | BFD_JUMP_TABLE_SYMBOLS (versados), | |
898 | BFD_JUMP_TABLE_RELOCS (versados), | |
899 | BFD_JUMP_TABLE_WRITE (versados), | |
900 | BFD_JUMP_TABLE_LINK (versados), | |
901 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), | |
902 | ||
c3c89269 | 903 | NULL, |
558e161f | 904 | |
116c20d2 | 905 | NULL |
252b5132 | 906 | }; |