Avoid making a machine-dependent (probably wrong) declaration of malloc
[deliverable/binutils-gdb.git] / bfd / ieee.c
1 /* bfd backend for ieee-695 objects.
2
3 IEEE 695 format is a stream of records, which we parse using a simple one
4 token (which is one byte in this lexicon) lookahead recursive decent
5 parser. */
6
7 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
8
9 This file is part of BFD, the Binary File Diddler.
10
11 BFD is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 1, or (at your option)
14 any later version.
15
16 BFD is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with BFD; see the file COPYING. If not, write to
23 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24
25 #include "sysdep.h"
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "obstack.h"
29 #include "ieee.h"
30 #include "libieee.h"
31
32
33 #define obstack_chunk_alloc malloc
34 #define obstack_chunk_free free
35 #define ieee_malloc(abfd,size) \
36 obstack_alloc(&( ieee_data(abfd)->ieee_obstack), (size))
37
38 typedef void generic_symbol_type;
39
40
41 /***************************************************************************
42 Functions for writing to ieee files in the strange way that the
43 standard requires:
44 */
45
46
47 static void
48 DEFUN(ieee_write_byte,(abfd, byte),
49 bfd *abfd AND
50 bfd_byte byte)
51 {
52 bfd_write(&byte, 1, 1, abfd);
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(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 (value >= 0 && 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, 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((bfd_byte *)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 #define this_byte(abfd) *(ptr(abfd))
134 #define next_byte(abfd) (ptr(abfd)++)
135 #define this_byte_and_next(abfd) *(ptr(abfd)++)
136
137
138 static unsigned short
139 DEFUN(read_2bytes,(abfd),
140 bfd *abfd)
141 {
142 unsigned char c1 = this_byte_and_next(abfd);
143 unsigned char c2 = this_byte_and_next(abfd);
144 return (c1<<8 ) | c2;
145
146 }
147
148 static void
149 DEFUN(bfd_get_string,(abfd, string, length),
150 bfd *abfd AND
151 char *string AND
152 size_t length)
153 {
154 size_t i;
155 for (i= 0; i < length; i++) {
156 string[i] = this_byte_and_next(abfd);
157 }
158 }
159
160 static char *
161 DEFUN(read_id,(abfd),
162 bfd *abfd)
163 {
164 size_t length;
165 char *string;
166 length = this_byte_and_next(abfd);
167 if (length >= 0x00 && length <= 0x7f) {
168 /* Simple string of length 0 to 127 */
169 }
170 else if (length == 0xde) {
171 /* Length is next byte, allowing 0..255 */
172 length = this_byte_and_next(abfd);
173 }
174 else if (length == 0xdf) {
175 /* Length is next two bytes, allowing 0..65535 */
176 length = this_byte_and_next(abfd) ;
177 length = (length * 256) + this_byte_and_next(abfd);
178 }
179 /* Buy memory and read string */
180 string = ieee_malloc(abfd, length+1);
181 bfd_get_string(abfd, string, length);
182 string[length] = 0;
183 return string;
184 }
185
186 static void
187 DEFUN(ieee_write_expression,(abfd, value, section, symbol),
188 bfd*abfd AND
189 bfd_vma value AND
190 asection *section AND
191 asymbol *symbol)
192 {
193 unsigned int plus_count = 0;
194 ieee_write_int(abfd, value);
195 if (section != (asection *)NULL) {
196 plus_count++;
197 ieee_write_byte(abfd, ieee_variable_L_enum);
198 ieee_write_byte(abfd, section->index +IEEE_SECTION_NUMBER_BASE);
199 }
200
201 if (symbol != (asymbol *)NULL) {
202 plus_count++;
203 if ((symbol->flags & BSF_UNDEFINED ) ||
204 (symbol->flags & BSF_FORT_COMM)) {
205 ieee_write_byte(abfd, ieee_variable_X_enum);
206 ieee_write_int(abfd, symbol->value);
207 }
208 else if (symbol->flags & BSF_GLOBAL) {
209 ieee_write_byte(abfd, ieee_variable_I_enum);
210 ieee_write_int(abfd, symbol->value);
211 }
212 else if (symbol->flags & BSF_LOCAL) {
213 /* This is a reference to a defined local symbol,
214 We can easily do a local as a section+offset */
215 if (symbol->section != (asection *)NULL) {
216 /* If this symbol is not absolute, add the base of it */
217 ieee_write_byte(abfd, ieee_variable_L_enum);
218 ieee_write_byte(abfd, symbol->section->index +
219 IEEE_SECTION_NUMBER_BASE);
220 plus_count++;
221 }
222
223 ieee_write_int(abfd, symbol->value);
224 }
225 else {
226
227 BFD_FAIL();
228 }
229 }
230
231 while (plus_count != 0) {
232 ieee_write_byte(abfd, ieee_function_plus_enum);
233 plus_count--;
234 }
235
236 }
237
238
239
240
241
242
243
244
245
246 /*****************************************************************************/
247
248 /*
249 writes any integer into the buffer supplied and always takes 5 bytes
250 */
251 static void
252 DEFUN(ieee_write_int5,(buffer, value),
253 bfd_byte*buffer AND
254 bfd_vma value )
255 {
256 buffer[0] = ieee_number_repeat_4_enum;
257 buffer[1] = (value >> 24 ) & 0xff;
258 buffer[2] = (value >> 16 ) & 0xff;
259 buffer[3] = (value >> 8 ) & 0xff;
260 buffer[4] = (value >> 0 ) & 0xff;
261 }
262 static void
263 DEFUN(ieee_write_int5_out, (abfd, value),
264 bfd *abfd AND
265 bfd_vma value)
266 {
267 char b[5];
268 ieee_write_int5(b, value);
269 bfd_write(b,1,5,abfd);
270 }
271
272
273 static boolean
274 DEFUN(parse_int,(abfd, value_ptr),
275 bfd *abfd AND
276 bfd_vma *value_ptr)
277 {
278 int value = this_byte(abfd);
279 int result;
280 if (value >= 0 && value <= 127) {
281 *value_ptr = value;
282 next_byte(abfd);
283 return true;
284 }
285 else if (value >= 0x80 && value <= 0x88) {
286 unsigned int count = value & 0xf;
287 result = 0;
288 next_byte(abfd);
289 while (count) {
290 result =(result << 8) | this_byte_and_next(abfd);
291 count--;
292 }
293 *value_ptr = result;
294 return true;
295 }
296 return false;
297 }
298 static int
299 DEFUN(parse_i,(abfd, ok),
300 bfd *abfd AND
301 boolean *ok)
302 {
303 bfd_vma x;
304 *ok = parse_int(abfd, &x);
305 return x;
306 }
307
308 static bfd_vma
309 DEFUN(must_parse_int,(abfd),
310 bfd *abfd)
311 {
312 bfd_vma result;
313 BFD_ASSERT(parse_int(abfd, &result) == true);
314 return result;
315 }
316
317 typedef struct
318 {
319 bfd_vma value;
320 asection *section;
321 ieee_symbol_index_type symbol;
322 } ieee_value_type;
323
324
325 static
326 reloc_howto_type abs32_howto
327 = HOWTO(1,0,2,32,0,0,0,true,0,"abs32",false,0xffffffff, 0xffffffff,false);
328 static
329 reloc_howto_type abs16_howto
330 = HOWTO(1,0,1,16,0,0,0,true,0,"abs16",false,0x0000ffff, 0x0000ffff,false);
331
332 static ieee_symbol_index_type NOSYMBOL = { 0, 0};
333
334
335 static void
336 DEFUN(parse_expression,(abfd, value, section, symbol, pcrel, extra),
337 bfd *abfd AND
338 bfd_vma *value AND
339 asection **section AND
340 ieee_symbol_index_type *symbol AND
341 boolean *pcrel AND
342 unsigned int *extra)
343 {
344 #define POS sp[1]
345 #define TOS sp[0]
346 #define NOS sp[-1]
347 #define INC sp++;
348 #define DEC sp--;
349
350 boolean loop = true;
351 ieee_value_type stack[10];
352
353 /* The stack pointer always points to the next unused location */
354 #define PUSH(x,y,z) TOS.symbol=x;TOS.section=y;TOS.value=z;INC;
355 #define POP(x,y,z) DEC;x=TOS.symbol;y=TOS.section;z=TOS.value;
356 ieee_value_type *sp = stack;
357
358 while (loop) {
359 switch (this_byte(abfd))
360 {
361 case ieee_variable_P_enum:
362 /* P variable, current program counter for section n */
363 {
364 int section_n ;
365 next_byte(abfd);
366 section_n = must_parse_int(abfd);
367 PUSH(NOSYMBOL, 0,
368 TOS.value = ieee_data(abfd)->section_table[section_n]->vma +
369 ieee_per_section(ieee_data(abfd)->section_table[section_n])->pc);
370 break;
371 }
372 case ieee_variable_L_enum:
373 /* L variable address of section N */
374 next_byte(abfd);
375 PUSH(NOSYMBOL,ieee_data(abfd)->section_table[must_parse_int(abfd)],0);
376 break;
377 case ieee_variable_R_enum:
378 /* R variable, logical address of section module */
379 /* FIXME, this should be different to L */
380 next_byte(abfd);
381 PUSH(NOSYMBOL,ieee_data(abfd)->section_table[must_parse_int(abfd)],0);
382 break;
383 case ieee_variable_S_enum:
384 /* S variable, size in MAUS of section module */
385 next_byte(abfd);
386 PUSH(NOSYMBOL,
387 0,
388 ieee_data(abfd)->section_table[must_parse_int(abfd)]->size);
389 break;
390
391 case ieee_variable_X_enum:
392 /* Push the address of external variable n */
393 {
394 ieee_symbol_index_type sy;
395 next_byte(abfd);
396 sy.index = (int)(must_parse_int(abfd)) ;
397 sy.letter = 'X';
398
399 PUSH(sy, 0, 0);
400 }
401 break;
402 case ieee_function_minus_enum:
403 {
404 bfd_vma value1, value2;
405 asection *section;
406 ieee_symbol_index_type sy;
407 next_byte(abfd);
408
409 POP(sy, section, value1);
410 POP(sy, section, value2);
411 PUSH(NOSYMBOL, 0, value1-value2);
412 }
413 break;
414 case ieee_function_plus_enum:
415 {
416 bfd_vma value1, value2;
417 asection *section1;
418 asection *section2;
419 ieee_symbol_index_type sy1;
420 ieee_symbol_index_type sy2;
421 next_byte(abfd);
422
423 POP(sy1, section1, value1);
424 POP(sy2, section2, value2);
425 PUSH(sy1.letter ? sy1 : sy2, section1 ? section1: section2, value1+value2);
426 }
427 break;
428 default:
429 {
430 bfd_vma va;
431 BFD_ASSERT(this_byte(abfd) < ieee_variable_A_enum
432 || this_byte(abfd) > ieee_variable_Z_enum);
433 if (parse_int(abfd, &va))
434 {
435 PUSH(NOSYMBOL,0, va);
436 }
437 else {
438 /*
439 Thats all that we can understand. As far as I can see
440 there is a bug in the Microtec IEEE output which I'm
441 using to scan, whereby the comma operator is ommited
442 sometimes in an expression, giving expressions with too
443 many terms. We can tell if that's the case by ensuring
444 that sp == stack here. If not, then we've pushed
445 something too far. -
446 */
447
448 POP(*symbol, *section, *value);
449 if (sp != stack) {
450 BFD_ASSERT(*section == 0);
451 *extra = *value;
452 /* Get what should be returned */
453 POP(*symbol, *section, *value);
454 }
455 else {
456 *extra = 0;
457 }
458 loop = false;
459 }
460 }
461
462 }
463 }
464 }
465
466
467
468 #define ieee_seek(abfd, offset) \
469 ieee_data(abfd)->input_p = ieee_data(abfd)->first_byte + offset
470
471 static void
472 DEFUN(ieee_slurp_external_symbols,(abfd),
473 bfd *abfd)
474 {
475 ieee_data_type *ieee = ieee_data(abfd);
476 file_ptr offset = ieee->w.r.external_part;
477
478 ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
479 ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
480 ieee_symbol_type *symbol;
481 unsigned int symbol_count = 0;
482 boolean loop = true;
483
484 ieee->symbol_table_full = true;
485
486 ieee_seek(abfd, offset );
487
488 while (loop) {
489 switch (this_byte(abfd)) {
490 case ieee_external_symbol_enum:
491 next_byte(abfd);
492 symbol = (ieee_symbol_type *)ieee_malloc(abfd, sizeof(ieee_symbol_type));
493
494 *prev_symbols_ptr = symbol;
495 prev_symbols_ptr= &symbol->next;
496 symbol->index = must_parse_int(abfd);
497 if (symbol->index > ieee->external_symbol_max_index) {
498 ieee->external_symbol_max_index = symbol->index;
499 }
500 BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
501 symbol_count++;
502 symbol->symbol.the_bfd = abfd;
503 symbol->symbol.name = read_id(abfd);
504 symbol->symbol.udata = (void *)NULL;
505 symbol->symbol.flags = BSF_NO_FLAGS;
506 break;
507 case ieee_attribute_record_enum >> 8:
508 {
509 unsigned int symbol_name_index;
510 unsigned int symbol_type_index;
511 unsigned int symbol_attribute_def;
512 bfd_vma value;
513 next_byte(abfd); /* Skip prefix */
514 next_byte(abfd);
515 symbol_name_index = must_parse_int(abfd);
516 symbol_type_index = must_parse_int(abfd);
517 symbol_attribute_def = must_parse_int(abfd);
518
519 parse_int(abfd,&value);
520
521 }
522 break;
523 case ieee_value_record_enum >> 8:
524 {
525 unsigned int symbol_name_index;
526 ieee_symbol_index_type symbol_ignore;
527 boolean pcrel_ignore;
528 unsigned int extra_ignore;
529 next_byte(abfd);
530 next_byte(abfd);
531
532 symbol_name_index = must_parse_int(abfd);
533 parse_expression(abfd,
534 &symbol->symbol.value,
535 &symbol->symbol.section,
536 &symbol_ignore,
537 &pcrel_ignore,
538 &extra_ignore);
539 if (symbol->symbol.section != (asection *)NULL) {
540 symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
541 }
542 else {
543 symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
544 }
545 }
546 break;
547 case ieee_weak_external_reference_enum:
548 { bfd_vma size;
549 bfd_vma value ;
550 next_byte(abfd);
551 /* Throw away the external reference index */
552 (void)must_parse_int(abfd);
553 /* Fetch the default size if not resolved */
554 size = must_parse_int(abfd);
555 /* Fetch the defautlt value if available */
556 if ( parse_int(abfd, &value) == false) {
557 value = 0;
558 }
559 /* This turns into a common */
560 symbol->symbol.flags = BSF_FORT_COMM;
561 symbol->symbol.value = size;
562 }
563 break;
564
565 case ieee_external_reference_enum:
566 next_byte(abfd);
567 symbol = (ieee_symbol_type *)ieee_malloc(abfd, sizeof(ieee_symbol_type));
568 symbol_count++;
569 *prev_reference_ptr = symbol;
570 prev_reference_ptr = &symbol->next;
571 symbol->index = must_parse_int(abfd);
572 symbol->symbol.the_bfd = abfd;
573 symbol->symbol.name = read_id(abfd);
574 symbol->symbol.udata = (void *)NULL;
575 symbol->symbol.section = (asection *)NULL;
576 symbol->symbol.value = (bfd_vma)0;
577 symbol->symbol.flags = BSF_UNDEFINED;
578 if (symbol->index > ieee->external_reference_max_index) {
579 ieee->external_reference_max_index = symbol->index;
580 }
581 BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
582 break;
583
584 default:
585 loop = false;
586 }
587 }
588
589 if (ieee->external_symbol_max_index != 0) {
590 ieee->external_symbol_count =
591 ieee->external_symbol_max_index -
592 ieee->external_symbol_min_index + 1 ;
593 }
594 else {
595 ieee->external_symbol_count = 0;
596 }
597
598
599 if(ieee->external_reference_max_index != 0) {
600 ieee->external_reference_count =
601 ieee->external_reference_max_index -
602 ieee->external_reference_min_index + 1;
603 }
604 else {
605 ieee->external_reference_count = 0;
606 }
607
608 abfd->symcount =
609 ieee->external_reference_count + ieee->external_symbol_count;
610
611 if (symbol_count != abfd->symcount) {
612 /* There are gaps in the table -- */
613 ieee->symbol_table_full = false;
614 }
615 *prev_symbols_ptr = (ieee_symbol_type *)NULL;
616 *prev_reference_ptr = (ieee_symbol_type *)NULL;
617 }
618
619 static void
620 DEFUN(ieee_slurp_symbol_table,(abfd),
621 bfd *abfd)
622 {
623 if (ieee_data(abfd)->read_symbols == false) {
624 ieee_slurp_external_symbols(abfd);
625 ieee_data(abfd)->read_symbols= true;
626 }
627 }
628
629 size_t
630 DEFUN(ieee_get_symtab_upper_bound,(abfd),
631 bfd *abfd)
632 {
633 ieee_slurp_symbol_table (abfd);
634
635 return (abfd->symcount != 0) ?
636 (abfd->symcount+1) * (sizeof (ieee_symbol_type *)) : 0;
637 }
638
639 /*
640 Move from our internal lists to the canon table, and insert in
641 symbol index order
642 */
643
644 extern bfd_target ieee_vec;
645
646 unsigned int
647 DEFUN(ieee_get_symtab,(abfd, location),
648 bfd *abfd AND
649 asymbol **location)
650 {
651 ieee_symbol_type *symp;
652 static bfd dummy_bfd;
653 static asymbol empty_symbol =
654 { &dummy_bfd," ieee empty",(symvalue)0,BSF_DEBUGGING | BSF_FAKE};
655
656 ieee_data_type *ieee = ieee_data(abfd);
657 dummy_bfd.xvec= &ieee_vec;
658 ieee_slurp_symbol_table(abfd);
659
660 if (ieee->symbol_table_full == false) {
661 /* Arrgh - there are gaps in the table, run through and fill them */
662 /* up with pointers to a null place */
663 unsigned int i;
664 for (i= 0; i < abfd->symcount; i++) {
665 location[i] = &empty_symbol;
666 }
667 }
668
669
670 ieee->external_symbol_base_offset= - ieee->external_symbol_min_index;
671 for (symp = ieee_data(abfd)->external_symbols;
672 symp != (ieee_symbol_type *)NULL;
673 symp = symp->next) {
674 /* Place into table at correct index locations */
675 location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
676
677 }
678
679 /* The external refs are indexed in a bit */
680 ieee->external_reference_base_offset =
681 - ieee->external_reference_min_index +ieee->external_symbol_count ;
682
683 for (symp = ieee_data(abfd)->external_reference;
684 symp != (ieee_symbol_type *)NULL;
685 symp = symp->next) {
686 location[symp->index + ieee->external_reference_base_offset] =
687 &symp->symbol;
688
689 }
690
691
692
693 location[abfd->symcount] = (asymbol *)NULL;
694
695 return abfd->symcount;
696 }
697
698
699 static void
700 DEFUN(ieee_slurp_sections,(abfd),
701 bfd *abfd)
702 {
703 ieee_data_type *ieee = ieee_data(abfd);
704 file_ptr offset = ieee->w.r.section_part;
705
706 asection *section = (asection *)NULL;
707
708 if (offset != 0) {
709 bfd_byte section_type[3];
710 ieee_seek(abfd, offset);
711 while (true) {
712 switch (this_byte(abfd)) {
713 case ieee_section_type_enum:
714 {
715 unsigned int section_index ;
716 next_byte(abfd);
717 section_index = must_parse_int(abfd);
718 /* Fixme to be nice about a silly number of sections */
719 BFD_ASSERT(section_index < NSECTIONS);
720
721 section = bfd_make_section(abfd, " tempname");
722 ieee->section_table[section_index] = section;
723 section->flags = SEC_NO_FLAGS;
724 section->target_index = section_index;
725 section_type[0] = this_byte_and_next(abfd);
726 switch (section_type[0]) {
727 case 0xC3:
728 section_type[1] = this_byte(abfd);
729 section->flags = SEC_LOAD;
730 switch (section_type[1]) {
731 case 0xD0:
732 /* Normal code */
733 next_byte(abfd);
734 section->flags |= SEC_LOAD | SEC_CODE;
735 break;
736 case 0xC4:
737 next_byte(abfd);
738 section->flags |= SEC_LOAD | SEC_DATA;
739 /* Normal data */
740 break;
741 case 0xD2:
742 next_byte(abfd);
743 /* Normal rom data */
744 section->flags |= SEC_LOAD | SEC_ROM | SEC_DATA;
745 break;
746 default:
747 break;
748 }
749 }
750 section->name = read_id(abfd);
751 { bfd_vma parent, brother, context;
752 parse_int(abfd, &parent);
753 parse_int(abfd, &brother);
754 parse_int(abfd, &context);
755 }
756
757
758 }
759 break;
760 case ieee_section_alignment_enum:
761 {
762 unsigned int section_index;
763 bfd_vma value;
764 next_byte(abfd);
765 section_index = must_parse_int(abfd);
766 if (section_index > ieee->section_count) {
767 ieee->section_count = section_index;
768 }
769 ieee->section_table[section_index]->alignment_power =
770 bfd_log2(must_parse_int(abfd));
771 (void)parse_int(abfd, & value);
772 }
773 break;
774 case ieee_e2_first_byte_enum:
775 {
776 ieee_record_enum_type t = read_2bytes(abfd);
777 switch (t) {
778 case ieee_section_size_enum:
779 section = ieee->section_table[must_parse_int(abfd)];
780 section->size = must_parse_int(abfd);
781 break;
782 case ieee_physical_region_size_enum:
783 section = ieee->section_table[must_parse_int(abfd)];
784 section->size = must_parse_int(abfd);
785 break;
786 case ieee_region_base_address_enum:
787 section = ieee->section_table[must_parse_int(abfd)];
788 section->vma = must_parse_int(abfd);
789 break;
790 case ieee_mau_size_enum:
791 must_parse_int(abfd);
792 must_parse_int(abfd);
793 break;
794 case ieee_m_value_enum:
795 must_parse_int(abfd);
796 must_parse_int(abfd);
797 break;
798 case ieee_section_base_address_enum:
799 section = ieee->section_table[must_parse_int(abfd)];
800 section->vma = must_parse_int(abfd);
801 break;
802 case ieee_section_offset_enum:
803 (void) must_parse_int(abfd);
804 (void) must_parse_int(abfd);
805 break;
806 default:
807 return;
808 }
809 }
810 break;
811 default:
812 return;
813 }
814 }
815 }
816 }
817
818 /***********************************************************************
819 * archive stuff
820 */
821 bfd_target *
822 DEFUN(ieee_archive_p,(abfd),
823 bfd *abfd)
824 {
825 char *library;
826 boolean loop;
827 ieee_ar_data_type *ar;
828 unsigned int i;
829 return 0; /* FIXME */
830 ieee_seek(abfd, (file_ptr) 0);
831 if (this_byte(abfd) != Module_Beginning) return (bfd_target*)NULL;
832 next_byte(abfd);
833 library= read_id(abfd);
834 if (strcmp(library , "LIBRARY") != 0) {
835 free(library);
836 return (bfd_target *)NULL;
837 }
838 /* Throw away the filename */
839 free( read_id(abfd));
840 /* This must be an IEEE archive, so we'll buy some space to do
841 things */
842 ar = (ieee_ar_data_type *) malloc(sizeof(ieee_ar_data_type));
843 set_tdata (abfd, ar);
844 ar->element_count = 0;
845 ar->element_index = 0;
846 obstack_init(&ar->element_obstack);
847
848 next_byte(abfd); /* Drop the ad part */
849 must_parse_int(abfd); /* And the two dummy numbers */
850 must_parse_int(abfd);
851
852 loop = true;
853 /* Read the index of the BB table */
854 while (loop) {
855 ieee_ar_obstack_type t;
856 int rec =read_2bytes(abfd);
857 if (rec ==ieee_assign_value_to_variable_enum) {
858 int record_number = must_parse_int(abfd);
859 t.file_offset = must_parse_int(abfd);
860 t.abfd = (bfd *)NULL;
861 ar->element_count++;
862 obstack_grow(&ar->element_obstack, (PTR)&t, sizeof(t));
863 }
864 else loop = false;
865 }
866 ar->elements = (ieee_ar_obstack_type *)obstack_base(&ar->element_obstack);
867
868 /* Now scan the area again, and replace BB offsets with file */
869 /* offsets */
870
871
872 for (i = 2; i < ar->element_count; i++) {
873 ieee_seek(abfd, ar->elements[i].file_offset);
874 next_byte(abfd); /* Drop F8 */
875 next_byte(abfd); /* Drop 14 */
876 must_parse_int(abfd); /* Drop size of block */
877 if (must_parse_int(abfd) != 0) {
878 /* This object has been deleted */
879 ar->elements[i].file_offset = 0;
880 }
881 else {
882 ar->elements[i].file_offset = must_parse_int(abfd);
883 }
884 }
885
886 obstack_finish(&ar->element_obstack);
887 return abfd->xvec;
888 }
889
890 static boolean
891 DEFUN(ieee_mkobject,(abfd),
892 bfd *abfd)
893 {
894 struct obstack tmp_obstack;
895 ieee_data_type *ieee;
896
897 obstack_init(&tmp_obstack);
898 BFD_ASSERT(ieee_data(abfd) == 0);
899 set_tdata (abfd, obstack_alloc(&tmp_obstack,sizeof(ieee_data_type)));
900 ieee = ieee_data(abfd);
901 ieee->ieee_obstack = tmp_obstack;
902 return true;
903 }
904
905 bfd_target *
906 DEFUN(ieee_object_p,(abfd),
907 bfd *abfd)
908 {
909 char *processor;
910 unsigned int part;
911 ieee_data_type *ieee;
912 char buffer[300];
913
914 set_tdata (abfd, 0);
915 ieee_mkobject(abfd);
916 ieee = ieee_data(abfd);
917
918 /* Read the first few bytes in to see if it makes sense */
919 bfd_read(buffer, 1, sizeof(buffer), abfd);
920
921 ptr(abfd)= buffer;
922 if (*(ptr(abfd)++) != Module_Beginning) goto fail;
923
924 ieee->read_symbols= false;
925 ieee->read_data= false;
926 ieee->section_count = 0;
927 ieee->external_symbol_max_index = 0;
928 ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
929 ieee->external_reference_min_index =IEEE_REFERENCE_BASE;
930 ieee->external_reference_max_index = 0;
931 memset((PTR)ieee->section_table, 0, sizeof(ieee->section_table));
932
933 processor = ieee->mb.processor = read_id(abfd);
934 if (strcmp(processor,"LIBRARY") == 0) goto fail;
935 ieee->mb.module_name = read_id(abfd);
936 if (abfd->filename == (char *)NULL) {
937 abfd->filename = ieee->mb.module_name;
938 }
939 /* Determine the architecture and machine type of the object file. */
940 bfd_scan_arch_mach(processor, &abfd->obj_arch, &abfd->obj_machine);
941
942 if (this_byte(abfd) != ieee_address_descriptor_enum) {
943 goto fail;
944 }
945 next_byte(abfd);
946
947 if (parse_int(abfd, &ieee->ad.number_of_bits_mau) == false) {
948 goto fail;
949 }
950 if(parse_int(abfd, &ieee->ad.number_of_maus_in_address) == false) {
951 goto fail;
952 }
953
954 /* If there is a byte order info, take it */
955 if (this_byte(abfd) == ieee_variable_L_enum ||
956 this_byte(abfd) == ieee_variable_M_enum)
957 next_byte(abfd);
958
959
960 for (part = 0; part < N_W_VARIABLES; part++) {
961 boolean ok;
962 if (read_2bytes(abfd) != ieee_assign_value_to_variable_enum) {
963 goto fail;
964 }
965 if (this_byte_and_next(abfd) != part) {
966 goto fail;
967 }
968
969 ieee->w.offset[part] = parse_i(abfd, &ok);
970 if (ok==false) {
971 goto fail;
972 }
973
974 }
975 abfd->flags = HAS_SYMS;
976
977 /* By now we know that this is a real IEEE file, we're going to read
978 the whole thing into memory so that we can run up and down it
979 quickly. We can work out how big the file is from the trailer
980 record */
981
982 ieee_data(abfd)->first_byte = ieee_malloc(abfd, ieee->w.r.me_record
983 + 50);
984 bfd_seek(abfd, 0, 0);
985 bfd_read(ieee_data(abfd)->first_byte, 1, ieee->w.r.me_record+50, abfd);
986
987 ieee_slurp_sections(abfd);
988 return abfd->xvec;
989 fail:
990 obstack_finish(&ieee->ieee_obstack);
991 return (bfd_target *)NULL;
992 }
993
994
995 void
996 DEFUN(ieee_print_symbol,(ignore_abfd, file, symbol, how),
997 bfd *ignore_abfd AND
998 FILE *file AND
999 asymbol *symbol AND
1000 bfd_print_symbol_enum_type how)
1001 {
1002 switch (how) {
1003 case bfd_print_symbol_name_enum:
1004 fprintf(file,"%s", symbol->name);
1005 break;
1006 case bfd_print_symbol_type_enum:
1007 #if 0
1008 fprintf(file,"%4x %2x",aout_symbol(symbol)->desc & 0xffff,
1009 aout_symbol(symbol)->other & 0xff);
1010 #endif
1011 BFD_FAIL();
1012 break;
1013 case bfd_print_symbol_all_enum:
1014 {
1015 CONST char *section_name = symbol->section == (asection *)NULL ?
1016 "*abs" : symbol->section->name;
1017
1018 bfd_print_symbol_vandf((void *)file,symbol);
1019
1020 fprintf(file," %-5s %04x %02x %s",
1021 section_name,
1022 (unsigned) ieee_symbol(symbol)->index,
1023 (unsigned) 0, /*
1024 aout_symbol(symbol)->desc & 0xffff,
1025 aout_symbol(symbol)->other & 0xff,*/
1026 symbol->name);
1027 }
1028 break;
1029 }
1030 }
1031
1032
1033
1034 /* Read in all the section data and relocation stuff too */
1035 static boolean
1036 DEFUN(ieee_slurp_section_data,(abfd),
1037 bfd *abfd)
1038 {
1039 bfd_byte *location_ptr ;
1040 ieee_data_type *ieee = ieee_data(abfd);
1041 unsigned int section_number ;
1042
1043 ieee_per_section_type *current_map;
1044 asection *s;
1045 /* Seek to the start of the data area */
1046 if (ieee->read_data== true) return true;
1047 ieee->read_data = true;
1048 ieee_seek(abfd, ieee->w.r.data_part);
1049
1050 /* Allocate enough space for all the section contents */
1051
1052
1053 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
1054 ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1055 per->data = (bfd_byte *) ieee_malloc(abfd, s->size);
1056 /*SUPPRESS 68*/
1057 per->reloc_tail_ptr =
1058 (ieee_reloc_type **)&(s->relocation);
1059 }
1060
1061
1062
1063 while (true) {
1064 switch (this_byte(abfd))
1065 {
1066 /* IF we see anything strange then quit */
1067 default:
1068 return true;
1069
1070 case ieee_set_current_section_enum:
1071 next_byte(abfd);
1072 section_number = must_parse_int(abfd);
1073 s = ieee->section_table[section_number];
1074 current_map = (ieee_per_section_type *) s->used_by_bfd;
1075 location_ptr = current_map->data - s->vma;
1076 /* The document I have says that Microtec's compilers reset */
1077 /* this after a sec section, even though the standard says not */
1078 /* to. SO .. */
1079 current_map->pc =s->vma;
1080 break;
1081
1082 case ieee_load_constant_bytes_enum:
1083 {
1084 unsigned int number_of_maus;
1085 unsigned int i;
1086 next_byte(abfd);
1087 number_of_maus = must_parse_int(abfd);
1088
1089 for (i = 0; i < number_of_maus; i++) {
1090 location_ptr[current_map->pc++]= this_byte(abfd);
1091 next_byte(abfd);
1092 }
1093 }
1094 break;
1095
1096 case ieee_e2_first_byte_enum:
1097 next_byte(abfd);
1098 switch (this_byte(abfd))
1099 {
1100 case ieee_set_current_pc_enum & 0xff:
1101 {
1102 bfd_vma value;
1103 asection *dsection;
1104 ieee_symbol_index_type symbol;
1105 unsigned int extra;
1106 boolean pcrel;
1107 next_byte(abfd);
1108 must_parse_int(abfd); /* Thow away section #*/
1109 parse_expression(abfd, &value, &dsection, &symbol, &pcrel, &extra);
1110 current_map->pc = value;
1111 BFD_ASSERT((unsigned)(value - s->vma) <= s->size);
1112 }
1113 break;
1114
1115 case ieee_value_starting_address_enum & 0xff:
1116 /* We've got to the end of the data now - */
1117 return true;
1118 break;
1119 default:
1120 BFD_FAIL();
1121 return true;
1122 }
1123 break;
1124 case ieee_load_with_relocation_enum:
1125 {
1126 boolean loop = true;
1127 next_byte(abfd);
1128 while (loop)
1129 {
1130 switch (this_byte(abfd))
1131 {
1132 case ieee_variable_R_enum:
1133
1134 case ieee_function_signed_open_b_enum:
1135 case ieee_function_unsigned_open_b_enum:
1136 case ieee_function_either_open_b_enum:
1137 {
1138 unsigned int extra;
1139 boolean pcrel;
1140
1141 ieee_reloc_type *r =
1142 (ieee_reloc_type *) ieee_malloc(abfd,
1143 sizeof(ieee_reloc_type));
1144
1145 *(current_map->reloc_tail_ptr) = r;
1146 current_map->reloc_tail_ptr= &r->next;
1147 r->next = (ieee_reloc_type *)NULL;
1148 next_byte(abfd);
1149 parse_expression(abfd,
1150 &r->relent.addend,
1151 &r->relent.section,
1152 &r->symbol,
1153 &pcrel,
1154 &extra);
1155 r->relent.address = current_map->pc;
1156 s->reloc_count++;
1157 switch (this_byte(abfd)) {
1158 case ieee_function_signed_close_b_enum:
1159 next_byte(abfd);
1160 break;
1161 case ieee_function_unsigned_close_b_enum:
1162 next_byte(abfd);
1163 break;
1164 case ieee_function_either_close_b_enum:
1165 next_byte(abfd);
1166 break;
1167 default:
1168 break;
1169 }
1170 /* Build a relocation entry for this type */
1171 if (this_byte(abfd) == ieee_comma) {
1172
1173 next_byte(abfd);
1174 /* Fetch number of bytes to pad */
1175 extra = must_parse_int(abfd);
1176 BFD_FAIL();
1177 }
1178 switch (extra) {
1179 case 0:
1180 case 4:
1181 location_ptr[current_map->pc++] = 0;
1182 location_ptr[current_map->pc++] = 0;
1183 location_ptr[current_map->pc++] = 0;
1184 location_ptr[current_map->pc++] = 0;
1185 r->relent.howto = &abs32_howto;
1186 break;
1187 case 2:
1188 location_ptr[current_map->pc++] = 0;
1189 location_ptr[current_map->pc++] = 0;
1190 r->relent.howto = &abs16_howto;
1191 break;
1192
1193 default:
1194 BFD_FAIL();
1195 break;
1196 }
1197 }
1198 break;
1199 default:
1200 {
1201 bfd_vma this_size ;
1202 if (parse_int(abfd, &this_size) == true) {
1203 unsigned int i;
1204 for (i = 0; i < this_size; i++) {
1205 location_ptr[current_map->pc ++] = this_byte(abfd);
1206 next_byte(abfd);
1207 }
1208 }
1209 else {
1210 loop = false;
1211 }
1212 }
1213 }
1214 }
1215 }
1216 }
1217 }
1218 }
1219
1220
1221
1222 boolean
1223 DEFUN(ieee_new_section_hook,(abfd, newsect),
1224 bfd *abfd AND
1225 asection *newsect)
1226 {
1227 newsect->used_by_bfd = (PTR)
1228 ieee_malloc(abfd, sizeof(ieee_per_section_type));
1229 ieee_per_section( newsect)->data = (bfd_byte *)NULL;
1230 ieee_per_section(newsect)->section = newsect;
1231 return true;
1232 }
1233
1234
1235 unsigned int
1236 DEFUN(ieee_get_reloc_upper_bound,(abfd, asect),
1237 bfd *abfd AND
1238 sec_ptr asect)
1239 {
1240 ieee_slurp_section_data(abfd);
1241 return (asect->reloc_count+1) * sizeof(arelent *);
1242 }
1243
1244 static boolean
1245 DEFUN(ieee_get_section_contents,(abfd, section, location, offset, count),
1246 bfd *abfd AND
1247 sec_ptr section AND
1248 void *location AND
1249 file_ptr offset AND
1250 unsigned int count)
1251 {
1252 ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
1253 ieee_slurp_section_data(abfd);
1254 (void) memcpy(location, p->data + offset, count);
1255 return true;
1256 }
1257
1258
1259 unsigned int
1260 DEFUN(ieee_canonicalize_reloc,(abfd, section, relptr, symbols),
1261 bfd *abfd AND
1262 sec_ptr section AND
1263 arelent **relptr AND
1264 asymbol **symbols)
1265 {
1266 ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
1267 ieee_reloc_type *src = (ieee_reloc_type *)(section->relocation);
1268 ieee_data_type *ieee = ieee_data(abfd);
1269
1270 while (src != (ieee_reloc_type *)NULL) {
1271 /* Work out which symbol to attatch it this reloc to */
1272 switch (src->symbol.letter) {
1273 case 'X':
1274 src->relent.sym_ptr_ptr =
1275 symbols + src->symbol.index + ieee->external_reference_base_offset;
1276 break;
1277 case 0:
1278 src->relent.sym_ptr_ptr = (asymbol **)NULL;
1279 break;
1280 default:
1281
1282 BFD_FAIL();
1283 }
1284 *relptr++ = &src->relent;
1285 src = src->next;
1286 }
1287 *relptr = (arelent *)NULL;
1288 return section->reloc_count;
1289 }
1290
1291 boolean
1292 DEFUN(ieee_set_arch_mach,(abfd, arch, machine),
1293 bfd *abfd AND
1294 enum bfd_architecture arch AND
1295 unsigned long machine)
1296 {
1297 abfd->obj_arch = arch;
1298 abfd->obj_machine = machine;
1299 return true;
1300 }
1301
1302
1303 static int
1304 DEFUN(comp,(ap, bp),
1305 CONST PTR ap AND
1306 CONST PTR bp)
1307 {
1308 arelent *a = *((arelent **)ap);
1309 arelent *b = *((arelent **)bp);
1310 return a->address - b->address;
1311 }
1312
1313 /*
1314 Write the section headers
1315 */
1316
1317 static void
1318 DEFUN(ieee_write_section_part,(abfd),
1319 bfd *abfd)
1320 {
1321 ieee_data_type *ieee = ieee_data(abfd);
1322 asection *s;
1323 ieee->w.r.section_part = bfd_tell(abfd);
1324 for (s = abfd->sections; s != (asection *)NULL; s=s->next) {
1325 ieee_write_byte(abfd, ieee_section_type_enum);
1326 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1327
1328 switch (s->flags & (SEC_LOAD | SEC_CODE | SEC_DATA | SEC_ROM)) {
1329 case SEC_LOAD | SEC_CODE:
1330 /* Normal named section, code */
1331 ieee_write_byte(abfd, ieee_variable_C_enum);
1332 ieee_write_byte(abfd, ieee_variable_P_enum);
1333 break;
1334 case SEC_LOAD | SEC_DATA:
1335 /* Normal named section, data */
1336 ieee_write_byte(abfd, ieee_variable_C_enum);
1337 ieee_write_byte(abfd, ieee_variable_D_enum);
1338 break;
1339 case SEC_LOAD | SEC_DATA | SEC_ROM:
1340 /* Normal named section, data rom */
1341 ieee_write_byte(abfd, ieee_variable_C_enum);
1342 ieee_write_byte(abfd, ieee_variable_R_enum);
1343 break;
1344 default:
1345 ieee_write_byte(abfd, ieee_variable_C_enum);
1346 break;
1347 }
1348
1349 ieee_write_id(abfd, s->name);
1350 ieee_write_int(abfd, 0); /* Parent */
1351 ieee_write_int(abfd, 0); /* Brother */
1352 ieee_write_int(abfd, 0); /* Context */
1353
1354 /* Alignment */
1355 ieee_write_byte(abfd, ieee_section_alignment_enum);
1356 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1357 ieee_write_int(abfd, 1 << s->alignment_power);
1358
1359 /* Size */
1360 ieee_write_2bytes(abfd, ieee_section_size_enum);
1361 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1362 ieee_write_int(abfd, s->size);
1363
1364 /* Vma */
1365 ieee_write_2bytes(abfd, ieee_region_base_address_enum);
1366 ieee_write_byte(abfd, s->index + IEEE_SECTION_NUMBER_BASE);
1367 ieee_write_int(abfd, s->vma);
1368
1369 }
1370 }
1371
1372
1373
1374 /* write the data in an ieee way */
1375 static void
1376 DEFUN(ieee_write_data_part,(abfd),
1377 bfd *abfd)
1378 {
1379 asection *s;
1380 ieee_data_type *ieee = ieee_data(abfd);
1381 ieee->w.r.data_part = bfd_tell(abfd);
1382 for (s = abfd->sections; s != (asection *)NULL; s = s->next)
1383 {
1384 bfd_byte header[11];
1385 bfd_byte *stream = ieee_per_section(s)->data;
1386 arelent **p = s->orelocation;
1387 unsigned int relocs_to_go = s->reloc_count;
1388 size_t current_byte_index = 0;
1389
1390
1391 /* Sort the reloc records so we can insert them in the correct
1392 places */
1393 if (s->reloc_count != 0) {
1394 qsort(s->orelocation,
1395 s->reloc_count,
1396 sizeof(arelent **),
1397 comp);
1398 }
1399
1400
1401 /* Output the section preheader */
1402 header[0] =ieee_set_current_section_enum;
1403 header[1] = s->index + IEEE_SECTION_NUMBER_BASE;
1404
1405 header[2] = ieee_set_current_pc_enum >> 8;
1406 header[3]= ieee_set_current_pc_enum & 0xff;
1407 header[4] = s->index + IEEE_SECTION_NUMBER_BASE;
1408 ieee_write_int5(header+5, s->vma );
1409 header[10] = ieee_load_with_relocation_enum;
1410 bfd_write(header, 1, sizeof(header), abfd);
1411
1412 /* Output the data stream as the longest sequence of bytes
1413 possible, allowing for the a reasonable packet size and
1414 relocation stuffs */
1415
1416 if (stream == (void *)NULL) {
1417 /* Outputting a section without data, fill it up */
1418 stream = ieee_malloc(abfd, s->size);
1419 memset(stream, 0, s->size);
1420 }
1421 while (current_byte_index < s->size) {
1422 size_t run;
1423 unsigned int MAXRUN = 32;
1424 if (p && *p) {
1425 run = (*p)->address - current_byte_index;
1426 }
1427 else {
1428 run = MAXRUN;
1429 }
1430 if (run > s->size - current_byte_index) {
1431 run = s->size - current_byte_index;
1432 }
1433
1434 if (run != 0) {
1435 /* Output a stream of bytes */
1436 ieee_write_int(abfd, run);
1437 bfd_write(stream + current_byte_index,
1438 1,
1439 run,
1440 abfd);
1441 current_byte_index += run;
1442 }
1443 /* Output any relocations here */
1444 if (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
1445 while (relocs_to_go && (*p) && (*p)->address == current_byte_index) {
1446
1447 arelent *r = *p;
1448 ieee_write_byte(abfd, ieee_function_either_open_b_enum);
1449 if (r->sym_ptr_ptr != (asymbol **)NULL) {
1450 ieee_write_expression(abfd, r->addend,
1451 r->section,
1452 *(r->sym_ptr_ptr));
1453 }
1454 else {
1455 ieee_write_expression(abfd, r->addend,
1456 r->section,
1457 (asymbol *)NULL);
1458 }
1459 ieee_write_byte(abfd,
1460 ieee_function_either_close_b_enum);
1461 relocs_to_go --;
1462 p++;
1463 }
1464 /* FIXME !! Are all relocations 4 bytes ? */
1465 current_byte_index += 4;
1466 }
1467 }
1468 }
1469
1470 }
1471
1472
1473
1474 static void
1475 DEFUN(init_for_output,(abfd),
1476 bfd *abfd)
1477 {
1478 asection *s;
1479 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
1480 if (s->size != 0) {
1481 ieee_per_section(s)->data = (bfd_byte *)(ieee_malloc(abfd, s->size));
1482 }
1483 }
1484 }
1485
1486 /** exec and core file sections */
1487
1488 /* set section contents is complicated with IEEE since the format is
1489 * not a byte image, but a record stream.
1490 */
1491 boolean
1492 DEFUN(ieee_set_section_contents,(abfd, section, location, offset, count),
1493 bfd *abfd AND
1494 sec_ptr section AND
1495 unsigned char *location AND
1496 file_ptr offset AND
1497 int count)
1498 {
1499 if (ieee_per_section(section)->data == (bfd_byte *)NULL) {
1500 init_for_output(abfd);
1501 }
1502 (void) memcpy(ieee_per_section(section)->data + offset, location, count);
1503 return true;
1504 }
1505
1506 /*
1507 write the external symbols of a file, IEEE considers two sorts of
1508 external symbols, public, and referenced. It uses to internal forms
1509 to index them as well. When we write them out we turn their symbol
1510 values into indexes from the right base.
1511 */
1512 static void
1513 DEFUN(ieee_write_external_part,(abfd),
1514 bfd *abfd)
1515 {
1516 asymbol **q;
1517 ieee_data_type *ieee = ieee_data(abfd);
1518
1519 unsigned int reference_index = IEEE_REFERENCE_BASE;
1520 unsigned int public_index = IEEE_PUBLIC_BASE;
1521 ieee->w.r.external_part = bfd_tell(abfd);
1522 if (abfd->outsymbols != (asymbol **)NULL) {
1523 for (q = abfd->outsymbols; *q != (asymbol *)NULL; q++) {
1524 asymbol *p = *q;
1525 if (p->flags & BSF_UNDEFINED) {
1526 /* This must be a symbol reference .. */
1527 ieee_write_byte(abfd, ieee_external_reference_enum);
1528 ieee_write_int(abfd, reference_index);
1529 ieee_write_id(abfd, p->name);
1530 p->value = reference_index;
1531 reference_index++;
1532 }
1533 else if(p->flags & BSF_FORT_COMM) {
1534 /* This is a weak reference */
1535 ieee_write_byte(abfd, ieee_external_reference_enum);
1536 ieee_write_int(abfd, reference_index);
1537 ieee_write_id(abfd, p->name);
1538 ieee_write_byte(abfd, ieee_weak_external_reference_enum);
1539 ieee_write_int(abfd, reference_index);
1540 ieee_write_int(abfd, p->value);
1541 ieee_write_int(abfd, BFD_FORT_COMM_DEFAULT_VALUE);
1542 p->value = reference_index;
1543 reference_index++;
1544 }
1545 else if(p->flags & BSF_GLOBAL) {
1546 /* This must be a symbol definition */
1547
1548 ieee_write_byte(abfd, ieee_external_symbol_enum);
1549 ieee_write_int(abfd, public_index );
1550 ieee_write_id(abfd, p->name);
1551
1552 /* Write out the value */
1553 ieee_write_2bytes(abfd, ieee_value_record_enum);
1554 ieee_write_int(abfd, public_index);
1555 if (p->section != (asection *)NULL)
1556 {
1557 ieee_write_expression(abfd,
1558 p->value + p->section->output_offset,
1559 p->section->output_section,
1560 (asymbol *)NULL);
1561 }
1562 else
1563 {
1564 ieee_write_expression(abfd,
1565 p->value,
1566 (asection *)NULL,
1567 (asymbol *)NULL);
1568 }
1569 p->value = public_index;
1570 public_index++;
1571 }
1572 else {
1573 /* This can happen - when there are gaps in the symbols read */
1574 /* from an input ieee file */
1575 }
1576 }
1577 }
1578
1579 }
1580
1581 static
1582 void
1583 DEFUN(ieee_write_me_part,(abfd),
1584 bfd *abfd)
1585 {
1586 ieee_data_type *ieee= ieee_data(abfd);
1587 ieee->w.r.me_record = bfd_tell(abfd);
1588
1589 ieee_write_2bytes(abfd, ieee_value_starting_address_enum);
1590 ieee_write_int(abfd, abfd->start_address);
1591 ieee_write_byte(abfd, ieee_module_end_enum);
1592
1593 }
1594 boolean
1595 DEFUN(ieee_write_object_contents,(abfd),
1596 bfd *abfd)
1597 {
1598 ieee_data_type *ieee = ieee_data(abfd);
1599 unsigned int i;
1600 file_ptr old;
1601 /* Fast forward over the header area */
1602 bfd_seek(abfd, 0, 0);
1603 ieee_write_byte(abfd, ieee_module_beginning_enum);
1604
1605 ieee_write_id(abfd, bfd_printable_arch_mach(abfd->obj_arch,
1606 abfd->obj_machine));
1607 ieee_write_id(abfd, abfd->filename);
1608 ieee_write_byte(abfd, ieee_address_descriptor_enum);
1609 ieee_write_byte(abfd, 8); /* Bits per MAU */
1610 ieee_write_byte(abfd, 4); /* MAU's per address */
1611
1612 /* Fast forward over the variable bits */
1613 old = bfd_tell(abfd);
1614 bfd_seek(abfd, 8 * N_W_VARIABLES, 1);
1615
1616 /*
1617 First write the symbols, this changes their values into table
1618 indeces so we cant use it after this point
1619 */
1620 ieee_write_external_part(abfd);
1621 ieee_write_byte(abfd, ieee_record_seperator_enum);
1622
1623 ieee_write_section_part(abfd);
1624 ieee_write_byte(abfd, ieee_record_seperator_enum);
1625 /*
1626 Can only write the data once the symbols have been written since
1627 the data contains relocation information which points to the
1628 symbols
1629 */
1630 ieee_write_data_part(abfd);
1631 ieee_write_byte(abfd, ieee_record_seperator_enum);
1632
1633 /*
1634 At the end we put the end !
1635 */
1636 ieee_write_me_part(abfd);
1637
1638
1639 /* Generate the header */
1640 bfd_seek(abfd, old, false);
1641
1642 for (i= 0; i < N_W_VARIABLES; i++) {
1643 ieee_write_2bytes(abfd,ieee_assign_value_to_variable_enum);
1644 ieee_write_byte(abfd, i);
1645 ieee_write_int5_out(abfd, ieee->w.offset[i]);
1646 }
1647 return true;
1648 }
1649
1650
1651
1652 \f
1653 /* Native-level interface to symbols. */
1654
1655 /* We read the symbols into a buffer, which is discarded when this
1656 function exits. We read the strings into a buffer large enough to
1657 hold them all plus all the cached symbol entries. */
1658
1659 asymbol *
1660 DEFUN(ieee_make_empty_symbol,(abfd),
1661 bfd *abfd)
1662 {
1663
1664 ieee_symbol_type *new =
1665 (ieee_symbol_type *)zalloc (sizeof (ieee_symbol_type));
1666 new->symbol.the_bfd = abfd;
1667 return &new->symbol;
1668
1669 }
1670
1671 void
1672 DEFUN(ieee_reclaim_symbol_table, (abfd),
1673 bfd *abfd)
1674 {
1675 #if 0
1676 asection *section;
1677
1678 if (!bfd_get_symcount (abfd)) return;
1679
1680 for (section = abfd->sections; section != NULL; section = section->next)
1681 if (section->relocation) {
1682 free ((void *)section->relocation);
1683 section->relocation = NULL;
1684 section->reloc_count = 0;
1685 }
1686
1687 bfd_get_symcount (abfd) = 0;
1688 free ((void *)obj_aout_symbols (abfd));
1689 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
1690 #endif
1691 }
1692 \f
1693
1694
1695 \f
1696 /* Obsolete procedural interface; better to look at the cache directly */
1697
1698 /* User should have checked the file flags; perhaps we should return
1699 BFD_NO_MORE_SYMBOLS if there are none? */
1700
1701 int
1702 DEFUN(ieee_get_symcount_upper_bound,(abfd),
1703 bfd *abfd)
1704 {
1705 #if 0
1706 /* In case we're doing an output file or something...? */
1707 if (bfd_get_symcount (abfd)) return bfd_get_symcount (abfd);
1708
1709 return (exec_hdr (abfd)->a_syms) / (sizeof (struct nlist));
1710 #endif
1711 return 0;
1712 }
1713
1714 symindex
1715 DEFUN(ieee_get_first_symbol,(ignore_abfd),
1716 bfd * ignore_abfd)
1717 {
1718 return 0;
1719 }
1720
1721 symindex
1722 DEFUN(ieee_get_next_symbol,(abfd, oidx),
1723 bfd *abfd AND
1724 symindex oidx)
1725 {
1726 #if 0
1727 if (oidx == BFD_NO_MORE_SYMBOLS) return BFD_NO_MORE_SYMBOLS;
1728 return ++oidx >= bfd_get_symcount (abfd) ? BFD_NO_MORE_SYMBOLS :
1729 oidx;
1730 #endif
1731 return 0;
1732 }
1733
1734 char *
1735 ieee_symbol_name (abfd, idx)
1736 bfd *abfd;
1737 symindex idx;
1738 {
1739 #if 0
1740 return (obj_aout_symbols (abfd) + idx)->symbol.name;
1741 #endif
1742 return 0;
1743 }
1744
1745 long
1746 ieee_symbol_value (abfd, idx)
1747 bfd *abfd;
1748 symindex idx;
1749 {
1750 #if 0
1751 return (obj_aout_symbols (abfd) + idx)->symbol.value;
1752 #endif
1753 return 0;
1754 }
1755
1756 symclass
1757 ieee_classify_symbol (abfd, idx)
1758 bfd *abfd;
1759 symindex idx;
1760 {
1761 #if 0
1762 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
1763
1764 if ((sym->symbol.flags & BSF_FORT_COMM) != 0) return bfd_symclass_fcommon;
1765 if ((sym->symbol.flags & BSF_GLOBAL) != 0) return bfd_symclass_global;
1766 if ((sym->symbol.flags & BSF_DEBUGGING) != 0) return bfd_symclass_debugger;
1767 if ((sym->symbol.flags & BSF_UNDEFINED) != 0) return bfd_symclass_undefined;
1768 #endif
1769 return bfd_symclass_unknown;
1770 }
1771
1772 boolean
1773 ieee_symbol_hasclass (abfd, idx, class)
1774 bfd *abfd;
1775 symindex idx;
1776 symclass class;
1777 {
1778 #if 0
1779 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
1780 switch (class) {
1781 case bfd_symclass_fcommon:
1782 return (sym->symbol.flags & BSF_FORT_COMM) ? true :false;
1783 case bfd_symclass_global:
1784 return (sym->symbol.flags & BSF_GLOBAL) ? true:false;
1785 case bfd_symclass_debugger:
1786 return (sym->symbol.flags & BSF_DEBUGGING) ? true:false;;
1787 case bfd_symclass_undefined:
1788 return (sym->symbol.flags & BSF_UNDEFINED) ? true:false;;
1789 default: return false;
1790 }
1791 #endif
1792 return 0;
1793 }
1794
1795
1796 void
1797 ieee_reclaim_reloc (ignore_abfd, section)
1798 bfd *ignore_abfd;
1799 sec_ptr section;
1800 {
1801 #if 0
1802 if (section->relocation) {
1803 free (section->relocation);
1804 section->relocation = NULL;
1805 section->reloc_count = 0;
1806 }
1807 #endif
1808 }
1809
1810 boolean
1811 ieee_close_and_cleanup (abfd)
1812 bfd *abfd;
1813 {
1814 if (bfd_read_p (abfd) == false)
1815 switch (abfd->format) {
1816 case bfd_archive:
1817 if (!_bfd_write_archive_contents (abfd)) {
1818 return false;
1819 }
1820 break;
1821 case bfd_object:
1822 if (!ieee_write_object_contents (abfd)) {
1823 return false;
1824 }
1825 break;
1826 default:
1827 bfd_error = invalid_operation;
1828 return false;
1829 }
1830
1831
1832 if (ieee_data(abfd) != (ieee_data_type *)NULL) {
1833 /* FIXME MORE LEAKS */
1834
1835 }
1836
1837 return true;
1838 }
1839
1840 static bfd *
1841 ieee_openr_next_archived_file(arch, prev)
1842 bfd *arch;
1843 bfd *prev;
1844 {
1845 ieee_ar_data_type *ar = ieee_ar_data(arch);
1846 /* take the next one from the arch state, or reset */
1847 if (prev == (bfd *)NULL) {
1848 /* Reset the index - the first two entries are bogus*/
1849 ar->element_index = 2;
1850 }
1851 while (true) {
1852 ieee_ar_obstack_type *p = ar->elements + ar->element_index;
1853 ar->element_index++;
1854 if (ar->element_index <= ar->element_count) {
1855 if (p->file_offset != (file_ptr)0) {
1856 if (p->abfd == (bfd *)NULL) {
1857 p->abfd = _bfd_create_empty_archive_element_shell(arch);
1858 p->abfd->origin = p->file_offset;
1859 }
1860 return p->abfd;
1861 }
1862 }
1863 else {
1864 return (bfd *)NULL;
1865 }
1866
1867 }
1868 }
1869
1870 static boolean
1871 ieee_find_nearest_line(abfd,
1872 section,
1873 symbols,
1874 offset,
1875 filename_ptr,
1876 functionname_ptr,
1877 line_ptr)
1878 bfd *abfd;
1879 asection *section;
1880 asymbol **symbols;
1881 bfd_vma offset;
1882 char **filename_ptr;
1883 char **functionname_ptr;
1884 unsigned int *line_ptr;
1885 {
1886 return false;
1887
1888 }
1889
1890
1891 static int
1892 ieee_generic_stat_arch_elt(abfd, buf)
1893 bfd *abfd;
1894 struct stat *buf;
1895 {
1896 ieee_ar_data_type *ar = ieee_ar_data(abfd);
1897 if (ar == (ieee_ar_data_type *)NULL) {
1898 bfd_error = invalid_operation;
1899 return -1;
1900 }
1901 else {
1902 buf->st_size = 0x1;
1903 buf->st_mode = 0666;
1904 return 0;
1905 }
1906 }
1907 static int
1908 DEFUN(ieee_sizeof_headers,(abfd),
1909 bfd *abfd)
1910 {
1911 return 0;
1912 }
1913
1914 #define ieee_core_file_failing_command bfd_false
1915 #define ieee_core_file_failing_signal bfd_false
1916 #define ieee_core_file_matches_executable_p bfd_false
1917 #define ieee_slurp_armap bfd_true
1918 #define ieee_slurp_extended_name_table bfd_true
1919 #define ieee_truncate_arname bfd_false
1920 #define ieee_write_armap bfd_false
1921 #define ieee_get_lineno bfd_false
1922
1923
1924 /*SUPPRESS 460 */
1925 bfd_target ieee_vec =
1926 {
1927 "ieee", /* name */
1928 bfd_target_ieee_flavour_enum,
1929 true, /* target byte order */
1930 true, /* target headers byte order */
1931 (HAS_RELOC | EXEC_P | /* object flags */
1932 HAS_LINENO | HAS_DEBUG |
1933 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
1934 (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
1935 |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1936 ' ', /* ar_pad_char */
1937 16, /* ar_max_namelen */
1938
1939 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
1940 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
1941
1942 {_bfd_dummy_target,
1943 ieee_object_p, /* bfd_check_format */
1944 ieee_archive_p,
1945 bfd_false
1946 },
1947 {
1948 bfd_false,
1949 ieee_mkobject,
1950 _bfd_generic_mkarchive,
1951 bfd_false
1952 },
1953 JUMP_TABLE(ieee)
1954 };
This page took 0.069909 seconds and 4 git commands to generate.