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