| 1 | /* BFD back-end for ieee-695 objects. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
| 3 | 2000, 2001, 2002, 2003, 2004, 2005 |
| 4 | Free Software Foundation, Inc. |
| 5 | |
| 6 | Written by Steve Chamberlain of Cygnus Support. |
| 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 | #define KEEPMINUSPCININST 0 |
| 25 | |
| 26 | /* IEEE 695 format is a stream of records, which we parse using a simple one- |
| 27 | token (which is one byte in this lexicon) lookahead recursive decent |
| 28 | parser. */ |
| 29 | |
| 30 | #include "bfd.h" |
| 31 | #include "sysdep.h" |
| 32 | #include "libbfd.h" |
| 33 | #include "ieee.h" |
| 34 | #include "libieee.h" |
| 35 | #include "safe-ctype.h" |
| 36 | |
| 37 | struct output_buffer_struct |
| 38 | { |
| 39 | unsigned char *ptrp; |
| 40 | int buffer; |
| 41 | }; |
| 42 | |
| 43 | static unsigned char *output_ptr_start; |
| 44 | static unsigned char *output_ptr; |
| 45 | static unsigned char *output_ptr_end; |
| 46 | static unsigned char *input_ptr_start; |
| 47 | static unsigned char *input_ptr; |
| 48 | static unsigned char *input_ptr_end; |
| 49 | static bfd *input_bfd; |
| 50 | static bfd *output_bfd; |
| 51 | static int output_buffer; |
| 52 | |
| 53 | |
| 54 | static void block (void); |
| 55 | |
| 56 | /* Functions for writing to ieee files in the strange way that the |
| 57 | standard requires. */ |
| 58 | |
| 59 | static bfd_boolean |
| 60 | ieee_write_byte (bfd *abfd, int barg) |
| 61 | { |
| 62 | bfd_byte byte; |
| 63 | |
| 64 | byte = barg; |
| 65 | if (bfd_bwrite ((void *) &byte, (bfd_size_type) 1, abfd) != 1) |
| 66 | return FALSE; |
| 67 | return TRUE; |
| 68 | } |
| 69 | |
| 70 | static bfd_boolean |
| 71 | ieee_write_2bytes (bfd *abfd, int bytes) |
| 72 | { |
| 73 | bfd_byte buffer[2]; |
| 74 | |
| 75 | buffer[0] = bytes >> 8; |
| 76 | buffer[1] = bytes & 0xff; |
| 77 | if (bfd_bwrite ((void *) buffer, (bfd_size_type) 2, abfd) != 2) |
| 78 | return FALSE; |
| 79 | return TRUE; |
| 80 | } |
| 81 | |
| 82 | static bfd_boolean |
| 83 | ieee_write_int (bfd *abfd, bfd_vma value) |
| 84 | { |
| 85 | if (value <= 127) |
| 86 | { |
| 87 | if (! ieee_write_byte (abfd, (bfd_byte) value)) |
| 88 | return FALSE; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | unsigned int length; |
| 93 | |
| 94 | /* How many significant bytes ? */ |
| 95 | /* FIXME FOR LONGER INTS. */ |
| 96 | if (value & 0xff000000) |
| 97 | length = 4; |
| 98 | else if (value & 0x00ff0000) |
| 99 | length = 3; |
| 100 | else if (value & 0x0000ff00) |
| 101 | length = 2; |
| 102 | else |
| 103 | length = 1; |
| 104 | |
| 105 | if (! ieee_write_byte (abfd, |
| 106 | (bfd_byte) ((int) ieee_number_repeat_start_enum |
| 107 | + length))) |
| 108 | return FALSE; |
| 109 | switch (length) |
| 110 | { |
| 111 | case 4: |
| 112 | if (! ieee_write_byte (abfd, (bfd_byte) (value >> 24))) |
| 113 | return FALSE; |
| 114 | /* Fall through. */ |
| 115 | case 3: |
| 116 | if (! ieee_write_byte (abfd, (bfd_byte) (value >> 16))) |
| 117 | return FALSE; |
| 118 | /* Fall through. */ |
| 119 | case 2: |
| 120 | if (! ieee_write_byte (abfd, (bfd_byte) (value >> 8))) |
| 121 | return FALSE; |
| 122 | /* Fall through. */ |
| 123 | case 1: |
| 124 | if (! ieee_write_byte (abfd, (bfd_byte) (value))) |
| 125 | return FALSE; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | static bfd_boolean |
| 133 | ieee_write_id (bfd *abfd, const char *id) |
| 134 | { |
| 135 | size_t length = strlen (id); |
| 136 | |
| 137 | if (length <= 127) |
| 138 | { |
| 139 | if (! ieee_write_byte (abfd, (bfd_byte) length)) |
| 140 | return FALSE; |
| 141 | } |
| 142 | else if (length < 255) |
| 143 | { |
| 144 | if (! ieee_write_byte (abfd, ieee_extension_length_1_enum) |
| 145 | || ! ieee_write_byte (abfd, (bfd_byte) length)) |
| 146 | return FALSE; |
| 147 | } |
| 148 | else if (length < 65535) |
| 149 | { |
| 150 | if (! ieee_write_byte (abfd, ieee_extension_length_2_enum) |
| 151 | || ! ieee_write_2bytes (abfd, (int) length)) |
| 152 | return FALSE; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | (*_bfd_error_handler) |
| 157 | (_("%s: string too long (%d chars, max 65535)"), |
| 158 | bfd_get_filename (abfd), length); |
| 159 | bfd_set_error (bfd_error_invalid_operation); |
| 160 | return FALSE; |
| 161 | } |
| 162 | |
| 163 | if (bfd_bwrite ((void *) id, (bfd_size_type) length, abfd) != length) |
| 164 | return FALSE; |
| 165 | return TRUE; |
| 166 | } |
| 167 | \f |
| 168 | /* Functions for reading from ieee files in the strange way that the |
| 169 | standard requires. */ |
| 170 | |
| 171 | #define this_byte(ieee) *((ieee)->input_p) |
| 172 | #define next_byte(ieee) ((ieee)->input_p++) |
| 173 | #define this_byte_and_next(ieee) (*((ieee)->input_p++)) |
| 174 | |
| 175 | static unsigned short |
| 176 | read_2bytes (common_header_type *ieee) |
| 177 | { |
| 178 | unsigned char c1 = this_byte_and_next (ieee); |
| 179 | unsigned char c2 = this_byte_and_next (ieee); |
| 180 | |
| 181 | return (c1 << 8) | c2; |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | bfd_get_string (common_header_type *ieee, char *string, size_t length) |
| 186 | { |
| 187 | size_t i; |
| 188 | |
| 189 | for (i = 0; i < length; i++) |
| 190 | string[i] = this_byte_and_next (ieee); |
| 191 | } |
| 192 | |
| 193 | static char * |
| 194 | read_id (common_header_type *ieee) |
| 195 | { |
| 196 | size_t length; |
| 197 | char *string; |
| 198 | |
| 199 | length = this_byte_and_next (ieee); |
| 200 | if (length <= 0x7f) |
| 201 | /* Simple string of length 0 to 127. */ |
| 202 | ; |
| 203 | |
| 204 | else if (length == 0xde) |
| 205 | /* Length is next byte, allowing 0..255. */ |
| 206 | length = this_byte_and_next (ieee); |
| 207 | |
| 208 | else if (length == 0xdf) |
| 209 | { |
| 210 | /* Length is next two bytes, allowing 0..65535. */ |
| 211 | length = this_byte_and_next (ieee); |
| 212 | length = (length * 256) + this_byte_and_next (ieee); |
| 213 | } |
| 214 | |
| 215 | /* Buy memory and read string. */ |
| 216 | string = bfd_alloc (ieee->abfd, (bfd_size_type) length + 1); |
| 217 | if (!string) |
| 218 | return NULL; |
| 219 | bfd_get_string (ieee, string, length); |
| 220 | string[length] = 0; |
| 221 | return string; |
| 222 | } |
| 223 | |
| 224 | static bfd_boolean |
| 225 | ieee_write_expression (bfd *abfd, |
| 226 | bfd_vma value, |
| 227 | asymbol *symbol, |
| 228 | bfd_boolean pcrel, |
| 229 | unsigned int index) |
| 230 | { |
| 231 | unsigned int term_count = 0; |
| 232 | |
| 233 | if (value != 0) |
| 234 | { |
| 235 | if (! ieee_write_int (abfd, value)) |
| 236 | return FALSE; |
| 237 | term_count++; |
| 238 | } |
| 239 | |
| 240 | /* Badly formatted binaries can have a missing symbol, |
| 241 | so test here to prevent a seg fault. */ |
| 242 | if (symbol != NULL) |
| 243 | { |
| 244 | if (bfd_is_com_section (symbol->section) |
| 245 | || bfd_is_und_section (symbol->section)) |
| 246 | { |
| 247 | /* Def of a common symbol. */ |
| 248 | if (! ieee_write_byte (abfd, ieee_variable_X_enum) |
| 249 | || ! ieee_write_int (abfd, symbol->value)) |
| 250 | return FALSE; |
| 251 | term_count ++; |
| 252 | } |
| 253 | else if (! bfd_is_abs_section (symbol->section)) |
| 254 | { |
| 255 | /* Ref to defined symbol - */ |
| 256 | if (symbol->flags & BSF_GLOBAL) |
| 257 | { |
| 258 | if (! ieee_write_byte (abfd, ieee_variable_I_enum) |
| 259 | || ! ieee_write_int (abfd, symbol->value)) |
| 260 | return FALSE; |
| 261 | term_count++; |
| 262 | } |
| 263 | else if (symbol->flags & (BSF_LOCAL | BSF_SECTION_SYM)) |
| 264 | { |
| 265 | /* This is a reference to a defined local symbol. We can |
| 266 | easily do a local as a section+offset. */ |
| 267 | if (! ieee_write_byte (abfd, ieee_variable_R_enum) |
| 268 | || ! ieee_write_byte (abfd, |
| 269 | (bfd_byte) (symbol->section->index |
| 270 | + IEEE_SECTION_NUMBER_BASE))) |
| 271 | return FALSE; |
| 272 | |
| 273 | term_count++; |
| 274 | if (symbol->value != 0) |
| 275 | { |
| 276 | if (! ieee_write_int (abfd, symbol->value)) |
| 277 | return FALSE; |
| 278 | term_count++; |
| 279 | } |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | (*_bfd_error_handler) |
| 284 | (_("%s: unrecognized symbol `%s' flags 0x%x"), |
| 285 | bfd_get_filename (abfd), bfd_asymbol_name (symbol), |
| 286 | symbol->flags); |
| 287 | bfd_set_error (bfd_error_invalid_operation); |
| 288 | return FALSE; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (pcrel) |
| 294 | { |
| 295 | /* Subtract the pc from here by asking for PC of this section. */ |
| 296 | if (! ieee_write_byte (abfd, ieee_variable_P_enum) |
| 297 | || ! ieee_write_byte (abfd, |
| 298 | (bfd_byte) (index + IEEE_SECTION_NUMBER_BASE)) |
| 299 | || ! ieee_write_byte (abfd, ieee_function_minus_enum)) |
| 300 | return FALSE; |
| 301 | } |
| 302 | |
| 303 | /* Handle the degenerate case of a 0 address. */ |
| 304 | if (term_count == 0) |
| 305 | if (! ieee_write_int (abfd, (bfd_vma) 0)) |
| 306 | return FALSE; |
| 307 | |
| 308 | while (term_count > 1) |
| 309 | { |
| 310 | if (! ieee_write_byte (abfd, ieee_function_plus_enum)) |
| 311 | return FALSE; |
| 312 | term_count--; |
| 313 | } |
| 314 | |
| 315 | return TRUE; |
| 316 | } |
| 317 | \f |
| 318 | /* Writes any integer into the buffer supplied and always takes 5 bytes. */ |
| 319 | |
| 320 | static void |
| 321 | ieee_write_int5 (bfd_byte *buffer, bfd_vma value) |
| 322 | { |
| 323 | buffer[0] = (bfd_byte) ieee_number_repeat_4_enum; |
| 324 | buffer[1] = (value >> 24) & 0xff; |
| 325 | buffer[2] = (value >> 16) & 0xff; |
| 326 | buffer[3] = (value >> 8) & 0xff; |
| 327 | buffer[4] = (value >> 0) & 0xff; |
| 328 | } |
| 329 | |
| 330 | static bfd_boolean |
| 331 | ieee_write_int5_out (bfd *abfd, bfd_vma value) |
| 332 | { |
| 333 | bfd_byte b[5]; |
| 334 | |
| 335 | ieee_write_int5 (b, value); |
| 336 | if (bfd_bwrite ((void *) b, (bfd_size_type) 5, abfd) != 5) |
| 337 | return FALSE; |
| 338 | return TRUE; |
| 339 | } |
| 340 | |
| 341 | static bfd_boolean |
| 342 | parse_int (common_header_type *ieee, bfd_vma *value_ptr) |
| 343 | { |
| 344 | int value = this_byte (ieee); |
| 345 | int result; |
| 346 | |
| 347 | if (value >= 0 && value <= 127) |
| 348 | { |
| 349 | *value_ptr = value; |
| 350 | next_byte (ieee); |
| 351 | return TRUE; |
| 352 | } |
| 353 | else if (value >= 0x80 && value <= 0x88) |
| 354 | { |
| 355 | unsigned int count = value & 0xf; |
| 356 | |
| 357 | result = 0; |
| 358 | next_byte (ieee); |
| 359 | while (count) |
| 360 | { |
| 361 | result = (result << 8) | this_byte_and_next (ieee); |
| 362 | count--; |
| 363 | } |
| 364 | *value_ptr = result; |
| 365 | return TRUE; |
| 366 | } |
| 367 | return FALSE; |
| 368 | } |
| 369 | |
| 370 | static int |
| 371 | parse_i (common_header_type *ieee, bfd_boolean *ok) |
| 372 | { |
| 373 | bfd_vma x; |
| 374 | *ok = parse_int (ieee, &x); |
| 375 | return x; |
| 376 | } |
| 377 | |
| 378 | static bfd_vma |
| 379 | must_parse_int (common_header_type *ieee) |
| 380 | { |
| 381 | bfd_vma result; |
| 382 | BFD_ASSERT (parse_int (ieee, &result)); |
| 383 | return result; |
| 384 | } |
| 385 | |
| 386 | typedef struct |
| 387 | { |
| 388 | bfd_vma value; |
| 389 | asection *section; |
| 390 | ieee_symbol_index_type symbol; |
| 391 | } ieee_value_type; |
| 392 | |
| 393 | |
| 394 | #if KEEPMINUSPCININST |
| 395 | |
| 396 | #define SRC_MASK(arg) arg |
| 397 | #define PCREL_OFFSET FALSE |
| 398 | |
| 399 | #else |
| 400 | |
| 401 | #define SRC_MASK(arg) 0 |
| 402 | #define PCREL_OFFSET TRUE |
| 403 | |
| 404 | #endif |
| 405 | |
| 406 | static reloc_howto_type abs32_howto = |
| 407 | HOWTO (1, |
| 408 | 0, |
| 409 | 2, |
| 410 | 32, |
| 411 | FALSE, |
| 412 | 0, |
| 413 | complain_overflow_bitfield, |
| 414 | 0, |
| 415 | "abs32", |
| 416 | TRUE, |
| 417 | 0xffffffff, |
| 418 | 0xffffffff, |
| 419 | FALSE); |
| 420 | |
| 421 | static reloc_howto_type abs16_howto = |
| 422 | HOWTO (1, |
| 423 | 0, |
| 424 | 1, |
| 425 | 16, |
| 426 | FALSE, |
| 427 | 0, |
| 428 | complain_overflow_bitfield, |
| 429 | 0, |
| 430 | "abs16", |
| 431 | TRUE, |
| 432 | 0x0000ffff, |
| 433 | 0x0000ffff, |
| 434 | FALSE); |
| 435 | |
| 436 | static reloc_howto_type abs8_howto = |
| 437 | HOWTO (1, |
| 438 | 0, |
| 439 | 0, |
| 440 | 8, |
| 441 | FALSE, |
| 442 | 0, |
| 443 | complain_overflow_bitfield, |
| 444 | 0, |
| 445 | "abs8", |
| 446 | TRUE, |
| 447 | 0x000000ff, |
| 448 | 0x000000ff, |
| 449 | FALSE); |
| 450 | |
| 451 | static reloc_howto_type rel32_howto = |
| 452 | HOWTO (1, |
| 453 | 0, |
| 454 | 2, |
| 455 | 32, |
| 456 | TRUE, |
| 457 | 0, |
| 458 | complain_overflow_signed, |
| 459 | 0, |
| 460 | "rel32", |
| 461 | TRUE, |
| 462 | SRC_MASK (0xffffffff), |
| 463 | 0xffffffff, |
| 464 | PCREL_OFFSET); |
| 465 | |
| 466 | static reloc_howto_type rel16_howto = |
| 467 | HOWTO (1, |
| 468 | 0, |
| 469 | 1, |
| 470 | 16, |
| 471 | TRUE, |
| 472 | 0, |
| 473 | complain_overflow_signed, |
| 474 | 0, |
| 475 | "rel16", |
| 476 | TRUE, |
| 477 | SRC_MASK (0x0000ffff), |
| 478 | 0x0000ffff, |
| 479 | PCREL_OFFSET); |
| 480 | |
| 481 | static reloc_howto_type rel8_howto = |
| 482 | HOWTO (1, |
| 483 | 0, |
| 484 | 0, |
| 485 | 8, |
| 486 | TRUE, |
| 487 | 0, |
| 488 | complain_overflow_signed, |
| 489 | 0, |
| 490 | "rel8", |
| 491 | TRUE, |
| 492 | SRC_MASK (0x000000ff), |
| 493 | 0x000000ff, |
| 494 | PCREL_OFFSET); |
| 495 | |
| 496 | static ieee_symbol_index_type NOSYMBOL = {0, 0}; |
| 497 | |
| 498 | static void |
| 499 | parse_expression (ieee_data_type *ieee, |
| 500 | bfd_vma *value, |
| 501 | ieee_symbol_index_type *symbol, |
| 502 | bfd_boolean *pcrel, |
| 503 | unsigned int *extra, |
| 504 | asection **section) |
| 505 | |
| 506 | { |
| 507 | bfd_boolean loop = TRUE; |
| 508 | ieee_value_type stack[10]; |
| 509 | ieee_value_type *sp = stack; |
| 510 | asection *dummy; |
| 511 | |
| 512 | #define POS sp[1] |
| 513 | #define TOS sp[0] |
| 514 | #define NOS sp[-1] |
| 515 | #define INC sp++; |
| 516 | #define DEC sp--; |
| 517 | |
| 518 | /* The stack pointer always points to the next unused location. */ |
| 519 | #define PUSH(x,y,z) TOS.symbol = x; TOS.section = y; TOS.value = z; INC; |
| 520 | #define POP(x,y,z) DEC; x = TOS.symbol; y = TOS.section; z = TOS.value; |
| 521 | |
| 522 | while (loop && ieee->h.input_p < ieee->h.last_byte) |
| 523 | { |
| 524 | switch (this_byte (&(ieee->h))) |
| 525 | { |
| 526 | case ieee_variable_P_enum: |
| 527 | /* P variable, current program counter for section n. */ |
| 528 | { |
| 529 | int section_n; |
| 530 | |
| 531 | next_byte (&(ieee->h)); |
| 532 | *pcrel = TRUE; |
| 533 | section_n = must_parse_int (&(ieee->h)); |
| 534 | PUSH (NOSYMBOL, bfd_abs_section_ptr, 0); |
| 535 | break; |
| 536 | } |
| 537 | case ieee_variable_L_enum: |
| 538 | /* L variable address of section N. */ |
| 539 | next_byte (&(ieee->h)); |
| 540 | PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0); |
| 541 | break; |
| 542 | case ieee_variable_R_enum: |
| 543 | /* R variable, logical address of section module. */ |
| 544 | /* FIXME, this should be different to L. */ |
| 545 | next_byte (&(ieee->h)); |
| 546 | PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0); |
| 547 | break; |
| 548 | case ieee_variable_S_enum: |
| 549 | /* S variable, size in MAUS of section module. */ |
| 550 | next_byte (&(ieee->h)); |
| 551 | PUSH (NOSYMBOL, |
| 552 | 0, |
| 553 | ieee->section_table[must_parse_int (&(ieee->h))]->size); |
| 554 | break; |
| 555 | case ieee_variable_I_enum: |
| 556 | /* Push the address of variable n. */ |
| 557 | { |
| 558 | ieee_symbol_index_type sy; |
| 559 | |
| 560 | next_byte (&(ieee->h)); |
| 561 | sy.index = (int) must_parse_int (&(ieee->h)); |
| 562 | sy.letter = 'I'; |
| 563 | |
| 564 | PUSH (sy, bfd_abs_section_ptr, 0); |
| 565 | } |
| 566 | break; |
| 567 | case ieee_variable_X_enum: |
| 568 | /* Push the address of external variable n. */ |
| 569 | { |
| 570 | ieee_symbol_index_type sy; |
| 571 | |
| 572 | next_byte (&(ieee->h)); |
| 573 | sy.index = (int) (must_parse_int (&(ieee->h))); |
| 574 | sy.letter = 'X'; |
| 575 | |
| 576 | PUSH (sy, bfd_und_section_ptr, 0); |
| 577 | } |
| 578 | break; |
| 579 | case ieee_function_minus_enum: |
| 580 | { |
| 581 | bfd_vma value1, value2; |
| 582 | asection *section1, *section_dummy; |
| 583 | ieee_symbol_index_type sy; |
| 584 | |
| 585 | next_byte (&(ieee->h)); |
| 586 | |
| 587 | POP (sy, section1, value1); |
| 588 | POP (sy, section_dummy, value2); |
| 589 | PUSH (sy, section1 ? section1 : section_dummy, value2 - value1); |
| 590 | } |
| 591 | break; |
| 592 | case ieee_function_plus_enum: |
| 593 | { |
| 594 | bfd_vma value1, value2; |
| 595 | asection *section1; |
| 596 | asection *section2; |
| 597 | ieee_symbol_index_type sy1; |
| 598 | ieee_symbol_index_type sy2; |
| 599 | |
| 600 | next_byte (&(ieee->h)); |
| 601 | |
| 602 | POP (sy1, section1, value1); |
| 603 | POP (sy2, section2, value2); |
| 604 | PUSH (sy1.letter ? sy1 : sy2, |
| 605 | bfd_is_abs_section (section1) ? section2 : section1, |
| 606 | value1 + value2); |
| 607 | } |
| 608 | break; |
| 609 | default: |
| 610 | { |
| 611 | bfd_vma va; |
| 612 | |
| 613 | BFD_ASSERT (this_byte (&(ieee->h)) < (int) ieee_variable_A_enum |
| 614 | || this_byte (&(ieee->h)) > (int) ieee_variable_Z_enum); |
| 615 | if (parse_int (&(ieee->h), &va)) |
| 616 | { |
| 617 | PUSH (NOSYMBOL, bfd_abs_section_ptr, va); |
| 618 | } |
| 619 | else |
| 620 | /* Thats all that we can understand. */ |
| 621 | loop = FALSE; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /* As far as I can see there is a bug in the Microtec IEEE output |
| 627 | which I'm using to scan, whereby the comma operator is omitted |
| 628 | sometimes in an expression, giving expressions with too many |
| 629 | terms. We can tell if that's the case by ensuring that |
| 630 | sp == stack here. If not, then we've pushed something too far, |
| 631 | so we keep adding. */ |
| 632 | while (sp != stack + 1) |
| 633 | { |
| 634 | asection *section1; |
| 635 | ieee_symbol_index_type sy1; |
| 636 | |
| 637 | POP (sy1, section1, *extra); |
| 638 | } |
| 639 | |
| 640 | POP (*symbol, dummy, *value); |
| 641 | if (section) |
| 642 | *section = dummy; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | #define ieee_seek(ieee, offset) \ |
| 647 | do \ |
| 648 | { \ |
| 649 | ieee->h.input_p = ieee->h.first_byte + offset; \ |
| 650 | ieee->h.last_byte = (ieee->h.first_byte \ |
| 651 | + ieee_part_after (ieee, offset)); \ |
| 652 | } \ |
| 653 | while (0) |
| 654 | |
| 655 | #define ieee_pos(ieee) \ |
| 656 | (ieee->h.input_p - ieee->h.first_byte) |
| 657 | |
| 658 | /* Find the first part of the ieee file after HERE. */ |
| 659 | |
| 660 | static file_ptr |
| 661 | ieee_part_after (ieee_data_type *ieee, file_ptr here) |
| 662 | { |
| 663 | int part; |
| 664 | file_ptr after = ieee->w.r.me_record; |
| 665 | |
| 666 | /* File parts can come in any order, except that module end is |
| 667 | guaranteed to be last (and the header first). */ |
| 668 | for (part = 0; part < N_W_VARIABLES; part++) |
| 669 | if (ieee->w.offset[part] > here && after > ieee->w.offset[part]) |
| 670 | after = ieee->w.offset[part]; |
| 671 | |
| 672 | return after; |
| 673 | } |
| 674 | |
| 675 | static unsigned int last_index; |
| 676 | static char last_type; /* Is the index for an X or a D. */ |
| 677 | |
| 678 | static ieee_symbol_type * |
| 679 | get_symbol (bfd *abfd ATTRIBUTE_UNUSED, |
| 680 | ieee_data_type *ieee, |
| 681 | ieee_symbol_type *last_symbol, |
| 682 | unsigned int *symbol_count, |
| 683 | ieee_symbol_type ***pptr, |
| 684 | unsigned int *max_index, |
| 685 | int this_type) |
| 686 | { |
| 687 | /* Need a new symbol. */ |
| 688 | unsigned int new_index = must_parse_int (&(ieee->h)); |
| 689 | |
| 690 | if (new_index != last_index || this_type != last_type) |
| 691 | { |
| 692 | ieee_symbol_type *new_symbol; |
| 693 | bfd_size_type amt = sizeof (ieee_symbol_type); |
| 694 | |
| 695 | new_symbol = bfd_alloc (ieee->h.abfd, amt); |
| 696 | if (!new_symbol) |
| 697 | return NULL; |
| 698 | |
| 699 | new_symbol->index = new_index; |
| 700 | last_index = new_index; |
| 701 | (*symbol_count)++; |
| 702 | **pptr = new_symbol; |
| 703 | *pptr = &new_symbol->next; |
| 704 | if (new_index > *max_index) |
| 705 | *max_index = new_index; |
| 706 | |
| 707 | last_type = this_type; |
| 708 | new_symbol->symbol.section = bfd_abs_section_ptr; |
| 709 | return new_symbol; |
| 710 | } |
| 711 | return last_symbol; |
| 712 | } |
| 713 | |
| 714 | static bfd_boolean |
| 715 | ieee_slurp_external_symbols (bfd *abfd) |
| 716 | { |
| 717 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 718 | file_ptr offset = ieee->w.r.external_part; |
| 719 | |
| 720 | ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols; |
| 721 | ieee_symbol_type **prev_reference_ptr = &ieee->external_reference; |
| 722 | ieee_symbol_type *symbol = NULL; |
| 723 | unsigned int symbol_count = 0; |
| 724 | bfd_boolean loop = TRUE; |
| 725 | |
| 726 | last_index = 0xffffff; |
| 727 | ieee->symbol_table_full = TRUE; |
| 728 | |
| 729 | ieee_seek (ieee, offset); |
| 730 | |
| 731 | while (loop) |
| 732 | { |
| 733 | switch (this_byte (&(ieee->h))) |
| 734 | { |
| 735 | case ieee_nn_record: |
| 736 | next_byte (&(ieee->h)); |
| 737 | |
| 738 | symbol = get_symbol (abfd, ieee, symbol, &symbol_count, |
| 739 | & prev_symbols_ptr, |
| 740 | & ieee->external_symbol_max_index, 'I'); |
| 741 | if (symbol == NULL) |
| 742 | return FALSE; |
| 743 | |
| 744 | symbol->symbol.the_bfd = abfd; |
| 745 | symbol->symbol.name = read_id (&(ieee->h)); |
| 746 | symbol->symbol.udata.p = NULL; |
| 747 | symbol->symbol.flags = BSF_NO_FLAGS; |
| 748 | break; |
| 749 | case ieee_external_symbol_enum: |
| 750 | next_byte (&(ieee->h)); |
| 751 | |
| 752 | symbol = get_symbol (abfd, ieee, symbol, &symbol_count, |
| 753 | &prev_symbols_ptr, |
| 754 | &ieee->external_symbol_max_index, 'D'); |
| 755 | if (symbol == NULL) |
| 756 | return FALSE; |
| 757 | |
| 758 | BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index); |
| 759 | |
| 760 | symbol->symbol.the_bfd = abfd; |
| 761 | symbol->symbol.name = read_id (&(ieee->h)); |
| 762 | symbol->symbol.udata.p = NULL; |
| 763 | symbol->symbol.flags = BSF_NO_FLAGS; |
| 764 | break; |
| 765 | case ieee_attribute_record_enum >> 8: |
| 766 | { |
| 767 | unsigned int symbol_name_index; |
| 768 | unsigned int symbol_type_index; |
| 769 | unsigned int symbol_attribute_def; |
| 770 | bfd_vma value; |
| 771 | |
| 772 | switch (read_2bytes (&ieee->h)) |
| 773 | { |
| 774 | case ieee_attribute_record_enum: |
| 775 | symbol_name_index = must_parse_int (&(ieee->h)); |
| 776 | symbol_type_index = must_parse_int (&(ieee->h)); |
| 777 | symbol_attribute_def = must_parse_int (&(ieee->h)); |
| 778 | switch (symbol_attribute_def) |
| 779 | { |
| 780 | case 8: |
| 781 | case 19: |
| 782 | parse_int (&ieee->h, &value); |
| 783 | break; |
| 784 | default: |
| 785 | (*_bfd_error_handler) |
| 786 | (_("%B: unimplemented ATI record %u for symbol %u"), |
| 787 | abfd, symbol_attribute_def, symbol_name_index); |
| 788 | bfd_set_error (bfd_error_bad_value); |
| 789 | return FALSE; |
| 790 | break; |
| 791 | } |
| 792 | break; |
| 793 | case ieee_external_reference_info_record_enum: |
| 794 | /* Skip over ATX record. */ |
| 795 | parse_int (&(ieee->h), &value); |
| 796 | parse_int (&(ieee->h), &value); |
| 797 | parse_int (&(ieee->h), &value); |
| 798 | parse_int (&(ieee->h), &value); |
| 799 | break; |
| 800 | case ieee_atn_record_enum: |
| 801 | /* We may get call optimization information here, |
| 802 | which we just ignore. The format is |
| 803 | {$F1}${CE}{index}{$00}{$3F}{$3F}{#_of_ASNs}. */ |
| 804 | parse_int (&ieee->h, &value); |
| 805 | parse_int (&ieee->h, &value); |
| 806 | parse_int (&ieee->h, &value); |
| 807 | if (value != 0x3f) |
| 808 | { |
| 809 | (*_bfd_error_handler) |
| 810 | (_("%B: unexpected ATN type %d in external part"), |
| 811 | abfd, (int) value); |
| 812 | bfd_set_error (bfd_error_bad_value); |
| 813 | return FALSE; |
| 814 | } |
| 815 | parse_int (&ieee->h, &value); |
| 816 | parse_int (&ieee->h, &value); |
| 817 | while (value > 0) |
| 818 | { |
| 819 | bfd_vma val1; |
| 820 | |
| 821 | --value; |
| 822 | |
| 823 | switch (read_2bytes (&ieee->h)) |
| 824 | { |
| 825 | case ieee_asn_record_enum: |
| 826 | parse_int (&ieee->h, &val1); |
| 827 | parse_int (&ieee->h, &val1); |
| 828 | break; |
| 829 | |
| 830 | default: |
| 831 | (*_bfd_error_handler) |
| 832 | (_("%B: unexpected type after ATN"), abfd); |
| 833 | bfd_set_error (bfd_error_bad_value); |
| 834 | return FALSE; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | break; |
| 840 | case ieee_value_record_enum >> 8: |
| 841 | { |
| 842 | unsigned int symbol_name_index; |
| 843 | ieee_symbol_index_type symbol_ignore; |
| 844 | bfd_boolean pcrel_ignore; |
| 845 | unsigned int extra; |
| 846 | |
| 847 | next_byte (&(ieee->h)); |
| 848 | next_byte (&(ieee->h)); |
| 849 | |
| 850 | symbol_name_index = must_parse_int (&(ieee->h)); |
| 851 | parse_expression (ieee, |
| 852 | &symbol->symbol.value, |
| 853 | &symbol_ignore, |
| 854 | &pcrel_ignore, |
| 855 | &extra, |
| 856 | &symbol->symbol.section); |
| 857 | |
| 858 | /* Fully linked IEEE-695 files tend to give every symbol |
| 859 | an absolute value. Try to convert that back into a |
| 860 | section relative value. FIXME: This won't always to |
| 861 | the right thing. */ |
| 862 | if (bfd_is_abs_section (symbol->symbol.section) |
| 863 | && (abfd->flags & HAS_RELOC) == 0) |
| 864 | { |
| 865 | bfd_vma val; |
| 866 | asection *s; |
| 867 | |
| 868 | val = symbol->symbol.value; |
| 869 | for (s = abfd->sections; s != NULL; s = s->next) |
| 870 | { |
| 871 | if (val >= s->vma && val < s->vma + s->size) |
| 872 | { |
| 873 | symbol->symbol.section = s; |
| 874 | symbol->symbol.value -= s->vma; |
| 875 | break; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT; |
| 881 | |
| 882 | } |
| 883 | break; |
| 884 | case ieee_weak_external_reference_enum: |
| 885 | { |
| 886 | bfd_vma size; |
| 887 | bfd_vma value; |
| 888 | |
| 889 | next_byte (&(ieee->h)); |
| 890 | /* Throw away the external reference index. */ |
| 891 | (void) must_parse_int (&(ieee->h)); |
| 892 | /* Fetch the default size if not resolved. */ |
| 893 | size = must_parse_int (&(ieee->h)); |
| 894 | /* Fetch the default value if available. */ |
| 895 | if (! parse_int (&(ieee->h), &value)) |
| 896 | value = 0; |
| 897 | /* This turns into a common. */ |
| 898 | symbol->symbol.section = bfd_com_section_ptr; |
| 899 | symbol->symbol.value = size; |
| 900 | } |
| 901 | break; |
| 902 | |
| 903 | case ieee_external_reference_enum: |
| 904 | next_byte (&(ieee->h)); |
| 905 | |
| 906 | symbol = get_symbol (abfd, ieee, symbol, &symbol_count, |
| 907 | &prev_reference_ptr, |
| 908 | &ieee->external_reference_max_index, 'X'); |
| 909 | if (symbol == NULL) |
| 910 | return FALSE; |
| 911 | |
| 912 | symbol->symbol.the_bfd = abfd; |
| 913 | symbol->symbol.name = read_id (&(ieee->h)); |
| 914 | symbol->symbol.udata.p = NULL; |
| 915 | symbol->symbol.section = bfd_und_section_ptr; |
| 916 | symbol->symbol.value = (bfd_vma) 0; |
| 917 | symbol->symbol.flags = 0; |
| 918 | |
| 919 | BFD_ASSERT (symbol->index >= ieee->external_reference_min_index); |
| 920 | break; |
| 921 | |
| 922 | default: |
| 923 | loop = FALSE; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (ieee->external_symbol_max_index != 0) |
| 928 | { |
| 929 | ieee->external_symbol_count = |
| 930 | ieee->external_symbol_max_index - |
| 931 | ieee->external_symbol_min_index + 1; |
| 932 | } |
| 933 | else |
| 934 | ieee->external_symbol_count = 0; |
| 935 | |
| 936 | if (ieee->external_reference_max_index != 0) |
| 937 | { |
| 938 | ieee->external_reference_count = |
| 939 | ieee->external_reference_max_index - |
| 940 | ieee->external_reference_min_index + 1; |
| 941 | } |
| 942 | else |
| 943 | ieee->external_reference_count = 0; |
| 944 | |
| 945 | abfd->symcount = |
| 946 | ieee->external_reference_count + ieee->external_symbol_count; |
| 947 | |
| 948 | if (symbol_count != abfd->symcount) |
| 949 | /* There are gaps in the table -- */ |
| 950 | ieee->symbol_table_full = FALSE; |
| 951 | |
| 952 | *prev_symbols_ptr = NULL; |
| 953 | *prev_reference_ptr = NULL; |
| 954 | |
| 955 | return TRUE; |
| 956 | } |
| 957 | |
| 958 | static bfd_boolean |
| 959 | ieee_slurp_symbol_table (bfd *abfd) |
| 960 | { |
| 961 | if (! IEEE_DATA (abfd)->read_symbols) |
| 962 | { |
| 963 | if (! ieee_slurp_external_symbols (abfd)) |
| 964 | return FALSE; |
| 965 | IEEE_DATA (abfd)->read_symbols = TRUE; |
| 966 | } |
| 967 | return TRUE; |
| 968 | } |
| 969 | |
| 970 | static long |
| 971 | ieee_get_symtab_upper_bound (bfd *abfd) |
| 972 | { |
| 973 | if (! ieee_slurp_symbol_table (abfd)) |
| 974 | return -1; |
| 975 | |
| 976 | return (abfd->symcount != 0) ? |
| 977 | (abfd->symcount + 1) * (sizeof (ieee_symbol_type *)) : 0; |
| 978 | } |
| 979 | |
| 980 | /* Move from our internal lists to the canon table, and insert in |
| 981 | symbol index order. */ |
| 982 | |
| 983 | extern const bfd_target ieee_vec; |
| 984 | |
| 985 | static long |
| 986 | ieee_canonicalize_symtab (bfd *abfd, asymbol **location) |
| 987 | { |
| 988 | ieee_symbol_type *symp; |
| 989 | static bfd dummy_bfd; |
| 990 | static asymbol empty_symbol = |
| 991 | { |
| 992 | &dummy_bfd, |
| 993 | " ieee empty", |
| 994 | (symvalue) 0, |
| 995 | BSF_DEBUGGING, |
| 996 | bfd_abs_section_ptr |
| 997 | #ifdef __STDC__ |
| 998 | /* K&R compilers can't initialise unions. */ |
| 999 | , { 0 } |
| 1000 | #endif |
| 1001 | }; |
| 1002 | |
| 1003 | if (abfd->symcount) |
| 1004 | { |
| 1005 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 1006 | |
| 1007 | dummy_bfd.xvec = &ieee_vec; |
| 1008 | if (! ieee_slurp_symbol_table (abfd)) |
| 1009 | return -1; |
| 1010 | |
| 1011 | if (! ieee->symbol_table_full) |
| 1012 | { |
| 1013 | /* Arrgh - there are gaps in the table, run through and fill them |
| 1014 | up with pointers to a null place. */ |
| 1015 | unsigned int i; |
| 1016 | |
| 1017 | for (i = 0; i < abfd->symcount; i++) |
| 1018 | location[i] = &empty_symbol; |
| 1019 | } |
| 1020 | |
| 1021 | ieee->external_symbol_base_offset = -ieee->external_symbol_min_index; |
| 1022 | for (symp = IEEE_DATA (abfd)->external_symbols; |
| 1023 | symp != (ieee_symbol_type *) NULL; |
| 1024 | symp = symp->next) |
| 1025 | /* Place into table at correct index locations. */ |
| 1026 | location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol; |
| 1027 | |
| 1028 | /* The external refs are indexed in a bit. */ |
| 1029 | ieee->external_reference_base_offset = |
| 1030 | -ieee->external_reference_min_index + ieee->external_symbol_count; |
| 1031 | |
| 1032 | for (symp = IEEE_DATA (abfd)->external_reference; |
| 1033 | symp != (ieee_symbol_type *) NULL; |
| 1034 | symp = symp->next) |
| 1035 | location[symp->index + ieee->external_reference_base_offset] = |
| 1036 | &symp->symbol; |
| 1037 | } |
| 1038 | |
| 1039 | if (abfd->symcount) |
| 1040 | location[abfd->symcount] = (asymbol *) NULL; |
| 1041 | |
| 1042 | return abfd->symcount; |
| 1043 | } |
| 1044 | |
| 1045 | static asection * |
| 1046 | get_section_entry (bfd *abfd, ieee_data_type *ieee, unsigned int index) |
| 1047 | { |
| 1048 | if (index >= ieee->section_table_size) |
| 1049 | { |
| 1050 | unsigned int c, i; |
| 1051 | asection **n; |
| 1052 | bfd_size_type amt; |
| 1053 | |
| 1054 | c = ieee->section_table_size; |
| 1055 | if (c == 0) |
| 1056 | c = 20; |
| 1057 | while (c <= index) |
| 1058 | c *= 2; |
| 1059 | |
| 1060 | amt = c; |
| 1061 | amt *= sizeof (asection *); |
| 1062 | n = bfd_realloc (ieee->section_table, amt); |
| 1063 | if (n == NULL) |
| 1064 | return NULL; |
| 1065 | |
| 1066 | for (i = ieee->section_table_size; i < c; i++) |
| 1067 | n[i] = NULL; |
| 1068 | |
| 1069 | ieee->section_table = n; |
| 1070 | ieee->section_table_size = c; |
| 1071 | } |
| 1072 | |
| 1073 | if (ieee->section_table[index] == (asection *) NULL) |
| 1074 | { |
| 1075 | char *tmp = bfd_alloc (abfd, (bfd_size_type) 11); |
| 1076 | asection *section; |
| 1077 | |
| 1078 | if (!tmp) |
| 1079 | return NULL; |
| 1080 | sprintf (tmp, " fsec%4d", index); |
| 1081 | section = bfd_make_section (abfd, tmp); |
| 1082 | ieee->section_table[index] = section; |
| 1083 | section->flags = SEC_NO_FLAGS; |
| 1084 | section->target_index = index; |
| 1085 | ieee->section_table[index] = section; |
| 1086 | } |
| 1087 | return ieee->section_table[index]; |
| 1088 | } |
| 1089 | |
| 1090 | static void |
| 1091 | ieee_slurp_sections (bfd *abfd) |
| 1092 | { |
| 1093 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 1094 | file_ptr offset = ieee->w.r.section_part; |
| 1095 | char *name; |
| 1096 | |
| 1097 | if (offset != 0) |
| 1098 | { |
| 1099 | bfd_byte section_type[3]; |
| 1100 | |
| 1101 | ieee_seek (ieee, offset); |
| 1102 | while (TRUE) |
| 1103 | { |
| 1104 | switch (this_byte (&(ieee->h))) |
| 1105 | { |
| 1106 | case ieee_section_type_enum: |
| 1107 | { |
| 1108 | asection *section; |
| 1109 | unsigned int section_index; |
| 1110 | |
| 1111 | next_byte (&(ieee->h)); |
| 1112 | section_index = must_parse_int (&(ieee->h)); |
| 1113 | |
| 1114 | section = get_section_entry (abfd, ieee, section_index); |
| 1115 | |
| 1116 | section_type[0] = this_byte_and_next (&(ieee->h)); |
| 1117 | |
| 1118 | /* Set minimal section attributes. Attributes are |
| 1119 | extended later, based on section contents. */ |
| 1120 | switch (section_type[0]) |
| 1121 | { |
| 1122 | case 0xC1: |
| 1123 | /* Normal attributes for absolute sections. */ |
| 1124 | section_type[1] = this_byte (&(ieee->h)); |
| 1125 | section->flags = SEC_ALLOC; |
| 1126 | switch (section_type[1]) |
| 1127 | { |
| 1128 | /* AS Absolute section attributes. */ |
| 1129 | case 0xD3: |
| 1130 | next_byte (&(ieee->h)); |
| 1131 | section_type[2] = this_byte (&(ieee->h)); |
| 1132 | switch (section_type[2]) |
| 1133 | { |
| 1134 | case 0xD0: |
| 1135 | /* Normal code. */ |
| 1136 | next_byte (&(ieee->h)); |
| 1137 | section->flags |= SEC_CODE; |
| 1138 | break; |
| 1139 | case 0xC4: |
| 1140 | /* Normal data. */ |
| 1141 | next_byte (&(ieee->h)); |
| 1142 | section->flags |= SEC_DATA; |
| 1143 | break; |
| 1144 | case 0xD2: |
| 1145 | next_byte (&(ieee->h)); |
| 1146 | /* Normal rom data. */ |
| 1147 | section->flags |= SEC_ROM | SEC_DATA; |
| 1148 | break; |
| 1149 | default: |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | break; |
| 1154 | |
| 1155 | /* Named relocatable sections (type C). */ |
| 1156 | case 0xC3: |
| 1157 | section_type[1] = this_byte (&(ieee->h)); |
| 1158 | section->flags = SEC_ALLOC; |
| 1159 | switch (section_type[1]) |
| 1160 | { |
| 1161 | case 0xD0: /* Normal code (CP). */ |
| 1162 | next_byte (&(ieee->h)); |
| 1163 | section->flags |= SEC_CODE; |
| 1164 | break; |
| 1165 | case 0xC4: /* Normal data (CD). */ |
| 1166 | next_byte (&(ieee->h)); |
| 1167 | section->flags |= SEC_DATA; |
| 1168 | break; |
| 1169 | case 0xD2: /* Normal rom data (CR). */ |
| 1170 | next_byte (&(ieee->h)); |
| 1171 | section->flags |= SEC_ROM | SEC_DATA; |
| 1172 | break; |
| 1173 | default: |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /* Read section name, use it if non empty. */ |
| 1179 | name = read_id (&ieee->h); |
| 1180 | if (name[0]) |
| 1181 | section->name = name; |
| 1182 | |
| 1183 | /* Skip these fields, which we don't care about. */ |
| 1184 | { |
| 1185 | bfd_vma parent, brother, context; |
| 1186 | |
| 1187 | parse_int (&(ieee->h), &parent); |
| 1188 | parse_int (&(ieee->h), &brother); |
| 1189 | parse_int (&(ieee->h), &context); |
| 1190 | } |
| 1191 | } |
| 1192 | break; |
| 1193 | case ieee_section_alignment_enum: |
| 1194 | { |
| 1195 | unsigned int section_index; |
| 1196 | bfd_vma value; |
| 1197 | asection *section; |
| 1198 | |
| 1199 | next_byte (&(ieee->h)); |
| 1200 | section_index = must_parse_int (&ieee->h); |
| 1201 | section = get_section_entry (abfd, ieee, section_index); |
| 1202 | if (section_index > ieee->section_count) |
| 1203 | ieee->section_count = section_index; |
| 1204 | |
| 1205 | section->alignment_power = |
| 1206 | bfd_log2 (must_parse_int (&ieee->h)); |
| 1207 | (void) parse_int (&(ieee->h), &value); |
| 1208 | } |
| 1209 | break; |
| 1210 | case ieee_e2_first_byte_enum: |
| 1211 | { |
| 1212 | asection *section; |
| 1213 | ieee_record_enum_type t; |
| 1214 | |
| 1215 | t = (ieee_record_enum_type) (read_2bytes (&(ieee->h))); |
| 1216 | switch (t) |
| 1217 | { |
| 1218 | case ieee_section_size_enum: |
| 1219 | section = ieee->section_table[must_parse_int (&(ieee->h))]; |
| 1220 | section->size = must_parse_int (&(ieee->h)); |
| 1221 | break; |
| 1222 | case ieee_physical_region_size_enum: |
| 1223 | section = ieee->section_table[must_parse_int (&(ieee->h))]; |
| 1224 | section->size = must_parse_int (&(ieee->h)); |
| 1225 | break; |
| 1226 | case ieee_region_base_address_enum: |
| 1227 | section = ieee->section_table[must_parse_int (&(ieee->h))]; |
| 1228 | section->vma = must_parse_int (&(ieee->h)); |
| 1229 | section->lma = section->vma; |
| 1230 | break; |
| 1231 | case ieee_mau_size_enum: |
| 1232 | must_parse_int (&(ieee->h)); |
| 1233 | must_parse_int (&(ieee->h)); |
| 1234 | break; |
| 1235 | case ieee_m_value_enum: |
| 1236 | must_parse_int (&(ieee->h)); |
| 1237 | must_parse_int (&(ieee->h)); |
| 1238 | break; |
| 1239 | case ieee_section_base_address_enum: |
| 1240 | section = ieee->section_table[must_parse_int (&(ieee->h))]; |
| 1241 | section->vma = must_parse_int (&(ieee->h)); |
| 1242 | section->lma = section->vma; |
| 1243 | break; |
| 1244 | case ieee_section_offset_enum: |
| 1245 | (void) must_parse_int (&(ieee->h)); |
| 1246 | (void) must_parse_int (&(ieee->h)); |
| 1247 | break; |
| 1248 | default: |
| 1249 | return; |
| 1250 | } |
| 1251 | } |
| 1252 | break; |
| 1253 | default: |
| 1254 | return; |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* Make a section for the debugging information, if any. We don't try |
| 1261 | to interpret the debugging information; we just point the section |
| 1262 | at the area in the file so that program which understand can dig it |
| 1263 | out. */ |
| 1264 | |
| 1265 | static bfd_boolean |
| 1266 | ieee_slurp_debug (bfd *abfd) |
| 1267 | { |
| 1268 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 1269 | asection *sec; |
| 1270 | file_ptr debug_end; |
| 1271 | |
| 1272 | if (ieee->w.r.debug_information_part == 0) |
| 1273 | return TRUE; |
| 1274 | |
| 1275 | sec = bfd_make_section (abfd, ".debug"); |
| 1276 | if (sec == NULL) |
| 1277 | return FALSE; |
| 1278 | sec->flags |= SEC_DEBUGGING | SEC_HAS_CONTENTS; |
| 1279 | sec->filepos = ieee->w.r.debug_information_part; |
| 1280 | |
| 1281 | debug_end = ieee_part_after (ieee, ieee->w.r.debug_information_part); |
| 1282 | sec->size = debug_end - ieee->w.r.debug_information_part; |
| 1283 | |
| 1284 | return TRUE; |
| 1285 | } |
| 1286 | \f |
| 1287 | /* Archive stuff. */ |
| 1288 | |
| 1289 | static const bfd_target * |
| 1290 | ieee_archive_p (bfd *abfd) |
| 1291 | { |
| 1292 | char *library; |
| 1293 | unsigned int i; |
| 1294 | unsigned char buffer[512]; |
| 1295 | file_ptr buffer_offset = 0; |
| 1296 | ieee_ar_data_type *save = abfd->tdata.ieee_ar_data; |
| 1297 | ieee_ar_data_type *ieee; |
| 1298 | bfd_size_type alc_elts; |
| 1299 | ieee_ar_obstack_type *elts = NULL; |
| 1300 | bfd_size_type amt = sizeof (ieee_ar_data_type); |
| 1301 | |
| 1302 | abfd->tdata.ieee_ar_data = bfd_alloc (abfd, amt); |
| 1303 | if (!abfd->tdata.ieee_ar_data) |
| 1304 | goto error_ret_restore; |
| 1305 | ieee = IEEE_AR_DATA (abfd); |
| 1306 | |
| 1307 | /* Ignore the return value here. It doesn't matter if we don't read |
| 1308 | the entire buffer. We might have a very small ieee file. */ |
| 1309 | bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd); |
| 1310 | |
| 1311 | ieee->h.first_byte = buffer; |
| 1312 | ieee->h.input_p = buffer; |
| 1313 | |
| 1314 | ieee->h.abfd = abfd; |
| 1315 | |
| 1316 | if (this_byte (&(ieee->h)) != Module_Beginning) |
| 1317 | goto got_wrong_format_error; |
| 1318 | |
| 1319 | next_byte (&(ieee->h)); |
| 1320 | library = read_id (&(ieee->h)); |
| 1321 | if (strcmp (library, "LIBRARY") != 0) |
| 1322 | goto got_wrong_format_error; |
| 1323 | |
| 1324 | /* Throw away the filename. */ |
| 1325 | read_id (&(ieee->h)); |
| 1326 | |
| 1327 | ieee->element_count = 0; |
| 1328 | ieee->element_index = 0; |
| 1329 | |
| 1330 | next_byte (&(ieee->h)); /* Drop the ad part. */ |
| 1331 | must_parse_int (&(ieee->h)); /* And the two dummy numbers. */ |
| 1332 | must_parse_int (&(ieee->h)); |
| 1333 | |
| 1334 | alc_elts = 10; |
| 1335 | elts = bfd_malloc (alc_elts * sizeof *elts); |
| 1336 | if (elts == NULL) |
| 1337 | goto error_return; |
| 1338 | |
| 1339 | /* Read the index of the BB table. */ |
| 1340 | while (1) |
| 1341 | { |
| 1342 | int rec; |
| 1343 | ieee_ar_obstack_type *t; |
| 1344 | |
| 1345 | rec = read_2bytes (&(ieee->h)); |
| 1346 | if (rec != (int) ieee_assign_value_to_variable_enum) |
| 1347 | break; |
| 1348 | |
| 1349 | if (ieee->element_count >= alc_elts) |
| 1350 | { |
| 1351 | ieee_ar_obstack_type *n; |
| 1352 | |
| 1353 | alc_elts *= 2; |
| 1354 | n = bfd_realloc (elts, alc_elts * sizeof (* elts)); |
| 1355 | if (n == NULL) |
| 1356 | goto error_return; |
| 1357 | elts = n; |
| 1358 | } |
| 1359 | |
| 1360 | t = &elts[ieee->element_count]; |
| 1361 | ieee->element_count++; |
| 1362 | |
| 1363 | must_parse_int (&(ieee->h)); |
| 1364 | t->file_offset = must_parse_int (&(ieee->h)); |
| 1365 | t->abfd = (bfd *) NULL; |
| 1366 | |
| 1367 | /* Make sure that we don't go over the end of the buffer. */ |
| 1368 | if ((size_t) ieee_pos (IEEE_DATA (abfd)) > sizeof (buffer) / 2) |
| 1369 | { |
| 1370 | /* Past half way, reseek and reprime. */ |
| 1371 | buffer_offset += ieee_pos (IEEE_DATA (abfd)); |
| 1372 | if (bfd_seek (abfd, buffer_offset, SEEK_SET) != 0) |
| 1373 | goto error_return; |
| 1374 | |
| 1375 | /* Again ignore return value of bfd_bread. */ |
| 1376 | bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd); |
| 1377 | ieee->h.first_byte = buffer; |
| 1378 | ieee->h.input_p = buffer; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | amt = ieee->element_count; |
| 1383 | amt *= sizeof *ieee->elements; |
| 1384 | ieee->elements = bfd_alloc (abfd, amt); |
| 1385 | if (ieee->elements == NULL) |
| 1386 | goto error_return; |
| 1387 | |
| 1388 | memcpy (ieee->elements, elts, (size_t) amt); |
| 1389 | free (elts); |
| 1390 | elts = NULL; |
| 1391 | |
| 1392 | /* Now scan the area again, and replace BB offsets with file offsets. */ |
| 1393 | for (i = 2; i < ieee->element_count; i++) |
| 1394 | { |
| 1395 | if (bfd_seek (abfd, ieee->elements[i].file_offset, SEEK_SET) != 0) |
| 1396 | goto error_return; |
| 1397 | |
| 1398 | /* Again ignore return value of bfd_bread. */ |
| 1399 | bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd); |
| 1400 | ieee->h.first_byte = buffer; |
| 1401 | ieee->h.input_p = buffer; |
| 1402 | |
| 1403 | next_byte (&(ieee->h)); /* Drop F8. */ |
| 1404 | next_byte (&(ieee->h)); /* Drop 14. */ |
| 1405 | must_parse_int (&(ieee->h)); /* Drop size of block. */ |
| 1406 | |
| 1407 | if (must_parse_int (&(ieee->h)) != 0) |
| 1408 | /* This object has been deleted. */ |
| 1409 | ieee->elements[i].file_offset = 0; |
| 1410 | else |
| 1411 | ieee->elements[i].file_offset = must_parse_int (&(ieee->h)); |
| 1412 | } |
| 1413 | |
| 1414 | /* abfd->has_armap = ;*/ |
| 1415 | |
| 1416 | return abfd->xvec; |
| 1417 | |
| 1418 | got_wrong_format_error: |
| 1419 | bfd_set_error (bfd_error_wrong_format); |
| 1420 | error_return: |
| 1421 | if (elts != NULL) |
| 1422 | free (elts); |
| 1423 | bfd_release (abfd, ieee); |
| 1424 | error_ret_restore: |
| 1425 | abfd->tdata.ieee_ar_data = save; |
| 1426 | |
| 1427 | return NULL; |
| 1428 | } |
| 1429 | |
| 1430 | static bfd_boolean |
| 1431 | ieee_mkobject (bfd *abfd) |
| 1432 | { |
| 1433 | bfd_size_type amt; |
| 1434 | |
| 1435 | output_ptr_start = NULL; |
| 1436 | output_ptr = NULL; |
| 1437 | output_ptr_end = NULL; |
| 1438 | input_ptr_start = NULL; |
| 1439 | input_ptr = NULL; |
| 1440 | input_ptr_end = NULL; |
| 1441 | input_bfd = NULL; |
| 1442 | output_bfd = NULL; |
| 1443 | output_buffer = 0; |
| 1444 | amt = sizeof (ieee_data_type); |
| 1445 | abfd->tdata.ieee_data = bfd_zalloc (abfd, amt); |
| 1446 | return abfd->tdata.ieee_data != NULL; |
| 1447 | } |
| 1448 | |
| 1449 | static bfd_boolean |
| 1450 | do_one (ieee_data_type *ieee, |
| 1451 | ieee_per_section_type *current_map, |
| 1452 | unsigned char *location_ptr, |
| 1453 | asection *s, |
| 1454 | int iterations) |
| 1455 | { |
| 1456 | switch (this_byte (&(ieee->h))) |
| 1457 | { |
| 1458 | case ieee_load_constant_bytes_enum: |
| 1459 | { |
| 1460 | unsigned int number_of_maus; |
| 1461 | unsigned int i; |
| 1462 | |
| 1463 | next_byte (&(ieee->h)); |
| 1464 | number_of_maus = must_parse_int (&(ieee->h)); |
| 1465 | |
| 1466 | for (i = 0; i < number_of_maus; i++) |
| 1467 | { |
| 1468 | location_ptr[current_map->pc++] = this_byte (&(ieee->h)); |
| 1469 | next_byte (&(ieee->h)); |
| 1470 | } |
| 1471 | } |
| 1472 | break; |
| 1473 | |
| 1474 | case ieee_load_with_relocation_enum: |
| 1475 | { |
| 1476 | bfd_boolean loop = TRUE; |
| 1477 | |
| 1478 | next_byte (&(ieee->h)); |
| 1479 | while (loop) |
| 1480 | { |
| 1481 | switch (this_byte (&(ieee->h))) |
| 1482 | { |
| 1483 | case ieee_variable_R_enum: |
| 1484 | |
| 1485 | case ieee_function_signed_open_b_enum: |
| 1486 | case ieee_function_unsigned_open_b_enum: |
| 1487 | case ieee_function_either_open_b_enum: |
| 1488 | { |
| 1489 | unsigned int extra = 4; |
| 1490 | bfd_boolean pcrel = FALSE; |
| 1491 | asection *section; |
| 1492 | ieee_reloc_type *r; |
| 1493 | bfd_size_type amt = sizeof (ieee_reloc_type); |
| 1494 | |
| 1495 | r = bfd_alloc (ieee->h.abfd, amt); |
| 1496 | if (!r) |
| 1497 | return FALSE; |
| 1498 | |
| 1499 | *(current_map->reloc_tail_ptr) = r; |
| 1500 | current_map->reloc_tail_ptr = &r->next; |
| 1501 | r->next = (ieee_reloc_type *) NULL; |
| 1502 | next_byte (&(ieee->h)); |
| 1503 | /* abort();*/ |
| 1504 | r->relent.sym_ptr_ptr = 0; |
| 1505 | parse_expression (ieee, |
| 1506 | &r->relent.addend, |
| 1507 | &r->symbol, |
| 1508 | &pcrel, &extra, §ion); |
| 1509 | r->relent.address = current_map->pc; |
| 1510 | s->flags |= SEC_RELOC; |
| 1511 | s->owner->flags |= HAS_RELOC; |
| 1512 | s->reloc_count++; |
| 1513 | if (r->relent.sym_ptr_ptr == NULL && section != NULL) |
| 1514 | r->relent.sym_ptr_ptr = section->symbol_ptr_ptr; |
| 1515 | |
| 1516 | if (this_byte (&(ieee->h)) == (int) ieee_comma) |
| 1517 | { |
| 1518 | next_byte (&(ieee->h)); |
| 1519 | /* Fetch number of bytes to pad. */ |
| 1520 | extra = must_parse_int (&(ieee->h)); |
| 1521 | }; |
| 1522 | |
| 1523 | switch (this_byte (&(ieee->h))) |
| 1524 | { |
| 1525 | case ieee_function_signed_close_b_enum: |
| 1526 | next_byte (&(ieee->h)); |
| 1527 | break; |
| 1528 | case ieee_function_unsigned_close_b_enum: |
| 1529 | next_byte (&(ieee->h)); |
| 1530 | break; |
| 1531 | case ieee_function_either_close_b_enum: |
| 1532 | next_byte (&(ieee->h)); |
| 1533 | break; |
| 1534 | default: |
| 1535 | break; |
| 1536 | } |
| 1537 | /* Build a relocation entry for this type. */ |
| 1538 | /* If pc rel then stick -ve pc into instruction |
| 1539 | and take out of reloc .. |
| 1540 | |
| 1541 | I've changed this. It's all too complicated. I |
| 1542 | keep 0 in the instruction now. */ |
| 1543 | |
| 1544 | switch (extra) |
| 1545 | { |
| 1546 | case 0: |
| 1547 | case 4: |
| 1548 | |
| 1549 | if (pcrel) |
| 1550 | { |
| 1551 | #if KEEPMINUSPCININST |
| 1552 | bfd_put_32 (ieee->h.abfd, -current_map->pc, |
| 1553 | location_ptr + current_map->pc); |
| 1554 | r->relent.howto = &rel32_howto; |
| 1555 | r->relent.addend -= current_map->pc; |
| 1556 | #else |
| 1557 | bfd_put_32 (ieee->h.abfd, (bfd_vma) 0, location_ptr + |
| 1558 | current_map->pc); |
| 1559 | r->relent.howto = &rel32_howto; |
| 1560 | #endif |
| 1561 | } |
| 1562 | else |
| 1563 | { |
| 1564 | bfd_put_32 (ieee->h.abfd, (bfd_vma) 0, |
| 1565 | location_ptr + current_map->pc); |
| 1566 | r->relent.howto = &abs32_howto; |
| 1567 | } |
| 1568 | current_map->pc += 4; |
| 1569 | break; |
| 1570 | case 2: |
| 1571 | if (pcrel) |
| 1572 | { |
| 1573 | #if KEEPMINUSPCININST |
| 1574 | bfd_put_16 (ieee->h.abfd, (bfd_vma) -current_map->pc, |
| 1575 | location_ptr + current_map->pc); |
| 1576 | r->relent.addend -= current_map->pc; |
| 1577 | r->relent.howto = &rel16_howto; |
| 1578 | #else |
| 1579 | |
| 1580 | bfd_put_16 (ieee->h.abfd, (bfd_vma) 0, |
| 1581 | location_ptr + current_map->pc); |
| 1582 | r->relent.howto = &rel16_howto; |
| 1583 | #endif |
| 1584 | } |
| 1585 | |
| 1586 | else |
| 1587 | { |
| 1588 | bfd_put_16 (ieee->h.abfd, (bfd_vma) 0, |
| 1589 | location_ptr + current_map->pc); |
| 1590 | r->relent.howto = &abs16_howto; |
| 1591 | } |
| 1592 | current_map->pc += 2; |
| 1593 | break; |
| 1594 | case 1: |
| 1595 | if (pcrel) |
| 1596 | { |
| 1597 | #if KEEPMINUSPCININST |
| 1598 | bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc); |
| 1599 | r->relent.addend -= current_map->pc; |
| 1600 | r->relent.howto = &rel8_howto; |
| 1601 | #else |
| 1602 | bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc); |
| 1603 | r->relent.howto = &rel8_howto; |
| 1604 | #endif |
| 1605 | } |
| 1606 | else |
| 1607 | { |
| 1608 | bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc); |
| 1609 | r->relent.howto = &abs8_howto; |
| 1610 | } |
| 1611 | current_map->pc += 1; |
| 1612 | break; |
| 1613 | |
| 1614 | default: |
| 1615 | BFD_FAIL (); |
| 1616 | return FALSE; |
| 1617 | } |
| 1618 | } |
| 1619 | break; |
| 1620 | default: |
| 1621 | { |
| 1622 | bfd_vma this_size; |
| 1623 | |
| 1624 | if (parse_int (&(ieee->h), &this_size)) |
| 1625 | { |
| 1626 | unsigned int i; |
| 1627 | |
| 1628 | for (i = 0; i < this_size; i++) |
| 1629 | { |
| 1630 | location_ptr[current_map->pc++] = this_byte (&(ieee->h)); |
| 1631 | next_byte (&(ieee->h)); |
| 1632 | } |
| 1633 | } |
| 1634 | else |
| 1635 | loop = FALSE; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /* Prevent more than the first load-item of an LR record |
| 1640 | from being repeated (MRI convention). */ |
| 1641 | if (iterations != 1) |
| 1642 | loop = FALSE; |
| 1643 | } |
| 1644 | } |
| 1645 | } |
| 1646 | return TRUE; |
| 1647 | } |
| 1648 | |
| 1649 | /* Read in all the section data and relocation stuff too. */ |
| 1650 | |
| 1651 | static bfd_boolean |
| 1652 | ieee_slurp_section_data (bfd *abfd) |
| 1653 | { |
| 1654 | bfd_byte *location_ptr = (bfd_byte *) NULL; |
| 1655 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 1656 | unsigned int section_number; |
| 1657 | ieee_per_section_type *current_map = NULL; |
| 1658 | asection *s; |
| 1659 | |
| 1660 | /* Seek to the start of the data area. */ |
| 1661 | if (ieee->read_data) |
| 1662 | return TRUE; |
| 1663 | ieee->read_data = TRUE; |
| 1664 | ieee_seek (ieee, ieee->w.r.data_part); |
| 1665 | |
| 1666 | /* Allocate enough space for all the section contents. */ |
| 1667 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
| 1668 | { |
| 1669 | ieee_per_section_type *per = ieee_per_section (s); |
| 1670 | |
| 1671 | if ((s->flags & SEC_DEBUGGING) != 0) |
| 1672 | continue; |
| 1673 | per->data = bfd_alloc (ieee->h.abfd, s->size); |
| 1674 | if (!per->data) |
| 1675 | return FALSE; |
| 1676 | per->reloc_tail_ptr = |
| 1677 | (ieee_reloc_type **) & (s->relocation); |
| 1678 | } |
| 1679 | |
| 1680 | while (TRUE) |
| 1681 | { |
| 1682 | switch (this_byte (&(ieee->h))) |
| 1683 | { |
| 1684 | /* IF we see anything strange then quit. */ |
| 1685 | default: |
| 1686 | return TRUE; |
| 1687 | |
| 1688 | case ieee_set_current_section_enum: |
| 1689 | next_byte (&(ieee->h)); |
| 1690 | section_number = must_parse_int (&(ieee->h)); |
| 1691 | s = ieee->section_table[section_number]; |
| 1692 | s->flags |= SEC_LOAD | SEC_HAS_CONTENTS; |
| 1693 | current_map = ieee_per_section (s); |
| 1694 | location_ptr = current_map->data - s->vma; |
| 1695 | /* The document I have says that Microtec's compilers reset |
| 1696 | this after a sec section, even though the standard says not |
| 1697 | to, SO... */ |
| 1698 | current_map->pc = s->vma; |
| 1699 | break; |
| 1700 | |
| 1701 | case ieee_e2_first_byte_enum: |
| 1702 | next_byte (&(ieee->h)); |
| 1703 | switch (this_byte (&(ieee->h))) |
| 1704 | { |
| 1705 | case ieee_set_current_pc_enum & 0xff: |
| 1706 | { |
| 1707 | bfd_vma value; |
| 1708 | ieee_symbol_index_type symbol; |
| 1709 | unsigned int extra; |
| 1710 | bfd_boolean pcrel; |
| 1711 | |
| 1712 | next_byte (&(ieee->h)); |
| 1713 | must_parse_int (&(ieee->h)); /* Throw away section #. */ |
| 1714 | parse_expression (ieee, &value, |
| 1715 | &symbol, |
| 1716 | &pcrel, &extra, |
| 1717 | 0); |
| 1718 | current_map->pc = value; |
| 1719 | BFD_ASSERT ((unsigned) (value - s->vma) <= s->size); |
| 1720 | } |
| 1721 | break; |
| 1722 | |
| 1723 | case ieee_value_starting_address_enum & 0xff: |
| 1724 | next_byte (&(ieee->h)); |
| 1725 | if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum) |
| 1726 | next_byte (&(ieee->h)); |
| 1727 | abfd->start_address = must_parse_int (&(ieee->h)); |
| 1728 | /* We've got to the end of the data now - */ |
| 1729 | return TRUE; |
| 1730 | default: |
| 1731 | BFD_FAIL (); |
| 1732 | return FALSE; |
| 1733 | } |
| 1734 | break; |
| 1735 | case ieee_repeat_data_enum: |
| 1736 | { |
| 1737 | /* Repeat the following LD or LR n times - we do this by |
| 1738 | remembering the stream pointer before running it and |
| 1739 | resetting it and running it n times. We special case |
| 1740 | the repetition of a repeat_data/load_constant. */ |
| 1741 | unsigned int iterations; |
| 1742 | unsigned char *start; |
| 1743 | |
| 1744 | next_byte (&(ieee->h)); |
| 1745 | iterations = must_parse_int (&(ieee->h)); |
| 1746 | start = ieee->h.input_p; |
| 1747 | if (start[0] == (int) ieee_load_constant_bytes_enum |
| 1748 | && start[1] == 1) |
| 1749 | { |
| 1750 | while (iterations != 0) |
| 1751 | { |
| 1752 | location_ptr[current_map->pc++] = start[2]; |
| 1753 | iterations--; |
| 1754 | } |
| 1755 | next_byte (&(ieee->h)); |
| 1756 | next_byte (&(ieee->h)); |
| 1757 | next_byte (&(ieee->h)); |
| 1758 | } |
| 1759 | else |
| 1760 | { |
| 1761 | while (iterations != 0) |
| 1762 | { |
| 1763 | ieee->h.input_p = start; |
| 1764 | if (!do_one (ieee, current_map, location_ptr, s, |
| 1765 | (int) iterations)) |
| 1766 | return FALSE; |
| 1767 | iterations--; |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | break; |
| 1772 | case ieee_load_constant_bytes_enum: |
| 1773 | case ieee_load_with_relocation_enum: |
| 1774 | if (!do_one (ieee, current_map, location_ptr, s, 1)) |
| 1775 | return FALSE; |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | static const bfd_target * |
| 1781 | ieee_object_p (bfd *abfd) |
| 1782 | { |
| 1783 | char *processor; |
| 1784 | unsigned int part; |
| 1785 | ieee_data_type *ieee; |
| 1786 | unsigned char buffer[300]; |
| 1787 | ieee_data_type *save = IEEE_DATA (abfd); |
| 1788 | bfd_size_type amt; |
| 1789 | |
| 1790 | abfd->tdata.ieee_data = 0; |
| 1791 | ieee_mkobject (abfd); |
| 1792 | |
| 1793 | ieee = IEEE_DATA (abfd); |
| 1794 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
| 1795 | goto fail; |
| 1796 | /* Read the first few bytes in to see if it makes sense. Ignore |
| 1797 | bfd_bread return value; The file might be very small. */ |
| 1798 | bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd); |
| 1799 | |
| 1800 | ieee->h.input_p = buffer; |
| 1801 | if (this_byte_and_next (&(ieee->h)) != Module_Beginning) |
| 1802 | goto got_wrong_format; |
| 1803 | |
| 1804 | ieee->read_symbols = FALSE; |
| 1805 | ieee->read_data = FALSE; |
| 1806 | ieee->section_count = 0; |
| 1807 | ieee->external_symbol_max_index = 0; |
| 1808 | ieee->external_symbol_min_index = IEEE_PUBLIC_BASE; |
| 1809 | ieee->external_reference_min_index = IEEE_REFERENCE_BASE; |
| 1810 | ieee->external_reference_max_index = 0; |
| 1811 | ieee->h.abfd = abfd; |
| 1812 | ieee->section_table = NULL; |
| 1813 | ieee->section_table_size = 0; |
| 1814 | |
| 1815 | processor = ieee->mb.processor = read_id (&(ieee->h)); |
| 1816 | if (strcmp (processor, "LIBRARY") == 0) |
| 1817 | goto got_wrong_format; |
| 1818 | ieee->mb.module_name = read_id (&(ieee->h)); |
| 1819 | if (abfd->filename == (const char *) NULL) |
| 1820 | abfd->filename = ieee->mb.module_name; |
| 1821 | |
| 1822 | /* Determine the architecture and machine type of the object file. */ |
| 1823 | { |
| 1824 | const bfd_arch_info_type *arch; |
| 1825 | char family[10]; |
| 1826 | |
| 1827 | /* IEEE does not specify the format of the processor identification |
| 1828 | string, so the compiler is free to put in it whatever it wants. |
| 1829 | We try here to recognize different processors belonging to the |
| 1830 | m68k family. Code for other processors can be added here. */ |
| 1831 | if ((processor[0] == '6') && (processor[1] == '8')) |
| 1832 | { |
| 1833 | if (processor[2] == '3') /* 683xx integrated processors. */ |
| 1834 | { |
| 1835 | switch (processor[3]) |
| 1836 | { |
| 1837 | case '0': /* 68302, 68306, 68307 */ |
| 1838 | case '2': /* 68322, 68328 */ |
| 1839 | case '5': /* 68356 */ |
| 1840 | strcpy (family, "68000"); /* MC68000-based controllers. */ |
| 1841 | break; |
| 1842 | |
| 1843 | case '3': /* 68330, 68331, 68332, 68333, |
| 1844 | 68334, 68335, 68336, 68338 */ |
| 1845 | case '6': /* 68360 */ |
| 1846 | case '7': /* 68376 */ |
| 1847 | strcpy (family, "68332"); /* CPU32 and CPU32+ */ |
| 1848 | break; |
| 1849 | |
| 1850 | case '4': |
| 1851 | if (processor[4] == '9') /* 68349 */ |
| 1852 | strcpy (family, "68030"); /* CPU030 */ |
| 1853 | else /* 68340, 68341 */ |
| 1854 | strcpy (family, "68332"); /* CPU32 and CPU32+ */ |
| 1855 | break; |
| 1856 | |
| 1857 | default: /* Does not exist yet. */ |
| 1858 | strcpy (family, "68332"); /* Guess it will be CPU32 */ |
| 1859 | } |
| 1860 | } |
| 1861 | else if (TOUPPER (processor[3]) == 'F') /* 68F333 */ |
| 1862 | strcpy (family, "68332"); /* CPU32 */ |
| 1863 | else if ((TOUPPER (processor[3]) == 'C') /* Embedded controllers. */ |
| 1864 | && ((TOUPPER (processor[2]) == 'E') |
| 1865 | || (TOUPPER (processor[2]) == 'H') |
| 1866 | || (TOUPPER (processor[2]) == 'L'))) |
| 1867 | { |
| 1868 | strcpy (family, "68"); |
| 1869 | strncat (family, processor + 4, 7); |
| 1870 | family[9] = '\0'; |
| 1871 | } |
| 1872 | else /* "Regular" processors. */ |
| 1873 | { |
| 1874 | strncpy (family, processor, 9); |
| 1875 | family[9] = '\0'; |
| 1876 | } |
| 1877 | } |
| 1878 | else if ((strncmp (processor, "cpu32", 5) == 0) /* CPU32 and CPU32+ */ |
| 1879 | || (strncmp (processor, "CPU32", 5) == 0)) |
| 1880 | strcpy (family, "68332"); |
| 1881 | else |
| 1882 | { |
| 1883 | strncpy (family, processor, 9); |
| 1884 | family[9] = '\0'; |
| 1885 | } |
| 1886 | |
| 1887 | arch = bfd_scan_arch (family); |
| 1888 | if (arch == 0) |
| 1889 | goto got_wrong_format; |
| 1890 | abfd->arch_info = arch; |
| 1891 | } |
| 1892 | |
| 1893 | if (this_byte (&(ieee->h)) != (int) ieee_address_descriptor_enum) |
| 1894 | goto fail; |
| 1895 | |
| 1896 | next_byte (&(ieee->h)); |
| 1897 | |
| 1898 | if (! parse_int (&(ieee->h), &ieee->ad.number_of_bits_mau)) |
| 1899 | goto fail; |
| 1900 | |
| 1901 | if (! parse_int (&(ieee->h), &ieee->ad.number_of_maus_in_address)) |
| 1902 | goto fail; |
| 1903 | |
| 1904 | /* If there is a byte order info, take it. */ |
| 1905 | if (this_byte (&(ieee->h)) == (int) ieee_variable_L_enum |
| 1906 | || this_byte (&(ieee->h)) == (int) ieee_variable_M_enum) |
| 1907 | next_byte (&(ieee->h)); |
| 1908 | |
| 1909 | for (part = 0; part < N_W_VARIABLES; part++) |
| 1910 | { |
| 1911 | bfd_boolean ok; |
| 1912 | |
| 1913 | if (read_2bytes (&(ieee->h)) != (int) ieee_assign_value_to_variable_enum) |
| 1914 | goto fail; |
| 1915 | |
| 1916 | if (this_byte_and_next (&(ieee->h)) != part) |
| 1917 | goto fail; |
| 1918 | |
| 1919 | ieee->w.offset[part] = parse_i (&(ieee->h), &ok); |
| 1920 | if (! ok) |
| 1921 | goto fail; |
| 1922 | } |
| 1923 | |
| 1924 | if (ieee->w.r.external_part != 0) |
| 1925 | abfd->flags = HAS_SYMS; |
| 1926 | |
| 1927 | /* By now we know that this is a real IEEE file, we're going to read |
| 1928 | the whole thing into memory so that we can run up and down it |
| 1929 | quickly. We can work out how big the file is from the trailer |
| 1930 | record. */ |
| 1931 | |
| 1932 | amt = ieee->w.r.me_record + 1; |
| 1933 | IEEE_DATA (abfd)->h.first_byte = bfd_alloc (ieee->h.abfd, amt); |
| 1934 | if (!IEEE_DATA (abfd)->h.first_byte) |
| 1935 | goto fail; |
| 1936 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
| 1937 | goto fail; |
| 1938 | /* FIXME: Check return value. I'm not sure whether it needs to read |
| 1939 | the entire buffer or not. */ |
| 1940 | bfd_bread ((void *) (IEEE_DATA (abfd)->h.first_byte), |
| 1941 | (bfd_size_type) ieee->w.r.me_record + 1, abfd); |
| 1942 | |
| 1943 | ieee_slurp_sections (abfd); |
| 1944 | |
| 1945 | if (! ieee_slurp_debug (abfd)) |
| 1946 | goto fail; |
| 1947 | |
| 1948 | /* Parse section data to activate file and section flags implied by |
| 1949 | section contents. */ |
| 1950 | if (! ieee_slurp_section_data (abfd)) |
| 1951 | goto fail; |
| 1952 | |
| 1953 | return abfd->xvec; |
| 1954 | got_wrong_format: |
| 1955 | bfd_set_error (bfd_error_wrong_format); |
| 1956 | fail: |
| 1957 | bfd_release (abfd, ieee); |
| 1958 | abfd->tdata.ieee_data = save; |
| 1959 | return (const bfd_target *) NULL; |
| 1960 | } |
| 1961 | |
| 1962 | static void |
| 1963 | ieee_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED, |
| 1964 | asymbol *symbol, |
| 1965 | symbol_info *ret) |
| 1966 | { |
| 1967 | bfd_symbol_info (symbol, ret); |
| 1968 | if (symbol->name[0] == ' ') |
| 1969 | ret->name = "* empty table entry "; |
| 1970 | if (!symbol->section) |
| 1971 | ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A'; |
| 1972 | } |
| 1973 | |
| 1974 | static void |
| 1975 | ieee_print_symbol (bfd *abfd, |
| 1976 | void * afile, |
| 1977 | asymbol *symbol, |
| 1978 | bfd_print_symbol_type how) |
| 1979 | { |
| 1980 | FILE *file = (FILE *) afile; |
| 1981 | |
| 1982 | switch (how) |
| 1983 | { |
| 1984 | case bfd_print_symbol_name: |
| 1985 | fprintf (file, "%s", symbol->name); |
| 1986 | break; |
| 1987 | case bfd_print_symbol_more: |
| 1988 | BFD_FAIL (); |
| 1989 | break; |
| 1990 | case bfd_print_symbol_all: |
| 1991 | { |
| 1992 | const char *section_name = |
| 1993 | (symbol->section == (asection *) NULL |
| 1994 | ? "*abs" |
| 1995 | : symbol->section->name); |
| 1996 | |
| 1997 | if (symbol->name[0] == ' ') |
| 1998 | fprintf (file, "* empty table entry "); |
| 1999 | else |
| 2000 | { |
| 2001 | bfd_print_symbol_vandf (abfd, (void *) file, symbol); |
| 2002 | |
| 2003 | fprintf (file, " %-5s %04x %02x %s", |
| 2004 | section_name, |
| 2005 | (unsigned) ieee_symbol (symbol)->index, |
| 2006 | (unsigned) 0, |
| 2007 | symbol->name); |
| 2008 | } |
| 2009 | } |
| 2010 | break; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | static bfd_boolean |
| 2015 | ieee_new_section_hook (bfd *abfd, asection *newsect) |
| 2016 | { |
| 2017 | newsect->used_by_bfd = bfd_alloc (abfd, (bfd_size_type) sizeof (ieee_per_section_type)); |
| 2018 | if (!newsect->used_by_bfd) |
| 2019 | return FALSE; |
| 2020 | ieee_per_section (newsect)->data = NULL; |
| 2021 | ieee_per_section (newsect)->section = newsect; |
| 2022 | return TRUE; |
| 2023 | } |
| 2024 | |
| 2025 | static long |
| 2026 | ieee_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) |
| 2027 | { |
| 2028 | if ((asect->flags & SEC_DEBUGGING) != 0) |
| 2029 | return 0; |
| 2030 | if (! ieee_slurp_section_data (abfd)) |
| 2031 | return -1; |
| 2032 | return (asect->reloc_count + 1) * sizeof (arelent *); |
| 2033 | } |
| 2034 | |
| 2035 | static bfd_boolean |
| 2036 | ieee_get_section_contents (bfd *abfd, |
| 2037 | sec_ptr section, |
| 2038 | void * location, |
| 2039 | file_ptr offset, |
| 2040 | bfd_size_type count) |
| 2041 | { |
| 2042 | ieee_per_section_type *p = ieee_per_section (section); |
| 2043 | if ((section->flags & SEC_DEBUGGING) != 0) |
| 2044 | return _bfd_generic_get_section_contents (abfd, section, location, |
| 2045 | offset, count); |
| 2046 | ieee_slurp_section_data (abfd); |
| 2047 | (void) memcpy ((void *) location, (void *) (p->data + offset), (unsigned) count); |
| 2048 | return TRUE; |
| 2049 | } |
| 2050 | |
| 2051 | static long |
| 2052 | ieee_canonicalize_reloc (bfd *abfd, |
| 2053 | sec_ptr section, |
| 2054 | arelent **relptr, |
| 2055 | asymbol **symbols) |
| 2056 | { |
| 2057 | ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation); |
| 2058 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 2059 | |
| 2060 | if ((section->flags & SEC_DEBUGGING) != 0) |
| 2061 | return 0; |
| 2062 | |
| 2063 | while (src != (ieee_reloc_type *) NULL) |
| 2064 | { |
| 2065 | /* Work out which symbol to attach it this reloc to. */ |
| 2066 | switch (src->symbol.letter) |
| 2067 | { |
| 2068 | case 'I': |
| 2069 | src->relent.sym_ptr_ptr = |
| 2070 | symbols + src->symbol.index + ieee->external_symbol_base_offset; |
| 2071 | break; |
| 2072 | case 'X': |
| 2073 | src->relent.sym_ptr_ptr = |
| 2074 | symbols + src->symbol.index + ieee->external_reference_base_offset; |
| 2075 | break; |
| 2076 | case 0: |
| 2077 | if (src->relent.sym_ptr_ptr != NULL) |
| 2078 | src->relent.sym_ptr_ptr = |
| 2079 | src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr; |
| 2080 | break; |
| 2081 | default: |
| 2082 | |
| 2083 | BFD_FAIL (); |
| 2084 | } |
| 2085 | *relptr++ = &src->relent; |
| 2086 | src = src->next; |
| 2087 | } |
| 2088 | *relptr = NULL; |
| 2089 | return section->reloc_count; |
| 2090 | } |
| 2091 | |
| 2092 | static int |
| 2093 | comp (const void * ap, const void * bp) |
| 2094 | { |
| 2095 | arelent *a = *((arelent **) ap); |
| 2096 | arelent *b = *((arelent **) bp); |
| 2097 | return a->address - b->address; |
| 2098 | } |
| 2099 | |
| 2100 | /* Write the section headers. */ |
| 2101 | |
| 2102 | static bfd_boolean |
| 2103 | ieee_write_section_part (bfd *abfd) |
| 2104 | { |
| 2105 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 2106 | asection *s; |
| 2107 | |
| 2108 | ieee->w.r.section_part = bfd_tell (abfd); |
| 2109 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
| 2110 | { |
| 2111 | if (! bfd_is_abs_section (s) |
| 2112 | && (s->flags & SEC_DEBUGGING) == 0) |
| 2113 | { |
| 2114 | if (! ieee_write_byte (abfd, ieee_section_type_enum) |
| 2115 | || ! ieee_write_byte (abfd, |
| 2116 | (bfd_byte) (s->index |
| 2117 | + IEEE_SECTION_NUMBER_BASE))) |
| 2118 | return FALSE; |
| 2119 | |
| 2120 | if (abfd->flags & EXEC_P) |
| 2121 | { |
| 2122 | /* This image is executable, so output absolute sections. */ |
| 2123 | if (! ieee_write_byte (abfd, ieee_variable_A_enum) |
| 2124 | || ! ieee_write_byte (abfd, ieee_variable_S_enum)) |
| 2125 | return FALSE; |
| 2126 | } |
| 2127 | else |
| 2128 | { |
| 2129 | if (! ieee_write_byte (abfd, ieee_variable_C_enum)) |
| 2130 | return FALSE; |
| 2131 | } |
| 2132 | |
| 2133 | switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM)) |
| 2134 | { |
| 2135 | case SEC_CODE | SEC_LOAD: |
| 2136 | case SEC_CODE: |
| 2137 | if (! ieee_write_byte (abfd, ieee_variable_P_enum)) |
| 2138 | return FALSE; |
| 2139 | break; |
| 2140 | case SEC_DATA: |
| 2141 | default: |
| 2142 | if (! ieee_write_byte (abfd, ieee_variable_D_enum)) |
| 2143 | return FALSE; |
| 2144 | break; |
| 2145 | case SEC_ROM: |
| 2146 | case SEC_ROM | SEC_DATA: |
| 2147 | case SEC_ROM | SEC_LOAD: |
| 2148 | case SEC_ROM | SEC_DATA | SEC_LOAD: |
| 2149 | if (! ieee_write_byte (abfd, ieee_variable_R_enum)) |
| 2150 | return FALSE; |
| 2151 | } |
| 2152 | |
| 2153 | |
| 2154 | if (! ieee_write_id (abfd, s->name)) |
| 2155 | return FALSE; |
| 2156 | /* Alignment. */ |
| 2157 | if (! ieee_write_byte (abfd, ieee_section_alignment_enum) |
| 2158 | || ! ieee_write_byte (abfd, |
| 2159 | (bfd_byte) (s->index |
| 2160 | + IEEE_SECTION_NUMBER_BASE)) |
| 2161 | || ! ieee_write_int (abfd, (bfd_vma) 1 << s->alignment_power)) |
| 2162 | return FALSE; |
| 2163 | |
| 2164 | /* Size. */ |
| 2165 | if (! ieee_write_2bytes (abfd, ieee_section_size_enum) |
| 2166 | || ! ieee_write_byte (abfd, |
| 2167 | (bfd_byte) (s->index |
| 2168 | + IEEE_SECTION_NUMBER_BASE)) |
| 2169 | || ! ieee_write_int (abfd, s->size)) |
| 2170 | return FALSE; |
| 2171 | if (abfd->flags & EXEC_P) |
| 2172 | { |
| 2173 | /* Relocateable sections don't have asl records. */ |
| 2174 | /* Vma. */ |
| 2175 | if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum) |
| 2176 | || ! ieee_write_byte (abfd, |
| 2177 | ((bfd_byte) |
| 2178 | (s->index |
| 2179 | + IEEE_SECTION_NUMBER_BASE))) |
| 2180 | || ! ieee_write_int (abfd, s->lma)) |
| 2181 | return FALSE; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | return TRUE; |
| 2187 | } |
| 2188 | |
| 2189 | static bfd_boolean |
| 2190 | do_with_relocs (bfd *abfd, asection *s) |
| 2191 | { |
| 2192 | unsigned int number_of_maus_in_address = |
| 2193 | bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd); |
| 2194 | unsigned int relocs_to_go = s->reloc_count; |
| 2195 | bfd_byte *stream = ieee_per_section (s)->data; |
| 2196 | arelent **p = s->orelocation; |
| 2197 | bfd_size_type current_byte_index = 0; |
| 2198 | |
| 2199 | qsort (s->orelocation, |
| 2200 | relocs_to_go, |
| 2201 | sizeof (arelent **), |
| 2202 | comp); |
| 2203 | |
| 2204 | /* Output the section preheader. */ |
| 2205 | if (! ieee_write_byte (abfd, ieee_set_current_section_enum) |
| 2206 | || ! ieee_write_byte (abfd, |
| 2207 | (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)) |
| 2208 | || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum) |
| 2209 | || ! ieee_write_byte (abfd, |
| 2210 | (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))) |
| 2211 | return FALSE; |
| 2212 | |
| 2213 | if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0) |
| 2214 | { |
| 2215 | if (! ieee_write_int (abfd, s->lma)) |
| 2216 | return FALSE; |
| 2217 | } |
| 2218 | else |
| 2219 | { |
| 2220 | if (! ieee_write_expression (abfd, (bfd_vma) 0, s->symbol, 0, 0)) |
| 2221 | return FALSE; |
| 2222 | } |
| 2223 | |
| 2224 | if (relocs_to_go == 0) |
| 2225 | { |
| 2226 | /* If there aren't any relocations then output the load constant |
| 2227 | byte opcode rather than the load with relocation opcode. */ |
| 2228 | while (current_byte_index < s->size) |
| 2229 | { |
| 2230 | bfd_size_type run; |
| 2231 | unsigned int MAXRUN = 127; |
| 2232 | |
| 2233 | run = MAXRUN; |
| 2234 | if (run > s->size - current_byte_index) |
| 2235 | run = s->size - current_byte_index; |
| 2236 | |
| 2237 | if (run != 0) |
| 2238 | { |
| 2239 | if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)) |
| 2240 | return FALSE; |
| 2241 | /* Output a stream of bytes. */ |
| 2242 | if (! ieee_write_int (abfd, run)) |
| 2243 | return FALSE; |
| 2244 | if (bfd_bwrite ((void *) (stream + current_byte_index), run, abfd) |
| 2245 | != run) |
| 2246 | return FALSE; |
| 2247 | current_byte_index += run; |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | else |
| 2252 | { |
| 2253 | if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum)) |
| 2254 | return FALSE; |
| 2255 | |
| 2256 | /* Output the data stream as the longest sequence of bytes |
| 2257 | possible, allowing for the a reasonable packet size and |
| 2258 | relocation stuffs. */ |
| 2259 | if (stream == NULL) |
| 2260 | { |
| 2261 | /* Outputting a section without data, fill it up. */ |
| 2262 | stream = bfd_zalloc (abfd, s->size); |
| 2263 | if (!stream) |
| 2264 | return FALSE; |
| 2265 | } |
| 2266 | while (current_byte_index < s->size) |
| 2267 | { |
| 2268 | bfd_size_type run; |
| 2269 | unsigned int MAXRUN = 127; |
| 2270 | |
| 2271 | if (relocs_to_go) |
| 2272 | { |
| 2273 | run = (*p)->address - current_byte_index; |
| 2274 | if (run > MAXRUN) |
| 2275 | run = MAXRUN; |
| 2276 | } |
| 2277 | else |
| 2278 | run = MAXRUN; |
| 2279 | |
| 2280 | if (run > s->size - current_byte_index) |
| 2281 | run = s->size - current_byte_index; |
| 2282 | |
| 2283 | if (run != 0) |
| 2284 | { |
| 2285 | /* Output a stream of bytes. */ |
| 2286 | if (! ieee_write_int (abfd, run)) |
| 2287 | return FALSE; |
| 2288 | if (bfd_bwrite ((void *) (stream + current_byte_index), run, abfd) |
| 2289 | != run) |
| 2290 | return FALSE; |
| 2291 | current_byte_index += run; |
| 2292 | } |
| 2293 | |
| 2294 | /* Output any relocations here. */ |
| 2295 | if (relocs_to_go && (*p) && (*p)->address == current_byte_index) |
| 2296 | { |
| 2297 | while (relocs_to_go |
| 2298 | && (*p) && (*p)->address == current_byte_index) |
| 2299 | { |
| 2300 | arelent *r = *p; |
| 2301 | bfd_signed_vma ov; |
| 2302 | switch (r->howto->size) |
| 2303 | { |
| 2304 | case 2: |
| 2305 | ov = bfd_get_signed_32 (abfd, |
| 2306 | stream + current_byte_index); |
| 2307 | current_byte_index += 4; |
| 2308 | break; |
| 2309 | case 1: |
| 2310 | ov = bfd_get_signed_16 (abfd, |
| 2311 | stream + current_byte_index); |
| 2312 | current_byte_index += 2; |
| 2313 | break; |
| 2314 | case 0: |
| 2315 | ov = bfd_get_signed_8 (abfd, |
| 2316 | stream + current_byte_index); |
| 2317 | current_byte_index++; |
| 2318 | break; |
| 2319 | default: |
| 2320 | ov = 0; |
| 2321 | BFD_FAIL (); |
| 2322 | return FALSE; |
| 2323 | } |
| 2324 | |
| 2325 | ov &= r->howto->src_mask; |
| 2326 | |
| 2327 | if (r->howto->pc_relative |
| 2328 | && ! r->howto->pcrel_offset) |
| 2329 | ov += r->address; |
| 2330 | |
| 2331 | if (! ieee_write_byte (abfd, |
| 2332 | ieee_function_either_open_b_enum)) |
| 2333 | return FALSE; |
| 2334 | |
| 2335 | if (r->sym_ptr_ptr != (asymbol **) NULL) |
| 2336 | { |
| 2337 | if (! ieee_write_expression (abfd, r->addend + ov, |
| 2338 | *(r->sym_ptr_ptr), |
| 2339 | r->howto->pc_relative, |
| 2340 | (unsigned) s->index)) |
| 2341 | return FALSE; |
| 2342 | } |
| 2343 | else |
| 2344 | { |
| 2345 | if (! ieee_write_expression (abfd, r->addend + ov, |
| 2346 | (asymbol *) NULL, |
| 2347 | r->howto->pc_relative, |
| 2348 | (unsigned) s->index)) |
| 2349 | return FALSE; |
| 2350 | } |
| 2351 | |
| 2352 | if (number_of_maus_in_address |
| 2353 | != bfd_get_reloc_size (r->howto)) |
| 2354 | { |
| 2355 | bfd_vma rsize = bfd_get_reloc_size (r->howto); |
| 2356 | if (! ieee_write_int (abfd, rsize)) |
| 2357 | return FALSE; |
| 2358 | } |
| 2359 | if (! ieee_write_byte (abfd, |
| 2360 | ieee_function_either_close_b_enum)) |
| 2361 | return FALSE; |
| 2362 | |
| 2363 | relocs_to_go--; |
| 2364 | p++; |
| 2365 | } |
| 2366 | |
| 2367 | } |
| 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | return TRUE; |
| 2372 | } |
| 2373 | |
| 2374 | /* If there are no relocations in the output section then we can be |
| 2375 | clever about how we write. We block items up into a max of 127 |
| 2376 | bytes. */ |
| 2377 | |
| 2378 | static bfd_boolean |
| 2379 | do_as_repeat (bfd *abfd, asection *s) |
| 2380 | { |
| 2381 | if (s->size) |
| 2382 | { |
| 2383 | if (! ieee_write_byte (abfd, ieee_set_current_section_enum) |
| 2384 | || ! ieee_write_byte (abfd, |
| 2385 | (bfd_byte) (s->index |
| 2386 | + IEEE_SECTION_NUMBER_BASE)) |
| 2387 | || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8) |
| 2388 | || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff) |
| 2389 | || ! ieee_write_byte (abfd, |
| 2390 | (bfd_byte) (s->index |
| 2391 | + IEEE_SECTION_NUMBER_BASE))) |
| 2392 | return FALSE; |
| 2393 | |
| 2394 | if ((abfd->flags & EXEC_P) != 0) |
| 2395 | { |
| 2396 | if (! ieee_write_int (abfd, s->lma)) |
| 2397 | return FALSE; |
| 2398 | } |
| 2399 | else |
| 2400 | { |
| 2401 | if (! ieee_write_expression (abfd, (bfd_vma) 0, s->symbol, 0, 0)) |
| 2402 | return FALSE; |
| 2403 | } |
| 2404 | |
| 2405 | if (! ieee_write_byte (abfd, ieee_repeat_data_enum) |
| 2406 | || ! ieee_write_int (abfd, s->size) |
| 2407 | || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum) |
| 2408 | || ! ieee_write_byte (abfd, 1) |
| 2409 | || ! ieee_write_byte (abfd, 0)) |
| 2410 | return FALSE; |
| 2411 | } |
| 2412 | |
| 2413 | return TRUE; |
| 2414 | } |
| 2415 | |
| 2416 | static bfd_boolean |
| 2417 | do_without_relocs (bfd *abfd, asection *s) |
| 2418 | { |
| 2419 | bfd_byte *stream = ieee_per_section (s)->data; |
| 2420 | |
| 2421 | if (stream == 0 || ((s->flags & SEC_LOAD) == 0)) |
| 2422 | { |
| 2423 | if (! do_as_repeat (abfd, s)) |
| 2424 | return FALSE; |
| 2425 | } |
| 2426 | else |
| 2427 | { |
| 2428 | unsigned int i; |
| 2429 | |
| 2430 | for (i = 0; i < s->size; i++) |
| 2431 | { |
| 2432 | if (stream[i] != 0) |
| 2433 | { |
| 2434 | if (! do_with_relocs (abfd, s)) |
| 2435 | return FALSE; |
| 2436 | return TRUE; |
| 2437 | } |
| 2438 | } |
| 2439 | if (! do_as_repeat (abfd, s)) |
| 2440 | return FALSE; |
| 2441 | } |
| 2442 | |
| 2443 | return TRUE; |
| 2444 | } |
| 2445 | |
| 2446 | static void |
| 2447 | fill (void) |
| 2448 | { |
| 2449 | bfd_size_type amt = input_ptr_end - input_ptr_start; |
| 2450 | /* FIXME: Check return value. I'm not sure whether it needs to read |
| 2451 | the entire buffer or not. */ |
| 2452 | bfd_bread ((void *) input_ptr_start, amt, input_bfd); |
| 2453 | input_ptr = input_ptr_start; |
| 2454 | } |
| 2455 | |
| 2456 | static void |
| 2457 | flush (void) |
| 2458 | { |
| 2459 | bfd_size_type amt = output_ptr - output_ptr_start; |
| 2460 | |
| 2461 | if (bfd_bwrite ((void *) (output_ptr_start), amt, output_bfd) != amt) |
| 2462 | abort (); |
| 2463 | output_ptr = output_ptr_start; |
| 2464 | output_buffer++; |
| 2465 | } |
| 2466 | |
| 2467 | #define THIS() ( *input_ptr ) |
| 2468 | #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill (); } |
| 2469 | #define OUT(x) { *output_ptr++ = (x); if (output_ptr == output_ptr_end) flush (); } |
| 2470 | |
| 2471 | static void |
| 2472 | write_int (int value) |
| 2473 | { |
| 2474 | if (value >= 0 && value <= 127) |
| 2475 | { |
| 2476 | OUT (value); |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | unsigned int length; |
| 2481 | |
| 2482 | /* How many significant bytes ? */ |
| 2483 | /* FIXME FOR LONGER INTS. */ |
| 2484 | if (value & 0xff000000) |
| 2485 | length = 4; |
| 2486 | else if (value & 0x00ff0000) |
| 2487 | length = 3; |
| 2488 | else if (value & 0x0000ff00) |
| 2489 | length = 2; |
| 2490 | else |
| 2491 | length = 1; |
| 2492 | |
| 2493 | OUT ((int) ieee_number_repeat_start_enum + length); |
| 2494 | switch (length) |
| 2495 | { |
| 2496 | case 4: |
| 2497 | OUT (value >> 24); |
| 2498 | case 3: |
| 2499 | OUT (value >> 16); |
| 2500 | case 2: |
| 2501 | OUT (value >> 8); |
| 2502 | case 1: |
| 2503 | OUT (value); |
| 2504 | } |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | static void |
| 2509 | copy_id (void) |
| 2510 | { |
| 2511 | int length = THIS (); |
| 2512 | char ch; |
| 2513 | |
| 2514 | OUT (length); |
| 2515 | NEXT (); |
| 2516 | while (length--) |
| 2517 | { |
| 2518 | ch = THIS (); |
| 2519 | OUT (ch); |
| 2520 | NEXT (); |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | #define VAR(x) ((x | 0x80)) |
| 2525 | static void |
| 2526 | copy_expression (void) |
| 2527 | { |
| 2528 | int stack[10]; |
| 2529 | int *tos = stack; |
| 2530 | int value; |
| 2531 | |
| 2532 | while (1) |
| 2533 | { |
| 2534 | switch (THIS ()) |
| 2535 | { |
| 2536 | case 0x84: |
| 2537 | NEXT (); |
| 2538 | value = THIS (); |
| 2539 | NEXT (); |
| 2540 | value = (value << 8) | THIS (); |
| 2541 | NEXT (); |
| 2542 | value = (value << 8) | THIS (); |
| 2543 | NEXT (); |
| 2544 | value = (value << 8) | THIS (); |
| 2545 | NEXT (); |
| 2546 | *tos++ = value; |
| 2547 | break; |
| 2548 | case 0x83: |
| 2549 | NEXT (); |
| 2550 | value = THIS (); |
| 2551 | NEXT (); |
| 2552 | value = (value << 8) | THIS (); |
| 2553 | NEXT (); |
| 2554 | value = (value << 8) | THIS (); |
| 2555 | NEXT (); |
| 2556 | *tos++ = value; |
| 2557 | break; |
| 2558 | case 0x82: |
| 2559 | NEXT (); |
| 2560 | value = THIS (); |
| 2561 | NEXT (); |
| 2562 | value = (value << 8) | THIS (); |
| 2563 | NEXT (); |
| 2564 | *tos++ = value; |
| 2565 | break; |
| 2566 | case 0x81: |
| 2567 | NEXT (); |
| 2568 | value = THIS (); |
| 2569 | NEXT (); |
| 2570 | *tos++ = value; |
| 2571 | break; |
| 2572 | case 0x80: |
| 2573 | NEXT (); |
| 2574 | *tos++ = 0; |
| 2575 | break; |
| 2576 | default: |
| 2577 | if (THIS () > 0x84) |
| 2578 | { |
| 2579 | /* Not a number, just bug out with the answer. */ |
| 2580 | write_int (*(--tos)); |
| 2581 | return; |
| 2582 | } |
| 2583 | *tos++ = THIS (); |
| 2584 | NEXT (); |
| 2585 | break; |
| 2586 | case 0xa5: |
| 2587 | /* PLUS anything. */ |
| 2588 | value = *(--tos); |
| 2589 | value += *(--tos); |
| 2590 | *tos++ = value; |
| 2591 | NEXT (); |
| 2592 | break; |
| 2593 | case VAR ('R'): |
| 2594 | { |
| 2595 | int section_number; |
| 2596 | ieee_data_type *ieee; |
| 2597 | asection *s; |
| 2598 | |
| 2599 | NEXT (); |
| 2600 | section_number = THIS (); |
| 2601 | |
| 2602 | NEXT (); |
| 2603 | ieee = IEEE_DATA (input_bfd); |
| 2604 | s = ieee->section_table[section_number]; |
| 2605 | value = 0; |
| 2606 | if (s->output_section) |
| 2607 | value = s->output_section->lma; |
| 2608 | value += s->output_offset; |
| 2609 | *tos++ = value; |
| 2610 | } |
| 2611 | break; |
| 2612 | case 0x90: |
| 2613 | { |
| 2614 | NEXT (); |
| 2615 | write_int (*(--tos)); |
| 2616 | OUT (0x90); |
| 2617 | return; |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | /* Drop the int in the buffer, and copy a null into the gap, which we |
| 2624 | will overwrite later. */ |
| 2625 | |
| 2626 | static void |
| 2627 | fill_int (struct output_buffer_struct *buf) |
| 2628 | { |
| 2629 | if (buf->buffer == output_buffer) |
| 2630 | { |
| 2631 | /* Still a chance to output the size. */ |
| 2632 | int value = output_ptr - buf->ptrp + 3; |
| 2633 | buf->ptrp[0] = value >> 24; |
| 2634 | buf->ptrp[1] = value >> 16; |
| 2635 | buf->ptrp[2] = value >> 8; |
| 2636 | buf->ptrp[3] = value >> 0; |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | static void |
| 2641 | drop_int (struct output_buffer_struct *buf) |
| 2642 | { |
| 2643 | int type = THIS (); |
| 2644 | int ch; |
| 2645 | |
| 2646 | if (type <= 0x84) |
| 2647 | { |
| 2648 | NEXT (); |
| 2649 | switch (type) |
| 2650 | { |
| 2651 | case 0x84: |
| 2652 | ch = THIS (); |
| 2653 | NEXT (); |
| 2654 | case 0x83: |
| 2655 | ch = THIS (); |
| 2656 | NEXT (); |
| 2657 | case 0x82: |
| 2658 | ch = THIS (); |
| 2659 | NEXT (); |
| 2660 | case 0x81: |
| 2661 | ch = THIS (); |
| 2662 | NEXT (); |
| 2663 | case 0x80: |
| 2664 | break; |
| 2665 | } |
| 2666 | } |
| 2667 | OUT (0x84); |
| 2668 | buf->ptrp = output_ptr; |
| 2669 | buf->buffer = output_buffer; |
| 2670 | OUT (0); |
| 2671 | OUT (0); |
| 2672 | OUT (0); |
| 2673 | OUT (0); |
| 2674 | } |
| 2675 | |
| 2676 | static void |
| 2677 | copy_int (void) |
| 2678 | { |
| 2679 | int type = THIS (); |
| 2680 | int ch; |
| 2681 | if (type <= 0x84) |
| 2682 | { |
| 2683 | OUT (type); |
| 2684 | NEXT (); |
| 2685 | switch (type) |
| 2686 | { |
| 2687 | case 0x84: |
| 2688 | ch = THIS (); |
| 2689 | NEXT (); |
| 2690 | OUT (ch); |
| 2691 | case 0x83: |
| 2692 | ch = THIS (); |
| 2693 | NEXT (); |
| 2694 | OUT (ch); |
| 2695 | case 0x82: |
| 2696 | ch = THIS (); |
| 2697 | NEXT (); |
| 2698 | OUT (ch); |
| 2699 | case 0x81: |
| 2700 | ch = THIS (); |
| 2701 | NEXT (); |
| 2702 | OUT (ch); |
| 2703 | case 0x80: |
| 2704 | break; |
| 2705 | } |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | #define ID copy_id () |
| 2710 | #define INT copy_int () |
| 2711 | #define EXP copy_expression () |
| 2712 | #define INTn(q) copy_int () |
| 2713 | #define EXPn(q) copy_expression () |
| 2714 | |
| 2715 | static void |
| 2716 | copy_till_end (void) |
| 2717 | { |
| 2718 | int ch = THIS (); |
| 2719 | |
| 2720 | while (1) |
| 2721 | { |
| 2722 | while (ch <= 0x80) |
| 2723 | { |
| 2724 | OUT (ch); |
| 2725 | NEXT (); |
| 2726 | ch = THIS (); |
| 2727 | } |
| 2728 | switch (ch) |
| 2729 | { |
| 2730 | case 0x84: |
| 2731 | OUT (THIS ()); |
| 2732 | NEXT (); |
| 2733 | case 0x83: |
| 2734 | OUT (THIS ()); |
| 2735 | NEXT (); |
| 2736 | case 0x82: |
| 2737 | OUT (THIS ()); |
| 2738 | NEXT (); |
| 2739 | case 0x81: |
| 2740 | OUT (THIS ()); |
| 2741 | NEXT (); |
| 2742 | OUT (THIS ()); |
| 2743 | NEXT (); |
| 2744 | |
| 2745 | ch = THIS (); |
| 2746 | break; |
| 2747 | default: |
| 2748 | return; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | } |
| 2753 | |
| 2754 | static void |
| 2755 | f1_record (void) |
| 2756 | { |
| 2757 | int ch; |
| 2758 | |
| 2759 | /* ATN record. */ |
| 2760 | NEXT (); |
| 2761 | ch = THIS (); |
| 2762 | switch (ch) |
| 2763 | { |
| 2764 | default: |
| 2765 | OUT (0xf1); |
| 2766 | OUT (ch); |
| 2767 | break; |
| 2768 | case 0xc9: |
| 2769 | NEXT (); |
| 2770 | OUT (0xf1); |
| 2771 | OUT (0xc9); |
| 2772 | INT; |
| 2773 | INT; |
| 2774 | ch = THIS (); |
| 2775 | switch (ch) |
| 2776 | { |
| 2777 | case 0x16: |
| 2778 | NEXT (); |
| 2779 | break; |
| 2780 | case 0x01: |
| 2781 | NEXT (); |
| 2782 | break; |
| 2783 | case 0x00: |
| 2784 | NEXT (); |
| 2785 | INT; |
| 2786 | break; |
| 2787 | case 0x03: |
| 2788 | NEXT (); |
| 2789 | INT; |
| 2790 | break; |
| 2791 | case 0x13: |
| 2792 | EXPn (instruction address); |
| 2793 | break; |
| 2794 | default: |
| 2795 | break; |
| 2796 | } |
| 2797 | break; |
| 2798 | case 0xd8: |
| 2799 | /* EXternal ref. */ |
| 2800 | NEXT (); |
| 2801 | OUT (0xf1); |
| 2802 | OUT (0xd8); |
| 2803 | EXP; |
| 2804 | EXP; |
| 2805 | EXP; |
| 2806 | EXP; |
| 2807 | break; |
| 2808 | case 0xce: |
| 2809 | NEXT (); |
| 2810 | OUT (0xf1); |
| 2811 | OUT (0xce); |
| 2812 | INT; |
| 2813 | INT; |
| 2814 | ch = THIS (); |
| 2815 | INT; |
| 2816 | switch (ch) |
| 2817 | { |
| 2818 | case 0x01: |
| 2819 | INT; |
| 2820 | INT; |
| 2821 | break; |
| 2822 | case 0x02: |
| 2823 | INT; |
| 2824 | break; |
| 2825 | case 0x04: |
| 2826 | EXPn (external function); |
| 2827 | break; |
| 2828 | case 0x05: |
| 2829 | break; |
| 2830 | case 0x07: |
| 2831 | INTn (line number); |
| 2832 | INT; |
| 2833 | case 0x08: |
| 2834 | break; |
| 2835 | case 0x0a: |
| 2836 | INTn (locked register); |
| 2837 | INT; |
| 2838 | break; |
| 2839 | case 0x3f: |
| 2840 | copy_till_end (); |
| 2841 | break; |
| 2842 | case 0x3e: |
| 2843 | copy_till_end (); |
| 2844 | break; |
| 2845 | case 0x40: |
| 2846 | copy_till_end (); |
| 2847 | break; |
| 2848 | case 0x41: |
| 2849 | ID; |
| 2850 | break; |
| 2851 | } |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | static void |
| 2856 | f0_record (void) |
| 2857 | { |
| 2858 | /* Attribute record. */ |
| 2859 | NEXT (); |
| 2860 | OUT (0xf0); |
| 2861 | INTn (Symbol name); |
| 2862 | ID; |
| 2863 | } |
| 2864 | |
| 2865 | static void |
| 2866 | f2_record (void) |
| 2867 | { |
| 2868 | NEXT (); |
| 2869 | OUT (0xf2); |
| 2870 | INT; |
| 2871 | NEXT (); |
| 2872 | OUT (0xce); |
| 2873 | INT; |
| 2874 | copy_till_end (); |
| 2875 | } |
| 2876 | |
| 2877 | static void |
| 2878 | f8_record (void) |
| 2879 | { |
| 2880 | int ch; |
| 2881 | NEXT (); |
| 2882 | ch = THIS (); |
| 2883 | switch (ch) |
| 2884 | { |
| 2885 | case 0x01: |
| 2886 | case 0x02: |
| 2887 | case 0x03: |
| 2888 | /* Unique typedefs for module. */ |
| 2889 | /* GLobal typedefs. */ |
| 2890 | /* High level module scope beginning. */ |
| 2891 | { |
| 2892 | struct output_buffer_struct ob; |
| 2893 | |
| 2894 | NEXT (); |
| 2895 | OUT (0xf8); |
| 2896 | OUT (ch); |
| 2897 | drop_int (&ob); |
| 2898 | ID; |
| 2899 | |
| 2900 | block (); |
| 2901 | |
| 2902 | NEXT (); |
| 2903 | fill_int (&ob); |
| 2904 | OUT (0xf9); |
| 2905 | } |
| 2906 | break; |
| 2907 | case 0x04: |
| 2908 | /* Global function. */ |
| 2909 | { |
| 2910 | struct output_buffer_struct ob; |
| 2911 | |
| 2912 | NEXT (); |
| 2913 | OUT (0xf8); |
| 2914 | OUT (0x04); |
| 2915 | drop_int (&ob); |
| 2916 | ID; |
| 2917 | INTn (stack size); |
| 2918 | INTn (ret val); |
| 2919 | EXPn (offset); |
| 2920 | |
| 2921 | block (); |
| 2922 | |
| 2923 | NEXT (); |
| 2924 | OUT (0xf9); |
| 2925 | EXPn (size of block); |
| 2926 | fill_int (&ob); |
| 2927 | } |
| 2928 | break; |
| 2929 | |
| 2930 | case 0x05: |
| 2931 | /* File name for source line numbers. */ |
| 2932 | { |
| 2933 | struct output_buffer_struct ob; |
| 2934 | |
| 2935 | NEXT (); |
| 2936 | OUT (0xf8); |
| 2937 | OUT (0x05); |
| 2938 | drop_int (&ob); |
| 2939 | ID; |
| 2940 | INTn (year); |
| 2941 | INTn (month); |
| 2942 | INTn (day); |
| 2943 | INTn (hour); |
| 2944 | INTn (monute); |
| 2945 | INTn (second); |
| 2946 | block (); |
| 2947 | NEXT (); |
| 2948 | OUT (0xf9); |
| 2949 | fill_int (&ob); |
| 2950 | } |
| 2951 | break; |
| 2952 | |
| 2953 | case 0x06: |
| 2954 | /* Local function. */ |
| 2955 | { |
| 2956 | struct output_buffer_struct ob; |
| 2957 | |
| 2958 | NEXT (); |
| 2959 | OUT (0xf8); |
| 2960 | OUT (0x06); |
| 2961 | drop_int (&ob); |
| 2962 | ID; |
| 2963 | INTn (stack size); |
| 2964 | INTn (type return); |
| 2965 | EXPn (offset); |
| 2966 | block (); |
| 2967 | NEXT (); |
| 2968 | OUT (0xf9); |
| 2969 | EXPn (size); |
| 2970 | fill_int (&ob); |
| 2971 | } |
| 2972 | break; |
| 2973 | |
| 2974 | case 0x0a: |
| 2975 | /* Assembler module scope beginning - */ |
| 2976 | { |
| 2977 | struct output_buffer_struct ob; |
| 2978 | |
| 2979 | NEXT (); |
| 2980 | OUT (0xf8); |
| 2981 | OUT (0x0a); |
| 2982 | drop_int (&ob); |
| 2983 | ID; |
| 2984 | ID; |
| 2985 | INT; |
| 2986 | ID; |
| 2987 | INT; |
| 2988 | INT; |
| 2989 | INT; |
| 2990 | INT; |
| 2991 | INT; |
| 2992 | INT; |
| 2993 | |
| 2994 | block (); |
| 2995 | |
| 2996 | NEXT (); |
| 2997 | OUT (0xf9); |
| 2998 | fill_int (&ob); |
| 2999 | } |
| 3000 | break; |
| 3001 | case 0x0b: |
| 3002 | { |
| 3003 | struct output_buffer_struct ob; |
| 3004 | |
| 3005 | NEXT (); |
| 3006 | OUT (0xf8); |
| 3007 | OUT (0x0b); |
| 3008 | drop_int (&ob); |
| 3009 | ID; |
| 3010 | INT; |
| 3011 | INTn (section index); |
| 3012 | EXPn (offset); |
| 3013 | INTn (stuff); |
| 3014 | |
| 3015 | block (); |
| 3016 | |
| 3017 | OUT (0xf9); |
| 3018 | NEXT (); |
| 3019 | EXPn (Size in Maus); |
| 3020 | fill_int (&ob); |
| 3021 | } |
| 3022 | break; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | static void |
| 3027 | e2_record (void) |
| 3028 | { |
| 3029 | OUT (0xe2); |
| 3030 | NEXT (); |
| 3031 | OUT (0xce); |
| 3032 | NEXT (); |
| 3033 | INT; |
| 3034 | EXP; |
| 3035 | } |
| 3036 | |
| 3037 | static void |
| 3038 | block (void) |
| 3039 | { |
| 3040 | int ch; |
| 3041 | |
| 3042 | while (1) |
| 3043 | { |
| 3044 | ch = THIS (); |
| 3045 | switch (ch) |
| 3046 | { |
| 3047 | case 0xe1: |
| 3048 | case 0xe5: |
| 3049 | return; |
| 3050 | case 0xf9: |
| 3051 | return; |
| 3052 | case 0xf0: |
| 3053 | f0_record (); |
| 3054 | break; |
| 3055 | case 0xf1: |
| 3056 | f1_record (); |
| 3057 | break; |
| 3058 | case 0xf2: |
| 3059 | f2_record (); |
| 3060 | break; |
| 3061 | case 0xf8: |
| 3062 | f8_record (); |
| 3063 | break; |
| 3064 | case 0xe2: |
| 3065 | e2_record (); |
| 3066 | break; |
| 3067 | |
| 3068 | } |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /* Moves all the debug information from the source bfd to the output |
| 3073 | bfd, and relocates any expressions it finds. */ |
| 3074 | |
| 3075 | static void |
| 3076 | relocate_debug (bfd *output ATTRIBUTE_UNUSED, |
| 3077 | bfd *input) |
| 3078 | { |
| 3079 | #define IBS 400 |
| 3080 | #define OBS 400 |
| 3081 | unsigned char input_buffer[IBS]; |
| 3082 | |
| 3083 | input_ptr_start = input_ptr = input_buffer; |
| 3084 | input_ptr_end = input_buffer + IBS; |
| 3085 | input_bfd = input; |
| 3086 | /* FIXME: Check return value. I'm not sure whether it needs to read |
| 3087 | the entire buffer or not. */ |
| 3088 | bfd_bread ((void *) input_ptr_start, (bfd_size_type) IBS, input); |
| 3089 | block (); |
| 3090 | } |
| 3091 | |
| 3092 | /* Gather together all the debug information from each input BFD into |
| 3093 | one place, relocating it and emitting it as we go. */ |
| 3094 | |
| 3095 | static bfd_boolean |
| 3096 | ieee_write_debug_part (bfd *abfd) |
| 3097 | { |
| 3098 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 3099 | bfd_chain_type *chain = ieee->chain_root; |
| 3100 | unsigned char obuff[OBS]; |
| 3101 | bfd_boolean some_debug = FALSE; |
| 3102 | file_ptr here = bfd_tell (abfd); |
| 3103 | |
| 3104 | output_ptr_start = output_ptr = obuff; |
| 3105 | output_ptr_end = obuff + OBS; |
| 3106 | output_ptr = obuff; |
| 3107 | output_bfd = abfd; |
| 3108 | |
| 3109 | if (chain == (bfd_chain_type *) NULL) |
| 3110 | { |
| 3111 | asection *s; |
| 3112 | |
| 3113 | for (s = abfd->sections; s != NULL; s = s->next) |
| 3114 | if ((s->flags & SEC_DEBUGGING) != 0) |
| 3115 | break; |
| 3116 | if (s == NULL) |
| 3117 | { |
| 3118 | ieee->w.r.debug_information_part = 0; |
| 3119 | return TRUE; |
| 3120 | } |
| 3121 | |
| 3122 | ieee->w.r.debug_information_part = here; |
| 3123 | if (bfd_bwrite (s->contents, s->size, abfd) != s->size) |
| 3124 | return FALSE; |
| 3125 | } |
| 3126 | else |
| 3127 | { |
| 3128 | while (chain != (bfd_chain_type *) NULL) |
| 3129 | { |
| 3130 | bfd *entry = chain->this; |
| 3131 | ieee_data_type *entry_ieee = IEEE_DATA (entry); |
| 3132 | |
| 3133 | if (entry_ieee->w.r.debug_information_part) |
| 3134 | { |
| 3135 | if (bfd_seek (entry, entry_ieee->w.r.debug_information_part, |
| 3136 | SEEK_SET) != 0) |
| 3137 | return FALSE; |
| 3138 | relocate_debug (abfd, entry); |
| 3139 | } |
| 3140 | |
| 3141 | chain = chain->next; |
| 3142 | } |
| 3143 | |
| 3144 | if (some_debug) |
| 3145 | ieee->w.r.debug_information_part = here; |
| 3146 | else |
| 3147 | ieee->w.r.debug_information_part = 0; |
| 3148 | |
| 3149 | flush (); |
| 3150 | } |
| 3151 | |
| 3152 | return TRUE; |
| 3153 | } |
| 3154 | |
| 3155 | /* Write the data in an ieee way. */ |
| 3156 | |
| 3157 | static bfd_boolean |
| 3158 | ieee_write_data_part (bfd *abfd) |
| 3159 | { |
| 3160 | asection *s; |
| 3161 | |
| 3162 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 3163 | ieee->w.r.data_part = bfd_tell (abfd); |
| 3164 | |
| 3165 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
| 3166 | { |
| 3167 | /* Skip sections that have no loadable contents (.bss, |
| 3168 | debugging, etc.) */ |
| 3169 | if ((s->flags & SEC_LOAD) == 0) |
| 3170 | continue; |
| 3171 | |
| 3172 | /* Sort the reloc records so we can insert them in the correct |
| 3173 | places. */ |
| 3174 | if (s->reloc_count != 0) |
| 3175 | { |
| 3176 | if (! do_with_relocs (abfd, s)) |
| 3177 | return FALSE; |
| 3178 | } |
| 3179 | else |
| 3180 | { |
| 3181 | if (! do_without_relocs (abfd, s)) |
| 3182 | return FALSE; |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | return TRUE; |
| 3187 | } |
| 3188 | |
| 3189 | static bfd_boolean |
| 3190 | init_for_output (bfd *abfd) |
| 3191 | { |
| 3192 | asection *s; |
| 3193 | |
| 3194 | for (s = abfd->sections; s != (asection *) NULL; s = s->next) |
| 3195 | { |
| 3196 | if ((s->flags & SEC_DEBUGGING) != 0) |
| 3197 | continue; |
| 3198 | if (s->size != 0) |
| 3199 | { |
| 3200 | bfd_size_type size = s->size; |
| 3201 | ieee_per_section (s)->data = bfd_alloc (abfd, size); |
| 3202 | if (!ieee_per_section (s)->data) |
| 3203 | return FALSE; |
| 3204 | } |
| 3205 | } |
| 3206 | return TRUE; |
| 3207 | } |
| 3208 | \f |
| 3209 | /* Exec and core file sections. */ |
| 3210 | |
| 3211 | /* Set section contents is complicated with IEEE since the format is |
| 3212 | not a byte image, but a record stream. */ |
| 3213 | |
| 3214 | static bfd_boolean |
| 3215 | ieee_set_section_contents (bfd *abfd, |
| 3216 | sec_ptr section, |
| 3217 | const void * location, |
| 3218 | file_ptr offset, |
| 3219 | bfd_size_type count) |
| 3220 | { |
| 3221 | if ((section->flags & SEC_DEBUGGING) != 0) |
| 3222 | { |
| 3223 | if (section->contents == NULL) |
| 3224 | { |
| 3225 | bfd_size_type size = section->size; |
| 3226 | section->contents = bfd_alloc (abfd, size); |
| 3227 | if (section->contents == NULL) |
| 3228 | return FALSE; |
| 3229 | } |
| 3230 | /* bfd_set_section_contents has already checked that everything |
| 3231 | is within range. */ |
| 3232 | memcpy (section->contents + offset, location, (size_t) count); |
| 3233 | return TRUE; |
| 3234 | } |
| 3235 | |
| 3236 | if (ieee_per_section (section)->data == (bfd_byte *) NULL) |
| 3237 | { |
| 3238 | if (!init_for_output (abfd)) |
| 3239 | return FALSE; |
| 3240 | } |
| 3241 | memcpy ((void *) (ieee_per_section (section)->data + offset), |
| 3242 | (void *) location, |
| 3243 | (unsigned int) count); |
| 3244 | return TRUE; |
| 3245 | } |
| 3246 | |
| 3247 | /* Write the external symbols of a file. IEEE considers two sorts of |
| 3248 | external symbols, public, and referenced. It uses to internal |
| 3249 | forms to index them as well. When we write them out we turn their |
| 3250 | symbol values into indexes from the right base. */ |
| 3251 | |
| 3252 | static bfd_boolean |
| 3253 | ieee_write_external_part (bfd *abfd) |
| 3254 | { |
| 3255 | asymbol **q; |
| 3256 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 3257 | unsigned int reference_index = IEEE_REFERENCE_BASE; |
| 3258 | unsigned int public_index = IEEE_PUBLIC_BASE + 2; |
| 3259 | file_ptr here = bfd_tell (abfd); |
| 3260 | bfd_boolean hadone = FALSE; |
| 3261 | |
| 3262 | if (abfd->outsymbols != (asymbol **) NULL) |
| 3263 | { |
| 3264 | |
| 3265 | for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++) |
| 3266 | { |
| 3267 | asymbol *p = *q; |
| 3268 | |
| 3269 | if (bfd_is_und_section (p->section)) |
| 3270 | { |
| 3271 | /* This must be a symbol reference. */ |
| 3272 | if (! ieee_write_byte (abfd, ieee_external_reference_enum) |
| 3273 | || ! ieee_write_int (abfd, (bfd_vma) reference_index) |
| 3274 | || ! ieee_write_id (abfd, p->name)) |
| 3275 | return FALSE; |
| 3276 | p->value = reference_index; |
| 3277 | reference_index++; |
| 3278 | hadone = TRUE; |
| 3279 | } |
| 3280 | else if (bfd_is_com_section (p->section)) |
| 3281 | { |
| 3282 | /* This is a weak reference. */ |
| 3283 | if (! ieee_write_byte (abfd, ieee_external_reference_enum) |
| 3284 | || ! ieee_write_int (abfd, (bfd_vma) reference_index) |
| 3285 | || ! ieee_write_id (abfd, p->name) |
| 3286 | || ! ieee_write_byte (abfd, |
| 3287 | ieee_weak_external_reference_enum) |
| 3288 | || ! ieee_write_int (abfd, (bfd_vma) reference_index) |
| 3289 | || ! ieee_write_int (abfd, p->value)) |
| 3290 | return FALSE; |
| 3291 | p->value = reference_index; |
| 3292 | reference_index++; |
| 3293 | hadone = TRUE; |
| 3294 | } |
| 3295 | else if (p->flags & BSF_GLOBAL) |
| 3296 | { |
| 3297 | /* This must be a symbol definition. */ |
| 3298 | if (! ieee_write_byte (abfd, ieee_external_symbol_enum) |
| 3299 | || ! ieee_write_int (abfd, (bfd_vma) public_index) |
| 3300 | || ! ieee_write_id (abfd, p->name) |
| 3301 | || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum) |
| 3302 | || ! ieee_write_int (abfd, (bfd_vma) public_index) |
| 3303 | || ! ieee_write_byte (abfd, 15) /* Instruction address. */ |
| 3304 | || ! ieee_write_byte (abfd, 19) /* Static symbol. */ |
| 3305 | || ! ieee_write_byte (abfd, 1)) /* One of them. */ |
| 3306 | return FALSE; |
| 3307 | |
| 3308 | /* Write out the value. */ |
| 3309 | if (! ieee_write_2bytes (abfd, ieee_value_record_enum) |
| 3310 | || ! ieee_write_int (abfd, (bfd_vma) public_index)) |
| 3311 | return FALSE; |
| 3312 | if (! bfd_is_abs_section (p->section)) |
| 3313 | { |
| 3314 | if (abfd->flags & EXEC_P) |
| 3315 | { |
| 3316 | /* If fully linked, then output all symbols |
| 3317 | relocated. */ |
| 3318 | if (! (ieee_write_int |
| 3319 | (abfd, |
| 3320 | (p->value |
| 3321 | + p->section->output_offset |
| 3322 | + p->section->output_section->vma)))) |
| 3323 | return FALSE; |
| 3324 | } |
| 3325 | else |
| 3326 | { |
| 3327 | if (! (ieee_write_expression |
| 3328 | (abfd, |
| 3329 | p->value + p->section->output_offset, |
| 3330 | p->section->output_section->symbol, |
| 3331 | FALSE, 0))) |
| 3332 | return FALSE; |
| 3333 | } |
| 3334 | } |
| 3335 | else |
| 3336 | { |
| 3337 | if (! ieee_write_expression (abfd, |
| 3338 | p->value, |
| 3339 | bfd_abs_section_ptr->symbol, |
| 3340 | FALSE, 0)) |
| 3341 | return FALSE; |
| 3342 | } |
| 3343 | p->value = public_index; |
| 3344 | public_index++; |
| 3345 | hadone = TRUE; |
| 3346 | } |
| 3347 | else |
| 3348 | { |
| 3349 | /* This can happen - when there are gaps in the symbols read |
| 3350 | from an input ieee file. */ |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | if (hadone) |
| 3355 | ieee->w.r.external_part = here; |
| 3356 | |
| 3357 | return TRUE; |
| 3358 | } |
| 3359 | |
| 3360 | |
| 3361 | static const unsigned char exten[] = |
| 3362 | { |
| 3363 | 0xf0, 0x20, 0x00, |
| 3364 | 0xf1, 0xce, 0x20, 0x00, 37, 3, 3, /* Set version 3 rev 3. */ |
| 3365 | 0xf1, 0xce, 0x20, 0x00, 39, 2, /* Keep symbol in original case. */ |
| 3366 | 0xf1, 0xce, 0x20, 0x00, 38 /* Set object type relocatable to x. */ |
| 3367 | }; |
| 3368 | |
| 3369 | static const unsigned char envi[] = |
| 3370 | { |
| 3371 | 0xf0, 0x21, 0x00, |
| 3372 | |
| 3373 | /* 0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11, |
| 3374 | 0x19, 0x2c, |
| 3375 | */ |
| 3376 | 0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok. */ |
| 3377 | |
| 3378 | 0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix. */ |
| 3379 | /* 0xf1, 0xce, 0x21, 0, 54, 2,1,1 tool & version # */ |
| 3380 | }; |
| 3381 | |
| 3382 | static bfd_boolean |
| 3383 | ieee_write_me_part (bfd *abfd) |
| 3384 | { |
| 3385 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 3386 | ieee->w.r.trailer_part = bfd_tell (abfd); |
| 3387 | if (abfd->start_address) |
| 3388 | { |
| 3389 | if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum) |
| 3390 | || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum) |
| 3391 | || ! ieee_write_int (abfd, abfd->start_address) |
| 3392 | || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum)) |
| 3393 | return FALSE; |
| 3394 | } |
| 3395 | ieee->w.r.me_record = bfd_tell (abfd); |
| 3396 | if (! ieee_write_byte (abfd, ieee_module_end_enum)) |
| 3397 | return FALSE; |
| 3398 | return TRUE; |
| 3399 | } |
| 3400 | |
| 3401 | /* Write out the IEEE processor ID. */ |
| 3402 | |
| 3403 | static bfd_boolean |
| 3404 | ieee_write_processor (bfd *abfd) |
| 3405 | { |
| 3406 | const bfd_arch_info_type *arch; |
| 3407 | |
| 3408 | arch = bfd_get_arch_info (abfd); |
| 3409 | switch (arch->arch) |
| 3410 | { |
| 3411 | default: |
| 3412 | if (! ieee_write_id (abfd, bfd_printable_name (abfd))) |
| 3413 | return FALSE; |
| 3414 | break; |
| 3415 | |
| 3416 | case bfd_arch_a29k: |
| 3417 | if (! ieee_write_id (abfd, "29000")) |
| 3418 | return FALSE; |
| 3419 | break; |
| 3420 | |
| 3421 | case bfd_arch_h8300: |
| 3422 | if (! ieee_write_id (abfd, "H8/300")) |
| 3423 | return FALSE; |
| 3424 | break; |
| 3425 | |
| 3426 | case bfd_arch_h8500: |
| 3427 | if (! ieee_write_id (abfd, "H8/500")) |
| 3428 | return FALSE; |
| 3429 | break; |
| 3430 | |
| 3431 | case bfd_arch_i960: |
| 3432 | switch (arch->mach) |
| 3433 | { |
| 3434 | default: |
| 3435 | case bfd_mach_i960_core: |
| 3436 | case bfd_mach_i960_ka_sa: |
| 3437 | if (! ieee_write_id (abfd, "80960KA")) |
| 3438 | return FALSE; |
| 3439 | break; |
| 3440 | |
| 3441 | case bfd_mach_i960_kb_sb: |
| 3442 | if (! ieee_write_id (abfd, "80960KB")) |
| 3443 | return FALSE; |
| 3444 | break; |
| 3445 | |
| 3446 | case bfd_mach_i960_ca: |
| 3447 | if (! ieee_write_id (abfd, "80960CA")) |
| 3448 | return FALSE; |
| 3449 | break; |
| 3450 | |
| 3451 | case bfd_mach_i960_mc: |
| 3452 | case bfd_mach_i960_xa: |
| 3453 | if (! ieee_write_id (abfd, "80960MC")) |
| 3454 | return FALSE; |
| 3455 | break; |
| 3456 | } |
| 3457 | break; |
| 3458 | |
| 3459 | case bfd_arch_m68k: |
| 3460 | { |
| 3461 | const char *id; |
| 3462 | |
| 3463 | switch (arch->mach) |
| 3464 | { |
| 3465 | default: id = "68020"; break; |
| 3466 | case bfd_mach_m68000: id = "68000"; break; |
| 3467 | case bfd_mach_m68008: id = "68008"; break; |
| 3468 | case bfd_mach_m68010: id = "68010"; break; |
| 3469 | case bfd_mach_m68020: id = "68020"; break; |
| 3470 | case bfd_mach_m68030: id = "68030"; break; |
| 3471 | case bfd_mach_m68040: id = "68040"; break; |
| 3472 | case bfd_mach_m68060: id = "68060"; break; |
| 3473 | case bfd_mach_cpu32: id = "cpu32"; break; |
| 3474 | case bfd_mach_mcf5200:id = "5200"; break; |
| 3475 | case bfd_mach_mcf5206e:id = "5206e"; break; |
| 3476 | case bfd_mach_mcf5307:id = "5307"; break; |
| 3477 | case bfd_mach_mcf5407:id = "5407"; break; |
| 3478 | case bfd_mach_mcf528x:id = "5282"; break; |
| 3479 | } |
| 3480 | |
| 3481 | if (! ieee_write_id (abfd, id)) |
| 3482 | return FALSE; |
| 3483 | } |
| 3484 | break; |
| 3485 | } |
| 3486 | |
| 3487 | return TRUE; |
| 3488 | } |
| 3489 | |
| 3490 | static bfd_boolean |
| 3491 | ieee_write_object_contents (bfd *abfd) |
| 3492 | { |
| 3493 | ieee_data_type *ieee = IEEE_DATA (abfd); |
| 3494 | unsigned int i; |
| 3495 | file_ptr old; |
| 3496 | |
| 3497 | /* Fast forward over the header area. */ |
| 3498 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) |
| 3499 | return FALSE; |
| 3500 | |
| 3501 | if (! ieee_write_byte (abfd, ieee_module_beginning_enum) |
| 3502 | || ! ieee_write_processor (abfd) |
| 3503 | || ! ieee_write_id (abfd, abfd->filename)) |
| 3504 | return FALSE; |
| 3505 | |
| 3506 | /* Fast forward over the variable bits. */ |
| 3507 | if (! ieee_write_byte (abfd, ieee_address_descriptor_enum)) |
| 3508 | return FALSE; |
| 3509 | |
| 3510 | /* Bits per MAU. */ |
| 3511 | if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd)))) |
| 3512 | return FALSE; |
| 3513 | /* MAU's per address. */ |
| 3514 | if (! ieee_write_byte (abfd, |
| 3515 | (bfd_byte) (bfd_arch_bits_per_address (abfd) |
| 3516 | / bfd_arch_bits_per_byte (abfd)))) |
| 3517 | return FALSE; |
| 3518 | |
| 3519 | old = bfd_tell (abfd); |
| 3520 | if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0) |
| 3521 | return FALSE; |
| 3522 | |
| 3523 | ieee->w.r.extension_record = bfd_tell (abfd); |
| 3524 | if (bfd_bwrite ((char *) exten, (bfd_size_type) sizeof (exten), abfd) |
| 3525 | != sizeof (exten)) |
| 3526 | return FALSE; |
| 3527 | if (abfd->flags & EXEC_P) |
| 3528 | { |
| 3529 | if (! ieee_write_byte (abfd, 0x1)) /* Absolute. */ |
| 3530 | return FALSE; |
| 3531 | } |
| 3532 | else |
| 3533 | { |
| 3534 | if (! ieee_write_byte (abfd, 0x2)) /* Relocateable. */ |
| 3535 | return FALSE; |
| 3536 | } |
| 3537 | |
| 3538 | ieee->w.r.environmental_record = bfd_tell (abfd); |
| 3539 | if (bfd_bwrite ((char *) envi, (bfd_size_type) sizeof (envi), abfd) |
| 3540 | != sizeof (envi)) |
| 3541 | return FALSE; |
| 3542 | |
| 3543 | /* The HP emulator database requires a timestamp in the file. */ |
| 3544 | { |
| 3545 | time_t now; |
| 3546 | const struct tm *t; |
| 3547 | |
| 3548 | time (&now); |
| 3549 | t = (struct tm *) localtime (&now); |
| 3550 | if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum) |
| 3551 | || ! ieee_write_byte (abfd, 0x21) |
| 3552 | || ! ieee_write_byte (abfd, 0) |
| 3553 | || ! ieee_write_byte (abfd, 50) |
| 3554 | || ! ieee_write_int (abfd, (bfd_vma) (t->tm_year + 1900)) |
| 3555 | || ! ieee_write_int (abfd, (bfd_vma) (t->tm_mon + 1)) |
| 3556 | || ! ieee_write_int (abfd, (bfd_vma) t->tm_mday) |
| 3557 | || ! ieee_write_int (abfd, (bfd_vma) t->tm_hour) |
| 3558 | || ! ieee_write_int (abfd, (bfd_vma) t->tm_min) |
| 3559 | || ! ieee_write_int (abfd, (bfd_vma) t->tm_sec)) |
| 3560 | return FALSE; |
| 3561 | } |
| 3562 | |
| 3563 | output_bfd = abfd; |
| 3564 | |
| 3565 | flush (); |
| 3566 | |
| 3567 | if (! ieee_write_section_part (abfd)) |
| 3568 | return FALSE; |
| 3569 | /* First write the symbols. This changes their values into table |
| 3570 | indeces so we cant use it after this point. */ |
| 3571 | if (! ieee_write_external_part (abfd)) |
| 3572 | return FALSE; |
| 3573 | |
| 3574 | /* Write any debugs we have been told about. */ |
| 3575 | if (! ieee_write_debug_part (abfd)) |
| 3576 | return FALSE; |
| 3577 | |
| 3578 | /* Can only write the data once the symbols have been written, since |
| 3579 | the data contains relocation information which points to the |
| 3580 | symbols. */ |
| 3581 | if (! ieee_write_data_part (abfd)) |
| 3582 | return FALSE; |
| 3583 | |
| 3584 | /* At the end we put the end! */ |
| 3585 | if (! ieee_write_me_part (abfd)) |
| 3586 | return FALSE; |
| 3587 | |
| 3588 | /* Generate the header. */ |
| 3589 | if (bfd_seek (abfd, old, SEEK_SET) != 0) |
| 3590 | return FALSE; |
| 3591 | |
| 3592 | for (i = 0; i < N_W_VARIABLES; i++) |
| 3593 | { |
| 3594 | if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum) |
| 3595 | || ! ieee_write_byte (abfd, (bfd_byte) i) |
| 3596 | || ! ieee_write_int5_out (abfd, (bfd_vma) ieee->w.offset[i])) |
| 3597 | return FALSE; |
| 3598 | } |
| 3599 | |
| 3600 | return TRUE; |
| 3601 | } |
| 3602 | \f |
| 3603 | /* Native-level interface to symbols. */ |
| 3604 | |
| 3605 | /* We read the symbols into a buffer, which is discarded when this |
| 3606 | function exits. We read the strings into a buffer large enough to |
| 3607 | hold them all plus all the cached symbol entries. */ |
| 3608 | |
| 3609 | static asymbol * |
| 3610 | ieee_make_empty_symbol (bfd *abfd) |
| 3611 | { |
| 3612 | bfd_size_type amt = sizeof (ieee_symbol_type); |
| 3613 | ieee_symbol_type *new = bfd_zalloc (abfd, amt); |
| 3614 | |
| 3615 | if (!new) |
| 3616 | return NULL; |
| 3617 | new->symbol.the_bfd = abfd; |
| 3618 | return &new->symbol; |
| 3619 | } |
| 3620 | |
| 3621 | static bfd * |
| 3622 | ieee_openr_next_archived_file (bfd *arch, bfd *prev) |
| 3623 | { |
| 3624 | ieee_ar_data_type *ar = IEEE_AR_DATA (arch); |
| 3625 | |
| 3626 | /* Take the next one from the arch state, or reset. */ |
| 3627 | if (prev == (bfd *) NULL) |
| 3628 | /* Reset the index - the first two entries are bogus. */ |
| 3629 | ar->element_index = 2; |
| 3630 | |
| 3631 | while (TRUE) |
| 3632 | { |
| 3633 | ieee_ar_obstack_type *p = ar->elements + ar->element_index; |
| 3634 | |
| 3635 | ar->element_index++; |
| 3636 | if (ar->element_index <= ar->element_count) |
| 3637 | { |
| 3638 | if (p->file_offset != (file_ptr) 0) |
| 3639 | { |
| 3640 | if (p->abfd == (bfd *) NULL) |
| 3641 | { |
| 3642 | p->abfd = _bfd_create_empty_archive_element_shell (arch); |
| 3643 | p->abfd->origin = p->file_offset; |
| 3644 | } |
| 3645 | return p->abfd; |
| 3646 | } |
| 3647 | } |
| 3648 | else |
| 3649 | { |
| 3650 | bfd_set_error (bfd_error_no_more_archived_files); |
| 3651 | return NULL; |
| 3652 | } |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | static bfd_boolean |
| 3657 | ieee_find_nearest_line (bfd *abfd ATTRIBUTE_UNUSED, |
| 3658 | asection *section ATTRIBUTE_UNUSED, |
| 3659 | asymbol **symbols ATTRIBUTE_UNUSED, |
| 3660 | bfd_vma offset ATTRIBUTE_UNUSED, |
| 3661 | const char **filename_ptr ATTRIBUTE_UNUSED, |
| 3662 | const char **functionname_ptr ATTRIBUTE_UNUSED, |
| 3663 | unsigned int *line_ptr ATTRIBUTE_UNUSED) |
| 3664 | { |
| 3665 | return FALSE; |
| 3666 | } |
| 3667 | |
| 3668 | static int |
| 3669 | ieee_generic_stat_arch_elt (bfd *abfd, struct stat *buf) |
| 3670 | { |
| 3671 | ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL; |
| 3672 | ieee_data_type *ieee; |
| 3673 | |
| 3674 | if (abfd->my_archive != NULL) |
| 3675 | ar = abfd->my_archive->tdata.ieee_ar_data; |
| 3676 | if (ar == (ieee_ar_data_type *) NULL) |
| 3677 | { |
| 3678 | bfd_set_error (bfd_error_invalid_operation); |
| 3679 | return -1; |
| 3680 | } |
| 3681 | |
| 3682 | if (IEEE_DATA (abfd) == NULL) |
| 3683 | { |
| 3684 | if (ieee_object_p (abfd) == NULL) |
| 3685 | { |
| 3686 | bfd_set_error (bfd_error_wrong_format); |
| 3687 | return -1; |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | ieee = IEEE_DATA (abfd); |
| 3692 | |
| 3693 | buf->st_size = ieee->w.r.me_record + 1; |
| 3694 | buf->st_mode = 0644; |
| 3695 | return 0; |
| 3696 | } |
| 3697 | |
| 3698 | static int |
| 3699 | ieee_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED, |
| 3700 | bfd_boolean x ATTRIBUTE_UNUSED) |
| 3701 | { |
| 3702 | return 0; |
| 3703 | } |
| 3704 | |
| 3705 | #define ieee_close_and_cleanup _bfd_generic_close_and_cleanup |
| 3706 | #define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info |
| 3707 | |
| 3708 | #define ieee_slurp_armap bfd_true |
| 3709 | #define ieee_slurp_extended_name_table bfd_true |
| 3710 | #define ieee_construct_extended_name_table \ |
| 3711 | ((bfd_boolean (*) \ |
| 3712 | PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \ |
| 3713 | bfd_true) |
| 3714 | #define ieee_truncate_arname bfd_dont_truncate_arname |
| 3715 | #define ieee_write_armap \ |
| 3716 | ((bfd_boolean (*) \ |
| 3717 | PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \ |
| 3718 | bfd_true) |
| 3719 | #define ieee_read_ar_hdr bfd_nullvoidptr |
| 3720 | #define ieee_update_armap_timestamp bfd_true |
| 3721 | #define ieee_get_elt_at_index _bfd_generic_get_elt_at_index |
| 3722 | |
| 3723 | #define ieee_bfd_is_target_special_symbol \ |
| 3724 | ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false) |
| 3725 | #define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name |
| 3726 | #define ieee_get_lineno _bfd_nosymbols_get_lineno |
| 3727 | #define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol |
| 3728 | #define ieee_read_minisymbols _bfd_generic_read_minisymbols |
| 3729 | #define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol |
| 3730 | |
| 3731 | #define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup |
| 3732 | |
| 3733 | #define ieee_set_arch_mach _bfd_generic_set_arch_mach |
| 3734 | |
| 3735 | #define ieee_get_section_contents_in_window \ |
| 3736 | _bfd_generic_get_section_contents_in_window |
| 3737 | #define ieee_bfd_get_relocated_section_contents \ |
| 3738 | bfd_generic_get_relocated_section_contents |
| 3739 | #define ieee_bfd_relax_section bfd_generic_relax_section |
| 3740 | #define ieee_bfd_gc_sections bfd_generic_gc_sections |
| 3741 | #define ieee_bfd_merge_sections bfd_generic_merge_sections |
| 3742 | #define ieee_bfd_is_group_section bfd_generic_is_group_section |
| 3743 | #define ieee_bfd_discard_group bfd_generic_discard_group |
| 3744 | #define ieee_section_already_linked \ |
| 3745 | _bfd_generic_section_already_linked |
| 3746 | #define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create |
| 3747 | #define ieee_bfd_link_hash_table_free _bfd_generic_link_hash_table_free |
| 3748 | #define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols |
| 3749 | #define ieee_bfd_link_just_syms _bfd_generic_link_just_syms |
| 3750 | #define ieee_bfd_final_link _bfd_generic_final_link |
| 3751 | #define ieee_bfd_link_split_section _bfd_generic_link_split_section |
| 3752 | |
| 3753 | const bfd_target ieee_vec = |
| 3754 | { |
| 3755 | "ieee", /* name */ |
| 3756 | bfd_target_ieee_flavour, |
| 3757 | BFD_ENDIAN_UNKNOWN, /* target byte order */ |
| 3758 | BFD_ENDIAN_UNKNOWN, /* target headers byte order */ |
| 3759 | (HAS_RELOC | EXEC_P | /* object flags */ |
| 3760 | HAS_LINENO | HAS_DEBUG | |
| 3761 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED), |
| 3762 | (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS |
| 3763 | | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */ |
| 3764 | '_', /* leading underscore */ |
| 3765 | ' ', /* ar_pad_char */ |
| 3766 | 16, /* ar_max_namelen */ |
| 3767 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
| 3768 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, |
| 3769 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */ |
| 3770 | bfd_getb64, bfd_getb_signed_64, bfd_putb64, |
| 3771 | bfd_getb32, bfd_getb_signed_32, bfd_putb32, |
| 3772 | bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */ |
| 3773 | |
| 3774 | {_bfd_dummy_target, |
| 3775 | ieee_object_p, /* bfd_check_format */ |
| 3776 | ieee_archive_p, |
| 3777 | _bfd_dummy_target, |
| 3778 | }, |
| 3779 | { |
| 3780 | bfd_false, |
| 3781 | ieee_mkobject, |
| 3782 | _bfd_generic_mkarchive, |
| 3783 | bfd_false |
| 3784 | }, |
| 3785 | { |
| 3786 | bfd_false, |
| 3787 | ieee_write_object_contents, |
| 3788 | _bfd_write_archive_contents, |
| 3789 | bfd_false, |
| 3790 | }, |
| 3791 | |
| 3792 | /* ieee_close_and_cleanup, ieee_bfd_free_cached_info, ieee_new_section_hook, |
| 3793 | ieee_get_section_contents, ieee_get_section_contents_in_window. */ |
| 3794 | BFD_JUMP_TABLE_GENERIC (ieee), |
| 3795 | |
| 3796 | BFD_JUMP_TABLE_COPY (_bfd_generic), |
| 3797 | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
| 3798 | |
| 3799 | /* ieee_slurp_armap, ieee_slurp_extended_name_table, |
| 3800 | ieee_construct_extended_name_table, ieee_truncate_arname, |
| 3801 | ieee_write_armap, ieee_read_ar_hdr, ieee_openr_next_archived_file, |
| 3802 | ieee_get_elt_at_index, ieee_generic_stat_arch_elt, |
| 3803 | ieee_update_armap_timestamp. */ |
| 3804 | BFD_JUMP_TABLE_ARCHIVE (ieee), |
| 3805 | |
| 3806 | /* ieee_get_symtab_upper_bound, ieee_canonicalize_symtab, |
| 3807 | ieee_make_empty_symbol, ieee_print_symbol, ieee_get_symbol_info, |
| 3808 | ieee_bfd_is_local_label_name, ieee_get_lineno, |
| 3809 | ieee_find_nearest_line, ieee_bfd_make_debug_symbol, |
| 3810 | ieee_read_minisymbols, ieee_minisymbol_to_symbol. */ |
| 3811 | BFD_JUMP_TABLE_SYMBOLS (ieee), |
| 3812 | |
| 3813 | /* ieee_get_reloc_upper_bound, ieee_canonicalize_reloc, |
| 3814 | ieee_bfd_reloc_type_lookup. */ |
| 3815 | BFD_JUMP_TABLE_RELOCS (ieee), |
| 3816 | |
| 3817 | /* ieee_set_arch_mach, ieee_set_section_contents. */ |
| 3818 | BFD_JUMP_TABLE_WRITE (ieee), |
| 3819 | |
| 3820 | /* ieee_sizeof_headers, ieee_bfd_get_relocated_section_contents, |
| 3821 | ieee_bfd_relax_section, ieee_bfd_link_hash_table_create, |
| 3822 | _bfd_generic_link_hash_table_free, |
| 3823 | ieee_bfd_link_add_symbols, ieee_bfd_final_link, |
| 3824 | ieee_bfd_link_split_section, ieee_bfd_gc_sections, |
| 3825 | ieee_bfd_merge_sections. */ |
| 3826 | BFD_JUMP_TABLE_LINK (ieee), |
| 3827 | |
| 3828 | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
| 3829 | |
| 3830 | NULL, |
| 3831 | |
| 3832 | (void *) 0 |
| 3833 | }; |