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