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