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