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