1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002
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
25 #include "safe-ctype.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. */
71 oasys_read_record (abfd, record)
73 oasys_record_union_type *record;
75 bfd_size_type amt = sizeof (record->header);
76 if (bfd_bread ((PTR) record, amt, abfd) != amt)
79 amt = record->header.length - sizeof (record->header);
82 if (bfd_bread ((PTR) ((char *) record + sizeof (record->header)), amt, abfd)
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
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.
113 oasys_slurp_symbol_table (abfd)
116 oasys_record_union_type record;
117 oasys_data_type *data = OASYS_DATA (abfd);
119 asymbol *dest_defined;
124 if (data->symbols != (asymbol *) NULL)
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;
138 data->strings = bfd_alloc (abfd, amt);
140 if (!data->symbols || !data->strings)
143 dest_defined = data->symbols + abfd->symcount - 1;
145 string_ptr = data->strings;
146 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
151 if (! oasys_read_record (abfd, &record))
153 switch (record.header.type)
155 case oasys_record_is_header_enum:
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;
173 case RELOCATION_TYPE_REL:
174 dest = dest_defined--;
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
195 case RELOCATION_TYPE_UND:
196 dest = data->symbols + H_GET_16 (abfd, record.symbol.refno);
197 dest->section = bfd_und_section_ptr;
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;
208 dest = dest_defined--;
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] != '_')
224 memcpy (string_ptr, record.symbol.name, length);
227 string_ptr[length] = 0;
228 string_ptr += length + 1;
239 oasys_get_symtab_upper_bound (abfd)
242 if (! oasys_slurp_symbol_table (abfd))
245 return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *));
248 extern const bfd_target oasys_vec;
251 oasys_get_symtab (abfd, location)
256 unsigned int counter;
257 if (oasys_slurp_symbol_table (abfd) == false)
261 symbase = OASYS_DATA (abfd)->symbols;
262 for (counter = 0; counter < abfd->symcount; counter++)
264 *(location++) = symbase++;
267 return abfd->symcount;
270 /***********************************************************************
274 static const bfd_target *
275 oasys_archive_p (abfd)
278 oasys_archive_header_type header;
279 oasys_extarchive_header_type header_ext;
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);
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
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);
333 abfd->tdata.oasys_ar_data = ar;
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)
343 /* There are two ways of specifying the archive header */
347 oasys_extmodule_table_type_a_type record_ext;
349 amt = sizeof (record_ext);
350 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
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);
364 memcpy (module[i].name, record_ext.mod_name, 33);
366 sizeof (record_ext) +
367 record.dep_count * 4 +
368 record.depee_count * 4 +
369 record.sect_count * 8 + 187;
373 oasys_extmodule_table_type_b_type record_ext;
375 amt = sizeof (record_ext);
376 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
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);
392 if (bfd_bread ((PTR) module[i].name, amt, abfd) != amt)
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;
409 oasys_mkobject (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;
418 static const bfd_target *
419 oasys_object_p (abfd)
422 oasys_data_type *oasys;
423 oasys_data_type *save = OASYS_DATA (abfd);
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)
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;
442 oasys_record_union_type record;
443 if (! oasys_read_record (abfd, &record))
445 if ((size_t) record.header.length < (size_t) sizeof (record.header))
449 switch ((oasys_record_enum_type) (record.header.type))
451 case oasys_record_is_header_enum:
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 */
458 oasys->symbol_string_length += 1 + oasys_string_length (&record);
461 case oasys_record_is_section_enum:
465 unsigned int section_number;
466 if (record.section.header.length != sizeof (record.section))
470 buffer = bfd_alloc (abfd, (bfd_size_type) 3);
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:
482 case RELOCATION_TYPE_UND:
483 case RELOCATION_TYPE_COM:
487 s->_raw_size = H_GET_32 (abfd, record.section.value);
488 s->vma = H_GET_32 (abfd, record.section.vma);
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)
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);
528 (void) bfd_release (abfd, oasys);
529 abfd->tdata.oasys_obj_data = save;
530 return (const bfd_target *) NULL;
535 oasys_get_symbol_info (ignore_abfd, symbol, ret)
536 bfd *ignore_abfd ATTRIBUTE_UNUSED;
540 bfd_symbol_info (symbol, ret);
541 if (!symbol->section)
542 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
546 oasys_print_symbol (abfd, afile, symbol, how)
550 bfd_print_symbol_type how;
552 FILE *file = (FILE *) afile;
556 case bfd_print_symbol_name:
557 case bfd_print_symbol_more:
558 fprintf (file, "%s", symbol->name);
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",
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 */
589 oasys_slurp_section_data (abfd)
592 oasys_record_union_type record;
593 oasys_data_type *data = OASYS_DATA (abfd);
595 oasys_per_section_type *per;
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)
607 if (data->first_data_record == 0)
610 if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0)
614 if (! oasys_read_record (abfd, &record))
616 switch (record.header.type)
618 case oasys_record_is_header_enum:
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;
626 bfd_byte *dst_base_ptr;
630 data->sections[record.data.relb & RELOCATION_SECT_BITS];
633 per = oasys_per_section (section);
635 if (per->initialized == false)
637 per->data = (bfd_byte *) bfd_zalloc (abfd, section->_raw_size);
641 = (oasys_reloc_type **) §ion->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;
656 dst_offset -= section->vma;
658 dst_base_ptr = oasys_per_section (section)->data;
659 dst_ptr = oasys_per_section (section)->data +
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;
672 if (mod_byte == 0 && gap >= 8)
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:
699 case RELOCATION_TYPE_REL:
701 /* Relocate the item relative to the section */
704 amt = sizeof (oasys_reloc_type);
705 r = (oasys_reloc_type *) bfd_alloc (abfd,
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 */
714 /* There is no symbol */
716 /* Work out the howto */
720 data->sections[reloc &
721 RELOCATION_SECT_BITS];
724 r->relent.section->vma;
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
735 if (r->relent.howto->pc_relative == true)
737 r->relent.addend -= dst_ptr - dst_base_ptr;
745 case RELOCATION_TYPE_UND:
749 amt = sizeof (oasys_reloc_type);
750 r = (oasys_reloc_type *) bfd_alloc (abfd,
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 */
759 /* Get symbol number */
760 r->symbol = (src[0] << 8) | src[1];
761 /* Work out the howto */
765 r->relent.section = (asection
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++;
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
779 if (r->relent.howto->pc_relative == true)
781 r->relent.addend -= dst_ptr - dst_base_ptr;
788 case RELOCATION_TYPE_COM:
798 case oasys_record_is_local_enum:
799 case oasys_record_is_symbol_enum:
800 case oasys_record_is_section_enum:
812 oasys_new_section_hook (abfd, 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)
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);
834 oasys_get_reloc_upper_bound (abfd, asect)
838 if (! oasys_slurp_section_data (abfd))
840 return (asect->reloc_count + 1) * sizeof (arelent *);
844 oasys_get_section_contents (abfd, section, location, offset, 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);
859 (void) memcpy (location, (PTR) (p->data + offset), (size_t) count);
866 oasys_canonicalize_reloc (ignore_abfd, section, relptr, symbols)
867 bfd *ignore_abfd ATTRIBUTE_UNUSED;
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)
879 if (src->relent.section == (asection *) NULL)
881 src->relent.sym_ptr_ptr = symbols + src->symbol;
885 *relptr++ = &src->relent;
889 *relptr = (arelent *) NULL;
890 return section->reloc_count = reloc_count;
899 /* Calculate the checksum and write one record */
901 oasys_write_record (abfd, type, record, size)
903 oasys_record_enum_type type;
904 oasys_record_union_type *record;
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];
917 for (i = 0; i < size; i++)
921 record->header.check_sum = 0xff & (-checksum);
922 if (bfd_bwrite ((PTR) record, (bfd_size_type) size, abfd) != size)
928 /* Write out all the symbols */
930 oasys_write_syms (abfd)
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;
946 if (bfd_is_com_section (g->section))
948 symbol.relb = RELOCATION_TYPE_COM;
949 H_PUT_16 (abfd, index, symbol.refno);
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 */
965 else if (g->flags & BSF_DEBUGGING)
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;
981 symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index;
983 H_PUT_16 (abfd, 0, symbol.refno);
985 #ifdef UNDERSCORE_HACK
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,
1009 if (! oasys_write_record (abfd,
1010 oasys_record_is_symbol_enum,
1011 (oasys_record_union_type *) & symbol,
1012 offsetof (oasys_symbol_record_type,
1016 g->value = index - 1;
1023 /* Write a section header for each section */
1025 oasys_write_sections (abfd)
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);
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,
1055 oasys_write_header (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,
1069 (void) memset (r.module_name + length,
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,
1086 oasys_write_end (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);
1095 if (! oasys_write_record (abfd,
1096 oasys_record_is_end_enum,
1097 (oasys_record_union_type *) & end,
1100 if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1)
1110 arelent *a = *((arelent **) ap);
1111 arelent *b = *((arelent **) bp);
1112 return a->address - b->address;
1120 oasys_write_data (abfd)
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
1138 qsort (s->orelocation,
1140 sizeof (arelent **),
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];
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)
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;
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 */
1194 bfd_get_16 (abfd, raw_data) +
1195 current_byte_index, raw_data);
1201 bfd_get_32 (abfd, raw_data) +
1202 current_byte_index, raw_data);
1211 rel_byte |= RELOCATION_32BIT_BIT;
1214 /* Is this a section relative relocation, or a symbol
1215 relative relocation ? */
1219 if (r->section != (asection *) NULL)
1221 /* The relent has a section attached, so it must be section
1223 rel_byte |= RELOCATION_TYPE_REL;
1224 rel_byte |= r->section->output_section->target_index;
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;
1239 sym->section->output_section->target_index;
1244 rel_byte |= RELOCATION_TYPE_UND;
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++;
1261 * dst++ = *raw_data++;
1265 *dst++ = *raw_data++;
1267 * dst++ = *raw_data++;
1273 /* If this is coming from an unloadable section then copy
1275 if (raw_data == NULL)
1281 *dst++ = *raw_data++;
1286 /* Don't write a useless null modification byte */
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))))
1305 oasys_write_object_contents (abfd)
1308 if (! oasys_write_header (abfd))
1310 if (! oasys_write_syms (abfd))
1312 if (! oasys_write_sections (abfd))
1314 if (! oasys_write_data (abfd))
1316 if (! oasys_write_end (abfd))
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.
1330 oasys_set_section_contents (abfd, section, location, offset, count)
1335 bfd_size_type count;
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)
1346 (void) memcpy ((PTR) (oasys_per_section (section)->data + offset),
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. */
1362 oasys_make_empty_symbol (abfd)
1365 bfd_size_type amt = sizeof (oasys_symbol_type);
1366 oasys_symbol_type *new = (oasys_symbol_type *) bfd_zalloc (abfd, amt);
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? */
1380 oasys_openr_next_archived_file (arch, 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;
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;
1411 bfd_set_error (bfd_error_no_more_archived_files);
1412 return (bfd *) NULL;
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;
1432 oasys_generic_stat_arch_elt (abfd, 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);
1444 buf->st_size = mod->size;
1445 buf->st_mode = 0666;
1451 oasys_sizeof_headers (abfd, exec)
1452 bfd *abfd ATTRIBUTE_UNUSED;
1453 boolean exec ATTRIBUTE_UNUSED;
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 **))) \
1466 #define oasys_truncate_arname bfd_dont_truncate_arname
1467 #define oasys_write_armap \
1469 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
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_discard_group bfd_generic_discard_group
1494 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1495 #define oasys_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
1496 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1497 #define oasys_bfd_link_just_syms _bfd_generic_link_just_syms
1498 #define oasys_bfd_final_link _bfd_generic_final_link
1499 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1502 const bfd_target oasys_vec =
1505 bfd_target_oasys_flavour,
1506 BFD_ENDIAN_BIG, /* target byte order */
1507 BFD_ENDIAN_BIG, /* target headers byte order */
1508 (HAS_RELOC | EXEC_P | /* object flags */
1509 HAS_LINENO | HAS_DEBUG |
1510 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1511 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1512 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1513 0, /* leading underscore */
1514 ' ', /* ar_pad_char */
1515 16, /* ar_max_namelen */
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, /* data */
1519 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1520 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1521 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1524 oasys_object_p, /* bfd_check_format */
1528 { /* bfd_set_format */
1531 _bfd_generic_mkarchive,
1534 { /* bfd_write_contents */
1536 oasys_write_object_contents,
1537 _bfd_write_archive_contents,
1541 BFD_JUMP_TABLE_GENERIC (oasys),
1542 BFD_JUMP_TABLE_COPY (_bfd_generic),
1543 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1544 BFD_JUMP_TABLE_ARCHIVE (oasys),
1545 BFD_JUMP_TABLE_SYMBOLS (oasys),
1546 BFD_JUMP_TABLE_RELOCS (oasys),
1547 BFD_JUMP_TABLE_WRITE (oasys),
1548 BFD_JUMP_TABLE_LINK (oasys),
1549 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),