* elf.c (elf_fake_sections): Undo change of 1999-05-10.
[binutils.git] / bfd / ieee.c
blob126b136d3378a7bc10051efee818bace83d9df13
1 /* BFD back-end for ieee-695 objects.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #define KEEPMINUSPCININST 0
25 /* IEEE 695 format is a stream of records, which we parse using a simple one-
26 token (which is one byte in this lexicon) lookahead recursive decent
27 parser. */
29 #include "bfd.h"
30 #include "sysdep.h"
31 #include "libbfd.h"
32 #include "ieee.h"
33 #include "libieee.h"
35 #include <ctype.h>
37 static boolean ieee_write_byte PARAMS ((bfd *, int));
38 static boolean ieee_write_2bytes PARAMS ((bfd *, int));
39 static boolean ieee_write_int PARAMS ((bfd *, bfd_vma));
40 static boolean ieee_write_id PARAMS ((bfd *, const char *));
41 static boolean ieee_write_expression
42 PARAMS ((bfd *, bfd_vma, asymbol *, boolean, unsigned int));
43 static void ieee_write_int5 PARAMS ((bfd_byte *, bfd_vma));
44 static boolean ieee_write_int5_out PARAMS ((bfd *, bfd_vma));
45 static boolean ieee_write_section_part PARAMS ((bfd *));
46 static boolean do_with_relocs PARAMS ((bfd *, asection *));
47 static boolean do_as_repeat PARAMS ((bfd *, asection *));
48 static boolean do_without_relocs PARAMS ((bfd *, asection *));
49 static boolean ieee_write_external_part PARAMS ((bfd *));
50 static boolean ieee_write_data_part PARAMS ((bfd *));
51 static boolean ieee_write_debug_part PARAMS ((bfd *));
52 static boolean ieee_write_me_part PARAMS ((bfd *));
53 static boolean ieee_write_processor PARAMS ((bfd *));
55 static boolean ieee_slurp_debug PARAMS ((bfd *));
56 static boolean ieee_slurp_section_data PARAMS ((bfd *));
58 /* Functions for writing to ieee files in the strange way that the
59 standard requires. */
61 static boolean
62 ieee_write_byte (abfd, barg)
63 bfd *abfd;
64 int barg;
66 bfd_byte byte;
68 byte = barg;
69 if (bfd_write ((PTR) &byte, 1, 1, abfd) != 1)
70 return false;
71 return true;
74 static boolean
75 ieee_write_2bytes (abfd, bytes)
76 bfd *abfd;
77 int bytes;
79 bfd_byte buffer[2];
81 buffer[0] = bytes >> 8;
82 buffer[1] = bytes & 0xff;
83 if (bfd_write ((PTR) buffer, 1, 2, abfd) != 2)
84 return false;
85 return true;
88 static boolean
89 ieee_write_int (abfd, value)
90 bfd *abfd;
91 bfd_vma value;
93 if (value <= 127)
95 if (! ieee_write_byte (abfd, (bfd_byte) value))
96 return false;
98 else
100 unsigned int length;
102 /* How many significant bytes ? */
103 /* FIXME FOR LONGER INTS */
104 if (value & 0xff000000)
105 length = 4;
106 else if (value & 0x00ff0000)
107 length = 3;
108 else if (value & 0x0000ff00)
109 length = 2;
110 else
111 length = 1;
113 if (! ieee_write_byte (abfd,
114 (bfd_byte) ((int) ieee_number_repeat_start_enum
115 + length)))
116 return false;
117 switch (length)
119 case 4:
120 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 24)))
121 return false;
122 /* Fall through. */
123 case 3:
124 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 16)))
125 return false;
126 /* Fall through. */
127 case 2:
128 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 8)))
129 return false;
130 /* Fall through. */
131 case 1:
132 if (! ieee_write_byte (abfd, (bfd_byte) (value)))
133 return false;
137 return true;
140 static boolean
141 ieee_write_id (abfd, id)
142 bfd *abfd;
143 const char *id;
145 size_t length = strlen (id);
147 if (length <= 127)
149 if (! ieee_write_byte (abfd, (bfd_byte) length))
150 return false;
152 else if (length < 255)
154 if (! ieee_write_byte (abfd, ieee_extension_length_1_enum)
155 || ! ieee_write_byte (abfd, (bfd_byte) length))
156 return false;
158 else if (length < 65535)
160 if (! ieee_write_byte (abfd, ieee_extension_length_2_enum)
161 || ! ieee_write_2bytes (abfd, (int) length))
162 return false;
164 else
166 (*_bfd_error_handler)
167 (_("%s: string too long (%d chars, max 65535)"),
168 bfd_get_filename (abfd), length);
169 bfd_set_error (bfd_error_invalid_operation);
170 return false;
173 if (bfd_write ((PTR) id, 1, length, abfd) != length)
174 return false;
175 return true;
178 /***************************************************************************
179 Functions for reading from ieee files in the strange way that the
180 standard requires:
183 #define this_byte(ieee) *((ieee)->input_p)
184 #define next_byte(ieee) ((ieee)->input_p++)
185 #define this_byte_and_next(ieee) (*((ieee)->input_p++))
187 static unsigned short
188 read_2bytes (ieee)
189 common_header_type *ieee;
191 unsigned char c1 = this_byte_and_next (ieee);
192 unsigned char c2 = this_byte_and_next (ieee);
193 return (c1 << 8) | c2;
196 static void
197 bfd_get_string (ieee, string, length)
198 common_header_type *ieee;
199 char *string;
200 size_t length;
202 size_t i;
203 for (i = 0; i < length; i++)
205 string[i] = this_byte_and_next (ieee);
209 static char *
210 read_id (ieee)
211 common_header_type *ieee;
213 size_t length;
214 char *string;
215 length = this_byte_and_next (ieee);
216 if (length <= 0x7f)
218 /* Simple string of length 0 to 127 */
220 else if (length == 0xde)
222 /* Length is next byte, allowing 0..255 */
223 length = this_byte_and_next (ieee);
225 else if (length == 0xdf)
227 /* Length is next two bytes, allowing 0..65535 */
228 length = this_byte_and_next (ieee);
229 length = (length * 256) + this_byte_and_next (ieee);
231 /* Buy memory and read string */
232 string = bfd_alloc (ieee->abfd, length + 1);
233 if (!string)
234 return NULL;
235 bfd_get_string (ieee, string, length);
236 string[length] = 0;
237 return string;
240 static boolean
241 ieee_write_expression (abfd, value, symbol, pcrel, index)
242 bfd *abfd;
243 bfd_vma value;
244 asymbol *symbol;
245 boolean pcrel;
246 unsigned int index;
248 unsigned int term_count = 0;
250 if (value != 0)
252 if (! ieee_write_int (abfd, value))
253 return false;
254 term_count++;
257 if (bfd_is_com_section (symbol->section)
258 || bfd_is_und_section (symbol->section))
260 /* Def of a common symbol */
261 if (! ieee_write_byte (abfd, ieee_variable_X_enum)
262 || ! ieee_write_int (abfd, symbol->value))
263 return false;
264 term_count++;
266 else if (! bfd_is_abs_section (symbol->section))
268 /* Ref to defined symbol - */
270 if (symbol->flags & BSF_GLOBAL)
272 if (! ieee_write_byte (abfd, ieee_variable_I_enum)
273 || ! ieee_write_int (abfd, symbol->value))
274 return false;
275 term_count++;
277 else if (symbol->flags & (BSF_LOCAL | BSF_SECTION_SYM))
279 /* This is a reference to a defined local symbol. We can
280 easily do a local as a section+offset. */
281 if (! ieee_write_byte (abfd, ieee_variable_R_enum)
282 || ! ieee_write_byte (abfd,
283 (bfd_byte) (symbol->section->index
284 + IEEE_SECTION_NUMBER_BASE)))
285 return false;
286 term_count++;
287 if (symbol->value != 0)
289 if (! ieee_write_int (abfd, symbol->value))
290 return false;
291 term_count++;
294 else
296 (*_bfd_error_handler)
297 (_("%s: unrecognized symbol `%s' flags 0x%x"),
298 bfd_get_filename (abfd), bfd_asymbol_name (symbol),
299 symbol->flags);
300 bfd_set_error (bfd_error_invalid_operation);
301 return false;
305 if (pcrel)
307 /* subtract the pc from here by asking for PC of this section*/
308 if (! ieee_write_byte (abfd, ieee_variable_P_enum)
309 || ! ieee_write_byte (abfd,
310 (bfd_byte) (index + IEEE_SECTION_NUMBER_BASE))
311 || ! ieee_write_byte (abfd, ieee_function_minus_enum))
312 return false;
315 /* Handle the degenerate case of a 0 address. */
316 if (term_count == 0)
318 if (! ieee_write_int (abfd, 0))
319 return false;
322 while (term_count > 1)
324 if (! ieee_write_byte (abfd, ieee_function_plus_enum))
325 return false;
326 term_count--;
329 return true;
332 /*****************************************************************************/
335 writes any integer into the buffer supplied and always takes 5 bytes
337 static void
338 ieee_write_int5 (buffer, value)
339 bfd_byte *buffer;
340 bfd_vma value;
342 buffer[0] = (bfd_byte) ieee_number_repeat_4_enum;
343 buffer[1] = (value >> 24) & 0xff;
344 buffer[2] = (value >> 16) & 0xff;
345 buffer[3] = (value >> 8) & 0xff;
346 buffer[4] = (value >> 0) & 0xff;
349 static boolean
350 ieee_write_int5_out (abfd, value)
351 bfd *abfd;
352 bfd_vma value;
354 bfd_byte b[5];
356 ieee_write_int5 (b, value);
357 if (bfd_write ((PTR) b, 1, 5, abfd) != 5)
358 return false;
359 return true;
362 static boolean
363 parse_int (ieee, value_ptr)
364 common_header_type *ieee;
365 bfd_vma *value_ptr;
367 int value = this_byte (ieee);
368 int result;
369 if (value >= 0 && value <= 127)
371 *value_ptr = value;
372 next_byte (ieee);
373 return true;
375 else if (value >= 0x80 && value <= 0x88)
377 unsigned int count = value & 0xf;
378 result = 0;
379 next_byte (ieee);
380 while (count)
382 result = (result << 8) | this_byte_and_next (ieee);
383 count--;
385 *value_ptr = result;
386 return true;
388 return false;
391 static int
392 parse_i (ieee, ok)
393 common_header_type *ieee;
394 boolean *ok;
396 bfd_vma x;
397 *ok = parse_int (ieee, &x);
398 return x;
401 static bfd_vma
402 must_parse_int (ieee)
403 common_header_type *ieee;
405 bfd_vma result;
406 BFD_ASSERT (parse_int (ieee, &result) == true);
407 return result;
410 typedef struct
412 bfd_vma value;
413 asection *section;
414 ieee_symbol_index_type symbol;
415 } ieee_value_type;
418 #if KEEPMINUSPCININST
420 #define SRC_MASK(arg) arg
421 #define PCREL_OFFSET false
423 #else
425 #define SRC_MASK(arg) 0
426 #define PCREL_OFFSET true
428 #endif
430 static reloc_howto_type abs32_howto =
431 HOWTO (1,
435 false,
437 complain_overflow_bitfield,
439 "abs32",
440 true,
441 0xffffffff,
442 0xffffffff,
443 false);
445 static reloc_howto_type abs16_howto =
446 HOWTO (1,
450 false,
452 complain_overflow_bitfield,
454 "abs16",
455 true,
456 0x0000ffff,
457 0x0000ffff,
458 false);
460 static reloc_howto_type abs8_howto =
461 HOWTO (1,
465 false,
467 complain_overflow_bitfield,
469 "abs8",
470 true,
471 0x000000ff,
472 0x000000ff,
473 false);
475 static reloc_howto_type rel32_howto =
476 HOWTO (1,
480 true,
482 complain_overflow_signed,
484 "rel32",
485 true,
486 SRC_MASK (0xffffffff),
487 0xffffffff,
488 PCREL_OFFSET);
490 static reloc_howto_type rel16_howto =
491 HOWTO (1,
495 true,
497 complain_overflow_signed,
499 "rel16",
500 true,
501 SRC_MASK (0x0000ffff),
502 0x0000ffff,
503 PCREL_OFFSET);
505 static reloc_howto_type rel8_howto =
506 HOWTO (1,
510 true,
512 complain_overflow_signed,
514 "rel8",
515 true,
516 SRC_MASK (0x000000ff),
517 0x000000ff,
518 PCREL_OFFSET);
520 static ieee_symbol_index_type NOSYMBOL = {0, 0};
522 static void
523 parse_expression (ieee, value, symbol, pcrel, extra, section)
524 ieee_data_type *ieee;
525 bfd_vma *value;
526 ieee_symbol_index_type *symbol;
527 boolean *pcrel;
528 unsigned int *extra;
529 asection **section;
532 #define POS sp[1]
533 #define TOS sp[0]
534 #define NOS sp[-1]
535 #define INC sp++;
536 #define DEC sp--;
538 boolean loop = true;
539 ieee_value_type stack[10];
541 /* The stack pointer always points to the next unused location */
542 #define PUSH(x,y,z) TOS.symbol=x;TOS.section=y;TOS.value=z;INC;
543 #define POP(x,y,z) DEC;x=TOS.symbol;y=TOS.section;z=TOS.value;
544 ieee_value_type *sp = stack;
546 while (loop)
548 switch (this_byte (&(ieee->h)))
550 case ieee_variable_P_enum:
551 /* P variable, current program counter for section n */
553 int section_n;
554 next_byte (&(ieee->h));
555 *pcrel = true;
556 section_n = must_parse_int (&(ieee->h));
557 PUSH (NOSYMBOL, bfd_abs_section_ptr, 0);
558 break;
560 case ieee_variable_L_enum:
561 /* L variable address of section N */
562 next_byte (&(ieee->h));
563 PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
564 break;
565 case ieee_variable_R_enum:
566 /* R variable, logical address of section module */
567 /* FIXME, this should be different to L */
568 next_byte (&(ieee->h));
569 PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
570 break;
571 case ieee_variable_S_enum:
572 /* S variable, size in MAUS of section module */
573 next_byte (&(ieee->h));
574 PUSH (NOSYMBOL,
576 ieee->section_table[must_parse_int (&(ieee->h))]->_raw_size);
577 break;
578 case ieee_variable_I_enum:
579 /* Push the address of variable n */
581 ieee_symbol_index_type sy;
582 next_byte (&(ieee->h));
583 sy.index = (int) must_parse_int (&(ieee->h));
584 sy.letter = 'I';
586 PUSH (sy, bfd_abs_section_ptr, 0);
588 break;
589 case ieee_variable_X_enum:
590 /* Push the address of external variable n */
592 ieee_symbol_index_type sy;
593 next_byte (&(ieee->h));
594 sy.index = (int) (must_parse_int (&(ieee->h)));
595 sy.letter = 'X';
597 PUSH (sy, bfd_und_section_ptr, 0);
599 break;
600 case ieee_function_minus_enum:
602 bfd_vma value1, value2;
603 asection *section1, *section_dummy;
604 ieee_symbol_index_type sy;
605 next_byte (&(ieee->h));
607 POP (sy, section1, value1);
608 POP (sy, section_dummy, value2);
609 PUSH (sy, section1 ? section1 : section_dummy, value2 - value1);
611 break;
612 case ieee_function_plus_enum:
614 bfd_vma value1, value2;
615 asection *section1;
616 asection *section2;
617 ieee_symbol_index_type sy1;
618 ieee_symbol_index_type sy2;
619 next_byte (&(ieee->h));
621 POP (sy1, section1, value1);
622 POP (sy2, section2, value2);
623 PUSH (sy1.letter ? sy1 : sy2,
624 bfd_is_abs_section (section1) ? section2 : section1,
625 value1 + value2);
627 break;
628 default:
630 bfd_vma va;
631 BFD_ASSERT (this_byte (&(ieee->h)) < (int) ieee_variable_A_enum
632 || this_byte (&(ieee->h)) > (int) ieee_variable_Z_enum);
633 if (parse_int (&(ieee->h), &va))
635 PUSH (NOSYMBOL, bfd_abs_section_ptr, va);
637 else
640 Thats all that we can understand. As far as I can see
641 there is a bug in the Microtec IEEE output which I'm
642 using to scan, whereby the comma operator is omitted
643 sometimes in an expression, giving expressions with too
644 many terms. We can tell if that's the case by ensuring
645 that sp == stack here. If not, then we've pushed
646 something too far, so we keep adding. */
648 while (sp != stack + 1)
650 asection *section1;
651 ieee_symbol_index_type sy1;
652 POP (sy1, section1, *extra);
655 asection *dummy;
657 POP (*symbol, dummy, *value);
658 if (section)
659 *section = dummy;
662 loop = false;
670 #define ieee_seek(abfd, offset) \
671 IEEE_DATA(abfd)->h.input_p = IEEE_DATA(abfd)->h.first_byte + offset
673 #define ieee_pos(abfd) \
674 (IEEE_DATA(abfd)->h.input_p - IEEE_DATA(abfd)->h.first_byte)
676 static unsigned int last_index;
677 static char last_type; /* is the index for an X or a D */
679 static ieee_symbol_type *
680 get_symbol (abfd,
681 ieee,
682 last_symbol,
683 symbol_count,
684 pptr,
685 max_index,
686 this_type
688 bfd *abfd;
689 ieee_data_type *ieee;
690 ieee_symbol_type *last_symbol;
691 unsigned int *symbol_count;
692 ieee_symbol_type ***pptr;
693 unsigned int *max_index;
694 char this_type
697 /* Need a new symbol */
698 unsigned int new_index = must_parse_int (&(ieee->h));
699 if (new_index != last_index || this_type != last_type)
701 ieee_symbol_type *new_symbol = (ieee_symbol_type *) bfd_alloc (ieee->h.abfd,
702 sizeof (ieee_symbol_type));
703 if (!new_symbol)
704 return NULL;
706 new_symbol->index = new_index;
707 last_index = new_index;
708 (*symbol_count)++;
709 **pptr = new_symbol;
710 *pptr = &new_symbol->next;
711 if (new_index > *max_index)
713 *max_index = new_index;
715 last_type = this_type;
716 new_symbol->symbol.section = bfd_abs_section_ptr;
717 return new_symbol;
719 return last_symbol;
722 static boolean
723 ieee_slurp_external_symbols (abfd)
724 bfd *abfd;
726 ieee_data_type *ieee = IEEE_DATA (abfd);
727 file_ptr offset = ieee->w.r.external_part;
729 ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
730 ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
731 ieee_symbol_type *symbol = (ieee_symbol_type *) NULL;
732 unsigned int symbol_count = 0;
733 boolean loop = true;
734 last_index = 0xffffff;
735 ieee->symbol_table_full = true;
737 ieee_seek (abfd, offset);
739 while (loop)
741 switch (this_byte (&(ieee->h)))
743 case ieee_nn_record:
744 next_byte (&(ieee->h));
746 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
747 &prev_symbols_ptr,
748 &ieee->external_symbol_max_index, 'I');
749 if (symbol == NULL)
750 return false;
752 symbol->symbol.the_bfd = abfd;
753 symbol->symbol.name = read_id (&(ieee->h));
754 symbol->symbol.udata.p = (PTR) NULL;
755 symbol->symbol.flags = BSF_NO_FLAGS;
756 break;
757 case ieee_external_symbol_enum:
758 next_byte (&(ieee->h));
760 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
761 &prev_symbols_ptr,
762 &ieee->external_symbol_max_index, 'D');
763 if (symbol == NULL)
764 return false;
766 BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
768 symbol->symbol.the_bfd = abfd;
769 symbol->symbol.name = read_id (&(ieee->h));
770 symbol->symbol.udata.p = (PTR) NULL;
771 symbol->symbol.flags = BSF_NO_FLAGS;
772 break;
773 case ieee_attribute_record_enum >> 8:
775 unsigned int symbol_name_index;
776 unsigned int symbol_type_index;
777 unsigned int symbol_attribute_def;
778 bfd_vma value;
779 switch (read_2bytes (ieee))
781 case ieee_attribute_record_enum:
782 symbol_name_index = must_parse_int (&(ieee->h));
783 symbol_type_index = must_parse_int (&(ieee->h));
784 symbol_attribute_def = must_parse_int (&(ieee->h));
785 switch (symbol_attribute_def)
787 case 8:
788 case 19:
789 parse_int (&ieee->h, &value);
790 break;
791 default:
792 (*_bfd_error_handler)
793 (_("%s: unimplemented ATI record %u for symbol %u"),
794 bfd_get_filename (abfd), symbol_attribute_def,
795 symbol_name_index);
796 bfd_set_error (bfd_error_bad_value);
797 return false;
798 break;
800 break;
801 case ieee_external_reference_info_record_enum:
802 /* Skip over ATX record. */
803 parse_int (&(ieee->h), &value);
804 parse_int (&(ieee->h), &value);
805 parse_int (&(ieee->h), &value);
806 parse_int (&(ieee->h), &value);
807 break;
808 case ieee_atn_record_enum:
809 /* We may get call optimization information here,
810 which we just ignore. The format is
811 {$F1}${CE}{index}{$00}{$3F}{$3F}{#_of_ASNs} */
812 parse_int (&ieee->h, &value);
813 parse_int (&ieee->h, &value);
814 parse_int (&ieee->h, &value);
815 if (value != 0x3f)
817 (*_bfd_error_handler)
818 (_("%s: unexpected ATN type %d in external part"),
819 bfd_get_filename (abfd), (int) value);
820 bfd_set_error (bfd_error_bad_value);
821 return false;
823 parse_int (&ieee->h, &value);
824 parse_int (&ieee->h, &value);
825 while (value > 0)
827 bfd_vma val1;
829 --value;
831 switch (read_2bytes (ieee))
833 case ieee_asn_record_enum:
834 parse_int (&ieee->h, &val1);
835 parse_int (&ieee->h, &val1);
836 break;
838 default:
839 (*_bfd_error_handler)
840 (_("%s: unexpected type after ATN"),
841 bfd_get_filename (abfd));
842 bfd_set_error (bfd_error_bad_value);
843 return false;
848 break;
849 case ieee_value_record_enum >> 8:
851 unsigned int symbol_name_index;
852 ieee_symbol_index_type symbol_ignore;
853 boolean pcrel_ignore;
854 unsigned int extra;
855 next_byte (&(ieee->h));
856 next_byte (&(ieee->h));
858 symbol_name_index = must_parse_int (&(ieee->h));
859 parse_expression (ieee,
860 &symbol->symbol.value,
861 &symbol_ignore,
862 &pcrel_ignore,
863 &extra,
864 &symbol->symbol.section);
866 /* Fully linked IEEE-695 files tend to give every symbol
867 an absolute value. Try to convert that back into a
868 section relative value. FIXME: This won't always to
869 the right thing. */
870 if (bfd_is_abs_section (symbol->symbol.section)
871 && (abfd->flags & HAS_RELOC) == 0)
873 bfd_vma val;
874 asection *s;
876 val = symbol->symbol.value;
877 for (s = abfd->sections; s != NULL; s = s->next)
879 if (val >= s->vma && val < s->vma + s->_raw_size)
881 symbol->symbol.section = s;
882 symbol->symbol.value -= s->vma;
883 break;
888 symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
891 break;
892 case ieee_weak_external_reference_enum:
894 bfd_vma size;
895 bfd_vma value;
896 next_byte (&(ieee->h));
897 /* Throw away the external reference index */
898 (void) must_parse_int (&(ieee->h));
899 /* Fetch the default size if not resolved */
900 size = must_parse_int (&(ieee->h));
901 /* Fetch the defautlt value if available */
902 if (parse_int (&(ieee->h), &value) == false)
904 value = 0;
906 /* This turns into a common */
907 symbol->symbol.section = bfd_com_section_ptr;
908 symbol->symbol.value = size;
910 break;
912 case ieee_external_reference_enum:
913 next_byte (&(ieee->h));
915 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
916 &prev_reference_ptr,
917 &ieee->external_reference_max_index, 'X');
918 if (symbol == NULL)
919 return false;
921 symbol->symbol.the_bfd = abfd;
922 symbol->symbol.name = read_id (&(ieee->h));
923 symbol->symbol.udata.p = (PTR) NULL;
924 symbol->symbol.section = bfd_und_section_ptr;
925 symbol->symbol.value = (bfd_vma) 0;
926 symbol->symbol.flags = 0;
928 BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
929 break;
931 default:
932 loop = false;
936 if (ieee->external_symbol_max_index != 0)
938 ieee->external_symbol_count =
939 ieee->external_symbol_max_index -
940 ieee->external_symbol_min_index + 1;
942 else
944 ieee->external_symbol_count = 0;
947 if (ieee->external_reference_max_index != 0)
949 ieee->external_reference_count =
950 ieee->external_reference_max_index -
951 ieee->external_reference_min_index + 1;
953 else
955 ieee->external_reference_count = 0;
958 abfd->symcount =
959 ieee->external_reference_count + ieee->external_symbol_count;
961 if (symbol_count != abfd->symcount)
963 /* There are gaps in the table -- */
964 ieee->symbol_table_full = false;
967 *prev_symbols_ptr = (ieee_symbol_type *) NULL;
968 *prev_reference_ptr = (ieee_symbol_type *) NULL;
970 return true;
973 static boolean
974 ieee_slurp_symbol_table (abfd)
975 bfd *abfd;
977 if (IEEE_DATA (abfd)->read_symbols == false)
979 if (! ieee_slurp_external_symbols (abfd))
980 return false;
981 IEEE_DATA (abfd)->read_symbols = true;
983 return true;
986 long
987 ieee_get_symtab_upper_bound (abfd)
988 bfd *abfd;
990 if (! ieee_slurp_symbol_table (abfd))
991 return -1;
993 return (abfd->symcount != 0) ?
994 (abfd->symcount + 1) * (sizeof (ieee_symbol_type *)) : 0;
998 Move from our internal lists to the canon table, and insert in
999 symbol index order
1002 extern const bfd_target ieee_vec;
1004 long
1005 ieee_get_symtab (abfd, location)
1006 bfd *abfd;
1007 asymbol **location;
1009 ieee_symbol_type *symp;
1010 static bfd dummy_bfd;
1011 static asymbol empty_symbol =
1012 /* the_bfd, name, value, attr, section */
1013 {&dummy_bfd, " ieee empty", (symvalue) 0, BSF_DEBUGGING, bfd_abs_section_ptr};
1015 if (abfd->symcount)
1017 ieee_data_type *ieee = IEEE_DATA (abfd);
1018 dummy_bfd.xvec = &ieee_vec;
1019 if (! ieee_slurp_symbol_table (abfd))
1020 return -1;
1022 if (ieee->symbol_table_full == false)
1024 /* Arrgh - there are gaps in the table, run through and fill them */
1025 /* up with pointers to a null place */
1026 unsigned int i;
1027 for (i = 0; i < abfd->symcount; i++)
1029 location[i] = &empty_symbol;
1033 ieee->external_symbol_base_offset = -ieee->external_symbol_min_index;
1034 for (symp = IEEE_DATA (abfd)->external_symbols;
1035 symp != (ieee_symbol_type *) NULL;
1036 symp = symp->next)
1038 /* Place into table at correct index locations */
1039 location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
1042 /* The external refs are indexed in a bit */
1043 ieee->external_reference_base_offset =
1044 -ieee->external_reference_min_index + ieee->external_symbol_count;
1046 for (symp = IEEE_DATA (abfd)->external_reference;
1047 symp != (ieee_symbol_type *) NULL;
1048 symp = symp->next)
1050 location[symp->index + ieee->external_reference_base_offset] =
1051 &symp->symbol;
1055 if (abfd->symcount)
1057 location[abfd->symcount] = (asymbol *) NULL;
1059 return abfd->symcount;
1062 static asection *
1063 get_section_entry (abfd, ieee, index)
1064 bfd *abfd;
1065 ieee_data_type *ieee;
1066 unsigned int index;
1068 if (index >= ieee->section_table_size)
1070 unsigned int c, i;
1071 asection **n;
1073 c = ieee->section_table_size;
1074 if (c == 0)
1075 c = 20;
1076 while (c <= index)
1077 c *= 2;
1079 n = ((asection **)
1080 bfd_realloc (ieee->section_table, c * sizeof (asection *)));
1081 if (n == NULL)
1082 return NULL;
1084 for (i = ieee->section_table_size; i < c; i++)
1085 n[i] = NULL;
1087 ieee->section_table = n;
1088 ieee->section_table_size = c;
1091 if (ieee->section_table[index] == (asection *) NULL)
1093 char *tmp = bfd_alloc (abfd, 11);
1094 asection *section;
1096 if (!tmp)
1097 return NULL;
1098 sprintf (tmp, " fsec%4d", index);
1099 section = bfd_make_section (abfd, tmp);
1100 ieee->section_table[index] = section;
1101 section->flags = SEC_NO_FLAGS;
1102 section->target_index = index;
1103 ieee->section_table[index] = section;
1105 return ieee->section_table[index];
1108 static void
1109 ieee_slurp_sections (abfd)
1110 bfd *abfd;
1112 ieee_data_type *ieee = IEEE_DATA (abfd);
1113 file_ptr offset = ieee->w.r.section_part;
1114 asection *section = (asection *) NULL;
1115 char *name;
1117 if (offset != 0)
1119 bfd_byte section_type[3];
1120 ieee_seek (abfd, offset);
1121 while (true)
1123 switch (this_byte (&(ieee->h)))
1125 case ieee_section_type_enum:
1127 unsigned int section_index;
1128 next_byte (&(ieee->h));
1129 section_index = must_parse_int (&(ieee->h));
1131 section = get_section_entry (abfd, ieee, section_index);
1133 section_type[0] = this_byte_and_next (&(ieee->h));
1135 /* Set minimal section attributes. Attributes are
1136 extended later, based on section contents. */
1138 switch (section_type[0])
1140 case 0xC1:
1141 /* Normal attributes for absolute sections */
1142 section_type[1] = this_byte (&(ieee->h));
1143 section->flags = SEC_ALLOC;
1144 switch (section_type[1])
1146 case 0xD3: /* AS Absolute section attributes */
1147 next_byte (&(ieee->h));
1148 section_type[2] = this_byte (&(ieee->h));
1149 switch (section_type[2])
1151 case 0xD0:
1152 /* Normal code */
1153 next_byte (&(ieee->h));
1154 section->flags |= SEC_CODE;
1155 break;
1156 case 0xC4:
1157 /* Normal data */
1158 next_byte (&(ieee->h));
1159 section->flags |= SEC_DATA;
1160 break;
1161 case 0xD2:
1162 next_byte (&(ieee->h));
1163 /* Normal rom data */
1164 section->flags |= SEC_ROM | SEC_DATA;
1165 break;
1166 default:
1167 break;
1170 break;
1171 case 0xC3: /* Named relocatable sections (type C) */
1172 section_type[1] = this_byte (&(ieee->h));
1173 section->flags = SEC_ALLOC;
1174 switch (section_type[1])
1176 case 0xD0: /* Normal code (CP) */
1177 next_byte (&(ieee->h));
1178 section->flags |= SEC_CODE;
1179 break;
1180 case 0xC4: /* Normal data (CD) */
1181 next_byte (&(ieee->h));
1182 section->flags |= SEC_DATA;
1183 break;
1184 case 0xD2: /* Normal rom data (CR) */
1185 next_byte (&(ieee->h));
1186 section->flags |= SEC_ROM | SEC_DATA;
1187 break;
1188 default:
1189 break;
1193 /* Read section name, use it if non empty. */
1194 name = read_id (&ieee->h);
1195 if (name[0])
1196 section->name = name;
1198 /* Skip these fields, which we don't care about */
1200 bfd_vma parent, brother, context;
1201 parse_int (&(ieee->h), &parent);
1202 parse_int (&(ieee->h), &brother);
1203 parse_int (&(ieee->h), &context);
1206 break;
1207 case ieee_section_alignment_enum:
1209 unsigned int section_index;
1210 bfd_vma value;
1211 asection *section;
1212 next_byte (&(ieee->h));
1213 section_index = must_parse_int (&ieee->h);
1214 section = get_section_entry (abfd, ieee, section_index);
1215 if (section_index > ieee->section_count)
1217 ieee->section_count = section_index;
1219 section->alignment_power =
1220 bfd_log2 (must_parse_int (&ieee->h));
1221 (void) parse_int (&(ieee->h), &value);
1223 break;
1224 case ieee_e2_first_byte_enum:
1226 ieee_record_enum_type t = (ieee_record_enum_type) (read_2bytes (&(ieee->h)));
1228 switch (t)
1230 case ieee_section_size_enum:
1231 section = ieee->section_table[must_parse_int (&(ieee->h))];
1232 section->_raw_size = must_parse_int (&(ieee->h));
1233 break;
1234 case ieee_physical_region_size_enum:
1235 section = ieee->section_table[must_parse_int (&(ieee->h))];
1236 section->_raw_size = must_parse_int (&(ieee->h));
1237 break;
1238 case ieee_region_base_address_enum:
1239 section = ieee->section_table[must_parse_int (&(ieee->h))];
1240 section->vma = must_parse_int (&(ieee->h));
1241 section->lma = section->vma;
1242 break;
1243 case ieee_mau_size_enum:
1244 must_parse_int (&(ieee->h));
1245 must_parse_int (&(ieee->h));
1246 break;
1247 case ieee_m_value_enum:
1248 must_parse_int (&(ieee->h));
1249 must_parse_int (&(ieee->h));
1250 break;
1251 case ieee_section_base_address_enum:
1252 section = ieee->section_table[must_parse_int (&(ieee->h))];
1253 section->vma = must_parse_int (&(ieee->h));
1254 section->lma = section->vma;
1255 break;
1256 case ieee_section_offset_enum:
1257 (void) must_parse_int (&(ieee->h));
1258 (void) must_parse_int (&(ieee->h));
1259 break;
1260 default:
1261 return;
1264 break;
1265 default:
1266 return;
1272 /* Make a section for the debugging information, if any. We don't try
1273 to interpret the debugging information; we just point the section
1274 at the area in the file so that program which understand can dig it
1275 out. */
1277 static boolean
1278 ieee_slurp_debug (abfd)
1279 bfd *abfd;
1281 ieee_data_type *ieee = IEEE_DATA (abfd);
1282 asection *sec;
1284 if (ieee->w.r.debug_information_part == 0)
1285 return true;
1287 sec = bfd_make_section (abfd, ".debug");
1288 if (sec == NULL)
1289 return false;
1290 sec->flags |= SEC_DEBUGGING | SEC_HAS_CONTENTS;
1291 sec->filepos = ieee->w.r.debug_information_part;
1292 sec->_raw_size = ieee->w.r.data_part - ieee->w.r.debug_information_part;
1294 return true;
1297 /***********************************************************************
1298 * archive stuff
1301 const bfd_target *
1302 ieee_archive_p (abfd)
1303 bfd *abfd;
1305 char *library;
1306 unsigned int i;
1307 unsigned char buffer[512];
1308 file_ptr buffer_offset = 0;
1309 ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
1310 ieee_ar_data_type *ieee;
1311 unsigned int alc_elts;
1312 ieee_ar_obstack_type *elts = NULL;
1314 abfd->tdata.ieee_ar_data =
1315 (ieee_ar_data_type *) bfd_alloc (abfd, sizeof (ieee_ar_data_type));
1316 if (!abfd->tdata.ieee_ar_data)
1317 goto error_return;
1318 ieee = IEEE_AR_DATA (abfd);
1320 /* FIXME: Check return value. I'm not sure whether it needs to read
1321 the entire buffer or not. */
1322 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1324 ieee->h.first_byte = buffer;
1325 ieee->h.input_p = buffer;
1327 ieee->h.abfd = abfd;
1329 if (this_byte (&(ieee->h)) != Module_Beginning)
1331 abfd->tdata.ieee_ar_data = save;
1332 goto error_return;
1335 next_byte (&(ieee->h));
1336 library = read_id (&(ieee->h));
1337 if (strcmp (library, "LIBRARY") != 0)
1339 bfd_release (abfd, ieee);
1340 abfd->tdata.ieee_ar_data = save;
1341 goto error_return;
1343 /* Throw away the filename */
1344 read_id (&(ieee->h));
1346 ieee->element_count = 0;
1347 ieee->element_index = 0;
1349 next_byte (&(ieee->h)); /* Drop the ad part */
1350 must_parse_int (&(ieee->h)); /* And the two dummy numbers */
1351 must_parse_int (&(ieee->h));
1353 alc_elts = 10;
1354 elts = (ieee_ar_obstack_type *) bfd_malloc (alc_elts * sizeof *elts);
1355 if (elts == NULL)
1356 goto error_return;
1358 /* Read the index of the BB table */
1359 while (1)
1361 int rec;
1362 ieee_ar_obstack_type *t;
1364 rec = read_2bytes (&(ieee->h));
1365 if (rec != (int) ieee_assign_value_to_variable_enum)
1366 break;
1368 if (ieee->element_count >= alc_elts)
1370 ieee_ar_obstack_type *n;
1372 alc_elts *= 2;
1373 n = ((ieee_ar_obstack_type *)
1374 bfd_realloc (elts, alc_elts * sizeof *elts));
1375 if (n == NULL)
1376 goto error_return;
1377 elts = n;
1380 t = &elts[ieee->element_count];
1381 ieee->element_count++;
1383 must_parse_int (&(ieee->h));
1384 t->file_offset = must_parse_int (&(ieee->h));
1385 t->abfd = (bfd *) NULL;
1387 /* Make sure that we don't go over the end of the buffer */
1389 if ((size_t) ieee_pos (abfd) > sizeof (buffer) / 2)
1391 /* Past half way, reseek and reprime */
1392 buffer_offset += ieee_pos (abfd);
1393 if (bfd_seek (abfd, buffer_offset, SEEK_SET) != 0)
1394 goto error_return;
1395 /* FIXME: Check return value. I'm not sure whether it needs
1396 to read the entire buffer or not. */
1397 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1398 ieee->h.first_byte = buffer;
1399 ieee->h.input_p = buffer;
1403 ieee->elements = ((ieee_ar_obstack_type *)
1404 bfd_alloc (abfd,
1405 ieee->element_count * sizeof *ieee->elements));
1406 if (ieee->elements == NULL)
1407 goto error_return;
1408 memcpy (ieee->elements, elts,
1409 ieee->element_count * sizeof *ieee->elements);
1410 free (elts);
1411 elts = NULL;
1413 /* Now scan the area again, and replace BB offsets with file */
1414 /* offsets */
1416 for (i = 2; i < ieee->element_count; i++)
1418 if (bfd_seek (abfd, ieee->elements[i].file_offset, SEEK_SET) != 0)
1419 goto error_return;
1420 /* FIXME: Check return value. I'm not sure whether it needs to
1421 read the entire buffer or not. */
1422 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1423 ieee->h.first_byte = buffer;
1424 ieee->h.input_p = buffer;
1426 next_byte (&(ieee->h)); /* Drop F8 */
1427 next_byte (&(ieee->h)); /* Drop 14 */
1428 must_parse_int (&(ieee->h)); /* Drop size of block */
1429 if (must_parse_int (&(ieee->h)) != 0)
1431 /* This object has been deleted */
1432 ieee->elements[i].file_offset = 0;
1434 else
1436 ieee->elements[i].file_offset = must_parse_int (&(ieee->h));
1440 /* abfd->has_armap = ;*/
1442 return abfd->xvec;
1444 error_return:
1445 if (elts != NULL)
1446 free (elts);
1447 return NULL;
1450 static boolean
1451 ieee_mkobject (abfd)
1452 bfd *abfd;
1454 abfd->tdata.ieee_data = (ieee_data_type *) bfd_zalloc (abfd, sizeof (ieee_data_type));
1455 return abfd->tdata.ieee_data ? true : false;
1458 const bfd_target *
1459 ieee_object_p (abfd)
1460 bfd *abfd;
1462 char *processor;
1463 unsigned int part;
1464 ieee_data_type *ieee;
1465 unsigned char buffer[300];
1466 ieee_data_type *save = IEEE_DATA (abfd);
1468 abfd->tdata.ieee_data = 0;
1469 ieee_mkobject (abfd);
1471 ieee = IEEE_DATA (abfd);
1472 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1473 goto fail;
1474 /* Read the first few bytes in to see if it makes sense */
1475 /* FIXME: Check return value. I'm not sure whether it needs to read
1476 the entire buffer or not. */
1477 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1479 ieee->h.input_p = buffer;
1480 if (this_byte_and_next (&(ieee->h)) != Module_Beginning)
1481 goto got_wrong_format;
1483 ieee->read_symbols = false;
1484 ieee->read_data = false;
1485 ieee->section_count = 0;
1486 ieee->external_symbol_max_index = 0;
1487 ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
1488 ieee->external_reference_min_index = IEEE_REFERENCE_BASE;
1489 ieee->external_reference_max_index = 0;
1490 ieee->h.abfd = abfd;
1491 ieee->section_table = NULL;
1492 ieee->section_table_size = 0;
1494 processor = ieee->mb.processor = read_id (&(ieee->h));
1495 if (strcmp (processor, "LIBRARY") == 0)
1496 goto got_wrong_format;
1497 ieee->mb.module_name = read_id (&(ieee->h));
1498 if (abfd->filename == (CONST char *) NULL)
1500 abfd->filename = ieee->mb.module_name;
1502 /* Determine the architecture and machine type of the object file.
1505 const bfd_arch_info_type *arch;
1506 char family[10];
1508 /* IEEE does not specify the format of the processor identificaton
1509 string, so the compiler is free to put in it whatever it wants.
1510 We try here to recognize different processors belonging to the
1511 m68k family. Code for other processors can be added here. */
1512 if ((processor[0] == '6') && (processor[1] == '8'))
1514 if (processor[2] == '3') /* 683xx integrated processors */
1516 switch (processor[3])
1518 case '0': /* 68302, 68306, 68307 */
1519 case '2': /* 68322, 68328 */
1520 case '5': /* 68356 */
1521 strcpy (family, "68000"); /* MC68000-based controllers */
1522 break;
1524 case '3': /* 68330, 68331, 68332, 68333,
1525 68334, 68335, 68336, 68338 */
1526 case '6': /* 68360 */
1527 case '7': /* 68376 */
1528 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1529 break;
1531 case '4':
1532 if (processor[4] == '9') /* 68349 */
1533 strcpy (family, "68030"); /* CPU030 */
1534 else /* 68340, 68341 */
1535 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1536 break;
1538 default: /* Does not exist yet */
1539 strcpy (family, "68332"); /* Guess it will be CPU32 */
1542 else if (toupper (processor[3]) == 'F') /* 68F333 */
1543 strcpy (family, "68332"); /* CPU32 */
1544 else if ((toupper (processor[3]) == 'C') /* Embedded controllers */
1545 && ((toupper (processor[2]) == 'E')
1546 || (toupper (processor[2]) == 'H')
1547 || (toupper (processor[2]) == 'L')))
1549 strcpy (family, "68");
1550 strncat (family, processor + 4, 7);
1551 family[9] = '\0';
1553 else /* "Regular" processors */
1555 strncpy (family, processor, 9);
1556 family[9] = '\0';
1559 else if ((strncmp (processor, "cpu32", 5) == 0) /* CPU32 and CPU32+ */
1560 || (strncmp (processor, "CPU32", 5) == 0))
1561 strcpy (family, "68332");
1562 else
1564 strncpy (family, processor, 9);
1565 family[9] = '\0';
1568 arch = bfd_scan_arch (family);
1569 if (arch == 0)
1570 goto got_wrong_format;
1571 abfd->arch_info = arch;
1574 if (this_byte (&(ieee->h)) != (int) ieee_address_descriptor_enum)
1576 goto fail;
1578 next_byte (&(ieee->h));
1580 if (parse_int (&(ieee->h), &ieee->ad.number_of_bits_mau) == false)
1582 goto fail;
1584 if (parse_int (&(ieee->h), &ieee->ad.number_of_maus_in_address) == false)
1586 goto fail;
1589 /* If there is a byte order info, take it */
1590 if (this_byte (&(ieee->h)) == (int) ieee_variable_L_enum ||
1591 this_byte (&(ieee->h)) == (int) ieee_variable_M_enum)
1592 next_byte (&(ieee->h));
1594 for (part = 0; part < N_W_VARIABLES; part++)
1596 boolean ok;
1597 if (read_2bytes (&(ieee->h)) != (int) ieee_assign_value_to_variable_enum)
1599 goto fail;
1601 if (this_byte_and_next (&(ieee->h)) != part)
1603 goto fail;
1606 ieee->w.offset[part] = parse_i (&(ieee->h), &ok);
1607 if (ok == false)
1609 goto fail;
1614 if (ieee->w.r.external_part != 0)
1615 abfd->flags = HAS_SYMS;
1617 /* By now we know that this is a real IEEE file, we're going to read
1618 the whole thing into memory so that we can run up and down it
1619 quickly. We can work out how big the file is from the trailer
1620 record */
1622 IEEE_DATA (abfd)->h.first_byte =
1623 (unsigned char *) bfd_alloc (ieee->h.abfd, ieee->w.r.me_record + 1);
1624 if (!IEEE_DATA (abfd)->h.first_byte)
1625 goto fail;
1626 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1627 goto fail;
1628 /* FIXME: Check return value. I'm not sure whether it needs to read
1629 the entire buffer or not. */
1630 bfd_read ((PTR) (IEEE_DATA (abfd)->h.first_byte), 1,
1631 ieee->w.r.me_record + 1, abfd);
1633 ieee_slurp_sections (abfd);
1635 if (! ieee_slurp_debug (abfd))
1636 goto fail;
1638 /* Parse section data to activate file and section flags implied by
1639 section contents. */
1641 if (! ieee_slurp_section_data (abfd))
1642 goto fail;
1644 return abfd->xvec;
1645 got_wrong_format:
1646 bfd_set_error (bfd_error_wrong_format);
1647 fail:
1648 (void) bfd_release (abfd, ieee);
1649 abfd->tdata.ieee_data = save;
1650 return (const bfd_target *) NULL;
1653 void
1654 ieee_get_symbol_info (ignore_abfd, symbol, ret)
1655 bfd *ignore_abfd;
1656 asymbol *symbol;
1657 symbol_info *ret;
1659 bfd_symbol_info (symbol, ret);
1660 if (symbol->name[0] == ' ')
1661 ret->name = "* empty table entry ";
1662 if (!symbol->section)
1663 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
1666 void
1667 ieee_print_symbol (ignore_abfd, afile, symbol, how)
1668 bfd *ignore_abfd;
1669 PTR afile;
1670 asymbol *symbol;
1671 bfd_print_symbol_type how;
1673 FILE *file = (FILE *) afile;
1675 switch (how)
1677 case bfd_print_symbol_name:
1678 fprintf (file, "%s", symbol->name);
1679 break;
1680 case bfd_print_symbol_more:
1681 #if 0
1682 fprintf (file, "%4x %2x", aout_symbol (symbol)->desc & 0xffff,
1683 aout_symbol (symbol)->other & 0xff);
1684 #endif
1685 BFD_FAIL ();
1686 break;
1687 case bfd_print_symbol_all:
1689 const char *section_name =
1690 (symbol->section == (asection *) NULL
1691 ? "*abs"
1692 : symbol->section->name);
1693 if (symbol->name[0] == ' ')
1695 fprintf (file, "* empty table entry ");
1697 else
1699 bfd_print_symbol_vandf ((PTR) file, symbol);
1701 fprintf (file, " %-5s %04x %02x %s",
1702 section_name,
1703 (unsigned) ieee_symbol (symbol)->index,
1704 (unsigned) 0,
1705 symbol->name);
1708 break;
1712 static boolean
1713 do_one (ieee, current_map, location_ptr, s, iterations)
1714 ieee_data_type *ieee;
1715 ieee_per_section_type *current_map;
1716 unsigned char *location_ptr;
1717 asection *s;
1718 int iterations;
1720 switch (this_byte (&(ieee->h)))
1722 case ieee_load_constant_bytes_enum:
1724 unsigned int number_of_maus;
1725 unsigned int i;
1726 next_byte (&(ieee->h));
1727 number_of_maus = must_parse_int (&(ieee->h));
1729 for (i = 0; i < number_of_maus; i++)
1731 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1732 next_byte (&(ieee->h));
1735 break;
1737 case ieee_load_with_relocation_enum:
1739 boolean loop = true;
1740 next_byte (&(ieee->h));
1741 while (loop)
1743 switch (this_byte (&(ieee->h)))
1745 case ieee_variable_R_enum:
1747 case ieee_function_signed_open_b_enum:
1748 case ieee_function_unsigned_open_b_enum:
1749 case ieee_function_either_open_b_enum:
1751 unsigned int extra = 4;
1752 boolean pcrel = false;
1753 asection *section;
1754 ieee_reloc_type *r =
1755 (ieee_reloc_type *) bfd_alloc (ieee->h.abfd,
1756 sizeof (ieee_reloc_type));
1757 if (!r)
1758 return false;
1760 *(current_map->reloc_tail_ptr) = r;
1761 current_map->reloc_tail_ptr = &r->next;
1762 r->next = (ieee_reloc_type *) NULL;
1763 next_byte (&(ieee->h));
1764 /* abort();*/
1765 r->relent.sym_ptr_ptr = 0;
1766 parse_expression (ieee,
1767 &r->relent.addend,
1768 &r->symbol,
1769 &pcrel, &extra, &section);
1770 r->relent.address = current_map->pc;
1771 s->flags |= SEC_RELOC;
1772 s->owner->flags |= HAS_RELOC;
1773 s->reloc_count++;
1774 if (r->relent.sym_ptr_ptr == NULL && section != NULL)
1775 r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1777 if (this_byte (&(ieee->h)) == (int) ieee_comma)
1779 next_byte (&(ieee->h));
1780 /* Fetch number of bytes to pad */
1781 extra = must_parse_int (&(ieee->h));
1784 switch (this_byte (&(ieee->h)))
1786 case ieee_function_signed_close_b_enum:
1787 next_byte (&(ieee->h));
1788 break;
1789 case ieee_function_unsigned_close_b_enum:
1790 next_byte (&(ieee->h));
1791 break;
1792 case ieee_function_either_close_b_enum:
1793 next_byte (&(ieee->h));
1794 break;
1795 default:
1796 break;
1798 /* Build a relocation entry for this type */
1799 /* If pc rel then stick -ve pc into instruction
1800 and take out of reloc ..
1802 I've changed this. It's all too complicated. I
1803 keep 0 in the instruction now. */
1805 switch (extra)
1807 case 0:
1808 case 4:
1810 if (pcrel == true)
1812 #if KEEPMINUSPCININST
1813 bfd_put_32 (ieee->h.abfd, -current_map->pc, location_ptr +
1814 current_map->pc);
1815 r->relent.howto = &rel32_howto;
1816 r->relent.addend -=
1817 current_map->pc;
1818 #else
1819 bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1820 current_map->pc);
1821 r->relent.howto = &rel32_howto;
1822 #endif
1824 else
1826 bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1827 current_map->pc);
1828 r->relent.howto = &abs32_howto;
1830 current_map->pc += 4;
1831 break;
1832 case 2:
1833 if (pcrel == true)
1835 #if KEEPMINUSPCININST
1836 bfd_put_16 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1837 r->relent.addend -= current_map->pc;
1838 r->relent.howto = &rel16_howto;
1839 #else
1841 bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1842 r->relent.howto = &rel16_howto;
1843 #endif
1846 else
1848 bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1849 r->relent.howto = &abs16_howto;
1851 current_map->pc += 2;
1852 break;
1853 case 1:
1854 if (pcrel == true)
1856 #if KEEPMINUSPCININST
1857 bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1858 r->relent.addend -= current_map->pc;
1859 r->relent.howto = &rel8_howto;
1860 #else
1861 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1862 r->relent.howto = &rel8_howto;
1863 #endif
1865 else
1867 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1868 r->relent.howto = &abs8_howto;
1870 current_map->pc += 1;
1871 break;
1873 default:
1874 BFD_FAIL ();
1875 return false;
1878 break;
1879 default:
1881 bfd_vma this_size;
1882 if (parse_int (&(ieee->h), &this_size) == true)
1884 unsigned int i;
1885 for (i = 0; i < this_size; i++)
1887 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1888 next_byte (&(ieee->h));
1891 else
1893 loop = false;
1898 /* Prevent more than the first load-item of an LR record
1899 from being repeated (MRI convention). */
1900 if (iterations != 1)
1901 loop = false;
1905 return true;
1908 /* Read in all the section data and relocation stuff too */
1909 static boolean
1910 ieee_slurp_section_data (abfd)
1911 bfd *abfd;
1913 bfd_byte *location_ptr = (bfd_byte *) NULL;
1914 ieee_data_type *ieee = IEEE_DATA (abfd);
1915 unsigned int section_number;
1917 ieee_per_section_type *current_map = (ieee_per_section_type *) NULL;
1918 asection *s;
1919 /* Seek to the start of the data area */
1920 if (ieee->read_data == true)
1921 return true;
1922 ieee->read_data = true;
1923 ieee_seek (abfd, ieee->w.r.data_part);
1925 /* Allocate enough space for all the section contents */
1927 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1929 ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1930 if ((s->flags & SEC_DEBUGGING) != 0)
1931 continue;
1932 per->data = (bfd_byte *) bfd_alloc (ieee->h.abfd, s->_raw_size);
1933 if (!per->data)
1934 return false;
1935 /*SUPPRESS 68*/
1936 per->reloc_tail_ptr =
1937 (ieee_reloc_type **) & (s->relocation);
1940 while (true)
1942 switch (this_byte (&(ieee->h)))
1944 /* IF we see anything strange then quit */
1945 default:
1946 return true;
1948 case ieee_set_current_section_enum:
1949 next_byte (&(ieee->h));
1950 section_number = must_parse_int (&(ieee->h));
1951 s = ieee->section_table[section_number];
1952 s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
1953 current_map = (ieee_per_section_type *) s->used_by_bfd;
1954 location_ptr = current_map->data - s->vma;
1955 /* The document I have says that Microtec's compilers reset */
1956 /* this after a sec section, even though the standard says not */
1957 /* to. SO .. */
1958 current_map->pc = s->vma;
1959 break;
1961 case ieee_e2_first_byte_enum:
1962 next_byte (&(ieee->h));
1963 switch (this_byte (&(ieee->h)))
1965 case ieee_set_current_pc_enum & 0xff:
1967 bfd_vma value;
1968 ieee_symbol_index_type symbol;
1969 unsigned int extra;
1970 boolean pcrel;
1971 next_byte (&(ieee->h));
1972 must_parse_int (&(ieee->h)); /* Thow away section #*/
1973 parse_expression (ieee, &value,
1974 &symbol,
1975 &pcrel, &extra,
1977 current_map->pc = value;
1978 BFD_ASSERT ((unsigned) (value - s->vma) <= s->_raw_size);
1980 break;
1982 case ieee_value_starting_address_enum & 0xff:
1983 next_byte (&(ieee->h));
1984 if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum)
1985 next_byte (&(ieee->h));
1986 abfd->start_address = must_parse_int (&(ieee->h));
1987 /* We've got to the end of the data now - */
1988 return true;
1989 default:
1990 BFD_FAIL ();
1991 return false;
1993 break;
1994 case ieee_repeat_data_enum:
1996 /* Repeat the following LD or LR n times - we do this by
1997 remembering the stream pointer before running it and
1998 resetting it and running it n times. We special case
1999 the repetition of a repeat_data/load_constant
2002 unsigned int iterations;
2003 unsigned char *start;
2004 next_byte (&(ieee->h));
2005 iterations = must_parse_int (&(ieee->h));
2006 start = ieee->h.input_p;
2007 if (start[0] == (int) ieee_load_constant_bytes_enum &&
2008 start[1] == 1)
2010 while (iterations != 0)
2012 location_ptr[current_map->pc++] = start[2];
2013 iterations--;
2015 next_byte (&(ieee->h));
2016 next_byte (&(ieee->h));
2017 next_byte (&(ieee->h));
2019 else
2021 while (iterations != 0)
2023 ieee->h.input_p = start;
2024 if (!do_one (ieee, current_map, location_ptr, s,
2025 iterations))
2026 return false;
2027 iterations--;
2031 break;
2032 case ieee_load_constant_bytes_enum:
2033 case ieee_load_with_relocation_enum:
2035 if (!do_one (ieee, current_map, location_ptr, s, 1))
2036 return false;
2042 boolean
2043 ieee_new_section_hook (abfd, newsect)
2044 bfd *abfd;
2045 asection *newsect;
2047 newsect->used_by_bfd = (PTR)
2048 bfd_alloc (abfd, sizeof (ieee_per_section_type));
2049 if (!newsect->used_by_bfd)
2050 return false;
2051 ieee_per_section (newsect)->data = (bfd_byte *) NULL;
2052 ieee_per_section (newsect)->section = newsect;
2053 return true;
2056 long
2057 ieee_get_reloc_upper_bound (abfd, asect)
2058 bfd *abfd;
2059 sec_ptr asect;
2061 if ((asect->flags & SEC_DEBUGGING) != 0)
2062 return 0;
2063 if (! ieee_slurp_section_data (abfd))
2064 return -1;
2065 return (asect->reloc_count + 1) * sizeof (arelent *);
2068 static boolean
2069 ieee_get_section_contents (abfd, section, location, offset, count)
2070 bfd *abfd;
2071 sec_ptr section;
2072 PTR location;
2073 file_ptr offset;
2074 bfd_size_type count;
2076 ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
2077 if ((section->flags & SEC_DEBUGGING) != 0)
2078 return _bfd_generic_get_section_contents (abfd, section, location,
2079 offset, count);
2080 ieee_slurp_section_data (abfd);
2081 (void) memcpy ((PTR) location, (PTR) (p->data + offset), (unsigned) count);
2082 return true;
2085 long
2086 ieee_canonicalize_reloc (abfd, section, relptr, symbols)
2087 bfd *abfd;
2088 sec_ptr section;
2089 arelent **relptr;
2090 asymbol **symbols;
2092 /* ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
2093 ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation);
2094 ieee_data_type *ieee = IEEE_DATA (abfd);
2096 if ((section->flags & SEC_DEBUGGING) != 0)
2097 return 0;
2099 while (src != (ieee_reloc_type *) NULL)
2101 /* Work out which symbol to attach it this reloc to */
2102 switch (src->symbol.letter)
2104 case 'I':
2105 src->relent.sym_ptr_ptr =
2106 symbols + src->symbol.index + ieee->external_symbol_base_offset;
2107 break;
2108 case 'X':
2109 src->relent.sym_ptr_ptr =
2110 symbols + src->symbol.index + ieee->external_reference_base_offset;
2111 break;
2112 case 0:
2113 if (src->relent.sym_ptr_ptr != NULL)
2114 src->relent.sym_ptr_ptr =
2115 src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr;
2116 break;
2117 default:
2119 BFD_FAIL ();
2121 *relptr++ = &src->relent;
2122 src = src->next;
2124 *relptr = (arelent *) NULL;
2125 return section->reloc_count;
2128 static int
2129 comp (ap, bp)
2130 CONST PTR ap;
2131 CONST PTR bp;
2133 arelent *a = *((arelent **) ap);
2134 arelent *b = *((arelent **) bp);
2135 return a->address - b->address;
2138 /* Write the section headers. */
2140 static boolean
2141 ieee_write_section_part (abfd)
2142 bfd *abfd;
2144 ieee_data_type *ieee = IEEE_DATA (abfd);
2145 asection *s;
2146 ieee->w.r.section_part = bfd_tell (abfd);
2147 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2149 if (! bfd_is_abs_section (s)
2150 && (s->flags & SEC_DEBUGGING) == 0)
2152 if (! ieee_write_byte (abfd, ieee_section_type_enum)
2153 || ! ieee_write_byte (abfd,
2154 (bfd_byte) (s->index
2155 + IEEE_SECTION_NUMBER_BASE)))
2156 return false;
2158 if (abfd->flags & EXEC_P)
2160 /* This image is executable, so output absolute sections */
2161 if (! ieee_write_byte (abfd, ieee_variable_A_enum)
2162 || ! ieee_write_byte (abfd, ieee_variable_S_enum))
2163 return false;
2165 else
2167 if (! ieee_write_byte (abfd, ieee_variable_C_enum))
2168 return false;
2171 switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM))
2173 case SEC_CODE | SEC_LOAD:
2174 case SEC_CODE:
2175 if (! ieee_write_byte (abfd, ieee_variable_P_enum))
2176 return false;
2177 break;
2178 case SEC_DATA:
2179 default:
2180 if (! ieee_write_byte (abfd, ieee_variable_D_enum))
2181 return false;
2182 break;
2183 case SEC_ROM:
2184 case SEC_ROM | SEC_DATA:
2185 case SEC_ROM | SEC_LOAD:
2186 case SEC_ROM | SEC_DATA | SEC_LOAD:
2187 if (! ieee_write_byte (abfd, ieee_variable_R_enum))
2188 return false;
2192 if (! ieee_write_id (abfd, s->name))
2193 return false;
2194 #if 0
2195 ieee_write_int (abfd, 0); /* Parent */
2196 ieee_write_int (abfd, 0); /* Brother */
2197 ieee_write_int (abfd, 0); /* Context */
2198 #endif
2199 /* Alignment */
2200 if (! ieee_write_byte (abfd, ieee_section_alignment_enum)
2201 || ! ieee_write_byte (abfd,
2202 (bfd_byte) (s->index
2203 + IEEE_SECTION_NUMBER_BASE))
2204 || ! ieee_write_int (abfd, 1 << s->alignment_power))
2205 return false;
2207 /* Size */
2208 if (! ieee_write_2bytes (abfd, ieee_section_size_enum)
2209 || ! ieee_write_byte (abfd,
2210 (bfd_byte) (s->index
2211 + IEEE_SECTION_NUMBER_BASE))
2212 || ! ieee_write_int (abfd, s->_raw_size))
2213 return false;
2214 if (abfd->flags & EXEC_P)
2216 /* Relocateable sections don't have asl records */
2217 /* Vma */
2218 if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum)
2219 || ! ieee_write_byte (abfd,
2220 ((bfd_byte)
2221 (s->index
2222 + IEEE_SECTION_NUMBER_BASE)))
2223 || ! ieee_write_int (abfd, s->lma))
2224 return false;
2229 return true;
2233 static boolean
2234 do_with_relocs (abfd, s)
2235 bfd *abfd;
2236 asection *s;
2238 unsigned int number_of_maus_in_address =
2239 bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd);
2240 unsigned int relocs_to_go = s->reloc_count;
2241 bfd_byte *stream = ieee_per_section (s)->data;
2242 arelent **p = s->orelocation;
2243 bfd_size_type current_byte_index = 0;
2245 qsort (s->orelocation,
2246 relocs_to_go,
2247 sizeof (arelent **),
2248 comp);
2250 /* Output the section preheader */
2251 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2252 || ! ieee_write_byte (abfd,
2253 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))
2254 || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum)
2255 || ! ieee_write_byte (abfd,
2256 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)))
2257 return false;
2258 if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0)
2260 if (! ieee_write_int (abfd, s->lma))
2261 return false;
2263 else
2265 if (! ieee_write_expression (abfd, 0, s->symbol, 0, 0))
2266 return false;
2269 if (relocs_to_go == 0)
2271 /* If there aren't any relocations then output the load constant
2272 byte opcode rather than the load with relocation opcode */
2274 while (current_byte_index < s->_raw_size)
2276 bfd_size_type run;
2277 unsigned int MAXRUN = 127;
2278 run = MAXRUN;
2279 if (run > s->_raw_size - current_byte_index)
2281 run = s->_raw_size - current_byte_index;
2284 if (run != 0)
2286 if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum))
2287 return false;
2288 /* Output a stream of bytes */
2289 if (! ieee_write_int (abfd, run))
2290 return false;
2291 if (bfd_write ((PTR) (stream + current_byte_index),
2293 run,
2294 abfd)
2295 != run)
2296 return false;
2297 current_byte_index += run;
2301 else
2303 if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum))
2304 return false;
2306 /* Output the data stream as the longest sequence of bytes
2307 possible, allowing for the a reasonable packet size and
2308 relocation stuffs. */
2310 if ((PTR) stream == (PTR) NULL)
2312 /* Outputting a section without data, fill it up */
2313 stream = (unsigned char *) (bfd_alloc (abfd, s->_raw_size));
2314 if (!stream)
2315 return false;
2316 memset ((PTR) stream, 0, (size_t) s->_raw_size);
2318 while (current_byte_index < s->_raw_size)
2320 bfd_size_type run;
2321 unsigned int MAXRUN = 127;
2322 if (relocs_to_go)
2324 run = (*p)->address - current_byte_index;
2325 if (run > MAXRUN)
2326 run = MAXRUN;
2328 else
2330 run = MAXRUN;
2332 if (run > s->_raw_size - current_byte_index)
2334 run = s->_raw_size - current_byte_index;
2337 if (run != 0)
2339 /* Output a stream of bytes */
2340 if (! ieee_write_int (abfd, run))
2341 return false;
2342 if (bfd_write ((PTR) (stream + current_byte_index),
2344 run,
2345 abfd)
2346 != run)
2347 return false;
2348 current_byte_index += run;
2350 /* Output any relocations here */
2351 if (relocs_to_go && (*p) && (*p)->address == current_byte_index)
2353 while (relocs_to_go
2354 && (*p) && (*p)->address == current_byte_index)
2356 arelent *r = *p;
2357 bfd_signed_vma ov;
2359 #if 0
2360 if (r->howto->pc_relative)
2362 r->addend += current_byte_index;
2364 #endif
2366 switch (r->howto->size)
2368 case 2:
2370 ov = bfd_get_signed_32 (abfd,
2371 stream + current_byte_index);
2372 current_byte_index += 4;
2373 break;
2374 case 1:
2375 ov = bfd_get_signed_16 (abfd,
2376 stream + current_byte_index);
2377 current_byte_index += 2;
2378 break;
2379 case 0:
2380 ov = bfd_get_signed_8 (abfd,
2381 stream + current_byte_index);
2382 current_byte_index++;
2383 break;
2384 default:
2385 ov = 0;
2386 BFD_FAIL ();
2387 return false;
2390 ov &= r->howto->src_mask;
2392 if (r->howto->pc_relative
2393 && ! r->howto->pcrel_offset)
2394 ov += r->address;
2396 if (! ieee_write_byte (abfd,
2397 ieee_function_either_open_b_enum))
2398 return false;
2400 /* abort();*/
2402 if (r->sym_ptr_ptr != (asymbol **) NULL)
2404 if (! ieee_write_expression (abfd, r->addend + ov,
2405 *(r->sym_ptr_ptr),
2406 r->howto->pc_relative,
2407 s->index))
2408 return false;
2410 else
2412 if (! ieee_write_expression (abfd, r->addend + ov,
2413 (asymbol *) NULL,
2414 r->howto->pc_relative,
2415 s->index))
2416 return false;
2419 if (number_of_maus_in_address
2420 != bfd_get_reloc_size (r->howto))
2422 if (! ieee_write_int (abfd,
2423 bfd_get_reloc_size (r->howto)))
2424 return false;
2426 if (! ieee_write_byte (abfd,
2427 ieee_function_either_close_b_enum))
2428 return false;
2430 relocs_to_go--;
2431 p++;
2438 return true;
2441 /* If there are no relocations in the output section then we can be
2442 clever about how we write. We block items up into a max of 127
2443 bytes. */
2445 static boolean
2446 do_as_repeat (abfd, s)
2447 bfd *abfd;
2448 asection *s;
2450 if (s->_raw_size)
2452 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2453 || ! ieee_write_byte (abfd,
2454 (bfd_byte) (s->index
2455 + IEEE_SECTION_NUMBER_BASE))
2456 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8)
2457 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff)
2458 || ! ieee_write_byte (abfd,
2459 (bfd_byte) (s->index
2460 + IEEE_SECTION_NUMBER_BASE))
2461 || ! ieee_write_int (abfd, s->lma)
2462 || ! ieee_write_byte (abfd, ieee_repeat_data_enum)
2463 || ! ieee_write_int (abfd, s->_raw_size)
2464 || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)
2465 || ! ieee_write_byte (abfd, 1)
2466 || ! ieee_write_byte (abfd, 0))
2467 return false;
2470 return true;
2473 static boolean
2474 do_without_relocs (abfd, s)
2475 bfd *abfd;
2476 asection *s;
2478 bfd_byte *stream = ieee_per_section (s)->data;
2480 if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
2482 if (! do_as_repeat (abfd, s))
2483 return false;
2485 else
2487 unsigned int i;
2488 for (i = 0; i < s->_raw_size; i++)
2490 if (stream[i] != 0)
2492 if (! do_with_relocs (abfd, s))
2493 return false;
2494 return true;
2497 if (! do_as_repeat (abfd, s))
2498 return false;
2501 return true;
2505 static unsigned char *output_ptr_start;
2506 static unsigned char *output_ptr;
2507 static unsigned char *output_ptr_end;
2508 static unsigned char *input_ptr_start;
2509 static unsigned char *input_ptr;
2510 static unsigned char *input_ptr_end;
2511 static bfd *input_bfd;
2512 static bfd *output_bfd;
2513 static int output_buffer;
2515 static void
2516 fill ()
2518 /* FIXME: Check return value. I'm not sure whether it needs to read
2519 the entire buffer or not. */
2520 bfd_read ((PTR) input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
2521 input_ptr = input_ptr_start;
2523 static void
2524 flush ()
2526 if (bfd_write ((PTR) (output_ptr_start), 1, output_ptr - output_ptr_start,
2527 output_bfd)
2528 != (bfd_size_type) (output_ptr - output_ptr_start))
2529 abort ();
2530 output_ptr = output_ptr_start;
2531 output_buffer++;
2534 #define THIS() ( *input_ptr )
2535 #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
2536 #define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end) flush(); }
2538 static void
2539 write_int (value)
2540 int value;
2542 if (value >= 0 && value <= 127)
2544 OUT (value);
2546 else
2548 unsigned int length;
2549 /* How many significant bytes ? */
2550 /* FIXME FOR LONGER INTS */
2551 if (value & 0xff000000)
2553 length = 4;
2555 else if (value & 0x00ff0000)
2557 length = 3;
2559 else if (value & 0x0000ff00)
2561 length = 2;
2563 else
2564 length = 1;
2566 OUT ((int) ieee_number_repeat_start_enum + length);
2567 switch (length)
2569 case 4:
2570 OUT (value >> 24);
2571 case 3:
2572 OUT (value >> 16);
2573 case 2:
2574 OUT (value >> 8);
2575 case 1:
2576 OUT (value);
2582 static void
2583 copy_id ()
2585 int length = THIS ();
2586 char ch;
2587 OUT (length);
2588 NEXT ();
2589 while (length--)
2591 ch = THIS ();
2592 OUT (ch);
2593 NEXT ();
2597 #define VAR(x) ((x | 0x80))
2598 static void
2599 copy_expression ()
2601 int stack[10];
2602 int *tos = stack;
2603 int value = 0;
2604 while (1)
2606 switch (THIS ())
2608 case 0x84:
2609 NEXT ();
2610 value = THIS ();
2611 NEXT ();
2612 value = (value << 8) | THIS ();
2613 NEXT ();
2614 value = (value << 8) | THIS ();
2615 NEXT ();
2616 value = (value << 8) | THIS ();
2617 NEXT ();
2618 *tos++ = value;
2619 break;
2620 case 0x83:
2621 NEXT ();
2622 value = THIS ();
2623 NEXT ();
2624 value = (value << 8) | THIS ();
2625 NEXT ();
2626 value = (value << 8) | THIS ();
2627 NEXT ();
2628 *tos++ = value;
2629 break;
2630 case 0x82:
2631 NEXT ();
2632 value = THIS ();
2633 NEXT ();
2634 value = (value << 8) | THIS ();
2635 NEXT ();
2636 *tos++ = value;
2637 break;
2638 case 0x81:
2639 NEXT ();
2640 value = THIS ();
2641 NEXT ();
2642 *tos++ = value;
2643 break;
2644 case 0x80:
2645 NEXT ();
2646 *tos++ = 0;
2647 break;
2648 default:
2649 if (THIS () > 0x84)
2651 /* Not a number, just bug out with the answer */
2652 write_int (*(--tos));
2653 return;
2655 *tos++ = THIS ();
2656 NEXT ();
2657 value = 0;
2658 break;
2659 case 0xa5:
2660 /* PLUS anything */
2662 int value = *(--tos);
2663 value += *(--tos);
2664 *tos++ = value;
2665 NEXT ();
2667 break;
2668 case VAR ('R'):
2670 int section_number;
2671 ieee_data_type *ieee;
2672 asection *s;
2673 NEXT ();
2674 section_number = THIS ();
2676 NEXT ();
2677 ieee = IEEE_DATA (input_bfd);
2678 s = ieee->section_table[section_number];
2679 if (s->output_section)
2681 value = s->output_section->lma;
2683 else
2685 value = 0;
2687 value += s->output_offset;
2688 *tos++ = value;
2689 value = 0;
2691 break;
2692 case 0x90:
2694 NEXT ();
2695 write_int (*(--tos));
2696 OUT (0x90);
2697 return;
2705 /* Drop the int in the buffer, and copy a null into the gap, which we
2706 will overwrite later */
2708 struct output_buffer_struct
2710 unsigned char *ptrp;
2711 int buffer;
2714 static void
2715 fill_int (buf)
2716 struct output_buffer_struct *buf;
2718 if (buf->buffer == output_buffer)
2720 /* Still a chance to output the size */
2721 int value = output_ptr - buf->ptrp + 3;
2722 buf->ptrp[0] = value >> 24;
2723 buf->ptrp[1] = value >> 16;
2724 buf->ptrp[2] = value >> 8;
2725 buf->ptrp[3] = value >> 0;
2729 static void
2730 drop_int (buf)
2731 struct output_buffer_struct *buf;
2733 int type = THIS ();
2734 int ch;
2735 if (type <= 0x84)
2737 NEXT ();
2738 switch (type)
2740 case 0x84:
2741 ch = THIS ();
2742 NEXT ();
2743 case 0x83:
2744 ch = THIS ();
2745 NEXT ();
2746 case 0x82:
2747 ch = THIS ();
2748 NEXT ();
2749 case 0x81:
2750 ch = THIS ();
2751 NEXT ();
2752 case 0x80:
2753 break;
2756 OUT (0x84);
2757 buf->ptrp = output_ptr;
2758 buf->buffer = output_buffer;
2759 OUT (0);
2760 OUT (0);
2761 OUT (0);
2762 OUT (0);
2765 static void
2766 copy_int ()
2768 int type = THIS ();
2769 int ch;
2770 if (type <= 0x84)
2772 OUT (type);
2773 NEXT ();
2774 switch (type)
2776 case 0x84:
2777 ch = THIS ();
2778 NEXT ();
2779 OUT (ch);
2780 case 0x83:
2781 ch = THIS ();
2782 NEXT ();
2783 OUT (ch);
2784 case 0x82:
2785 ch = THIS ();
2786 NEXT ();
2787 OUT (ch);
2788 case 0x81:
2789 ch = THIS ();
2790 NEXT ();
2791 OUT (ch);
2792 case 0x80:
2793 break;
2798 #define ID copy_id()
2799 #define INT copy_int()
2800 #define EXP copy_expression()
2801 static void copy_till_end ();
2802 #define INTn(q) copy_int()
2803 #define EXPn(q) copy_expression()
2805 static void
2806 f1_record ()
2808 int ch;
2809 /* ATN record */
2810 NEXT ();
2811 ch = THIS ();
2812 switch (ch)
2814 default:
2815 OUT (0xf1);
2816 OUT (ch);
2817 break;
2818 case 0xc9:
2819 NEXT ();
2820 OUT (0xf1);
2821 OUT (0xc9);
2822 INT;
2823 INT;
2824 ch = THIS ();
2825 switch (ch)
2827 case 0x16:
2828 NEXT ();
2829 break;
2830 case 0x01:
2831 NEXT ();
2832 break;
2833 case 0x00:
2834 NEXT ();
2835 INT;
2836 break;
2837 case 0x03:
2838 NEXT ();
2839 INT;
2840 break;
2841 case 0x13:
2842 EXPn (instruction address);
2843 break;
2844 default:
2845 break;
2847 break;
2848 case 0xd8:
2849 /* EXternal ref */
2850 NEXT ();
2851 OUT (0xf1);
2852 OUT (0xd8);
2853 EXP;
2854 EXP;
2855 EXP;
2856 EXP;
2857 break;
2858 case 0xce:
2859 NEXT ();
2860 OUT (0xf1);
2861 OUT (0xce);
2862 INT;
2863 INT;
2864 ch = THIS ();
2865 INT;
2866 switch (ch)
2868 case 0x01:
2869 INT;
2870 INT;
2871 break;
2872 case 0x02:
2873 INT;
2874 break;
2875 case 0x04:
2876 EXPn (external function);
2877 break;
2878 case 0x05:
2879 break;
2880 case 0x07:
2881 INTn (line number);
2882 INT;
2883 case 0x08:
2884 break;
2885 case 0x0a:
2886 INTn (locked register);
2887 INT;
2888 break;
2889 case 0x3f:
2890 copy_till_end ();
2891 break;
2892 case 0x3e:
2893 copy_till_end ();
2894 break;
2895 case 0x40:
2896 copy_till_end ();
2897 break;
2898 case 0x41:
2900 break;
2906 static void
2907 f0_record ()
2909 /* Attribute record */
2910 NEXT ();
2911 OUT (0xf0);
2912 INTn (Symbol name);
2916 static void
2917 copy_till_end ()
2919 int ch = THIS ();
2920 while (1)
2922 while (ch <= 0x80)
2924 OUT (ch);
2925 NEXT ();
2926 ch = THIS ();
2928 switch (ch)
2930 case 0x84:
2931 OUT (THIS ());
2932 NEXT ();
2933 case 0x83:
2934 OUT (THIS ());
2935 NEXT ();
2936 case 0x82:
2937 OUT (THIS ());
2938 NEXT ();
2939 case 0x81:
2940 OUT (THIS ());
2941 NEXT ();
2942 OUT (THIS ());
2943 NEXT ();
2945 ch = THIS ();
2946 break;
2947 default:
2948 return;
2954 static void
2955 f2_record ()
2957 NEXT ();
2958 OUT (0xf2);
2959 INT;
2960 NEXT ();
2961 OUT (0xce);
2962 INT;
2963 copy_till_end ();
2967 static void block ();
2968 static void
2969 f8_record ()
2971 int ch;
2972 NEXT ();
2973 ch = THIS ();
2974 switch (ch)
2976 case 0x01:
2977 case 0x02:
2978 case 0x03:
2979 /* Unique typedefs for module */
2980 /* GLobal typedefs */
2981 /* High level module scope beginning */
2983 struct output_buffer_struct ob;
2984 NEXT ();
2985 OUT (0xf8);
2986 OUT (ch);
2987 drop_int (&ob);
2990 block ();
2992 NEXT ();
2993 fill_int (&ob);
2994 OUT (0xf9);
2996 break;
2997 case 0x04:
2998 /* Global function */
3000 struct output_buffer_struct ob;
3001 NEXT ();
3002 OUT (0xf8);
3003 OUT (0x04);
3004 drop_int (&ob);
3006 INTn (stack size);
3007 INTn (ret val);
3008 EXPn (offset);
3010 block ();
3012 NEXT ();
3013 OUT (0xf9);
3014 EXPn (size of block);
3015 fill_int (&ob);
3017 break;
3019 case 0x05:
3020 /* File name for source line numbers */
3022 struct output_buffer_struct ob;
3023 NEXT ();
3024 OUT (0xf8);
3025 OUT (0x05);
3026 drop_int (&ob);
3028 INTn (year);
3029 INTn (month);
3030 INTn (day);
3031 INTn (hour);
3032 INTn (monute);
3033 INTn (second);
3034 block ();
3035 NEXT ();
3036 OUT (0xf9);
3037 fill_int (&ob);
3039 break;
3041 case 0x06:
3042 /* Local function */
3044 struct output_buffer_struct ob;
3045 NEXT ();
3046 OUT (0xf8);
3047 OUT (0x06);
3048 drop_int (&ob);
3050 INTn (stack size);
3051 INTn (type return);
3052 EXPn (offset);
3053 block ();
3054 NEXT ();
3055 OUT (0xf9);
3056 EXPn (size);
3057 fill_int (&ob);
3059 break;
3061 case 0x0a:
3062 /* Assembler module scope beginning -*/
3064 struct output_buffer_struct ob;
3066 NEXT ();
3067 OUT (0xf8);
3068 OUT (0x0a);
3069 drop_int (&ob);
3072 INT;
3074 INT;
3075 INT;
3076 INT;
3077 INT;
3078 INT;
3079 INT;
3081 block ();
3083 NEXT ();
3084 OUT (0xf9);
3085 fill_int (&ob);
3087 break;
3088 case 0x0b:
3090 struct output_buffer_struct ob;
3091 NEXT ();
3092 OUT (0xf8);
3093 OUT (0x0b);
3094 drop_int (&ob);
3096 INT;
3097 INTn (section index);
3098 EXPn (offset);
3099 INTn (stuff);
3101 block ();
3103 OUT (0xf9);
3104 NEXT ();
3105 EXPn (Size in Maus);
3106 fill_int (&ob);
3108 break;
3112 static void
3113 e2_record ()
3115 OUT (0xe2);
3116 NEXT ();
3117 OUT (0xce);
3118 NEXT ();
3119 INT;
3120 EXP;
3123 static void
3124 block ()
3126 int ch;
3127 while (1)
3129 ch = THIS ();
3130 switch (ch)
3132 case 0xe1:
3133 case 0xe5:
3134 return;
3135 case 0xf9:
3136 return;
3137 case 0xf0:
3138 f0_record ();
3139 break;
3140 case 0xf1:
3141 f1_record ();
3142 break;
3143 case 0xf2:
3144 f2_record ();
3145 break;
3146 case 0xf8:
3147 f8_record ();
3148 break;
3149 case 0xe2:
3150 e2_record ();
3151 break;
3159 /* relocate_debug,
3160 moves all the debug information from the source bfd to the output
3161 bfd, and relocates any expressions it finds
3164 static void
3165 relocate_debug (output, input)
3166 bfd *output;
3167 bfd *input;
3169 #define IBS 400
3170 #define OBS 400
3171 unsigned char input_buffer[IBS];
3173 input_ptr_start = input_ptr = input_buffer;
3174 input_ptr_end = input_buffer + IBS;
3175 input_bfd = input;
3176 /* FIXME: Check return value. I'm not sure whether it needs to read
3177 the entire buffer or not. */
3178 bfd_read ((PTR) input_ptr_start, 1, IBS, input);
3179 block ();
3183 During linking, we we told about the bfds which made up our
3184 contents, we have a list of them. They will still be open, so go to
3185 the debug info in each, and copy it out, relocating it as we go.
3188 static boolean
3189 ieee_write_debug_part (abfd)
3190 bfd *abfd;
3192 ieee_data_type *ieee = IEEE_DATA (abfd);
3193 bfd_chain_type *chain = ieee->chain_root;
3194 unsigned char output_buffer[OBS];
3195 boolean some_debug = false;
3196 file_ptr here = bfd_tell (abfd);
3198 output_ptr_start = output_ptr = output_buffer;
3199 output_ptr_end = output_buffer + OBS;
3200 output_ptr = output_buffer;
3201 output_bfd = abfd;
3203 if (chain == (bfd_chain_type *) NULL)
3205 asection *s;
3207 for (s = abfd->sections; s != NULL; s = s->next)
3208 if ((s->flags & SEC_DEBUGGING) != 0)
3209 break;
3210 if (s == NULL)
3212 ieee->w.r.debug_information_part = 0;
3213 return true;
3216 ieee->w.r.debug_information_part = here;
3217 if (bfd_write (s->contents, 1, s->_raw_size, abfd) != s->_raw_size)
3218 return false;
3220 else
3222 while (chain != (bfd_chain_type *) NULL)
3224 bfd *entry = chain->this;
3225 ieee_data_type *entry_ieee = IEEE_DATA (entry);
3226 if (entry_ieee->w.r.debug_information_part)
3228 if (bfd_seek (entry, entry_ieee->w.r.debug_information_part,
3229 SEEK_SET)
3230 != 0)
3231 return false;
3232 relocate_debug (abfd, entry);
3235 chain = chain->next;
3237 if (some_debug)
3239 ieee->w.r.debug_information_part = here;
3241 else
3243 ieee->w.r.debug_information_part = 0;
3246 flush ();
3249 return true;
3252 /* Write the data in an ieee way. */
3254 static boolean
3255 ieee_write_data_part (abfd)
3256 bfd *abfd;
3258 asection *s;
3259 ieee_data_type *ieee = IEEE_DATA (abfd);
3260 ieee->w.r.data_part = bfd_tell (abfd);
3261 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3263 /* Skip sections that have no loadable contents (.bss,
3264 debugging, etc.) */
3265 if ((s->flags & SEC_LOAD) == 0)
3266 continue;
3268 /* Sort the reloc records so we can insert them in the correct
3269 places */
3270 if (s->reloc_count != 0)
3272 if (! do_with_relocs (abfd, s))
3273 return false;
3275 else
3277 if (! do_without_relocs (abfd, s))
3278 return false;
3282 return true;
3286 static boolean
3287 init_for_output (abfd)
3288 bfd *abfd;
3290 asection *s;
3291 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3293 if ((s->flags & SEC_DEBUGGING) != 0)
3294 continue;
3295 if (s->_raw_size != 0)
3297 ieee_per_section (s)->data = (bfd_byte *) (bfd_alloc (abfd, s->_raw_size));
3298 if (!ieee_per_section (s)->data)
3299 return false;
3302 return true;
3305 /** exec and core file sections */
3307 /* set section contents is complicated with IEEE since the format is
3308 * not a byte image, but a record stream.
3310 boolean
3311 ieee_set_section_contents (abfd, section, location, offset, count)
3312 bfd *abfd;
3313 sec_ptr section;
3314 PTR location;
3315 file_ptr offset;
3316 bfd_size_type count;
3318 if ((section->flags & SEC_DEBUGGING) != 0)
3320 if (section->contents == NULL)
3322 section->contents = ((unsigned char *)
3323 bfd_alloc (abfd, section->_raw_size));
3324 if (section->contents == NULL)
3325 return false;
3327 /* bfd_set_section_contents has already checked that everything
3328 is within range. */
3329 memcpy (section->contents + offset, location, count);
3330 return true;
3333 if (ieee_per_section (section)->data == (bfd_byte *) NULL)
3335 if (!init_for_output (abfd))
3336 return false;
3338 memcpy ((PTR) (ieee_per_section (section)->data + offset),
3339 (PTR) location,
3340 (unsigned int) count);
3341 return true;
3344 /* Write the external symbols of a file. IEEE considers two sorts of
3345 external symbols, public, and referenced. It uses to internal
3346 forms to index them as well. When we write them out we turn their
3347 symbol values into indexes from the right base. */
3349 static boolean
3350 ieee_write_external_part (abfd)
3351 bfd *abfd;
3353 asymbol **q;
3354 ieee_data_type *ieee = IEEE_DATA (abfd);
3356 unsigned int reference_index = IEEE_REFERENCE_BASE;
3357 unsigned int public_index = IEEE_PUBLIC_BASE + 2;
3358 file_ptr here = bfd_tell (abfd);
3359 boolean hadone = false;
3360 if (abfd->outsymbols != (asymbol **) NULL)
3363 for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++)
3365 asymbol *p = *q;
3366 if (bfd_is_und_section (p->section))
3368 /* This must be a symbol reference .. */
3369 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3370 || ! ieee_write_int (abfd, reference_index)
3371 || ! ieee_write_id (abfd, p->name))
3372 return false;
3373 p->value = reference_index;
3374 reference_index++;
3375 hadone = true;
3377 else if (bfd_is_com_section (p->section))
3379 /* This is a weak reference */
3380 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3381 || ! ieee_write_int (abfd, reference_index)
3382 || ! ieee_write_id (abfd, p->name)
3383 || ! ieee_write_byte (abfd,
3384 ieee_weak_external_reference_enum)
3385 || ! ieee_write_int (abfd, reference_index)
3386 || ! ieee_write_int (abfd, p->value))
3387 return false;
3388 p->value = reference_index;
3389 reference_index++;
3390 hadone = true;
3392 else if (p->flags & BSF_GLOBAL)
3394 /* This must be a symbol definition */
3396 if (! ieee_write_byte (abfd, ieee_external_symbol_enum)
3397 || ! ieee_write_int (abfd, public_index)
3398 || ! ieee_write_id (abfd, p->name)
3399 || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum)
3400 || ! ieee_write_int (abfd, public_index)
3401 || ! ieee_write_byte (abfd, 15) /* instruction address */
3402 || ! ieee_write_byte (abfd, 19) /* static symbol */
3403 || ! ieee_write_byte (abfd, 1)) /* one of them */
3404 return false;
3406 /* Write out the value */
3407 if (! ieee_write_2bytes (abfd, ieee_value_record_enum)
3408 || ! ieee_write_int (abfd, public_index))
3409 return false;
3410 if (! bfd_is_abs_section (p->section))
3412 if (abfd->flags & EXEC_P)
3414 /* If fully linked, then output all symbols
3415 relocated */
3416 if (! (ieee_write_int
3417 (abfd,
3418 (p->value
3419 + p->section->output_offset
3420 + p->section->output_section->vma))))
3421 return false;
3423 else
3425 if (! (ieee_write_expression
3426 (abfd,
3427 p->value + p->section->output_offset,
3428 p->section->output_section->symbol,
3429 false, 0)))
3430 return false;
3433 else
3435 if (! ieee_write_expression (abfd,
3436 p->value,
3437 bfd_abs_section_ptr->symbol,
3438 false, 0))
3439 return false;
3441 p->value = public_index;
3442 public_index++;
3443 hadone = true;
3445 else
3447 /* This can happen - when there are gaps in the symbols read */
3448 /* from an input ieee file */
3452 if (hadone)
3453 ieee->w.r.external_part = here;
3455 return true;
3459 static CONST unsigned char exten[] =
3461 0xf0, 0x20, 0x00,
3462 0xf1, 0xce, 0x20, 0x00, 37, 3, 3, /* Set version 3 rev 3 */
3463 0xf1, 0xce, 0x20, 0x00, 39, 2,/* keep symbol in original case */
3464 0xf1, 0xce, 0x20, 0x00, 38 /* set object type relocateable to x */
3467 static CONST unsigned char envi[] =
3469 0xf0, 0x21, 0x00,
3471 /* 0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
3472 0x19, 0x2c,
3474 0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok */
3476 0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix */
3477 /* 0xf1, 0xce, 0x21, 0, 54, 2,1,1 tool & version # */
3480 static boolean
3481 ieee_write_me_part (abfd)
3482 bfd *abfd;
3484 ieee_data_type *ieee = IEEE_DATA (abfd);
3485 ieee->w.r.trailer_part = bfd_tell (abfd);
3486 if (abfd->start_address)
3488 if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum)
3489 || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum)
3490 || ! ieee_write_int (abfd, abfd->start_address)
3491 || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum))
3492 return false;
3494 ieee->w.r.me_record = bfd_tell (abfd);
3495 if (! ieee_write_byte (abfd, ieee_module_end_enum))
3496 return false;
3497 return true;
3500 /* Write out the IEEE processor ID. */
3502 static boolean
3503 ieee_write_processor (abfd)
3504 bfd *abfd;
3506 const bfd_arch_info_type *arch;
3508 arch = bfd_get_arch_info (abfd);
3509 switch (arch->arch)
3511 default:
3512 if (! ieee_write_id (abfd, bfd_printable_name (abfd)))
3513 return false;
3514 break;
3516 case bfd_arch_a29k:
3517 if (! ieee_write_id (abfd, "29000"))
3518 return false;
3519 break;
3521 case bfd_arch_h8300:
3522 if (! ieee_write_id (abfd, "H8/300"))
3523 return false;
3524 break;
3526 case bfd_arch_h8500:
3527 if (! ieee_write_id (abfd, "H8/500"))
3528 return false;
3529 break;
3531 case bfd_arch_i960:
3532 switch (arch->mach)
3534 default:
3535 case bfd_mach_i960_core:
3536 case bfd_mach_i960_ka_sa:
3537 if (! ieee_write_id (abfd, "80960KA"))
3538 return false;
3539 break;
3541 case bfd_mach_i960_kb_sb:
3542 if (! ieee_write_id (abfd, "80960KB"))
3543 return false;
3544 break;
3546 case bfd_mach_i960_ca:
3547 if (! ieee_write_id (abfd, "80960CA"))
3548 return false;
3549 break;
3551 case bfd_mach_i960_mc:
3552 case bfd_mach_i960_xa:
3553 if (! ieee_write_id (abfd, "80960MC"))
3554 return false;
3555 break;
3557 break;
3559 case bfd_arch_m68k:
3561 const char *id;
3563 switch (arch->mach)
3565 default: id = "68020"; break;
3566 case bfd_mach_m68000: id = "68000"; break;
3567 case bfd_mach_m68008: id = "68008"; break;
3568 case bfd_mach_m68010: id = "68010"; break;
3569 case bfd_mach_m68020: id = "68020"; break;
3570 case bfd_mach_m68030: id = "68030"; break;
3571 case bfd_mach_m68040: id = "68040"; break;
3572 case bfd_mach_m68060: id = "68060"; break;
3573 case bfd_mach_cpu32: id = "cpu32"; break;
3576 if (! ieee_write_id (abfd, id))
3577 return false;
3579 break;
3582 return true;
3585 boolean
3586 ieee_write_object_contents (abfd)
3587 bfd *abfd;
3589 ieee_data_type *ieee = IEEE_DATA (abfd);
3590 unsigned int i;
3591 file_ptr old;
3593 /* Fast forward over the header area */
3594 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3595 return false;
3597 if (! ieee_write_byte (abfd, ieee_module_beginning_enum)
3598 || ! ieee_write_processor (abfd)
3599 || ! ieee_write_id (abfd, abfd->filename))
3600 return false;
3602 /* Fast forward over the variable bits */
3603 if (! ieee_write_byte (abfd, ieee_address_descriptor_enum))
3604 return false;
3606 /* Bits per MAU */
3607 if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd))))
3608 return false;
3609 /* MAU's per address */
3610 if (! ieee_write_byte (abfd,
3611 (bfd_byte) (bfd_arch_bits_per_address (abfd)
3612 / bfd_arch_bits_per_byte (abfd))))
3613 return false;
3615 old = bfd_tell (abfd);
3616 if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0)
3617 return false;
3619 ieee->w.r.extension_record = bfd_tell (abfd);
3620 if (bfd_write ((char *) exten, 1, sizeof (exten), abfd) != sizeof (exten))
3621 return false;
3622 if (abfd->flags & EXEC_P)
3624 if (! ieee_write_byte (abfd, 0x1)) /* Absolute */
3625 return false;
3627 else
3629 if (! ieee_write_byte (abfd, 0x2)) /* Relocateable */
3630 return false;
3633 ieee->w.r.environmental_record = bfd_tell (abfd);
3634 if (bfd_write ((char *) envi, 1, sizeof (envi), abfd) != sizeof (envi))
3635 return false;
3637 /* The HP emulator database requires a timestamp in the file. */
3639 time_t now;
3640 const struct tm *t;
3642 time (&now);
3643 t = (struct tm *) localtime (&now);
3644 if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum)
3645 || ! ieee_write_byte (abfd, 0x21)
3646 || ! ieee_write_byte (abfd, 0)
3647 || ! ieee_write_byte (abfd, 50)
3648 || ! ieee_write_int (abfd, t->tm_year + 1900)
3649 || ! ieee_write_int (abfd, t->tm_mon + 1)
3650 || ! ieee_write_int (abfd, t->tm_mday)
3651 || ! ieee_write_int (abfd, t->tm_hour)
3652 || ! ieee_write_int (abfd, t->tm_min)
3653 || ! ieee_write_int (abfd, t->tm_sec))
3654 return false;
3657 output_bfd = abfd;
3659 flush ();
3661 if (! ieee_write_section_part (abfd))
3662 return false;
3663 /* First write the symbols. This changes their values into table
3664 indeces so we cant use it after this point. */
3665 if (! ieee_write_external_part (abfd))
3666 return false;
3668 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3670 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3673 /* Write any debugs we have been told about. */
3674 if (! ieee_write_debug_part (abfd))
3675 return false;
3677 /* Can only write the data once the symbols have been written, since
3678 the data contains relocation information which points to the
3679 symbols. */
3680 if (! ieee_write_data_part (abfd))
3681 return false;
3683 /* At the end we put the end! */
3684 if (! ieee_write_me_part (abfd))
3685 return false;
3687 /* Generate the header */
3688 if (bfd_seek (abfd, old, SEEK_SET) != 0)
3689 return false;
3691 for (i = 0; i < N_W_VARIABLES; i++)
3693 if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum)
3694 || ! ieee_write_byte (abfd, (bfd_byte) i)
3695 || ! ieee_write_int5_out (abfd, ieee->w.offset[i]))
3696 return false;
3699 return true;
3702 /* Native-level interface to symbols. */
3704 /* We read the symbols into a buffer, which is discarded when this
3705 function exits. We read the strings into a buffer large enough to
3706 hold them all plus all the cached symbol entries. */
3708 asymbol *
3709 ieee_make_empty_symbol (abfd)
3710 bfd *abfd;
3712 ieee_symbol_type *new =
3713 (ieee_symbol_type *) bfd_zmalloc (sizeof (ieee_symbol_type));
3714 if (!new)
3715 return NULL;
3716 new->symbol.the_bfd = abfd;
3717 return &new->symbol;
3720 static bfd *
3721 ieee_openr_next_archived_file (arch, prev)
3722 bfd *arch;
3723 bfd *prev;
3725 ieee_ar_data_type *ar = IEEE_AR_DATA (arch);
3726 /* take the next one from the arch state, or reset */
3727 if (prev == (bfd *) NULL)
3729 /* Reset the index - the first two entries are bogus*/
3730 ar->element_index = 2;
3732 while (true)
3734 ieee_ar_obstack_type *p = ar->elements + ar->element_index;
3735 ar->element_index++;
3736 if (ar->element_index <= ar->element_count)
3738 if (p->file_offset != (file_ptr) 0)
3740 if (p->abfd == (bfd *) NULL)
3742 p->abfd = _bfd_create_empty_archive_element_shell (arch);
3743 p->abfd->origin = p->file_offset;
3745 return p->abfd;
3748 else
3750 bfd_set_error (bfd_error_no_more_archived_files);
3751 return (bfd *) NULL;
3757 static boolean
3758 ieee_find_nearest_line (abfd,
3759 section,
3760 symbols,
3761 offset,
3762 filename_ptr,
3763 functionname_ptr,
3764 line_ptr)
3765 bfd *abfd;
3766 asection *section;
3767 asymbol **symbols;
3768 bfd_vma offset;
3769 const char **filename_ptr;
3770 const char **functionname_ptr;
3771 unsigned int *line_ptr;
3773 return false;
3776 static int
3777 ieee_generic_stat_arch_elt (abfd, buf)
3778 bfd *abfd;
3779 struct stat *buf;
3781 ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL;
3782 ieee_data_type *ieee;
3784 if (abfd->my_archive != NULL)
3785 ar = abfd->my_archive->tdata.ieee_ar_data;
3786 if (ar == (ieee_ar_data_type *) NULL)
3788 bfd_set_error (bfd_error_invalid_operation);
3789 return -1;
3792 if (IEEE_DATA (abfd) == NULL)
3794 if (ieee_object_p (abfd) == NULL)
3796 bfd_set_error (bfd_error_wrong_format);
3797 return -1;
3801 ieee = IEEE_DATA (abfd);
3803 buf->st_size = ieee->w.r.me_record + 1;
3804 buf->st_mode = 0644;
3805 return 0;
3808 static int
3809 ieee_sizeof_headers (abfd, x)
3810 bfd *abfd;
3811 boolean x;
3813 return 0;
3817 /* The debug info routines are never used. */
3818 #if 0
3820 static void
3821 ieee_bfd_debug_info_start (abfd)
3822 bfd *abfd;
3827 static void
3828 ieee_bfd_debug_info_end (abfd)
3829 bfd *abfd;
3835 /* Add this section to the list of sections we have debug info for, to
3836 be ready to output it at close time
3838 static void
3839 ieee_bfd_debug_info_accumulate (abfd, section)
3840 bfd *abfd;
3841 asection *section;
3843 ieee_data_type *ieee = IEEE_DATA (section->owner);
3844 ieee_data_type *output_ieee = IEEE_DATA (abfd);
3845 /* can only accumulate data from other ieee bfds */
3846 if (section->owner->xvec != abfd->xvec)
3847 return;
3848 /* Only bother once per bfd */
3849 if (ieee->done_debug == true)
3850 return;
3851 ieee->done_debug = true;
3853 /* Don't bother if there is no debug info */
3854 if (ieee->w.r.debug_information_part == 0)
3855 return;
3858 /* Add to chain */
3860 bfd_chain_type *n = (bfd_chain_type *) bfd_alloc (abfd, sizeof (bfd_chain_type));
3861 if (!n)
3862 abort (); /* FIXME */
3863 n->this = section->owner;
3864 n->next = (bfd_chain_type *) NULL;
3866 if (output_ieee->chain_head)
3868 output_ieee->chain_head->next = n;
3870 else
3872 output_ieee->chain_root = n;
3875 output_ieee->chain_head = n;
3879 #endif
3881 #define ieee_close_and_cleanup _bfd_generic_close_and_cleanup
3882 #define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3884 #define ieee_slurp_armap bfd_true
3885 #define ieee_slurp_extended_name_table bfd_true
3886 #define ieee_construct_extended_name_table \
3887 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
3888 bfd_true)
3889 #define ieee_truncate_arname bfd_dont_truncate_arname
3890 #define ieee_write_armap \
3891 ((boolean (*) \
3892 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
3893 bfd_true)
3894 #define ieee_read_ar_hdr bfd_nullvoidptr
3895 #define ieee_update_armap_timestamp bfd_true
3896 #define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
3898 #define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
3899 #define ieee_get_lineno _bfd_nosymbols_get_lineno
3900 #define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3901 #define ieee_read_minisymbols _bfd_generic_read_minisymbols
3902 #define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3904 #define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3906 #define ieee_set_arch_mach _bfd_generic_set_arch_mach
3908 #define ieee_get_section_contents_in_window \
3909 _bfd_generic_get_section_contents_in_window
3910 #define ieee_bfd_get_relocated_section_contents \
3911 bfd_generic_get_relocated_section_contents
3912 #define ieee_bfd_relax_section bfd_generic_relax_section
3913 #define ieee_bfd_gc_sections bfd_generic_gc_sections
3914 #define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3915 #define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
3916 #define ieee_bfd_final_link _bfd_generic_final_link
3917 #define ieee_bfd_link_split_section _bfd_generic_link_split_section
3919 /*SUPPRESS 460 */
3920 const bfd_target ieee_vec =
3922 "ieee", /* name */
3923 bfd_target_ieee_flavour,
3924 BFD_ENDIAN_UNKNOWN, /* target byte order */
3925 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
3926 (HAS_RELOC | EXEC_P | /* object flags */
3927 HAS_LINENO | HAS_DEBUG |
3928 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3929 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3930 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3931 '_', /* leading underscore */
3932 ' ', /* ar_pad_char */
3933 16, /* ar_max_namelen */
3934 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3935 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3936 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3937 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3938 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3939 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3941 {_bfd_dummy_target,
3942 ieee_object_p, /* bfd_check_format */
3943 ieee_archive_p,
3944 _bfd_dummy_target,
3947 bfd_false,
3948 ieee_mkobject,
3949 _bfd_generic_mkarchive,
3950 bfd_false
3953 bfd_false,
3954 ieee_write_object_contents,
3955 _bfd_write_archive_contents,
3956 bfd_false,
3959 BFD_JUMP_TABLE_GENERIC (ieee),
3960 BFD_JUMP_TABLE_COPY (_bfd_generic),
3961 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3962 BFD_JUMP_TABLE_ARCHIVE (ieee),
3963 BFD_JUMP_TABLE_SYMBOLS (ieee),
3964 BFD_JUMP_TABLE_RELOCS (ieee),
3965 BFD_JUMP_TABLE_WRITE (ieee),
3966 BFD_JUMP_TABLE_LINK (ieee),
3967 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3969 (PTR) 0