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