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