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