1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define UNDERSCORE_HACK 1
29 /* XXX - FIXME. offsetof belongs in the system-specific files in
31 /* Define offsetof for those systems which lack it */
34 #define offsetof(type, identifier) (size_t) &(((type *) 0)->identifier)
37 static boolean oasys_read_record PARAMS ((bfd *,
38 oasys_record_union_type *));
39 static boolean oasys_write_sections PARAMS ((bfd *));
40 static boolean oasys_write_record PARAMS ((bfd *,
41 oasys_record_enum_type,
42 oasys_record_union_type *,
44 static boolean oasys_write_syms PARAMS ((bfd *));
45 static boolean oasys_write_header PARAMS ((bfd *));
46 static boolean oasys_write_end PARAMS ((bfd *));
47 static boolean oasys_write_data PARAMS ((bfd *));
49 /* Read in all the section data and relocation stuff too */
50 PROTO (static boolean, oasys_slurp_section_data, (bfd * CONST abfd));
53 oasys_read_record (abfd, record)
55 oasys_record_union_type *record;
57 if (bfd_read ((PTR) record, 1, sizeof (record->header), abfd)
58 != sizeof (record->header))
61 if ((size_t) record->header.length <= (size_t) sizeof (record->header))
63 if (bfd_read ((PTR) (((char *) record) + sizeof (record->header)),
64 1, record->header.length - sizeof (record->header),
66 != record->header.length - sizeof (record->header))
71 oasys_string_length (record)
72 oasys_record_union_type *record;
74 return record->header.length
75 - ((char *) record->symbol.name - (char *) record);
78 /*****************************************************************************/
82 Slurp the symbol table by reading in all the records at the start file
83 till we get to the first section record.
85 We'll sort the symbolss into two lists, defined and undefined. The
86 undefined symbols will be placed into the table according to their
89 We do this by placing all undefined symbols at the front of the table
90 moving in, and the defined symbols at the end of the table moving back.
95 oasys_slurp_symbol_table (abfd)
98 oasys_record_union_type record;
99 oasys_data_type *data = OASYS_DATA (abfd);
101 asymbol *dest_defined;
106 if (data->symbols != (asymbol *) NULL)
110 /* Buy enough memory for all the symbols and all the names */
112 (asymbol *) bfd_alloc (abfd, sizeof (asymbol) * abfd->symcount);
113 #ifdef UNDERSCORE_HACK
114 /* buy 1 more char for each symbol to keep the underscore in*/
115 data->strings = bfd_alloc (abfd, data->symbol_string_length +
118 data->strings = bfd_alloc (abfd, data->symbol_string_length);
120 if (!data->symbols || !data->strings)
122 bfd_set_error (bfd_error_no_memory);
126 dest_defined = data->symbols + abfd->symcount - 1;
128 string_ptr = data->strings;
129 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
134 if (! oasys_read_record (abfd, &record))
136 switch (record.header.type)
138 case oasys_record_is_header_enum:
140 case oasys_record_is_local_enum:
141 case oasys_record_is_symbol_enum:
143 int flag = record.header.type == (int) oasys_record_is_local_enum ?
144 (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT);
147 size_t length = oasys_string_length (&record);
148 switch (record.symbol.relb & RELOCATION_TYPE_BITS)
150 case RELOCATION_TYPE_ABS:
151 dest = dest_defined--;
152 dest->section = bfd_abs_section_ptr;
156 case RELOCATION_TYPE_REL:
157 dest = dest_defined--;
159 OASYS_DATA (abfd)->sections[record.symbol.relb &
160 RELOCATION_SECT_BITS];
161 if (record.header.type == (int) oasys_record_is_local_enum)
163 dest->flags = BSF_LOCAL;
164 if (dest->section == (asection *) (~0))
166 /* It seems that sometimes internal symbols are tied up, but
167 still get output, even though there is no
178 case RELOCATION_TYPE_UND:
179 dest = data->symbols + bfd_h_get_16 (abfd, record.symbol.refno);
180 dest->section = bfd_und_section_ptr;
182 case RELOCATION_TYPE_COM:
183 dest = dest_defined--;
184 dest->name = string_ptr;
185 dest->the_bfd = abfd;
187 dest->section = bfd_com_section_ptr;
191 dest = dest_defined--;
195 dest->name = string_ptr;
196 dest->the_bfd = abfd;
197 dest->udata.p = (PTR) NULL;
198 dest->value = bfd_h_get_32 (abfd, record.symbol.value);
200 #ifdef UNDERSCORE_HACK
201 if (record.symbol.name[0] != '_')
207 memcpy (string_ptr, record.symbol.name, length);
210 string_ptr[length] = 0;
211 string_ptr += length + 1;
222 oasys_get_symtab_upper_bound (abfd)
225 if (! oasys_slurp_symbol_table (abfd))
228 return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *));
234 extern const bfd_target oasys_vec;
237 oasys_get_symtab (abfd, location)
242 unsigned int counter;
243 if (oasys_slurp_symbol_table (abfd) == false)
247 symbase = OASYS_DATA (abfd)->symbols;
248 for (counter = 0; counter < abfd->symcount; counter++)
250 *(location++) = symbase++;
253 return abfd->symcount;
256 /***********************************************************************
260 static const bfd_target *
261 oasys_archive_p (abfd)
264 oasys_archive_header_type header;
265 oasys_extarchive_header_type header_ext;
269 if (bfd_seek (abfd, (file_ptr) 0, false) != 0
270 || (bfd_read ((PTR) & header_ext, 1, sizeof (header_ext), abfd)
271 != sizeof (header_ext)))
273 if (bfd_get_error () != bfd_error_system_call)
274 bfd_set_error (bfd_error_wrong_format);
278 header.version = bfd_h_get_32 (abfd, header_ext.version);
279 header.mod_count = bfd_h_get_32 (abfd, header_ext.mod_count);
280 header.mod_tbl_offset = bfd_h_get_32 (abfd, header_ext.mod_tbl_offset);
281 header.sym_tbl_size = bfd_h_get_32 (abfd, header_ext.sym_tbl_size);
282 header.sym_count = bfd_h_get_32 (abfd, header_ext.sym_count);
283 header.sym_tbl_offset = bfd_h_get_32 (abfd, header_ext.sym_tbl_offset);
284 header.xref_count = bfd_h_get_32 (abfd, header_ext.xref_count);
285 header.xref_lst_offset = bfd_h_get_32 (abfd, header_ext.xref_lst_offset);
288 There isn't a magic number in an Oasys archive, so the best we
289 can do to verify reasnableness is to make sure that the values in
290 the header are too weird
293 if (header.version > 10000 ||
294 header.mod_count > 10000 ||
295 header.sym_count > 100000 ||
296 header.xref_count > 100000)
297 return (const bfd_target *) NULL;
300 That all worked, let's buy the space for the header and read in
304 oasys_ar_data_type *ar =
305 (oasys_ar_data_type *) bfd_alloc (abfd, sizeof (oasys_ar_data_type));
307 oasys_module_info_type *module =
308 (oasys_module_info_type *)
309 bfd_alloc (abfd, sizeof (oasys_module_info_type) * header.mod_count);
310 oasys_module_table_type record;
314 bfd_set_error (bfd_error_no_memory);
318 abfd->tdata.oasys_ar_data = ar;
320 ar->module_count = header.mod_count;
322 filepos = header.mod_tbl_offset;
323 for (i = 0; i < header.mod_count; i++)
325 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
328 /* There are two ways of specifying the archive header */
332 oasys_extmodule_table_type_a_type record_ext;
333 if (bfd_read ((PTR) & record_ext, 1, sizeof (record_ext), abfd)
334 != sizeof (record_ext))
337 record.mod_size = bfd_h_get_32 (abfd, record_ext.mod_size);
338 record.file_offset = bfd_h_get_32 (abfd, record_ext.file_offset);
340 record.dep_count = bfd_h_get_32 (abfd, record_ext.dep_count);
341 record.depee_count = bfd_h_get_32 (abfd, record_ext.depee_count);
342 record.sect_count = bfd_h_get_32 (abfd, record_ext.sect_count);
344 module[i].name = bfd_alloc (abfd, 33);
347 bfd_set_error (bfd_error_no_memory);
351 memcpy (module[i].name, record_ext.mod_name, 33);
353 sizeof (record_ext) +
354 record.dep_count * 4 +
355 record.depee_count * 4 +
356 record.sect_count * 8 + 187;
360 oasys_extmodule_table_type_b_type record_ext;
361 if (bfd_read ((PTR) & record_ext, 1, sizeof (record_ext), abfd)
362 != sizeof (record_ext))
365 record.mod_size = bfd_h_get_32 (abfd, record_ext.mod_size);
366 record.file_offset = bfd_h_get_32 (abfd, record_ext.file_offset);
368 record.dep_count = bfd_h_get_32 (abfd, record_ext.dep_count);
369 record.depee_count = bfd_h_get_32 (abfd, record_ext.depee_count);
370 record.sect_count = bfd_h_get_32 (abfd, record_ext.sect_count);
371 record.module_name_size = bfd_h_get_32 (abfd, record_ext.mod_name_length);
373 module[i].name = bfd_alloc (abfd, record.module_name_size + 1);
376 bfd_set_error (bfd_error_no_memory);
379 if (bfd_read ((PTR) module[i].name, 1, record.module_name_size,
381 != record.module_name_size)
383 module[i].name[record.module_name_size] = 0;
385 sizeof (record_ext) +
386 record.dep_count * 4 +
387 record.module_name_size + 1;
392 module[i].size = record.mod_size;
393 module[i].pos = record.file_offset;
402 oasys_mkobject (abfd)
406 abfd->tdata.oasys_obj_data = (oasys_data_type *) bfd_alloc (abfd, sizeof (oasys_data_type));
407 return abfd->tdata.oasys_obj_data ? true : false;
411 static const bfd_target *
412 oasys_object_p (abfd)
415 oasys_data_type *oasys;
416 oasys_data_type *save = OASYS_DATA (abfd);
418 boolean had_usefull = false;
420 abfd->tdata.oasys_obj_data = 0;
421 oasys_mkobject (abfd);
422 oasys = OASYS_DATA (abfd);
423 memset ((PTR) oasys->sections, 0xff, sizeof (oasys->sections));
425 /* Point to the start of the file */
426 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
428 oasys->symbol_string_length = 0;
429 /* Inspect the records, but only keep the section info -
430 remember the size of the symbols
432 oasys->first_data_record = 0;
435 oasys_record_union_type record;
436 if (! oasys_read_record (abfd, &record))
438 if ((size_t) record.header.length < (size_t) sizeof (record.header))
442 switch ((oasys_record_enum_type) (record.header.type))
444 case oasys_record_is_header_enum:
447 case oasys_record_is_symbol_enum:
448 case oasys_record_is_local_enum:
449 /* Count symbols and remember their size for a future malloc */
451 oasys->symbol_string_length += 1 + oasys_string_length (&record);
454 case oasys_record_is_section_enum:
458 unsigned int section_number;
459 if (record.section.header.length != sizeof (record.section))
463 buffer = bfd_alloc (abfd, 3);
466 bfd_set_error (bfd_error_no_memory);
469 section_number = record.section.relb & RELOCATION_SECT_BITS;
470 sprintf (buffer, "%u", section_number);
471 s = bfd_make_section (abfd, buffer);
472 oasys->sections[section_number] = s;
473 switch (record.section.relb & RELOCATION_TYPE_BITS)
475 case RELOCATION_TYPE_ABS:
476 case RELOCATION_TYPE_REL:
478 case RELOCATION_TYPE_UND:
479 case RELOCATION_TYPE_COM:
483 s->_raw_size = bfd_h_get_32 (abfd, record.section.value);
484 s->vma = bfd_h_get_32 (abfd, record.section.vma);
489 case oasys_record_is_data_enum:
490 oasys->first_data_record = bfd_tell (abfd) - record.header.length;
491 case oasys_record_is_debug_enum:
492 case oasys_record_is_module_enum:
493 case oasys_record_is_named_section_enum:
494 case oasys_record_is_end_enum:
495 if (had_usefull == false)
503 oasys->symbols = (asymbol *) NULL;
505 Oasys support several architectures, but I can't see a simple way
506 to discover which one is in a particular file - we'll guess
508 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
509 if (abfd->symcount != 0)
511 abfd->flags |= HAS_SYMS;
515 We don't know if a section has data until we've read it..
518 oasys_slurp_section_data (abfd);
524 (void) bfd_release (abfd, oasys);
525 abfd->tdata.oasys_obj_data = save;
526 return (const bfd_target *) NULL;
531 oasys_get_symbol_info (ignore_abfd, symbol, ret)
536 bfd_symbol_info (symbol, ret);
537 if (!symbol->section)
538 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
542 oasys_print_symbol (ignore_abfd, afile, symbol, how)
546 bfd_print_symbol_type how;
548 FILE *file = (FILE *) afile;
552 case bfd_print_symbol_name:
553 case bfd_print_symbol_more:
554 fprintf (file, "%s", symbol->name);
556 case bfd_print_symbol_all:
558 CONST char *section_name = symbol->section == (asection *) NULL ?
559 (CONST char *) "*abs" : symbol->section->name;
561 bfd_print_symbol_vandf ((PTR) file, symbol);
563 fprintf (file, " %-5s %s",
571 The howto table is build using the top two bits of a reloc byte to
572 index into it. The bits are PCREL,WORD/LONG
574 static reloc_howto_type howto_table[] =
577 HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
578 HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield, 0, "abs32", true, 0xffffffff, 0xffffffff, false),
579 HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false),
580 HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false)
583 /* Read in all the section data and relocation stuff too */
585 oasys_slurp_section_data (abfd)
588 oasys_record_union_type record;
589 oasys_data_type *data = OASYS_DATA (abfd);
592 oasys_per_section_type *per;
596 /* See if the data has been slurped already .. */
597 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
599 per = oasys_per_section (s);
600 if (per->initialized == true)
604 if (data->first_data_record == 0)
607 if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0)
611 if (! oasys_read_record (abfd, &record))
613 switch (record.header.type)
615 case oasys_record_is_header_enum:
617 case oasys_record_is_data_enum:
620 bfd_byte *src = record.data.data;
621 bfd_byte *end_src = ((bfd_byte *) & record) + record.header.length;
623 bfd_byte *dst_base_ptr;
627 data->sections[record.data.relb & RELOCATION_SECT_BITS];
630 per = oasys_per_section (section);
632 if (per->initialized == false)
634 per->data = (bfd_byte *) bfd_zalloc (abfd, section->_raw_size);
637 bfd_set_error (bfd_error_no_memory);
640 per->reloc_tail_ptr = (oasys_reloc_type **) & (section->relocation);
641 per->had_vma = false;
642 per->initialized = true;
643 section->reloc_count = 0;
644 section->flags = SEC_ALLOC;
647 dst_offset = bfd_h_get_32 (abfd, record.data.addr);
648 if (per->had_vma == false)
650 /* Take the first vma we see as the base */
651 section->vma = dst_offset;
655 dst_offset -= section->vma;
657 dst_base_ptr = oasys_per_section (section)->data;
658 dst_ptr = oasys_per_section (section)->data +
663 section->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
665 while (src < end_src)
667 unsigned char mod_byte = *src++;
668 size_t gap = end_src - src;
671 if (mod_byte == 0 && gap >= 8)
686 for (relbit = 1; count-- != 0 && src < end_src; relbit <<= 1)
688 if (relbit & mod_byte)
690 unsigned char reloc = *src;
691 /* This item needs to be relocated */
692 switch (reloc & RELOCATION_TYPE_BITS)
694 case RELOCATION_TYPE_ABS:
698 case RELOCATION_TYPE_REL:
700 /* Relocate the item relative to the section */
701 oasys_reloc_type *r =
704 sizeof (oasys_reloc_type));
707 bfd_set_error (bfd_error_no_memory);
710 *(per->reloc_tail_ptr) = r;
711 per->reloc_tail_ptr = &r->next;
712 r->next = (oasys_reloc_type *) NULL;
713 /* Reference to undefined symbol */
715 /* There is no symbol */
717 /* Work out the howto */
721 data->sections[reloc &
722 RELOCATION_SECT_BITS];
725 r->relent.section->vma;
727 r->relent.address = dst_ptr - dst_base_ptr;
728 r->relent.howto = &howto_table[reloc >> 6];
729 r->relent.sym_ptr_ptr = (asymbol **) NULL;
730 section->reloc_count++;
732 /* Fake up the data to look like it's got the -ve pc in it, this makes
733 it much easier to convert into other formats. This is done by
736 if (r->relent.howto->pc_relative == true)
738 r->relent.addend -= dst_ptr - dst_base_ptr;
746 case RELOCATION_TYPE_UND:
748 oasys_reloc_type *r =
751 sizeof (oasys_reloc_type));
754 bfd_set_error (bfd_error_no_memory);
757 *(per->reloc_tail_ptr) = r;
758 per->reloc_tail_ptr = &r->next;
759 r->next = (oasys_reloc_type *) NULL;
760 /* Reference to undefined symbol */
762 /* Get symbol number */
763 r->symbol = (src[0] << 8) | src[1];
764 /* Work out the howto */
768 r->relent.section = (asection
771 r->relent.addend = 0;
772 r->relent.address = dst_ptr - dst_base_ptr;
773 r->relent.howto = &howto_table[reloc >> 6];
774 r->relent.sym_ptr_ptr = (asymbol **) NULL;
775 section->reloc_count++;
778 /* Fake up the data to look like it's got the -ve pc in it, this makes
779 it much easier to convert into other formats. This is done by
782 if (r->relent.howto->pc_relative == true)
784 r->relent.addend -= dst_ptr - dst_base_ptr;
791 case RELOCATION_TYPE_COM:
801 case oasys_record_is_local_enum:
802 case oasys_record_is_symbol_enum:
803 case oasys_record_is_section_enum:
815 oasys_new_section_hook (abfd, newsect)
819 newsect->used_by_bfd = (PTR)
820 bfd_alloc (abfd, sizeof (oasys_per_section_type));
821 if (!newsect->used_by_bfd)
823 bfd_set_error (bfd_error_no_memory);
826 oasys_per_section (newsect)->data = (bfd_byte *) NULL;
827 oasys_per_section (newsect)->section = newsect;
828 oasys_per_section (newsect)->offset = 0;
829 oasys_per_section (newsect)->initialized = false;
830 newsect->alignment_power = 1;
831 /* Turn the section string into an index */
833 sscanf (newsect->name, "%u", &newsect->target_index);
840 oasys_get_reloc_upper_bound (abfd, asect)
844 if (! oasys_slurp_section_data (abfd))
846 return (asect->reloc_count + 1) * sizeof (arelent *);
850 oasys_get_section_contents (abfd, section, location, offset, count)
857 oasys_per_section_type *p = (oasys_per_section_type *) section->used_by_bfd;
858 oasys_slurp_section_data (abfd);
859 if (p->initialized == false)
861 (void) memset (location, 0, (int) count);
865 (void) memcpy (location, (PTR) (p->data + offset), (int) count);
872 oasys_canonicalize_reloc (ignore_abfd, section, relptr, symbols)
878 unsigned int reloc_count = 0;
879 oasys_reloc_type *src = (oasys_reloc_type *) (section->relocation);
880 while (src != (oasys_reloc_type *) NULL)
885 if (src->relent.section == (asection *) NULL)
887 src->relent.sym_ptr_ptr = symbols + src->symbol;
891 *relptr++ = &src->relent;
895 *relptr = (arelent *) NULL;
896 return section->reloc_count = reloc_count;
905 /* Calculate the checksum and write one record */
907 oasys_write_record (abfd, type, record, size)
909 oasys_record_enum_type type;
910 oasys_record_union_type *record;
917 record->header.length = size;
918 record->header.type = (int) type;
919 record->header.check_sum = 0;
920 record->header.fill = 0;
921 ptr = (unsigned char *) &record->pad[0];
923 for (i = 0; i < size; i++)
927 record->header.check_sum = 0xff & (-checksum);
928 if (bfd_write ((PTR) record, 1, size, abfd) != size)
934 /* Write out all the symbols */
936 oasys_write_syms (abfd)
940 asymbol **generic = bfd_get_outsymbols (abfd);
941 unsigned int index = 0;
942 for (count = 0; count < bfd_get_symcount (abfd); count++)
945 oasys_symbol_record_type symbol;
946 asymbol *CONST g = generic[count];
948 CONST char *src = g->name;
949 char *dst = symbol.name;
952 if (bfd_is_com_section (g->section))
954 symbol.relb = RELOCATION_TYPE_COM;
955 bfd_h_put_16 (abfd, index, symbol.refno);
958 else if (bfd_is_abs_section (g->section))
960 symbol.relb = RELOCATION_TYPE_ABS;
961 bfd_h_put_16 (abfd, 0, symbol.refno);
964 else if (bfd_is_und_section (g->section))
966 symbol.relb = RELOCATION_TYPE_UND;
967 bfd_h_put_16 (abfd, index, symbol.refno);
968 /* Overload the value field with the output index number */
971 else if (g->flags & BSF_DEBUGGING)
978 if (g->section == (asection *) NULL)
980 /* Sometime, the oasys tools give out a symbol with illegal
981 bits in it, we'll output it in the same broken way */
983 symbol.relb = RELOCATION_TYPE_REL | 0;
987 symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index;
989 bfd_h_put_16 (abfd, 0, symbol.refno);
991 #ifdef UNDERSCORE_HACK
1001 bfd_h_put_32 (abfd, g->value, symbol.value);
1004 if (g->flags & BSF_LOCAL)
1006 if (! oasys_write_record (abfd,
1007 oasys_record_is_local_enum,
1008 (oasys_record_union_type *) & symbol,
1009 offsetof (oasys_symbol_record_type,
1015 if (! oasys_write_record (abfd,
1016 oasys_record_is_symbol_enum,
1017 (oasys_record_union_type *) & symbol,
1018 offsetof (oasys_symbol_record_type,
1022 g->value = index - 1;
1029 /* Write a section header for each section */
1031 oasys_write_sections (abfd)
1035 static oasys_section_record_type out;
1037 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1039 if (!isdigit (s->name[0]))
1041 bfd_set_error (bfd_error_nonrepresentable_section);
1044 out.relb = RELOCATION_TYPE_REL | s->target_index;
1045 bfd_h_put_32 (abfd, s->_cooked_size, out.value);
1046 bfd_h_put_32 (abfd, s->vma, out.vma);
1048 if (! oasys_write_record (abfd,
1049 oasys_record_is_section_enum,
1050 (oasys_record_union_type *) & out,
1058 oasys_write_header (abfd)
1061 /* Create and write the header */
1062 oasys_header_record_type r;
1063 size_t length = strlen (abfd->filename);
1064 if (length > (size_t) sizeof (r.module_name))
1066 length = sizeof (r.module_name);
1069 (void) memcpy (r.module_name,
1072 (void) memset (r.module_name + length,
1074 sizeof (r.module_name) - length);
1076 r.version_number = OASYS_VERSION_NUMBER;
1077 r.rev_number = OASYS_REV_NUMBER;
1078 if (! oasys_write_record (abfd,
1079 oasys_record_is_header_enum,
1080 (oasys_record_union_type *) & r,
1081 offsetof (oasys_header_record_type,
1089 oasys_write_end (abfd)
1092 oasys_end_record_type end;
1093 unsigned char null = 0;
1094 end.relb = RELOCATION_TYPE_ABS;
1095 bfd_h_put_32 (abfd, abfd->start_address, end.entry);
1096 bfd_h_put_16 (abfd, 0, end.fill);
1098 if (! oasys_write_record (abfd,
1099 oasys_record_is_end_enum,
1100 (oasys_record_union_type *) & end,
1103 if (bfd_write ((PTR) & null, 1, 1, abfd) != 1)
1113 arelent *a = *((arelent **) ap);
1114 arelent *b = *((arelent **) bp);
1115 return a->address - b->address;
1123 oasys_write_data (abfd)
1127 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1129 if (s->flags & SEC_LOAD)
1131 bfd_byte *raw_data = oasys_per_section (s)->data;
1132 oasys_data_record_type processed_data;
1133 bfd_size_type current_byte_index = 0;
1134 unsigned int relocs_to_go = s->reloc_count;
1135 arelent **p = s->orelocation;
1136 if (s->reloc_count != 0)
1138 /* Sort the reloc records so it's easy to insert the relocs into the
1141 qsort (s->orelocation,
1143 sizeof (arelent **),
1146 current_byte_index = 0;
1147 processed_data.relb = s->target_index | RELOCATION_TYPE_REL;
1149 while (current_byte_index < s->_cooked_size)
1151 /* Scan forwards by eight bytes or however much is left and see if
1152 there are any relocations going on */
1153 bfd_byte *mod = &processed_data.data[0];
1154 bfd_byte *dst = &processed_data.data[1];
1160 bfd_h_put_32 (abfd, s->vma + current_byte_index,
1161 processed_data.addr);
1163 /* Don't start a relocation unless you're sure you can finish it
1164 within the same data record. The worst case relocation is a
1165 4-byte relocatable value which is split across two modification
1166 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1167 1 modification byte + 2 data = 8 bytes total). That's where
1168 the magic number 8 comes from.
1170 while (current_byte_index < s->_raw_size && dst <=
1171 &processed_data.data[sizeof (processed_data.data) - 8])
1175 if (relocs_to_go != 0)
1178 reloc_howto_type *const how = r->howto;
1179 /* There is a relocation, is it for this byte ? */
1180 if (r->address == current_byte_index)
1182 unsigned char rel_byte;
1188 if (how->pc_relative)
1190 rel_byte = RELOCATION_PCREL_BIT;
1192 /* Also patch the raw data so that it doesn't have
1193 the -ve stuff any more */
1197 bfd_get_16 (abfd, raw_data) +
1198 current_byte_index, raw_data);
1204 bfd_get_32 (abfd, raw_data) +
1205 current_byte_index, raw_data);
1214 rel_byte |= RELOCATION_32BIT_BIT;
1217 /* Is this a section relative relocation, or a symbol
1218 relative relocation ? */
1222 if (r->section != (asection *) NULL)
1224 /* The relent has a section attached, so it must be section
1226 rel_byte |= RELOCATION_TYPE_REL;
1227 rel_byte |= r->section->output_section->target_index;
1233 asymbol *p = *(r->sym_ptr_ptr);
1235 /* If this symbol has a section attached, then it
1236 has already been resolved. Change from a symbol
1237 ref to a section ref */
1238 if (p->section != (asection *) NULL)
1240 rel_byte |= RELOCATION_TYPE_REL;
1242 p->section->output_section->target_index;
1247 rel_byte |= RELOCATION_TYPE_UND;
1249 /* Next two bytes are a symbol index - we can get
1250 this from the symbol value which has been zapped
1251 into the symbol index in the table when the
1252 symbol table was written
1254 *dst++ = p->value >> 8;
1258 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1259 /* relocations never occur from an unloadable section,
1260 so we can assume that raw_data is not NULL
1262 *dst++ = *raw_data++;
1264 * dst++ = *raw_data++;
1268 *dst++ = *raw_data++;
1270 * dst++ = *raw_data++;
1276 /* If this is coming from an unloadable section then copy
1278 if (raw_data == NULL)
1284 *dst++ = *raw_data++;
1289 /* Don't write a useless null modification byte */
1295 if (! oasys_write_record (abfd,
1296 oasys_record_is_data_enum,
1297 ((oasys_record_union_type *)
1299 dst - (bfd_byte *) & processed_data))
1309 oasys_write_object_contents (abfd)
1312 if (! oasys_write_header (abfd))
1314 if (! oasys_write_syms (abfd))
1316 if (! oasys_write_sections (abfd))
1318 if (! oasys_write_data (abfd))
1320 if (! oasys_write_end (abfd))
1328 /** exec and core file sections */
1330 /* set section contents is complicated with OASYS since the format is
1331 * not a byte image, but a record stream.
1334 oasys_set_section_contents (abfd, section, location, offset, count)
1339 bfd_size_type count;
1343 if (oasys_per_section (section)->data == (bfd_byte *) NULL)
1345 oasys_per_section (section)->data =
1346 (bfd_byte *) (bfd_alloc (abfd, section->_cooked_size));
1347 if (!oasys_per_section (section)->data)
1349 bfd_set_error (bfd_error_no_memory);
1353 (void) memcpy ((PTR) (oasys_per_section (section)->data + offset),
1362 /* Native-level interface to symbols. */
1364 /* We read the symbols into a buffer, which is discarded when this
1365 function exits. We read the strings into a buffer large enough to
1366 hold them all plus all the cached symbol entries. */
1369 oasys_make_empty_symbol (abfd)
1373 oasys_symbol_type *new =
1374 (oasys_symbol_type *) bfd_zalloc (abfd, sizeof (oasys_symbol_type));
1377 bfd_set_error (bfd_error_no_memory);
1380 new->symbol.the_bfd = abfd;
1381 return &new->symbol;
1387 /* User should have checked the file flags; perhaps we should return
1388 BFD_NO_MORE_SYMBOLS if there are none? */
1391 oasys_openr_next_archived_file (arch, prev)
1395 oasys_ar_data_type *ar = OASYS_AR_DATA (arch);
1396 oasys_module_info_type *p;
1397 /* take the next one from the arch state, or reset */
1398 if (prev == (bfd *) NULL)
1400 /* Reset the index - the first two entries are bogus*/
1401 ar->module_index = 0;
1404 p = ar->module + ar->module_index;
1407 if (ar->module_index <= ar->module_count)
1409 if (p->abfd == (bfd *) NULL)
1411 p->abfd = _bfd_create_empty_archive_element_shell (arch);
1412 p->abfd->origin = p->pos;
1413 p->abfd->filename = p->name;
1415 /* Fixup a pointer to this element for the member */
1416 p->abfd->arelt_data = (PTR) p;
1422 bfd_set_error (bfd_error_no_more_archived_files);
1423 return (bfd *) NULL;
1428 oasys_find_nearest_line (abfd,
1439 char **filename_ptr;
1440 char **functionname_ptr;
1441 unsigned int *line_ptr;
1448 oasys_generic_stat_arch_elt (abfd, buf)
1452 oasys_module_info_type *mod = (oasys_module_info_type *) abfd->arelt_data;
1453 if (mod == (oasys_module_info_type *) NULL)
1455 bfd_set_error (bfd_error_invalid_operation);
1460 buf->st_size = mod->size;
1461 buf->st_mode = 0666;
1467 oasys_sizeof_headers (abfd, exec)
1474 #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup
1475 #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1477 #define oasys_slurp_armap bfd_true
1478 #define oasys_slurp_extended_name_table bfd_true
1479 #define oasys_construct_extended_name_table \
1480 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1482 #define oasys_truncate_arname bfd_dont_truncate_arname
1483 #define oasys_write_armap \
1485 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1487 #define oasys_update_armap_timestamp bfd_true
1489 #define oasys_bfd_is_local_label bfd_generic_is_local_label
1490 #define oasys_get_lineno _bfd_nosymbols_get_lineno
1491 #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1492 #define oasys_read_minisymbols _bfd_generic_read_minisymbols
1493 #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1495 #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1497 #define oasys_set_arch_mach bfd_default_set_arch_mach
1499 #define oasys_bfd_get_relocated_section_contents \
1500 bfd_generic_get_relocated_section_contents
1501 #define oasys_bfd_relax_section bfd_generic_relax_section
1502 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1503 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1504 #define oasys_bfd_final_link _bfd_generic_final_link
1505 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1508 const bfd_target oasys_vec =
1511 bfd_target_oasys_flavour,
1512 true, /* target byte order */
1513 true, /* target headers byte order */
1514 (HAS_RELOC | EXEC_P | /* object flags */
1515 HAS_LINENO | HAS_DEBUG |
1516 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1517 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1518 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1519 0, /* leading underscore */
1520 ' ', /* ar_pad_char */
1521 16, /* ar_max_namelen */
1522 1, /* minimum alignment */
1523 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1524 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1525 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1526 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1527 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1528 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1531 oasys_object_p, /* bfd_check_format */
1535 { /* bfd_set_format */
1538 _bfd_generic_mkarchive,
1541 { /* bfd_write_contents */
1543 oasys_write_object_contents,
1544 _bfd_write_archive_contents,
1548 BFD_JUMP_TABLE_GENERIC (oasys),
1549 BFD_JUMP_TABLE_COPY (_bfd_generic),
1550 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1551 BFD_JUMP_TABLE_ARCHIVE (oasys),
1552 BFD_JUMP_TABLE_SYMBOLS (oasys),
1553 BFD_JUMP_TABLE_RELOCS (oasys),
1554 BFD_JUMP_TABLE_WRITE (oasys),
1555 BFD_JUMP_TABLE_LINK (oasys),
1556 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),