1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #define UNDERSCORE_HACK 1
28 /* XXX - FIXME. offsetof belongs in the system-specific files in
30 /* Define offsetof for those systems which lack it */
33 #define offsetof(type, identifier) (size_t) &(((type *) 0)->identifier)
36 static boolean oasys_write_sections PARAMS ((bfd *));
38 /* Read in all the section data and relocation stuff too */
39 PROTO(static boolean,oasys_slurp_section_data,(bfd *CONST abfd));
42 DEFUN(oasys_read_record,(abfd, record),
44 oasys_record_union_type *record)
47 bfd_read((PTR)record, 1, sizeof(record->header), abfd);
49 if ((size_t) record->header.length <= (size_t) sizeof (record->header))
51 bfd_read((PTR)(((char *)record )+ sizeof(record->header)),
52 1, record->header.length - sizeof(record->header),
56 DEFUN(oasys_string_length,(record),
57 oasys_record_union_type *record)
59 return record->header.length
60 - ((char *)record->symbol.name - (char *)record);
63 /*****************************************************************************/
67 Slurp the symbol table by reading in all the records at the start file
68 till we get to the first section record.
70 We'll sort the symbolss into two lists, defined and undefined. The
71 undefined symbols will be placed into the table according to their
74 We do this by placing all undefined symbols at the front of the table
75 moving in, and the defined symbols at the end of the table moving back.
80 DEFUN(oasys_slurp_symbol_table,(abfd),
83 oasys_record_union_type record;
84 oasys_data_type *data = OASYS_DATA(abfd);
86 asymbol *dest_defined;
91 if (data->symbols != (asymbol *)NULL) {
94 /* Buy enough memory for all the symbols and all the names */
96 (asymbol *)bfd_alloc(abfd, sizeof(asymbol) * abfd->symcount);
97 #ifdef UNDERSCORE_HACK
98 /* buy 1 more char for each symbol to keep the underscore in*/
99 data->strings = bfd_alloc(abfd, data->symbol_string_length +
102 data->strings = bfd_alloc(abfd, data->symbol_string_length);
104 if (!data->symbols || !data->strings)
106 bfd_error = no_memory;
110 dest_defined = data->symbols + abfd->symcount -1;
112 string_ptr = data->strings;
113 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
116 oasys_read_record(abfd, &record);
117 switch (record.header.type) {
118 case oasys_record_is_header_enum:
120 case oasys_record_is_local_enum:
121 case oasys_record_is_symbol_enum:
123 int flag = record.header.type == (int)oasys_record_is_local_enum ?
124 (BSF_LOCAL) : (BSF_GLOBAL | BSF_EXPORT);
127 size_t length = oasys_string_length(&record);
128 switch (record.symbol.relb & RELOCATION_TYPE_BITS) {
129 case RELOCATION_TYPE_ABS:
130 dest = dest_defined--;
131 dest->section = &bfd_abs_section;
135 case RELOCATION_TYPE_REL:
136 dest = dest_defined--;
138 OASYS_DATA(abfd)->sections[record.symbol.relb &
139 RELOCATION_SECT_BITS];
140 if (record.header.type == (int)oasys_record_is_local_enum)
142 dest->flags = BSF_LOCAL;
143 if (dest->section ==(asection *)(~0)) {
144 /* It seems that sometimes internal symbols are tied up, but
145 still get output, even though there is no
155 case RELOCATION_TYPE_UND:
156 dest = data->symbols + bfd_h_get_16(abfd, record.symbol.refno);
157 dest->section = &bfd_und_section;
159 case RELOCATION_TYPE_COM:
160 dest = dest_defined--;
161 dest->name = string_ptr;
162 dest->the_bfd = abfd;
164 dest->section = &bfd_com_section;
168 dest = dest_defined--;
172 dest->name = string_ptr;
173 dest->the_bfd = abfd;
174 dest->udata = (PTR)NULL;
175 dest->value = bfd_h_get_32(abfd, record.symbol.value);
177 #ifdef UNDERSCORE_HACK
178 if (record.symbol.name[0] != '_') {
183 memcpy(string_ptr, record.symbol.name, length);
186 string_ptr[length] =0;
187 string_ptr += length +1;
198 DEFUN(oasys_get_symtab_upper_bound,(abfd),
201 oasys_slurp_symbol_table (abfd);
203 return (abfd->symcount+1) * (sizeof (oasys_symbol_type *));
209 extern bfd_target oasys_vec;
212 DEFUN(oasys_get_symtab,(abfd, location),
217 unsigned int counter ;
218 if (oasys_slurp_symbol_table(abfd) == false) {
221 symbase = OASYS_DATA(abfd)->symbols;
222 for (counter = 0; counter < abfd->symcount; counter++) {
223 *(location++) = symbase++;
226 return abfd->symcount;
229 /***********************************************************************
234 DEFUN(oasys_archive_p,(abfd),
237 oasys_archive_header_type header;
238 oasys_extarchive_header_type header_ext;
242 bfd_seek(abfd, (file_ptr) 0, false);
243 bfd_read((PTR)&header_ext, 1, sizeof(header_ext), abfd);
245 header.version = bfd_h_get_32(abfd, header_ext.version);
246 header.mod_count = bfd_h_get_32(abfd, header_ext.mod_count);
247 header.mod_tbl_offset = bfd_h_get_32(abfd, header_ext.mod_tbl_offset);
248 header.sym_tbl_size = bfd_h_get_32(abfd, header_ext.sym_tbl_size);
249 header.sym_count = bfd_h_get_32(abfd, header_ext.sym_count);
250 header.sym_tbl_offset = bfd_h_get_32(abfd, header_ext.sym_tbl_offset);
251 header.xref_count = bfd_h_get_32(abfd, header_ext.xref_count);
252 header.xref_lst_offset = bfd_h_get_32(abfd, header_ext.xref_lst_offset);
255 There isn't a magic number in an Oasys archive, so the best we
256 can do to verify reasnableness is to make sure that the values in
257 the header are too weird
260 if (header.version>10000 ||
261 header.mod_count>10000 ||
262 header.sym_count>100000 ||
263 header.xref_count > 100000) return (bfd_target *)NULL;
266 That all worked, let's buy the space for the header and read in
270 oasys_ar_data_type *ar =
271 (oasys_ar_data_type*) bfd_alloc(abfd, sizeof(oasys_ar_data_type));
273 oasys_module_info_type *module =
274 (oasys_module_info_type*)
275 bfd_alloc(abfd, sizeof(oasys_module_info_type) * header.mod_count);
276 oasys_module_table_type record;
280 bfd_error = no_memory;
284 abfd->tdata.oasys_ar_data = ar;
286 ar->module_count = header.mod_count;
288 filepos = header.mod_tbl_offset;
289 for (i = 0; i < header.mod_count; i++) {
290 bfd_seek(abfd, filepos, SEEK_SET);
292 /* There are two ways of specifying the archive header */
295 oasys_extmodule_table_type_a_type record_ext;
296 bfd_read((PTR)&record_ext, 1, sizeof(record_ext), abfd);
298 record.mod_size = bfd_h_get_32(abfd, record_ext.mod_size);
299 record.file_offset = bfd_h_get_32(abfd, record_ext.file_offset);
301 record.dep_count = bfd_h_get_32(abfd, record_ext.dep_count);
302 record.depee_count = bfd_h_get_32(abfd, record_ext.depee_count);
303 record.sect_count = bfd_h_get_32(abfd, record_ext.sect_count);
305 module[i].name = bfd_alloc(abfd,33);
308 bfd_error = no_error;
312 memcpy(module[i].name, record_ext.mod_name, 33);
315 record.dep_count * 4 +
316 record.depee_count * 4 +
317 record.sect_count * 8 + 187;
320 oasys_extmodule_table_type_b_type record_ext;
321 bfd_read((PTR)&record_ext, 1, sizeof(record_ext), abfd);
323 record.mod_size = bfd_h_get_32(abfd, record_ext.mod_size);
324 record.file_offset = bfd_h_get_32(abfd, record_ext.file_offset);
326 record.dep_count = bfd_h_get_32(abfd, record_ext.dep_count);
327 record.depee_count = bfd_h_get_32(abfd, record_ext.depee_count);
328 record.sect_count = bfd_h_get_32(abfd, record_ext.sect_count);
329 record.module_name_size = bfd_h_get_32(abfd, record_ext.mod_name_length);
331 module[i].name = bfd_alloc(abfd,record.module_name_size + 1);
334 bfd_error = no_error;
337 bfd_read((PTR)module[i].name, 1, record.module_name_size, abfd);
338 module[i].name[record.module_name_size] = 0;
341 record.dep_count * 4 +
342 record.module_name_size + 1;
347 module[i].size = record.mod_size;
348 module[i].pos = record.file_offset;
357 DEFUN(oasys_mkobject,(abfd),
361 abfd->tdata.oasys_obj_data = (oasys_data_type*)bfd_alloc(abfd, sizeof(oasys_data_type));
362 return abfd->tdata.oasys_obj_data ? true : false;
367 DEFUN(oasys_object_p,(abfd),
370 oasys_data_type *oasys;
371 oasys_data_type *save = OASYS_DATA(abfd);
373 boolean had_usefull = false;
375 abfd->tdata.oasys_obj_data = 0;
376 oasys_mkobject(abfd);
377 oasys = OASYS_DATA(abfd);
378 memset((PTR)oasys->sections, 0xff, sizeof(oasys->sections));
380 /* Point to the start of the file */
381 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
382 oasys->symbol_string_length = 0;
383 /* Inspect the records, but only keep the section info -
384 remember the size of the symbols
386 oasys->first_data_record = 0;
388 oasys_record_union_type record;
389 oasys_read_record(abfd, &record);
390 if ((size_t)record.header.length < (size_t)sizeof(record.header))
394 switch ((oasys_record_enum_type)(record.header.type)) {
395 case oasys_record_is_header_enum:
398 case oasys_record_is_symbol_enum:
399 case oasys_record_is_local_enum:
400 /* Count symbols and remember their size for a future malloc */
402 oasys->symbol_string_length += 1 + oasys_string_length(&record);
405 case oasys_record_is_section_enum:
409 unsigned int section_number;
410 if (record.section.header.length != sizeof(record.section))
414 buffer = bfd_alloc(abfd, 3);
417 bfd_error = no_memory;
420 section_number= record.section.relb & RELOCATION_SECT_BITS;
421 sprintf(buffer,"%u", section_number);
422 s = bfd_make_section(abfd,buffer);
423 oasys->sections[section_number] = s;
424 switch (record.section.relb & RELOCATION_TYPE_BITS) {
425 case RELOCATION_TYPE_ABS:
426 case RELOCATION_TYPE_REL:
428 case RELOCATION_TYPE_UND:
429 case RELOCATION_TYPE_COM:
433 s->_raw_size = bfd_h_get_32(abfd, record.section.value);
434 s->vma = bfd_h_get_32(abfd, record.section.vma);
439 case oasys_record_is_data_enum:
440 oasys->first_data_record = bfd_tell(abfd) - record.header.length;
441 case oasys_record_is_debug_enum:
442 case oasys_record_is_module_enum:
443 case oasys_record_is_named_section_enum:
444 case oasys_record_is_end_enum:
445 if (had_usefull == false) goto fail;
452 oasys->symbols = (asymbol *)NULL;
454 Oasys support several architectures, but I can't see a simple way
455 to discover which one is in a particular file - we'll guess
457 bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
458 if (abfd->symcount != 0) {
459 abfd->flags |= HAS_SYMS;
463 We don't know if a section has data until we've read it..
466 oasys_slurp_section_data(abfd);
472 (void) bfd_release(abfd, oasys);
473 abfd->tdata.oasys_obj_data = save;
474 return (bfd_target *)NULL;
479 DEFUN(oasys_get_symbol_info,(ignore_abfd, symbol, ret),
484 bfd_symbol_info (symbol, ret);
485 if (!symbol->section)
486 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
490 DEFUN(oasys_print_symbol,(ignore_abfd, afile, symbol, how),
494 bfd_print_symbol_type how)
496 FILE *file = (FILE *)afile;
499 case bfd_print_symbol_name:
500 case bfd_print_symbol_more:
501 fprintf(file,"%s", symbol->name);
503 case bfd_print_symbol_all:
505 CONST char *section_name = symbol->section == (asection *)NULL ?
506 (CONST char *) "*abs" : symbol->section->name;
508 bfd_print_symbol_vandf((PTR)file,symbol);
510 fprintf(file," %-5s %s",
518 The howto table is build using the top two bits of a reloc byte to
519 index into it. The bits are PCREL,WORD/LONG
521 static reloc_howto_type howto_table[]=
524 HOWTO( 0, 0, 1, 16, false,0,complain_overflow_bitfield,0,"abs16",true,0x0000ffff, 0x0000ffff,false),
525 HOWTO( 0, 0, 2, 32, false,0,complain_overflow_bitfield,0,"abs32",true,0xffffffff, 0xffffffff,false),
526 HOWTO( 0, 0, 1, 16, true,0,complain_overflow_signed,0,"pcrel16",true,0x0000ffff, 0x0000ffff,false),
527 HOWTO( 0, 0, 2, 32, true,0,complain_overflow_signed,0,"pcrel32",true,0xffffffff, 0xffffffff,false)
530 /* Read in all the section data and relocation stuff too */
532 DEFUN(oasys_slurp_section_data,(abfd),
535 oasys_record_union_type record;
536 oasys_data_type *data = OASYS_DATA(abfd);
539 oasys_per_section_type *per ;
543 /* See if the data has been slurped already .. */
544 for (s = abfd->sections; s != (asection *)NULL; s= s->next) {
545 per = oasys_per_section(s);
546 if (per->initialized == true)
550 if (data->first_data_record == 0) return true;
552 bfd_seek(abfd, data->first_data_record, SEEK_SET);
554 oasys_read_record(abfd, &record);
555 switch (record.header.type)
557 case oasys_record_is_header_enum:
559 case oasys_record_is_data_enum:
562 bfd_byte *src = record.data.data;
563 bfd_byte *end_src = ((bfd_byte *)&record) + record.header.length;
565 bfd_byte *dst_base_ptr;
569 data->sections[record.data.relb & RELOCATION_SECT_BITS];
572 per = oasys_per_section(section);
574 if (per->initialized == false)
576 per->data = (bfd_byte *) bfd_zalloc(abfd, section->_raw_size);
579 bfd_error = no_memory;
582 per->reloc_tail_ptr = (oasys_reloc_type **)&(section->relocation);
583 per->had_vma = false;
584 per->initialized = true;
585 section->reloc_count = 0;
586 section->flags = SEC_ALLOC;
589 dst_offset = bfd_h_get_32(abfd, record.data.addr) ;
590 if (per->had_vma == false) {
591 /* Take the first vma we see as the base */
592 section->vma = dst_offset;
596 dst_offset -= section->vma;
598 dst_base_ptr = oasys_per_section(section)->data;
599 dst_ptr = oasys_per_section(section)->data +
603 section->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
605 while (src < end_src) {
606 unsigned char mod_byte = *src++;
607 size_t gap = end_src - src;
610 if (mod_byte == 0 && gap >= 8) {
623 for (relbit = 1; count-- != 0 && src < end_src; relbit <<=1)
625 if (relbit & mod_byte)
627 unsigned char reloc = *src;
628 /* This item needs to be relocated */
629 switch (reloc & RELOCATION_TYPE_BITS) {
630 case RELOCATION_TYPE_ABS:
634 case RELOCATION_TYPE_REL:
636 /* Relocate the item relative to the section */
637 oasys_reloc_type *r =
640 sizeof(oasys_reloc_type));
643 bfd_error = no_memory;
646 *(per->reloc_tail_ptr) = r;
647 per->reloc_tail_ptr = &r->next;
648 r->next= (oasys_reloc_type *)NULL;
649 /* Reference to undefined symbol */
651 /* There is no symbol */
653 /* Work out the howto */
657 data->sections[reloc &
658 RELOCATION_SECT_BITS];
661 r->relent.section->vma;
663 r->relent.address = dst_ptr - dst_base_ptr;
664 r->relent.howto = &howto_table[reloc>>6];
665 r->relent.sym_ptr_ptr = (asymbol **)NULL;
666 section->reloc_count++;
668 /* Fake up the data to look like it's got the -ve pc in it, this makes
669 it much easier to convert into other formats. This is done by
672 if (r->relent.howto->pc_relative == true) {
673 r->relent.addend -= dst_ptr - dst_base_ptr;
681 case RELOCATION_TYPE_UND:
683 oasys_reloc_type *r =
686 sizeof(oasys_reloc_type));
689 bfd_error = no_memory;
692 *(per->reloc_tail_ptr) = r;
693 per->reloc_tail_ptr = &r->next;
694 r->next= (oasys_reloc_type *)NULL;
695 /* Reference to undefined symbol */
697 /* Get symbol number */
698 r->symbol = (src[0]<<8) | src[1];
699 /* Work out the howto */
703 r->relent.section = (asection
706 r->relent.addend = 0;
707 r->relent.address = dst_ptr - dst_base_ptr;
708 r->relent.howto = &howto_table[reloc>>6];
709 r->relent.sym_ptr_ptr = (asymbol **)NULL;
710 section->reloc_count++;
713 /* Fake up the data to look like it's got the -ve pc in it, this makes
714 it much easier to convert into other formats. This is done by
717 if (r->relent.howto->pc_relative == true) {
718 r->relent.addend -= dst_ptr - dst_base_ptr;
725 case RELOCATION_TYPE_COM:
735 case oasys_record_is_local_enum:
736 case oasys_record_is_symbol_enum:
737 case oasys_record_is_section_enum:
749 DEFUN(oasys_new_section_hook,(abfd, newsect),
753 newsect->used_by_bfd = (PTR)
754 bfd_alloc(abfd, sizeof(oasys_per_section_type));
755 if (!newsect->used_by_bfd)
757 bfd_error = no_memory;
760 oasys_per_section( newsect)->data = (bfd_byte *)NULL;
761 oasys_per_section(newsect)->section = newsect;
762 oasys_per_section(newsect)->offset = 0;
763 oasys_per_section(newsect)->initialized = false;
764 newsect->alignment_power = 1;
765 /* Turn the section string into an index */
767 sscanf(newsect->name,"%u", &newsect->target_index);
774 DEFUN(oasys_get_reloc_upper_bound, (abfd, asect),
778 oasys_slurp_section_data(abfd);
779 return (asect->reloc_count+1) * sizeof(arelent *);
783 DEFUN(oasys_get_section_contents,(abfd, section, location, offset, count),
790 oasys_per_section_type *p = (oasys_per_section_type *) section->used_by_bfd;
791 oasys_slurp_section_data(abfd);
792 if (p->initialized == false)
794 (void) memset(location, 0, (int)count);
798 (void) memcpy(location,(PTR)( p->data + offset), (int)count);
805 DEFUN(oasys_canonicalize_reloc,(ignore_abfd, section, relptr, symbols),
811 unsigned int reloc_count = 0;
812 oasys_reloc_type *src = (oasys_reloc_type *)(section->relocation);
813 while (src != (oasys_reloc_type *)NULL) {
817 if (src->relent.section == (asection *)NULL)
819 src->relent.sym_ptr_ptr = symbols + src->symbol;
823 *relptr ++ = &src->relent;
827 *relptr = (arelent *)NULL;
828 return section->reloc_count = reloc_count;
837 /* Calculate the checksum and write one record */
839 DEFUN(oasys_write_record,(abfd, type, record, size),
841 CONST oasys_record_enum_type type AND
842 oasys_record_union_type *record AND
849 record->header.length = size;
850 record->header.type = (int)type;
851 record->header.check_sum = 0;
852 record->header.fill = 0;
853 ptr = (unsigned char *)&record->pad[0];
855 for (i = 0; i < size; i++) {
858 record->header.check_sum = 0xff & (- checksum);
859 bfd_write((PTR)record, 1, size, abfd);
863 /* Write out all the symbols */
865 DEFUN(oasys_write_syms, (abfd),
869 asymbol **generic = bfd_get_outsymbols(abfd);
870 unsigned int index = 0;
871 for (count = 0; count < bfd_get_symcount(abfd); count++) {
873 oasys_symbol_record_type symbol;
874 asymbol * CONST g = generic[count];
876 CONST char *src = g->name;
877 char *dst = symbol.name;
880 if (bfd_is_com_section (g->section)) {
881 symbol.relb = RELOCATION_TYPE_COM;
882 bfd_h_put_16(abfd, index, symbol.refno);
885 else if (g->section == & bfd_abs_section) {
886 symbol.relb = RELOCATION_TYPE_ABS;
887 bfd_h_put_16(abfd, 0, symbol.refno);
890 else if (g->section == &bfd_und_section) {
891 symbol.relb = RELOCATION_TYPE_UND ;
892 bfd_h_put_16(abfd, index, symbol.refno);
893 /* Overload the value field with the output index number */
896 else if (g->flags & BSF_DEBUGGING) {
901 if (g->section == (asection *)NULL) {
902 /* Sometime, the oasys tools give out a symbol with illegal
903 bits in it, we'll output it in the same broken way */
905 symbol.relb = RELOCATION_TYPE_REL | 0;
908 symbol.relb = RELOCATION_TYPE_REL |g->section->output_section->target_index;
910 bfd_h_put_16(abfd, 0, symbol.refno);
912 #ifdef UNDERSCORE_HACK
921 bfd_h_put_32(abfd, g->value, symbol.value);
924 if (g->flags & BSF_LOCAL) {
925 oasys_write_record(abfd,
926 oasys_record_is_local_enum,
927 (oasys_record_union_type *) &symbol,
928 offsetof(oasys_symbol_record_type, name[0]) + l);
931 oasys_write_record(abfd,
932 oasys_record_is_symbol_enum,
933 (oasys_record_union_type *) &symbol,
934 offsetof(oasys_symbol_record_type, name[0]) + l);
941 /* Write a section header for each section */
943 oasys_write_sections (abfd)
947 static oasys_section_record_type out;
949 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
950 if (!isdigit(s->name[0]))
952 bfd_error = nonrepresentable_section;
955 out.relb = RELOCATION_TYPE_REL | s->target_index;
956 bfd_h_put_32(abfd, s->_cooked_size, out.value);
957 bfd_h_put_32(abfd, s->vma, out.vma);
959 oasys_write_record(abfd,
960 oasys_record_is_section_enum,
961 (oasys_record_union_type *) &out,
968 DEFUN(oasys_write_header, (abfd),
971 /* Create and write the header */
972 oasys_header_record_type r;
973 size_t length = strlen(abfd->filename);
974 if (length > (size_t)sizeof(r.module_name)) {
975 length = sizeof(r.module_name);
978 (void)memcpy(r.module_name,
981 (void)memset(r.module_name + length,
983 sizeof(r.module_name) - length);
985 r.version_number = OASYS_VERSION_NUMBER;
986 r.rev_number = OASYS_REV_NUMBER;
987 oasys_write_record(abfd,
988 oasys_record_is_header_enum,
989 (oasys_record_union_type *)&r,
990 offsetof(oasys_header_record_type, description[0]));
997 DEFUN(oasys_write_end,(abfd),
1000 oasys_end_record_type end;
1001 unsigned char null = 0;
1002 end.relb = RELOCATION_TYPE_ABS;
1003 bfd_h_put_32(abfd, abfd->start_address, end.entry);
1004 bfd_h_put_16(abfd, 0, end.fill);
1006 oasys_write_record(abfd,
1007 oasys_record_is_end_enum,
1008 (oasys_record_union_type *)&end,
1010 bfd_write((PTR)&null, 1, 1, abfd);
1014 DEFUN(comp,(ap, bp),
1018 arelent *a = *((arelent **)ap);
1019 arelent *b = *((arelent **)bp);
1020 return a->address - b->address;
1028 DEFUN(oasys_write_data, (abfd),
1032 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
1033 if (s->flags & SEC_LOAD) {
1034 bfd_byte *raw_data = oasys_per_section(s)->data;
1035 oasys_data_record_type processed_data;
1036 bfd_size_type current_byte_index = 0;
1037 unsigned int relocs_to_go = s->reloc_count;
1038 arelent **p = s->orelocation;
1039 if (s->reloc_count != 0) {
1040 /* Sort the reloc records so it's easy to insert the relocs into the
1043 qsort(s->orelocation,
1048 current_byte_index = 0;
1049 processed_data.relb = s->target_index | RELOCATION_TYPE_REL;
1051 while (current_byte_index < s->_cooked_size)
1053 /* Scan forwards by eight bytes or however much is left and see if
1054 there are any relocations going on */
1055 bfd_byte *mod = &processed_data.data[0];
1056 bfd_byte *dst = &processed_data.data[1];
1062 bfd_h_put_32(abfd, s->vma + current_byte_index,
1063 processed_data.addr);
1065 /* Don't start a relocation unless you're sure you can finish it
1066 within the same data record. The worst case relocation is a
1067 4-byte relocatable value which is split across two modification
1068 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1069 1 modification byte + 2 data = 8 bytes total). That's where
1070 the magic number 8 comes from.
1072 while (current_byte_index < s->_raw_size && dst <=
1073 &processed_data.data[sizeof(processed_data.data)-8]) {
1076 if (relocs_to_go != 0) {
1078 const reloc_howto_type * const how=r->howto;
1079 /* There is a relocation, is it for this byte ? */
1080 if (r->address == current_byte_index) {
1081 unsigned char rel_byte;
1087 if(how->pc_relative) {
1088 rel_byte = RELOCATION_PCREL_BIT;
1090 /* Also patch the raw data so that it doesn't have
1091 the -ve stuff any more */
1092 if (how->size != 2) {
1094 bfd_get_16(abfd,raw_data) +
1095 current_byte_index, raw_data);
1100 bfd_get_32(abfd,raw_data) +
1101 current_byte_index, raw_data);
1107 if (how->size ==2) {
1108 rel_byte |= RELOCATION_32BIT_BIT;
1111 /* Is this a section relative relocation, or a symbol
1112 relative relocation ? */
1116 if (r->section != (asection*)NULL)
1118 /* The relent has a section attached, so it must be section
1120 rel_byte |= RELOCATION_TYPE_REL;
1121 rel_byte |= r->section->output_section->target_index;
1127 asymbol *p = *(r->sym_ptr_ptr);
1129 /* If this symbol has a section attached, then it
1130 has already been resolved. Change from a symbol
1131 ref to a section ref */
1132 if(p->section != (asection *)NULL) {
1133 rel_byte |= RELOCATION_TYPE_REL;
1135 p->section->output_section->target_index;
1139 rel_byte |= RELOCATION_TYPE_UND;
1141 /* Next two bytes are a symbol index - we can get
1142 this from the symbol value which has been zapped
1143 into the symbol index in the table when the
1144 symbol table was written
1146 *dst++ = p->value >> 8;
1150 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1151 /* relocations never occur from an unloadable section,
1152 so we can assume that raw_data is not NULL
1154 *dst++ = *raw_data++;
1156 *dst++ = *raw_data++;
1158 if (how->size == 2) {
1159 *dst++ = *raw_data++;
1161 *dst++ = *raw_data++;
1167 /* If this is coming from an unloadable section then copy
1169 if (raw_data == NULL) {
1173 *dst++ = *raw_data++;
1178 /* Don't write a useless null modification byte */
1183 oasys_write_record(abfd,
1184 oasys_record_is_data_enum,
1185 (oasys_record_union_type *)&processed_data,
1186 dst - (bfd_byte *)&processed_data);
1193 DEFUN(oasys_write_object_contents, (abfd),
1196 oasys_write_header(abfd);
1197 oasys_write_syms(abfd);
1198 if (! oasys_write_sections(abfd))
1200 oasys_write_data(abfd);
1201 oasys_write_end(abfd);
1208 /** exec and core file sections */
1210 /* set section contents is complicated with OASYS since the format is
1211 * not a byte image, but a record stream.
1214 DEFUN(oasys_set_section_contents,(abfd, section, location, offset, count),
1219 bfd_size_type count)
1222 if (oasys_per_section(section)->data == (bfd_byte *)NULL )
1224 oasys_per_section(section)->data =
1225 (bfd_byte *)(bfd_alloc(abfd,section->_cooked_size));
1226 if (! oasys_per_section(section)->data)
1228 bfd_error = no_memory;
1232 (void) memcpy((PTR)(oasys_per_section(section)->data + offset),
1241 /* Native-level interface to symbols. */
1243 /* We read the symbols into a buffer, which is discarded when this
1244 function exits. We read the strings into a buffer large enough to
1245 hold them all plus all the cached symbol entries. */
1248 DEFUN(oasys_make_empty_symbol,(abfd),
1252 oasys_symbol_type *new =
1253 (oasys_symbol_type *)bfd_zalloc (abfd, sizeof (oasys_symbol_type));
1256 bfd_error = no_memory;
1259 new->symbol.the_bfd = abfd;
1260 return &new->symbol;
1266 /* User should have checked the file flags; perhaps we should return
1267 BFD_NO_MORE_SYMBOLS if there are none? */
1270 oasys_openr_next_archived_file(arch, prev)
1274 oasys_ar_data_type *ar = OASYS_AR_DATA(arch);
1275 oasys_module_info_type *p;
1276 /* take the next one from the arch state, or reset */
1277 if (prev == (bfd *)NULL) {
1278 /* Reset the index - the first two entries are bogus*/
1279 ar->module_index = 0;
1282 p = ar->module + ar->module_index;
1285 if (ar->module_index <= ar->module_count) {
1286 if (p->abfd == (bfd *)NULL) {
1287 p->abfd = _bfd_create_empty_archive_element_shell(arch);
1288 p->abfd->origin = p->pos;
1289 p->abfd->filename = p->name;
1291 /* Fixup a pointer to this element for the member */
1292 p->abfd->arelt_data = (PTR)p;
1297 bfd_error = no_more_archived_files;
1303 oasys_find_nearest_line(abfd,
1314 char **filename_ptr;
1315 char **functionname_ptr;
1316 unsigned int *line_ptr;
1323 DEFUN(oasys_generic_stat_arch_elt,(abfd, buf),
1327 oasys_module_info_type *mod = (oasys_module_info_type *) abfd->arelt_data;
1328 if (mod == (oasys_module_info_type *)NULL) {
1329 bfd_error = invalid_operation;
1333 buf->st_size = mod->size;
1334 buf->st_mode = 0666;
1340 DEFUN(oasys_sizeof_headers,(abfd, exec),
1347 #define oasys_core_file_failing_command (char *(*)())(bfd_nullvoidptr)
1348 #define oasys_core_file_failing_signal (int (*)())bfd_0
1349 #define oasys_core_file_matches_executable_p 0
1350 #define oasys_slurp_armap bfd_true
1351 #define oasys_slurp_extended_name_table bfd_true
1352 #define oasys_truncate_arname (void (*)())bfd_nullvoidptr
1353 #define oasys_write_armap 0
1354 #define oasys_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
1355 #define oasys_close_and_cleanup bfd_generic_close_and_cleanup
1356 #define oasys_set_arch_mach bfd_default_set_arch_mach
1357 #define oasys_bfd_debug_info_start bfd_void
1358 #define oasys_bfd_debug_info_end bfd_void
1359 #define oasys_bfd_debug_info_accumulate (FOO(void, (*), (bfd *, asection *)))bfd_void
1360 #define oasys_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
1361 #define oasys_bfd_relax_section bfd_generic_relax_section
1362 #define oasys_bfd_reloc_type_lookup \
1363 ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
1364 #define oasys_bfd_make_debug_symbol \
1365 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
1366 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1367 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1368 #define oasys_bfd_final_link _bfd_generic_final_link
1371 bfd_target oasys_vec =
1374 bfd_target_oasys_flavour,
1375 true, /* target byte order */
1376 true, /* target headers byte order */
1377 (HAS_RELOC | EXEC_P | /* object flags */
1378 HAS_LINENO | HAS_DEBUG |
1379 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1380 (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
1381 |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1382 0, /* leading underscore */
1383 ' ', /* ar_pad_char */
1384 16, /* ar_max_namelen */
1385 1, /* minimum alignment */
1386 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1387 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1388 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1389 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1390 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1391 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1394 oasys_object_p, /* bfd_check_format */
1398 { /* bfd_set_format */
1401 _bfd_generic_mkarchive,
1404 { /* bfd_write_contents */
1406 oasys_write_object_contents,
1407 _bfd_write_archive_contents,