1 /* BFD back-end for VERSAdos-E objects.
2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
5 Versados is a Motorola trademark.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
26 VERSAdos-E relocatable object file format
30 This module supports reading of VERSAdos relocatable
33 A VERSAdos file looks like contains
35 o Identification Record
36 o External Symbol Definition Record
43 #include "libiberty.h"
51 #define ES_BASE 17 /* First symbol has esdid 17. */
53 /* Per file target dependent information. */
55 /* One for each section. */
58 asection *section; /* Ptr to bfd version. */
59 unsigned char *contents; /* Used to build image. */
61 int relocs; /* Reloc count, valid end of pass 1. */
62 int donerel; /* Have relocs been translated. */
65 typedef struct versados_data_struct
67 int es_done; /* Count of symbol index, starts at ES_BASE. */
68 asymbol *symbols; /* Pointer to local symbols. */
69 char *strings; /* Strings of all the above. */
70 int stringlen; /* Len of string table (valid end of pass1). */
71 int nsecsyms; /* Number of sections. */
73 int ndefs; /* Number of exported symbols (they dont get esdids). */
74 int nrefs; /* Number of imported symbols (valid end of pass1). */
76 int ref_idx; /* Current processed value of the above. */
81 struct esdid e[16]; /* Per section info. */
82 int alert; /* To see if we're trampling. */
83 asymbol *rest[256 - 16]; /* Per symbol info. */
87 #define VDATA(abfd) (abfd->tdata.versados_data)
88 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
89 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
97 unsigned char data[200];
103 char type; /* Record type. */
104 char name[10]; /* Module name. */
105 char rev; /* Module rev number. */
121 unsigned char esd_entries[1];
126 #define ESD_STD_REL_SEC 2
127 #define ESD_SHRT_REL_SEC 3
128 #define ESD_XDEF_IN_SEC 4
129 #define ESD_XDEF_IN_ABS 5
130 #define ESD_XREF_SEC 6
131 #define ESD_XREF_SYM 7
136 struct ext_vheader header;
141 /* Initialize by filling in the hex conversion array. */
143 /* Set up the tdata information. */
146 versados_mkobject (bfd *abfd)
148 if (abfd->tdata.versados_data == NULL)
150 bfd_size_type amt = sizeof (tdata_type);
151 tdata_type *tdata = bfd_alloc (abfd, amt);
155 abfd->tdata.versados_data = tdata;
156 tdata->symbols = NULL;
157 VDATA (abfd)->alert = 0x12345678;
160 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
164 /* Report a problem in an S record file. FIXME: This probably should
165 not call fprintf, but we really do need some mechanism for printing
169 versados_new_symbol (bfd *abfd,
175 asymbol *n = VDATA (abfd)->symbols + snum;
185 get_record (bfd *abfd, union ext_any *ptr)
187 if (bfd_bread (&ptr->size, (bfd_size_type) 1, abfd) != 1
188 || (bfd_bread ((char *) ptr + 1, (bfd_size_type) ptr->size, abfd)
195 get_4 (unsigned char **pp)
197 unsigned char *p = *pp;
200 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
204 get_10 (unsigned char **pp, char *name)
206 char *p = (char *) *pp;
210 while (*p != ' ' && len)
219 new_symbol_string (bfd *abfd, const char *name)
221 char *n = VDATA (abfd)->strings;
223 strcpy (VDATA (abfd)->strings, name);
224 VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1;
229 process_esd (bfd *abfd, struct ext_esd *esd, int pass)
231 /* Read through the ext def for the est entries. */
232 int togo = esd->size - 2;
237 unsigned char *ptr = esd->esd_entries;
238 unsigned char *end = ptr + togo;
242 int scn = *ptr & 0xf;
243 int typ = (*ptr >> 4) & 0xf;
245 /* Declare this section. */
246 sprintf (name, "%d", scn);
247 sec = bfd_make_section_old_way (abfd, strdup (name));
248 sec->target_index = scn;
249 EDATA (abfd, scn).section = sec;
259 int snum = VDATA (abfd)->ref_idx++;
262 VDATA (abfd)->stringlen += strlen (name) + 1;
267 char *n = new_symbol_string (abfd, name);
269 s = versados_new_symbol (abfd, snum, n, (bfd_vma) 0,
270 bfd_und_section_ptr);
271 esidx = VDATA (abfd)->es_done++;
272 RDATA (abfd, esidx - ES_BASE) = s;
280 start = get_4 (&ptr);
283 case ESD_STD_REL_SEC:
284 case ESD_SHRT_REL_SEC:
285 sec->size = get_4 (&ptr);
286 sec->flags |= SEC_ALLOC;
288 case ESD_XDEF_IN_ABS:
289 sec = bfd_abs_section_ptr;
290 case ESD_XDEF_IN_SEC:
292 int snum = VDATA (abfd)->def_idx++;
298 /* Just remember the symbol. */
299 VDATA (abfd)->stringlen += strlen (name) + 1;
303 char *n = new_symbol_string (abfd, name);
305 s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n,
307 s->flags |= BSF_GLOBAL;
317 #define R_RELWORD_NEG 3
318 #define R_RELLONG_NEG 4
320 reloc_howto_type versados_howto_table[] =
322 HOWTO (R_RELWORD, 0, 1, 16, FALSE,
323 0, complain_overflow_dont, 0,
324 "+v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
325 HOWTO (R_RELLONG, 0, 2, 32, FALSE,
326 0, complain_overflow_dont, 0,
327 "+v32", TRUE, 0xffffffff, 0xffffffff, FALSE),
329 HOWTO (R_RELWORD_NEG, 0, -1, 16, FALSE,
330 0, complain_overflow_dont, 0,
331 "-v16", TRUE, 0x0000ffff, 0x0000ffff, FALSE),
332 HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE,
333 0, complain_overflow_dont, 0,
334 "-v32", TRUE, 0xffffffff, 0xffffffff, FALSE),
338 get_offset (int len, unsigned char *ptr)
349 for (i = 1; i < len; i++)
350 val = (val << 8) | *ptr++;
357 process_otr (bfd *abfd, struct ext_otr *otr, int pass)
360 unsigned char *srcp = otr->data;
361 unsigned char *endp = (unsigned char *) otr + otr->size;
362 unsigned int bits = (otr->map[0] << 24)
363 | (otr->map[1] << 16)
365 | (otr->map[3] << 0);
367 struct esdid *esdid = &EDATA (abfd, otr->esdid - 1);
368 unsigned char *contents = esdid->contents;
369 int need_contents = 0;
370 unsigned int dst_idx = esdid->pc;
372 for (shift = ((unsigned long) 1 << 31); shift && srcp < endp; shift >>= 1)
377 int esdids = (flag >> 5) & 0x7;
378 int sizeinwords = ((flag >> 3) & 1) ? 2 : 1;
379 int offsetlen = flag & 0x7;
384 /* A zero esdid means the new pc is the offset given. */
385 dst_idx += get_offset (offsetlen, srcp);
390 int val = get_offset (offsetlen, srcp + esdids);
395 for (j = 0; j < sizeinwords * 2; j++)
397 contents[dst_idx + (sizeinwords * 2) - j - 1] = val;
401 for (j = 0; j < esdids; j++)
407 int rn = EDATA (abfd, otr->esdid - 1).relocs++;
411 /* This is the first pass over the data,
412 just remember that we need a reloc. */
417 EDATA (abfd, otr->esdid - 1).section->relocation + rn;
418 n->address = dst_idx;
420 n->sym_ptr_ptr = (asymbol **) (size_t) id;
422 n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1);
427 dst_idx += sizeinwords * 2;
433 if (dst_idx < esdid->section->size)
436 /* Absolute code, comes in 16 bit lumps. */
437 contents[dst_idx] = srcp[0];
438 contents[dst_idx + 1] = srcp[1];
444 EDATA (abfd, otr->esdid - 1).pc = dst_idx;
446 if (!contents && need_contents)
448 bfd_size_type size = esdid->section->size;
449 esdid->contents = bfd_alloc (abfd, size);
454 versados_scan (bfd *abfd)
462 VDATA (abfd)->stringlen = 0;
463 VDATA (abfd)->nrefs = 0;
464 VDATA (abfd)->ndefs = 0;
465 VDATA (abfd)->ref_idx = 0;
466 VDATA (abfd)->def_idx = 0;
467 VDATA (abfd)->pass_2_done = 0;
473 if (!get_record (abfd, &any))
475 switch (any.header.type)
483 process_esd (abfd, &any.esd, 1);
486 process_otr (abfd, &any.otr, 1);
491 /* Now allocate space for the relocs and sections. */
492 VDATA (abfd)->nrefs = VDATA (abfd)->ref_idx;
493 VDATA (abfd)->ndefs = VDATA (abfd)->def_idx;
494 VDATA (abfd)->ref_idx = 0;
495 VDATA (abfd)->def_idx = 0;
497 abfd->symcount = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs;
499 for (i = 0; i < 16; i++)
501 struct esdid *esdid = &EDATA (abfd, i);
505 amt = (bfd_size_type) esdid->relocs * sizeof (arelent);
506 esdid->section->relocation = bfd_alloc (abfd, amt);
511 esdid->section->flags |= SEC_HAS_CONTENTS | SEC_LOAD;
513 esdid->section->reloc_count = esdid->relocs;
515 esdid->section->flags |= SEC_RELOC;
519 /* Add an entry into the symbol table for it. */
521 VDATA (abfd)->stringlen += strlen (esdid->section->name) + 1;
525 abfd->symcount += nsecs;
527 amt = abfd->symcount;
528 amt *= sizeof (asymbol);
529 VDATA (abfd)->symbols = bfd_alloc (abfd, amt);
531 amt = VDATA (abfd)->stringlen;
532 VDATA (abfd)->strings = bfd_alloc (abfd, amt);
534 if ((VDATA (abfd)->symbols == NULL && abfd->symcount > 0)
535 || (VDATA (abfd)->strings == NULL && VDATA (abfd)->stringlen > 0))
538 /* Actually fill in the section symbols,
539 we stick them at the end of the table. */
540 for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++)
542 struct esdid *esdid = &EDATA (abfd, i);
543 asection *sec = esdid->section;
547 asymbol *s = VDATA (abfd)->symbols + j;
548 s->name = new_symbol_string (abfd, sec->name);
550 s->flags = BSF_LOCAL;
558 abfd->flags |= HAS_SYMS;
560 /* Set this to nsecs - since we've already planted the section
562 VDATA (abfd)->nsecsyms = nsecs;
564 VDATA (abfd)->ref_idx = 0;
569 /* Check whether an existing file is a versados file. */
571 static const bfd_target *
572 versados_object_p (bfd *abfd)
574 struct ext_vheader ext;
576 tdata_type *tdata_save;
578 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
581 if (bfd_bread (&len, (bfd_size_type) 1, abfd) != 1)
583 if (bfd_get_error () != bfd_error_system_call)
584 bfd_set_error (bfd_error_wrong_format);
588 if (bfd_bread (&ext.type, (bfd_size_type) len, abfd) != len)
590 if (bfd_get_error () != bfd_error_system_call)
591 bfd_set_error (bfd_error_wrong_format);
595 /* We guess that the language field will never be larger than 10.
596 In sample files, it is always either 0 or 1. Checking for this
597 prevents confusion with Intel Hex files. */
598 if (ext.type != VHEADER
601 bfd_set_error (bfd_error_wrong_format);
605 /* OK, looks like a record, build the tdata and read in. */
606 tdata_save = abfd->tdata.versados_data;
607 if (!versados_mkobject (abfd) || !versados_scan (abfd))
609 abfd->tdata.versados_data = tdata_save;
617 versados_pass_2 (bfd *abfd)
621 if (VDATA (abfd)->pass_2_done)
624 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
627 VDATA (abfd)->es_done = ES_BASE;
629 /* Read records till we get to where we want to be. */
632 get_record (abfd, &any);
633 switch (any.header.type)
636 VDATA (abfd)->pass_2_done = 1;
639 process_esd (abfd, &any.esd, 2);
642 process_otr (abfd, &any.otr, 2);
649 versados_get_section_contents (bfd *abfd,
655 if (!versados_pass_2 (abfd))
659 EDATA (abfd, section->target_index).contents + offset,
665 #define versados_get_section_contents_in_window \
666 _bfd_generic_get_section_contents_in_window
669 versados_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
670 sec_ptr section ATTRIBUTE_UNUSED,
671 const void * location ATTRIBUTE_UNUSED,
672 file_ptr offset ATTRIBUTE_UNUSED,
673 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED)
679 versados_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
680 struct bfd_link_info *info ATTRIBUTE_UNUSED)
685 /* Return the amount of memory needed to read the symbol table. */
688 versados_get_symtab_upper_bound (bfd *abfd)
690 return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
693 /* Return the symbol table. */
696 versados_canonicalize_symtab (bfd *abfd, asymbol **alocation)
698 unsigned int symcount = bfd_get_symcount (abfd);
702 versados_pass_2 (abfd);
704 for (i = 0, s = VDATA (abfd)->symbols;
715 versados_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
719 bfd_symbol_info (symbol, ret);
723 versados_print_symbol (bfd *abfd,
726 bfd_print_symbol_type how)
728 FILE *file = (FILE *) afile;
732 case bfd_print_symbol_name:
733 fprintf (file, "%s", symbol->name);
736 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
737 fprintf (file, " %-5s %s",
738 symbol->section->name,
744 versados_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
747 return (asect->reloc_count + 1) * sizeof (arelent *);
751 versados_canonicalize_reloc (bfd *abfd,
759 versados_pass_2 (abfd);
760 src = section->relocation;
761 if (!EDATA (abfd, section->target_index).donerel)
763 EDATA (abfd, section->target_index).donerel = 1;
764 /* Translate from indexes to symptr ptrs. */
765 for (count = 0; count < section->reloc_count; count++)
767 int esdid = (int) (size_t) src[count].sym_ptr_ptr;
770 src[count].sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
771 else if (esdid < ES_BASE)
773 /* Section relative thing. */
774 struct esdid *e = &EDATA (abfd, esdid - 1);
776 src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr;
779 src[count].sym_ptr_ptr = symbols + esdid - ES_BASE;
783 for (count = 0; count < section->reloc_count; count++)
787 return section->reloc_count;
790 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
791 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
792 #define versados_new_section_hook _bfd_generic_new_section_hook
793 #define versados_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
794 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
795 #define versados_get_lineno _bfd_nosymbols_get_lineno
796 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
797 #define versados_find_inliner_info _bfd_nosymbols_find_inliner_info
798 #define versados_make_empty_symbol _bfd_generic_make_empty_symbol
799 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
800 #define versados_read_minisymbols _bfd_generic_read_minisymbols
801 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
802 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
803 #define versados_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
804 #define versados_set_arch_mach bfd_default_set_arch_mach
805 #define versados_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
806 #define versados_bfd_relax_section bfd_generic_relax_section
807 #define versados_bfd_gc_sections bfd_generic_gc_sections
808 #define versados_bfd_lookup_section_flags bfd_generic_lookup_section_flags
809 #define versados_bfd_merge_sections bfd_generic_merge_sections
810 #define versados_bfd_is_group_section bfd_generic_is_group_section
811 #define versados_bfd_discard_group bfd_generic_discard_group
812 #define versados_section_already_linked _bfd_generic_section_already_linked
813 #define versados_bfd_define_common_symbol bfd_generic_define_common_symbol
814 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
815 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
816 #define versados_bfd_link_just_syms _bfd_generic_link_just_syms
817 #define versados_bfd_copy_link_hash_symbol_type \
818 _bfd_generic_copy_link_hash_symbol_type
819 #define versados_bfd_final_link _bfd_generic_final_link
820 #define versados_bfd_link_split_section _bfd_generic_link_split_section
822 const bfd_target m68k_versados_vec =
824 "versados", /* Name. */
825 bfd_target_versados_flavour,
826 BFD_ENDIAN_BIG, /* Target byte order. */
827 BFD_ENDIAN_BIG, /* Target headers byte order. */
828 (HAS_RELOC | EXEC_P | /* Object flags. */
829 HAS_LINENO | HAS_DEBUG |
830 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
831 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
832 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
833 0, /* Leading underscore. */
834 ' ', /* AR_pad_char. */
835 16, /* AR_max_namelen. */
836 0, /* match priority. */
837 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
838 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
839 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
840 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
841 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
842 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
846 versados_object_p, /* bfd_check_format. */
853 _bfd_generic_mkarchive,
856 { /* bfd_write_contents. */
859 _bfd_write_archive_contents,
863 BFD_JUMP_TABLE_GENERIC (versados),
864 BFD_JUMP_TABLE_COPY (_bfd_generic),
865 BFD_JUMP_TABLE_CORE (_bfd_nocore),
866 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
867 BFD_JUMP_TABLE_SYMBOLS (versados),
868 BFD_JUMP_TABLE_RELOCS (versados),
869 BFD_JUMP_TABLE_WRITE (versados),
870 BFD_JUMP_TABLE_LINK (versados),
871 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),