1 /* BFD back-end for VERSAdos-E objects.
2 Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 VERSAdos-E relocateable object file format
29 This module supports reading of VERSAdos relocateable
32 A VERSAdos file looks like contains
34 o Indentification Record
35 o External Symbol Definition Record
44 #include "libiberty.h"
46 static boolean versados_mkobject PARAMS ((bfd *));
47 static boolean versados_scan PARAMS ((bfd *));
48 static const bfd_target *versados_object_p PARAMS ((bfd *));
55 #define ES_BASE 17 /* first symbol has esdid 17 */
57 /* Per file target dependent information */
59 /* one for each section */
62 asection *section; /* ptr to bfd version */
63 unsigned char *contents; /* used to build image */
65 int relocs; /* reloc count, valid end of pass 1 */
66 int donerel; /* have relocs been translated */
69 typedef struct versados_data_struct
71 int es_done; /* count of symbol index, starts at ES_BASE */
72 asymbol *symbols; /* pointer to local symbols */
73 char *strings; /* strings of all the above */
74 int stringlen; /* len of string table (valid end of pass1) */
75 int nsecsyms; /* number of sections */
77 int ndefs; /* number of exported symbols (they dont get esdids) */
78 int nrefs; /* number of imported symbols (valid end of pass1) */
80 int ref_idx; /* current processed value of the above */
85 struct esdid e[16]; /* per section info */
86 int alert; /* to see if we're trampling */
87 asymbol *rest[256 - 16]; /* per symbol info */
92 #define VDATA(abfd) (abfd->tdata.versados_data)
93 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
94 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
100 unsigned char map[4];
102 unsigned char data[200];
108 char type; /* record type */
109 char name[10]; /* module name */
110 char rev; /* module rev number */
126 unsigned char esd_entries[1];
130 #define ESD_STD_REL_SEC 2
131 #define ESD_SHRT_REL_SEC 3
132 #define ESD_XDEF_IN_SEC 4
133 #define ESD_XREF_SYM 7
134 #define ESD_XREF_SEC 6
135 #define ESD_XDEF_IN_ABS 5
139 struct ext_vheader header;
144 /* Initialize by filling in the hex conversion array. */
146 /* Set up the tdata information. */
149 versados_mkobject (abfd)
152 if (abfd->tdata.versados_data == NULL)
154 tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
157 abfd->tdata.versados_data = tdata;
158 tdata->symbols = NULL;
159 VDATA (abfd)->alert = 0x12345678;
162 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
166 /* Report a problem in an S record file. FIXME: This probably should
167 not call fprintf, but we really do need some mechanism for printing
171 versados_new_symbol (abfd, snum, name, val, sec)
178 asymbol *n = VDATA (abfd)->symbols + snum;
188 get_record (abfd, ptr)
192 bfd_read (&ptr->size, 1, 1, abfd);
193 if (bfd_read ((char *) ptr + 1, 1, ptr->size, abfd) != ptr->size)
202 unsigned char *p = *pp;
204 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
212 char *p = (char *) *pp;
225 new_symbol_string (abfd, name)
229 char *n = VDATA (abfd)->strings;
230 strcpy (VDATA (abfd)->strings, name);
231 VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1;
236 process_esd (abfd, esd, pass)
241 /* Read through the ext def for the est entries */
242 int togo = esd->size - 2;
247 unsigned char *ptr = esd->esd_entries;
248 unsigned char *end = ptr + togo;
251 int scn = *ptr & 0xf;
252 int typ = (*ptr >> 4) & 0xf;
254 /* Declare this section */
255 sprintf (name, "%d", scn);
256 sec = bfd_make_section_old_way (abfd, strdup (name));
257 sec->target_index = scn;
258 EDATA (abfd, scn).section = sec;
267 int snum = VDATA (abfd)->ref_idx++;
271 VDATA (abfd)->stringlen += strlen (name) + 1;
277 char *n = new_symbol_string (abfd, name);
278 s = versados_new_symbol (abfd, snum, n, 0,
279 &bfd_und_section, scn);
280 esidx = VDATA (abfd)->es_done++;
281 RDATA (abfd, esidx - ES_BASE) = s;
288 start = get_4 (&ptr);
290 case ESD_STD_REL_SEC:
291 case ESD_SHRT_REL_SEC:
293 sec->_raw_size = get_4 (&ptr);
294 sec->flags |= SEC_ALLOC;
297 case ESD_XDEF_IN_ABS:
298 sec = (asection *) & bfd_abs_section;
299 case ESD_XDEF_IN_SEC:
301 int snum = VDATA (abfd)->def_idx++;
307 /* Just remember the symbol */
308 VDATA (abfd)->stringlen += strlen (name) + 1;
313 char *n = new_symbol_string (abfd, name);
314 s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n, val, sec, scn);
315 s->flags |= BSF_GLOBAL;
325 #define R_RELWORD_NEG 3
326 #define R_RELLONG_NEG 4
328 reloc_howto_type versados_howto_table[] =
330 HOWTO (R_RELWORD, 0, 1, 16, false,
331 0, complain_overflow_dont, 0,
332 "+v16", true, 0x0000ffff, 0x0000ffff, false),
333 HOWTO (R_RELLONG, 0, 2, 32, false,
334 0, complain_overflow_dont, 0,
335 "+v32", true, 0xffffffff, 0xffffffff, false),
337 HOWTO (R_RELWORD_NEG, 0, -1, 16, false,
338 0, complain_overflow_dont, 0,
339 "-v16", true, 0x0000ffff, 0x0000ffff, false),
340 HOWTO (R_RELLONG_NEG, 0, -2, 32, false,
341 0, complain_overflow_dont, 0,
342 "-v32", true, 0xffffffff, 0xffffffff, false),
346 get_offset (len, ptr)
357 for (i = 1; i < len; i++)
358 val = (val << 8) | *ptr++;
365 process_otr (abfd, otr, pass)
371 unsigned char *srcp = otr->data;
372 unsigned char *endp = (unsigned char *) otr + otr->size;
373 unsigned int bits = (otr->map[0] << 24)
374 | (otr->map[1] << 16)
376 | (otr->map[3] << 0);
378 struct esdid *esdid = &EDATA (abfd, otr->esdid - 1);
379 unsigned char *contents = esdid->contents;
380 int need_contents = 0;
381 unsigned int dst_idx = esdid->pc;
383 for (shift = (1 << 31); shift && srcp < endp; shift >>= 1)
388 int esdids = (flag >> 5) & 0x7;
389 int sizeinwords = ((flag >> 3) & 1) ? 2 : 1;
390 int offsetlen = flag & 0x7;
395 /* A zero esdid means the new pc is the offset given */
396 dst_idx += get_offset (offsetlen, srcp);
401 int val = get_offset (offsetlen, srcp + esdids);
405 for (j = 0; j < sizeinwords * 2; j++)
407 contents[dst_idx + (sizeinwords * 2) - j - 1] = val;
411 for (j = 0; j < esdids; j++)
417 int rn = EDATA (abfd, otr->esdid - 1).relocs++;
420 /* this is the first pass over the data,
421 just remember that we need a reloc */
426 EDATA (abfd, otr->esdid - 1).section->relocation + rn;
427 n->address = dst_idx;
429 n->sym_ptr_ptr = (asymbol **) esdid;
431 n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1);
436 dst_idx += sizeinwords * 2;
442 if (dst_idx < esdid->section->_raw_size)
445 /* absolute code, comes in 16 bit lumps */
446 contents[dst_idx] = srcp[0];
447 contents[dst_idx + 1] = srcp[1];
453 EDATA (abfd, otr->esdid - 1).pc = dst_idx;
455 if (!contents && need_contents)
456 esdid->contents = (unsigned char *) bfd_alloc (abfd, esdid->section->_raw_size);
469 VDATA (abfd)->stringlen = 0;
470 VDATA (abfd)->nrefs = 0;
471 VDATA (abfd)->ndefs = 0;
472 VDATA (abfd)->ref_idx = 0;
473 VDATA (abfd)->def_idx = 0;
474 VDATA (abfd)->pass_2_done = 0;
479 if (!get_record (abfd, &any))
481 switch (any.header.type)
489 process_esd (abfd, &any.esd, 1);
492 process_otr (abfd, &any.otr, 1);
497 /* Now allocate space for the relocs and sections */
499 VDATA (abfd)->nrefs = VDATA (abfd)->ref_idx;
500 VDATA (abfd)->ndefs = VDATA (abfd)->def_idx;
501 VDATA (abfd)->ref_idx = 0;
502 VDATA (abfd)->def_idx = 0;
504 abfd->symcount = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs;
506 for (i = 0; i < 16; i++)
508 struct esdid *esdid = &EDATA (abfd, i);
511 esdid->section->relocation
512 = (arelent *) bfd_alloc (abfd, sizeof (arelent) * esdid->relocs);
517 esdid->section->flags |= SEC_HAS_CONTENTS | SEC_LOAD;
519 esdid->section->reloc_count = esdid->relocs;
521 esdid->section->flags |= SEC_RELOC;
525 /* Add an entry into the symbol table for it */
527 VDATA (abfd)->stringlen += strlen (esdid->section->name) + 1;
531 abfd->symcount += nsecs;
533 VDATA (abfd)->symbols = (asymbol *) bfd_alloc (abfd,
534 sizeof (asymbol) * (abfd->symcount));
536 VDATA (abfd)->strings = bfd_alloc (abfd, VDATA (abfd)->stringlen);
538 if ((VDATA (abfd)->symbols == NULL && abfd->symcount > 0)
539 || (VDATA (abfd)->strings == NULL && VDATA (abfd)->stringlen > 0))
542 /* Actually fill in the section symbols,
543 we stick them at the end of the table */
545 for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++)
547 struct esdid *esdid = &EDATA (abfd, i);
548 asection *sec = esdid->section;
551 asymbol *s = VDATA (abfd)->symbols + j;
552 s->name = new_symbol_string (abfd, sec->name);
554 s->flags = BSF_LOCAL;
561 abfd->flags |= HAS_SYMS;
563 /* Set this to nsecs - since we've already planted the section
565 VDATA (abfd)->nsecsyms = nsecs;
567 VDATA (abfd)->ref_idx = 0;
572 /* Check whether an existing file is a versados file. */
574 static const bfd_target *
575 versados_object_p (abfd)
578 struct ext_vheader ext;
581 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
584 if (bfd_read (&len, 1, 1, abfd) != 1)
586 if (bfd_get_error () != bfd_error_system_call)
587 bfd_set_error (bfd_error_wrong_format);
591 if (bfd_read (&ext.type, 1, len, abfd) != len)
593 if (bfd_get_error () != bfd_error_system_call)
594 bfd_set_error (bfd_error_wrong_format);
598 /* We guess that the language field will never be larger than 10.
599 In sample files, it is always either 0 or 1. Checking for this
600 prevents confusion with Intel Hex files. */
601 if (ext.type != VHEADER
604 bfd_set_error (bfd_error_wrong_format);
608 /* OK, looks like a record, build the tdata and read in. */
610 if (!versados_mkobject (abfd)
611 || !versados_scan (abfd))
618 versados_pass_2 (abfd)
623 if (VDATA (abfd)->pass_2_done)
626 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
629 VDATA (abfd)->es_done = ES_BASE;
631 /* read records till we get to where we want to be */
635 get_record (abfd, &any);
636 switch (any.header.type)
639 VDATA (abfd)->pass_2_done = 1;
642 process_esd (abfd, &any.esd, 2);
645 process_otr (abfd, &any.otr, 2);
652 versados_get_section_contents (abfd, section, location, offset, count)
659 if (!versados_pass_2 (abfd))
663 EDATA (abfd, section->target_index).contents + offset,
669 #define versados_get_section_contents_in_window \
670 _bfd_generic_get_section_contents_in_window
673 versados_set_section_contents (abfd, section, location, offset, bytes_to_do)
674 bfd *abfd ATTRIBUTE_UNUSED;
675 sec_ptr section ATTRIBUTE_UNUSED;
676 PTR location ATTRIBUTE_UNUSED;
677 file_ptr offset ATTRIBUTE_UNUSED;
678 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
684 versados_sizeof_headers (abfd, exec)
685 bfd *abfd ATTRIBUTE_UNUSED;
686 boolean exec ATTRIBUTE_UNUSED;
692 versados_make_empty_symbol (abfd)
695 asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
701 /* Return the amount of memory needed to read the symbol table. */
704 versados_get_symtab_upper_bound (abfd)
707 return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
710 /* Return the symbol table. */
713 versados_get_symtab (abfd, alocation)
717 unsigned int symcount = bfd_get_symcount (abfd);
721 versados_pass_2 (abfd);
723 for (i = 0, s = VDATA (abfd)->symbols;
736 versados_get_symbol_info (ignore_abfd, symbol, ret)
737 bfd *ignore_abfd ATTRIBUTE_UNUSED;
741 bfd_symbol_info (symbol, ret);
745 versados_print_symbol (ignore_abfd, afile, symbol, how)
746 bfd *ignore_abfd ATTRIBUTE_UNUSED;
749 bfd_print_symbol_type how;
751 FILE *file = (FILE *) afile;
754 case bfd_print_symbol_name:
755 fprintf (file, "%s", symbol->name);
758 bfd_print_symbol_vandf ((PTR) file, symbol);
759 fprintf (file, " %-5s %s",
760 symbol->section->name,
767 versados_get_reloc_upper_bound (abfd, asect)
768 bfd *abfd ATTRIBUTE_UNUSED;
771 return (asect->reloc_count + 1) * sizeof (arelent *);
775 versados_canonicalize_reloc (abfd, section, relptr, symbols)
784 versados_pass_2 (abfd);
785 src = section->relocation;
786 if (!EDATA (abfd, section->target_index).donerel)
788 EDATA (abfd, section->target_index).donerel = 1;
789 /* translate from indexes to symptr ptrs */
790 for (count = 0; count < section->reloc_count; count++)
792 int esdid = (int) src[count].sym_ptr_ptr;
796 src[count].sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
798 else if (esdid < ES_BASE) /* Section relative thing */
800 struct esdid *e = &EDATA (abfd, esdid - 1);
803 /** relocation relative to section which was
806 src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr;
810 src[count].sym_ptr_ptr = symbols + esdid - ES_BASE;
816 for (count = 0; count < section->reloc_count; count++)
821 return section->reloc_count;
824 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
825 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
826 #define versados_new_section_hook _bfd_generic_new_section_hook
828 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
829 #define versados_get_lineno _bfd_nosymbols_get_lineno
830 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
831 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
832 #define versados_read_minisymbols _bfd_generic_read_minisymbols
833 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
835 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
837 #define versados_set_arch_mach bfd_default_set_arch_mach
839 #define versados_bfd_get_relocated_section_contents \
840 bfd_generic_get_relocated_section_contents
841 #define versados_bfd_relax_section bfd_generic_relax_section
842 #define versados_bfd_gc_sections bfd_generic_gc_sections
843 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
844 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
845 #define versados_bfd_final_link _bfd_generic_final_link
846 #define versados_bfd_link_split_section _bfd_generic_link_split_section
848 const bfd_target versados_vec =
850 "versados", /* name */
851 bfd_target_versados_flavour,
852 BFD_ENDIAN_BIG, /* target byte order */
853 BFD_ENDIAN_BIG, /* target headers byte order */
854 (HAS_RELOC | EXEC_P | /* object flags */
855 HAS_LINENO | HAS_DEBUG |
856 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
857 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
858 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
859 0, /* leading underscore */
860 ' ', /* ar_pad_char */
861 16, /* ar_max_namelen */
862 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
863 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
864 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
865 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
866 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
867 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
871 versados_object_p, /* bfd_check_format */
878 _bfd_generic_mkarchive,
881 { /* bfd_write_contents */
884 _bfd_write_archive_contents,
888 BFD_JUMP_TABLE_GENERIC (versados),
889 BFD_JUMP_TABLE_COPY (_bfd_generic),
890 BFD_JUMP_TABLE_CORE (_bfd_nocore),
891 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
892 BFD_JUMP_TABLE_SYMBOLS (versados),
893 BFD_JUMP_TABLE_RELOCS (versados),
894 BFD_JUMP_TABLE_WRITE (versados),
895 BFD_JUMP_TABLE_LINK (versados),
896 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),