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