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