2001-01-01 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / ieee.c
blob70f0d130d446924e7d6c08e83891929abccad700
1 /* BFD back-end for ieee-695 objects.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
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 ATTRIBUTE_UNUSED;
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 =
1013 &dummy_bfd,
1014 " ieee empty",
1015 (symvalue) 0,
1016 BSF_DEBUGGING,
1017 bfd_abs_section_ptr
1018 #ifdef __STDC__
1019 /* K&R compilers can't initialise unions. */
1020 , { 0 }
1021 #endif
1024 if (abfd->symcount)
1026 ieee_data_type *ieee = IEEE_DATA (abfd);
1027 dummy_bfd.xvec = &ieee_vec;
1028 if (! ieee_slurp_symbol_table (abfd))
1029 return -1;
1031 if (ieee->symbol_table_full == false)
1033 /* Arrgh - there are gaps in the table, run through and fill them */
1034 /* up with pointers to a null place */
1035 unsigned int i;
1036 for (i = 0; i < abfd->symcount; i++)
1038 location[i] = &empty_symbol;
1042 ieee->external_symbol_base_offset = -ieee->external_symbol_min_index;
1043 for (symp = IEEE_DATA (abfd)->external_symbols;
1044 symp != (ieee_symbol_type *) NULL;
1045 symp = symp->next)
1047 /* Place into table at correct index locations */
1048 location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
1051 /* The external refs are indexed in a bit */
1052 ieee->external_reference_base_offset =
1053 -ieee->external_reference_min_index + ieee->external_symbol_count;
1055 for (symp = IEEE_DATA (abfd)->external_reference;
1056 symp != (ieee_symbol_type *) NULL;
1057 symp = symp->next)
1059 location[symp->index + ieee->external_reference_base_offset] =
1060 &symp->symbol;
1064 if (abfd->symcount)
1066 location[abfd->symcount] = (asymbol *) NULL;
1068 return abfd->symcount;
1071 static asection *
1072 get_section_entry (abfd, ieee, index)
1073 bfd *abfd;
1074 ieee_data_type *ieee;
1075 unsigned int index;
1077 if (index >= ieee->section_table_size)
1079 unsigned int c, i;
1080 asection **n;
1082 c = ieee->section_table_size;
1083 if (c == 0)
1084 c = 20;
1085 while (c <= index)
1086 c *= 2;
1088 n = ((asection **)
1089 bfd_realloc (ieee->section_table, c * sizeof (asection *)));
1090 if (n == NULL)
1091 return NULL;
1093 for (i = ieee->section_table_size; i < c; i++)
1094 n[i] = NULL;
1096 ieee->section_table = n;
1097 ieee->section_table_size = c;
1100 if (ieee->section_table[index] == (asection *) NULL)
1102 char *tmp = bfd_alloc (abfd, 11);
1103 asection *section;
1105 if (!tmp)
1106 return NULL;
1107 sprintf (tmp, " fsec%4d", index);
1108 section = bfd_make_section (abfd, tmp);
1109 ieee->section_table[index] = section;
1110 section->flags = SEC_NO_FLAGS;
1111 section->target_index = index;
1112 ieee->section_table[index] = section;
1114 return ieee->section_table[index];
1117 static void
1118 ieee_slurp_sections (abfd)
1119 bfd *abfd;
1121 ieee_data_type *ieee = IEEE_DATA (abfd);
1122 file_ptr offset = ieee->w.r.section_part;
1123 asection *section = (asection *) NULL;
1124 char *name;
1126 if (offset != 0)
1128 bfd_byte section_type[3];
1129 ieee_seek (abfd, offset);
1130 while (true)
1132 switch (this_byte (&(ieee->h)))
1134 case ieee_section_type_enum:
1136 unsigned int section_index;
1137 next_byte (&(ieee->h));
1138 section_index = must_parse_int (&(ieee->h));
1140 section = get_section_entry (abfd, ieee, section_index);
1142 section_type[0] = this_byte_and_next (&(ieee->h));
1144 /* Set minimal section attributes. Attributes are
1145 extended later, based on section contents. */
1147 switch (section_type[0])
1149 case 0xC1:
1150 /* Normal attributes for absolute sections */
1151 section_type[1] = this_byte (&(ieee->h));
1152 section->flags = SEC_ALLOC;
1153 switch (section_type[1])
1155 case 0xD3: /* AS Absolute section attributes */
1156 next_byte (&(ieee->h));
1157 section_type[2] = this_byte (&(ieee->h));
1158 switch (section_type[2])
1160 case 0xD0:
1161 /* Normal code */
1162 next_byte (&(ieee->h));
1163 section->flags |= SEC_CODE;
1164 break;
1165 case 0xC4:
1166 /* Normal data */
1167 next_byte (&(ieee->h));
1168 section->flags |= SEC_DATA;
1169 break;
1170 case 0xD2:
1171 next_byte (&(ieee->h));
1172 /* Normal rom data */
1173 section->flags |= SEC_ROM | SEC_DATA;
1174 break;
1175 default:
1176 break;
1179 break;
1180 case 0xC3: /* Named relocatable sections (type C) */
1181 section_type[1] = this_byte (&(ieee->h));
1182 section->flags = SEC_ALLOC;
1183 switch (section_type[1])
1185 case 0xD0: /* Normal code (CP) */
1186 next_byte (&(ieee->h));
1187 section->flags |= SEC_CODE;
1188 break;
1189 case 0xC4: /* Normal data (CD) */
1190 next_byte (&(ieee->h));
1191 section->flags |= SEC_DATA;
1192 break;
1193 case 0xD2: /* Normal rom data (CR) */
1194 next_byte (&(ieee->h));
1195 section->flags |= SEC_ROM | SEC_DATA;
1196 break;
1197 default:
1198 break;
1202 /* Read section name, use it if non empty. */
1203 name = read_id (&ieee->h);
1204 if (name[0])
1205 section->name = name;
1207 /* Skip these fields, which we don't care about */
1209 bfd_vma parent, brother, context;
1210 parse_int (&(ieee->h), &parent);
1211 parse_int (&(ieee->h), &brother);
1212 parse_int (&(ieee->h), &context);
1215 break;
1216 case ieee_section_alignment_enum:
1218 unsigned int section_index;
1219 bfd_vma value;
1220 asection *section;
1221 next_byte (&(ieee->h));
1222 section_index = must_parse_int (&ieee->h);
1223 section = get_section_entry (abfd, ieee, section_index);
1224 if (section_index > ieee->section_count)
1226 ieee->section_count = section_index;
1228 section->alignment_power =
1229 bfd_log2 (must_parse_int (&ieee->h));
1230 (void) parse_int (&(ieee->h), &value);
1232 break;
1233 case ieee_e2_first_byte_enum:
1235 ieee_record_enum_type t = (ieee_record_enum_type) (read_2bytes (&(ieee->h)));
1237 switch (t)
1239 case ieee_section_size_enum:
1240 section = ieee->section_table[must_parse_int (&(ieee->h))];
1241 section->_raw_size = must_parse_int (&(ieee->h));
1242 break;
1243 case ieee_physical_region_size_enum:
1244 section = ieee->section_table[must_parse_int (&(ieee->h))];
1245 section->_raw_size = must_parse_int (&(ieee->h));
1246 break;
1247 case ieee_region_base_address_enum:
1248 section = ieee->section_table[must_parse_int (&(ieee->h))];
1249 section->vma = must_parse_int (&(ieee->h));
1250 section->lma = section->vma;
1251 break;
1252 case ieee_mau_size_enum:
1253 must_parse_int (&(ieee->h));
1254 must_parse_int (&(ieee->h));
1255 break;
1256 case ieee_m_value_enum:
1257 must_parse_int (&(ieee->h));
1258 must_parse_int (&(ieee->h));
1259 break;
1260 case ieee_section_base_address_enum:
1261 section = ieee->section_table[must_parse_int (&(ieee->h))];
1262 section->vma = must_parse_int (&(ieee->h));
1263 section->lma = section->vma;
1264 break;
1265 case ieee_section_offset_enum:
1266 (void) must_parse_int (&(ieee->h));
1267 (void) must_parse_int (&(ieee->h));
1268 break;
1269 default:
1270 return;
1273 break;
1274 default:
1275 return;
1281 /* Make a section for the debugging information, if any. We don't try
1282 to interpret the debugging information; we just point the section
1283 at the area in the file so that program which understand can dig it
1284 out. */
1286 static boolean
1287 ieee_slurp_debug (abfd)
1288 bfd *abfd;
1290 ieee_data_type *ieee = IEEE_DATA (abfd);
1291 asection *sec;
1292 file_ptr debug_end;
1294 if (ieee->w.r.debug_information_part == 0)
1295 return true;
1297 sec = bfd_make_section (abfd, ".debug");
1298 if (sec == NULL)
1299 return false;
1300 sec->flags |= SEC_DEBUGGING | SEC_HAS_CONTENTS;
1301 sec->filepos = ieee->w.r.debug_information_part;
1303 debug_end = ieee->w.r.data_part;
1304 if (debug_end == 0)
1305 debug_end = ieee->w.r.trailer_part;
1306 if (debug_end == 0)
1307 debug_end = ieee->w.r.me_record;
1308 sec->_raw_size = debug_end - ieee->w.r.debug_information_part;
1310 return true;
1313 /***********************************************************************
1314 * archive stuff
1317 const bfd_target *
1318 ieee_archive_p (abfd)
1319 bfd *abfd;
1321 char *library;
1322 unsigned int i;
1323 unsigned char buffer[512];
1324 file_ptr buffer_offset = 0;
1325 ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
1326 ieee_ar_data_type *ieee;
1327 unsigned int alc_elts;
1328 ieee_ar_obstack_type *elts = NULL;
1330 abfd->tdata.ieee_ar_data =
1331 (ieee_ar_data_type *) bfd_alloc (abfd, sizeof (ieee_ar_data_type));
1332 if (!abfd->tdata.ieee_ar_data)
1333 goto error_return;
1334 ieee = IEEE_AR_DATA (abfd);
1336 /* FIXME: Check return value. I'm not sure whether it needs to read
1337 the entire buffer or not. */
1338 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1340 ieee->h.first_byte = buffer;
1341 ieee->h.input_p = buffer;
1343 ieee->h.abfd = abfd;
1345 if (this_byte (&(ieee->h)) != Module_Beginning)
1346 goto got_wrong_format_error;
1348 next_byte (&(ieee->h));
1349 library = read_id (&(ieee->h));
1350 if (strcmp (library, "LIBRARY") != 0)
1351 goto got_wrong_format_error;
1353 /* Throw away the filename. */
1354 read_id (&(ieee->h));
1356 ieee->element_count = 0;
1357 ieee->element_index = 0;
1359 next_byte (&(ieee->h)); /* Drop the ad part. */
1360 must_parse_int (&(ieee->h)); /* And the two dummy numbers. */
1361 must_parse_int (&(ieee->h));
1363 alc_elts = 10;
1364 elts = (ieee_ar_obstack_type *) bfd_malloc (alc_elts * sizeof *elts);
1365 if (elts == NULL)
1366 goto error_return;
1368 /* Read the index of the BB table. */
1369 while (1)
1371 int rec;
1372 ieee_ar_obstack_type *t;
1374 rec = read_2bytes (&(ieee->h));
1375 if (rec != (int) ieee_assign_value_to_variable_enum)
1376 break;
1378 if (ieee->element_count >= alc_elts)
1380 ieee_ar_obstack_type *n;
1382 alc_elts *= 2;
1383 n = ((ieee_ar_obstack_type *)
1384 bfd_realloc (elts, alc_elts * sizeof *elts));
1385 if (n == NULL)
1386 goto error_return;
1387 elts = n;
1390 t = &elts[ieee->element_count];
1391 ieee->element_count++;
1393 must_parse_int (&(ieee->h));
1394 t->file_offset = must_parse_int (&(ieee->h));
1395 t->abfd = (bfd *) NULL;
1397 /* Make sure that we don't go over the end of the buffer. */
1398 if ((size_t) ieee_pos (abfd) > sizeof (buffer) / 2)
1400 /* Past half way, reseek and reprime. */
1401 buffer_offset += ieee_pos (abfd);
1402 if (bfd_seek (abfd, buffer_offset, SEEK_SET) != 0)
1403 goto error_return;
1405 /* FIXME: Check return value. I'm not sure whether it needs
1406 to read the entire buffer or not. */
1407 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1408 ieee->h.first_byte = buffer;
1409 ieee->h.input_p = buffer;
1413 ieee->elements = ((ieee_ar_obstack_type *)
1414 bfd_alloc (abfd,
1415 ieee->element_count * sizeof *ieee->elements));
1416 if (ieee->elements == NULL)
1417 goto error_return;
1419 memcpy (ieee->elements, elts,
1420 ieee->element_count * sizeof *ieee->elements);
1421 free (elts);
1422 elts = NULL;
1424 /* Now scan the area again, and replace BB offsets with file offsets. */
1425 for (i = 2; i < ieee->element_count; i++)
1427 if (bfd_seek (abfd, ieee->elements[i].file_offset, SEEK_SET) != 0)
1428 goto error_return;
1430 /* FIXME: Check return value. I'm not sure whether it needs to
1431 read the entire buffer or not. */
1432 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1433 ieee->h.first_byte = buffer;
1434 ieee->h.input_p = buffer;
1436 next_byte (&(ieee->h)); /* Drop F8. */
1437 next_byte (&(ieee->h)); /* Drop 14. */
1438 must_parse_int (&(ieee->h)); /* Drop size of block. */
1440 if (must_parse_int (&(ieee->h)) != 0)
1441 /* This object has been deleted. */
1442 ieee->elements[i].file_offset = 0;
1443 else
1444 ieee->elements[i].file_offset = must_parse_int (&(ieee->h));
1447 /* abfd->has_armap = ;*/
1449 return abfd->xvec;
1451 got_wrong_format_error:
1452 bfd_release (abfd, ieee);
1453 abfd->tdata.ieee_ar_data = save;
1454 bfd_set_error (bfd_error_wrong_format);
1456 error_return:
1457 if (elts != NULL)
1458 free (elts);
1460 return NULL;
1463 static boolean
1464 ieee_mkobject (abfd)
1465 bfd *abfd;
1467 abfd->tdata.ieee_data = (ieee_data_type *) bfd_zalloc (abfd, sizeof (ieee_data_type));
1468 return abfd->tdata.ieee_data ? true : false;
1471 const bfd_target *
1472 ieee_object_p (abfd)
1473 bfd *abfd;
1475 char *processor;
1476 unsigned int part;
1477 ieee_data_type *ieee;
1478 unsigned char buffer[300];
1479 ieee_data_type *save = IEEE_DATA (abfd);
1481 abfd->tdata.ieee_data = 0;
1482 ieee_mkobject (abfd);
1484 ieee = IEEE_DATA (abfd);
1485 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1486 goto fail;
1487 /* Read the first few bytes in to see if it makes sense */
1488 /* FIXME: Check return value. I'm not sure whether it needs to read
1489 the entire buffer or not. */
1490 bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1492 ieee->h.input_p = buffer;
1493 if (this_byte_and_next (&(ieee->h)) != Module_Beginning)
1494 goto got_wrong_format;
1496 ieee->read_symbols = false;
1497 ieee->read_data = false;
1498 ieee->section_count = 0;
1499 ieee->external_symbol_max_index = 0;
1500 ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
1501 ieee->external_reference_min_index = IEEE_REFERENCE_BASE;
1502 ieee->external_reference_max_index = 0;
1503 ieee->h.abfd = abfd;
1504 ieee->section_table = NULL;
1505 ieee->section_table_size = 0;
1507 processor = ieee->mb.processor = read_id (&(ieee->h));
1508 if (strcmp (processor, "LIBRARY") == 0)
1509 goto got_wrong_format;
1510 ieee->mb.module_name = read_id (&(ieee->h));
1511 if (abfd->filename == (CONST char *) NULL)
1513 abfd->filename = ieee->mb.module_name;
1515 /* Determine the architecture and machine type of the object file.
1518 const bfd_arch_info_type *arch;
1519 char family[10];
1521 /* IEEE does not specify the format of the processor identificaton
1522 string, so the compiler is free to put in it whatever it wants.
1523 We try here to recognize different processors belonging to the
1524 m68k family. Code for other processors can be added here. */
1525 if ((processor[0] == '6') && (processor[1] == '8'))
1527 if (processor[2] == '3') /* 683xx integrated processors */
1529 switch (processor[3])
1531 case '0': /* 68302, 68306, 68307 */
1532 case '2': /* 68322, 68328 */
1533 case '5': /* 68356 */
1534 strcpy (family, "68000"); /* MC68000-based controllers */
1535 break;
1537 case '3': /* 68330, 68331, 68332, 68333,
1538 68334, 68335, 68336, 68338 */
1539 case '6': /* 68360 */
1540 case '7': /* 68376 */
1541 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1542 break;
1544 case '4':
1545 if (processor[4] == '9') /* 68349 */
1546 strcpy (family, "68030"); /* CPU030 */
1547 else /* 68340, 68341 */
1548 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1549 break;
1551 default: /* Does not exist yet */
1552 strcpy (family, "68332"); /* Guess it will be CPU32 */
1555 else if (toupper (processor[3]) == 'F') /* 68F333 */
1556 strcpy (family, "68332"); /* CPU32 */
1557 else if ((toupper (processor[3]) == 'C') /* Embedded controllers */
1558 && ((toupper (processor[2]) == 'E')
1559 || (toupper (processor[2]) == 'H')
1560 || (toupper (processor[2]) == 'L')))
1562 strcpy (family, "68");
1563 strncat (family, processor + 4, 7);
1564 family[9] = '\0';
1566 else /* "Regular" processors */
1568 strncpy (family, processor, 9);
1569 family[9] = '\0';
1572 else if ((strncmp (processor, "cpu32", 5) == 0) /* CPU32 and CPU32+ */
1573 || (strncmp (processor, "CPU32", 5) == 0))
1574 strcpy (family, "68332");
1575 else
1577 strncpy (family, processor, 9);
1578 family[9] = '\0';
1581 arch = bfd_scan_arch (family);
1582 if (arch == 0)
1583 goto got_wrong_format;
1584 abfd->arch_info = arch;
1587 if (this_byte (&(ieee->h)) != (int) ieee_address_descriptor_enum)
1589 goto fail;
1591 next_byte (&(ieee->h));
1593 if (parse_int (&(ieee->h), &ieee->ad.number_of_bits_mau) == false)
1595 goto fail;
1597 if (parse_int (&(ieee->h), &ieee->ad.number_of_maus_in_address) == false)
1599 goto fail;
1602 /* If there is a byte order info, take it */
1603 if (this_byte (&(ieee->h)) == (int) ieee_variable_L_enum ||
1604 this_byte (&(ieee->h)) == (int) ieee_variable_M_enum)
1605 next_byte (&(ieee->h));
1607 for (part = 0; part < N_W_VARIABLES; part++)
1609 boolean ok;
1610 if (read_2bytes (&(ieee->h)) != (int) ieee_assign_value_to_variable_enum)
1612 goto fail;
1614 if (this_byte_and_next (&(ieee->h)) != part)
1616 goto fail;
1619 ieee->w.offset[part] = parse_i (&(ieee->h), &ok);
1620 if (ok == false)
1622 goto fail;
1627 if (ieee->w.r.external_part != 0)
1628 abfd->flags = HAS_SYMS;
1630 /* By now we know that this is a real IEEE file, we're going to read
1631 the whole thing into memory so that we can run up and down it
1632 quickly. We can work out how big the file is from the trailer
1633 record */
1635 IEEE_DATA (abfd)->h.first_byte =
1636 (unsigned char *) bfd_alloc (ieee->h.abfd, ieee->w.r.me_record + 1);
1637 if (!IEEE_DATA (abfd)->h.first_byte)
1638 goto fail;
1639 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1640 goto fail;
1641 /* FIXME: Check return value. I'm not sure whether it needs to read
1642 the entire buffer or not. */
1643 bfd_read ((PTR) (IEEE_DATA (abfd)->h.first_byte), 1,
1644 ieee->w.r.me_record + 1, abfd);
1646 ieee_slurp_sections (abfd);
1648 if (! ieee_slurp_debug (abfd))
1649 goto fail;
1651 /* Parse section data to activate file and section flags implied by
1652 section contents. */
1654 if (! ieee_slurp_section_data (abfd))
1655 goto fail;
1657 return abfd->xvec;
1658 got_wrong_format:
1659 bfd_set_error (bfd_error_wrong_format);
1660 fail:
1661 (void) bfd_release (abfd, ieee);
1662 abfd->tdata.ieee_data = save;
1663 return (const bfd_target *) NULL;
1666 void
1667 ieee_get_symbol_info (ignore_abfd, symbol, ret)
1668 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1669 asymbol *symbol;
1670 symbol_info *ret;
1672 bfd_symbol_info (symbol, ret);
1673 if (symbol->name[0] == ' ')
1674 ret->name = "* empty table entry ";
1675 if (!symbol->section)
1676 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
1679 void
1680 ieee_print_symbol (ignore_abfd, afile, symbol, how)
1681 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1682 PTR afile;
1683 asymbol *symbol;
1684 bfd_print_symbol_type how;
1686 FILE *file = (FILE *) afile;
1688 switch (how)
1690 case bfd_print_symbol_name:
1691 fprintf (file, "%s", symbol->name);
1692 break;
1693 case bfd_print_symbol_more:
1694 #if 0
1695 fprintf (file, "%4x %2x", aout_symbol (symbol)->desc & 0xffff,
1696 aout_symbol (symbol)->other & 0xff);
1697 #endif
1698 BFD_FAIL ();
1699 break;
1700 case bfd_print_symbol_all:
1702 const char *section_name =
1703 (symbol->section == (asection *) NULL
1704 ? "*abs"
1705 : symbol->section->name);
1706 if (symbol->name[0] == ' ')
1708 fprintf (file, "* empty table entry ");
1710 else
1712 bfd_print_symbol_vandf ((PTR) file, symbol);
1714 fprintf (file, " %-5s %04x %02x %s",
1715 section_name,
1716 (unsigned) ieee_symbol (symbol)->index,
1717 (unsigned) 0,
1718 symbol->name);
1721 break;
1725 static boolean
1726 do_one (ieee, current_map, location_ptr, s, iterations)
1727 ieee_data_type *ieee;
1728 ieee_per_section_type *current_map;
1729 unsigned char *location_ptr;
1730 asection *s;
1731 int iterations;
1733 switch (this_byte (&(ieee->h)))
1735 case ieee_load_constant_bytes_enum:
1737 unsigned int number_of_maus;
1738 unsigned int i;
1739 next_byte (&(ieee->h));
1740 number_of_maus = must_parse_int (&(ieee->h));
1742 for (i = 0; i < number_of_maus; i++)
1744 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1745 next_byte (&(ieee->h));
1748 break;
1750 case ieee_load_with_relocation_enum:
1752 boolean loop = true;
1753 next_byte (&(ieee->h));
1754 while (loop)
1756 switch (this_byte (&(ieee->h)))
1758 case ieee_variable_R_enum:
1760 case ieee_function_signed_open_b_enum:
1761 case ieee_function_unsigned_open_b_enum:
1762 case ieee_function_either_open_b_enum:
1764 unsigned int extra = 4;
1765 boolean pcrel = false;
1766 asection *section;
1767 ieee_reloc_type *r =
1768 (ieee_reloc_type *) bfd_alloc (ieee->h.abfd,
1769 sizeof (ieee_reloc_type));
1770 if (!r)
1771 return false;
1773 *(current_map->reloc_tail_ptr) = r;
1774 current_map->reloc_tail_ptr = &r->next;
1775 r->next = (ieee_reloc_type *) NULL;
1776 next_byte (&(ieee->h));
1777 /* abort();*/
1778 r->relent.sym_ptr_ptr = 0;
1779 parse_expression (ieee,
1780 &r->relent.addend,
1781 &r->symbol,
1782 &pcrel, &extra, &section);
1783 r->relent.address = current_map->pc;
1784 s->flags |= SEC_RELOC;
1785 s->owner->flags |= HAS_RELOC;
1786 s->reloc_count++;
1787 if (r->relent.sym_ptr_ptr == NULL && section != NULL)
1788 r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1790 if (this_byte (&(ieee->h)) == (int) ieee_comma)
1792 next_byte (&(ieee->h));
1793 /* Fetch number of bytes to pad */
1794 extra = must_parse_int (&(ieee->h));
1797 switch (this_byte (&(ieee->h)))
1799 case ieee_function_signed_close_b_enum:
1800 next_byte (&(ieee->h));
1801 break;
1802 case ieee_function_unsigned_close_b_enum:
1803 next_byte (&(ieee->h));
1804 break;
1805 case ieee_function_either_close_b_enum:
1806 next_byte (&(ieee->h));
1807 break;
1808 default:
1809 break;
1811 /* Build a relocation entry for this type */
1812 /* If pc rel then stick -ve pc into instruction
1813 and take out of reloc ..
1815 I've changed this. It's all too complicated. I
1816 keep 0 in the instruction now. */
1818 switch (extra)
1820 case 0:
1821 case 4:
1823 if (pcrel == true)
1825 #if KEEPMINUSPCININST
1826 bfd_put_32 (ieee->h.abfd, -current_map->pc, location_ptr +
1827 current_map->pc);
1828 r->relent.howto = &rel32_howto;
1829 r->relent.addend -=
1830 current_map->pc;
1831 #else
1832 bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1833 current_map->pc);
1834 r->relent.howto = &rel32_howto;
1835 #endif
1837 else
1839 bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1840 current_map->pc);
1841 r->relent.howto = &abs32_howto;
1843 current_map->pc += 4;
1844 break;
1845 case 2:
1846 if (pcrel == true)
1848 #if KEEPMINUSPCININST
1849 bfd_put_16 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1850 r->relent.addend -= current_map->pc;
1851 r->relent.howto = &rel16_howto;
1852 #else
1854 bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1855 r->relent.howto = &rel16_howto;
1856 #endif
1859 else
1861 bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1862 r->relent.howto = &abs16_howto;
1864 current_map->pc += 2;
1865 break;
1866 case 1:
1867 if (pcrel == true)
1869 #if KEEPMINUSPCININST
1870 bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1871 r->relent.addend -= current_map->pc;
1872 r->relent.howto = &rel8_howto;
1873 #else
1874 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1875 r->relent.howto = &rel8_howto;
1876 #endif
1878 else
1880 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1881 r->relent.howto = &abs8_howto;
1883 current_map->pc += 1;
1884 break;
1886 default:
1887 BFD_FAIL ();
1888 return false;
1891 break;
1892 default:
1894 bfd_vma this_size;
1895 if (parse_int (&(ieee->h), &this_size) == true)
1897 unsigned int i;
1898 for (i = 0; i < this_size; i++)
1900 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1901 next_byte (&(ieee->h));
1904 else
1906 loop = false;
1911 /* Prevent more than the first load-item of an LR record
1912 from being repeated (MRI convention). */
1913 if (iterations != 1)
1914 loop = false;
1918 return true;
1921 /* Read in all the section data and relocation stuff too */
1922 static boolean
1923 ieee_slurp_section_data (abfd)
1924 bfd *abfd;
1926 bfd_byte *location_ptr = (bfd_byte *) NULL;
1927 ieee_data_type *ieee = IEEE_DATA (abfd);
1928 unsigned int section_number;
1930 ieee_per_section_type *current_map = (ieee_per_section_type *) NULL;
1931 asection *s;
1932 /* Seek to the start of the data area */
1933 if (ieee->read_data == true)
1934 return true;
1935 ieee->read_data = true;
1936 ieee_seek (abfd, ieee->w.r.data_part);
1938 /* Allocate enough space for all the section contents */
1940 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1942 ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1943 if ((s->flags & SEC_DEBUGGING) != 0)
1944 continue;
1945 per->data = (bfd_byte *) bfd_alloc (ieee->h.abfd, s->_raw_size);
1946 if (!per->data)
1947 return false;
1948 /*SUPPRESS 68*/
1949 per->reloc_tail_ptr =
1950 (ieee_reloc_type **) & (s->relocation);
1953 while (true)
1955 switch (this_byte (&(ieee->h)))
1957 /* IF we see anything strange then quit */
1958 default:
1959 return true;
1961 case ieee_set_current_section_enum:
1962 next_byte (&(ieee->h));
1963 section_number = must_parse_int (&(ieee->h));
1964 s = ieee->section_table[section_number];
1965 s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
1966 current_map = (ieee_per_section_type *) s->used_by_bfd;
1967 location_ptr = current_map->data - s->vma;
1968 /* The document I have says that Microtec's compilers reset */
1969 /* this after a sec section, even though the standard says not */
1970 /* to. SO .. */
1971 current_map->pc = s->vma;
1972 break;
1974 case ieee_e2_first_byte_enum:
1975 next_byte (&(ieee->h));
1976 switch (this_byte (&(ieee->h)))
1978 case ieee_set_current_pc_enum & 0xff:
1980 bfd_vma value;
1981 ieee_symbol_index_type symbol;
1982 unsigned int extra;
1983 boolean pcrel;
1984 next_byte (&(ieee->h));
1985 must_parse_int (&(ieee->h)); /* Thow away section #*/
1986 parse_expression (ieee, &value,
1987 &symbol,
1988 &pcrel, &extra,
1990 current_map->pc = value;
1991 BFD_ASSERT ((unsigned) (value - s->vma) <= s->_raw_size);
1993 break;
1995 case ieee_value_starting_address_enum & 0xff:
1996 next_byte (&(ieee->h));
1997 if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum)
1998 next_byte (&(ieee->h));
1999 abfd->start_address = must_parse_int (&(ieee->h));
2000 /* We've got to the end of the data now - */
2001 return true;
2002 default:
2003 BFD_FAIL ();
2004 return false;
2006 break;
2007 case ieee_repeat_data_enum:
2009 /* Repeat the following LD or LR n times - we do this by
2010 remembering the stream pointer before running it and
2011 resetting it and running it n times. We special case
2012 the repetition of a repeat_data/load_constant
2015 unsigned int iterations;
2016 unsigned char *start;
2017 next_byte (&(ieee->h));
2018 iterations = must_parse_int (&(ieee->h));
2019 start = ieee->h.input_p;
2020 if (start[0] == (int) ieee_load_constant_bytes_enum &&
2021 start[1] == 1)
2023 while (iterations != 0)
2025 location_ptr[current_map->pc++] = start[2];
2026 iterations--;
2028 next_byte (&(ieee->h));
2029 next_byte (&(ieee->h));
2030 next_byte (&(ieee->h));
2032 else
2034 while (iterations != 0)
2036 ieee->h.input_p = start;
2037 if (!do_one (ieee, current_map, location_ptr, s,
2038 iterations))
2039 return false;
2040 iterations--;
2044 break;
2045 case ieee_load_constant_bytes_enum:
2046 case ieee_load_with_relocation_enum:
2048 if (!do_one (ieee, current_map, location_ptr, s, 1))
2049 return false;
2055 boolean
2056 ieee_new_section_hook (abfd, newsect)
2057 bfd *abfd;
2058 asection *newsect;
2060 newsect->used_by_bfd = (PTR)
2061 bfd_alloc (abfd, sizeof (ieee_per_section_type));
2062 if (!newsect->used_by_bfd)
2063 return false;
2064 ieee_per_section (newsect)->data = (bfd_byte *) NULL;
2065 ieee_per_section (newsect)->section = newsect;
2066 return true;
2069 long
2070 ieee_get_reloc_upper_bound (abfd, asect)
2071 bfd *abfd;
2072 sec_ptr asect;
2074 if ((asect->flags & SEC_DEBUGGING) != 0)
2075 return 0;
2076 if (! ieee_slurp_section_data (abfd))
2077 return -1;
2078 return (asect->reloc_count + 1) * sizeof (arelent *);
2081 static boolean
2082 ieee_get_section_contents (abfd, section, location, offset, count)
2083 bfd *abfd;
2084 sec_ptr section;
2085 PTR location;
2086 file_ptr offset;
2087 bfd_size_type count;
2089 ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
2090 if ((section->flags & SEC_DEBUGGING) != 0)
2091 return _bfd_generic_get_section_contents (abfd, section, location,
2092 offset, count);
2093 ieee_slurp_section_data (abfd);
2094 (void) memcpy ((PTR) location, (PTR) (p->data + offset), (unsigned) count);
2095 return true;
2098 long
2099 ieee_canonicalize_reloc (abfd, section, relptr, symbols)
2100 bfd *abfd;
2101 sec_ptr section;
2102 arelent **relptr;
2103 asymbol **symbols;
2105 /* ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
2106 ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation);
2107 ieee_data_type *ieee = IEEE_DATA (abfd);
2109 if ((section->flags & SEC_DEBUGGING) != 0)
2110 return 0;
2112 while (src != (ieee_reloc_type *) NULL)
2114 /* Work out which symbol to attach it this reloc to */
2115 switch (src->symbol.letter)
2117 case 'I':
2118 src->relent.sym_ptr_ptr =
2119 symbols + src->symbol.index + ieee->external_symbol_base_offset;
2120 break;
2121 case 'X':
2122 src->relent.sym_ptr_ptr =
2123 symbols + src->symbol.index + ieee->external_reference_base_offset;
2124 break;
2125 case 0:
2126 if (src->relent.sym_ptr_ptr != NULL)
2127 src->relent.sym_ptr_ptr =
2128 src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr;
2129 break;
2130 default:
2132 BFD_FAIL ();
2134 *relptr++ = &src->relent;
2135 src = src->next;
2137 *relptr = (arelent *) NULL;
2138 return section->reloc_count;
2141 static int
2142 comp (ap, bp)
2143 CONST PTR ap;
2144 CONST PTR bp;
2146 arelent *a = *((arelent **) ap);
2147 arelent *b = *((arelent **) bp);
2148 return a->address - b->address;
2151 /* Write the section headers. */
2153 static boolean
2154 ieee_write_section_part (abfd)
2155 bfd *abfd;
2157 ieee_data_type *ieee = IEEE_DATA (abfd);
2158 asection *s;
2159 ieee->w.r.section_part = bfd_tell (abfd);
2160 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2162 if (! bfd_is_abs_section (s)
2163 && (s->flags & SEC_DEBUGGING) == 0)
2165 if (! ieee_write_byte (abfd, ieee_section_type_enum)
2166 || ! ieee_write_byte (abfd,
2167 (bfd_byte) (s->index
2168 + IEEE_SECTION_NUMBER_BASE)))
2169 return false;
2171 if (abfd->flags & EXEC_P)
2173 /* This image is executable, so output absolute sections */
2174 if (! ieee_write_byte (abfd, ieee_variable_A_enum)
2175 || ! ieee_write_byte (abfd, ieee_variable_S_enum))
2176 return false;
2178 else
2180 if (! ieee_write_byte (abfd, ieee_variable_C_enum))
2181 return false;
2184 switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM))
2186 case SEC_CODE | SEC_LOAD:
2187 case SEC_CODE:
2188 if (! ieee_write_byte (abfd, ieee_variable_P_enum))
2189 return false;
2190 break;
2191 case SEC_DATA:
2192 default:
2193 if (! ieee_write_byte (abfd, ieee_variable_D_enum))
2194 return false;
2195 break;
2196 case SEC_ROM:
2197 case SEC_ROM | SEC_DATA:
2198 case SEC_ROM | SEC_LOAD:
2199 case SEC_ROM | SEC_DATA | SEC_LOAD:
2200 if (! ieee_write_byte (abfd, ieee_variable_R_enum))
2201 return false;
2205 if (! ieee_write_id (abfd, s->name))
2206 return false;
2207 #if 0
2208 ieee_write_int (abfd, 0); /* Parent */
2209 ieee_write_int (abfd, 0); /* Brother */
2210 ieee_write_int (abfd, 0); /* Context */
2211 #endif
2212 /* Alignment */
2213 if (! ieee_write_byte (abfd, ieee_section_alignment_enum)
2214 || ! ieee_write_byte (abfd,
2215 (bfd_byte) (s->index
2216 + IEEE_SECTION_NUMBER_BASE))
2217 || ! ieee_write_int (abfd, 1 << s->alignment_power))
2218 return false;
2220 /* Size */
2221 if (! ieee_write_2bytes (abfd, ieee_section_size_enum)
2222 || ! ieee_write_byte (abfd,
2223 (bfd_byte) (s->index
2224 + IEEE_SECTION_NUMBER_BASE))
2225 || ! ieee_write_int (abfd, s->_raw_size))
2226 return false;
2227 if (abfd->flags & EXEC_P)
2229 /* Relocateable sections don't have asl records */
2230 /* Vma */
2231 if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum)
2232 || ! ieee_write_byte (abfd,
2233 ((bfd_byte)
2234 (s->index
2235 + IEEE_SECTION_NUMBER_BASE)))
2236 || ! ieee_write_int (abfd, s->lma))
2237 return false;
2242 return true;
2246 static boolean
2247 do_with_relocs (abfd, s)
2248 bfd *abfd;
2249 asection *s;
2251 unsigned int number_of_maus_in_address =
2252 bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd);
2253 unsigned int relocs_to_go = s->reloc_count;
2254 bfd_byte *stream = ieee_per_section (s)->data;
2255 arelent **p = s->orelocation;
2256 bfd_size_type current_byte_index = 0;
2258 qsort (s->orelocation,
2259 relocs_to_go,
2260 sizeof (arelent **),
2261 comp);
2263 /* Output the section preheader */
2264 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2265 || ! ieee_write_byte (abfd,
2266 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))
2267 || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum)
2268 || ! ieee_write_byte (abfd,
2269 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)))
2270 return false;
2271 if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0)
2273 if (! ieee_write_int (abfd, s->lma))
2274 return false;
2276 else
2278 if (! ieee_write_expression (abfd, 0, s->symbol, 0, 0))
2279 return false;
2282 if (relocs_to_go == 0)
2284 /* If there aren't any relocations then output the load constant
2285 byte opcode rather than the load with relocation opcode */
2287 while (current_byte_index < s->_raw_size)
2289 bfd_size_type run;
2290 unsigned int MAXRUN = 127;
2291 run = MAXRUN;
2292 if (run > s->_raw_size - current_byte_index)
2294 run = s->_raw_size - current_byte_index;
2297 if (run != 0)
2299 if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum))
2300 return false;
2301 /* Output a stream of bytes */
2302 if (! ieee_write_int (abfd, run))
2303 return false;
2304 if (bfd_write ((PTR) (stream + current_byte_index),
2306 run,
2307 abfd)
2308 != run)
2309 return false;
2310 current_byte_index += run;
2314 else
2316 if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum))
2317 return false;
2319 /* Output the data stream as the longest sequence of bytes
2320 possible, allowing for the a reasonable packet size and
2321 relocation stuffs. */
2323 if ((PTR) stream == (PTR) NULL)
2325 /* Outputting a section without data, fill it up */
2326 stream = (unsigned char *) (bfd_alloc (abfd, s->_raw_size));
2327 if (!stream)
2328 return false;
2329 memset ((PTR) stream, 0, (size_t) s->_raw_size);
2331 while (current_byte_index < s->_raw_size)
2333 bfd_size_type run;
2334 unsigned int MAXRUN = 127;
2335 if (relocs_to_go)
2337 run = (*p)->address - current_byte_index;
2338 if (run > MAXRUN)
2339 run = MAXRUN;
2341 else
2343 run = MAXRUN;
2345 if (run > s->_raw_size - current_byte_index)
2347 run = s->_raw_size - current_byte_index;
2350 if (run != 0)
2352 /* Output a stream of bytes */
2353 if (! ieee_write_int (abfd, run))
2354 return false;
2355 if (bfd_write ((PTR) (stream + current_byte_index),
2357 run,
2358 abfd)
2359 != run)
2360 return false;
2361 current_byte_index += run;
2363 /* Output any relocations here */
2364 if (relocs_to_go && (*p) && (*p)->address == current_byte_index)
2366 while (relocs_to_go
2367 && (*p) && (*p)->address == current_byte_index)
2369 arelent *r = *p;
2370 bfd_signed_vma ov;
2372 #if 0
2373 if (r->howto->pc_relative)
2375 r->addend += current_byte_index;
2377 #endif
2379 switch (r->howto->size)
2381 case 2:
2383 ov = bfd_get_signed_32 (abfd,
2384 stream + current_byte_index);
2385 current_byte_index += 4;
2386 break;
2387 case 1:
2388 ov = bfd_get_signed_16 (abfd,
2389 stream + current_byte_index);
2390 current_byte_index += 2;
2391 break;
2392 case 0:
2393 ov = bfd_get_signed_8 (abfd,
2394 stream + current_byte_index);
2395 current_byte_index++;
2396 break;
2397 default:
2398 ov = 0;
2399 BFD_FAIL ();
2400 return false;
2403 ov &= r->howto->src_mask;
2405 if (r->howto->pc_relative
2406 && ! r->howto->pcrel_offset)
2407 ov += r->address;
2409 if (! ieee_write_byte (abfd,
2410 ieee_function_either_open_b_enum))
2411 return false;
2413 /* abort();*/
2415 if (r->sym_ptr_ptr != (asymbol **) NULL)
2417 if (! ieee_write_expression (abfd, r->addend + ov,
2418 *(r->sym_ptr_ptr),
2419 r->howto->pc_relative,
2420 s->index))
2421 return false;
2423 else
2425 if (! ieee_write_expression (abfd, r->addend + ov,
2426 (asymbol *) NULL,
2427 r->howto->pc_relative,
2428 s->index))
2429 return false;
2432 if (number_of_maus_in_address
2433 != bfd_get_reloc_size (r->howto))
2435 if (! ieee_write_int (abfd,
2436 bfd_get_reloc_size (r->howto)))
2437 return false;
2439 if (! ieee_write_byte (abfd,
2440 ieee_function_either_close_b_enum))
2441 return false;
2443 relocs_to_go--;
2444 p++;
2451 return true;
2454 /* If there are no relocations in the output section then we can be
2455 clever about how we write. We block items up into a max of 127
2456 bytes. */
2458 static boolean
2459 do_as_repeat (abfd, s)
2460 bfd *abfd;
2461 asection *s;
2463 if (s->_raw_size)
2465 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2466 || ! ieee_write_byte (abfd,
2467 (bfd_byte) (s->index
2468 + IEEE_SECTION_NUMBER_BASE))
2469 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8)
2470 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff)
2471 || ! ieee_write_byte (abfd,
2472 (bfd_byte) (s->index
2473 + IEEE_SECTION_NUMBER_BASE))
2474 || ! ieee_write_int (abfd, s->lma)
2475 || ! ieee_write_byte (abfd, ieee_repeat_data_enum)
2476 || ! ieee_write_int (abfd, s->_raw_size)
2477 || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)
2478 || ! ieee_write_byte (abfd, 1)
2479 || ! ieee_write_byte (abfd, 0))
2480 return false;
2483 return true;
2486 static boolean
2487 do_without_relocs (abfd, s)
2488 bfd *abfd;
2489 asection *s;
2491 bfd_byte *stream = ieee_per_section (s)->data;
2493 if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
2495 if (! do_as_repeat (abfd, s))
2496 return false;
2498 else
2500 unsigned int i;
2501 for (i = 0; i < s->_raw_size; i++)
2503 if (stream[i] != 0)
2505 if (! do_with_relocs (abfd, s))
2506 return false;
2507 return true;
2510 if (! do_as_repeat (abfd, s))
2511 return false;
2514 return true;
2518 static unsigned char *output_ptr_start;
2519 static unsigned char *output_ptr;
2520 static unsigned char *output_ptr_end;
2521 static unsigned char *input_ptr_start;
2522 static unsigned char *input_ptr;
2523 static unsigned char *input_ptr_end;
2524 static bfd *input_bfd;
2525 static bfd *output_bfd;
2526 static int output_buffer;
2528 static void
2529 fill ()
2531 /* FIXME: Check return value. I'm not sure whether it needs to read
2532 the entire buffer or not. */
2533 bfd_read ((PTR) input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
2534 input_ptr = input_ptr_start;
2536 static void
2537 flush ()
2539 if (bfd_write ((PTR) (output_ptr_start), 1, output_ptr - output_ptr_start,
2540 output_bfd)
2541 != (bfd_size_type) (output_ptr - output_ptr_start))
2542 abort ();
2543 output_ptr = output_ptr_start;
2544 output_buffer++;
2547 #define THIS() ( *input_ptr )
2548 #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
2549 #define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end) flush(); }
2551 static void
2552 write_int (value)
2553 int value;
2555 if (value >= 0 && value <= 127)
2557 OUT (value);
2559 else
2561 unsigned int length;
2562 /* How many significant bytes ? */
2563 /* FIXME FOR LONGER INTS */
2564 if (value & 0xff000000)
2566 length = 4;
2568 else if (value & 0x00ff0000)
2570 length = 3;
2572 else if (value & 0x0000ff00)
2574 length = 2;
2576 else
2577 length = 1;
2579 OUT ((int) ieee_number_repeat_start_enum + length);
2580 switch (length)
2582 case 4:
2583 OUT (value >> 24);
2584 case 3:
2585 OUT (value >> 16);
2586 case 2:
2587 OUT (value >> 8);
2588 case 1:
2589 OUT (value);
2595 static void
2596 copy_id ()
2598 int length = THIS ();
2599 char ch;
2600 OUT (length);
2601 NEXT ();
2602 while (length--)
2604 ch = THIS ();
2605 OUT (ch);
2606 NEXT ();
2610 #define VAR(x) ((x | 0x80))
2611 static void
2612 copy_expression ()
2614 int stack[10];
2615 int *tos = stack;
2616 int value = 0;
2617 while (1)
2619 switch (THIS ())
2621 case 0x84:
2622 NEXT ();
2623 value = THIS ();
2624 NEXT ();
2625 value = (value << 8) | THIS ();
2626 NEXT ();
2627 value = (value << 8) | THIS ();
2628 NEXT ();
2629 value = (value << 8) | THIS ();
2630 NEXT ();
2631 *tos++ = value;
2632 break;
2633 case 0x83:
2634 NEXT ();
2635 value = THIS ();
2636 NEXT ();
2637 value = (value << 8) | THIS ();
2638 NEXT ();
2639 value = (value << 8) | THIS ();
2640 NEXT ();
2641 *tos++ = value;
2642 break;
2643 case 0x82:
2644 NEXT ();
2645 value = THIS ();
2646 NEXT ();
2647 value = (value << 8) | THIS ();
2648 NEXT ();
2649 *tos++ = value;
2650 break;
2651 case 0x81:
2652 NEXT ();
2653 value = THIS ();
2654 NEXT ();
2655 *tos++ = value;
2656 break;
2657 case 0x80:
2658 NEXT ();
2659 *tos++ = 0;
2660 break;
2661 default:
2662 if (THIS () > 0x84)
2664 /* Not a number, just bug out with the answer */
2665 write_int (*(--tos));
2666 return;
2668 *tos++ = THIS ();
2669 NEXT ();
2670 value = 0;
2671 break;
2672 case 0xa5:
2673 /* PLUS anything */
2675 int value = *(--tos);
2676 value += *(--tos);
2677 *tos++ = value;
2678 NEXT ();
2680 break;
2681 case VAR ('R'):
2683 int section_number;
2684 ieee_data_type *ieee;
2685 asection *s;
2686 NEXT ();
2687 section_number = THIS ();
2689 NEXT ();
2690 ieee = IEEE_DATA (input_bfd);
2691 s = ieee->section_table[section_number];
2692 if (s->output_section)
2694 value = s->output_section->lma;
2696 else
2698 value = 0;
2700 value += s->output_offset;
2701 *tos++ = value;
2702 value = 0;
2704 break;
2705 case 0x90:
2707 NEXT ();
2708 write_int (*(--tos));
2709 OUT (0x90);
2710 return;
2718 /* Drop the int in the buffer, and copy a null into the gap, which we
2719 will overwrite later */
2721 struct output_buffer_struct
2723 unsigned char *ptrp;
2724 int buffer;
2727 static void
2728 fill_int (buf)
2729 struct output_buffer_struct *buf;
2731 if (buf->buffer == output_buffer)
2733 /* Still a chance to output the size */
2734 int value = output_ptr - buf->ptrp + 3;
2735 buf->ptrp[0] = value >> 24;
2736 buf->ptrp[1] = value >> 16;
2737 buf->ptrp[2] = value >> 8;
2738 buf->ptrp[3] = value >> 0;
2742 static void
2743 drop_int (buf)
2744 struct output_buffer_struct *buf;
2746 int type = THIS ();
2747 int ch;
2748 if (type <= 0x84)
2750 NEXT ();
2751 switch (type)
2753 case 0x84:
2754 ch = THIS ();
2755 NEXT ();
2756 case 0x83:
2757 ch = THIS ();
2758 NEXT ();
2759 case 0x82:
2760 ch = THIS ();
2761 NEXT ();
2762 case 0x81:
2763 ch = THIS ();
2764 NEXT ();
2765 case 0x80:
2766 break;
2769 OUT (0x84);
2770 buf->ptrp = output_ptr;
2771 buf->buffer = output_buffer;
2772 OUT (0);
2773 OUT (0);
2774 OUT (0);
2775 OUT (0);
2778 static void
2779 copy_int ()
2781 int type = THIS ();
2782 int ch;
2783 if (type <= 0x84)
2785 OUT (type);
2786 NEXT ();
2787 switch (type)
2789 case 0x84:
2790 ch = THIS ();
2791 NEXT ();
2792 OUT (ch);
2793 case 0x83:
2794 ch = THIS ();
2795 NEXT ();
2796 OUT (ch);
2797 case 0x82:
2798 ch = THIS ();
2799 NEXT ();
2800 OUT (ch);
2801 case 0x81:
2802 ch = THIS ();
2803 NEXT ();
2804 OUT (ch);
2805 case 0x80:
2806 break;
2811 #define ID copy_id()
2812 #define INT copy_int()
2813 #define EXP copy_expression()
2814 static void copy_till_end ();
2815 #define INTn(q) copy_int()
2816 #define EXPn(q) copy_expression()
2818 static void
2819 f1_record ()
2821 int ch;
2822 /* ATN record */
2823 NEXT ();
2824 ch = THIS ();
2825 switch (ch)
2827 default:
2828 OUT (0xf1);
2829 OUT (ch);
2830 break;
2831 case 0xc9:
2832 NEXT ();
2833 OUT (0xf1);
2834 OUT (0xc9);
2835 INT;
2836 INT;
2837 ch = THIS ();
2838 switch (ch)
2840 case 0x16:
2841 NEXT ();
2842 break;
2843 case 0x01:
2844 NEXT ();
2845 break;
2846 case 0x00:
2847 NEXT ();
2848 INT;
2849 break;
2850 case 0x03:
2851 NEXT ();
2852 INT;
2853 break;
2854 case 0x13:
2855 EXPn (instruction address);
2856 break;
2857 default:
2858 break;
2860 break;
2861 case 0xd8:
2862 /* EXternal ref */
2863 NEXT ();
2864 OUT (0xf1);
2865 OUT (0xd8);
2866 EXP;
2867 EXP;
2868 EXP;
2869 EXP;
2870 break;
2871 case 0xce:
2872 NEXT ();
2873 OUT (0xf1);
2874 OUT (0xce);
2875 INT;
2876 INT;
2877 ch = THIS ();
2878 INT;
2879 switch (ch)
2881 case 0x01:
2882 INT;
2883 INT;
2884 break;
2885 case 0x02:
2886 INT;
2887 break;
2888 case 0x04:
2889 EXPn (external function);
2890 break;
2891 case 0x05:
2892 break;
2893 case 0x07:
2894 INTn (line number);
2895 INT;
2896 case 0x08:
2897 break;
2898 case 0x0a:
2899 INTn (locked register);
2900 INT;
2901 break;
2902 case 0x3f:
2903 copy_till_end ();
2904 break;
2905 case 0x3e:
2906 copy_till_end ();
2907 break;
2908 case 0x40:
2909 copy_till_end ();
2910 break;
2911 case 0x41:
2913 break;
2919 static void
2920 f0_record ()
2922 /* Attribute record */
2923 NEXT ();
2924 OUT (0xf0);
2925 INTn (Symbol name);
2929 static void
2930 copy_till_end ()
2932 int ch = THIS ();
2933 while (1)
2935 while (ch <= 0x80)
2937 OUT (ch);
2938 NEXT ();
2939 ch = THIS ();
2941 switch (ch)
2943 case 0x84:
2944 OUT (THIS ());
2945 NEXT ();
2946 case 0x83:
2947 OUT (THIS ());
2948 NEXT ();
2949 case 0x82:
2950 OUT (THIS ());
2951 NEXT ();
2952 case 0x81:
2953 OUT (THIS ());
2954 NEXT ();
2955 OUT (THIS ());
2956 NEXT ();
2958 ch = THIS ();
2959 break;
2960 default:
2961 return;
2967 static void
2968 f2_record ()
2970 NEXT ();
2971 OUT (0xf2);
2972 INT;
2973 NEXT ();
2974 OUT (0xce);
2975 INT;
2976 copy_till_end ();
2980 static void block ();
2981 static void
2982 f8_record ()
2984 int ch;
2985 NEXT ();
2986 ch = THIS ();
2987 switch (ch)
2989 case 0x01:
2990 case 0x02:
2991 case 0x03:
2992 /* Unique typedefs for module */
2993 /* GLobal typedefs */
2994 /* High level module scope beginning */
2996 struct output_buffer_struct ob;
2997 NEXT ();
2998 OUT (0xf8);
2999 OUT (ch);
3000 drop_int (&ob);
3003 block ();
3005 NEXT ();
3006 fill_int (&ob);
3007 OUT (0xf9);
3009 break;
3010 case 0x04:
3011 /* Global function */
3013 struct output_buffer_struct ob;
3014 NEXT ();
3015 OUT (0xf8);
3016 OUT (0x04);
3017 drop_int (&ob);
3019 INTn (stack size);
3020 INTn (ret val);
3021 EXPn (offset);
3023 block ();
3025 NEXT ();
3026 OUT (0xf9);
3027 EXPn (size of block);
3028 fill_int (&ob);
3030 break;
3032 case 0x05:
3033 /* File name for source line numbers */
3035 struct output_buffer_struct ob;
3036 NEXT ();
3037 OUT (0xf8);
3038 OUT (0x05);
3039 drop_int (&ob);
3041 INTn (year);
3042 INTn (month);
3043 INTn (day);
3044 INTn (hour);
3045 INTn (monute);
3046 INTn (second);
3047 block ();
3048 NEXT ();
3049 OUT (0xf9);
3050 fill_int (&ob);
3052 break;
3054 case 0x06:
3055 /* Local function */
3057 struct output_buffer_struct ob;
3058 NEXT ();
3059 OUT (0xf8);
3060 OUT (0x06);
3061 drop_int (&ob);
3063 INTn (stack size);
3064 INTn (type return);
3065 EXPn (offset);
3066 block ();
3067 NEXT ();
3068 OUT (0xf9);
3069 EXPn (size);
3070 fill_int (&ob);
3072 break;
3074 case 0x0a:
3075 /* Assembler module scope beginning -*/
3077 struct output_buffer_struct ob;
3079 NEXT ();
3080 OUT (0xf8);
3081 OUT (0x0a);
3082 drop_int (&ob);
3085 INT;
3087 INT;
3088 INT;
3089 INT;
3090 INT;
3091 INT;
3092 INT;
3094 block ();
3096 NEXT ();
3097 OUT (0xf9);
3098 fill_int (&ob);
3100 break;
3101 case 0x0b:
3103 struct output_buffer_struct ob;
3104 NEXT ();
3105 OUT (0xf8);
3106 OUT (0x0b);
3107 drop_int (&ob);
3109 INT;
3110 INTn (section index);
3111 EXPn (offset);
3112 INTn (stuff);
3114 block ();
3116 OUT (0xf9);
3117 NEXT ();
3118 EXPn (Size in Maus);
3119 fill_int (&ob);
3121 break;
3125 static void
3126 e2_record ()
3128 OUT (0xe2);
3129 NEXT ();
3130 OUT (0xce);
3131 NEXT ();
3132 INT;
3133 EXP;
3136 static void
3137 block ()
3139 int ch;
3140 while (1)
3142 ch = THIS ();
3143 switch (ch)
3145 case 0xe1:
3146 case 0xe5:
3147 return;
3148 case 0xf9:
3149 return;
3150 case 0xf0:
3151 f0_record ();
3152 break;
3153 case 0xf1:
3154 f1_record ();
3155 break;
3156 case 0xf2:
3157 f2_record ();
3158 break;
3159 case 0xf8:
3160 f8_record ();
3161 break;
3162 case 0xe2:
3163 e2_record ();
3164 break;
3172 /* relocate_debug,
3173 moves all the debug information from the source bfd to the output
3174 bfd, and relocates any expressions it finds
3177 static void
3178 relocate_debug (output, input)
3179 bfd *output ATTRIBUTE_UNUSED;
3180 bfd *input;
3182 #define IBS 400
3183 #define OBS 400
3184 unsigned char input_buffer[IBS];
3186 input_ptr_start = input_ptr = input_buffer;
3187 input_ptr_end = input_buffer + IBS;
3188 input_bfd = input;
3189 /* FIXME: Check return value. I'm not sure whether it needs to read
3190 the entire buffer or not. */
3191 bfd_read ((PTR) input_ptr_start, 1, IBS, input);
3192 block ();
3195 /* Gather together all the debug information from each input BFD into
3196 one place, relocating it and emitting it as we go. */
3198 static boolean
3199 ieee_write_debug_part (abfd)
3200 bfd *abfd;
3202 ieee_data_type *ieee = IEEE_DATA (abfd);
3203 bfd_chain_type *chain = ieee->chain_root;
3204 unsigned char output_buffer[OBS];
3205 boolean some_debug = false;
3206 file_ptr here = bfd_tell (abfd);
3208 output_ptr_start = output_ptr = output_buffer;
3209 output_ptr_end = output_buffer + OBS;
3210 output_ptr = output_buffer;
3211 output_bfd = abfd;
3213 if (chain == (bfd_chain_type *) NULL)
3215 asection *s;
3217 for (s = abfd->sections; s != NULL; s = s->next)
3218 if ((s->flags & SEC_DEBUGGING) != 0)
3219 break;
3220 if (s == NULL)
3222 ieee->w.r.debug_information_part = 0;
3223 return true;
3226 ieee->w.r.debug_information_part = here;
3227 if (bfd_write (s->contents, 1, s->_raw_size, abfd) != s->_raw_size)
3228 return false;
3230 else
3232 while (chain != (bfd_chain_type *) NULL)
3234 bfd *entry = chain->this;
3235 ieee_data_type *entry_ieee = IEEE_DATA (entry);
3236 if (entry_ieee->w.r.debug_information_part)
3238 if (bfd_seek (entry, entry_ieee->w.r.debug_information_part,
3239 SEEK_SET)
3240 != 0)
3241 return false;
3242 relocate_debug (abfd, entry);
3245 chain = chain->next;
3247 if (some_debug)
3249 ieee->w.r.debug_information_part = here;
3251 else
3253 ieee->w.r.debug_information_part = 0;
3256 flush ();
3259 return true;
3262 /* Write the data in an ieee way. */
3264 static boolean
3265 ieee_write_data_part (abfd)
3266 bfd *abfd;
3268 asection *s;
3269 ieee_data_type *ieee = IEEE_DATA (abfd);
3270 ieee->w.r.data_part = bfd_tell (abfd);
3271 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3273 /* Skip sections that have no loadable contents (.bss,
3274 debugging, etc.) */
3275 if ((s->flags & SEC_LOAD) == 0)
3276 continue;
3278 /* Sort the reloc records so we can insert them in the correct
3279 places */
3280 if (s->reloc_count != 0)
3282 if (! do_with_relocs (abfd, s))
3283 return false;
3285 else
3287 if (! do_without_relocs (abfd, s))
3288 return false;
3292 return true;
3296 static boolean
3297 init_for_output (abfd)
3298 bfd *abfd;
3300 asection *s;
3301 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3303 if ((s->flags & SEC_DEBUGGING) != 0)
3304 continue;
3305 if (s->_raw_size != 0)
3307 ieee_per_section (s)->data = (bfd_byte *) (bfd_alloc (abfd, s->_raw_size));
3308 if (!ieee_per_section (s)->data)
3309 return false;
3312 return true;
3315 /** exec and core file sections */
3317 /* set section contents is complicated with IEEE since the format is
3318 * not a byte image, but a record stream.
3320 boolean
3321 ieee_set_section_contents (abfd, section, location, offset, count)
3322 bfd *abfd;
3323 sec_ptr section;
3324 PTR location;
3325 file_ptr offset;
3326 bfd_size_type count;
3328 if ((section->flags & SEC_DEBUGGING) != 0)
3330 if (section->contents == NULL)
3332 section->contents = ((unsigned char *)
3333 bfd_alloc (abfd, section->_raw_size));
3334 if (section->contents == NULL)
3335 return false;
3337 /* bfd_set_section_contents has already checked that everything
3338 is within range. */
3339 memcpy (section->contents + offset, location, count);
3340 return true;
3343 if (ieee_per_section (section)->data == (bfd_byte *) NULL)
3345 if (!init_for_output (abfd))
3346 return false;
3348 memcpy ((PTR) (ieee_per_section (section)->data + offset),
3349 (PTR) location,
3350 (unsigned int) count);
3351 return true;
3354 /* Write the external symbols of a file. IEEE considers two sorts of
3355 external symbols, public, and referenced. It uses to internal
3356 forms to index them as well. When we write them out we turn their
3357 symbol values into indexes from the right base. */
3359 static boolean
3360 ieee_write_external_part (abfd)
3361 bfd *abfd;
3363 asymbol **q;
3364 ieee_data_type *ieee = IEEE_DATA (abfd);
3366 unsigned int reference_index = IEEE_REFERENCE_BASE;
3367 unsigned int public_index = IEEE_PUBLIC_BASE + 2;
3368 file_ptr here = bfd_tell (abfd);
3369 boolean hadone = false;
3370 if (abfd->outsymbols != (asymbol **) NULL)
3373 for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++)
3375 asymbol *p = *q;
3376 if (bfd_is_und_section (p->section))
3378 /* This must be a symbol reference .. */
3379 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3380 || ! ieee_write_int (abfd, reference_index)
3381 || ! ieee_write_id (abfd, p->name))
3382 return false;
3383 p->value = reference_index;
3384 reference_index++;
3385 hadone = true;
3387 else if (bfd_is_com_section (p->section))
3389 /* This is a weak reference */
3390 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3391 || ! ieee_write_int (abfd, reference_index)
3392 || ! ieee_write_id (abfd, p->name)
3393 || ! ieee_write_byte (abfd,
3394 ieee_weak_external_reference_enum)
3395 || ! ieee_write_int (abfd, reference_index)
3396 || ! ieee_write_int (abfd, p->value))
3397 return false;
3398 p->value = reference_index;
3399 reference_index++;
3400 hadone = true;
3402 else if (p->flags & BSF_GLOBAL)
3404 /* This must be a symbol definition */
3406 if (! ieee_write_byte (abfd, ieee_external_symbol_enum)
3407 || ! ieee_write_int (abfd, public_index)
3408 || ! ieee_write_id (abfd, p->name)
3409 || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum)
3410 || ! ieee_write_int (abfd, public_index)
3411 || ! ieee_write_byte (abfd, 15) /* instruction address */
3412 || ! ieee_write_byte (abfd, 19) /* static symbol */
3413 || ! ieee_write_byte (abfd, 1)) /* one of them */
3414 return false;
3416 /* Write out the value */
3417 if (! ieee_write_2bytes (abfd, ieee_value_record_enum)
3418 || ! ieee_write_int (abfd, public_index))
3419 return false;
3420 if (! bfd_is_abs_section (p->section))
3422 if (abfd->flags & EXEC_P)
3424 /* If fully linked, then output all symbols
3425 relocated */
3426 if (! (ieee_write_int
3427 (abfd,
3428 (p->value
3429 + p->section->output_offset
3430 + p->section->output_section->vma))))
3431 return false;
3433 else
3435 if (! (ieee_write_expression
3436 (abfd,
3437 p->value + p->section->output_offset,
3438 p->section->output_section->symbol,
3439 false, 0)))
3440 return false;
3443 else
3445 if (! ieee_write_expression (abfd,
3446 p->value,
3447 bfd_abs_section_ptr->symbol,
3448 false, 0))
3449 return false;
3451 p->value = public_index;
3452 public_index++;
3453 hadone = true;
3455 else
3457 /* This can happen - when there are gaps in the symbols read */
3458 /* from an input ieee file */
3462 if (hadone)
3463 ieee->w.r.external_part = here;
3465 return true;
3469 static CONST unsigned char exten[] =
3471 0xf0, 0x20, 0x00,
3472 0xf1, 0xce, 0x20, 0x00, 37, 3, 3, /* Set version 3 rev 3 */
3473 0xf1, 0xce, 0x20, 0x00, 39, 2,/* keep symbol in original case */
3474 0xf1, 0xce, 0x20, 0x00, 38 /* set object type relocateable to x */
3477 static CONST unsigned char envi[] =
3479 0xf0, 0x21, 0x00,
3481 /* 0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
3482 0x19, 0x2c,
3484 0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok */
3486 0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix */
3487 /* 0xf1, 0xce, 0x21, 0, 54, 2,1,1 tool & version # */
3490 static boolean
3491 ieee_write_me_part (abfd)
3492 bfd *abfd;
3494 ieee_data_type *ieee = IEEE_DATA (abfd);
3495 ieee->w.r.trailer_part = bfd_tell (abfd);
3496 if (abfd->start_address)
3498 if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum)
3499 || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum)
3500 || ! ieee_write_int (abfd, abfd->start_address)
3501 || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum))
3502 return false;
3504 ieee->w.r.me_record = bfd_tell (abfd);
3505 if (! ieee_write_byte (abfd, ieee_module_end_enum))
3506 return false;
3507 return true;
3510 /* Write out the IEEE processor ID. */
3512 static boolean
3513 ieee_write_processor (abfd)
3514 bfd *abfd;
3516 const bfd_arch_info_type *arch;
3518 arch = bfd_get_arch_info (abfd);
3519 switch (arch->arch)
3521 default:
3522 if (! ieee_write_id (abfd, bfd_printable_name (abfd)))
3523 return false;
3524 break;
3526 case bfd_arch_a29k:
3527 if (! ieee_write_id (abfd, "29000"))
3528 return false;
3529 break;
3531 case bfd_arch_h8300:
3532 if (! ieee_write_id (abfd, "H8/300"))
3533 return false;
3534 break;
3536 case bfd_arch_h8500:
3537 if (! ieee_write_id (abfd, "H8/500"))
3538 return false;
3539 break;
3541 case bfd_arch_i960:
3542 switch (arch->mach)
3544 default:
3545 case bfd_mach_i960_core:
3546 case bfd_mach_i960_ka_sa:
3547 if (! ieee_write_id (abfd, "80960KA"))
3548 return false;
3549 break;
3551 case bfd_mach_i960_kb_sb:
3552 if (! ieee_write_id (abfd, "80960KB"))
3553 return false;
3554 break;
3556 case bfd_mach_i960_ca:
3557 if (! ieee_write_id (abfd, "80960CA"))
3558 return false;
3559 break;
3561 case bfd_mach_i960_mc:
3562 case bfd_mach_i960_xa:
3563 if (! ieee_write_id (abfd, "80960MC"))
3564 return false;
3565 break;
3567 break;
3569 case bfd_arch_m68k:
3571 const char *id;
3573 switch (arch->mach)
3575 default: id = "68020"; break;
3576 case bfd_mach_m68000: id = "68000"; break;
3577 case bfd_mach_m68008: id = "68008"; break;
3578 case bfd_mach_m68010: id = "68010"; break;
3579 case bfd_mach_m68020: id = "68020"; break;
3580 case bfd_mach_m68030: id = "68030"; break;
3581 case bfd_mach_m68040: id = "68040"; break;
3582 case bfd_mach_m68060: id = "68060"; break;
3583 case bfd_mach_cpu32: id = "cpu32"; break;
3586 if (! ieee_write_id (abfd, id))
3587 return false;
3589 break;
3592 return true;
3595 boolean
3596 ieee_write_object_contents (abfd)
3597 bfd *abfd;
3599 ieee_data_type *ieee = IEEE_DATA (abfd);
3600 unsigned int i;
3601 file_ptr old;
3603 /* Fast forward over the header area */
3604 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3605 return false;
3607 if (! ieee_write_byte (abfd, ieee_module_beginning_enum)
3608 || ! ieee_write_processor (abfd)
3609 || ! ieee_write_id (abfd, abfd->filename))
3610 return false;
3612 /* Fast forward over the variable bits */
3613 if (! ieee_write_byte (abfd, ieee_address_descriptor_enum))
3614 return false;
3616 /* Bits per MAU */
3617 if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd))))
3618 return false;
3619 /* MAU's per address */
3620 if (! ieee_write_byte (abfd,
3621 (bfd_byte) (bfd_arch_bits_per_address (abfd)
3622 / bfd_arch_bits_per_byte (abfd))))
3623 return false;
3625 old = bfd_tell (abfd);
3626 if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0)
3627 return false;
3629 ieee->w.r.extension_record = bfd_tell (abfd);
3630 if (bfd_write ((char *) exten, 1, sizeof (exten), abfd) != sizeof (exten))
3631 return false;
3632 if (abfd->flags & EXEC_P)
3634 if (! ieee_write_byte (abfd, 0x1)) /* Absolute */
3635 return false;
3637 else
3639 if (! ieee_write_byte (abfd, 0x2)) /* Relocateable */
3640 return false;
3643 ieee->w.r.environmental_record = bfd_tell (abfd);
3644 if (bfd_write ((char *) envi, 1, sizeof (envi), abfd) != sizeof (envi))
3645 return false;
3647 /* The HP emulator database requires a timestamp in the file. */
3649 time_t now;
3650 const struct tm *t;
3652 time (&now);
3653 t = (struct tm *) localtime (&now);
3654 if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum)
3655 || ! ieee_write_byte (abfd, 0x21)
3656 || ! ieee_write_byte (abfd, 0)
3657 || ! ieee_write_byte (abfd, 50)
3658 || ! ieee_write_int (abfd, t->tm_year + 1900)
3659 || ! ieee_write_int (abfd, t->tm_mon + 1)
3660 || ! ieee_write_int (abfd, t->tm_mday)
3661 || ! ieee_write_int (abfd, t->tm_hour)
3662 || ! ieee_write_int (abfd, t->tm_min)
3663 || ! ieee_write_int (abfd, t->tm_sec))
3664 return false;
3667 output_bfd = abfd;
3669 flush ();
3671 if (! ieee_write_section_part (abfd))
3672 return false;
3673 /* First write the symbols. This changes their values into table
3674 indeces so we cant use it after this point. */
3675 if (! ieee_write_external_part (abfd))
3676 return false;
3678 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3680 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3683 /* Write any debugs we have been told about. */
3684 if (! ieee_write_debug_part (abfd))
3685 return false;
3687 /* Can only write the data once the symbols have been written, since
3688 the data contains relocation information which points to the
3689 symbols. */
3690 if (! ieee_write_data_part (abfd))
3691 return false;
3693 /* At the end we put the end! */
3694 if (! ieee_write_me_part (abfd))
3695 return false;
3697 /* Generate the header */
3698 if (bfd_seek (abfd, old, SEEK_SET) != 0)
3699 return false;
3701 for (i = 0; i < N_W_VARIABLES; i++)
3703 if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum)
3704 || ! ieee_write_byte (abfd, (bfd_byte) i)
3705 || ! ieee_write_int5_out (abfd, ieee->w.offset[i]))
3706 return false;
3709 return true;
3712 /* Native-level interface to symbols. */
3714 /* We read the symbols into a buffer, which is discarded when this
3715 function exits. We read the strings into a buffer large enough to
3716 hold them all plus all the cached symbol entries. */
3718 asymbol *
3719 ieee_make_empty_symbol (abfd)
3720 bfd *abfd;
3722 ieee_symbol_type *new =
3723 (ieee_symbol_type *) bfd_zalloc (abfd, sizeof (ieee_symbol_type));
3724 if (!new)
3725 return NULL;
3726 new->symbol.the_bfd = abfd;
3727 return &new->symbol;
3730 static bfd *
3731 ieee_openr_next_archived_file (arch, prev)
3732 bfd *arch;
3733 bfd *prev;
3735 ieee_ar_data_type *ar = IEEE_AR_DATA (arch);
3736 /* take the next one from the arch state, or reset */
3737 if (prev == (bfd *) NULL)
3739 /* Reset the index - the first two entries are bogus*/
3740 ar->element_index = 2;
3742 while (true)
3744 ieee_ar_obstack_type *p = ar->elements + ar->element_index;
3745 ar->element_index++;
3746 if (ar->element_index <= ar->element_count)
3748 if (p->file_offset != (file_ptr) 0)
3750 if (p->abfd == (bfd *) NULL)
3752 p->abfd = _bfd_create_empty_archive_element_shell (arch);
3753 p->abfd->origin = p->file_offset;
3755 return p->abfd;
3758 else
3760 bfd_set_error (bfd_error_no_more_archived_files);
3761 return (bfd *) NULL;
3767 static boolean
3768 ieee_find_nearest_line (abfd,
3769 section,
3770 symbols,
3771 offset,
3772 filename_ptr,
3773 functionname_ptr,
3774 line_ptr)
3775 bfd *abfd ATTRIBUTE_UNUSED;
3776 asection *section ATTRIBUTE_UNUSED;
3777 asymbol **symbols ATTRIBUTE_UNUSED;
3778 bfd_vma offset ATTRIBUTE_UNUSED;
3779 const char **filename_ptr ATTRIBUTE_UNUSED;
3780 const char **functionname_ptr ATTRIBUTE_UNUSED;
3781 unsigned int *line_ptr ATTRIBUTE_UNUSED;
3783 return false;
3786 static int
3787 ieee_generic_stat_arch_elt (abfd, buf)
3788 bfd *abfd;
3789 struct stat *buf;
3791 ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL;
3792 ieee_data_type *ieee;
3794 if (abfd->my_archive != NULL)
3795 ar = abfd->my_archive->tdata.ieee_ar_data;
3796 if (ar == (ieee_ar_data_type *) NULL)
3798 bfd_set_error (bfd_error_invalid_operation);
3799 return -1;
3802 if (IEEE_DATA (abfd) == NULL)
3804 if (ieee_object_p (abfd) == NULL)
3806 bfd_set_error (bfd_error_wrong_format);
3807 return -1;
3811 ieee = IEEE_DATA (abfd);
3813 buf->st_size = ieee->w.r.me_record + 1;
3814 buf->st_mode = 0644;
3815 return 0;
3818 static int
3819 ieee_sizeof_headers (abfd, x)
3820 bfd *abfd ATTRIBUTE_UNUSED;
3821 boolean x ATTRIBUTE_UNUSED;
3823 return 0;
3827 /* The debug info routines are never used. */
3828 #if 0
3830 static void
3831 ieee_bfd_debug_info_start (abfd)
3832 bfd *abfd;
3837 static void
3838 ieee_bfd_debug_info_end (abfd)
3839 bfd *abfd;
3845 /* Add this section to the list of sections we have debug info for, to
3846 be ready to output it at close time
3848 static void
3849 ieee_bfd_debug_info_accumulate (abfd, section)
3850 bfd *abfd;
3851 asection *section;
3853 ieee_data_type *ieee = IEEE_DATA (section->owner);
3854 ieee_data_type *output_ieee = IEEE_DATA (abfd);
3855 /* can only accumulate data from other ieee bfds */
3856 if (section->owner->xvec != abfd->xvec)
3857 return;
3858 /* Only bother once per bfd */
3859 if (ieee->done_debug == true)
3860 return;
3861 ieee->done_debug = true;
3863 /* Don't bother if there is no debug info */
3864 if (ieee->w.r.debug_information_part == 0)
3865 return;
3868 /* Add to chain */
3870 bfd_chain_type *n = (bfd_chain_type *) bfd_alloc (abfd, sizeof (bfd_chain_type));
3871 if (!n)
3872 abort (); /* FIXME */
3873 n->this = section->owner;
3874 n->next = (bfd_chain_type *) NULL;
3876 if (output_ieee->chain_head)
3878 output_ieee->chain_head->next = n;
3880 else
3882 output_ieee->chain_root = n;
3885 output_ieee->chain_head = n;
3889 #endif
3891 #define ieee_close_and_cleanup _bfd_generic_close_and_cleanup
3892 #define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3894 #define ieee_slurp_armap bfd_true
3895 #define ieee_slurp_extended_name_table bfd_true
3896 #define ieee_construct_extended_name_table \
3897 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
3898 bfd_true)
3899 #define ieee_truncate_arname bfd_dont_truncate_arname
3900 #define ieee_write_armap \
3901 ((boolean (*) \
3902 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
3903 bfd_true)
3904 #define ieee_read_ar_hdr bfd_nullvoidptr
3905 #define ieee_update_armap_timestamp bfd_true
3906 #define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
3908 #define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
3909 #define ieee_get_lineno _bfd_nosymbols_get_lineno
3910 #define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3911 #define ieee_read_minisymbols _bfd_generic_read_minisymbols
3912 #define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3914 #define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3916 #define ieee_set_arch_mach _bfd_generic_set_arch_mach
3918 #define ieee_get_section_contents_in_window \
3919 _bfd_generic_get_section_contents_in_window
3920 #define ieee_bfd_get_relocated_section_contents \
3921 bfd_generic_get_relocated_section_contents
3922 #define ieee_bfd_relax_section bfd_generic_relax_section
3923 #define ieee_bfd_gc_sections bfd_generic_gc_sections
3924 #define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3925 #define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
3926 #define ieee_bfd_final_link _bfd_generic_final_link
3927 #define ieee_bfd_link_split_section _bfd_generic_link_split_section
3929 /*SUPPRESS 460 */
3930 const bfd_target ieee_vec =
3932 "ieee", /* name */
3933 bfd_target_ieee_flavour,
3934 BFD_ENDIAN_UNKNOWN, /* target byte order */
3935 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
3936 (HAS_RELOC | EXEC_P | /* object flags */
3937 HAS_LINENO | HAS_DEBUG |
3938 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3939 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3940 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3941 '_', /* leading underscore */
3942 ' ', /* ar_pad_char */
3943 16, /* ar_max_namelen */
3944 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3945 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3946 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3947 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3948 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3949 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3951 {_bfd_dummy_target,
3952 ieee_object_p, /* bfd_check_format */
3953 ieee_archive_p,
3954 _bfd_dummy_target,
3957 bfd_false,
3958 ieee_mkobject,
3959 _bfd_generic_mkarchive,
3960 bfd_false
3963 bfd_false,
3964 ieee_write_object_contents,
3965 _bfd_write_archive_contents,
3966 bfd_false,
3969 BFD_JUMP_TABLE_GENERIC (ieee),
3970 BFD_JUMP_TABLE_COPY (_bfd_generic),
3971 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3972 BFD_JUMP_TABLE_ARCHIVE (ieee),
3973 BFD_JUMP_TABLE_SYMBOLS (ieee),
3974 BFD_JUMP_TABLE_RELOCS (ieee),
3975 BFD_JUMP_TABLE_WRITE (ieee),
3976 BFD_JUMP_TABLE_LINK (ieee),
3977 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3979 NULL,
3981 (PTR) 0