3 bfd backend for oasys objects.
6 Object files contain records in order:
17 Written by Steve Chamberlain
35 #define obstack_chunk_alloc malloc
36 #define obstack_chunk_free free
38 typedef void generic_symbol_type;
41 void DEFUN(oasys_read_record,(abfd, record),
43 oasys_record_union_type *record)
46 bfd_read(record, 1, sizeof(record->header), abfd);
48 bfd_read(((char *)record )+ sizeof(record->header),
49 1, record->header.length - sizeof(record->header),
53 oasys_string_length(record)
54 oasys_record_union_type *record;
56 return record->header.length
57 - ((char *)record->symbol.name - (char *)record);
60 /*****************************************************************************/
64 Slurp the symbol table by reading in all the records at the start file
65 till we get to the first section record.
67 We'll sort the symbols into two lists, defined and undefined. The
68 undefined symbols will also be sorted by refno. We do this by placing
69 all undefined symbols at the front of the table moving in, and the
70 defined symbols at the end of the table moving back.
75 oasys_slurp_symbol_table(abfd)
78 oasys_record_union_type record;
79 oasys_data_type *data = oasys_data(abfd);
81 asymbol *dest_undefined;
82 asymbol *dest_defined;
87 if (data->symbols != (asymbol *)NULL) {
90 /* Buy enough memory for all the symbols and all the names */
92 (asymbol *)malloc(sizeof(asymbol) * abfd->symcount);
93 data->strings = malloc(data->symbol_string_length);
95 dest_undefined = data->symbols;
96 dest_defined = data->symbols + abfd->symcount -1;
98 string_ptr = data->strings;
99 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
101 oasys_read_record(abfd, &record);
102 switch (record.header.type) {
103 case oasys_record_is_header_enum:
105 case oasys_record_is_local_enum:
106 case oasys_record_is_symbol_enum:
108 size_t length = oasys_string_length(&record);
109 switch (record.symbol.relb[0] & RELOCATION_TYPE_BITS) {
110 case RELOCATION_TYPE_ABS:
111 dest = dest_defined--;
113 dest->flags = BSF_ABSOLUTE | BSF_EXPORT | BSF_GLOBAL;
116 case RELOCATION_TYPE_REL:
117 dest = dest_defined--;
119 oasys_data(abfd)->sections[record.symbol.relb[0] &
120 RELOCATION_SECT_BITS];
121 if (record.header.type == oasys_record_is_local_enum)
123 dest->flags = BSF_LOCAL;
127 dest->flags = BSF_EXPORT | BSF_GLOBAL;
130 case RELOCATION_TYPE_UND:
131 dest = dest_undefined++;
132 dest->section = (asection *)NULL;
133 dest->flags = BSF_UNDEFINED;
135 case RELOCATION_TYPE_COM:
136 dest = dest_defined--;
137 dest->name = string_ptr;
138 dest->the_bfd = abfd;
140 dest->section = (asection *)NULL;
141 dest->flags = BSF_FORT_COMM;
144 dest->name = string_ptr;
145 dest->the_bfd = abfd;
147 dest->value = bfd_h_getlong(abfd, &record.symbol.value);
148 memcpy(string_ptr, record.symbol.name, length);
149 string_ptr[length] =0;
150 string_ptr += length +1;
162 oasys_get_symtab_upper_bound (abfd)
165 oasys_slurp_symbol_table (abfd);
167 return (abfd->symcount != 0) ?
168 (abfd->symcount+1) * (sizeof (oasys_symbol_type *)) : 0;
174 extern bfd_target oasys_vec;
177 oasys_get_symtab (abfd, location)
182 unsigned int counter ;
183 if (oasys_slurp_symbol_table(abfd) == false) {
186 symbase = oasys_data(abfd)->symbols;
187 for (counter = 0; counter < abfd->symcount; counter++) {
188 *(location++) = symbase++;
191 return abfd->symcount;
194 /***********************************************************************
197 #define swap(x) x = bfd_h_get_x(abfd, &x);
199 oasys_archive_p(abfd)
202 oasys_archive_header_type header;
205 bfd_seek(abfd, (file_ptr) 0, false);
208 bfd_read(&header, 1, sizeof(header), abfd);
211 swap(header.version);
212 swap(header.mod_count);
213 swap(header.mod_tbl_offset);
214 swap(header.sym_tbl_size);
215 swap(header.sym_count);
216 swap(header.sym_tbl_offset);
217 swap(header.xref_count);
218 swap(header.xref_lst_offset);
221 There isn't a magic number in an Oasys archive, so the best we
222 can do to verify reasnableness is to make sure that the values in
223 the header are too weird
226 if (header.version>10000 ||
227 header.mod_count>10000 ||
228 header.sym_count>100000 ||
229 header.xref_count > 100000) return (bfd_target *)NULL;
232 That all worked, lets buy the space for the header and read in
236 oasys_ar_data_type *ar =
237 (oasys_ar_data_type*) malloc(sizeof(oasys_ar_data_type));
240 oasys_module_info_type *module =
241 (oasys_module_info_type*)
242 malloc(sizeof(oasys_module_info_type) * header.mod_count);
244 oasys_module_table_type record;
246 oasys_ar_data(abfd) =ar;
248 ar->module_count = header.mod_count;
250 bfd_seek(abfd , header.mod_tbl_offset, SEEK_SET);
251 for (i = 0; i < header.mod_count; i++) {
253 bfd_read(&record, 1, sizeof(record), abfd);
254 swap(record.mod_size);
255 swap(record.file_offset);
256 swap(record.mod_name_length);
257 module[i].name = malloc(record.mod_name_length+1);
259 bfd_read(module[i].name, 1, record.mod_name_length +1, abfd);
260 /* SKip some stuff */
261 bfd_seek(abfd, record.dep_count * sizeof(int32_type),
264 module[i].size = record.mod_size;
265 module[i].pos = record.file_offset;
274 oasys_object_p (abfd)
277 oasys_data_type *oasys;
278 oasys_data_type static_data;
283 boolean had_usefull = false;
285 memset((PTR)static_data.sections, 0xff, sizeof(static_data.sections));
287 /* Point to the start of the file */
288 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
289 static_data.symbol_string_length = 0;
290 /* Inspect the records, but only keep the section info -
291 remember the size of the symbols
294 oasys_record_union_type record;
295 oasys_read_record(abfd, &record);
296 if (record.header.length < sizeof(record.header))
297 return (bfd_target *)NULL;
299 switch ((oasys_record_enum_type)(record.header.type)) {
300 case oasys_record_is_header_enum:
303 case oasys_record_is_symbol_enum:
304 case oasys_record_is_local_enum:
305 /* Count symbols and remember their size for a future malloc */
307 static_data.symbol_string_length += 1 + oasys_string_length(&record);
310 case oasys_record_is_section_enum:
314 unsigned int section_number;
315 if (record.section.header.length != sizeof(record.section))
317 return (bfd_target *)NULL;
320 section_number= record.section.relb & RELOCATION_SECT_BITS;
321 sprintf(buffer,"%u", section_number);
322 s = bfd_make_section(abfd,buffer);
323 static_data.sections[section_number] = s;
324 switch (record.section.relb & RELOCATION_TYPE_BITS) {
325 case RELOCATION_TYPE_ABS:
326 case RELOCATION_TYPE_REL:
328 case RELOCATION_TYPE_UND:
329 case RELOCATION_TYPE_COM:
334 s->size = bfd_h_getlong(abfd, & record.section.value) ;
335 s->vma = bfd_h_getlong(abfd, &record.section.vma);
336 s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
340 case oasys_record_is_data_enum:
341 static_data.first_data_record = bfd_tell(abfd) - record.header.length;
342 case oasys_record_is_debug_enum:
343 case oasys_record_is_module_enum:
344 case oasys_record_is_named_section_enum:
345 case oasys_record_is_end_enum:
346 if (had_usefull == false) return (bfd_target *)NULL;
350 return (bfd_target *)NULL;
353 oasys_data(abfd) = (oasys_data_type
354 *)malloc(sizeof(oasys_data_type));
355 oasys = oasys_data(abfd);
356 * oasys = static_data;
358 oasys->symbols = (asymbol *)NULL;
360 Oasys support several architectures, but I can't see a simple way
361 to discover which one is in a particular file - we'll guess
363 abfd->obj_arch = bfd_arch_m68k;
364 abfd->obj_machine =0;
365 if (abfd->symcount != 0) {
366 abfd->flags |= HAS_SYMS;
373 oasys_print_symbol(ignore_abfd, file, symbol, how)
377 bfd_print_symbol_enum_type how;
380 case bfd_print_symbol_name_enum:
381 case bfd_print_symbol_type_enum:
382 fprintf(file,"%s", symbol->name);
384 case bfd_print_symbol_all_enum:
386 char *section_name = symbol->section == (asection *)NULL ?
387 "*abs" : symbol->section->name;
389 bfd_print_symbol_vandf((void *)file,symbol);
391 fprintf(file," %-5s %s",
399 The howto table is build using the top two bits of a reloc byte to
400 index into it. The bits are PCREL,WORD/LONG
402 static reloc_howto_type howto_table[]=
404 /* T rs size bsz pcrel bitpos abs ovr sf name partial inplace mask */
406 { 0, 0, 1, 16, false,0, true,true,0,"abs16",true,0x0000ffff},
407 { 0, 0, 2, 32, false,0, true,true,0,"abs32",true,0xffffffff},
408 { 0, 0, 1, 16, true,0, true,true,0,"pcrel16",true,0x0000ffff},
409 { 0, 0, 2, 32, true,0, true,true,0,"pcrel32",true,0xffffffff}
412 /* Read in all the section data and relocation stuff too */
413 static boolean oasys_slurp_section_data(abfd)
416 oasys_record_union_type record;
417 oasys_data_type *data = oasys_data(abfd);
420 oasys_per_section_type *per ;
424 /* Buy enough memory for all the section data and relocations */
425 for (s = abfd->sections; s != (asection *)NULL; s= s->next) {
426 per = oasys_per_section(s);
427 if (per->data != (bfd_byte*)NULL) return true;
428 per->data = (bfd_byte *) malloc(s->size);
429 obstack_init(&per->reloc_obstack);
430 per->reloc_tail_ptr = (oasys_reloc_type **)&(s->relocation);
433 bfd_seek(abfd, data->first_data_record, SEEK_SET);
435 oasys_read_record(abfd, &record);
436 switch (record.header.type) {
437 case oasys_record_is_header_enum:
439 case oasys_record_is_data_enum:
442 uint8e_type *src = record.data.data;
443 uint8e_type *end_src = ((uint8e_type *)&record) + record.header.length;
446 bfd_byte *dst_base_ptr ;
450 bfd_vma dst_offset = bfd_h_getlong(abfd, record.data.addr);
451 section = data->sections[record.data.relb & RELOCATION_SECT_BITS];
452 per = oasys_per_section(section);
453 dst_base_ptr = dst_ptr = oasys_per_section(section)->data + dst_offset;
455 while (src < end_src) {
456 uint8e_type mod_byte = *src++;
459 for (relbit = 1; count-- != 0; relbit <<=1)
461 if (relbit & mod_byte)
463 uint8e_type reloc = *src;
464 /* This item needs to be relocated */
465 switch (reloc & RELOCATION_TYPE_BITS) {
466 case RELOCATION_TYPE_ABS:
470 case RELOCATION_TYPE_REL:
472 /* Relocate the item relative to the section */
473 oasys_reloc_type *r =
475 obstack_alloc(&per->reloc_obstack,
476 sizeof(oasys_reloc_type));
477 *(per->reloc_tail_ptr) = r;
478 per->reloc_tail_ptr = &r->next;
479 r->next= (oasys_reloc_type *)NULL;
480 /* Reference to undefined symbol */
482 /* There is no symbol */
484 /* Work out the howto */
486 data->sections[reloc & RELOCATION_SECT_BITS];
487 r->relent.addend = 0;
488 r->relent.address = dst_ptr - dst_base_ptr;
489 r->relent.howto = &howto_table[reloc>>6];
490 section->reloc_count++;
496 case RELOCATION_TYPE_UND:
498 oasys_reloc_type *r =
500 obstack_alloc(&per->reloc_obstack,
501 sizeof(oasys_reloc_type));
502 *(per->reloc_tail_ptr) = r;
503 per->reloc_tail_ptr = &r->next;
504 r->next= (oasys_reloc_type *)NULL;
505 /* Reference to undefined symbol */
507 /* Get symbol number */
508 r->symbol = (src[0]<<8) | src[1];
509 /* Work out the howto */
510 r->relent.section = (asection *)NULL;
511 r->relent.addend = 0;
512 r->relent.address = dst_ptr - dst_base_ptr;
513 r->relent.howto = &howto_table[reloc>>6];
515 section->reloc_count++;
519 case RELOCATION_TYPE_COM:
528 case oasys_record_is_local_enum:
529 case oasys_record_is_symbol_enum:
530 case oasys_record_is_section_enum:
545 oasys_new_section_hook (abfd, newsect)
549 newsect->used_by_bfd = (oasys_per_section_type *)
550 malloc(sizeof(oasys_per_section_type));
551 oasys_per_section( newsect)->data = (bfd_byte *)NULL;
552 oasys_per_section(newsect)->section = newsect;
553 oasys_per_section(newsect)->offset = 0;
559 oasys_get_reloc_upper_bound (abfd, asect)
563 oasys_slurp_section_data(abfd);
564 return (asect->reloc_count+1) * sizeof(arelent *);
568 oasys_get_section_contents (abfd, section, location, offset, count)
575 oasys_per_section_type *p = section->used_by_bfd;
576 oasys_slurp_section_data(abfd);
577 (void) memcpy(location, p->data + offset, count);
583 oasys_canonicalize_reloc (abfd, section, relptr, symbols)
589 oasys_reloc_type *src = (oasys_reloc_type *)(section->relocation);
590 while (src != (oasys_reloc_type *)NULL) {
591 if (src->relent.section == (asection *)NULL) {
592 src->relent.sym_ptr_ptr = symbols + src->symbol;
594 *relptr ++ = &src->relent;
597 *relptr = (arelent *)NULL;
598 return section->reloc_count;
602 oasys_set_arch_mach (abfd, arch, machine)
604 enum bfd_architecture arch;
605 unsigned long machine;
607 abfd->obj_arch = arch;
608 abfd->obj_machine = machine;
616 oasys_data_type *oasys =
617 (oasys_data_type *) malloc(sizeof(oasys_data_type));
618 oasys_data(abfd) = oasys;
619 if (oasys == (oasys_data_type *)NULL) {
620 bfd_error = no_memory;
634 init_for_output(abfd)
638 for (s = abfd->sections; s != (asection *)NULL; s = s->next) {
640 oasys_per_section(s)->data = (bfd_byte *)(malloc(s->size));
645 /** exec and core file sections */
647 /* set section contents is complicated with OASYS since the format is
648 * not a byte image, but a record stream.
651 oasys_set_section_contents (abfd, section, location, offset, count)
654 unsigned char *location;
658 if (oasys_per_section(section)->data == (bfd_byte *)NULL) {
659 init_for_output(abfd);
661 (void) memcpy(oasys_per_section(section)->data + offset, location, count);
668 /* Native-level interface to symbols. */
670 /* We read the symbols into a buffer, which is discarded when this
671 function exits. We read the strings into a buffer large enough to
672 hold them all plus all the cached symbol entries. */
675 oasys_make_empty_symbol (abfd)
679 oasys_symbol_type *new =
680 (oasys_symbol_type *)zalloc (sizeof (oasys_symbol_type));
681 new->symbol.the_bfd = abfd;
687 oasys_reclaim_symbol_table (abfd)
693 if (!bfd_get_symcount (abfd)) return;
695 for (section = abfd->sections; section != NULL; section = section->next)
696 if (section->relocation) {
697 free ((void *)section->relocation);
698 section->relocation = NULL;
699 section->reloc_count = 0;
702 bfd_get_symcount (abfd) = 0;
703 free ((void *)obj_aout_symbols (abfd));
704 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
711 /* Obsbolete procedural interface; better to look at the cache directly */
713 /* User should have checked the file flags; perhaps we should return
714 BFD_NO_MORE_SYMBOLS if there are none? */
717 oasys_get_symcount_upper_bound (abfd)
721 /* In case we're doing an output file or something...? */
722 if (bfd_get_symcount (abfd)) return bfd_get_symcount (abfd);
724 return (exec_hdr (abfd)->a_syms) / (sizeof (struct nlist));
729 oasys_get_first_symbol (ignore_abfd)
736 oasys_get_next_symbol (abfd, oidx)
741 if (oidx == BFD_NO_MORE_SYMBOLS) return BFD_NO_MORE_SYMBOLS;
742 return ++oidx >= bfd_get_symcount (abfd) ? BFD_NO_MORE_SYMBOLS :
748 oasys_symbol_name (abfd, idx)
753 return (obj_aout_symbols (abfd) + idx)->symbol.name;
758 oasys_symbol_value (abfd, idx)
763 return (obj_aout_symbols (abfd) + idx)->symbol.value;
768 oasys_classify_symbol (abfd, idx)
773 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
775 if ((sym->symbol.flags & BSF_FORT_COMM) != 0) return bfd_symclass_fcommon;
776 if ((sym->symbol.flags & BSF_GLOBAL) != 0) return bfd_symclass_global;
777 if ((sym->symbol.flags & BSF_DEBUGGING) != 0) return bfd_symclass_debugger;
778 if ((sym->symbol.flags & BSF_UNDEFINED) != 0) return bfd_symclass_undefined;
780 return bfd_symclass_unknown;
784 oasys_symbol_hasclass (abfd, idx, class)
790 aout_symbol_type *sym = obj_aout_symbols (abfd) + idx;
792 case bfd_symclass_fcommon:
793 return (sym->symbol.flags & BSF_FORT_COMM) ? true :false;
794 case bfd_symclass_global:
795 return (sym->symbol.flags & BSF_GLOBAL) ? true:false;
796 case bfd_symclass_debugger:
797 return (sym->symbol.flags & BSF_DEBUGGING) ? true:false;;
798 case bfd_symclass_undefined:
799 return (sym->symbol.flags & BSF_UNDEFINED) ? true:false;;
800 default: return false;
807 oasys_reclaim_reloc (ignore_abfd, section)
812 if (section->relocation) {
813 free (section->relocation);
814 section->relocation = NULL;
815 section->reloc_count = 0;
821 oasys_close_and_cleanup (abfd)
824 if (bfd_read_p (abfd) == false)
825 switch (abfd->format) {
827 if (!_bfd_write_archive_contents (abfd)) {
832 /* if (!oasys_write_object_contents (abfd)) */{
837 bfd_error = invalid_operation;
842 if (oasys_data(abfd) != (oasys_data_type *)NULL) {
843 /* FIXME MORE LEAKS */
851 oasys_openr_next_archived_file(arch, prev)
855 oasys_ar_data_type *ar = oasys_ar_data(arch);
856 oasys_module_info_type *p;
857 /* take the next one from the arch state, or reset */
858 if (prev == (bfd *)NULL) {
859 /* Reset the index - the first two entries are bogus*/
860 ar->module_index = 0;
863 p = ar->module + ar->module_index;
866 if (ar->module_index <= ar->module_count) {
867 if (p->abfd == (bfd *)NULL) {
868 p->abfd = _bfd_create_empty_archive_element_shell(arch);
869 p->abfd->origin = p->pos;
870 p->abfd->filename = p->name;
872 /* Fixup a pointer to this element for the member */
873 p->abfd->arelt_data = (void *)p;
878 bfd_error = no_more_archived_files;
884 oasys_find_nearest_line(abfd,
896 char **functionname_ptr;
897 unsigned int *line_ptr;
904 oasys_stat_arch_elt(abfd, buf)
908 oasys_module_info_type *mod = abfd->arelt_data;
909 if (mod == (oasys_module_info_type *)NULL) {
910 bfd_error = invalid_operation;
914 buf->st_size = mod->size;
924 bfd_target oasys_vec =
927 bfd_target_oasys_flavour_enum,
928 true, /* target byte order */
929 true, /* target headers byte order */
930 (HAS_RELOC | EXEC_P | /* object flags */
931 HAS_LINENO | HAS_DEBUG |
932 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
933 (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
934 |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
935 0, /* valid reloc types */
936 ' ', /* ar_pad_char */
937 16, /* ar_max_namelen */
938 oasys_close_and_cleanup, /* _close_and_cleanup */
939 oasys_set_section_contents, /* bfd_set_section_contents */
940 oasys_get_section_contents,
941 oasys_new_section_hook, /* new_section_hook */
942 0, /* _core_file_failing_command */
943 0, /* _core_file_failing_signal */
944 0, /* _core_file_matches_ex...p */
946 0, /* bfd_slurp_bsd_armap, bfd_slurp_armap */
947 bfd_true, /* bfd_slurp_extended_name_table */
948 bfd_bsd_truncate_arname, /* bfd_truncate_arname */
950 oasys_get_symtab_upper_bound, /* get_symtab_upper_bound */
951 oasys_get_symtab, /* canonicalize_symtab */
952 0, /* oasys_reclaim_symbol_table, bfd_reclaim_symbol_table */
953 oasys_get_reloc_upper_bound, /* get_reloc_upper_bound */
954 oasys_canonicalize_reloc, /* bfd_canonicalize_reloc */
955 0, /* oasys_reclaim_reloc, bfd_reclaim_reloc */
956 0, /* oasys_get_symcount_upper_bound, bfd_get_symcount_upper_bound */
957 0, /* oasys_get_first_symbol, bfd_get_first_symbol */
958 0, /* oasys_get_next_symbol, bfd_get_next_symbol */
959 0, /* oasys_classify_symbol, bfd_classify_symbol */
960 0, /* oasys_symbol_hasclass, bfd_symbol_hasclass */
961 0, /* oasys_symbol_name, bfd_symbol_name */
962 0, /* oasys_symbol_value, bfd_symbol_value */
964 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* data */
965 _do_getblong, _do_putblong, _do_getbshort, _do_putbshort, /* hdrs */
968 oasys_object_p, /* bfd_check_format */
975 _bfd_generic_mkarchive,
978 oasys_make_empty_symbol,
980 bfd_false, /* oasys_get_lineno,*/
981 oasys_set_arch_mach, /* bfd_set_arch_mach,*/
983 oasys_openr_next_archived_file,
984 oasys_find_nearest_line, /* bfd_find_nearest_line */
985 oasys_stat_arch_elt, /* bfd_stat_arch_elt */