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