Moved in from p3:
[deliverable/binutils-gdb.git] / bfd / ieee.c
1 /* bfd back-end for ieee-695 objects.
2 Copyright (C) 1990-1991 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /* IEEE 695 format is a stream of records, which we parse using a simple one-
22 token (which is one byte in this lexicon) lookahead recursive decent
23 parser. */
24
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "ieee.h"
29 #include "libieee.h"
30
31 /* Functions for writing to ieee files in the strange way that the
32 standard requires. */
33
34 static void
35 DEFUN(ieee_write_byte,(abfd, byte),
36 bfd *abfd AND
37 bfd_byte byte)
38 {
39 bfd_write((PTR)&byte, 1, 1, abfd);
40 }
41
42
43 static void
44 DEFUN(ieee_write_twobyte,(abfd, twobyte),
45 bfd *abfd AND
46 int twobyte)
47 {
48 bfd_byte b[2];
49 b[1] = twobyte & 0xff;
50 b[0] = twobyte >> 8;
51 bfd_write((PTR)&b[0], 1, 2, abfd);
52 }
53
54
55
56 static void
57 DEFUN(ieee_write_2bytes,(abfd, bytes),
58 bfd *abfd AND
59 int bytes)
60 {
61 bfd_byte buffer[2];
62 buffer[0] = bytes >> 8;
63 buffer[1] = bytes & 0xff;
64
65 bfd_write((PTR)buffer, 1, 2, abfd);
66 }
67
68 static void
69 DEFUN(ieee_write_int,(abfd, value),
70 bfd *abfd AND
71 bfd_vma value)
72 {
73 if (((unsigned)value) <= 127) {
74 ieee_write_byte(abfd, value);
75 }
76 else {
77 unsigned int length;
78 /* How many significant bytes ? */
79 /* FIXME FOR LONGER INTS */
80 if (value & 0xff000000) {
81 length = 4;
82 }
83 else if (value & 0x00ff0000) {
84 length = 3;
85 }
86 else if (value & 0x0000ff00) {
87 length = 2;
88 }
89 else length = 1;
90
91 ieee_write_byte(abfd, (int)ieee_number_repeat_start_enum + length);
92 switch (length) {
93 case 4:
94 ieee_write_byte(abfd, value >> 24);
95 case 3:
96 ieee_write_byte(abfd, value >> 16);
97 case 2:
98 ieee_write_byte(abfd, value >> 8);
99 case 1:
100 ieee_write_byte(abfd, value);
101 }
102 }
103 }
104
105 static void
106 DEFUN(ieee_write_id,(abfd, id),
107 bfd *abfd AND
108 CONST char *id)
109 {
110 size_t length = strlen(id);
111 if (length >= 0 && length <= 127) {
112 ieee_write_byte(abfd, length);
113 }
114 else if (length < 255) {
115 ieee_write_byte(abfd, ieee_extension_length_1_enum);
116 ieee_write_byte(abfd, length);
117 }
118 else if (length < 65535) {
119 ieee_write_byte(abfd, ieee_extension_length_2_enum);
120 ieee_write_byte(abfd, length >> 8);
121 ieee_write_byte(abfd, length & 0xff);
122 }
123 else {
124 BFD_FAIL();
125 }
126 bfd_write((PTR)id, 1, length, abfd);
127 }
128 /***************************************************************************
129 Functions for reading from ieee files in the strange way that the
130 standard requires:
131 */
132
133
134 #define this_byte(ieee) *((ieee)->input_p)
135 #define next_byte(ieee) ((ieee)->input_p++)
136 #define this_byte_and_next(ieee) (*((ieee)->input_p++))
137
138
139 static unsigned short
140 DEFUN(read_2bytes,(ieee),
141 common_header_type *ieee)
142 {
143 unsigned char c1 = this_byte_and_next(ieee);
144 unsigned char c2 = this_byte_and_next(ieee);
145 return (c1<<8 ) | c2;
146
147 }
148
149 static void
150 DEFUN(bfd_get_string,(ieee, string, length),
151 common_header_type *ieee AND
152 char *string AND
153 size_t length)
154 {
155 size_t i;
156 for (i= 0; i < length; i++) {
157 string[i] = this_byte_and_next(ieee);
158 }
159 }
160
161 static char *
162 DEFUN(read_id,(ieee),
163 common_header_type *ieee)
164 {
165 size_t length;
166 char *string;
167 length = this_byte_and_next(ieee);
168 if (length >= 0x00 && length <= 0x7f) {
169 /* Simple string of length 0 to 127 */
170 }
171 else if (length == 0xde) {
172 /* Length is next byte, allowing 0..255 */
173 length = this_byte_and_next(ieee);
174 }
175 else if (length == 0xdf) {
176 /* Length is next two bytes, allowing 0..65535 */
177 length = this_byte_and_next(ieee) ;
178 length = (length * 256) + this_byte_and_next(ieee);
179 }
180 /* Buy memory and read string */
181 string = bfd_alloc(ieee->abfd, length+1);
182 bfd_get_string(ieee, string, length);
183 string[length] = 0;
184 return string;
185 }
186
187 static void
188 DEFUN(ieee_write_expression,(abfd, value, symbol, pcrel, index),
189 bfd*abfd AND
190 bfd_vma value AND
191 asymbol *symbol AND
192 boolean pcrel AND
193 unsigned int index)
194 {
195 unsigned int term_count = 0;
196
197 if (value != 0)
198 {
199 ieee_write_int(abfd, value);
200 term_count++;
201 }
202
203
204
205 if (symbol->section == &bfd_com_section
206 || symbol->section == &bfd_und_section)
207 {
208 /* Def of a common symbol */
209 ieee_write_byte(abfd, ieee_variable_X_enum);
210 ieee_write_int(abfd, symbol->value);
211 term_count++;
212 }
213 else if (symbol->section != &bfd_abs_section)
214 {
215 /* Ref to defined symbol - */
216
217 ieee_write_byte(abfd, ieee_variable_R_enum);
218 ieee_write_byte(abfd, symbol->section->index + IEEE_SECTION_NUMBER_BASE);
219 term_count++;
220 if (symbol->flags & BSF_GLOBAL)
221 {
222 ieee_write_byte(abfd, ieee_variable_I_enum);
223 ieee_write_int(abfd, symbol->value);
224 term_count++;
225 }
226 else if (symbol->flags & ( BSF_LOCAL | BSF_SECTION_SYM))
227 {
228 /* This is a reference to a defined local symbol,
229 We can easily do a local as a section+offset */
230 ieee_write_byte(abfd, ieee_variable_R_enum); /* or L */
231 ieee_write_byte(abfd, symbol->section->index +
232 IEEE_SECTION_NUMBER_BASE);
233 ieee_write_int(abfd, symbol->value);
234 term_count++;
235
236 }
237 else {
238 BFD_FAIL();
239 }
240
241 }
242
243
244
245 if(pcrel) {
246 /* subtract the pc from here by asking for PC of this section*/
247 ieee_write_byte(abfd, ieee_variable_P_enum);
248 ieee_write_byte(abfd, index +IEEE_SECTION_NUMBER_BASE);
249 ieee_write_byte(abfd, ieee_function_minus_enum);
250 }
251
252 if (term_count == 1)
253 {
254 ieee_write_byte(abfd,0);
255 }
256 else {
257 while (term_count > 1) {
258 ieee_write_byte(abfd, ieee_function_plus_enum);
259 term_count--;
260 }
261
262 }
263
264 }
265
266
267
268
269
270
271
272
273 /*****************************************************************************/
274
275 /*
276 writes any integer into the buffer supplied and always takes 5 bytes
277 */
278 static void
279 DEFUN(ieee_write_int5,(buffer, value),
280 bfd_byte*buffer AND
281 bfd_vma value )
282 {
283 buffer[0] = (bfd_byte)ieee_number_repeat_4_enum;
284 buffer[1] = (value >> 24 ) & 0xff;
285 buffer[2] = (value >> 16 ) & 0xff;
286 buffer[3] = (value >> 8 ) & 0xff;
287 buffer[4] = (value >> 0 ) & 0xff;
288 }
289 static void
290 DEFUN(ieee_write_int5_out, (abfd, value),
291 bfd *abfd AND
292 bfd_vma value)
293 {
294 bfd_byte b[5];
295 ieee_write_int5(b, value);
296 bfd_write((PTR)b,1,5,abfd);
297 }
298
299
300 static boolean
301 DEFUN(parse_int,(ieee, value_ptr),
302 common_header_type *ieee AND
303 bfd_vma *value_ptr)
304 {
305 int value = this_byte(ieee);
306 int result;
307 if (value >= 0 && value <= 127) {
308 *value_ptr = value;
309 next_byte(ieee);
310 return true;
311 }
312 else if (value >= 0x80 && value <= 0x88) {
313 unsigned int count = value & 0xf;
314 result = 0;
315 next_byte(ieee);
316 while (count) {
317 result =(result << 8) | this_byte_and_next(ieee);
318 count--;
319 }
320 *value_ptr = result;
321 return true;
322 }
323 return false;
324 }
325 static int
326 DEFUN(parse_i,(ieee, ok),
327 common_header_type *ieee AND
328 boolean *ok)
329 {
330 bfd_vma x;
331 *ok = parse_int(ieee, &x);
332 return x;
333 }
334
335 static bfd_vma
336 DEFUN(must_parse_int,(ieee),
337 common_header_type *ieee)
338 {
339 bfd_vma result;
340 BFD_ASSERT(parse_int(ieee, &result) == true);
341 return result;
342 }
343
344 typedef struct
345 {
346 bfd_vma value;
347 asection *section;
348 ieee_symbol_index_type symbol;
349 } ieee_value_type;
350
351
352 static
353 reloc_howto_type abs32_howto
354 = HOWTO(1,0,2,32,false,0,false,true,0,"abs32",true,0xffffffff, 0xffffffff,false);
355 static
356 reloc_howto_type abs16_howto
357 = HOWTO(1,0,1,16,false,0,false,true,0,"abs16",true,0x0000ffff, 0x0000ffff,false);
358
359 static
360 reloc_howto_type abs8_howto
361 = HOWTO(1,0,0,8,false,0,false,true,0,"abs8",true,0x000000ff, 0x000000ff,false);
362
363 static
364 reloc_howto_type rel32_howto
365 = HOWTO(1,0,2,32,true,0,false,true,0,"rel32",true,0xffffffff, 0xffffffff,true);
366 static
367 reloc_howto_type rel16_howto
368 = HOWTO(1,0,1,16,true,0,false,true,0,"rel16",true,0x0000ffff, 0x0000ffff,true);
369
370 static
371 reloc_howto_type rel8_howto
372 = HOWTO(1,0,0,8,true,0,false,true,0,"rel8",true,0x000000ff, 0x000000ff,true);
373
374
375 static ieee_symbol_index_type NOSYMBOL = { 0, 0};
376
377
378 static void
379 DEFUN(parse_expression,(ieee, value, symbol, pcrel, extra),
380 ieee_data_type *ieee AND
381 bfd_vma *value AND
382 ieee_symbol_index_type *symbol AND
383 boolean *pcrel AND
384 unsigned int *extra)
385
386 {
387 #define POS sp[1]
388 #define TOS sp[0]
389 #define NOS sp[-1]
390 #define INC sp++;
391 #define DEC sp--;
392
393
394 boolean loop = true;
395 ieee_value_type stack[10];
396
397 /* The stack pointer always points to the next unused location */
398 #define PUSH(x,y,z) TOS.symbol=x;TOS.section=y;TOS.value=z;INC;
399 #define POP(x,y,z) DEC;x=TOS.symbol;y=TOS.section;z=TOS.value;
400 ieee_value_type *sp = stack;
401
402 while (loop) {
403 switch (this_byte(&(ieee->h)))
404 {
405 case ieee_variable_P_enum:
406 /* P variable, current program counter for section n */
407 {
408 int section_n ;
409 next_byte(&(ieee->h));
410 *pcrel = true;
411 section_n = must_parse_int(&(ieee->h));
412 PUSH(NOSYMBOL, &bfd_abs_section,
413 TOS.value = ieee->section_table[section_n]->vma +
414 ieee_per_section(ieee->section_table[section_n])->pc);
415 break;
416 }
417 case ieee_variable_L_enum:
418 /* L variable address of section N */
419 next_byte(&(ieee->h));
420 PUSH(NOSYMBOL,ieee->section_table[must_parse_int(&(ieee->h))],0);
421 break;
422 case ieee_variable_R_enum:
423 /* R variable, logical address of section module */
424 /* FIXME, this should be different to L */
425 next_byte(&(ieee->h));
426 PUSH(NOSYMBOL,ieee->section_table[must_parse_int(&(ieee->h))],0);
427 break;
428 case ieee_variable_S_enum:
429 /* S variable, size in MAUS of section module */
430 next_byte(&(ieee->h));
431 PUSH(NOSYMBOL,
432 0,
433 ieee->section_table[must_parse_int(&(ieee->h))]->_raw_size);
434 break;
435 case ieee_variable_I_enum:
436 case ieee_variable_X_enum:
437 /* Push the address of external variable n */
438 {
439 ieee_symbol_index_type sy;
440 next_byte(&(ieee->h));
441 sy.index = (int)(must_parse_int(&(ieee->h))) ;
442 sy.letter = 'X';
443
444 PUSH(sy, &bfd_und_section, 0);
445 }
446 break;
447 case ieee_function_minus_enum:
448 {
449 bfd_vma value1, value2;
450 asection *section1, *section_dummy;
451 ieee_symbol_index_type sy;
452 next_byte(&(ieee->h));
453
454 POP(sy, section1, value1);
455 POP(sy, section_dummy, value2);
456 PUSH(sy, section1 ? section1 : section_dummy, value1-value2);
457 }
458 break;
459 case ieee_function_plus_enum:
460 {
461 bfd_vma value1, value2;
462 asection *section1;
463 asection *section2;
464 ieee_symbol_index_type sy1;
465 ieee_symbol_index_type sy2;
466 next_byte(&(ieee->h));
467
468 POP(sy1, section1, value1);
469 POP(sy2, section2, value2);
470 PUSH(sy1.letter ? sy1 : sy2, section1 ? section1: section2, value1+value2);
471 }
472 break;
473 default:
474 {
475 bfd_vma va;
476 BFD_ASSERT(this_byte(&(ieee->h)) < (int)ieee_variable_A_enum
477 || this_byte(&(ieee->h)) > (int)ieee_variable_Z_enum);
478 if (parse_int(&(ieee->h), &va))
479 {
480 PUSH(NOSYMBOL, &bfd_abs_section, va);
481 }
482 else {
483 /*
484 Thats all that we can understand. As far as I can see
485 there is a bug in the Microtec IEEE output which I'm
486 using to scan, whereby the comma operator is ommited
487 sometimes in an expression, giving expressions with too
488 many terms. We can tell if that's the case by ensuring
489 that sp == stack here. If not, then we've pushed
490 something too far, so we keep adding
491 */
492
493 while (sp != stack+1) {
494 asection *section1;
495 ieee_symbol_index_type sy1;
496 POP(sy1, section1, *extra);
497 }
498 {
499 asection *dummy;
500
501 POP(*symbol, dummy, *value);
502 }
503
504 loop = false;
505 }
506 }
507
508 }
509 }
510 }
511
512
513
514 #define ieee_seek(abfd, offset) \
515 IEEE_DATA(abfd)->h.input_p = IEEE_DATA(abfd)->h.first_byte + offset
516
517 #define ieee_pos(abfd) IEEE_DATA(abfd)->h.input_p -IEEE_DATA(abfd)->h.first_byte
518
519 static unsigned int last_index;
520
521 static ieee_symbol_type *
522 DEFUN(get_symbol,(abfd,
523 ieee,
524 last_symbol,
525 symbol_count,
526 pptr,
527 max_index
528 ),
529 bfd *abfd AND
530 ieee_data_type *ieee AND
531 ieee_symbol_type *last_symbol AND
532 unsigned int *symbol_count AND
533 ieee_symbol_type *** pptr AND
534 unsigned int *max_index
535 )
536 {
537 /* Need a new symbol */
538 unsigned int new_index = must_parse_int(&(ieee->h));
539 if (new_index != last_index) {
540 ieee_symbol_type * new_symbol = (ieee_symbol_type *)bfd_alloc(ieee->h.abfd,
541 sizeof(ieee_symbol_type));
542
543 new_symbol->index = new_index;
544 last_index = new_index;
545 ( *symbol_count)++;
546 ** pptr= new_symbol;
547 *pptr = &new_symbol->next;
548 if (new_index > *max_index) {
549 *max_index = new_index;
550 }
551 return new_symbol;
552 }
553 return last_symbol;
554 }
555 static void
556 DEFUN(ieee_slurp_external_symbols,(abfd),
557 bfd *abfd)
558 {
559 ieee_data_type *ieee = IEEE_DATA(abfd);
560 file_ptr offset = ieee->w.r.external_part;
561
562
563 ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
564 ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
565 ieee_symbol_type *symbol = (ieee_symbol_type *)NULL;
566 unsigned int symbol_count = 0;
567 boolean loop = true;
568 last_index = 0xffffff;
569 ieee->symbol_table_full = true;
570
571 ieee_seek(abfd, offset );
572
573 while (loop) {
574 switch (this_byte(&(ieee->h))) {
575 case ieee_nn_record:
576 next_byte(&(ieee->h));
577 symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
578 &prev_symbols_ptr,
579 &ieee->external_symbol_max_index);
580
581 symbol->symbol.the_bfd = abfd;
582 symbol->symbol.name = read_id(&(ieee->h));
583 symbol->symbol.udata = (PTR)NULL;
584 symbol->symbol.flags = BSF_NO_FLAGS;
585
586
587 break;
588 case ieee_external_symbol_enum:
589 next_byte(&(ieee->h));
590
591 symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
592 &prev_symbols_ptr,
593 &ieee->external_symbol_max_index);
594
595
596 BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
597
598 symbol->symbol.the_bfd = abfd;
599 symbol->symbol.name = read_id(&(ieee->h));
600 symbol->symbol.udata = (PTR)NULL;
601 symbol->symbol.flags = BSF_NO_FLAGS;
602 break;
603 case ieee_attribute_record_enum >> 8:
604 {
605 unsigned int symbol_name_index;
606 unsigned int symbol_type_index;
607 unsigned int symbol_attribute_def;
608 bfd_vma value;
609 next_byte(&(ieee->h)); /* Skip prefix */
610 next_byte(&(ieee->h));
611 symbol_name_index = must_parse_int(&(ieee->h));
612 symbol_type_index = must_parse_int(&(ieee->h));
613 symbol_attribute_def = must_parse_int(&(ieee->h));
614 switch (symbol_attribute_def) {
615 case 63:
616 /* Module misc; followed by two fields which describe the
617 current module block. The first fired is the type id
618 number, the second is the number of asn records
619 associated with the directive */
620 parse_int(&(ieee->h),&value);
621 parse_int(&(ieee->h),&value);
622 break;
623
624 default:
625 parse_int(&(ieee->h),&value);
626 break;
627 }
628 }
629 break;
630 case ieee_value_record_enum >> 8:
631 {
632 unsigned int symbol_name_index;
633 ieee_symbol_index_type symbol_ignore;
634 boolean pcrel_ignore;
635 unsigned int extra;
636 next_byte(&(ieee->h));
637 next_byte(&(ieee->h));
638
639 symbol_name_index = must_parse_int(&(ieee->h));
640 parse_expression(ieee,
641 &symbol->symbol.value,
642 &symbol->symbol.section,
643 &symbol_ignore,
644 &pcrel_ignore,
645 &extra);
646
647 symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
648
649 }
650 break;
651 case ieee_weak_external_reference_enum:
652 { bfd_vma size;
653 bfd_vma value ;
654 next_byte(&(ieee->h));
655 /* Throw away the external reference index */
656 (void)must_parse_int(&(ieee->h));
657 /* Fetch the default size if not resolved */
658 size = must_parse_int(&(ieee->h));
659 /* Fetch the defautlt value if available */
660 if ( parse_int(&(ieee->h), &value) == false) {
661 value = 0;
662 }
663 /* This turns into a common */
664 symbol->symbol.section = &bfd_com_section;
665 symbol->symbol.value = size;
666 }
667 break;
668
669 case ieee_external_reference_enum:
670 next_byte(&(ieee->h));
671
672 symbol = get_symbol(abfd, ieee, symbol, &symbol_count,
673 &prev_reference_ptr,
674 &ieee->external_reference_max_index);
675
676
677 symbol->symbol.the_bfd = abfd;
678 symbol->symbol.name = read_id(&(ieee->h));
679 symbol->symbol.udata = (PTR)NULL;
680 symbol->symbol.section = &bfd_und_section;
681 symbol->symbol.value = (bfd_vma)0;
682 symbol->symbol.flags = 0;
683
684 BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
685 break;
686
687 default:
688 loop = false;
689 }
690 }
691
692 if (ieee->external_symbol_max_index != 0) {
693 ieee->external_symbol_count =
694 ieee->external_symbol_max_index -
695 ieee->external_symbol_min_index + 1 ;
696 }
697 else {
698 ieee->external_symbol_count = 0;
699 }
700
701
702 if(ieee->external_reference_max_index != 0) {
703 ieee->external_reference_count =
704 ieee->external_reference_max_index -
705 ieee->external_reference_min_index + 1;
706 }
707 else {
708 ieee->external_reference_count = 0;
709 }
710
711 abfd->symcount =
712 ieee->external_reference_count + ieee->external_symbol_count;
713
714 if (symbol_count != abfd->symcount) {
715 /* There are gaps in the table -- */
716 ieee->symbol_table_full = false;
717 }
718
719
720 *prev_symbols_ptr = (ieee_symbol_type *)NULL;
721 *prev_reference_ptr = (ieee_symbol_type *)NULL;
722 }
723
724 static void
725 DEFUN(ieee_slurp_symbol_table,(abfd),
726 bfd *abfd)
727 {
728 if (IEEE_DATA(abfd)->read_symbols == false) {
729 ieee_slurp_external_symbols(abfd);
730 IEEE_DATA(abfd)->read_symbols= true;
731 }
732 }
733
734 unsigned int
735 DEFUN(ieee_get_symtab_upper_bound,(abfd),
736 bfd *abfd)
737 {
738 ieee_slurp_symbol_table (abfd);
739
740 return (abfd->symcount != 0) ?
741 (abfd->symcount+1) * (sizeof (ieee_symbol_type *)) : 0;
742 }
743
744 /*
745 Move from our internal lists to the canon table, and insert in
746 symbol index order
747 */
748
749 extern bfd_target ieee_vec;
750 unsigned int
751 DEFUN(ieee_get_symtab,(abfd, location),
752 bfd *abfd AND
753 asymbol **location)
754 {
755 ieee_symbol_type *symp;
756 static bfd dummy_bfd;
757 static asymbol empty_symbol =
758 { &dummy_bfd," ieee empty",(symvalue)0,BSF_DEBUGGING , &bfd_abs_section};
759
760 if (abfd->symcount) {
761
762
763
764
765 ieee_data_type *ieee = IEEE_DATA(abfd);
766 dummy_bfd.xvec= &ieee_vec;
767 ieee_slurp_symbol_table(abfd);
768
769 if (ieee->symbol_table_full == false) {
770 /* Arrgh - there are gaps in the table, run through and fill them */
771 /* up with pointers to a null place */
772 unsigned int i;
773 for (i= 0; i < abfd->symcount; i++) {
774 location[i] = &empty_symbol;
775 }
776 }
777
778
779 ieee->external_symbol_base_offset= - ieee->external_symbol_min_index;
780 for (symp = IEEE_DATA(abfd)->external_symbols;
781 symp != (ieee_symbol_type *)NULL;
782 symp = symp->next) {
783 /* Place into table at correct index locations */
784 location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
785
786 }
787
788 /* The external refs are indexed in a bit */
789 ieee->external_reference_base_offset =
790 - ieee->external_reference_min_index +ieee->external_symbol_count ;
791
792 for (symp = IEEE_DATA(abfd)->external_reference;
793 symp != (ieee_symbol_type *)NULL;
794 symp = symp->next) {
795 location[symp->index + ieee->external_reference_base_offset] =
796 &symp->symbol;
797
798 }
799
800
801
802 location[abfd->symcount] = (asymbol *)NULL;
803 }
804 return abfd->symcount;
805 }
806 static asection *
807 DEFUN(get_section_entry,(abfd, ieee,index),
808 bfd *abfd AND
809 ieee_data_type *ieee AND
810 unsigned int index)
811 {
812 if (ieee->section_table[index] == (asection *)NULL) {
813 asection *section = bfd_make_section(abfd, " tempname");
814 ieee->section_table[index] = section;
815 section->flags = SEC_NO_FLAGS;
816 section->target_index = index;
817 ieee->section_table[index] = section;
818 }
819 return ieee->section_table[index];
820 }
821
822 static void
823 DEFUN(ieee_slurp_sections,(abfd),
824 bfd *abfd)
825 {
826 ieee_data_type *ieee = IEEE_DATA(abfd);
827 file_ptr offset = ieee->w.r.section_part;
828
829 asection *section = (asection *)NULL;
830
831 if (offset != 0) {
832 bfd_byte section_type[3];
833 ieee_seek(abfd, offset);
834 while (true) {
835 switch (this_byte(&(ieee->h))) {
836 case ieee_section_type_enum:
837 {
838 unsigned int section_index ;
839 next_byte(&(ieee->h));
840 section_index = must_parse_int(&(ieee->h));
841 /* Fixme to be nice about a silly number of sections */
842 BFD_ASSERT(section_index < NSECTIONS);
843
844 section =get_section_entry(abfd, ieee, section_index);
845
846 section_type[0] = this_byte_and_next(&(ieee->h));
847 switch (section_type[0]) {
848 case 0xC1:
849 /* Normal attributes for absolute sections */
850 section_type[1] = this_byte(&(ieee->h));
851 section->flags = SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
852 switch(section_type[1]) {
853 case 0xD3:
854 next_byte(&(ieee->h));
855 section_type[2] = this_byte(&(ieee->h));
856 switch (section_type[2])
857 {
858 case 0xD0:
859 /* Normal code */
860 next_byte(&(ieee->h));
861 section->flags |= SEC_LOAD | SEC_CODE;
862 break;
863 case 0xC4:
864 next_byte(&(ieee->h));
865 section->flags |= SEC_LOAD | SEC_DATA;
866 /* Normal data */
867 break;
868 case 0xD2:
869 next_byte(&(ieee->h));
870 /* Normal rom data */
871 section->flags |= SEC_LOAD | SEC_ROM | SEC_DATA;
872 break;
873 default:
874 break;
875 }
876 }
877 break;
878 case 0xC3:
879 section_type[1] = this_byte(&(ieee->h));
880 section->flags = SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
881 switch (section_type[1]) {
882 case 0xD0:
883 /* Normal code */
884 next_byte(&(ieee->h));
885 section->flags |= SEC_LOAD | SEC_CODE;
886 break;
887 case 0xC4:
888 next_byte(&(ieee->h));
889 section->flags |= SEC_LOAD | SEC_DATA;
890 /* Normal data */
891 break;
892 case 0xD2:
893 next_byte(&(ieee->h));
894 /* Normal rom data */
895 section->flags |= SEC_LOAD | SEC_ROM | SEC_DATA;
896 break;
897 default:
898 break;
899 }
900 }
901 section->name = read_id(&(ieee->h));
902 { bfd_vma parent, brother, context;
903 parse_int(&(ieee->h), &parent);
904 parse_int(&(ieee->h), &brother);
905 parse_int(&(ieee->h), &context);
906 }
907
908
909 }
910 break;
911 case ieee_section_alignment_enum:
912 {
913 unsigned int section_index;
914 bfd_vma value;
915 asection *section;
916 next_byte(&(ieee->h));
917 section_index = must_parse_int(&ieee->h);
918 section = get_section_entry(abfd, ieee, section_index);
919 if (section_index > ieee->section_count) {
920 ieee->section_count = section_index;
921 }
922 section->alignment_power =
923 bfd_log2(must_parse_int(&ieee->h));
924 (void)parse_int(&(ieee->h), & value);
925 }
926 break;
927 case ieee_e2_first_byte_enum:
928 {
929 ieee_record_enum_type t = (ieee_record_enum_type)(read_2bytes(&(ieee->h)));
930
931 switch (t) {
932 case ieee_section_size_enum:
933 section = ieee->section_table[must_parse_int(&(ieee->h))];
934 section->_raw_size = must_parse_int(&(ieee->h));
935 break;
936 case ieee_physical_region_size_enum:
937 section = ieee->section_table[must_parse_int(&(ieee->h))];
938 section->_raw_size = must_parse_int(&(ieee->h));
939 break;
940 case ieee_region_base_address_enum:
941 section = ieee->section_table[must_parse_int(&(ieee->h))];
942 section->vma = must_parse_int(&(ieee->h));
943 break;
944 case ieee_mau_size_enum:
945 must_parse_int(&(ieee->h));
946 must_parse_int(&(ieee->h));
947 break;
948 case ieee_m_value_enum:
949 must_parse_int(&(ieee->h));
950 must_parse_int(&(ieee->h));
951 break;
952 case ieee_section_base_address_enum:
953 section = ieee->section_table[must_parse_int(&(ieee->h))];
954 section->vma = must_parse_int(&(ieee->h));
955 break;
956 case ieee_section_offset_enum:
957 (void) must_parse_int(&(ieee->h));
958 (void) must_parse_int(&(ieee->h));
959 break;
960 default:
961 return;
962 }
963 }
964 break;
965 default:
966 return;
967 }
968 }
969 }
970 }
971
972 /***********************************************************************
973 * archive stuff
974 */
975 bfd_target *
976 DEFUN(ieee_archive_p,(abfd),
977 bfd *abfd)
978 {
979 char *library;
980 boolean loop;
981
982 unsigned int i;
983 uint8e_type buffer[512];
984
985 int buffer_offset = 0;
986 ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
987 ieee_ar_data_type *ieee ;
988 abfd->tdata.ieee_ar_data = (ieee_ar_data_type *)bfd_alloc(abfd, sizeof(ieee_ar_data_type));
989 ieee= IEEE_AR_DATA(abfd);
990
991
992 bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
993
994 ieee->h.first_byte = buffer;
995 ieee->h.input_p = buffer;
996
997 ieee->h.abfd = abfd;
998
999 if (this_byte(&(ieee->h)) != Module_Beginning) {
1000 abfd->tdata.ieee_ar_data = save;
1001 return (bfd_target*)NULL;
1002 }
1003
1004
1005 next_byte(&(ieee->h));
1006 library= read_id(&(ieee->h));
1007 if (strcmp(library , "LIBRARY") != 0) {
1008 bfd_release(abfd, ieee);
1009 abfd->tdata.ieee_ar_data = save;
1010 return (bfd_target *)NULL;
1011 }
1012 /* Throw away the filename */
1013 free( read_id(&(ieee->h)));
1014 /* This must be an IEEE archive, so we'll buy some space to do
1015 things */
1016 ieee->element_count = 0;
1017 ieee->element_index = 0;
1018
1019 next_byte(&(ieee->h)); /* Drop the ad part */
1020 must_parse_int(&(ieee->h)); /* And the two dummy numbers */
1021 must_parse_int(&(ieee->h));
1022
1023 loop = true;
1024 /* Read the index of the BB table */
1025 while (loop) {
1026 ieee_ar_obstack_type t;
1027 int rec =read_2bytes(&(ieee->h));
1028 if (rec ==(int)ieee_assign_value_to_variable_enum) {
1029 int record_number = must_parse_int(&(ieee->h));
1030 t.file_offset = must_parse_int(&(ieee->h));
1031 t.abfd = (bfd *)NULL;
1032 ieee->element_count++;
1033 bfd_alloc_grow(abfd, (PTR)&t, sizeof(t));
1034
1035 /* Make sure that we don't go over the end of the buffer */
1036
1037 if (ieee_pos(abfd) > sizeof(buffer)/2) {
1038 /* Past half way, reseek and reprime */
1039 buffer_offset += ieee_pos(abfd);
1040 bfd_seek(abfd, buffer_offset, SEEK_SET);
1041 bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
1042 ieee->h.first_byte = buffer;
1043 ieee->h.input_p = buffer;
1044 }
1045 }
1046 else loop = false;
1047 }
1048
1049 ieee->elements = (ieee_ar_obstack_type *)bfd_alloc_finish(abfd);
1050
1051 /* Now scan the area again, and replace BB offsets with file */
1052 /* offsets */
1053
1054
1055 for (i = 2; i < ieee->element_count; i++) {
1056 bfd_seek(abfd, ieee->elements[i].file_offset, SEEK_SET);
1057 bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
1058 ieee->h.first_byte = buffer;
1059 ieee->h.input_p = buffer;
1060
1061 next_byte(&(ieee->h)); /* Drop F8 */
1062 next_byte(&(ieee->h)); /* Drop 14 */
1063 must_parse_int(&(ieee->h)); /* Drop size of block */
1064 if (must_parse_int(&(ieee->h)) != 0) {
1065 /* This object has been deleted */
1066 ieee->elements[i].file_offset = 0;
1067 }
1068 else {
1069 ieee->elements[i].file_offset = must_parse_int(&(ieee->h));
1070 }
1071 }
1072
1073 return abfd->xvec;
1074
1075 }
1076
1077 static boolean
1078 DEFUN(ieee_mkobject,(abfd),
1079 bfd *abfd)
1080 {
1081 abfd->tdata.ieee_data = (ieee_data_type *)bfd_zalloc(abfd,sizeof(ieee_data_type));
1082
1083
1084 return true;
1085 }
1086
1087 bfd_target *
1088 DEFUN(ieee_object_p,(abfd),
1089 bfd *abfd)
1090 {
1091 char *processor;
1092 unsigned int part;
1093 ieee_data_type *ieee;
1094 uint8e_type buffer[300];
1095 ieee_data_type *save = IEEE_DATA(abfd);
1096 abfd->tdata.ieee_data = 0;
1097 ieee_mkobject(abfd);
1098 ieee = IEEE_DATA(abfd);
1099
1100 /* Read the first few bytes in to see if it makes sense */
1101 bfd_read((PTR)buffer, 1, sizeof(buffer), abfd);
1102
1103 ieee->h.input_p = buffer;
1104 if (this_byte_and_next(&(ieee->h)) != Module_Beginning) goto fail;
1105
1106 ieee->read_symbols= false;
1107 ieee->read_data= false;
1108 ieee->section_count = 0;
1109 ieee->external_symbol_max_index = 0;
1110 ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
1111 ieee->external_reference_min_index =IEEE_REFERENCE_BASE;
1112 ieee->external_reference_max_index = 0;
1113 ieee->h.abfd = abfd;
1114 memset((PTR)ieee->section_table, 0, sizeof(ieee->section_table));
1115
1116 processor = ieee->mb.processor = read_id(&(ieee->h));
1117 if (strcmp(processor,"LIBRARY") == 0) goto fail;
1118 ieee->mb.module_name = read_id(&(ieee->h));
1119 if (abfd->filename == (CONST char *)NULL) {
1120 abfd->filename = ieee->mb.module_name;
1121 }
1122 /* Determine the architecture and machine type of the object file.
1123 */
1124 {
1125 bfd_arch_info_type *arch = bfd_scan_arch(processor);
1126 if (arch == 0) goto fail;
1127 abfd->arch_info = arch;
1128 }
1129
1130 if (this_byte(&(ieee->h)) != (int)ieee_address_descriptor_enum) {
1131 goto fail;
1132 }
1133 next_byte(&(ieee->h));
1134
1135 if (parse_int(&(ieee->h), &ieee->ad.number_of_bits_mau) == false) {
1136 goto fail;
1137 }
1138 if(parse_int(&(ieee->h), &ieee->ad.number_of_maus_in_address) == false) {
1139 goto fail;
1140 }
1141
1142 /* If there is a byte order info, take it */
1143 if (this_byte(&(ieee->h)) == (int)ieee_variable_L_enum ||
1144 this_byte(&(ieee->h)) == (int)ieee_variable_M_enum)
1145 next_byte(&(ieee->h));
1146
1147
1148 for (part = 0; part < N_W_VARIABLES; part++) {
1149 boolean ok;
1150 if (read_2bytes(&(ieee->h)) != (int) ieee_assign_value_to_variable_enum) {
1151 goto fail;
1152 }
1153 if (this_byte_and_next(&(ieee->h)) != part) {
1154 goto fail;
1155 }
1156
1157 ieee->w.offset[part] = parse_i(&(ieee->h), &ok);
1158 if (ok==false) {
1159 goto fail;
1160 }
1161
1162 }
1163 abfd->flags = HAS_SYMS;
1164
1165 /* By now we know that this is a real IEEE file, we're going to read
1166 the whole thing into memory so that we can run up and down it
1167 quickly. We can work out how big the file is from the trailer
1168 record */
1169
1170 IEEE_DATA(abfd)->h.first_byte = (uint8e_type *) bfd_alloc(ieee->h.abfd, ieee->w.r.me_record
1171 + 50);
1172 bfd_seek(abfd, 0, 0);
1173 bfd_read((PTR)(IEEE_DATA(abfd)->h.first_byte), 1, ieee->w.r.me_record+50, abfd);
1174
1175 ieee_slurp_sections(abfd);
1176 return abfd->xvec;
1177 fail:
1178 (void) bfd_release(abfd, ieee);
1179 abfd->tdata.ieee_data = save;
1180 return (bfd_target *)NULL;
1181 }
1182
1183
1184 void
1185 DEFUN(ieee_print_symbol,(ignore_abfd, afile, symbol, how),
1186 bfd *ignore_abfd AND
1187 PTR afile AND
1188 asymbol *symbol AND
1189 bfd_print_symbol_type how)
1190 {
1191 FILE *file = (FILE *)afile;
1192
1193 switch (how) {
1194 case bfd_print_symbol_name:
1195 fprintf(file,"%s", symbol->name);
1196 break;
1197 case bfd_print_symbol_more:
1198 #if 0
1199 fprintf(file,"%4x %2x",aout_symbol(symbol)->desc & 0xffff,
1200 aout_symbol(symbol)->other & 0xff);
1201 #endif
1202 BFD_FAIL();
1203 break;
1204 case bfd_print_symbol_nm:
1205 case bfd_print_symbol_all:
1206 {
1207 CONST char *section_name = symbol->section == (asection *)NULL ?
1208 (CONST char *)"*abs" : symbol->section->name;
1209 if (symbol->name[0] == ' ') {
1210 fprintf(file,"* empty table entry ");
1211 }
1212 else {
1213 bfd_print_symbol_vandf((PTR)file,symbol);
1214
1215 fprintf(file," %-5s %04x %02x %s",
1216 section_name,
1217 (unsigned) ieee_symbol(symbol)->index,
1218 (unsigned) 0, /*
1219 aout_symbol(symbol)->desc & 0xffff,
1220 aout_symbol(symbol)->other & 0xff,*/
1221 symbol->name);
1222 }
1223 }
1224 break;
1225 }
1226 }
1227
1228
1229 static void
1230 DEFUN(do_one,(ieee, current_map, location_ptr,s),
1231 ieee_data_type *ieee AND
1232 ieee_per_section_type *current_map AND
1233 uint8e_type *location_ptr AND
1234 asection *s)
1235 {
1236 switch (this_byte(&(ieee->h)))
1237 {
1238 case ieee_load_constant_bytes_enum:
1239 {
1240 unsigned int number_of_maus;
1241 unsigned int i;
1242 next_byte(&(ieee->h));
1243 number_of_maus = must_parse_int(&(ieee->h));
1244
1245 for (i = 0; i < number_of_maus; i++) {
1246 location_ptr[current_map->pc++]= this_byte(&(ieee->h));
1247 next_byte(&(ieee->h));
1248 }
1249 }
1250 break;
1251
1252 case ieee_load_with_relocation_enum:
1253 {
1254 boolean loop = true;
1255 next_byte(&(ieee->h));
1256 while (loop)
1257 {
1258 switch (this_byte(&(ieee->h)))
1259 {
1260 case ieee_variable_R_enum:
1261
1262 case ieee_function_signed_open_b_enum:
1263 case ieee_function_unsigned_open_b_enum:
1264 case ieee_function_either_open_b_enum:
1265 {
1266 unsigned int extra = 4;
1267 boolean pcrel = false;
1268 asection *section;
1269 ieee_reloc_type *r =
1270 (ieee_reloc_type *) bfd_alloc(ieee->h.abfd,
1271 sizeof(ieee_reloc_type));
1272
1273 *(current_map->reloc_tail_ptr) = r;
1274 current_map->reloc_tail_ptr= &r->next;
1275 r->next = (ieee_reloc_type *)NULL;
1276 next_byte(&(ieee->h));
1277 /* abort();*/
1278
1279 parse_expression(ieee,
1280 &r->relent.addend,
1281 &section,
1282 &r->symbol,
1283 &pcrel, &extra);
1284 r->relent.address = current_map->pc;
1285 s->reloc_count++;
1286 if (r->relent.sym_ptr_ptr == 0) {
1287 r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1288 }
1289
1290 if (this_byte(&(ieee->h)) == (int)ieee_comma) {
1291 next_byte(&(ieee->h));
1292 /* Fetch number of bytes to pad */
1293 extra = must_parse_int(&(ieee->h));
1294 };
1295
1296 switch (this_byte(&(ieee->h))) {
1297 case ieee_function_signed_close_b_enum:
1298 next_byte(&(ieee->h));
1299 break;
1300 case ieee_function_unsigned_close_b_enum:
1301 next_byte(&(ieee->h));
1302 break;
1303 case ieee_function_either_close_b_enum:
1304 next_byte(&(ieee->h));
1305 break;
1306 default:
1307 break;
1308 }
1309 /* Build a relocation entry for this type */
1310 /* If pc rel then stick -ve pc into instruction
1311 and take out of reloc ..
1312
1313 I've changed this. It's all too
1314 complicated. I keep 0 in the
1315 instruction now.
1316 */
1317
1318 switch (extra)
1319 {
1320 case 0:
1321 case 4:
1322
1323 if (pcrel == true)
1324 {
1325 #if KEEPMINUSPCININST
1326 bfd_put_32(ieee->h.abfd, -current_map->pc, location_ptr +
1327 current_map->pc);
1328 r->relent.howto = &rel32_howto;
1329 r->relent.addend -=
1330 current_map->pc;
1331 #else
1332 bfd_put_32(ieee->h.abfd,0, location_ptr +
1333 current_map->pc);
1334 r->relent.howto = &rel32_howto;
1335 #endif
1336 }
1337 else
1338 {
1339 bfd_put_32(ieee->h.abfd, 0, location_ptr +
1340 current_map->pc);
1341 r->relent.howto = &abs32_howto;
1342 }
1343 current_map->pc +=4;
1344 break;
1345 case 2:
1346 if (pcrel == true) {
1347 #if KEEPMINUSPCININST
1348 bfd_put_16(ieee->h.abfd, (int)(-current_map->pc), location_ptr +current_map->pc);
1349 r->relent.addend -= current_map->pc;
1350 r->relent.howto = &rel16_howto;
1351 #else
1352
1353 bfd_put_16(ieee->h.abfd, 0, location_ptr +current_map->pc);
1354 r->relent.howto = &rel16_howto;
1355 #endif
1356 }
1357
1358 else {
1359 bfd_put_16(ieee->h.abfd, 0, location_ptr +current_map->pc);
1360 r->relent.howto = &abs16_howto;
1361 }
1362 current_map->pc +=2;
1363 break;
1364 case 1:
1365 if (pcrel == true) {
1366 #if KEEPMINUSPCININST
1367 bfd_put_8(ieee->h.abfd, (int)(-current_map->pc), location_ptr +current_map->pc);
1368 r->relent.addend -= current_map->pc;
1369 r->relent.howto = &rel8_howto;
1370 #else
1371 bfd_put_8(ieee->h.abfd,0, location_ptr +current_map->pc);
1372 r->relent.howto = &rel8_howto;
1373 #endif
1374 }
1375 else {
1376 bfd_put_8(ieee->h.abfd, 0, location_ptr +current_map->pc);
1377 r->relent.howto = &abs8_howto;
1378 }
1379 current_map->pc +=1;
1380 break;
1381
1382 default:
1383 BFD_FAIL();
1384 break;
1385 }
1386 }
1387 break;
1388 default:
1389 {
1390 bfd_vma this_size ;
1391 if (parse_int(&(ieee->h), &this_size) == true) {
1392 unsigned int i;
1393 for (i = 0; i < this_size; i++) {
1394 location_ptr[current_map->pc ++] = this_byte(&(ieee->h));
1395 next_byte(&(ieee->h));
1396 }
1397 }
1398 else {
1399 loop = false;
1400 }
1401 }
1402 }
1403 }
1404 }
1405 }
1406 }
1407
1408 /* Read in all the section data and relocation stuff too */
1409 static boolean
1410 DEFUN(ieee_slurp_section_data,(abfd),
1411 bfd *abfd)
1412 {
1413 bfd_byte *location_ptr = (bfd_byte *)NULL;
1414 ieee_data_type *ieee = IEEE_DATA(abfd);
1415 unsigned int section_number ;
1416
1417 ieee_per_section_type *current_map = (ieee_per_section_type *)NULL;
1418 asection *s;
1419 /* Seek to the start of the data area */
1420 if (ieee->read_data== true) return true;
1421 ieee->read_data = true;
1422 ieee_seek(abfd, ieee->w.r.data_part);
1423
1424 /* Allocate enough space for all the section contents */
1425
1426
1427 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
1428 ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1429 per->data = (bfd_byte *) bfd_alloc(ieee->h.abfd, s->_raw_size);
1430 /*SUPPRESS 68*/
1431 per->reloc_tail_ptr =
1432 (ieee_reloc_type **)&(s->relocation);
1433 }
1434
1435
1436
1437 while (true) {
1438 switch (this_byte(&(ieee->h)))
1439 {
1440 /* IF we see anything strange then quit */
1441 default:
1442 return true;
1443
1444 case ieee_set_current_section_enum:
1445 next_byte(&(ieee->h));
1446 section_number = must_parse_int(&(ieee->h));
1447 s = ieee->section_table[section_number];
1448 current_map = (ieee_per_section_type *) s->used_by_bfd;
1449 location_ptr = current_map->data - s->vma;
1450 /* The document I have says that Microtec's compilers reset */
1451 /* this after a sec section, even though the standard says not */
1452 /* to. SO .. */
1453 current_map->pc =s->vma;
1454 break;
1455
1456
1457 case ieee_e2_first_byte_enum:
1458 next_byte(&(ieee->h));
1459 switch (this_byte(&(ieee->h)))
1460 {
1461 case ieee_set_current_pc_enum & 0xff:
1462 {
1463 bfd_vma value;
1464 asection *dsection;
1465 ieee_symbol_index_type symbol;
1466 unsigned int extra;
1467 boolean pcrel;
1468 next_byte(&(ieee->h));
1469 must_parse_int(&(ieee->h)); /* Thow away section #*/
1470 parse_expression(ieee, &value,
1471 &dsection,
1472 &symbol,
1473 &pcrel, &extra);
1474 current_map->pc = value;
1475 BFD_ASSERT((unsigned)(value - s->vma) <= s->_raw_size);
1476 }
1477 break;
1478
1479 case ieee_value_starting_address_enum & 0xff:
1480 /* We've got to the end of the data now - */
1481 return true;
1482 default:
1483 BFD_FAIL();
1484 return true;
1485 }
1486 break;
1487 case ieee_repeat_data_enum:
1488 {
1489 /* Repeat the following LD or LR n times - we do this by
1490 remembering the stream pointer before running it and
1491 resetting it and running it n times. We special case
1492 the repetition of a repeat_data/load_constant
1493 */
1494
1495 unsigned int iterations ;
1496 uint8e_type *start ;
1497 next_byte(&(ieee->h));
1498 iterations = must_parse_int(&(ieee->h));
1499 start = ieee->h.input_p;
1500 if (start[0] == (int)ieee_load_constant_bytes_enum &&
1501 start[1] == 1) {
1502 while (iterations != 0) {
1503 location_ptr[current_map->pc++] = start[2];
1504 iterations--;
1505 }
1506 next_byte(&(ieee->h));
1507 next_byte(&(ieee->h));
1508 next_byte(&(ieee->h));
1509 }
1510 else {
1511 while (iterations != 0) {
1512 ieee->h.input_p = start;
1513 do_one(ieee, current_map, location_ptr,s);
1514 iterations --;
1515 }
1516 }
1517 }
1518 break;
1519 case ieee_load_constant_bytes_enum:
1520 case ieee_load_with_relocation_enum:
1521 {
1522 do_one(ieee, current_map, location_ptr,s);
1523 }
1524 }
1525 }
1526 }
1527
1528
1529
1530
1531
1532 boolean
1533 DEFUN(ieee_new_section_hook,(abfd, newsect),
1534 bfd *abfd AND
1535 asection *newsect)
1536 {
1537 newsect->used_by_bfd = (PTR)
1538 bfd_alloc(abfd, sizeof(ieee_per_section_type));
1539 ieee_per_section( newsect)->data = (bfd_byte *)NULL;
1540 ieee_per_section(newsect)->section = newsect;
1541 return true;
1542 }
1543
1544
1545 unsigned int
1546 DEFUN(ieee_get_reloc_upper_bound,(abfd, asect),
1547 bfd *abfd AND
1548 sec_ptr asect)
1549 {
1550 ieee_slurp_section_data(abfd);
1551 return (asect->reloc_count+1) * sizeof(arelent *);
1552 }
1553
1554 static boolean
1555 DEFUN(ieee_get_section_contents,(abfd, section, location, offset, count),
1556 bfd *abfd AND
1557 sec_ptr section AND
1558 PTR location AND
1559 file_ptr offset AND
1560 bfd_size_type count)
1561 {
1562 ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
1563 ieee_slurp_section_data(abfd);
1564 (void) memcpy((PTR)location, (PTR)(p->data + offset), (unsigned)count);
1565 return true;
1566 }
1567
1568
1569 unsigned int
1570 DEFUN(ieee_canonicalize_reloc,(abfd, section, relptr, symbols),
1571 bfd *abfd AND
1572 sec_ptr section AND
1573 arelent **relptr AND
1574 asymbol **symbols)
1575 {
1576 /* ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
1577 ieee_reloc_type *src = (ieee_reloc_type *)(section->relocation);
1578 ieee_data_type *ieee = IEEE_DATA(abfd);
1579
1580 while (src != (ieee_reloc_type *)NULL) {
1581 /* Work out which symbol to attach it this reloc to */
1582 switch (src->symbol.letter) {
1583 case 'X':
1584 src->relent.sym_ptr_ptr =
1585 symbols + src->symbol.index + ieee->external_reference_base_offset;
1586 break;
1587 case 0:
1588 src->relent.sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1589 break;
1590 default:
1591
1592 BFD_FAIL();
1593 }
1594 *relptr++ = &src->relent;
1595 src = src->next;
1596 }
1597 *relptr = (arelent *)NULL;
1598 return section->reloc_count;
1599 }
1600
1601
1602
1603 static int
1604 DEFUN(comp,(ap, bp),
1605 CONST PTR ap AND
1606 CONST PTR bp)
1607 {
1608 arelent *a = *((arelent **)ap);
1609 arelent *b = *((arelent **)bp);
1610 return a->address - b->address;
1611 }
1612
1613 /*
1614 Write the section headers
1615 */
1616
1617 static void
1618 DEFUN(ieee_write_section_part,(abfd),
1619 bfd *abfd)
1620 {
1621 ieee_data_type *ieee = IEEE_DATA(abfd);
1622 asection *s;
1623 ieee->w.r.section_part = bfd_tell(abfd);
1624 for (s = abfd->sections; s != (asection *)NULL; s=s->next) {
1625 if (s != &bfd_abs_section)
1626 {
1627
1628 ieee_write_byte(abfd, ieee_section_type_enum);
1629 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1630
1631 if (abfd->flags & EXEC_P)
1632 {
1633 /* This image is executable, so output absolute sections */
1634 ieee_write_byte(abfd, ieee_variable_A_enum);
1635 ieee_write_byte(abfd, ieee_variable_S_enum);
1636 }
1637 else
1638 {
1639 ieee_write_byte(abfd, ieee_variable_C_enum);
1640 }
1641
1642 switch (s->flags &(SEC_CODE | SEC_DATA | SEC_ROM))
1643 {
1644 case SEC_CODE | SEC_LOAD:
1645 case SEC_CODE:
1646 ieee_write_byte(abfd, ieee_variable_P_enum);
1647 break;
1648 case SEC_DATA:
1649 default:
1650 ieee_write_byte(abfd, ieee_variable_D_enum);
1651 break;
1652 case SEC_ROM:
1653 case SEC_ROM | SEC_DATA:
1654 case SEC_ROM | SEC_LOAD:
1655 case SEC_ROM | SEC_DATA | SEC_LOAD:
1656
1657 ieee_write_byte(abfd, ieee_variable_R_enum);
1658 }
1659
1660
1661 ieee_write_id(abfd, s->name);
1662 #if 0
1663 ieee_write_int(abfd, 0); /* Parent */
1664 ieee_write_int(abfd, 0); /* Brother */
1665 ieee_write_int(abfd, 0); /* Context */
1666 #endif
1667 /* Alignment */
1668 ieee_write_byte(abfd, ieee_section_alignment_enum);
1669 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1670 ieee_write_int(abfd, 1 << s->alignment_power);
1671
1672 /* Size */
1673 ieee_write_2bytes(abfd, ieee_section_size_enum);
1674 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1675 ieee_write_int(abfd, s->_raw_size);
1676 if (abfd->flags & EXEC_P) {
1677 /* Relocateable sections don't have asl records */
1678 /* Vma */
1679 ieee_write_2bytes(abfd, ieee_section_base_address_enum);
1680 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1681 ieee_write_int(abfd, s->vma);
1682 }
1683 }
1684
1685 }
1686 }
1687
1688
1689
1690 static void
1691 DEFUN(do_with_relocs,(abfd, s),
1692 bfd *abfd AND
1693 asection *s)
1694 {
1695 unsigned int relocs_to_go = s->reloc_count;
1696
1697
1698 bfd_byte *stream = ieee_per_section(s)->data;
1699 arelent **p = s->orelocation;
1700
1701 bfd_size_type current_byte_index = 0;
1702
1703 qsort(s->orelocation,
1704 relocs_to_go,
1705 sizeof(arelent **),
1706 comp);
1707
1708 /* Output the section preheader */
1709 ieee_write_byte(abfd, ieee_set_current_section_enum);
1710 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1711
1712 ieee_write_twobyte(abfd, ieee_set_current_pc_enum);
1713 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1714 ieee_write_expression(abfd, 0, s->symbol, 0, 0);
1715
1716 if (relocs_to_go == 0)
1717 {
1718 /* If there arn't any relocations then output the load constant byte
1719 opcode rather than the load with relocation opcode */
1720
1721 while (current_byte_index < s->_raw_size) {
1722 bfd_size_type run;
1723 unsigned int MAXRUN = 32;
1724 run = MAXRUN;
1725 if (run > s->_raw_size - current_byte_index) {
1726 run = s->_raw_size - current_byte_index;
1727 }
1728
1729 if (run != 0) {
1730 ieee_write_byte(abfd, ieee_load_constant_bytes_enum);
1731 /* Output a stream of bytes */
1732 ieee_write_int(abfd, run);
1733 bfd_write((PTR)(stream + current_byte_index),
1734 1,
1735 run,
1736 abfd);
1737 current_byte_index += run;
1738 }
1739 }
1740 }
1741 else
1742 {
1743 ieee_write_byte(abfd, ieee_load_with_relocation_enum);
1744
1745
1746 /* Output the data stream as the longest sequence of bytes
1747 possible, allowing for the a reasonable packet size and
1748 relocation stuffs */
1749
1750 if ((PTR)stream == (PTR)NULL) {
1751 /* Outputting a section without data, fill it up */
1752 stream = (uint8e_type *)(bfd_alloc(abfd, s->_raw_size));
1753 memset((PTR)stream, 0, s->_raw_size);
1754 }
1755 while (current_byte_index < s->_raw_size) {
1756 bfd_size_type run;
1757 unsigned int MAXRUN = 32;
1758 if (relocs_to_go) {
1759 run = (*p)->address - current_byte_index;
1760 }
1761 else {
1762 run = MAXRUN;
1763 }
1764 if (run > s->_raw_size - current_byte_index) {
1765 run = s->_raw_size - current_byte_index;
1766 }
1767
1768 if (run != 0) {
1769 /* Output a stream of bytes */
1770 ieee_write_int(abfd, run);
1771 bfd_write((PTR)(stream + current_byte_index),
1772 1,
1773 run,
1774 abfd);
1775 current_byte_index += run;
1776 }
1777 /* Output any relocations here */
1778 if (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
1779 while (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
1780
1781 arelent *r = *p;
1782 bfd_vma ov;
1783
1784 #if 0
1785 if (r->howto->pc_relative) {
1786 r->addend += current_byte_index ;
1787 }
1788 #endif
1789
1790 switch (r->howto->size) {
1791 case 2:
1792
1793 ov = bfd_get_32(abfd,
1794 stream+current_byte_index);
1795 current_byte_index +=4;
1796 break;
1797 case 1:
1798 ov = bfd_get_16(abfd,
1799 stream+current_byte_index);
1800 current_byte_index +=2;
1801 break;
1802 case 0:
1803 ov = bfd_get_8(abfd,
1804 stream+current_byte_index);
1805 current_byte_index ++;
1806 break;
1807 default:
1808 ov = 0;
1809 BFD_FAIL();
1810 }
1811 ieee_write_byte(abfd, ieee_function_either_open_b_enum);
1812 abort();
1813
1814 if (r->sym_ptr_ptr != (asymbol **)NULL) {
1815 ieee_write_expression(abfd, r->addend + ov,
1816 *(r->sym_ptr_ptr),
1817 r->howto->pc_relative, s->index);
1818 }
1819 else {
1820 ieee_write_expression(abfd, r->addend + ov,
1821 (asymbol *)NULL,
1822 r->howto->pc_relative, s->index);
1823 }
1824
1825 if (1 || r->howto->size != 2) {
1826 ieee_write_byte(abfd, ieee_comma);
1827 ieee_write_int(abfd, 1<< r->howto->size);
1828 }
1829 ieee_write_byte(abfd,
1830 ieee_function_either_close_b_enum);
1831
1832 relocs_to_go --;
1833 p++;
1834 }
1835
1836 }
1837 }
1838 }
1839 }
1840
1841 /* If there are no relocations in the output section then we can
1842 be clever about how we write. We block items up into a max of 127
1843 bytes */
1844
1845 static void
1846 DEFUN(do_as_repeat, (abfd, s),
1847 bfd *abfd AND
1848 asection *s)
1849 {
1850 ieee_write_byte(abfd, ieee_set_current_section_enum);
1851 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1852 ieee_write_byte(abfd, ieee_set_current_pc_enum >> 8);
1853 ieee_write_byte(abfd, ieee_set_current_pc_enum & 0xff);
1854 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1855 ieee_write_int(abfd, s->vma );
1856
1857 ieee_write_byte(abfd,ieee_repeat_data_enum);
1858 ieee_write_int(abfd, s->_raw_size);
1859 ieee_write_byte(abfd, ieee_load_constant_bytes_enum);
1860 ieee_write_byte(abfd, 1);
1861 ieee_write_byte(abfd, 0);
1862 }
1863
1864 static void
1865 DEFUN(do_without_relocs, (abfd, s),
1866 bfd *abfd AND
1867 asection *s)
1868 {
1869 bfd_byte *stream = ieee_per_section(s)->data;
1870
1871 if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
1872 {
1873 do_as_repeat(abfd, s);
1874 }
1875 else
1876 {
1877 unsigned int i;
1878 for (i = 0; i < s->_raw_size; i++) {
1879 if (stream[i] != 0) {
1880 do_with_relocs(abfd, s);
1881 return;
1882 }
1883 }
1884 do_as_repeat(abfd, s);
1885 }
1886
1887 }
1888
1889
1890 static unsigned char *output_ptr_start;
1891 static unsigned char *output_ptr;
1892 static unsigned char *output_ptr_end;
1893 static unsigned char *input_ptr_start;
1894 static unsigned char *input_ptr;
1895 static unsigned char *input_ptr_end;
1896 static bfd *input_bfd;
1897 static bfd *output_bfd;
1898 static int output_buffer;
1899
1900 static void fill()
1901 {
1902 bfd_read((PTR)input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
1903 input_ptr = input_ptr_start;
1904 }
1905 static void flush()
1906 {
1907 bfd_write((PTR)(output_ptr_start),1,output_ptr - output_ptr_start, output_bfd);
1908 output_ptr = output_ptr_start;
1909 output_buffer++;
1910 }
1911
1912 #define THIS() ( *input_ptr )
1913 #define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
1914 #define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end) flush(); }
1915
1916 static void write_int(value)
1917 int value;
1918 {
1919 if (value >= 0 && value <= 127) {
1920 OUT(value);
1921 }
1922 else {
1923 unsigned int length;
1924 /* How many significant bytes ? */
1925 /* FIXME FOR LONGER INTS */
1926 if (value & 0xff000000) {
1927 length = 4;
1928 }
1929 else if (value & 0x00ff0000) {
1930 length = 3;
1931 }
1932 else if (value & 0x0000ff00) {
1933 length = 2;
1934 }
1935 else length = 1;
1936
1937 OUT((int)ieee_number_repeat_start_enum + length);
1938 switch (length) {
1939 case 4:
1940 OUT( value >> 24);
1941 case 3:
1942 OUT( value >> 16);
1943 case 2:
1944 OUT( value >> 8);
1945 case 1:
1946 OUT( value);
1947 }
1948
1949 }
1950 }
1951 static void copy_id()
1952 {
1953 int length = THIS();
1954 char ch;
1955 OUT(length);
1956 NEXT();
1957 while (length--) {
1958 ch = THIS();
1959 OUT(ch);
1960 NEXT();
1961 }
1962 }
1963 #define VAR(x) ((x | 0x80))
1964 static void copy_expression()
1965 {
1966 int stack[10];
1967 int *tos = stack;
1968 int value = 0;
1969 while (1) {
1970 switch (THIS()) {
1971 case 0x84:
1972 NEXT();
1973 value = THIS(); NEXT();
1974 value = (value << 8) | THIS(); NEXT();
1975 value = (value << 8) | THIS(); NEXT();
1976 value = (value << 8) | THIS(); NEXT();
1977 *tos ++ = value;
1978 break;
1979 case 0x83:
1980 NEXT();
1981 value = THIS(); NEXT();
1982 value = (value << 8) | THIS(); NEXT();
1983 value = (value << 8) | THIS(); NEXT();
1984 *tos ++ = value;
1985 break;
1986 case 0x82:
1987 NEXT();
1988 value = THIS(); NEXT();
1989 value = (value << 8) | THIS(); NEXT();
1990 *tos ++ = value;
1991 break;
1992 case 0x81:
1993 NEXT();
1994 value = THIS(); NEXT();
1995 *tos ++ = value;
1996 break;
1997 case 0x80:
1998 NEXT();
1999 *tos ++ = 0;
2000 break;
2001 default:
2002 if (THIS() >0x84) {
2003 /* Not a number, just bug out with the answer */
2004 write_int(*(--tos));
2005 return;
2006 }
2007 *tos++ = THIS();
2008 NEXT();
2009 value = 0;
2010 break;
2011 case 0xa5:
2012 /* PLUS anything */
2013 {
2014 int value = *(--tos);
2015 value += *(--tos);
2016 *tos++ = value;
2017 NEXT();
2018 }
2019 break;
2020 case VAR('R') :
2021 {
2022 int section_number ;
2023 ieee_data_type *ieee;
2024 asection *s;
2025 NEXT();
2026 section_number = THIS();
2027
2028 NEXT();
2029 ieee= IEEE_DATA(input_bfd);
2030 s = ieee->section_table[section_number];
2031 if (s->output_section) {
2032 value = s->output_section->vma ;
2033 } else { value = 0; }
2034 value += s->output_offset;
2035 *tos++ = value;
2036 value = 0;
2037 }
2038 break;
2039 case 0x90:
2040 {
2041 NEXT();
2042 write_int(*(--tos));
2043 OUT(0x90);
2044 return;
2045
2046 }
2047 }
2048 }
2049
2050 }
2051
2052 /* Drop the int in the buffer, and copy a null into the gap, which we
2053 will overwrite later */
2054
2055 struct output_buffer_struct {
2056 unsigned char *ptrp;
2057 int buffer;
2058 } ;
2059
2060 static void
2061 DEFUN(fill_int,(buf),
2062 struct output_buffer_struct *buf)
2063 {
2064 if (buf->buffer == output_buffer) {
2065 /* Still a chance to output the size */
2066 int value = output_ptr - buf->ptrp + 3;
2067 buf->ptrp[0] = value >> 24;
2068 buf->ptrp[1] = value >> 16;
2069 buf->ptrp[2] = value >> 8;
2070 buf->ptrp[3] = value >> 0;
2071 }
2072
2073 }
2074 static void
2075 DEFUN(drop_int,(buf),
2076 struct output_buffer_struct *buf)
2077 {
2078 int type = THIS();
2079 int ch;
2080 if (type <= 0x84) {
2081 NEXT();
2082 switch(type) {
2083 case 0x84: ch = THIS(); NEXT();
2084 case 0x83: ch = THIS(); NEXT();
2085 case 0x82: ch = THIS(); NEXT();
2086 case 0x81: ch = THIS(); NEXT();
2087 case 0x80: break;
2088 }
2089 }
2090 OUT(0x84);
2091 buf->ptrp = output_ptr;
2092 buf->buffer = output_buffer;
2093 OUT(0);OUT(0);OUT(0);OUT(0);
2094 }
2095
2096 static void copy_int()
2097 {
2098 int type = THIS();
2099 int ch;
2100 if (type <= 0x84) {
2101 OUT(type);
2102 NEXT();
2103 switch(type) {
2104 case 0x84: ch = THIS(); NEXT(); OUT(ch);
2105 case 0x83: ch = THIS(); NEXT(); OUT(ch);
2106 case 0x82: ch = THIS(); NEXT(); OUT(ch);
2107 case 0x81: ch = THIS(); NEXT(); OUT(ch);
2108 case 0x80: break;
2109 }
2110 }
2111 }
2112
2113 #define ID copy_id()
2114 #define INT copy_int()
2115 #define EXP copy_expression()
2116 static void copy_till_end();
2117 #define INTn(q) copy_int()
2118 #define EXPn(q) copy_expression()
2119 static void f1_record()
2120 {
2121 int ch;
2122 /* ATN record */
2123 NEXT();
2124 ch = THIS();
2125 switch (ch)
2126 {
2127 default:
2128 OUT(0xf1); OUT(ch);
2129 break;
2130 case 0xc9:
2131 NEXT();
2132 OUT(0xf1); OUT(0xc9);
2133 INT; INT; ch = THIS();
2134 switch (ch)
2135 {
2136 case 0x16: NEXT();break;
2137 case 0x01: NEXT();break;
2138 case 0x00: NEXT(); INT; break;
2139 case 0x03: NEXT(); INT; break;
2140 case 0x13: EXPn(instruction address); break;
2141 default:
2142 break;
2143 }
2144 break;
2145 case 0xd8:
2146 /* EXternal ref */
2147 NEXT();
2148 OUT(0xf1); OUT(0xd8);
2149 EXP ; EXP; EXP; EXP;
2150 break;
2151 case 0xce:
2152 NEXT();
2153 OUT(0xf1);OUT(0xce); INT; INT; ch = THIS(); INT;
2154 switch (ch) {
2155 case 0x01:
2156 INT; INT; break;
2157 case 0x02:
2158 INT; break;
2159 case 0x04:
2160 EXPn(external function); break;
2161 case 0x05:
2162 break;
2163 case 0x07: INTn(line number); INT;
2164 case 0x08: break;
2165 case 0x0a: INTn(locked register); INT; break;
2166 case 0x3f: copy_till_end(); break;
2167 case 0x3e: copy_till_end(); break;
2168 case 0x40: copy_till_end(); break;
2169 case 0x41: ID; break;
2170 }
2171 }
2172
2173 }
2174 static void f0_record()
2175 {
2176 /* Attribute record */
2177 NEXT();
2178 OUT(0xf0);
2179 INTn(Symbol name );
2180 ID;
2181 }
2182 static void copy_till_end()
2183 {
2184 int ch = THIS();
2185 while (1) {
2186 while (ch <= 0x80)
2187 {
2188 OUT(ch);
2189 NEXT();
2190 ch = THIS();
2191 }
2192 switch (ch) {
2193 case 0x84:
2194 OUT(THIS());
2195 NEXT();
2196 case 0x83:
2197 OUT(THIS());
2198 NEXT();
2199 case 0x82:
2200 OUT(THIS());
2201 NEXT();
2202 case 0x81:
2203 OUT(THIS());
2204 NEXT();
2205 OUT(THIS());
2206 NEXT();
2207
2208 ch = THIS();
2209 break;
2210 default:
2211 return;
2212 }
2213 }
2214
2215 }
2216
2217 static void f2_record()
2218 {
2219 NEXT();
2220 OUT(0xf2);
2221 INT ;
2222 NEXT();
2223 OUT(0xce);
2224 INT ;
2225 copy_till_end();
2226 }
2227
2228
2229 static void block();
2230 static void f8_record()
2231 {
2232 int ch;
2233 NEXT();
2234 ch = THIS();
2235 switch (ch)
2236 {
2237 case 0x01:
2238 case 0x02:
2239 case 0x03:
2240 /* Unique typedefs for module */
2241 /* GLobal typedefs */
2242 /* High level module scope beginning */
2243 {
2244 struct output_buffer_struct ob;
2245 NEXT();
2246 OUT(0xf8); OUT(ch);
2247 drop_int(&ob); ID ;
2248
2249 block();
2250
2251 NEXT();
2252 fill_int(&ob);
2253 OUT(0xf9);
2254 }
2255 break;
2256 case 0x04:
2257 /* Global function */
2258 {
2259 struct output_buffer_struct ob;
2260 NEXT();
2261 OUT(0xf8); OUT(0x04);
2262 drop_int(&ob); ID ; INTn(stack size); INTn(ret val);
2263 EXPn(offset);
2264
2265 block();
2266
2267 NEXT();
2268 OUT(0xf9);
2269 EXPn(size of block);
2270 fill_int(&ob);
2271 }
2272 break;
2273
2274 case 0x05:
2275 /* File name for source line numbers */
2276 {
2277 struct output_buffer_struct ob;
2278 NEXT();
2279 OUT(0xf8); OUT(0x05);
2280 drop_int(&ob);
2281 ID; INTn(year); INTn(month); INTn(day);
2282 INTn(hour); INTn(monute); INTn(second);
2283 block();
2284 NEXT();
2285 OUT(0xf9);
2286 fill_int(&ob);
2287 }
2288 break;
2289
2290 case 0x06:
2291 /* Local function */
2292 { struct output_buffer_struct ob;
2293 NEXT(); OUT(0xf8); OUT(0x06);
2294 drop_int(&ob);
2295 ID; INTn(stack size); INTn(type return);
2296 EXPn(offset);
2297 block();
2298 NEXT();
2299 OUT(0xf9);
2300 EXPn(size);
2301 fill_int(&ob);
2302 }
2303 break;
2304
2305 case 0x0a:
2306 /* Assembler module scope beginning -*/
2307 { struct output_buffer_struct ob;
2308
2309 NEXT();
2310 OUT(0xf8); OUT(0x0a);
2311 drop_int(&ob);
2312 ID; ID; INT; ID; INT; INT; INT; INT; INT; INT;
2313
2314 block();
2315
2316 NEXT();
2317 OUT(0xf9);
2318 fill_int(&ob);
2319 }
2320 break;
2321 case 0x0b:
2322 {
2323 struct output_buffer_struct ob;
2324 NEXT();
2325 OUT(0xf8); OUT(0x0b);
2326 drop_int(&ob); ID ; INT; INTn(section index); EXPn(offset); INTn(stuff);
2327
2328 block();
2329
2330 OUT(0xf9);
2331 NEXT();
2332 EXPn(Size in Maus);
2333 fill_int(&ob);
2334 }
2335 break;
2336 }
2337 }
2338
2339 static void e2_record()
2340 {
2341 OUT(0xe2);
2342 NEXT();
2343 OUT(0xce);
2344 NEXT();
2345 INT;
2346 EXP;
2347 }
2348
2349 static void DEFUN_VOID(block)
2350 {
2351 int ch ;
2352 while (1) {
2353 ch = THIS();
2354 switch (ch) {
2355 case 0xe1:
2356 case 0xe5:
2357 return;
2358 case 0xf9:
2359 return;
2360 case 0xf0:
2361 f0_record();
2362 break;
2363 case 0xf1:
2364 f1_record();
2365 break;
2366 case 0xf2:
2367 f2_record();
2368 break;
2369 case 0xf8:
2370 f8_record();
2371 break;
2372 case 0xe2:
2373 e2_record();
2374 break;
2375
2376 }
2377 }
2378 }
2379
2380
2381
2382 /* relocate_debug,
2383 moves all the debug information from the source bfd to the output
2384 bfd, and relocates any expressions it finds
2385 */
2386
2387 static void
2388 DEFUN(relocate_debug,(output, input),
2389 bfd *output AND
2390 bfd *input)
2391 {
2392 #define IBS 400
2393 #define OBS 400
2394 unsigned char input_buffer[IBS];
2395
2396 input_ptr_start = input_ptr = input_buffer;
2397 input_ptr_end = input_buffer + IBS;
2398 input_bfd = input;
2399 bfd_read((PTR)input_ptr_start, 1, IBS, input);
2400 block();
2401 }
2402 /*
2403 During linking, we we told about the bfds which made up our
2404 contents, we have a list of them. They will still be open, so go to
2405 the debug info in each, and copy it out, relocating it as we go.
2406 */
2407
2408 static void
2409 DEFUN(ieee_write_debug_part, (abfd),
2410 bfd *abfd)
2411 {
2412 ieee_data_type *ieee = IEEE_DATA(abfd);
2413 bfd_chain_type *chain = ieee->chain_root;
2414 unsigned char output_buffer[OBS];
2415 boolean some_debug = false;
2416 file_ptr here = bfd_tell(abfd);
2417
2418 output_ptr_start = output_ptr = output_buffer ;
2419 output_ptr_end = output_buffer + OBS;
2420 output_ptr = output_buffer;
2421 output_bfd = abfd;
2422
2423 if (chain == (bfd_chain_type *)NULL) {
2424 #if 0
2425 /* There is no debug info, so we'll fake some up */
2426 CONST static char fake[] = {
2427 0xf8, 0xa, 0, 5, 't', 't', 't', 't', 't', 0, 2, 3,
2428 '1','.','1',0x82, 1991>>8, 1991 & 0xff, 9, 20, 11, 07,50 };
2429 ieee->w.r.debug_information_part = 0;
2430
2431
2432 here;
2433
2434
2435 /* bfd_write(fake, 1, sizeof(fake), abfd);*/
2436 /* Now write a header for each section */
2437 {
2438 int i = 0;
2439 asection *s = abfd->sections;
2440 while (s) {
2441 if (s != abfd->abs_section)
2442 {
2443
2444 ieee_write_byte(abfd, 0xf8);
2445 ieee_write_byte(abfd, 0x0b);
2446 ieee_write_byte(abfd, 0);
2447 ieee_write_byte(abfd, 0);
2448 ieee_write_byte(abfd, 1);
2449 ieee_write_byte(abfd, i + IEEE_SECTION_NUMBER_BASE);
2450 ieee_write_expression(abfd, 0, s->symbol, 0, 0, 0);
2451 ieee_write_byte(abfd,0);
2452 ieee_write_byte(abfd, 0xf9);
2453 ieee_write_expression(abfd, s->size,
2454 bfd_abs_section.symbol, 0, 0, 0);
2455 i++;
2456 }
2457
2458 s = s->next;
2459
2460 }
2461 /* Close the scope */
2462 ieee_write_byte(abfd, 0xf9);
2463 }
2464 #endif
2465 }
2466 else{
2467 while (chain != (bfd_chain_type *)NULL) {
2468 bfd *entry = chain->this;
2469 ieee_data_type *entry_ieee = IEEE_DATA(entry);
2470 if (entry_ieee->w.r.debug_information_part) {
2471 bfd_seek(entry, entry_ieee->w.r.debug_information_part, SEEK_SET);
2472 relocate_debug(abfd, entry);
2473 }
2474
2475 chain = chain->next;
2476 }
2477 if (some_debug) {
2478 ieee->w.r.debug_information_part = here;
2479 }
2480 else {
2481 ieee->w.r.debug_information_part = 0;
2482 }
2483 }
2484 flush();
2485
2486 }
2487 /* write the data in an ieee way */
2488 static void
2489 DEFUN(ieee_write_data_part,(abfd),
2490 bfd *abfd)
2491 {
2492 asection *s;
2493 ieee_data_type *ieee = IEEE_DATA(abfd);
2494 ieee->w.r.data_part = bfd_tell(abfd);
2495 for (s = abfd->sections; s != (asection *)NULL; s = s->next)
2496 {
2497 /* Sort the reloc records so we can insert them in the correct
2498 places */
2499 if (s->reloc_count != 0)
2500 {
2501 do_with_relocs(abfd, s);
2502 }
2503 else
2504 {
2505 do_without_relocs(abfd, s);
2506 }
2507 }
2508 }
2509
2510
2511
2512 static void
2513 DEFUN(init_for_output,(abfd),
2514 bfd *abfd)
2515 {
2516 asection *s;
2517 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
2518 if (s->_raw_size != 0) {
2519 ieee_per_section(s)->data = (bfd_byte *)(bfd_alloc(abfd, s->_raw_size));
2520 }
2521 }
2522 }
2523
2524 /** exec and core file sections */
2525
2526 /* set section contents is complicated with IEEE since the format is
2527 * not a byte image, but a record stream.
2528 */
2529 boolean
2530 DEFUN(ieee_set_section_contents,(abfd, section, location, offset, count),
2531 bfd *abfd AND
2532 sec_ptr section AND
2533 PTR location AND
2534 file_ptr offset AND
2535 bfd_size_type count)
2536 {
2537 if (ieee_per_section(section)->data == (bfd_byte *)NULL) {
2538 init_for_output(abfd);
2539 }
2540 (void) memcpy((PTR)(ieee_per_section(section)->data + offset),
2541 (PTR)location,
2542 (unsigned int)count);
2543 return true;
2544 }
2545
2546 /*
2547 write the external symbols of a file, IEEE considers two sorts of
2548 external symbols, public, and referenced. It uses to internal forms
2549 to index them as well. When we write them out we turn their symbol
2550 values into indexes from the right base.
2551 */
2552 static void
2553 DEFUN(ieee_write_external_part,(abfd),
2554 bfd *abfd)
2555 {
2556 asymbol **q;
2557 ieee_data_type *ieee = IEEE_DATA(abfd);
2558
2559 unsigned int reference_index = IEEE_REFERENCE_BASE;
2560 unsigned int public_index = IEEE_PUBLIC_BASE+2;
2561 file_ptr here = bfd_tell(abfd);
2562 boolean hadone = false;
2563 if (abfd->outsymbols != (asymbol **)NULL) {
2564
2565 for (q = abfd->outsymbols; *q != (asymbol *)NULL; q++) {
2566 asymbol *p = *q;
2567 hadone = true;
2568 if (p->section == &bfd_und_section) {
2569 /* This must be a symbol reference .. */
2570 ieee_write_byte(abfd, ieee_external_reference_enum);
2571 ieee_write_int(abfd, reference_index);
2572 ieee_write_id(abfd, p->name);
2573 p->value = reference_index;
2574 reference_index++;
2575 }
2576 else if(p->section == &bfd_com_section) {
2577 /* This is a weak reference */
2578 ieee_write_byte(abfd, ieee_external_reference_enum);
2579 ieee_write_int(abfd, reference_index);
2580 ieee_write_id(abfd, p->name);
2581 ieee_write_byte(abfd, ieee_weak_external_reference_enum);
2582 ieee_write_int(abfd, reference_index);
2583 ieee_write_int(abfd, p->value);
2584 ieee_write_int(abfd, BFD_FORT_COMM_DEFAULT_VALUE);
2585 p->value = reference_index;
2586 reference_index++;
2587 }
2588 else if(p->flags & BSF_GLOBAL) {
2589 /* This must be a symbol definition */
2590
2591
2592 ieee_write_byte(abfd, ieee_external_symbol_enum);
2593 ieee_write_int(abfd, public_index );
2594 ieee_write_id(abfd, p->name);
2595
2596 ieee_write_twobyte(abfd, ieee_attribute_record_enum);
2597 ieee_write_int(abfd, public_index );
2598 ieee_write_byte(abfd, 15); /* instruction address */
2599 ieee_write_byte(abfd, 19); /* static symbol */
2600 ieee_write_byte(abfd, 1); /* one of them */
2601
2602
2603 /* Write out the value */
2604 ieee_write_2bytes(abfd, ieee_value_record_enum);
2605 ieee_write_int(abfd, public_index);
2606 if (p->section != &bfd_abs_section)
2607 {
2608 if (abfd->flags & EXEC_P)
2609 {
2610 /* If fully linked, then output all symbols
2611 relocated */
2612 ieee_write_int(abfd,
2613 p->value + p->section->output_offset+ p->section->output_section->vma);
2614
2615 }
2616 else {
2617 ieee_write_expression(abfd,
2618 p->value + p->section->output_offset,
2619 p->section->output_section->symbol
2620 , false, 0);
2621 }
2622 }
2623 else
2624 {
2625 ieee_write_expression(abfd,
2626 p->value,
2627 bfd_abs_section.symbol,
2628 false, 0);
2629 }
2630 p->value = public_index;
2631 public_index++;
2632 }
2633 else {
2634 /* This can happen - when there are gaps in the symbols read */
2635 /* from an input ieee file */
2636 }
2637 }
2638 }
2639 if (hadone)
2640 ieee->w.r.external_part = here;
2641
2642 }
2643
2644
2645 CONST static unsigned char exten[] =
2646 {
2647 0xf0, 0x20, 0x00,
2648 0xf1, 0xce, 0x20, 0x00, 37, 3, 3, /* Set version 3 rev 3 */
2649 0xf1, 0xce, 0x20, 0x00, 39, 2, /* keep symbol in original case */
2650 0xf1, 0xce, 0x20, 0x00, 38 /* set object type relocateable to x */
2651 };
2652
2653 CONST static unsigned char envi[] =
2654 {
2655 0xf0, 0x21, 0x00,
2656
2657 /* 0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
2658 0x19, 0x2c,
2659 */
2660 0xf1, 0xce, 0x21, 00, 52, 0x00, /* exec ok */
2661
2662 0xf1, 0xce, 0x21, 0, 53, 0x03, /* host unix */
2663 /* 0xf1, 0xce, 0x21, 0, 54, 2,1,1 tool & version # */
2664 };
2665
2666 static
2667 void
2668 DEFUN(ieee_write_me_part,(abfd),
2669 bfd *abfd)
2670 {
2671 ieee_data_type *ieee= IEEE_DATA(abfd);
2672 ieee->w.r.trailer_part = bfd_tell(abfd);
2673 if (abfd->start_address) {
2674 ieee->w.r.me_record = bfd_tell(abfd);
2675 ieee_write_2bytes(abfd, ieee_value_starting_address_enum);
2676 ieee_write_byte(abfd, ieee_function_either_open_b_enum);
2677 ieee_write_int(abfd, abfd->start_address);
2678 ieee_write_byte(abfd, ieee_function_either_close_b_enum);
2679 }
2680 else {
2681 ieee->w.r.me_record = bfd_tell(abfd);
2682 }
2683 ieee_write_byte(abfd, ieee_module_end_enum);
2684
2685 }
2686 boolean
2687 DEFUN(ieee_write_object_contents,(abfd),
2688 bfd *abfd)
2689 {
2690 ieee_data_type *ieee = IEEE_DATA(abfd);
2691 unsigned int i;
2692 file_ptr old;
2693 /* Fast forward over the header area */
2694 bfd_seek(abfd, 0, 0);
2695 ieee_write_byte(abfd, ieee_module_beginning_enum);
2696
2697 ieee_write_id(abfd, bfd_printable_name(abfd));
2698 ieee_write_id(abfd, abfd->filename);
2699
2700
2701
2702
2703 /* Fast forward over the variable bits */
2704
2705
2706
2707 ieee_write_byte(abfd, ieee_address_descriptor_enum);
2708
2709 /* Bits per MAU */
2710 ieee_write_byte(abfd, bfd_arch_bits_per_byte(abfd));
2711 /* MAU's per address */
2712 ieee_write_byte(abfd, bfd_arch_bits_per_address(abfd) /
2713 bfd_arch_bits_per_byte(abfd));
2714
2715
2716 old = bfd_tell(abfd);
2717 bfd_seek(abfd, 8 * N_W_VARIABLES, 1);
2718
2719
2720 ieee->w.r.extension_record = bfd_tell(abfd);
2721 bfd_write((char *)exten, 1, sizeof(exten), abfd);
2722 if (abfd->flags & EXEC_P)
2723 ieee_write_byte(abfd, 0x1); /* Absolute */
2724 else
2725 ieee_write_byte(abfd, 0x2); /* Relocateable */
2726
2727 ieee->w.r.environmental_record = bfd_tell(abfd);
2728 bfd_write((char *)envi, 1, sizeof(envi), abfd);
2729 output_bfd = abfd;
2730 flush();
2731
2732 ieee_write_section_part(abfd);
2733 /*
2734 First write the symbols, this changes their values into table
2735 indeces so we cant use it after this point
2736 */
2737 ieee_write_external_part(abfd);
2738 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
2739
2740
2741 /* ieee_write_byte(abfd, ieee_record_seperator_enum);*/
2742
2743
2744 /*
2745 Write any debugs we have been told about
2746 */
2747 ieee_write_debug_part(abfd);
2748
2749 /*
2750 Can only write the data once the symbols have been written since
2751 the data contains relocation information which points to the
2752 symbols
2753 */
2754 ieee_write_data_part(abfd);
2755
2756
2757 /*
2758 At the end we put the end !
2759 */
2760 ieee_write_me_part(abfd);
2761
2762
2763 /* Generate the header */
2764 bfd_seek(abfd, old, false);
2765
2766 for (i= 0; i < N_W_VARIABLES; i++) {
2767 ieee_write_2bytes(abfd,ieee_assign_value_to_variable_enum);
2768 ieee_write_byte(abfd, i);
2769 ieee_write_int5_out(abfd, ieee->w.offset[i]);
2770 }
2771 return true;
2772 }
2773
2774
2775
2776 \f
2777 /* Native-level interface to symbols. */
2778
2779 /* We read the symbols into a buffer, which is discarded when this
2780 function exits. We read the strings into a buffer large enough to
2781 hold them all plus all the cached symbol entries. */
2782
2783 asymbol *
2784 DEFUN(ieee_make_empty_symbol,(abfd),
2785 bfd *abfd)
2786 {
2787
2788 ieee_symbol_type *new =
2789 (ieee_symbol_type *)zalloc (sizeof (ieee_symbol_type));
2790 new->symbol.the_bfd = abfd;
2791 return &new->symbol;
2792
2793 }
2794
2795 static bfd *
2796 DEFUN(ieee_openr_next_archived_file,(arch, prev),
2797 bfd *arch AND
2798 bfd *prev)
2799 {
2800 ieee_ar_data_type *ar = IEEE_AR_DATA(arch);
2801 /* take the next one from the arch state, or reset */
2802 if (prev == (bfd *)NULL) {
2803 /* Reset the index - the first two entries are bogus*/
2804 ar->element_index = 2;
2805 }
2806 while (true) {
2807 ieee_ar_obstack_type *p = ar->elements + ar->element_index;
2808 ar->element_index++;
2809 if (ar->element_index <= ar->element_count) {
2810 if (p->file_offset != (file_ptr)0) {
2811 if (p->abfd == (bfd *)NULL) {
2812 p->abfd = _bfd_create_empty_archive_element_shell(arch);
2813 p->abfd->origin = p->file_offset;
2814 }
2815 return p->abfd;
2816 }
2817 }
2818 else {
2819 bfd_error = no_more_archived_files;
2820 return (bfd *)NULL;
2821 }
2822
2823 }
2824 }
2825
2826 static boolean
2827 ieee_find_nearest_line(abfd,
2828 section,
2829 symbols,
2830 offset,
2831 filename_ptr,
2832 functionname_ptr,
2833 line_ptr)
2834 bfd *abfd;
2835 asection *section;
2836 asymbol **symbols;
2837 bfd_vma offset;
2838 char **filename_ptr;
2839 char **functionname_ptr;
2840 int *line_ptr;
2841 {
2842 return false;
2843 }
2844
2845
2846 static int
2847 ieee_generic_stat_arch_elt(abfd, buf)
2848 bfd *abfd;
2849 struct stat *buf;
2850 {
2851 ieee_ar_data_type *ar = IEEE_AR_DATA(abfd);
2852 if (ar == (ieee_ar_data_type *)NULL) {
2853 bfd_error = invalid_operation;
2854 return -1;
2855 }
2856 else {
2857 buf->st_size = 0x1;
2858 buf->st_mode = 0666;
2859 return 0;
2860 }
2861 }
2862 static int
2863 DEFUN(ieee_sizeof_headers,(abfd, x),
2864 bfd *abfd AND
2865 boolean x)
2866 {
2867 return 0;
2868 }
2869
2870
2871
2872 static void
2873 DEFUN(ieee_bfd_debug_info_start,(abfd),
2874 bfd *abfd)
2875 {
2876
2877 }
2878
2879 static void
2880 DEFUN(ieee_bfd_debug_info_end,(abfd),
2881 bfd *abfd)
2882 {
2883
2884 }
2885
2886
2887 /* Add this section to the list of sections we have debug info for, to
2888 be ready to output it at close time
2889 */
2890 static void
2891 DEFUN(ieee_bfd_debug_info_accumulate,(abfd, section),
2892 bfd *abfd AND
2893 asection *section)
2894 {
2895 ieee_data_type *ieee = IEEE_DATA(section->owner);
2896 ieee_data_type *output_ieee = IEEE_DATA(abfd);
2897 /* can only accumulate data from other ieee bfds */
2898 if (section->owner->xvec != abfd->xvec)
2899 return;
2900 /* Only bother once per bfd */
2901 if (ieee->done_debug == true)
2902 return;
2903 ieee->done_debug = true;
2904
2905 /* Don't bother if there is no debug info */
2906 if (ieee->w.r.debug_information_part == 0)
2907 return;
2908
2909
2910 /* Add to chain */
2911 {
2912 bfd_chain_type *n = (bfd_chain_type *) bfd_alloc(abfd, sizeof(bfd_chain_type));
2913 n->this = section->owner;
2914 n->next = (bfd_chain_type *)NULL;
2915
2916 if (output_ieee->chain_head) {
2917 output_ieee->chain_head->next = n;
2918 }
2919 else {
2920 output_ieee->chain_root = n;
2921
2922 }
2923 output_ieee->chain_head = n;
2924 }
2925 }
2926
2927
2928
2929
2930
2931
2932 #define FOO PROTO
2933 #define ieee_core_file_failing_command (char *(*)())(bfd_nullvoidptr)
2934 #define ieee_core_file_failing_signal (int (*)())bfd_0
2935 #define ieee_core_file_matches_executable_p ( FOO(boolean, (*),(bfd *, bfd *)))bfd_false
2936 #define ieee_slurp_armap bfd_true
2937 #define ieee_slurp_extended_name_table bfd_true
2938 #define ieee_truncate_arname (void (*)())bfd_nullvoidptr
2939 #define ieee_write_armap (FOO( boolean, (*),(bfd *, unsigned int, struct orl *, unsigned int, int))) bfd_nullvoidptr
2940 #define ieee_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
2941 #define ieee_close_and_cleanup bfd_generic_close_and_cleanup
2942 #define ieee_set_arch_mach bfd_default_set_arch_mach
2943 #define ieee_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
2944 #define ieee_bfd_relax_section bfd_generic_relax_section
2945 /*SUPPRESS 460 */
2946 bfd_target ieee_vec =
2947 {
2948 "ieee", /* name */
2949 bfd_target_ieee_flavour,
2950 true, /* target byte order */
2951 true, /* target headers byte order */
2952 (HAS_RELOC | EXEC_P | /* object flags */
2953 HAS_LINENO | HAS_DEBUG |
2954 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2955 ( SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
2956 |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2957 ' ', /* ar_pad_char */
2958 16, /* ar_max_namelen */
2959 1, /* minimum alignment */
2960 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
2961 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
2962
2963 { _bfd_dummy_target,
2964 ieee_object_p, /* bfd_check_format */
2965 ieee_archive_p,
2966 _bfd_dummy_target,
2967 },
2968 {
2969 bfd_false,
2970 ieee_mkobject,
2971 _bfd_generic_mkarchive,
2972 bfd_false
2973 },
2974 {
2975 bfd_false,
2976 ieee_write_object_contents,
2977 _bfd_write_archive_contents,
2978 bfd_false,
2979 },
2980 JUMP_TABLE(ieee)
2981 };
This page took 0.123455 seconds and 4 git commands to generate.