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