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