* tc-arm.c (do_xsc_mia, do_xsc_mar, do_xsc_mra): Renamed from
[binutils.git] / bfd / oasys.c
blobd8dc957c8e8799797fe4f7f7300029f227d83ef9
1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define UNDERSCORE_HACK 1
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "safe-ctype.h"
26 #include "libbfd.h"
27 #include "oasys.h"
28 #include "liboasys.h"
30 static boolean oasys_slurp_section_data PARAMS ((bfd * const));
31 static boolean oasys_read_record PARAMS ((bfd *, oasys_record_union_type *));
32 static boolean oasys_write_sections PARAMS ((bfd *));
33 static boolean oasys_write_record
34 PARAMS ((bfd *, oasys_record_enum_type, oasys_record_union_type *, size_t));
35 static boolean oasys_write_syms PARAMS ((bfd *));
36 static boolean oasys_write_header PARAMS ((bfd *));
37 static boolean oasys_write_end PARAMS ((bfd *));
38 static boolean oasys_write_data PARAMS ((bfd *));
39 static size_t oasys_string_length PARAMS ((oasys_record_union_type *));
40 static boolean oasys_slurp_symbol_table PARAMS ((bfd *const));
41 static long int oasys_get_symtab_upper_bound PARAMS ((bfd *const));
42 static const bfd_target *oasys_archive_p PARAMS ((bfd *));
43 static boolean oasys_mkobject PARAMS ((bfd *));
44 static const bfd_target *oasys_object_p PARAMS ((bfd *));
45 static void oasys_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
46 static void oasys_print_symbol
47 PARAMS ((bfd *, void *, asymbol *, bfd_print_symbol_type));
48 static boolean oasys_new_section_hook PARAMS ((bfd *, asection *));
49 static long int oasys_get_reloc_upper_bound PARAMS ((bfd *, sec_ptr));
50 static boolean oasys_get_section_contents
51 PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type));
52 static int comp PARAMS ((const void *, const void *));
53 static boolean oasys_write_object_contents PARAMS ((bfd *));
54 static boolean oasys_set_section_contents
55 PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type));
56 static asymbol *oasys_make_empty_symbol PARAMS ((bfd *));
57 static bfd *oasys_openr_next_archived_file PARAMS ((bfd *, bfd *));
58 static boolean oasys_find_nearest_line
59 PARAMS ((bfd *, asection *, asymbol **, bfd_vma,
60 const char **, const char **, unsigned int *));
61 static int oasys_generic_stat_arch_elt PARAMS ((bfd *, struct stat *));
62 static int oasys_sizeof_headers PARAMS ((bfd *, boolean));
64 long oasys_get_symtab PARAMS ((bfd *, asymbol **));
65 long oasys_canonicalize_reloc
66 PARAMS ((bfd *, sec_ptr, arelent **, asymbol **));
68 /* Read in all the section data and relocation stuff too. */
70 static boolean
71 oasys_read_record (abfd, record)
72 bfd *abfd;
73 oasys_record_union_type *record;
75 bfd_size_type amt = sizeof (record->header);
76 if (bfd_bread ((PTR) record, amt, abfd) != amt)
77 return false;
79 amt = record->header.length - sizeof (record->header);
80 if ((long) amt <= 0)
81 return true;
82 if (bfd_bread ((PTR) ((char *) record + sizeof (record->header)), amt, abfd)
83 != amt)
84 return false;
85 return true;
88 static size_t
89 oasys_string_length (record)
90 oasys_record_union_type *record;
92 return record->header.length
93 - ((char *) record->symbol.name - (char *) record);
96 /*****************************************************************************/
100 Slurp the symbol table by reading in all the records at the start file
101 till we get to the first section record.
103 We'll sort the symbolss into two lists, defined and undefined. The
104 undefined symbols will be placed into the table according to their
105 refno.
107 We do this by placing all undefined symbols at the front of the table
108 moving in, and the defined symbols at the end of the table moving back.
112 static boolean
113 oasys_slurp_symbol_table (abfd)
114 bfd *const abfd;
116 oasys_record_union_type record;
117 oasys_data_type *data = OASYS_DATA (abfd);
118 boolean loop = true;
119 asymbol *dest_defined;
120 asymbol *dest;
121 char *string_ptr;
122 bfd_size_type amt;
124 if (data->symbols != (asymbol *) NULL)
126 return true;
128 /* Buy enough memory for all the symbols and all the names */
129 amt = abfd->symcount;
130 amt *= sizeof (asymbol);
131 data->symbols = (asymbol *) bfd_alloc (abfd, amt);
133 amt = data->symbol_string_length;
134 #ifdef UNDERSCORE_HACK
135 /* buy 1 more char for each symbol to keep the underscore in*/
136 amt += abfd->symcount;
137 #endif
138 data->strings = bfd_alloc (abfd, amt);
140 if (!data->symbols || !data->strings)
141 return false;
143 dest_defined = data->symbols + abfd->symcount - 1;
145 string_ptr = data->strings;
146 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
147 return false;
148 while (loop)
151 if (! oasys_read_record (abfd, &record))
152 return false;
153 switch (record.header.type)
155 case oasys_record_is_header_enum:
156 break;
157 case oasys_record_is_local_enum:
158 case oasys_record_is_symbol_enum:
160 int flag = record.header.type == (int) oasys_record_is_local_enum ?
161 (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT);
164 size_t length = oasys_string_length (&record);
165 switch (record.symbol.relb & RELOCATION_TYPE_BITS)
167 case RELOCATION_TYPE_ABS:
168 dest = dest_defined--;
169 dest->section = bfd_abs_section_ptr;
170 dest->flags = 0;
172 break;
173 case RELOCATION_TYPE_REL:
174 dest = dest_defined--;
175 dest->section =
176 OASYS_DATA (abfd)->sections[record.symbol.relb &
177 RELOCATION_SECT_BITS];
178 if (record.header.type == (int) oasys_record_is_local_enum)
180 dest->flags = BSF_LOCAL;
181 if (dest->section == (asection *) (~0))
183 /* It seems that sometimes internal symbols are tied up, but
184 still get output, even though there is no
185 section */
186 dest->section = 0;
189 else
192 dest->flags = flag;
194 break;
195 case RELOCATION_TYPE_UND:
196 dest = data->symbols + H_GET_16 (abfd, record.symbol.refno);
197 dest->section = bfd_und_section_ptr;
198 break;
199 case RELOCATION_TYPE_COM:
200 dest = dest_defined--;
201 dest->name = string_ptr;
202 dest->the_bfd = abfd;
204 dest->section = bfd_com_section_ptr;
206 break;
207 default:
208 dest = dest_defined--;
209 BFD_ASSERT (0);
210 break;
212 dest->name = string_ptr;
213 dest->the_bfd = abfd;
214 dest->udata.p = (PTR) NULL;
215 dest->value = H_GET_32 (abfd, record.symbol.value);
217 #ifdef UNDERSCORE_HACK
218 if (record.symbol.name[0] != '_')
220 string_ptr[0] = '_';
221 string_ptr++;
223 #endif
224 memcpy (string_ptr, record.symbol.name, length);
227 string_ptr[length] = 0;
228 string_ptr += length + 1;
230 break;
231 default:
232 loop = false;
235 return true;
238 static long
239 oasys_get_symtab_upper_bound (abfd)
240 bfd *const abfd;
242 if (! oasys_slurp_symbol_table (abfd))
243 return -1;
245 return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *));
248 extern const bfd_target oasys_vec;
250 long
251 oasys_get_symtab (abfd, location)
252 bfd *abfd;
253 asymbol **location;
255 asymbol *symbase;
256 unsigned int counter;
257 if (oasys_slurp_symbol_table (abfd) == false)
259 return -1;
261 symbase = OASYS_DATA (abfd)->symbols;
262 for (counter = 0; counter < abfd->symcount; counter++)
264 *(location++) = symbase++;
266 *location = 0;
267 return abfd->symcount;
270 /***********************************************************************
271 * archive stuff
274 static const bfd_target *
275 oasys_archive_p (abfd)
276 bfd *abfd;
278 oasys_archive_header_type header;
279 oasys_extarchive_header_type header_ext;
280 unsigned int i;
281 file_ptr filepos;
282 bfd_size_type amt;
284 amt = sizeof (header_ext);
285 if (bfd_seek (abfd, (file_ptr) 0, 0) != 0
286 || bfd_bread ((PTR) &header_ext, amt, abfd) != amt)
288 if (bfd_get_error () != bfd_error_system_call)
289 bfd_set_error (bfd_error_wrong_format);
290 return NULL;
293 header.version = H_GET_32 (abfd, header_ext.version);
294 header.mod_count = H_GET_32 (abfd, header_ext.mod_count);
295 header.mod_tbl_offset = H_GET_32 (abfd, header_ext.mod_tbl_offset);
296 header.sym_tbl_size = H_GET_32 (abfd, header_ext.sym_tbl_size);
297 header.sym_count = H_GET_32 (abfd, header_ext.sym_count);
298 header.sym_tbl_offset = H_GET_32 (abfd, header_ext.sym_tbl_offset);
299 header.xref_count = H_GET_32 (abfd, header_ext.xref_count);
300 header.xref_lst_offset = H_GET_32 (abfd, header_ext.xref_lst_offset);
303 There isn't a magic number in an Oasys archive, so the best we
304 can do to verify reasnableness is to make sure that the values in
305 the header are too weird
308 if (header.version > 10000 ||
309 header.mod_count > 10000 ||
310 header.sym_count > 100000 ||
311 header.xref_count > 100000)
312 return (const bfd_target *) NULL;
315 That all worked, let's buy the space for the header and read in
316 the headers.
319 oasys_ar_data_type *ar;
320 oasys_module_info_type *module;
321 oasys_module_table_type record;
323 amt = sizeof (oasys_ar_data_type);
324 ar = (oasys_ar_data_type *) bfd_alloc (abfd, amt);
326 amt = header.mod_count;
327 amt *= sizeof (oasys_module_info_type);
328 module = (oasys_module_info_type *) bfd_alloc (abfd, amt);
330 if (!ar || !module)
331 return NULL;
333 abfd->tdata.oasys_ar_data = ar;
334 ar->module = module;
335 ar->module_count = header.mod_count;
337 filepos = header.mod_tbl_offset;
338 for (i = 0; i < header.mod_count; i++)
340 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
341 return NULL;
343 /* There are two ways of specifying the archive header */
345 if (0)
347 oasys_extmodule_table_type_a_type record_ext;
349 amt = sizeof (record_ext);
350 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
351 return NULL;
353 record.mod_size = H_GET_32 (abfd, record_ext.mod_size);
354 record.file_offset = H_GET_32 (abfd, record_ext.file_offset);
356 record.dep_count = H_GET_32 (abfd, record_ext.dep_count);
357 record.depee_count = H_GET_32 (abfd, record_ext.depee_count);
358 record.sect_count = H_GET_32 (abfd, record_ext.sect_count);
360 module[i].name = bfd_alloc (abfd, (bfd_size_type) 33);
361 if (!module[i].name)
362 return NULL;
364 memcpy (module[i].name, record_ext.mod_name, 33);
365 filepos +=
366 sizeof (record_ext) +
367 record.dep_count * 4 +
368 record.depee_count * 4 +
369 record.sect_count * 8 + 187;
371 else
373 oasys_extmodule_table_type_b_type record_ext;
375 amt = sizeof (record_ext);
376 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
377 return NULL;
379 record.mod_size = H_GET_32 (abfd, record_ext.mod_size);
380 record.file_offset = H_GET_32 (abfd, record_ext.file_offset);
382 record.dep_count = H_GET_32 (abfd, record_ext.dep_count);
383 record.depee_count = H_GET_32 (abfd, record_ext.depee_count);
384 record.sect_count = H_GET_32 (abfd, record_ext.sect_count);
385 record.module_name_size = H_GET_32 (abfd,
386 record_ext.mod_name_length);
388 amt = record.module_name_size;
389 module[i].name = bfd_alloc (abfd, amt + 1);
390 if (!module[i].name)
391 return NULL;
392 if (bfd_bread ((PTR) module[i].name, amt, abfd) != amt)
393 return NULL;
394 module[i].name[record.module_name_size] = 0;
395 filepos += (sizeof (record_ext)
396 + record.dep_count * 4
397 + record.module_name_size + 1);
400 module[i].size = record.mod_size;
401 module[i].pos = record.file_offset;
402 module[i].abfd = 0;
405 return abfd->xvec;
408 static boolean
409 oasys_mkobject (abfd)
410 bfd *abfd;
412 bfd_size_type amt = sizeof (oasys_data_type);
413 abfd->tdata.oasys_obj_data = (oasys_data_type *) bfd_alloc (abfd, amt);
414 return abfd->tdata.oasys_obj_data ? true : false;
417 #define MAX_SECS 16
418 static const bfd_target *
419 oasys_object_p (abfd)
420 bfd *abfd;
422 oasys_data_type *oasys;
423 oasys_data_type *save = OASYS_DATA (abfd);
424 boolean loop = true;
425 boolean had_usefull = false;
427 abfd->tdata.oasys_obj_data = 0;
428 oasys_mkobject (abfd);
429 oasys = OASYS_DATA (abfd);
430 memset ((PTR) oasys->sections, 0xff, sizeof (oasys->sections));
432 /* Point to the start of the file */
433 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
434 goto fail;
435 oasys->symbol_string_length = 0;
436 /* Inspect the records, but only keep the section info -
437 remember the size of the symbols
439 oasys->first_data_record = 0;
440 while (loop)
442 oasys_record_union_type record;
443 if (! oasys_read_record (abfd, &record))
444 goto fail;
445 if ((size_t) record.header.length < (size_t) sizeof (record.header))
446 goto fail;
449 switch ((oasys_record_enum_type) (record.header.type))
451 case oasys_record_is_header_enum:
452 had_usefull = true;
453 break;
454 case oasys_record_is_symbol_enum:
455 case oasys_record_is_local_enum:
456 /* Count symbols and remember their size for a future malloc */
457 abfd->symcount++;
458 oasys->symbol_string_length += 1 + oasys_string_length (&record);
459 had_usefull = true;
460 break;
461 case oasys_record_is_section_enum:
463 asection *s;
464 char *buffer;
465 unsigned int section_number;
466 if (record.section.header.length != sizeof (record.section))
468 goto fail;
470 buffer = bfd_alloc (abfd, (bfd_size_type) 3);
471 if (!buffer)
472 goto fail;
473 section_number = record.section.relb & RELOCATION_SECT_BITS;
474 sprintf (buffer, "%u", section_number);
475 s = bfd_make_section (abfd, buffer);
476 oasys->sections[section_number] = s;
477 switch (record.section.relb & RELOCATION_TYPE_BITS)
479 case RELOCATION_TYPE_ABS:
480 case RELOCATION_TYPE_REL:
481 break;
482 case RELOCATION_TYPE_UND:
483 case RELOCATION_TYPE_COM:
484 BFD_FAIL ();
487 s->_raw_size = H_GET_32 (abfd, record.section.value);
488 s->vma = H_GET_32 (abfd, record.section.vma);
489 s->flags = 0;
490 had_usefull = true;
492 break;
493 case oasys_record_is_data_enum:
494 oasys->first_data_record = bfd_tell (abfd) - record.header.length;
495 case oasys_record_is_debug_enum:
496 case oasys_record_is_module_enum:
497 case oasys_record_is_named_section_enum:
498 case oasys_record_is_end_enum:
499 if (had_usefull == false)
500 goto fail;
501 loop = false;
502 break;
503 default:
504 goto fail;
507 oasys->symbols = (asymbol *) NULL;
509 Oasys support several architectures, but I can't see a simple way
510 to discover which one is in a particular file - we'll guess
512 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
513 if (abfd->symcount != 0)
515 abfd->flags |= HAS_SYMS;
519 We don't know if a section has data until we've read it..
522 oasys_slurp_section_data (abfd);
525 return abfd->xvec;
527 fail:
528 (void) bfd_release (abfd, oasys);
529 abfd->tdata.oasys_obj_data = save;
530 return (const bfd_target *) NULL;
534 static void
535 oasys_get_symbol_info (ignore_abfd, symbol, ret)
536 bfd *ignore_abfd ATTRIBUTE_UNUSED;
537 asymbol *symbol;
538 symbol_info *ret;
540 bfd_symbol_info (symbol, ret);
541 if (!symbol->section)
542 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
545 static void
546 oasys_print_symbol (abfd, afile, symbol, how)
547 bfd *abfd;
548 PTR afile;
549 asymbol *symbol;
550 bfd_print_symbol_type how;
552 FILE *file = (FILE *) afile;
554 switch (how)
556 case bfd_print_symbol_name:
557 case bfd_print_symbol_more:
558 fprintf (file, "%s", symbol->name);
559 break;
560 case bfd_print_symbol_all:
562 const char *section_name = symbol->section == (asection *) NULL ?
563 (const char *) "*abs" : symbol->section->name;
565 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
567 fprintf (file, " %-5s %s",
568 section_name,
569 symbol->name);
571 break;
575 The howto table is build using the top two bits of a reloc byte to
576 index into it. The bits are PCREL,WORD/LONG
578 static reloc_howto_type howto_table[] =
581 HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
582 HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs32", true, 0xffffffff, 0xffffffff, false),
583 HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false),
584 HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false)
587 /* Read in all the section data and relocation stuff too */
588 static boolean
589 oasys_slurp_section_data (abfd)
590 bfd *const abfd;
592 oasys_record_union_type record;
593 oasys_data_type *data = OASYS_DATA (abfd);
594 boolean loop = true;
595 oasys_per_section_type *per;
596 asection *s;
597 bfd_size_type amt;
599 /* See if the data has been slurped already .. */
600 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
602 per = oasys_per_section (s);
603 if (per->initialized == true)
604 return true;
607 if (data->first_data_record == 0)
608 return true;
610 if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0)
611 return false;
612 while (loop)
614 if (! oasys_read_record (abfd, &record))
615 return false;
616 switch (record.header.type)
618 case oasys_record_is_header_enum:
619 break;
620 case oasys_record_is_data_enum:
623 bfd_byte *src = record.data.data;
624 bfd_byte *end_src = ((bfd_byte *) & record) + record.header.length;
625 bfd_byte *dst_ptr;
626 bfd_byte *dst_base_ptr;
627 unsigned int relbit;
628 unsigned int count;
629 asection *section =
630 data->sections[record.data.relb & RELOCATION_SECT_BITS];
631 bfd_vma dst_offset;
633 per = oasys_per_section (section);
635 if (per->initialized == false)
637 per->data = (bfd_byte *) bfd_zalloc (abfd, section->_raw_size);
638 if (!per->data)
639 return false;
640 per->reloc_tail_ptr
641 = (oasys_reloc_type **) &section->relocation;
642 per->had_vma = false;
643 per->initialized = true;
644 section->reloc_count = 0;
645 section->flags = SEC_ALLOC;
648 dst_offset = H_GET_32 (abfd, record.data.addr);
649 if (per->had_vma == false)
651 /* Take the first vma we see as the base */
652 section->vma = dst_offset;
653 per->had_vma = true;
656 dst_offset -= section->vma;
658 dst_base_ptr = oasys_per_section (section)->data;
659 dst_ptr = oasys_per_section (section)->data +
660 dst_offset;
662 if (src < end_src)
664 section->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
666 while (src < end_src)
668 unsigned char mod_byte = *src++;
669 size_t gap = end_src - src;
671 count = 8;
672 if (mod_byte == 0 && gap >= 8)
674 dst_ptr[0] = src[0];
675 dst_ptr[1] = src[1];
676 dst_ptr[2] = src[2];
677 dst_ptr[3] = src[3];
678 dst_ptr[4] = src[4];
679 dst_ptr[5] = src[5];
680 dst_ptr[6] = src[6];
681 dst_ptr[7] = src[7];
682 dst_ptr += 8;
683 src += 8;
685 else
687 for (relbit = 1; count-- != 0 && src < end_src; relbit <<= 1)
689 if (relbit & mod_byte)
691 unsigned char reloc = *src;
692 /* This item needs to be relocated */
693 switch (reloc & RELOCATION_TYPE_BITS)
695 case RELOCATION_TYPE_ABS:
697 break;
699 case RELOCATION_TYPE_REL:
701 /* Relocate the item relative to the section */
702 oasys_reloc_type *r;
704 amt = sizeof (oasys_reloc_type);
705 r = (oasys_reloc_type *) bfd_alloc (abfd,
706 amt);
707 if (!r)
708 return false;
709 *(per->reloc_tail_ptr) = r;
710 per->reloc_tail_ptr = &r->next;
711 r->next = (oasys_reloc_type *) NULL;
712 /* Reference to undefined symbol */
713 src++;
714 /* There is no symbol */
715 r->symbol = 0;
716 /* Work out the howto */
717 abort ();
718 #if 0
719 r->relent.section =
720 data->sections[reloc &
721 RELOCATION_SECT_BITS];
723 r->relent.addend = -
724 r->relent.section->vma;
725 #endif
726 r->relent.address = dst_ptr - dst_base_ptr;
727 r->relent.howto = &howto_table[reloc >> 6];
728 r->relent.sym_ptr_ptr = (asymbol **) NULL;
729 section->reloc_count++;
731 /* Fake up the data to look like it's got the -ve pc in it, this makes
732 it much easier to convert into other formats. This is done by
733 hitting the addend.
735 if (r->relent.howto->pc_relative == true)
737 r->relent.addend -= dst_ptr - dst_base_ptr;
742 break;
745 case RELOCATION_TYPE_UND:
747 oasys_reloc_type *r;
749 amt = sizeof (oasys_reloc_type);
750 r = (oasys_reloc_type *) bfd_alloc (abfd,
751 amt);
752 if (!r)
753 return false;
754 *(per->reloc_tail_ptr) = r;
755 per->reloc_tail_ptr = &r->next;
756 r->next = (oasys_reloc_type *) NULL;
757 /* Reference to undefined symbol */
758 src++;
759 /* Get symbol number */
760 r->symbol = (src[0] << 8) | src[1];
761 /* Work out the howto */
762 abort ();
764 #if 0
765 r->relent.section = (asection
766 *) NULL;
767 #endif
768 r->relent.addend = 0;
769 r->relent.address = dst_ptr - dst_base_ptr;
770 r->relent.howto = &howto_table[reloc >> 6];
771 r->relent.sym_ptr_ptr = (asymbol **) NULL;
772 section->reloc_count++;
774 src += 2;
775 /* Fake up the data to look like it's got the -ve pc in it, this makes
776 it much easier to convert into other formats. This is done by
777 hitting the addend.
779 if (r->relent.howto->pc_relative == true)
781 r->relent.addend -= dst_ptr - dst_base_ptr;
787 break;
788 case RELOCATION_TYPE_COM:
789 BFD_FAIL ();
792 *dst_ptr++ = *src++;
797 break;
798 case oasys_record_is_local_enum:
799 case oasys_record_is_symbol_enum:
800 case oasys_record_is_section_enum:
801 break;
802 default:
803 loop = false;
807 return true;
811 static boolean
812 oasys_new_section_hook (abfd, newsect)
813 bfd *abfd;
814 asection *newsect;
816 newsect->used_by_bfd = (PTR)
817 bfd_alloc (abfd, (bfd_size_type) sizeof (oasys_per_section_type));
818 if (!newsect->used_by_bfd)
819 return false;
820 oasys_per_section (newsect)->data = (bfd_byte *) NULL;
821 oasys_per_section (newsect)->section = newsect;
822 oasys_per_section (newsect)->offset = 0;
823 oasys_per_section (newsect)->initialized = false;
824 newsect->alignment_power = 1;
825 /* Turn the section string into an index */
827 sscanf (newsect->name, "%u", &newsect->target_index);
829 return true;
833 static long
834 oasys_get_reloc_upper_bound (abfd, asect)
835 bfd *abfd;
836 sec_ptr asect;
838 if (! oasys_slurp_section_data (abfd))
839 return -1;
840 return (asect->reloc_count + 1) * sizeof (arelent *);
843 static boolean
844 oasys_get_section_contents (abfd, section, location, offset, count)
845 bfd *abfd;
846 sec_ptr section;
847 PTR location;
848 file_ptr offset;
849 bfd_size_type count;
851 oasys_per_section_type *p = (oasys_per_section_type *) section->used_by_bfd;
852 oasys_slurp_section_data (abfd);
853 if (p->initialized == false)
855 (void) memset (location, 0, (size_t) count);
857 else
859 (void) memcpy (location, (PTR) (p->data + offset), (size_t) count);
861 return true;
865 long
866 oasys_canonicalize_reloc (ignore_abfd, section, relptr, symbols)
867 bfd *ignore_abfd ATTRIBUTE_UNUSED;
868 sec_ptr section;
869 arelent **relptr;
870 asymbol **symbols ATTRIBUTE_UNUSED;
872 unsigned int reloc_count = 0;
873 oasys_reloc_type *src = (oasys_reloc_type *) (section->relocation);
874 while (src != (oasys_reloc_type *) NULL)
876 abort ();
878 #if 0
879 if (src->relent.section == (asection *) NULL)
881 src->relent.sym_ptr_ptr = symbols + src->symbol;
883 #endif
885 *relptr++ = &src->relent;
886 src = src->next;
887 reloc_count++;
889 *relptr = (arelent *) NULL;
890 return section->reloc_count = reloc_count;
896 /* Writing */
899 /* Calculate the checksum and write one record */
900 static boolean
901 oasys_write_record (abfd, type, record, size)
902 bfd *abfd;
903 oasys_record_enum_type type;
904 oasys_record_union_type *record;
905 size_t size;
907 int checksum;
908 size_t i;
909 unsigned char *ptr;
911 record->header.length = size;
912 record->header.type = (int) type;
913 record->header.check_sum = 0;
914 record->header.fill = 0;
915 ptr = (unsigned char *) &record->pad[0];
916 checksum = 0;
917 for (i = 0; i < size; i++)
919 checksum += *ptr++;
921 record->header.check_sum = 0xff & (-checksum);
922 if (bfd_bwrite ((PTR) record, (bfd_size_type) size, abfd) != size)
923 return false;
924 return true;
928 /* Write out all the symbols */
929 static boolean
930 oasys_write_syms (abfd)
931 bfd *abfd;
933 unsigned int count;
934 asymbol **generic = bfd_get_outsymbols (abfd);
935 unsigned int index = 0;
936 for (count = 0; count < bfd_get_symcount (abfd); count++)
939 oasys_symbol_record_type symbol;
940 asymbol *const g = generic[count];
942 const char *src = g->name;
943 char *dst = symbol.name;
944 unsigned int l = 0;
946 if (bfd_is_com_section (g->section))
948 symbol.relb = RELOCATION_TYPE_COM;
949 H_PUT_16 (abfd, index, symbol.refno);
950 index++;
952 else if (bfd_is_abs_section (g->section))
954 symbol.relb = RELOCATION_TYPE_ABS;
955 H_PUT_16 (abfd, 0, symbol.refno);
958 else if (bfd_is_und_section (g->section))
960 symbol.relb = RELOCATION_TYPE_UND;
961 H_PUT_16 (abfd, index, symbol.refno);
962 /* Overload the value field with the output index number */
963 index++;
965 else if (g->flags & BSF_DEBUGGING)
967 /* throw it away */
968 continue;
970 else
972 if (g->section == (asection *) NULL)
974 /* Sometime, the oasys tools give out a symbol with illegal
975 bits in it, we'll output it in the same broken way */
977 symbol.relb = RELOCATION_TYPE_REL | 0;
979 else
981 symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index;
983 H_PUT_16 (abfd, 0, symbol.refno);
985 #ifdef UNDERSCORE_HACK
986 if (src[l] == '_')
987 dst[l++] = '.';
988 #endif
989 while (src[l])
991 dst[l] = src[l];
992 l++;
995 H_PUT_32 (abfd, g->value, symbol.value);
998 if (g->flags & BSF_LOCAL)
1000 if (! oasys_write_record (abfd,
1001 oasys_record_is_local_enum,
1002 (oasys_record_union_type *) & symbol,
1003 offsetof (oasys_symbol_record_type,
1004 name[0]) + l))
1005 return false;
1007 else
1009 if (! oasys_write_record (abfd,
1010 oasys_record_is_symbol_enum,
1011 (oasys_record_union_type *) & symbol,
1012 offsetof (oasys_symbol_record_type,
1013 name[0]) + l))
1014 return false;
1016 g->value = index - 1;
1019 return true;
1023 /* Write a section header for each section */
1024 static boolean
1025 oasys_write_sections (abfd)
1026 bfd *abfd;
1028 asection *s;
1029 static oasys_section_record_type out;
1031 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1033 if (!ISDIGIT (s->name[0]))
1035 (*_bfd_error_handler)
1036 (_("%s: can not represent section `%s' in oasys"),
1037 bfd_get_filename (abfd), s->name);
1038 bfd_set_error (bfd_error_nonrepresentable_section);
1039 return false;
1041 out.relb = RELOCATION_TYPE_REL | s->target_index;
1042 H_PUT_32 (abfd, s->_cooked_size, out.value);
1043 H_PUT_32 (abfd, s->vma, out.vma);
1045 if (! oasys_write_record (abfd,
1046 oasys_record_is_section_enum,
1047 (oasys_record_union_type *) & out,
1048 sizeof (out)))
1049 return false;
1051 return true;
1054 static boolean
1055 oasys_write_header (abfd)
1056 bfd *abfd;
1058 /* Create and write the header */
1059 oasys_header_record_type r;
1060 size_t length = strlen (abfd->filename);
1061 if (length > (size_t) sizeof (r.module_name))
1063 length = sizeof (r.module_name);
1066 (void) memcpy (r.module_name,
1067 abfd->filename,
1068 length);
1069 (void) memset (r.module_name + length,
1070 ' ',
1071 sizeof (r.module_name) - length);
1073 r.version_number = OASYS_VERSION_NUMBER;
1074 r.rev_number = OASYS_REV_NUMBER;
1075 if (! oasys_write_record (abfd,
1076 oasys_record_is_header_enum,
1077 (oasys_record_union_type *) & r,
1078 offsetof (oasys_header_record_type,
1079 description[0])))
1080 return false;
1082 return true;
1085 static boolean
1086 oasys_write_end (abfd)
1087 bfd *abfd;
1089 oasys_end_record_type end;
1090 unsigned char null = 0;
1091 end.relb = RELOCATION_TYPE_ABS;
1092 H_PUT_32 (abfd, abfd->start_address, end.entry);
1093 H_PUT_16 (abfd, 0, end.fill);
1094 end.zero = 0;
1095 if (! oasys_write_record (abfd,
1096 oasys_record_is_end_enum,
1097 (oasys_record_union_type *) & end,
1098 sizeof (end)))
1099 return false;
1100 if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1)
1101 return false;
1102 return true;
1105 static int
1106 comp (ap, bp)
1107 const PTR ap;
1108 const PTR bp;
1110 arelent *a = *((arelent **) ap);
1111 arelent *b = *((arelent **) bp);
1112 return a->address - b->address;
1116 Writing data..
1119 static boolean
1120 oasys_write_data (abfd)
1121 bfd *abfd;
1123 asection *s;
1124 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1126 if (s->flags & SEC_LOAD)
1128 bfd_byte *raw_data = oasys_per_section (s)->data;
1129 oasys_data_record_type processed_data;
1130 bfd_size_type current_byte_index = 0;
1131 unsigned int relocs_to_go = s->reloc_count;
1132 arelent **p = s->orelocation;
1133 if (s->reloc_count != 0)
1135 /* Sort the reloc records so it's easy to insert the relocs into the
1136 data */
1138 qsort (s->orelocation,
1139 s->reloc_count,
1140 sizeof (arelent **),
1141 comp);
1143 current_byte_index = 0;
1144 processed_data.relb = s->target_index | RELOCATION_TYPE_REL;
1146 while (current_byte_index < s->_cooked_size)
1148 /* Scan forwards by eight bytes or however much is left and see if
1149 there are any relocations going on */
1150 bfd_byte *mod = &processed_data.data[0];
1151 bfd_byte *dst = &processed_data.data[1];
1153 unsigned int i = 0;
1154 *mod = 0;
1157 H_PUT_32 (abfd, s->vma + current_byte_index,
1158 processed_data.addr);
1160 /* Don't start a relocation unless you're sure you can finish it
1161 within the same data record. The worst case relocation is a
1162 4-byte relocatable value which is split across two modification
1163 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1164 1 modification byte + 2 data = 8 bytes total). That's where
1165 the magic number 8 comes from.
1167 while (current_byte_index < s->_raw_size && dst <=
1168 &processed_data.data[sizeof (processed_data.data) - 8])
1172 if (relocs_to_go != 0)
1174 arelent *r = *p;
1175 reloc_howto_type *const how = r->howto;
1176 /* There is a relocation, is it for this byte ? */
1177 if (r->address == current_byte_index)
1179 unsigned char rel_byte;
1181 p++;
1182 relocs_to_go--;
1184 *mod |= (1 << i);
1185 if (how->pc_relative)
1187 rel_byte = RELOCATION_PCREL_BIT;
1189 /* Also patch the raw data so that it doesn't have
1190 the -ve stuff any more */
1191 if (how->size != 2)
1193 bfd_put_16 (abfd,
1194 bfd_get_16 (abfd, raw_data) +
1195 current_byte_index, raw_data);
1198 else
1200 bfd_put_32 (abfd,
1201 bfd_get_32 (abfd, raw_data) +
1202 current_byte_index, raw_data);
1205 else
1207 rel_byte = 0;
1209 if (how->size == 2)
1211 rel_byte |= RELOCATION_32BIT_BIT;
1214 /* Is this a section relative relocation, or a symbol
1215 relative relocation ? */
1216 abort ();
1218 #if 0
1219 if (r->section != (asection *) NULL)
1221 /* The relent has a section attached, so it must be section
1222 relative */
1223 rel_byte |= RELOCATION_TYPE_REL;
1224 rel_byte |= r->section->output_section->target_index;
1225 *dst++ = rel_byte;
1227 else
1228 #endif
1230 asymbol *sym = *(r->sym_ptr_ptr);
1232 /* If this symbol has a section attached, then it
1233 has already been resolved. Change from a symbol
1234 ref to a section ref */
1235 if (sym->section != (asection *) NULL)
1237 rel_byte |= RELOCATION_TYPE_REL;
1238 rel_byte |=
1239 sym->section->output_section->target_index;
1240 *dst++ = rel_byte;
1242 else
1244 rel_byte |= RELOCATION_TYPE_UND;
1245 *dst++ = rel_byte;
1246 /* Next two bytes are a symbol index - we can get
1247 this from the symbol value which has been zapped
1248 into the symbol index in the table when the
1249 symbol table was written
1251 *dst++ = sym->value >> 8;
1252 *dst++ = sym->value;
1255 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1256 /* relocations never occur from an unloadable section,
1257 so we can assume that raw_data is not NULL
1259 *dst++ = *raw_data++;
1260 ADVANCE
1261 * dst++ = *raw_data++;
1262 ADVANCE
1263 if (how->size == 2)
1265 *dst++ = *raw_data++;
1266 ADVANCE
1267 * dst++ = *raw_data++;
1268 ADVANCE
1270 continue;
1273 /* If this is coming from an unloadable section then copy
1274 zeros */
1275 if (raw_data == NULL)
1277 *dst++ = 0;
1279 else
1281 *dst++ = *raw_data++;
1283 ADVANCE
1286 /* Don't write a useless null modification byte */
1287 if (dst == mod + 1)
1289 --dst;
1292 if (! (oasys_write_record
1293 (abfd, oasys_record_is_data_enum,
1294 ((oasys_record_union_type *) &processed_data),
1295 (size_t) (dst - (bfd_byte *) &processed_data))))
1296 return false;
1301 return true;
1304 static boolean
1305 oasys_write_object_contents (abfd)
1306 bfd *abfd;
1308 if (! oasys_write_header (abfd))
1309 return false;
1310 if (! oasys_write_syms (abfd))
1311 return false;
1312 if (! oasys_write_sections (abfd))
1313 return false;
1314 if (! oasys_write_data (abfd))
1315 return false;
1316 if (! oasys_write_end (abfd))
1317 return false;
1318 return true;
1324 /** exec and core file sections */
1326 /* set section contents is complicated with OASYS since the format is
1327 * not a byte image, but a record stream.
1329 static boolean
1330 oasys_set_section_contents (abfd, section, location, offset, count)
1331 bfd *abfd;
1332 sec_ptr section;
1333 PTR location;
1334 file_ptr offset;
1335 bfd_size_type count;
1337 if (count != 0)
1339 if (oasys_per_section (section)->data == (bfd_byte *) NULL)
1341 oasys_per_section (section)->data =
1342 (bfd_byte *) (bfd_alloc (abfd, section->_cooked_size));
1343 if (!oasys_per_section (section)->data)
1344 return false;
1346 (void) memcpy ((PTR) (oasys_per_section (section)->data + offset),
1347 location,
1348 (size_t) count);
1350 return true;
1355 /* Native-level interface to symbols. */
1357 /* We read the symbols into a buffer, which is discarded when this
1358 function exits. We read the strings into a buffer large enough to
1359 hold them all plus all the cached symbol entries. */
1361 static asymbol *
1362 oasys_make_empty_symbol (abfd)
1363 bfd *abfd;
1365 bfd_size_type amt = sizeof (oasys_symbol_type);
1366 oasys_symbol_type *new = (oasys_symbol_type *) bfd_zalloc (abfd, amt);
1367 if (!new)
1368 return NULL;
1369 new->symbol.the_bfd = abfd;
1370 return &new->symbol;
1376 /* User should have checked the file flags; perhaps we should return
1377 BFD_NO_MORE_SYMBOLS if there are none? */
1379 static bfd *
1380 oasys_openr_next_archived_file (arch, prev)
1381 bfd *arch;
1382 bfd *prev;
1384 oasys_ar_data_type *ar = OASYS_AR_DATA (arch);
1385 oasys_module_info_type *p;
1386 /* take the next one from the arch state, or reset */
1387 if (prev == (bfd *) NULL)
1389 /* Reset the index - the first two entries are bogus*/
1390 ar->module_index = 0;
1393 p = ar->module + ar->module_index;
1394 ar->module_index++;
1396 if (ar->module_index <= ar->module_count)
1398 if (p->abfd == (bfd *) NULL)
1400 p->abfd = _bfd_create_empty_archive_element_shell (arch);
1401 p->abfd->origin = p->pos;
1402 p->abfd->filename = p->name;
1404 /* Fixup a pointer to this element for the member */
1405 p->abfd->arelt_data = (PTR) p;
1407 return p->abfd;
1409 else
1411 bfd_set_error (bfd_error_no_more_archived_files);
1412 return (bfd *) NULL;
1416 static boolean
1417 oasys_find_nearest_line (abfd, section, symbols, offset,
1418 filename_ptr, functionname_ptr, line_ptr)
1419 bfd *abfd ATTRIBUTE_UNUSED;
1420 asection *section ATTRIBUTE_UNUSED;
1421 asymbol **symbols ATTRIBUTE_UNUSED;
1422 bfd_vma offset ATTRIBUTE_UNUSED;
1423 const char **filename_ptr ATTRIBUTE_UNUSED;
1424 const char **functionname_ptr ATTRIBUTE_UNUSED;
1425 unsigned int *line_ptr ATTRIBUTE_UNUSED;
1427 return false;
1431 static int
1432 oasys_generic_stat_arch_elt (abfd, buf)
1433 bfd *abfd;
1434 struct stat *buf;
1436 oasys_module_info_type *mod = (oasys_module_info_type *) abfd->arelt_data;
1437 if (mod == (oasys_module_info_type *) NULL)
1439 bfd_set_error (bfd_error_invalid_operation);
1440 return -1;
1442 else
1444 buf->st_size = mod->size;
1445 buf->st_mode = 0666;
1446 return 0;
1450 static int
1451 oasys_sizeof_headers (abfd, exec)
1452 bfd *abfd ATTRIBUTE_UNUSED;
1453 boolean exec ATTRIBUTE_UNUSED;
1455 return 0;
1458 #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup
1459 #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1461 #define oasys_slurp_armap bfd_true
1462 #define oasys_slurp_extended_name_table bfd_true
1463 #define oasys_construct_extended_name_table \
1464 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1465 bfd_true)
1466 #define oasys_truncate_arname bfd_dont_truncate_arname
1467 #define oasys_write_armap \
1468 ((boolean (*) \
1469 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1470 bfd_true)
1471 #define oasys_read_ar_hdr bfd_nullvoidptr
1472 #define oasys_get_elt_at_index _bfd_generic_get_elt_at_index
1473 #define oasys_update_armap_timestamp bfd_true
1475 #define oasys_bfd_is_local_label_name bfd_generic_is_local_label_name
1476 #define oasys_get_lineno _bfd_nosymbols_get_lineno
1477 #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1478 #define oasys_read_minisymbols _bfd_generic_read_minisymbols
1479 #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1481 #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1483 #define oasys_set_arch_mach bfd_default_set_arch_mach
1485 #define oasys_get_section_contents_in_window \
1486 _bfd_generic_get_section_contents_in_window
1488 #define oasys_bfd_get_relocated_section_contents \
1489 bfd_generic_get_relocated_section_contents
1490 #define oasys_bfd_relax_section bfd_generic_relax_section
1491 #define oasys_bfd_gc_sections bfd_generic_gc_sections
1492 #define oasys_bfd_merge_sections bfd_generic_merge_sections
1493 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1494 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1495 #define oasys_bfd_final_link _bfd_generic_final_link
1496 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1498 /*SUPPRESS 460 */
1499 const bfd_target oasys_vec =
1501 "oasys", /* name */
1502 bfd_target_oasys_flavour,
1503 BFD_ENDIAN_BIG, /* target byte order */
1504 BFD_ENDIAN_BIG, /* target headers byte order */
1505 (HAS_RELOC | EXEC_P | /* object flags */
1506 HAS_LINENO | HAS_DEBUG |
1507 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1508 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1509 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1510 0, /* leading underscore */
1511 ' ', /* ar_pad_char */
1512 16, /* ar_max_namelen */
1513 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1514 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1515 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1516 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1517 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1518 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1520 {_bfd_dummy_target,
1521 oasys_object_p, /* bfd_check_format */
1522 oasys_archive_p,
1523 _bfd_dummy_target,
1525 { /* bfd_set_format */
1526 bfd_false,
1527 oasys_mkobject,
1528 _bfd_generic_mkarchive,
1529 bfd_false
1531 { /* bfd_write_contents */
1532 bfd_false,
1533 oasys_write_object_contents,
1534 _bfd_write_archive_contents,
1535 bfd_false,
1538 BFD_JUMP_TABLE_GENERIC (oasys),
1539 BFD_JUMP_TABLE_COPY (_bfd_generic),
1540 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1541 BFD_JUMP_TABLE_ARCHIVE (oasys),
1542 BFD_JUMP_TABLE_SYMBOLS (oasys),
1543 BFD_JUMP_TABLE_RELOCS (oasys),
1544 BFD_JUMP_TABLE_WRITE (oasys),
1545 BFD_JUMP_TABLE_LINK (oasys),
1546 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1548 NULL,
1550 (PTR) 0