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