Update my e-mail address.
[binutils-gdb.git] / bfd / ieee.c
blob9dce4a8cb309e3bf59b01f40d7e70b2940e89053
1 /* BFD back-end for ieee-695 objects.
2 Copyright (C) 1990-2017 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 #define KEEPMINUSPCININST 0
26 /* IEEE 695 format is a stream of records, which we parse using a simple one-
27 token (which is one byte in this lexicon) lookahead recursive decent
28 parser. */
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "libbfd.h"
33 #include "ieee.h"
34 #include "libieee.h"
35 #include "safe-ctype.h"
36 #include "libiberty.h"
38 struct output_buffer_struct
40 unsigned char *ptrp;
41 int buffer;
44 static unsigned char *output_ptr_start;
45 static unsigned char *output_ptr;
46 static unsigned char *output_ptr_end;
47 static unsigned char *input_ptr_start;
48 static unsigned char *input_ptr;
49 static unsigned char *input_ptr_end;
50 static bfd *input_bfd;
51 static bfd *output_bfd;
52 static int output_buffer;
55 static void block (void);
57 /* Functions for writing to ieee files in the strange way that the
58 standard requires. */
60 static bfd_boolean
61 ieee_write_byte (bfd *abfd, int barg)
63 bfd_byte byte;
65 byte = barg;
66 if (bfd_bwrite ((void *) &byte, (bfd_size_type) 1, abfd) != 1)
67 return FALSE;
68 return TRUE;
71 static bfd_boolean
72 ieee_write_2bytes (bfd *abfd, int bytes)
74 bfd_byte buffer[2];
76 buffer[0] = bytes >> 8;
77 buffer[1] = bytes & 0xff;
78 if (bfd_bwrite ((void *) buffer, (bfd_size_type) 2, abfd) != 2)
79 return FALSE;
80 return TRUE;
83 static bfd_boolean
84 ieee_write_int (bfd *abfd, bfd_vma value)
86 if (value <= 127)
88 if (! ieee_write_byte (abfd, (bfd_byte) value))
89 return FALSE;
91 else
93 unsigned int length;
95 /* How many significant bytes ? */
96 /* FIXME FOR LONGER INTS. */
97 if (value & 0xff000000)
98 length = 4;
99 else if (value & 0x00ff0000)
100 length = 3;
101 else if (value & 0x0000ff00)
102 length = 2;
103 else
104 length = 1;
106 if (! ieee_write_byte (abfd,
107 (bfd_byte) ((int) ieee_number_repeat_start_enum
108 + length)))
109 return FALSE;
110 switch (length)
112 case 4:
113 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 24)))
114 return FALSE;
115 /* Fall through. */
116 case 3:
117 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 16)))
118 return FALSE;
119 /* Fall through. */
120 case 2:
121 if (! ieee_write_byte (abfd, (bfd_byte) (value >> 8)))
122 return FALSE;
123 /* Fall through. */
124 case 1:
125 if (! ieee_write_byte (abfd, (bfd_byte) (value)))
126 return FALSE;
130 return TRUE;
133 static bfd_boolean
134 ieee_write_id (bfd *abfd, const char *id)
136 size_t length = strlen (id);
138 if (length <= 127)
140 if (! ieee_write_byte (abfd, (bfd_byte) length))
141 return FALSE;
143 else if (length < 255)
145 if (! ieee_write_byte (abfd, ieee_extension_length_1_enum)
146 || ! ieee_write_byte (abfd, (bfd_byte) length))
147 return FALSE;
149 else if (length < 65535)
151 if (! ieee_write_byte (abfd, ieee_extension_length_2_enum)
152 || ! ieee_write_2bytes (abfd, (int) length))
153 return FALSE;
155 else
157 _bfd_error_handler
158 /* xgettext:c-format */
159 (_("%B: string too long (%ld chars, max 65535)"), abfd, (long) length);
160 bfd_set_error (bfd_error_invalid_operation);
161 return FALSE;
164 if (bfd_bwrite ((void *) id, (bfd_size_type) length, abfd) != length)
165 return FALSE;
166 return TRUE;
169 /* Functions for reading from ieee files in the strange way that the
170 standard requires. */
172 #define this_byte(ieee) *((ieee)->input_p)
173 #define this_byte_and_next(ieee) ((ieee)->input_p < (ieee)->end_p ? *((ieee)->input_p++) : 0)
175 static bfd_boolean
176 next_byte (common_header_type * ieee)
178 ieee->input_p++;
180 return ieee->input_p < ieee->last_byte;
183 static unsigned short
184 read_2bytes (common_header_type *ieee)
186 unsigned char c1 = this_byte_and_next (ieee);
187 unsigned char c2 = this_byte_and_next (ieee);
189 return (c1 << 8) | c2;
192 static void
193 bfd_get_string (common_header_type *ieee, char *string, size_t length)
195 size_t i;
197 for (i = 0; i < length; i++)
198 string[i] = this_byte_and_next (ieee);
201 static char *
202 read_id (common_header_type *ieee)
204 size_t length;
205 char *string;
207 length = this_byte_and_next (ieee);
208 if (length <= 0x7f)
209 /* Simple string of length 0 to 127. */
212 else if (length == 0xde)
213 /* Length is next byte, allowing 0..255. */
214 length = this_byte_and_next (ieee);
216 else if (length == 0xdf)
218 /* Length is next two bytes, allowing 0..65535. */
219 length = this_byte_and_next (ieee);
220 length = (length * 256) + this_byte_and_next (ieee);
223 /* PR 21612: Check for an invalid length. */
224 if (ieee->input_p + length >= ieee->end_p)
226 _bfd_error_handler (_("IEEE parser: string length: %#lx longer than buffer: %#lx"),
227 (long) length, (long) (ieee->end_p - ieee->input_p));
228 bfd_set_error (bfd_error_invalid_operation);
229 return NULL;
232 /* Buy memory and read string. */
233 string = bfd_alloc (ieee->abfd, (bfd_size_type) length + 1);
234 if (!string)
235 return NULL;
236 bfd_get_string (ieee, string, length);
237 string[length] = 0;
238 return string;
241 static bfd_boolean
242 ieee_write_expression (bfd *abfd,
243 bfd_vma value,
244 asymbol *symbol,
245 bfd_boolean pcrel,
246 unsigned int sindex)
248 unsigned int term_count = 0;
250 if (value != 0)
252 if (! ieee_write_int (abfd, value))
253 return FALSE;
254 term_count++;
257 /* Badly formatted binaries can have a missing symbol,
258 so test here to prevent a seg fault. */
259 if (symbol != NULL)
261 if (bfd_is_com_section (symbol->section)
262 || bfd_is_und_section (symbol->section))
264 /* Def of a common symbol. */
265 if (! ieee_write_byte (abfd, ieee_variable_X_enum)
266 || ! ieee_write_int (abfd, symbol->value))
267 return FALSE;
268 term_count ++;
270 else if (! bfd_is_abs_section (symbol->section))
272 /* Ref to defined symbol - */
273 if (symbol->flags & BSF_GLOBAL)
275 if (! ieee_write_byte (abfd, ieee_variable_I_enum)
276 || ! ieee_write_int (abfd, symbol->value))
277 return FALSE;
278 term_count++;
280 else if (symbol->flags & (BSF_LOCAL | BSF_SECTION_SYM))
282 /* This is a reference to a defined local symbol. We can
283 easily do a local as a section+offset. */
284 if (! ieee_write_byte (abfd, ieee_variable_R_enum)
285 || ! ieee_write_byte (abfd,
286 (bfd_byte) (symbol->section->index
287 + IEEE_SECTION_NUMBER_BASE)))
288 return FALSE;
290 term_count++;
291 if (symbol->value != 0)
293 if (! ieee_write_int (abfd, symbol->value))
294 return FALSE;
295 term_count++;
298 else
300 _bfd_error_handler
301 /* xgettext:c-format */
302 (_("%B: unrecognized symbol `%s' flags 0x%x"),
303 abfd, bfd_asymbol_name (symbol), symbol->flags);
304 bfd_set_error (bfd_error_invalid_operation);
305 return FALSE;
310 if (pcrel)
312 /* Subtract the pc from here by asking for PC of this section. */
313 if (! ieee_write_byte (abfd, ieee_variable_P_enum)
314 || ! ieee_write_byte (abfd,
315 (bfd_byte) (sindex + IEEE_SECTION_NUMBER_BASE))
316 || ! ieee_write_byte (abfd, ieee_function_minus_enum))
317 return FALSE;
320 /* Handle the degenerate case of a 0 address. */
321 if (term_count == 0)
322 if (! ieee_write_int (abfd, (bfd_vma) 0))
323 return FALSE;
325 while (term_count > 1)
327 if (! ieee_write_byte (abfd, ieee_function_plus_enum))
328 return FALSE;
329 term_count--;
332 return TRUE;
335 /* Writes any integer into the buffer supplied and always takes 5 bytes. */
337 static void
338 ieee_write_int5 (bfd_byte *buffer, bfd_vma value)
340 buffer[0] = (bfd_byte) ieee_number_repeat_4_enum;
341 buffer[1] = (value >> 24) & 0xff;
342 buffer[2] = (value >> 16) & 0xff;
343 buffer[3] = (value >> 8) & 0xff;
344 buffer[4] = (value >> 0) & 0xff;
347 static bfd_boolean
348 ieee_write_int5_out (bfd *abfd, bfd_vma value)
350 bfd_byte b[5];
352 ieee_write_int5 (b, value);
353 if (bfd_bwrite ((void *) b, (bfd_size_type) 5, abfd) != 5)
354 return FALSE;
355 return TRUE;
358 static bfd_boolean
359 parse_int (common_header_type *ieee, bfd_vma *value_ptr)
361 int value = this_byte (ieee);
362 int result;
364 if (value >= 0 && value <= 127)
366 *value_ptr = value;
367 return next_byte (ieee);
369 else if (value >= 0x80 && value <= 0x88)
371 unsigned int count = value & 0xf;
373 result = 0;
374 if (! next_byte (ieee))
375 return FALSE;
376 while (count)
378 result = (result << 8) | this_byte_and_next (ieee);
379 count--;
381 *value_ptr = result;
382 return TRUE;
384 return FALSE;
387 static int
388 parse_i (common_header_type *ieee, bfd_boolean *ok)
390 bfd_vma x = 0;
391 *ok = parse_int (ieee, &x);
392 return x;
395 static bfd_vma
396 must_parse_int (common_header_type *ieee)
398 bfd_vma result = 0;
399 BFD_ASSERT (parse_int (ieee, &result));
400 return result;
403 typedef struct
405 bfd_vma value;
406 asection *section;
407 ieee_symbol_index_type symbol;
408 } ieee_value_type;
411 #if KEEPMINUSPCININST
413 #define SRC_MASK(arg) arg
414 #define PCREL_OFFSET FALSE
416 #else
418 #define SRC_MASK(arg) 0
419 #define PCREL_OFFSET TRUE
421 #endif
423 static reloc_howto_type abs32_howto =
424 HOWTO (1,
428 FALSE,
430 complain_overflow_bitfield,
432 "abs32",
433 TRUE,
434 0xffffffff,
435 0xffffffff,
436 FALSE);
438 static reloc_howto_type abs16_howto =
439 HOWTO (1,
443 FALSE,
445 complain_overflow_bitfield,
447 "abs16",
448 TRUE,
449 0x0000ffff,
450 0x0000ffff,
451 FALSE);
453 static reloc_howto_type abs8_howto =
454 HOWTO (1,
458 FALSE,
460 complain_overflow_bitfield,
462 "abs8",
463 TRUE,
464 0x000000ff,
465 0x000000ff,
466 FALSE);
468 static reloc_howto_type rel32_howto =
469 HOWTO (1,
473 TRUE,
475 complain_overflow_signed,
477 "rel32",
478 TRUE,
479 SRC_MASK (0xffffffff),
480 0xffffffff,
481 PCREL_OFFSET);
483 static reloc_howto_type rel16_howto =
484 HOWTO (1,
488 TRUE,
490 complain_overflow_signed,
492 "rel16",
493 TRUE,
494 SRC_MASK (0x0000ffff),
495 0x0000ffff,
496 PCREL_OFFSET);
498 static reloc_howto_type rel8_howto =
499 HOWTO (1,
503 TRUE,
505 complain_overflow_signed,
507 "rel8",
508 TRUE,
509 SRC_MASK (0x000000ff),
510 0x000000ff,
511 PCREL_OFFSET);
513 static ieee_symbol_index_type NOSYMBOL = {0, 0};
515 static bfd_boolean
516 parse_expression (ieee_data_type *ieee,
517 bfd_vma *value,
518 ieee_symbol_index_type *symbol,
519 bfd_boolean *pcrel,
520 unsigned int *extra,
521 asection **section)
524 bfd_boolean loop = TRUE;
525 ieee_value_type stack[10];
526 ieee_value_type *sp = stack;
527 asection *dummy;
529 #define POS sp[1]
530 #define TOS sp[0]
531 #define NOS sp[-1]
532 #define INC sp++;
533 #define DEC sp--;
535 /* The stack pointer always points to the next unused location. */
536 #define PUSH(x,y,z) TOS.symbol = x; TOS.section = y; TOS.value = z; INC;
537 #define POP(x,y,z) DEC; x = TOS.symbol; y = TOS.section; z = TOS.value;
539 while (loop && ieee->h.input_p < ieee->h.last_byte)
541 switch (this_byte (&(ieee->h)))
543 case ieee_variable_P_enum:
544 /* P variable, current program counter for section n. */
546 int section_n;
548 if (! next_byte (&(ieee->h)))
549 return FALSE;
550 *pcrel = TRUE;
551 section_n = must_parse_int (&(ieee->h));
552 (void) section_n;
553 PUSH (NOSYMBOL, bfd_abs_section_ptr, 0);
554 break;
557 case ieee_variable_L_enum:
558 /* L variable address of section N. */
559 if (! next_byte (&(ieee->h)))
560 return FALSE;
561 PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
562 break;
564 case ieee_variable_R_enum:
565 /* R variable, logical address of section module. */
566 /* FIXME, this should be different to L. */
567 if (! next_byte (&(ieee->h)))
568 return FALSE;
569 PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
570 break;
572 case ieee_variable_S_enum:
573 /* S variable, size in MAUS of section module. */
574 if (! next_byte (&(ieee->h)))
575 return FALSE;
576 PUSH (NOSYMBOL,
578 ieee->section_table[must_parse_int (&(ieee->h))]->size);
579 break;
581 case ieee_variable_I_enum:
582 /* Push the address of variable n. */
584 ieee_symbol_index_type sy;
586 if (! next_byte (&(ieee->h)))
587 return FALSE;
588 sy.index = (int) must_parse_int (&(ieee->h));
589 sy.letter = 'I';
591 PUSH (sy, bfd_abs_section_ptr, 0);
593 break;
595 case ieee_variable_X_enum:
596 /* Push the address of external variable n. */
598 ieee_symbol_index_type sy;
600 if (! next_byte (&(ieee->h)))
601 return FALSE;
603 sy.index = (int) (must_parse_int (&(ieee->h)));
604 sy.letter = 'X';
606 PUSH (sy, bfd_und_section_ptr, 0);
608 break;
610 case ieee_function_minus_enum:
612 bfd_vma value1, value2;
613 asection *section1, *section_dummy;
614 ieee_symbol_index_type sy;
616 if (! next_byte (&(ieee->h)))
617 return FALSE;
619 POP (sy, section1, value1);
620 POP (sy, section_dummy, value2);
621 PUSH (sy, section1 ? section1 : section_dummy, value2 - value1);
623 break;
625 case ieee_function_plus_enum:
627 bfd_vma value1, value2;
628 asection *section1;
629 asection *section2;
630 ieee_symbol_index_type sy1;
631 ieee_symbol_index_type sy2;
633 if (! next_byte (&(ieee->h)))
634 return FALSE;
636 POP (sy1, section1, value1);
637 POP (sy2, section2, value2);
638 PUSH (sy1.letter ? sy1 : sy2,
639 bfd_is_abs_section (section1) ? section2 : section1,
640 value1 + value2);
642 break;
644 default:
646 bfd_vma va;
648 BFD_ASSERT (this_byte (&(ieee->h)) < (int) ieee_variable_A_enum
649 || this_byte (&(ieee->h)) > (int) ieee_variable_Z_enum);
650 if (parse_int (&(ieee->h), &va))
652 PUSH (NOSYMBOL, bfd_abs_section_ptr, va);
654 else
655 /* Thats all that we can understand. */
656 loop = FALSE;
661 /* As far as I can see there is a bug in the Microtec IEEE output
662 which I'm using to scan, whereby the comma operator is omitted
663 sometimes in an expression, giving expressions with too many
664 terms. We can tell if that's the case by ensuring that
665 sp == stack here. If not, then we've pushed something too far,
666 so we keep adding. */
667 while (sp != stack + 1)
669 asection *section1;
670 ieee_symbol_index_type sy1;
672 POP (sy1, section1, *extra);
673 (void) section1;
674 (void) sy1;
677 POP (*symbol, dummy, *value);
678 if (section)
679 *section = dummy;
681 return TRUE;
684 #define ieee_pos(ieee) \
685 (ieee->h.input_p - ieee->h.first_byte)
687 /* Find the first part of the ieee file after HERE. */
689 static file_ptr
690 ieee_part_after (ieee_data_type *ieee, file_ptr here)
692 int part;
693 file_ptr after = ieee->w.r.me_record;
695 /* File parts can come in any order, except that module end is
696 guaranteed to be last (and the header first). */
697 for (part = 0; part < N_W_VARIABLES; part++)
698 if (ieee->w.offset[part] > here && after > ieee->w.offset[part])
699 after = ieee->w.offset[part];
701 return after;
704 static bfd_boolean
705 ieee_seek (ieee_data_type * ieee, file_ptr offset)
707 /* PR 17512: file: 017-1157-0.004. */
708 if (offset < 0 || (bfd_size_type) offset >= ieee->h.total_amt)
710 ieee->h.input_p = ieee->h.first_byte + ieee->h.total_amt;
711 ieee->h.end_p = ieee->h.last_byte = ieee->h.input_p;
712 return FALSE;
715 ieee->h.input_p = ieee->h.first_byte + offset;
716 ieee->h.end_p = ieee->h.last_byte = (ieee->h.first_byte + ieee_part_after (ieee, offset));
717 return TRUE;
720 static unsigned int last_index;
721 static char last_type; /* Is the index for an X or a D. */
723 static ieee_symbol_type *
724 get_symbol (bfd *abfd ATTRIBUTE_UNUSED,
725 ieee_data_type *ieee,
726 ieee_symbol_type *last_symbol,
727 unsigned int *symbol_count,
728 ieee_symbol_type ***pptr,
729 unsigned int *max_index,
730 int this_type)
732 /* Need a new symbol. */
733 unsigned int new_index = must_parse_int (&(ieee->h));
735 if (new_index != last_index || this_type != last_type)
737 ieee_symbol_type *new_symbol;
738 bfd_size_type amt = sizeof (ieee_symbol_type);
740 new_symbol = bfd_alloc (ieee->h.abfd, amt);
741 if (!new_symbol)
742 return NULL;
744 new_symbol->index = new_index;
745 last_index = new_index;
746 (*symbol_count)++;
747 **pptr = new_symbol;
748 *pptr = &new_symbol->next;
749 if (new_index > *max_index)
750 *max_index = new_index;
752 last_type = this_type;
753 new_symbol->symbol.section = bfd_abs_section_ptr;
754 return new_symbol;
756 return last_symbol;
759 static bfd_boolean
760 ieee_slurp_external_symbols (bfd *abfd)
762 ieee_data_type *ieee = IEEE_DATA (abfd);
763 file_ptr offset = ieee->w.r.external_part;
765 ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
766 ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
767 ieee_symbol_type *symbol = NULL;
768 unsigned int symbol_count = 0;
769 bfd_boolean loop = TRUE;
771 last_index = 0xffffff;
772 ieee->symbol_table_full = TRUE;
774 if (! ieee_seek (ieee, offset))
775 return FALSE;
777 while (loop)
779 switch (this_byte (&(ieee->h)))
781 case ieee_nn_record:
782 if (! next_byte (&(ieee->h)))
783 return FALSE;
785 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
786 & prev_symbols_ptr,
787 & ieee->external_symbol_max_index, 'I');
788 if (symbol == NULL)
789 return FALSE;
791 symbol->symbol.the_bfd = abfd;
792 symbol->symbol.name = read_id (&(ieee->h));
793 symbol->symbol.udata.p = NULL;
794 symbol->symbol.flags = BSF_NO_FLAGS;
795 break;
797 case ieee_external_symbol_enum:
798 if (! next_byte (&(ieee->h)))
799 return FALSE;
801 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
802 &prev_symbols_ptr,
803 &ieee->external_symbol_max_index, 'D');
804 if (symbol == NULL)
805 return FALSE;
807 BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
809 symbol->symbol.the_bfd = abfd;
810 symbol->symbol.name = read_id (&(ieee->h));
811 symbol->symbol.udata.p = NULL;
812 symbol->symbol.flags = BSF_NO_FLAGS;
813 break;
814 case ieee_attribute_record_enum >> 8:
816 unsigned int symbol_name_index;
817 unsigned int symbol_type_index;
818 unsigned int symbol_attribute_def;
819 bfd_vma value = 0;
821 switch (read_2bytes (&ieee->h))
823 case ieee_attribute_record_enum:
824 symbol_name_index = must_parse_int (&(ieee->h));
825 symbol_type_index = must_parse_int (&(ieee->h));
826 (void) symbol_type_index;
827 symbol_attribute_def = must_parse_int (&(ieee->h));
828 switch (symbol_attribute_def)
830 case 8:
831 case 19:
832 parse_int (&ieee->h, &value);
833 break;
834 default:
835 _bfd_error_handler
836 /* xgettext:c-format */
837 (_("%B: unimplemented ATI record %u for symbol %u"),
838 abfd, symbol_attribute_def, symbol_name_index);
839 bfd_set_error (bfd_error_bad_value);
840 return FALSE;
841 break;
843 break;
844 case ieee_external_reference_info_record_enum:
845 /* Skip over ATX record. */
846 parse_int (&(ieee->h), &value);
847 parse_int (&(ieee->h), &value);
848 parse_int (&(ieee->h), &value);
849 parse_int (&(ieee->h), &value);
850 break;
851 case ieee_atn_record_enum:
852 /* We may get call optimization information here,
853 which we just ignore. The format is
854 {$F1}${CE}{index}{$00}{$3F}{$3F}{#_of_ASNs}. */
855 parse_int (&ieee->h, &value);
856 parse_int (&ieee->h, &value);
857 parse_int (&ieee->h, &value);
858 if (value != 0x3f)
860 _bfd_error_handler
861 /* xgettext:c-format */
862 (_("%B: unexpected ATN type %Ld in external part"),
863 abfd, value);
864 bfd_set_error (bfd_error_bad_value);
865 return FALSE;
867 parse_int (&ieee->h, &value);
868 parse_int (&ieee->h, &value);
869 while (value > 0)
871 bfd_vma val1;
873 --value;
875 switch (read_2bytes (&ieee->h))
877 case ieee_asn_record_enum:
878 parse_int (&ieee->h, &val1);
879 parse_int (&ieee->h, &val1);
880 break;
882 default:
883 _bfd_error_handler
884 (_("%B: unexpected type after ATN"), abfd);
885 bfd_set_error (bfd_error_bad_value);
886 return FALSE;
891 break;
893 case ieee_value_record_enum >> 8:
895 unsigned int symbol_name_index;
896 ieee_symbol_index_type symbol_ignore;
897 bfd_boolean pcrel_ignore;
898 unsigned int extra;
900 if (! next_byte (&(ieee->h)))
901 return FALSE;
902 if (! next_byte (&(ieee->h)))
903 return FALSE;
905 symbol_name_index = must_parse_int (&(ieee->h));
906 (void) symbol_name_index;
907 if (! parse_expression (ieee,
908 &symbol->symbol.value,
909 &symbol_ignore,
910 &pcrel_ignore,
911 &extra,
912 &symbol->symbol.section))
913 return FALSE;
915 /* Fully linked IEEE-695 files tend to give every symbol
916 an absolute value. Try to convert that back into a
917 section relative value. FIXME: This won't always to
918 the right thing. */
919 if (bfd_is_abs_section (symbol->symbol.section)
920 && (abfd->flags & HAS_RELOC) == 0)
922 bfd_vma val;
923 asection *s;
925 val = symbol->symbol.value;
926 for (s = abfd->sections; s != NULL; s = s->next)
928 if (val >= s->vma && val < s->vma + s->size)
930 symbol->symbol.section = s;
931 symbol->symbol.value -= s->vma;
932 break;
937 symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
940 break;
941 case ieee_weak_external_reference_enum:
943 bfd_vma size;
944 bfd_vma value;
946 if (! next_byte (&(ieee->h)))
947 return FALSE;
949 /* Throw away the external reference index. */
950 (void) must_parse_int (&(ieee->h));
951 /* Fetch the default size if not resolved. */
952 size = must_parse_int (&(ieee->h));
953 /* Fetch the default value if available. */
954 if (! parse_int (&(ieee->h), &value))
955 value = 0;
956 /* This turns into a common. */
957 symbol->symbol.section = bfd_com_section_ptr;
958 symbol->symbol.value = size;
960 break;
962 case ieee_external_reference_enum:
963 if (! next_byte (&(ieee->h)))
964 return FALSE;
966 symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
967 &prev_reference_ptr,
968 &ieee->external_reference_max_index, 'X');
969 if (symbol == NULL)
970 return FALSE;
972 symbol->symbol.the_bfd = abfd;
973 symbol->symbol.name = read_id (&(ieee->h));
974 symbol->symbol.udata.p = NULL;
975 symbol->symbol.section = bfd_und_section_ptr;
976 symbol->symbol.value = (bfd_vma) 0;
977 symbol->symbol.flags = 0;
979 BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
980 break;
982 default:
983 loop = FALSE;
987 if (ieee->external_symbol_max_index != 0)
989 ieee->external_symbol_count =
990 ieee->external_symbol_max_index -
991 ieee->external_symbol_min_index + 1;
993 else
994 ieee->external_symbol_count = 0;
996 if (ieee->external_reference_max_index != 0)
998 ieee->external_reference_count =
999 ieee->external_reference_max_index -
1000 ieee->external_reference_min_index + 1;
1002 else
1003 ieee->external_reference_count = 0;
1005 abfd->symcount =
1006 ieee->external_reference_count + ieee->external_symbol_count;
1008 if (symbol_count != abfd->symcount)
1009 /* There are gaps in the table -- */
1010 ieee->symbol_table_full = FALSE;
1012 *prev_symbols_ptr = NULL;
1013 *prev_reference_ptr = NULL;
1015 return TRUE;
1018 static bfd_boolean
1019 ieee_slurp_symbol_table (bfd *abfd)
1021 if (! IEEE_DATA (abfd)->read_symbols)
1023 if (! ieee_slurp_external_symbols (abfd))
1024 return FALSE;
1025 IEEE_DATA (abfd)->read_symbols = TRUE;
1027 return TRUE;
1030 static long
1031 ieee_get_symtab_upper_bound (bfd *abfd)
1033 if (! ieee_slurp_symbol_table (abfd))
1034 return -1;
1036 return (abfd->symcount != 0) ?
1037 (abfd->symcount + 1) * (sizeof (ieee_symbol_type *)) : 0;
1040 /* Move from our internal lists to the canon table, and insert in
1041 symbol index order. */
1043 extern const bfd_target ieee_vec;
1045 static long
1046 ieee_canonicalize_symtab (bfd *abfd, asymbol **location)
1048 ieee_symbol_type *symp;
1049 static bfd dummy_bfd;
1050 static asymbol empty_symbol =
1052 &dummy_bfd,
1053 " ieee empty",
1054 (symvalue) 0,
1055 BSF_DEBUGGING,
1056 bfd_abs_section_ptr
1057 #ifdef __STDC__
1058 /* K&R compilers can't initialise unions. */
1059 , { 0 }
1060 #endif
1063 if (abfd->symcount)
1065 ieee_data_type *ieee = IEEE_DATA (abfd);
1067 dummy_bfd.xvec = &ieee_vec;
1068 if (! ieee_slurp_symbol_table (abfd))
1069 return -1;
1071 if (! ieee->symbol_table_full)
1073 /* Arrgh - there are gaps in the table, run through and fill them
1074 up with pointers to a null place. */
1075 unsigned int i;
1077 for (i = 0; i < abfd->symcount; i++)
1078 location[i] = &empty_symbol;
1081 ieee->external_symbol_base_offset = -ieee->external_symbol_min_index;
1082 for (symp = IEEE_DATA (abfd)->external_symbols;
1083 symp != (ieee_symbol_type *) NULL;
1084 symp = symp->next)
1085 /* Place into table at correct index locations. */
1086 location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
1088 /* The external refs are indexed in a bit. */
1089 ieee->external_reference_base_offset =
1090 -ieee->external_reference_min_index + ieee->external_symbol_count;
1092 for (symp = IEEE_DATA (abfd)->external_reference;
1093 symp != (ieee_symbol_type *) NULL;
1094 symp = symp->next)
1095 location[symp->index + ieee->external_reference_base_offset] =
1096 &symp->symbol;
1099 if (abfd->symcount)
1100 location[abfd->symcount] = (asymbol *) NULL;
1102 return abfd->symcount;
1105 static asection *
1106 get_section_entry (bfd *abfd, ieee_data_type *ieee, unsigned int sindex)
1108 if (sindex >= ieee->section_table_size)
1110 unsigned int c, i;
1111 asection **n;
1112 bfd_size_type amt;
1114 c = ieee->section_table_size;
1115 if (c == 0)
1116 c = 20;
1117 while (c <= sindex)
1118 c *= 2;
1120 amt = c;
1121 amt *= sizeof (asection *);
1122 n = bfd_realloc (ieee->section_table, amt);
1123 if (n == NULL)
1124 return NULL;
1126 for (i = ieee->section_table_size; i < c; i++)
1127 n[i] = NULL;
1129 ieee->section_table = n;
1130 ieee->section_table_size = c;
1133 if (ieee->section_table[sindex] == (asection *) NULL)
1135 char *tmp = bfd_alloc (abfd, (bfd_size_type) 11);
1136 asection *section;
1138 if (!tmp)
1139 return NULL;
1140 sprintf (tmp, " fsec%4d", sindex);
1141 section = bfd_make_section (abfd, tmp);
1142 ieee->section_table[sindex] = section;
1143 section->target_index = sindex;
1144 ieee->section_table[sindex] = section;
1146 return ieee->section_table[sindex];
1149 static bfd_boolean
1150 ieee_slurp_sections (bfd *abfd)
1152 ieee_data_type *ieee = IEEE_DATA (abfd);
1153 file_ptr offset = ieee->w.r.section_part;
1154 char *name;
1156 if (offset != 0)
1158 bfd_byte section_type[3];
1160 if (! ieee_seek (ieee, offset))
1161 return FALSE;
1163 while (TRUE)
1165 switch (this_byte (&(ieee->h)))
1167 case ieee_section_type_enum:
1169 asection *section;
1170 unsigned int section_index;
1172 if (! next_byte (&(ieee->h)))
1173 return FALSE;
1174 section_index = must_parse_int (&(ieee->h));
1176 section = get_section_entry (abfd, ieee, section_index);
1178 section_type[0] = this_byte_and_next (&(ieee->h));
1180 /* Set minimal section attributes. Attributes are
1181 extended later, based on section contents. */
1182 switch (section_type[0])
1184 case 0xC1:
1185 /* Normal attributes for absolute sections. */
1186 section_type[1] = this_byte (&(ieee->h));
1187 section->flags = SEC_ALLOC;
1188 switch (section_type[1])
1190 /* AS Absolute section attributes. */
1191 case 0xD3:
1192 if (! next_byte (&(ieee->h)))
1193 return FALSE;
1194 section_type[2] = this_byte (&(ieee->h));
1195 switch (section_type[2])
1197 case 0xD0:
1198 /* Normal code. */
1199 if (! next_byte (&(ieee->h)))
1200 return FALSE;
1201 section->flags |= SEC_CODE;
1202 break;
1203 case 0xC4:
1204 /* Normal data. */
1205 if (! next_byte (&(ieee->h)))
1206 return FALSE;
1207 section->flags |= SEC_DATA;
1208 break;
1209 case 0xD2:
1210 if (! next_byte (&(ieee->h)))
1211 return FALSE;
1212 /* Normal rom data. */
1213 section->flags |= SEC_ROM | SEC_DATA;
1214 break;
1215 default:
1216 break;
1219 break;
1221 /* Named relocatable sections (type C). */
1222 case 0xC3:
1223 section_type[1] = this_byte (&(ieee->h));
1224 section->flags = SEC_ALLOC;
1225 switch (section_type[1])
1227 case 0xD0: /* Normal code (CP). */
1228 if (! next_byte (&(ieee->h)))
1229 return FALSE;
1230 section->flags |= SEC_CODE;
1231 break;
1232 case 0xC4: /* Normal data (CD). */
1233 if (! next_byte (&(ieee->h)))
1234 return FALSE;
1235 section->flags |= SEC_DATA;
1236 break;
1237 case 0xD2: /* Normal rom data (CR). */
1238 if (! next_byte (&(ieee->h)))
1239 return FALSE;
1240 section->flags |= SEC_ROM | SEC_DATA;
1241 break;
1242 default:
1243 break;
1247 /* Read section name, use it if non empty. */
1248 name = read_id (&ieee->h);
1249 if (name == NULL)
1250 return FALSE;
1251 if (name[0])
1252 section->name = name;
1254 /* Skip these fields, which we don't care about. */
1256 bfd_vma parent, brother, context;
1258 parse_int (&(ieee->h), &parent);
1259 parse_int (&(ieee->h), &brother);
1260 parse_int (&(ieee->h), &context);
1263 break;
1264 case ieee_section_alignment_enum:
1266 unsigned int section_index;
1267 bfd_vma value;
1268 asection *section;
1270 if (! next_byte (&(ieee->h)))
1271 return FALSE;
1272 section_index = must_parse_int (&ieee->h);
1273 section = get_section_entry (abfd, ieee, section_index);
1274 if (section_index > ieee->section_count)
1275 ieee->section_count = section_index;
1277 section->alignment_power =
1278 bfd_log2 (must_parse_int (&ieee->h));
1279 (void) parse_int (&(ieee->h), &value);
1281 break;
1282 case ieee_e2_first_byte_enum:
1284 asection *section;
1285 ieee_record_enum_type t;
1287 t = (ieee_record_enum_type) (read_2bytes (&(ieee->h)));
1288 switch (t)
1290 case ieee_section_size_enum:
1291 section = ieee->section_table[must_parse_int (&(ieee->h))];
1292 section->size = must_parse_int (&(ieee->h));
1293 break;
1294 case ieee_physical_region_size_enum:
1295 section = ieee->section_table[must_parse_int (&(ieee->h))];
1296 section->size = must_parse_int (&(ieee->h));
1297 break;
1298 case ieee_region_base_address_enum:
1299 section = ieee->section_table[must_parse_int (&(ieee->h))];
1300 section->vma = must_parse_int (&(ieee->h));
1301 section->lma = section->vma;
1302 break;
1303 case ieee_mau_size_enum:
1304 must_parse_int (&(ieee->h));
1305 must_parse_int (&(ieee->h));
1306 break;
1307 case ieee_m_value_enum:
1308 must_parse_int (&(ieee->h));
1309 must_parse_int (&(ieee->h));
1310 break;
1311 case ieee_section_base_address_enum:
1312 section = ieee->section_table[must_parse_int (&(ieee->h))];
1313 section->vma = must_parse_int (&(ieee->h));
1314 section->lma = section->vma;
1315 break;
1316 case ieee_section_offset_enum:
1317 (void) must_parse_int (&(ieee->h));
1318 (void) must_parse_int (&(ieee->h));
1319 break;
1320 default:
1321 return TRUE;
1324 break;
1325 default:
1326 return TRUE;
1331 return TRUE;
1334 /* Make a section for the debugging information, if any. We don't try
1335 to interpret the debugging information; we just point the section
1336 at the area in the file so that program which understand can dig it
1337 out. */
1339 static bfd_boolean
1340 ieee_slurp_debug (bfd *abfd)
1342 ieee_data_type *ieee = IEEE_DATA (abfd);
1343 asection *sec;
1344 file_ptr debug_end;
1345 flagword flags;
1347 if (ieee->w.r.debug_information_part == 0)
1348 return TRUE;
1350 flags = SEC_DEBUGGING | SEC_HAS_CONTENTS;
1351 sec = bfd_make_section_with_flags (abfd, ".debug", flags);
1352 if (sec == NULL)
1353 return FALSE;
1354 sec->filepos = ieee->w.r.debug_information_part;
1356 debug_end = ieee_part_after (ieee, ieee->w.r.debug_information_part);
1357 sec->size = debug_end - ieee->w.r.debug_information_part;
1359 return TRUE;
1362 /* Archive stuff. */
1364 static const bfd_target *
1365 ieee_archive_p (bfd *abfd)
1367 char *library;
1368 unsigned int i;
1369 static unsigned char buffer[512];
1370 file_ptr buffer_offset = 0;
1371 ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
1372 ieee_ar_data_type *ieee;
1373 bfd_size_type alc_elts;
1374 ieee_ar_obstack_type *elts = NULL;
1375 bfd_size_type amt = sizeof (ieee_ar_data_type);
1377 abfd->tdata.ieee_ar_data = bfd_alloc (abfd, amt);
1378 if (!abfd->tdata.ieee_ar_data)
1379 goto error_ret_restore;
1380 ieee = IEEE_AR_DATA (abfd);
1382 /* Ignore the return value here. It doesn't matter if we don't read
1383 the entire buffer. We might have a very small ieee file. */
1384 if (bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd) <= 0)
1385 goto got_wrong_format_error;
1387 ieee->h.first_byte = buffer;
1388 ieee->h.input_p = buffer;
1389 ieee->h.total_amt = sizeof (buffer);
1390 ieee->h.end_p = buffer + sizeof (buffer);
1392 ieee->h.abfd = abfd;
1394 if (this_byte (&(ieee->h)) != Module_Beginning)
1395 goto got_wrong_format_error;
1397 (void) next_byte (&(ieee->h));
1399 library = read_id (&(ieee->h));
1400 if (library == NULL)
1401 goto got_wrong_format_error;
1402 if (strcmp (library, "LIBRARY") != 0)
1403 goto got_wrong_format_error;
1405 /* Throw away the filename. */
1406 read_id (&(ieee->h));
1408 ieee->element_count = 0;
1409 ieee->element_index = 0;
1411 (void) next_byte (&(ieee->h)); /* Drop the ad part. */
1412 must_parse_int (&(ieee->h)); /* And the two dummy numbers. */
1413 must_parse_int (&(ieee->h));
1415 alc_elts = 10;
1416 elts = bfd_malloc (alc_elts * sizeof *elts);
1417 if (elts == NULL)
1418 goto error_return;
1420 /* Read the index of the BB table. */
1421 while (1)
1423 int rec;
1424 ieee_ar_obstack_type *t;
1426 rec = read_2bytes (&(ieee->h));
1427 if (rec != (int) ieee_assign_value_to_variable_enum)
1428 break;
1430 if (ieee->element_count >= alc_elts)
1432 ieee_ar_obstack_type *n;
1434 alc_elts *= 2;
1435 n = bfd_realloc (elts, alc_elts * sizeof (* elts));
1436 if (n == NULL)
1437 goto error_return;
1438 elts = n;
1441 t = &elts[ieee->element_count];
1442 ieee->element_count++;
1444 must_parse_int (&(ieee->h));
1445 t->file_offset = must_parse_int (&(ieee->h));
1446 t->abfd = (bfd *) NULL;
1448 /* Make sure that we don't go over the end of the buffer. */
1449 if ((size_t) ieee_pos (IEEE_DATA (abfd)) > sizeof (buffer) / 2)
1451 /* Past half way, reseek and reprime. */
1452 buffer_offset += ieee_pos (IEEE_DATA (abfd));
1453 if (bfd_seek (abfd, buffer_offset, SEEK_SET) != 0)
1454 goto error_return;
1456 /* Again ignore return value of bfd_bread. */
1457 bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd);
1458 ieee->h.first_byte = buffer;
1459 ieee->h.input_p = buffer;
1460 ieee->h.total_amt = sizeof (buffer);
1461 ieee->h.end_p = buffer + sizeof (buffer);
1465 amt = ieee->element_count;
1466 amt *= sizeof *ieee->elements;
1467 ieee->elements = bfd_alloc (abfd, amt);
1468 if (ieee->elements == NULL)
1469 goto error_return;
1471 memcpy (ieee->elements, elts, (size_t) amt);
1472 free (elts);
1473 elts = NULL;
1475 /* Now scan the area again, and replace BB offsets with file offsets. */
1476 for (i = 2; i < ieee->element_count; i++)
1478 if (bfd_seek (abfd, ieee->elements[i].file_offset, SEEK_SET) != 0)
1479 goto error_return;
1481 /* Again ignore return value of bfd_bread. */
1482 bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd);
1483 ieee->h.first_byte = buffer;
1484 ieee->h.input_p = buffer;
1485 ieee->h.total_amt = sizeof (buffer);
1486 ieee->h.end_p = buffer + sizeof (buffer);
1488 (void) next_byte (&(ieee->h)); /* Drop F8. */
1489 if (! next_byte (&(ieee->h))) /* Drop 14. */
1490 goto error_return;
1491 must_parse_int (&(ieee->h)); /* Drop size of block. */
1493 if (must_parse_int (&(ieee->h)) != 0)
1494 /* This object has been deleted. */
1495 ieee->elements[i].file_offset = 0;
1496 else
1497 ieee->elements[i].file_offset = must_parse_int (&(ieee->h));
1500 /* abfd->has_armap = ;*/
1502 return abfd->xvec;
1504 got_wrong_format_error:
1505 bfd_set_error (bfd_error_wrong_format);
1506 error_return:
1507 if (elts != NULL)
1508 free (elts);
1509 bfd_release (abfd, ieee);
1510 error_ret_restore:
1511 abfd->tdata.ieee_ar_data = save;
1513 return NULL;
1516 static bfd_boolean
1517 ieee_mkobject (bfd *abfd)
1519 bfd_size_type amt;
1521 output_ptr_start = NULL;
1522 output_ptr = NULL;
1523 output_ptr_end = NULL;
1524 input_ptr_start = NULL;
1525 input_ptr = NULL;
1526 input_ptr_end = NULL;
1527 input_bfd = NULL;
1528 output_bfd = NULL;
1529 output_buffer = 0;
1530 amt = sizeof (ieee_data_type);
1531 abfd->tdata.ieee_data = bfd_zalloc (abfd, amt);
1532 return abfd->tdata.ieee_data != NULL;
1535 static bfd_boolean
1536 do_one (ieee_data_type *ieee,
1537 ieee_per_section_type *current_map,
1538 unsigned char *location_ptr,
1539 asection *s,
1540 int iterations)
1542 switch (this_byte (&(ieee->h)))
1544 case ieee_load_constant_bytes_enum:
1546 unsigned int number_of_maus;
1547 unsigned int i;
1549 if (! next_byte (&(ieee->h)))
1550 return FALSE;
1551 number_of_maus = must_parse_int (&(ieee->h));
1553 for (i = 0; i < number_of_maus; i++)
1555 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1556 next_byte (&(ieee->h));
1559 break;
1561 case ieee_load_with_relocation_enum:
1563 bfd_boolean loop = TRUE;
1565 if (! next_byte (&(ieee->h)))
1566 return FALSE;
1567 while (loop)
1569 switch (this_byte (&(ieee->h)))
1571 case ieee_variable_R_enum:
1573 case ieee_function_signed_open_b_enum:
1574 case ieee_function_unsigned_open_b_enum:
1575 case ieee_function_either_open_b_enum:
1577 unsigned int extra = 4;
1578 bfd_boolean pcrel = FALSE;
1579 asection *section;
1580 ieee_reloc_type *r;
1582 r = bfd_alloc (ieee->h.abfd, sizeof (* r));
1583 if (!r)
1584 return FALSE;
1586 *(current_map->reloc_tail_ptr) = r;
1587 current_map->reloc_tail_ptr = &r->next;
1588 r->next = (ieee_reloc_type *) NULL;
1589 if (! next_byte (&(ieee->h)))
1590 return FALSE;
1592 r->relent.sym_ptr_ptr = 0;
1593 if (! parse_expression (ieee,
1594 &r->relent.addend,
1595 &r->symbol,
1596 &pcrel, &extra, &section))
1597 return FALSE;
1599 r->relent.address = current_map->pc;
1600 s->flags |= SEC_RELOC;
1601 s->owner->flags |= HAS_RELOC;
1602 s->reloc_count++;
1603 if (r->relent.sym_ptr_ptr == NULL && section != NULL)
1604 r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1606 if (this_byte (&(ieee->h)) == (int) ieee_comma)
1608 if (! next_byte (&(ieee->h)))
1609 return FALSE;
1610 /* Fetch number of bytes to pad. */
1611 extra = must_parse_int (&(ieee->h));
1614 switch (this_byte (&(ieee->h)))
1616 case ieee_function_signed_close_b_enum:
1617 if (! next_byte (&(ieee->h)))
1618 return FALSE;
1619 break;
1620 case ieee_function_unsigned_close_b_enum:
1621 if (! next_byte (&(ieee->h)))
1622 return FALSE;
1623 break;
1624 case ieee_function_either_close_b_enum:
1625 if (! next_byte (&(ieee->h)))
1626 return FALSE;
1627 break;
1628 default:
1629 break;
1631 /* Build a relocation entry for this type. */
1632 /* If pc rel then stick -ve pc into instruction
1633 and take out of reloc ..
1635 I've changed this. It's all too complicated. I
1636 keep 0 in the instruction now. */
1638 switch (extra)
1640 case 0:
1641 case 4:
1643 if (pcrel)
1645 #if KEEPMINUSPCININST
1646 bfd_put_32 (ieee->h.abfd, -current_map->pc,
1647 location_ptr + current_map->pc);
1648 r->relent.howto = &rel32_howto;
1649 r->relent.addend -= current_map->pc;
1650 #else
1651 bfd_put_32 (ieee->h.abfd, (bfd_vma) 0, location_ptr +
1652 current_map->pc);
1653 r->relent.howto = &rel32_howto;
1654 #endif
1656 else
1658 bfd_put_32 (ieee->h.abfd, (bfd_vma) 0,
1659 location_ptr + current_map->pc);
1660 r->relent.howto = &abs32_howto;
1662 current_map->pc += 4;
1663 break;
1664 case 2:
1665 if (pcrel)
1667 #if KEEPMINUSPCININST
1668 bfd_put_16 (ieee->h.abfd, (bfd_vma) -current_map->pc,
1669 location_ptr + current_map->pc);
1670 r->relent.addend -= current_map->pc;
1671 r->relent.howto = &rel16_howto;
1672 #else
1674 bfd_put_16 (ieee->h.abfd, (bfd_vma) 0,
1675 location_ptr + current_map->pc);
1676 r->relent.howto = &rel16_howto;
1677 #endif
1680 else
1682 bfd_put_16 (ieee->h.abfd, (bfd_vma) 0,
1683 location_ptr + current_map->pc);
1684 r->relent.howto = &abs16_howto;
1686 current_map->pc += 2;
1687 break;
1688 case 1:
1689 if (pcrel)
1691 #if KEEPMINUSPCININST
1692 bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1693 r->relent.addend -= current_map->pc;
1694 r->relent.howto = &rel8_howto;
1695 #else
1696 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1697 r->relent.howto = &rel8_howto;
1698 #endif
1700 else
1702 bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1703 r->relent.howto = &abs8_howto;
1705 current_map->pc += 1;
1706 break;
1708 default:
1709 BFD_FAIL ();
1710 return FALSE;
1713 break;
1714 default:
1716 bfd_vma this_size;
1718 if (parse_int (&(ieee->h), &this_size))
1720 unsigned int i;
1722 for (i = 0; i < this_size; i++)
1724 location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1725 if (! next_byte (&(ieee->h)))
1726 return FALSE;
1729 else
1730 loop = FALSE;
1734 /* Prevent more than the first load-item of an LR record
1735 from being repeated (MRI convention). */
1736 if (iterations != 1)
1737 loop = FALSE;
1741 return TRUE;
1744 /* Read in all the section data and relocation stuff too. */
1746 static bfd_boolean
1747 ieee_slurp_section_data (bfd *abfd)
1749 bfd_byte *location_ptr = (bfd_byte *) NULL;
1750 ieee_data_type *ieee = IEEE_DATA (abfd);
1751 unsigned int section_number;
1752 ieee_per_section_type *current_map = NULL;
1753 asection *s;
1755 /* Seek to the start of the data area. */
1756 if (ieee->read_data)
1757 return TRUE;
1758 ieee->read_data = TRUE;
1760 if (! ieee_seek (ieee, ieee->w.r.data_part))
1761 return FALSE;
1763 /* Allocate enough space for all the section contents. */
1764 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1766 ieee_per_section_type *per = ieee_per_section (s);
1767 arelent **relpp;
1769 if ((s->flags & SEC_DEBUGGING) != 0)
1770 continue;
1771 per->data = bfd_alloc (ieee->h.abfd, s->size);
1772 if (!per->data)
1773 return FALSE;
1774 relpp = &s->relocation;
1775 per->reloc_tail_ptr = (ieee_reloc_type **) relpp;
1778 while (TRUE)
1780 switch (this_byte (&(ieee->h)))
1782 /* IF we see anything strange then quit. */
1783 default:
1784 return TRUE;
1786 case ieee_set_current_section_enum:
1787 if (! next_byte (&(ieee->h)))
1788 return FALSE;
1789 section_number = must_parse_int (&(ieee->h));
1790 s = ieee->section_table[section_number];
1791 s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
1792 current_map = ieee_per_section (s);
1793 location_ptr = current_map->data - s->vma;
1794 /* The document I have says that Microtec's compilers reset
1795 this after a sec section, even though the standard says not
1796 to, SO... */
1797 current_map->pc = s->vma;
1798 break;
1800 case ieee_e2_first_byte_enum:
1801 if (! next_byte (&(ieee->h)))
1802 return FALSE;
1803 switch (this_byte (&(ieee->h)))
1805 case ieee_set_current_pc_enum & 0xff:
1807 bfd_vma value;
1808 ieee_symbol_index_type symbol;
1809 unsigned int extra;
1810 bfd_boolean pcrel;
1812 if (! next_byte (&(ieee->h)))
1813 return FALSE;
1814 must_parse_int (&(ieee->h)); /* Throw away section #. */
1815 if (! parse_expression (ieee, &value,
1816 &symbol,
1817 &pcrel, &extra,
1819 return FALSE;
1821 current_map->pc = value;
1822 BFD_ASSERT ((unsigned) (value - s->vma) <= s->size);
1824 break;
1826 case ieee_value_starting_address_enum & 0xff:
1827 if (! next_byte (&(ieee->h)))
1828 return FALSE;
1829 if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum)
1831 if (! next_byte (&(ieee->h)))
1832 return FALSE;
1834 abfd->start_address = must_parse_int (&(ieee->h));
1835 /* We've got to the end of the data now - */
1836 return TRUE;
1837 default:
1838 BFD_FAIL ();
1839 return FALSE;
1841 break;
1842 case ieee_repeat_data_enum:
1844 /* Repeat the following LD or LR n times - we do this by
1845 remembering the stream pointer before running it and
1846 resetting it and running it n times. We special case
1847 the repetition of a repeat_data/load_constant. */
1848 unsigned int iterations;
1849 unsigned char *start;
1851 if (! next_byte (&(ieee->h)))
1852 return FALSE;
1853 iterations = must_parse_int (&(ieee->h));
1854 start = ieee->h.input_p;
1855 if (start[0] == (int) ieee_load_constant_bytes_enum
1856 && start[1] == 1)
1858 while (iterations != 0)
1860 location_ptr[current_map->pc++] = start[2];
1861 iterations--;
1863 (void) next_byte (&(ieee->h));
1864 (void) next_byte (&(ieee->h));
1865 if (! next_byte (&(ieee->h)))
1866 return FALSE;
1868 else
1870 while (iterations != 0)
1872 ieee->h.input_p = start;
1873 if (!do_one (ieee, current_map, location_ptr, s,
1874 (int) iterations))
1875 return FALSE;
1876 iterations--;
1880 break;
1881 case ieee_load_constant_bytes_enum:
1882 case ieee_load_with_relocation_enum:
1883 if (!do_one (ieee, current_map, location_ptr, s, 1))
1884 return FALSE;
1889 static const bfd_target *
1890 ieee_object_p (bfd *abfd)
1892 char *processor;
1893 unsigned int part;
1894 ieee_data_type *ieee;
1895 static unsigned char buffer[300];
1896 ieee_data_type *save = IEEE_DATA (abfd);
1897 bfd_size_type amt;
1899 abfd->tdata.ieee_data = 0;
1900 ieee_mkobject (abfd);
1902 ieee = IEEE_DATA (abfd);
1903 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1904 goto fail;
1905 /* Read the first few bytes in to see if it makes sense. Ignore
1906 bfd_bread return value; The file might be very small. */
1907 if (bfd_bread ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd) <= 0)
1908 goto got_wrong_format;
1910 ieee->h.input_p = buffer;
1911 ieee->h.total_amt = sizeof (buffer);
1912 ieee->h.end_p = buffer + sizeof (buffer);
1914 if (this_byte_and_next (&(ieee->h)) != Module_Beginning)
1915 goto got_wrong_format;
1917 ieee->read_symbols = FALSE;
1918 ieee->read_data = FALSE;
1919 ieee->section_count = 0;
1920 ieee->external_symbol_max_index = 0;
1921 ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
1922 ieee->external_reference_min_index = IEEE_REFERENCE_BASE;
1923 ieee->external_reference_max_index = 0;
1924 ieee->h.abfd = abfd;
1925 ieee->section_table = NULL;
1926 ieee->section_table_size = 0;
1928 processor = ieee->mb.processor = read_id (&(ieee->h));
1929 if (processor == NULL)
1930 goto got_wrong_format;
1931 if (strcmp (processor, "LIBRARY") == 0)
1932 goto got_wrong_format;
1933 ieee->mb.module_name = read_id (&(ieee->h));
1934 if (ieee->mb.module_name == NULL)
1935 goto got_wrong_format;
1936 if (abfd->filename == (const char *) NULL)
1937 abfd->filename = xstrdup (ieee->mb.module_name);
1939 /* Determine the architecture and machine type of the object file. */
1941 const bfd_arch_info_type *arch;
1942 char family[10];
1944 /* IEEE does not specify the format of the processor identification
1945 string, so the compiler is free to put in it whatever it wants.
1946 We try here to recognize different processors belonging to the
1947 m68k family. Code for other processors can be added here. */
1948 if ((processor[0] == '6') && (processor[1] == '8'))
1950 if (processor[2] == '3') /* 683xx integrated processors. */
1952 switch (processor[3])
1954 case '0': /* 68302, 68306, 68307 */
1955 case '2': /* 68322, 68328 */
1956 case '5': /* 68356 */
1957 strcpy (family, "68000"); /* MC68000-based controllers. */
1958 break;
1960 case '3': /* 68330, 68331, 68332, 68333,
1961 68334, 68335, 68336, 68338 */
1962 case '6': /* 68360 */
1963 case '7': /* 68376 */
1964 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1965 break;
1967 case '4':
1968 if (processor[4] == '9') /* 68349 */
1969 strcpy (family, "68030"); /* CPU030 */
1970 else /* 68340, 68341 */
1971 strcpy (family, "68332"); /* CPU32 and CPU32+ */
1972 break;
1974 default: /* Does not exist yet. */
1975 strcpy (family, "68332"); /* Guess it will be CPU32 */
1978 else if (TOUPPER (processor[3]) == 'F') /* 68F333 */
1979 strcpy (family, "68332"); /* CPU32 */
1980 else if ((TOUPPER (processor[3]) == 'C') /* Embedded controllers. */
1981 && ((TOUPPER (processor[2]) == 'E')
1982 || (TOUPPER (processor[2]) == 'H')
1983 || (TOUPPER (processor[2]) == 'L')))
1985 strcpy (family, "68");
1986 strncat (family, processor + 4, 7);
1987 family[9] = '\0';
1989 else /* "Regular" processors. */
1991 strncpy (family, processor, 9);
1992 family[9] = '\0';
1995 else if ((CONST_STRNEQ (processor, "cpu32")) /* CPU32 and CPU32+ */
1996 || (CONST_STRNEQ (processor, "CPU32")))
1997 strcpy (family, "68332");
1998 else
2000 strncpy (family, processor, 9);
2001 family[9] = '\0';
2004 arch = bfd_scan_arch (family);
2005 if (arch == 0)
2006 goto got_wrong_format;
2007 abfd->arch_info = arch;
2010 if (this_byte (&(ieee->h)) != (int) ieee_address_descriptor_enum)
2011 goto fail;
2013 if (! next_byte (&(ieee->h)))
2014 goto fail;
2016 if (! parse_int (&(ieee->h), &ieee->ad.number_of_bits_mau))
2017 goto fail;
2019 if (! parse_int (&(ieee->h), &ieee->ad.number_of_maus_in_address))
2020 goto fail;
2022 /* If there is a byte order info, take it. */
2023 if (this_byte (&(ieee->h)) == (int) ieee_variable_L_enum
2024 || this_byte (&(ieee->h)) == (int) ieee_variable_M_enum)
2026 if (! next_byte (&(ieee->h)))
2027 goto fail;
2030 for (part = 0; part < N_W_VARIABLES; part++)
2032 bfd_boolean ok;
2034 if (read_2bytes (&(ieee->h)) != (int) ieee_assign_value_to_variable_enum)
2035 goto fail;
2037 if (this_byte_and_next (&(ieee->h)) != part)
2038 goto fail;
2040 ieee->w.offset[part] = parse_i (&(ieee->h), &ok);
2041 if (! ok)
2042 goto fail;
2045 if (ieee->w.r.external_part != 0)
2046 abfd->flags = HAS_SYMS;
2048 /* By now we know that this is a real IEEE file, we're going to read
2049 the whole thing into memory so that we can run up and down it
2050 quickly. We can work out how big the file is from the trailer
2051 record. */
2053 amt = ieee->w.r.me_record + 1;
2054 IEEE_DATA (abfd)->h.first_byte = bfd_alloc (ieee->h.abfd, amt);
2055 if (!IEEE_DATA (abfd)->h.first_byte)
2056 goto fail;
2057 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2058 goto fail;
2060 /* FIXME: Check return value. I'm not sure whether it needs to read
2061 the entire buffer or not. */
2062 amt = bfd_bread ((void *) (IEEE_DATA (abfd)->h.first_byte),
2063 (bfd_size_type) ieee->w.r.me_record + 1, abfd);
2064 if (amt <= 0)
2065 goto fail;
2067 IEEE_DATA (abfd)->h.total_amt = amt;
2068 if (ieee_slurp_sections (abfd))
2069 goto fail;
2071 if (! ieee_slurp_debug (abfd))
2072 goto fail;
2074 /* Parse section data to activate file and section flags implied by
2075 section contents. */
2076 if (! ieee_slurp_section_data (abfd))
2077 goto fail;
2079 return abfd->xvec;
2080 got_wrong_format:
2081 bfd_set_error (bfd_error_wrong_format);
2082 fail:
2083 bfd_release (abfd, ieee);
2084 abfd->tdata.ieee_data = save;
2085 return (const bfd_target *) NULL;
2088 static void
2089 ieee_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2090 asymbol *symbol,
2091 symbol_info *ret)
2093 bfd_symbol_info (symbol, ret);
2094 if (symbol->name[0] == ' ')
2095 ret->name = "* empty table entry ";
2096 if (!symbol->section)
2097 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
2100 static void
2101 ieee_print_symbol (bfd *abfd,
2102 void * afile,
2103 asymbol *symbol,
2104 bfd_print_symbol_type how)
2106 FILE *file = (FILE *) afile;
2108 switch (how)
2110 case bfd_print_symbol_name:
2111 fprintf (file, "%s", symbol->name);
2112 break;
2113 case bfd_print_symbol_more:
2114 BFD_FAIL ();
2115 break;
2116 case bfd_print_symbol_all:
2118 const char *section_name =
2119 (symbol->section == (asection *) NULL
2120 ? "*abs"
2121 : symbol->section->name);
2123 if (symbol->name[0] == ' ')
2124 fprintf (file, "* empty table entry ");
2125 else
2127 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2129 fprintf (file, " %-5s %04x %02x %s",
2130 section_name,
2131 (unsigned) ieee_symbol (symbol)->index,
2132 (unsigned) 0,
2133 symbol->name);
2136 break;
2140 static bfd_boolean
2141 ieee_new_section_hook (bfd *abfd, asection *newsect)
2143 if (!newsect->used_by_bfd)
2145 newsect->used_by_bfd = bfd_alloc (abfd, sizeof (ieee_per_section_type));
2146 if (!newsect->used_by_bfd)
2147 return FALSE;
2149 ieee_per_section (newsect)->data = NULL;
2150 ieee_per_section (newsect)->section = newsect;
2151 return _bfd_generic_new_section_hook (abfd, newsect);
2154 static long
2155 ieee_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
2157 if ((asect->flags & SEC_DEBUGGING) != 0)
2158 return 0;
2159 if (! ieee_slurp_section_data (abfd))
2160 return -1;
2161 return (asect->reloc_count + 1) * sizeof (arelent *);
2164 static bfd_boolean
2165 ieee_get_section_contents (bfd *abfd,
2166 sec_ptr section,
2167 void * location,
2168 file_ptr offset,
2169 bfd_size_type count)
2171 ieee_per_section_type *p = ieee_per_section (section);
2172 if ((section->flags & SEC_DEBUGGING) != 0)
2173 return _bfd_generic_get_section_contents (abfd, section, location,
2174 offset, count);
2175 ieee_slurp_section_data (abfd);
2176 (void) memcpy ((void *) location, (void *) (p->data + offset), (unsigned) count);
2177 return TRUE;
2180 static long
2181 ieee_canonicalize_reloc (bfd *abfd,
2182 sec_ptr section,
2183 arelent **relptr,
2184 asymbol **symbols)
2186 ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation);
2187 ieee_data_type *ieee = IEEE_DATA (abfd);
2189 if ((section->flags & SEC_DEBUGGING) != 0)
2190 return 0;
2192 while (src != (ieee_reloc_type *) NULL)
2194 /* Work out which symbol to attach it this reloc to. */
2195 switch (src->symbol.letter)
2197 case 'I':
2198 src->relent.sym_ptr_ptr =
2199 symbols + src->symbol.index + ieee->external_symbol_base_offset;
2200 break;
2201 case 'X':
2202 src->relent.sym_ptr_ptr =
2203 symbols + src->symbol.index + ieee->external_reference_base_offset;
2204 break;
2205 case 0:
2206 if (src->relent.sym_ptr_ptr != NULL)
2207 src->relent.sym_ptr_ptr =
2208 src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr;
2209 break;
2210 default:
2212 BFD_FAIL ();
2214 *relptr++ = &src->relent;
2215 src = src->next;
2217 *relptr = NULL;
2218 return section->reloc_count;
2221 static int
2222 comp (const void * ap, const void * bp)
2224 arelent *a = *((arelent **) ap);
2225 arelent *b = *((arelent **) bp);
2226 return a->address - b->address;
2229 /* Write the section headers. */
2231 static bfd_boolean
2232 ieee_write_section_part (bfd *abfd)
2234 ieee_data_type *ieee = IEEE_DATA (abfd);
2235 asection *s;
2237 ieee->w.r.section_part = bfd_tell (abfd);
2238 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2240 if (! bfd_is_abs_section (s)
2241 && (s->flags & SEC_DEBUGGING) == 0)
2243 if (! ieee_write_byte (abfd, ieee_section_type_enum)
2244 || ! ieee_write_byte (abfd,
2245 (bfd_byte) (s->index
2246 + IEEE_SECTION_NUMBER_BASE)))
2247 return FALSE;
2249 if (abfd->flags & EXEC_P)
2251 /* This image is executable, so output absolute sections. */
2252 if (! ieee_write_byte (abfd, ieee_variable_A_enum)
2253 || ! ieee_write_byte (abfd, ieee_variable_S_enum))
2254 return FALSE;
2256 else
2258 if (! ieee_write_byte (abfd, ieee_variable_C_enum))
2259 return FALSE;
2262 switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM))
2264 case SEC_CODE | SEC_LOAD:
2265 case SEC_CODE:
2266 if (! ieee_write_byte (abfd, ieee_variable_P_enum))
2267 return FALSE;
2268 break;
2269 case SEC_DATA:
2270 default:
2271 if (! ieee_write_byte (abfd, ieee_variable_D_enum))
2272 return FALSE;
2273 break;
2274 case SEC_ROM:
2275 case SEC_ROM | SEC_DATA:
2276 case SEC_ROM | SEC_LOAD:
2277 case SEC_ROM | SEC_DATA | SEC_LOAD:
2278 if (! ieee_write_byte (abfd, ieee_variable_R_enum))
2279 return FALSE;
2283 if (! ieee_write_id (abfd, s->name))
2284 return FALSE;
2285 /* Alignment. */
2286 if (! ieee_write_byte (abfd, ieee_section_alignment_enum)
2287 || ! ieee_write_byte (abfd,
2288 (bfd_byte) (s->index
2289 + IEEE_SECTION_NUMBER_BASE))
2290 || ! ieee_write_int (abfd, (bfd_vma) 1 << s->alignment_power))
2291 return FALSE;
2293 /* Size. */
2294 if (! ieee_write_2bytes (abfd, ieee_section_size_enum)
2295 || ! ieee_write_byte (abfd,
2296 (bfd_byte) (s->index
2297 + IEEE_SECTION_NUMBER_BASE))
2298 || ! ieee_write_int (abfd, s->size))
2299 return FALSE;
2300 if (abfd->flags & EXEC_P)
2302 /* Relocateable sections don't have asl records. */
2303 /* Vma. */
2304 if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum)
2305 || ! ieee_write_byte (abfd,
2306 ((bfd_byte)
2307 (s->index
2308 + IEEE_SECTION_NUMBER_BASE)))
2309 || ! ieee_write_int (abfd, s->lma))
2310 return FALSE;
2315 return TRUE;
2318 static bfd_boolean
2319 do_with_relocs (bfd *abfd, asection *s)
2321 unsigned int number_of_maus_in_address =
2322 bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd);
2323 unsigned int relocs_to_go = s->reloc_count;
2324 bfd_byte *stream = ieee_per_section (s)->data;
2325 arelent **p = s->orelocation;
2326 bfd_size_type current_byte_index = 0;
2328 qsort (s->orelocation,
2329 relocs_to_go,
2330 sizeof (arelent **),
2331 comp);
2333 /* Output the section preheader. */
2334 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2335 || ! ieee_write_byte (abfd,
2336 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))
2337 || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum)
2338 || ! ieee_write_byte (abfd,
2339 (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)))
2340 return FALSE;
2342 if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0)
2344 if (! ieee_write_int (abfd, s->lma))
2345 return FALSE;
2347 else
2349 if (! ieee_write_expression (abfd, (bfd_vma) 0, s->symbol, 0, 0))
2350 return FALSE;
2353 if (relocs_to_go == 0)
2355 /* If there aren't any relocations then output the load constant
2356 byte opcode rather than the load with relocation opcode. */
2357 while (current_byte_index < s->size)
2359 bfd_size_type run;
2360 unsigned int MAXRUN = 127;
2362 run = MAXRUN;
2363 if (run > s->size - current_byte_index)
2364 run = s->size - current_byte_index;
2366 if (run != 0)
2368 if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum))
2369 return FALSE;
2370 /* Output a stream of bytes. */
2371 if (! ieee_write_int (abfd, run))
2372 return FALSE;
2373 if (bfd_bwrite ((void *) (stream + current_byte_index), run, abfd)
2374 != run)
2375 return FALSE;
2376 current_byte_index += run;
2380 else
2382 if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum))
2383 return FALSE;
2385 /* Output the data stream as the longest sequence of bytes
2386 possible, allowing for the a reasonable packet size and
2387 relocation stuffs. */
2388 if (stream == NULL)
2390 /* Outputting a section without data, fill it up. */
2391 stream = bfd_zalloc (abfd, s->size);
2392 if (!stream)
2393 return FALSE;
2395 while (current_byte_index < s->size)
2397 bfd_size_type run;
2398 unsigned int MAXRUN = 127;
2400 if (relocs_to_go)
2402 run = (*p)->address - current_byte_index;
2403 if (run > MAXRUN)
2404 run = MAXRUN;
2406 else
2407 run = MAXRUN;
2409 if (run > s->size - current_byte_index)
2410 run = s->size - current_byte_index;
2412 if (run != 0)
2414 /* Output a stream of bytes. */
2415 if (! ieee_write_int (abfd, run))
2416 return FALSE;
2417 if (bfd_bwrite ((void *) (stream + current_byte_index), run, abfd)
2418 != run)
2419 return FALSE;
2420 current_byte_index += run;
2423 /* Output any relocations here. */
2424 if (relocs_to_go && (*p) && (*p)->address == current_byte_index)
2426 while (relocs_to_go
2427 && (*p) && (*p)->address == current_byte_index)
2429 arelent *r = *p;
2430 bfd_signed_vma ov;
2431 switch (r->howto->size)
2433 case 2:
2434 ov = bfd_get_signed_32 (abfd,
2435 stream + current_byte_index);
2436 current_byte_index += 4;
2437 break;
2438 case 1:
2439 ov = bfd_get_signed_16 (abfd,
2440 stream + current_byte_index);
2441 current_byte_index += 2;
2442 break;
2443 case 0:
2444 ov = bfd_get_signed_8 (abfd,
2445 stream + current_byte_index);
2446 current_byte_index++;
2447 break;
2448 default:
2449 ov = 0;
2450 BFD_FAIL ();
2451 return FALSE;
2454 ov &= r->howto->src_mask;
2456 if (r->howto->pc_relative
2457 && ! r->howto->pcrel_offset)
2458 ov += r->address;
2460 if (! ieee_write_byte (abfd,
2461 ieee_function_either_open_b_enum))
2462 return FALSE;
2464 if (r->sym_ptr_ptr != (asymbol **) NULL)
2466 if (! ieee_write_expression (abfd, r->addend + ov,
2467 *(r->sym_ptr_ptr),
2468 r->howto->pc_relative,
2469 (unsigned) s->index))
2470 return FALSE;
2472 else
2474 if (! ieee_write_expression (abfd, r->addend + ov,
2475 (asymbol *) NULL,
2476 r->howto->pc_relative,
2477 (unsigned) s->index))
2478 return FALSE;
2481 if (number_of_maus_in_address
2482 != bfd_get_reloc_size (r->howto))
2484 bfd_vma rsize = bfd_get_reloc_size (r->howto);
2485 if (! ieee_write_int (abfd, rsize))
2486 return FALSE;
2488 if (! ieee_write_byte (abfd,
2489 ieee_function_either_close_b_enum))
2490 return FALSE;
2492 relocs_to_go--;
2493 p++;
2500 return TRUE;
2503 /* If there are no relocations in the output section then we can be
2504 clever about how we write. We block items up into a max of 127
2505 bytes. */
2507 static bfd_boolean
2508 do_as_repeat (bfd *abfd, asection *s)
2510 if (s->size)
2512 if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2513 || ! ieee_write_byte (abfd,
2514 (bfd_byte) (s->index
2515 + IEEE_SECTION_NUMBER_BASE))
2516 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8)
2517 || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff)
2518 || ! ieee_write_byte (abfd,
2519 (bfd_byte) (s->index
2520 + IEEE_SECTION_NUMBER_BASE)))
2521 return FALSE;
2523 if ((abfd->flags & EXEC_P) != 0)
2525 if (! ieee_write_int (abfd, s->lma))
2526 return FALSE;
2528 else
2530 if (! ieee_write_expression (abfd, (bfd_vma) 0, s->symbol, 0, 0))
2531 return FALSE;
2534 if (! ieee_write_byte (abfd, ieee_repeat_data_enum)
2535 || ! ieee_write_int (abfd, s->size)
2536 || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)
2537 || ! ieee_write_byte (abfd, 1)
2538 || ! ieee_write_byte (abfd, 0))
2539 return FALSE;
2542 return TRUE;
2545 static bfd_boolean
2546 do_without_relocs (bfd *abfd, asection *s)
2548 bfd_byte *stream = ieee_per_section (s)->data;
2550 if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
2552 if (! do_as_repeat (abfd, s))
2553 return FALSE;
2555 else
2557 unsigned int i;
2559 for (i = 0; i < s->size; i++)
2561 if (stream[i] != 0)
2563 if (! do_with_relocs (abfd, s))
2564 return FALSE;
2565 return TRUE;
2568 if (! do_as_repeat (abfd, s))
2569 return FALSE;
2572 return TRUE;
2575 static void
2576 fill (void)
2578 bfd_size_type amt = input_ptr_end - input_ptr_start;
2579 /* FIXME: Check return value. I'm not sure whether it needs to read
2580 the entire buffer or not. */
2581 bfd_bread ((void *) input_ptr_start, amt, input_bfd);
2582 input_ptr = input_ptr_start;
2585 static void
2586 flush (void)
2588 bfd_size_type amt = output_ptr - output_ptr_start;
2590 if (bfd_bwrite ((void *) (output_ptr_start), amt, output_bfd) != amt)
2591 abort ();
2592 output_ptr = output_ptr_start;
2593 output_buffer++;
2596 #define THIS() ( *input_ptr )
2597 #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill (); }
2598 #define OUT(x) { *output_ptr++ = (x); if (output_ptr == output_ptr_end) flush (); }
2600 static void
2601 write_int (int value)
2603 if (value >= 0 && value <= 127)
2605 OUT (value);
2607 else
2609 unsigned int length;
2611 /* How many significant bytes ? */
2612 /* FIXME FOR LONGER INTS. */
2613 if (value & 0xff000000)
2614 length = 4;
2615 else if (value & 0x00ff0000)
2616 length = 3;
2617 else if (value & 0x0000ff00)
2618 length = 2;
2619 else
2620 length = 1;
2622 OUT ((int) ieee_number_repeat_start_enum + length);
2623 switch (length)
2625 case 4:
2626 OUT (value >> 24);
2627 /* Fall through. */
2628 case 3:
2629 OUT (value >> 16);
2630 /* Fall through. */
2631 case 2:
2632 OUT (value >> 8);
2633 /* Fall through. */
2634 case 1:
2635 OUT (value);
2640 static void
2641 copy_id (void)
2643 int length = THIS ();
2644 char ch;
2646 OUT (length);
2647 NEXT ();
2648 while (length--)
2650 ch = THIS ();
2651 OUT (ch);
2652 NEXT ();
2656 #define VAR(x) ((x | 0x80))
2657 static void
2658 copy_expression (void)
2660 int stack[10];
2661 int *tos = stack;
2662 int value;
2664 while (1)
2666 switch (THIS ())
2668 case 0x84:
2669 NEXT ();
2670 value = THIS ();
2671 NEXT ();
2672 value = (value << 8) | THIS ();
2673 NEXT ();
2674 value = (value << 8) | THIS ();
2675 NEXT ();
2676 value = (value << 8) | THIS ();
2677 NEXT ();
2678 *tos++ = value;
2679 break;
2680 case 0x83:
2681 NEXT ();
2682 value = THIS ();
2683 NEXT ();
2684 value = (value << 8) | THIS ();
2685 NEXT ();
2686 value = (value << 8) | THIS ();
2687 NEXT ();
2688 *tos++ = value;
2689 break;
2690 case 0x82:
2691 NEXT ();
2692 value = THIS ();
2693 NEXT ();
2694 value = (value << 8) | THIS ();
2695 NEXT ();
2696 *tos++ = value;
2697 break;
2698 case 0x81:
2699 NEXT ();
2700 value = THIS ();
2701 NEXT ();
2702 *tos++ = value;
2703 break;
2704 case 0x80:
2705 NEXT ();
2706 *tos++ = 0;
2707 break;
2708 default:
2709 if (THIS () > 0x84)
2711 /* Not a number, just bug out with the answer. */
2712 write_int (*(--tos));
2713 return;
2715 *tos++ = THIS ();
2716 NEXT ();
2717 break;
2718 case 0xa5:
2719 /* PLUS anything. */
2720 value = *(--tos);
2721 value += *(--tos);
2722 *tos++ = value;
2723 NEXT ();
2724 break;
2725 case VAR ('R'):
2727 int section_number;
2728 ieee_data_type *ieee;
2729 asection *s;
2731 NEXT ();
2732 section_number = THIS ();
2734 NEXT ();
2735 ieee = IEEE_DATA (input_bfd);
2736 s = ieee->section_table[section_number];
2737 value = 0;
2738 if (s->output_section)
2739 value = s->output_section->lma;
2740 value += s->output_offset;
2741 *tos++ = value;
2743 break;
2744 case 0x90:
2746 NEXT ();
2747 write_int (*(--tos));
2748 OUT (0x90);
2749 return;
2755 /* Drop the int in the buffer, and copy a null into the gap, which we
2756 will overwrite later. */
2758 static void
2759 fill_int (struct output_buffer_struct *buf)
2761 if (buf->buffer == output_buffer)
2763 /* Still a chance to output the size. */
2764 int value = output_ptr - buf->ptrp + 3;
2765 buf->ptrp[0] = value >> 24;
2766 buf->ptrp[1] = value >> 16;
2767 buf->ptrp[2] = value >> 8;
2768 buf->ptrp[3] = value >> 0;
2772 static void
2773 drop_int (struct output_buffer_struct *buf)
2775 int type = THIS ();
2776 int ch;
2778 if (type <= 0x84)
2780 NEXT ();
2781 switch (type)
2783 case 0x84:
2784 ch = THIS ();
2785 NEXT ();
2786 /* Fall through. */
2787 case 0x83:
2788 ch = THIS ();
2789 NEXT ();
2790 /* Fall through. */
2791 case 0x82:
2792 ch = THIS ();
2793 NEXT ();
2794 /* Fall through. */
2795 case 0x81:
2796 ch = THIS ();
2797 NEXT ();
2798 /* Fall through. */
2799 case 0x80:
2800 break;
2803 (void) ch;
2804 OUT (0x84);
2805 buf->ptrp = output_ptr;
2806 buf->buffer = output_buffer;
2807 OUT (0);
2808 OUT (0);
2809 OUT (0);
2810 OUT (0);
2813 static void
2814 copy_int (void)
2816 int type = THIS ();
2817 int ch;
2818 if (type <= 0x84)
2820 OUT (type);
2821 NEXT ();
2822 switch (type)
2824 case 0x84:
2825 ch = THIS ();
2826 NEXT ();
2827 OUT (ch);
2828 /* Fall through. */
2829 case 0x83:
2830 ch = THIS ();
2831 NEXT ();
2832 OUT (ch);
2833 /* Fall through. */
2834 case 0x82:
2835 ch = THIS ();
2836 NEXT ();
2837 OUT (ch);
2838 /* Fall through. */
2839 case 0x81:
2840 ch = THIS ();
2841 NEXT ();
2842 OUT (ch);
2843 /* Fall through. */
2844 case 0x80:
2845 break;
2850 #define ID copy_id ()
2851 #define INT copy_int ()
2852 #define EXP copy_expression ()
2853 #define INTn(q) copy_int ()
2854 #define EXPn(q) copy_expression ()
2856 static void
2857 copy_till_end (void)
2859 int ch = THIS ();
2861 while (1)
2863 while (ch <= 0x80)
2865 OUT (ch);
2866 NEXT ();
2867 ch = THIS ();
2869 switch (ch)
2871 case 0x84:
2872 OUT (THIS ());
2873 NEXT ();
2874 /* Fall through. */
2875 case 0x83:
2876 OUT (THIS ());
2877 NEXT ();
2878 /* Fall through. */
2879 case 0x82:
2880 OUT (THIS ());
2881 NEXT ();
2882 /* Fall through. */
2883 case 0x81:
2884 OUT (THIS ());
2885 NEXT ();
2886 OUT (THIS ());
2887 NEXT ();
2889 ch = THIS ();
2890 break;
2891 default:
2892 return;
2898 static void
2899 f1_record (void)
2901 int ch;
2903 /* ATN record. */
2904 NEXT ();
2905 ch = THIS ();
2906 switch (ch)
2908 default:
2909 OUT (0xf1);
2910 OUT (ch);
2911 break;
2912 case 0xc9:
2913 NEXT ();
2914 OUT (0xf1);
2915 OUT (0xc9);
2916 INT;
2917 INT;
2918 ch = THIS ();
2919 switch (ch)
2921 case 0x16:
2922 NEXT ();
2923 break;
2924 case 0x01:
2925 NEXT ();
2926 break;
2927 case 0x00:
2928 NEXT ();
2929 INT;
2930 break;
2931 case 0x03:
2932 NEXT ();
2933 INT;
2934 break;
2935 case 0x13:
2936 EXPn (instruction address);
2937 break;
2938 default:
2939 break;
2941 break;
2942 case 0xd8:
2943 /* EXternal ref. */
2944 NEXT ();
2945 OUT (0xf1);
2946 OUT (0xd8);
2947 EXP;
2948 EXP;
2949 EXP;
2950 EXP;
2951 break;
2952 case 0xce:
2953 NEXT ();
2954 OUT (0xf1);
2955 OUT (0xce);
2956 INT;
2957 INT;
2958 ch = THIS ();
2959 INT;
2960 switch (ch)
2962 case 0x01:
2963 INT;
2964 INT;
2965 break;
2966 case 0x02:
2967 INT;
2968 break;
2969 case 0x04:
2970 EXPn (external function);
2971 break;
2972 case 0x05:
2973 break;
2974 case 0x07:
2975 INTn (line number);
2976 INT;
2977 case 0x08:
2978 break;
2979 case 0x0a:
2980 INTn (locked register);
2981 INT;
2982 break;
2983 case 0x3f:
2984 copy_till_end ();
2985 break;
2986 case 0x3e:
2987 copy_till_end ();
2988 break;
2989 case 0x40:
2990 copy_till_end ();
2991 break;
2992 case 0x41:
2994 break;
2999 static void
3000 f0_record (void)
3002 /* Attribute record. */
3003 NEXT ();
3004 OUT (0xf0);
3005 INTn (Symbol name);
3009 static void
3010 f2_record (void)
3012 NEXT ();
3013 OUT (0xf2);
3014 INT;
3015 NEXT ();
3016 OUT (0xce);
3017 INT;
3018 copy_till_end ();
3021 static void
3022 f8_record (void)
3024 int ch;
3025 NEXT ();
3026 ch = THIS ();
3027 switch (ch)
3029 case 0x01:
3030 case 0x02:
3031 case 0x03:
3032 /* Unique typedefs for module. */
3033 /* GLobal typedefs. */
3034 /* High level module scope beginning. */
3036 struct output_buffer_struct ob;
3038 NEXT ();
3039 OUT (0xf8);
3040 OUT (ch);
3041 drop_int (&ob);
3044 block ();
3046 NEXT ();
3047 fill_int (&ob);
3048 OUT (0xf9);
3050 break;
3051 case 0x04:
3052 /* Global function. */
3054 struct output_buffer_struct ob;
3056 NEXT ();
3057 OUT (0xf8);
3058 OUT (0x04);
3059 drop_int (&ob);
3061 INTn (stack size);
3062 INTn (ret val);
3063 EXPn (offset);
3065 block ();
3067 NEXT ();
3068 OUT (0xf9);
3069 EXPn (size of block);
3070 fill_int (&ob);
3072 break;
3074 case 0x05:
3075 /* File name for source line numbers. */
3077 struct output_buffer_struct ob;
3079 NEXT ();
3080 OUT (0xf8);
3081 OUT (0x05);
3082 drop_int (&ob);
3084 INTn (year);
3085 INTn (month);
3086 INTn (day);
3087 INTn (hour);
3088 INTn (monute);
3089 INTn (second);
3090 block ();
3091 NEXT ();
3092 OUT (0xf9);
3093 fill_int (&ob);
3095 break;
3097 case 0x06:
3098 /* Local function. */
3100 struct output_buffer_struct ob;
3102 NEXT ();
3103 OUT (0xf8);
3104 OUT (0x06);
3105 drop_int (&ob);
3107 INTn (stack size);
3108 INTn (type return);
3109 EXPn (offset);
3110 block ();
3111 NEXT ();
3112 OUT (0xf9);
3113 EXPn (size);
3114 fill_int (&ob);
3116 break;
3118 case 0x0a:
3119 /* Assembler module scope beginning - */
3121 struct output_buffer_struct ob;
3123 NEXT ();
3124 OUT (0xf8);
3125 OUT (0x0a);
3126 drop_int (&ob);
3129 INT;
3131 INT;
3132 INT;
3133 INT;
3134 INT;
3135 INT;
3136 INT;
3138 block ();
3140 NEXT ();
3141 OUT (0xf9);
3142 fill_int (&ob);
3144 break;
3145 case 0x0b:
3147 struct output_buffer_struct ob;
3149 NEXT ();
3150 OUT (0xf8);
3151 OUT (0x0b);
3152 drop_int (&ob);
3154 INT;
3155 INTn (section index);
3156 EXPn (offset);
3157 INTn (stuff);
3159 block ();
3161 OUT (0xf9);
3162 NEXT ();
3163 EXPn (Size in Maus);
3164 fill_int (&ob);
3166 break;
3170 static void
3171 e2_record (void)
3173 OUT (0xe2);
3174 NEXT ();
3175 OUT (0xce);
3176 NEXT ();
3177 INT;
3178 EXP;
3181 static void
3182 block (void)
3184 int ch;
3186 while (1)
3188 ch = THIS ();
3189 switch (ch)
3191 case 0xe1:
3192 case 0xe5:
3193 return;
3194 case 0xf9:
3195 return;
3196 case 0xf0:
3197 f0_record ();
3198 break;
3199 case 0xf1:
3200 f1_record ();
3201 break;
3202 case 0xf2:
3203 f2_record ();
3204 break;
3205 case 0xf8:
3206 f8_record ();
3207 break;
3208 case 0xe2:
3209 e2_record ();
3210 break;
3216 /* Moves all the debug information from the source bfd to the output
3217 bfd, and relocates any expressions it finds. */
3219 static void
3220 relocate_debug (bfd *output ATTRIBUTE_UNUSED,
3221 bfd *input)
3223 #define IBS 400
3224 #define OBS 400
3225 unsigned char input_buffer[IBS];
3227 input_ptr_start = input_ptr = input_buffer;
3228 input_ptr_end = input_buffer + IBS;
3229 input_bfd = input;
3230 /* FIXME: Check return value. I'm not sure whether it needs to read
3231 the entire buffer or not. */
3232 bfd_bread ((void *) input_ptr_start, (bfd_size_type) IBS, input);
3233 block ();
3236 /* Gather together all the debug information from each input BFD into
3237 one place, relocating it and emitting it as we go. */
3239 static bfd_boolean
3240 ieee_write_debug_part (bfd *abfd)
3242 ieee_data_type *ieee = IEEE_DATA (abfd);
3243 bfd_chain_type *chain = ieee->chain_root;
3244 unsigned char obuff[OBS];
3245 bfd_boolean some_debug = FALSE;
3246 file_ptr here = bfd_tell (abfd);
3248 output_ptr_start = output_ptr = obuff;
3249 output_ptr_end = obuff + OBS;
3250 output_ptr = obuff;
3251 output_bfd = abfd;
3253 if (chain == (bfd_chain_type *) NULL)
3255 asection *s;
3257 for (s = abfd->sections; s != NULL; s = s->next)
3258 if ((s->flags & SEC_DEBUGGING) != 0)
3259 break;
3260 if (s == NULL)
3262 ieee->w.r.debug_information_part = 0;
3263 return TRUE;
3266 ieee->w.r.debug_information_part = here;
3267 if (bfd_bwrite (s->contents, s->size, abfd) != s->size)
3268 return FALSE;
3270 else
3272 while (chain != (bfd_chain_type *) NULL)
3274 bfd *entry = chain->this;
3275 ieee_data_type *entry_ieee = IEEE_DATA (entry);
3277 if (entry_ieee->w.r.debug_information_part)
3279 if (bfd_seek (entry, entry_ieee->w.r.debug_information_part,
3280 SEEK_SET) != 0)
3281 return FALSE;
3282 relocate_debug (abfd, entry);
3285 chain = chain->next;
3288 if (some_debug)
3289 ieee->w.r.debug_information_part = here;
3290 else
3291 ieee->w.r.debug_information_part = 0;
3293 flush ();
3296 return TRUE;
3299 /* Write the data in an ieee way. */
3301 static bfd_boolean
3302 ieee_write_data_part (bfd *abfd)
3304 asection *s;
3306 ieee_data_type *ieee = IEEE_DATA (abfd);
3307 ieee->w.r.data_part = bfd_tell (abfd);
3309 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3311 /* Skip sections that have no loadable contents (.bss,
3312 debugging, etc.) */
3313 if ((s->flags & SEC_LOAD) == 0)
3314 continue;
3316 /* Sort the reloc records so we can insert them in the correct
3317 places. */
3318 if (s->reloc_count != 0)
3320 if (! do_with_relocs (abfd, s))
3321 return FALSE;
3323 else
3325 if (! do_without_relocs (abfd, s))
3326 return FALSE;
3330 return TRUE;
3333 static bfd_boolean
3334 init_for_output (bfd *abfd)
3336 asection *s;
3338 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3340 if ((s->flags & SEC_DEBUGGING) != 0)
3341 continue;
3342 if (s->size != 0)
3344 bfd_size_type size = s->size;
3345 ieee_per_section (s)->data = bfd_alloc (abfd, size);
3346 if (!ieee_per_section (s)->data)
3347 return FALSE;
3350 return TRUE;
3353 /* Exec and core file sections. */
3355 /* Set section contents is complicated with IEEE since the format is
3356 not a byte image, but a record stream. */
3358 static bfd_boolean
3359 ieee_set_section_contents (bfd *abfd,
3360 sec_ptr section,
3361 const void * location,
3362 file_ptr offset,
3363 bfd_size_type count)
3365 if ((section->flags & SEC_DEBUGGING) != 0)
3367 if (section->contents == NULL)
3369 bfd_size_type size = section->size;
3370 section->contents = bfd_alloc (abfd, size);
3371 if (section->contents == NULL)
3372 return FALSE;
3374 /* bfd_set_section_contents has already checked that everything
3375 is within range. */
3376 memcpy (section->contents + offset, location, (size_t) count);
3377 return TRUE;
3380 if (ieee_per_section (section)->data == (bfd_byte *) NULL)
3382 if (!init_for_output (abfd))
3383 return FALSE;
3385 memcpy ((void *) (ieee_per_section (section)->data + offset),
3386 (void *) location,
3387 (unsigned int) count);
3388 return TRUE;
3391 /* Write the external symbols of a file. IEEE considers two sorts of
3392 external symbols, public, and referenced. It uses to internal
3393 forms to index them as well. When we write them out we turn their
3394 symbol values into indexes from the right base. */
3396 static bfd_boolean
3397 ieee_write_external_part (bfd *abfd)
3399 asymbol **q;
3400 ieee_data_type *ieee = IEEE_DATA (abfd);
3401 unsigned int reference_index = IEEE_REFERENCE_BASE;
3402 unsigned int public_index = IEEE_PUBLIC_BASE + 2;
3403 file_ptr here = bfd_tell (abfd);
3404 bfd_boolean hadone = FALSE;
3406 if (abfd->outsymbols != (asymbol **) NULL)
3409 for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++)
3411 asymbol *p = *q;
3413 if (bfd_is_und_section (p->section))
3415 /* This must be a symbol reference. */
3416 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3417 || ! ieee_write_int (abfd, (bfd_vma) reference_index)
3418 || ! ieee_write_id (abfd, p->name))
3419 return FALSE;
3420 p->value = reference_index;
3421 reference_index++;
3422 hadone = TRUE;
3424 else if (bfd_is_com_section (p->section))
3426 /* This is a weak reference. */
3427 if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3428 || ! ieee_write_int (abfd, (bfd_vma) reference_index)
3429 || ! ieee_write_id (abfd, p->name)
3430 || ! ieee_write_byte (abfd,
3431 ieee_weak_external_reference_enum)
3432 || ! ieee_write_int (abfd, (bfd_vma) reference_index)
3433 || ! ieee_write_int (abfd, p->value))
3434 return FALSE;
3435 p->value = reference_index;
3436 reference_index++;
3437 hadone = TRUE;
3439 else if (p->flags & BSF_GLOBAL)
3441 /* This must be a symbol definition. */
3442 if (! ieee_write_byte (abfd, ieee_external_symbol_enum)
3443 || ! ieee_write_int (abfd, (bfd_vma) public_index)
3444 || ! ieee_write_id (abfd, p->name)
3445 || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum)
3446 || ! ieee_write_int (abfd, (bfd_vma) public_index)
3447 || ! ieee_write_byte (abfd, 15) /* Instruction address. */
3448 || ! ieee_write_byte (abfd, 19) /* Static symbol. */
3449 || ! ieee_write_byte (abfd, 1)) /* One of them. */
3450 return FALSE;
3452 /* Write out the value. */
3453 if (! ieee_write_2bytes (abfd, ieee_value_record_enum)
3454 || ! ieee_write_int (abfd, (bfd_vma) public_index))
3455 return FALSE;
3456 if (! bfd_is_abs_section (p->section))
3458 if (abfd->flags & EXEC_P)
3460 /* If fully linked, then output all symbols
3461 relocated. */
3462 if (! (ieee_write_int
3463 (abfd,
3464 (p->value
3465 + p->section->output_offset
3466 + p->section->output_section->vma))))
3467 return FALSE;
3469 else
3471 if (! (ieee_write_expression
3472 (abfd,
3473 p->value + p->section->output_offset,
3474 p->section->output_section->symbol,
3475 FALSE, 0)))
3476 return FALSE;
3479 else
3481 if (! ieee_write_expression (abfd,
3482 p->value,
3483 bfd_abs_section_ptr->symbol,
3484 FALSE, 0))
3485 return FALSE;
3487 p->value = public_index;
3488 public_index++;
3489 hadone = TRUE;
3491 else
3493 /* This can happen - when there are gaps in the symbols read
3494 from an input ieee file. */
3498 if (hadone)
3499 ieee->w.r.external_part = here;
3501 return TRUE;
3505 static const unsigned char exten[] =
3507 0xf0, 0x20, 0x00,
3508 0xf1, 0xce, 0x20, 0x00, 37, 3, 3, /* Set version 3 rev 3. */
3509 0xf1, 0xce, 0x20, 0x00, 39, 2, /* Keep symbol in original case. */
3510 0xf1, 0xce, 0x20, 0x00, 38 /* Set object type relocatable to x. */
3513 static const unsigned char envi[] =
3515 0xf0, 0x21, 0x00,
3517 /* 0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
3518 0x19, 0x2c,
3520 0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok. */
3522 0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix. */
3523 /* 0xf1, 0xce, 0x21, 0, 54, 2,1,1 tool & version # */
3526 static bfd_boolean
3527 ieee_write_me_part (bfd *abfd)
3529 ieee_data_type *ieee = IEEE_DATA (abfd);
3530 ieee->w.r.trailer_part = bfd_tell (abfd);
3531 if (abfd->start_address)
3533 if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum)
3534 || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum)
3535 || ! ieee_write_int (abfd, abfd->start_address)
3536 || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum))
3537 return FALSE;
3539 ieee->w.r.me_record = bfd_tell (abfd);
3540 if (! ieee_write_byte (abfd, ieee_module_end_enum))
3541 return FALSE;
3542 return TRUE;
3545 /* Write out the IEEE processor ID. */
3547 static bfd_boolean
3548 ieee_write_processor (bfd *abfd)
3550 const bfd_arch_info_type *arch;
3552 arch = bfd_get_arch_info (abfd);
3553 switch (arch->arch)
3555 default:
3556 if (! ieee_write_id (abfd, bfd_printable_name (abfd)))
3557 return FALSE;
3558 break;
3560 case bfd_arch_h8300:
3561 if (! ieee_write_id (abfd, "H8/300"))
3562 return FALSE;
3563 break;
3565 case bfd_arch_h8500:
3566 if (! ieee_write_id (abfd, "H8/500"))
3567 return FALSE;
3568 break;
3570 case bfd_arch_i960:
3571 switch (arch->mach)
3573 default:
3574 case bfd_mach_i960_core:
3575 case bfd_mach_i960_ka_sa:
3576 if (! ieee_write_id (abfd, "80960KA"))
3577 return FALSE;
3578 break;
3580 case bfd_mach_i960_kb_sb:
3581 if (! ieee_write_id (abfd, "80960KB"))
3582 return FALSE;
3583 break;
3585 case bfd_mach_i960_ca:
3586 if (! ieee_write_id (abfd, "80960CA"))
3587 return FALSE;
3588 break;
3590 case bfd_mach_i960_mc:
3591 case bfd_mach_i960_xa:
3592 if (! ieee_write_id (abfd, "80960MC"))
3593 return FALSE;
3594 break;
3596 break;
3598 case bfd_arch_m68k:
3600 const char *id;
3602 switch (arch->mach)
3604 default: id = "68020"; break;
3605 case bfd_mach_m68000: id = "68000"; break;
3606 case bfd_mach_m68008: id = "68008"; break;
3607 case bfd_mach_m68010: id = "68010"; break;
3608 case bfd_mach_m68020: id = "68020"; break;
3609 case bfd_mach_m68030: id = "68030"; break;
3610 case bfd_mach_m68040: id = "68040"; break;
3611 case bfd_mach_m68060: id = "68060"; break;
3612 case bfd_mach_cpu32: id = "cpu32"; break;
3613 case bfd_mach_mcf_isa_a_nodiv: id = "isa-a:nodiv"; break;
3614 case bfd_mach_mcf_isa_a: id = "isa-a"; break;
3615 case bfd_mach_mcf_isa_a_mac: id = "isa-a:mac"; break;
3616 case bfd_mach_mcf_isa_a_emac: id = "isa-a:emac"; break;
3617 case bfd_mach_mcf_isa_aplus: id = "isa-aplus"; break;
3618 case bfd_mach_mcf_isa_aplus_mac: id = "isa-aplus:mac"; break;
3619 case bfd_mach_mcf_isa_aplus_emac: id = "isa-aplus:mac"; break;
3620 case bfd_mach_mcf_isa_b_nousp: id = "isa-b:nousp"; break;
3621 case bfd_mach_mcf_isa_b_nousp_mac: id = "isa-b:nousp:mac"; break;
3622 case bfd_mach_mcf_isa_b_nousp_emac: id = "isa-b:nousp:emac"; break;
3623 case bfd_mach_mcf_isa_b: id = "isa-b"; break;
3624 case bfd_mach_mcf_isa_b_mac: id = "isa-b:mac"; break;
3625 case bfd_mach_mcf_isa_b_emac: id = "isa-b:emac"; break;
3626 case bfd_mach_mcf_isa_b_float: id = "isa-b:float"; break;
3627 case bfd_mach_mcf_isa_b_float_mac: id = "isa-b:float:mac"; break;
3628 case bfd_mach_mcf_isa_b_float_emac: id = "isa-b:float:emac"; break;
3629 case bfd_mach_mcf_isa_c: id = "isa-c"; break;
3630 case bfd_mach_mcf_isa_c_mac: id = "isa-c:mac"; break;
3631 case bfd_mach_mcf_isa_c_emac: id = "isa-c:emac"; break;
3632 case bfd_mach_mcf_isa_c_nodiv: id = "isa-c:nodiv"; break;
3633 case bfd_mach_mcf_isa_c_nodiv_mac: id = "isa-c:nodiv:mac"; break;
3634 case bfd_mach_mcf_isa_c_nodiv_emac: id = "isa-c:nodiv:emac"; break;
3637 if (! ieee_write_id (abfd, id))
3638 return FALSE;
3640 break;
3643 return TRUE;
3646 static bfd_boolean
3647 ieee_write_object_contents (bfd *abfd)
3649 ieee_data_type *ieee = IEEE_DATA (abfd);
3650 unsigned int i;
3651 file_ptr old;
3653 /* Fast forward over the header area. */
3654 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3655 return FALSE;
3657 if (! ieee_write_byte (abfd, ieee_module_beginning_enum)
3658 || ! ieee_write_processor (abfd)
3659 || ! ieee_write_id (abfd, abfd->filename))
3660 return FALSE;
3662 /* Fast forward over the variable bits. */
3663 if (! ieee_write_byte (abfd, ieee_address_descriptor_enum))
3664 return FALSE;
3666 /* Bits per MAU. */
3667 if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd))))
3668 return FALSE;
3669 /* MAU's per address. */
3670 if (! ieee_write_byte (abfd,
3671 (bfd_byte) (bfd_arch_bits_per_address (abfd)
3672 / bfd_arch_bits_per_byte (abfd))))
3673 return FALSE;
3675 old = bfd_tell (abfd);
3676 if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0)
3677 return FALSE;
3679 ieee->w.r.extension_record = bfd_tell (abfd);
3680 if (bfd_bwrite ((char *) exten, (bfd_size_type) sizeof (exten), abfd)
3681 != sizeof (exten))
3682 return FALSE;
3683 if (abfd->flags & EXEC_P)
3685 if (! ieee_write_byte (abfd, 0x1)) /* Absolute. */
3686 return FALSE;
3688 else
3690 if (! ieee_write_byte (abfd, 0x2)) /* Relocateable. */
3691 return FALSE;
3694 ieee->w.r.environmental_record = bfd_tell (abfd);
3695 if (bfd_bwrite ((char *) envi, (bfd_size_type) sizeof (envi), abfd)
3696 != sizeof (envi))
3697 return FALSE;
3699 /* The HP emulator database requires a timestamp in the file. */
3701 time_t now;
3702 const struct tm *t;
3704 time (&now);
3705 t = (struct tm *) localtime (&now);
3706 if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum)
3707 || ! ieee_write_byte (abfd, 0x21)
3708 || ! ieee_write_byte (abfd, 0)
3709 || ! ieee_write_byte (abfd, 50)
3710 || ! ieee_write_int (abfd, (bfd_vma) (t->tm_year + 1900))
3711 || ! ieee_write_int (abfd, (bfd_vma) (t->tm_mon + 1))
3712 || ! ieee_write_int (abfd, (bfd_vma) t->tm_mday)
3713 || ! ieee_write_int (abfd, (bfd_vma) t->tm_hour)
3714 || ! ieee_write_int (abfd, (bfd_vma) t->tm_min)
3715 || ! ieee_write_int (abfd, (bfd_vma) t->tm_sec))
3716 return FALSE;
3719 output_bfd = abfd;
3721 flush ();
3723 if (! ieee_write_section_part (abfd))
3724 return FALSE;
3725 /* First write the symbols. This changes their values into table
3726 indeces so we cant use it after this point. */
3727 if (! ieee_write_external_part (abfd))
3728 return FALSE;
3730 /* Write any debugs we have been told about. */
3731 if (! ieee_write_debug_part (abfd))
3732 return FALSE;
3734 /* Can only write the data once the symbols have been written, since
3735 the data contains relocation information which points to the
3736 symbols. */
3737 if (! ieee_write_data_part (abfd))
3738 return FALSE;
3740 /* At the end we put the end! */
3741 if (! ieee_write_me_part (abfd))
3742 return FALSE;
3744 /* Generate the header. */
3745 if (bfd_seek (abfd, old, SEEK_SET) != 0)
3746 return FALSE;
3748 for (i = 0; i < N_W_VARIABLES; i++)
3750 if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum)
3751 || ! ieee_write_byte (abfd, (bfd_byte) i)
3752 || ! ieee_write_int5_out (abfd, (bfd_vma) ieee->w.offset[i]))
3753 return FALSE;
3756 return TRUE;
3759 /* Native-level interface to symbols. */
3761 /* We read the symbols into a buffer, which is discarded when this
3762 function exits. We read the strings into a buffer large enough to
3763 hold them all plus all the cached symbol entries. */
3765 static asymbol *
3766 ieee_make_empty_symbol (bfd *abfd)
3768 bfd_size_type amt = sizeof (ieee_symbol_type);
3769 ieee_symbol_type *new_symbol = (ieee_symbol_type *) bfd_zalloc (abfd, amt);
3771 if (!new_symbol)
3772 return NULL;
3773 new_symbol->symbol.the_bfd = abfd;
3774 return &new_symbol->symbol;
3777 static bfd *
3778 ieee_openr_next_archived_file (bfd *arch, bfd *prev)
3780 ieee_ar_data_type *ar = IEEE_AR_DATA (arch);
3782 /* Take the next one from the arch state, or reset. */
3783 if (prev == (bfd *) NULL)
3784 /* Reset the index - the first two entries are bogus. */
3785 ar->element_index = 2;
3787 while (TRUE)
3789 ieee_ar_obstack_type *p = ar->elements + ar->element_index;
3791 ar->element_index++;
3792 if (ar->element_index <= ar->element_count)
3794 if (p->file_offset != (file_ptr) 0)
3796 if (p->abfd == (bfd *) NULL)
3798 p->abfd = _bfd_create_empty_archive_element_shell (arch);
3799 p->abfd->origin = p->file_offset;
3801 return p->abfd;
3804 else
3806 bfd_set_error (bfd_error_no_more_archived_files);
3807 return NULL;
3812 #define ieee_find_nearest_line _bfd_nosymbols_find_nearest_line
3813 #define ieee_find_line _bfd_nosymbols_find_line
3814 #define ieee_find_inliner_info _bfd_nosymbols_find_inliner_info
3816 static int
3817 ieee_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
3819 ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL;
3820 ieee_data_type *ieee;
3822 if (abfd->my_archive != NULL)
3823 ar = abfd->my_archive->tdata.ieee_ar_data;
3824 if (ar == (ieee_ar_data_type *) NULL)
3826 bfd_set_error (bfd_error_invalid_operation);
3827 return -1;
3830 if (IEEE_DATA (abfd) == NULL)
3832 if (ieee_object_p (abfd) == NULL)
3834 bfd_set_error (bfd_error_wrong_format);
3835 return -1;
3839 ieee = IEEE_DATA (abfd);
3841 buf->st_size = ieee->w.r.me_record + 1;
3842 buf->st_mode = 0644;
3843 return 0;
3846 static int
3847 ieee_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
3848 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3850 return 0;
3853 #define ieee_close_and_cleanup _bfd_generic_close_and_cleanup
3854 #define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3856 #define ieee_slurp_armap bfd_true
3857 #define ieee_slurp_extended_name_table bfd_true
3858 #define ieee_construct_extended_name_table \
3859 ((bfd_boolean (*) \
3860 (bfd *, char **, bfd_size_type *, const char **)) \
3861 bfd_true)
3862 #define ieee_truncate_arname bfd_dont_truncate_arname
3863 #define ieee_write_armap \
3864 ((bfd_boolean (*) \
3865 (bfd *, unsigned int, struct orl *, unsigned int, int)) \
3866 bfd_true)
3867 #define ieee_read_ar_hdr bfd_nullvoidptr
3868 #define ieee_write_ar_hdr ((bfd_boolean (*) (bfd *, bfd *)) bfd_false)
3869 #define ieee_update_armap_timestamp bfd_true
3870 #define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
3872 #define ieee_get_symbol_version_string \
3873 _bfd_nosymbols_get_symbol_version_string
3874 #define ieee_bfd_is_target_special_symbol \
3875 ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
3876 #define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
3877 #define ieee_get_lineno _bfd_nosymbols_get_lineno
3878 #define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3879 #define ieee_read_minisymbols _bfd_generic_read_minisymbols
3880 #define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3882 #define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3883 #define ieee_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3885 #define ieee_set_arch_mach _bfd_generic_set_arch_mach
3887 #define ieee_get_section_contents_in_window \
3888 _bfd_generic_get_section_contents_in_window
3889 #define ieee_bfd_get_relocated_section_contents \
3890 bfd_generic_get_relocated_section_contents
3891 #define ieee_bfd_relax_section bfd_generic_relax_section
3892 #define ieee_bfd_gc_sections bfd_generic_gc_sections
3893 #define ieee_bfd_lookup_section_flags bfd_generic_lookup_section_flags
3894 #define ieee_bfd_merge_sections bfd_generic_merge_sections
3895 #define ieee_bfd_is_group_section bfd_generic_is_group_section
3896 #define ieee_bfd_discard_group bfd_generic_discard_group
3897 #define ieee_section_already_linked \
3898 _bfd_generic_section_already_linked
3899 #define ieee_bfd_define_common_symbol bfd_generic_define_common_symbol
3900 #define ieee_bfd_define_start_stop bfd_generic_define_start_stop
3901 #define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3902 #define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
3903 #define ieee_bfd_link_just_syms _bfd_generic_link_just_syms
3904 #define ieee_bfd_copy_link_hash_symbol_type \
3905 _bfd_generic_copy_link_hash_symbol_type
3906 #define ieee_bfd_final_link _bfd_generic_final_link
3907 #define ieee_bfd_link_split_section _bfd_generic_link_split_section
3908 #define ieee_bfd_link_check_relocs _bfd_generic_link_check_relocs
3909 #define ieee_set_reloc _bfd_generic_set_reloc
3911 const bfd_target ieee_vec =
3913 "ieee", /* Name. */
3914 bfd_target_ieee_flavour,
3915 BFD_ENDIAN_UNKNOWN, /* Target byte order. */
3916 BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
3917 (HAS_RELOC | EXEC_P | /* Object flags. */
3918 HAS_LINENO | HAS_DEBUG |
3919 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3920 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3921 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
3922 '_', /* Leading underscore. */
3923 ' ', /* AR_pad_char. */
3924 16, /* AR_max_namelen. */
3925 0, /* match priority. */
3926 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3927 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3928 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
3929 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3930 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3931 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
3933 {_bfd_dummy_target,
3934 ieee_object_p, /* bfd_check_format. */
3935 ieee_archive_p,
3936 _bfd_dummy_target,
3939 bfd_false,
3940 ieee_mkobject,
3941 _bfd_generic_mkarchive,
3942 bfd_false
3945 bfd_false,
3946 ieee_write_object_contents,
3947 _bfd_write_archive_contents,
3948 bfd_false,
3951 /* ieee_close_and_cleanup, ieee_bfd_free_cached_info, ieee_new_section_hook,
3952 ieee_get_section_contents, ieee_get_section_contents_in_window. */
3953 BFD_JUMP_TABLE_GENERIC (ieee),
3955 BFD_JUMP_TABLE_COPY (_bfd_generic),
3956 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3958 /* ieee_slurp_armap, ieee_slurp_extended_name_table,
3959 ieee_construct_extended_name_table, ieee_truncate_arname,
3960 ieee_write_armap, ieee_read_ar_hdr, ieee_openr_next_archived_file,
3961 ieee_get_elt_at_index, ieee_generic_stat_arch_elt,
3962 ieee_update_armap_timestamp. */
3963 BFD_JUMP_TABLE_ARCHIVE (ieee),
3965 /* ieee_get_symtab_upper_bound, ieee_canonicalize_symtab,
3966 ieee_make_empty_symbol, ieee_print_symbol, ieee_get_symbol_info,
3967 ieee_bfd_is_local_label_name, ieee_get_lineno,
3968 ieee_find_nearest_line, ieee_bfd_make_debug_symbol,
3969 ieee_read_minisymbols, ieee_minisymbol_to_symbol. */
3970 BFD_JUMP_TABLE_SYMBOLS (ieee),
3972 /* ieee_get_reloc_upper_bound, ieee_canonicalize_reloc,
3973 ieee_bfd_reloc_type_lookup. */
3974 BFD_JUMP_TABLE_RELOCS (ieee),
3976 /* ieee_set_arch_mach, ieee_set_section_contents. */
3977 BFD_JUMP_TABLE_WRITE (ieee),
3979 /* ieee_sizeof_headers, ieee_bfd_get_relocated_section_contents,
3980 ieee_bfd_relax_section, ieee_bfd_link_hash_table_create,
3981 ieee_bfd_link_add_symbols, ieee_bfd_final_link,
3982 ieee_bfd_link_split_section, ieee_bfd_gc_sections,
3983 ieee_bfd_merge_sections. */
3984 BFD_JUMP_TABLE_LINK (ieee),
3986 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3988 NULL,
3990 NULL