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