1 /* BFD back-end for VERSAdos-E objects.
3 Versados is a Motorola trademark.
5 Copyright 1995 Free Software Foundation, Inc.
6 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 VERSAdos-E relocateable object file format
30 This module supports reading of VERSAdos relocateable
33 A VERSAdos file looks like contains
35 o Indentification Record
36 o External Symbol Definition Record
46 #include "libiberty.h"
49 static boolean versados_mkobject PARAMS ((bfd *));
50 static boolean versados_scan PARAMS ((bfd *));
51 static const bfd_target *versados_object_p PARAMS ((bfd *));
60 #define ES_BASE 17 /* first symbol has esdid 17 */
62 /* Per file target dependent information */
64 /* one for each section */
67 asection *section; /* ptr to bfd version */
68 unsigned char *contents; /* used to build image */
70 int relocs; /* reloc count, valid end of pass 1 */
71 int donerel; /* have relocs been translated */
74 typedef struct versados_data_struct
76 int es_done; /* count of symbol index, starts at ES_BASE */
77 asymbol *symbols; /* pointer to local symbols */
78 char *strings; /* strings of all the above */
79 int stringlen; /* len of string table (valid end of pass1) */
80 int nsecsyms; /* number of sections */
82 int ndefs; /* number of exported symbols (they dont get esdids) */
83 int nrefs; /* number of imported symbols (valid end of pass1) */
85 int ref_idx; /* current processed value of the above */
90 struct esdid e[16]; /* per section info */
91 int alert; /* to see if we're trampling */
92 asymbol *rest[256 - 16]; /* per symbol info */
97 #define VDATA(abfd) (abfd->tdata.versados_data)
98 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
99 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
105 unsigned char map[4];
107 unsigned char data[200];
113 char type; /* record type */
114 char name[10]; /* module name */
115 char rev; /* module rev number */
131 unsigned char esd_entries[1];
135 #define ESD_STD_REL_SEC 2
136 #define ESD_SHRT_REL_SEC 3
137 #define ESD_XDEF_IN_SEC 4
138 #define ESD_XREF_SYM 7
139 #define ESD_XREF_SEC 6
140 #define ESD_XDEF_IN_ABS 5
144 struct ext_vheader header;
149 /* Initialize by filling in the hex conversion array. */
155 /* Set up the tdata information. */
158 versados_mkobject (abfd)
161 if (abfd->tdata.versados_data == NULL)
163 tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
166 bfd_set_error (bfd_error_no_memory);
169 abfd->tdata.versados_data = tdata;
170 tdata->symbols = NULL;
171 VDATA (abfd)->alert = 0x12345678;
174 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
179 /* Report a problem in an S record file. FIXME: This probably should
180 not call fprintf, but we really do need some mechanism for printing
186 versados_new_symbol (abfd, snum, name, val, sec)
193 asymbol *n = VDATA (abfd)->symbols + snum;
204 get_record (abfd, ptr)
208 bfd_read (&ptr->size, 1, 1, abfd);
209 if (bfd_read ((char *) ptr + 1, 1, ptr->size, abfd) != ptr->size)
218 unsigned char *p = *pp;
220 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
228 char *p = (char *) *pp;
241 new_symbol_string (abfd, name)
245 char *n = VDATA (abfd)->strings;
246 strcpy (VDATA (abfd)->strings, name);
247 VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1;
253 process_esd (abfd, esd, pass)
258 /* Read through the ext def for the est entries */
259 int togo = esd->size - 2;
264 unsigned char *ptr = esd->esd_entries;
265 unsigned char *end = ptr + togo;
268 int scn = *ptr & 0xf;
269 int typ = (*ptr >> 4) & 0xf;
271 /* Declare this section */
272 sprintf (name, "%d", scn);
273 sec = bfd_make_section_old_way (abfd, strdup (name));
274 sec->target_index = scn;
275 EDATA (abfd, scn).section = sec;
284 int snum = VDATA (abfd)->ref_idx++;
288 VDATA (abfd)->stringlen += strlen (name) + 1;
294 char *n = new_symbol_string (abfd, name);
295 s = versados_new_symbol (abfd, snum, n, 0,
296 &bfd_und_section, scn);
297 esidx = VDATA (abfd)->es_done++;
298 RDATA (abfd, esidx - ES_BASE) = s;
306 start = get_4 (&ptr);
308 case ESD_STD_REL_SEC:
309 case ESD_SHRT_REL_SEC:
311 sec->_raw_size = get_4 (&ptr);
312 sec->flags |= SEC_ALLOC;
315 case ESD_XDEF_IN_ABS:
316 sec = (asection *) & bfd_abs_section;
317 case ESD_XDEF_IN_SEC:
319 int snum = VDATA (abfd)->def_idx++;
325 /* Just remember the symbol */
326 VDATA (abfd)->stringlen += strlen (name) + 1;
331 char *n = new_symbol_string (abfd, name);
332 s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n, val, sec, scn);
333 s->flags |= BSF_GLOBAL;
343 #define R_RELWORD_NEG 3
344 #define R_RELLONG_NEG 4
346 reloc_howto_type versados_howto_table[] =
348 HOWTO (R_RELWORD, 0, 1, 16, false,
349 0, complain_overflow_dont, 0,
350 "+v16", true, 0x0000ffff, 0x0000ffff, false),
351 HOWTO (R_RELLONG, 0, 2, 32, false,
352 0, complain_overflow_dont, 0,
353 "+v32", true, 0xffffffff, 0xffffffff, false),
355 HOWTO (R_RELWORD_NEG, 0, -1, 16, false,
356 0, complain_overflow_dont, 0,
357 "-v16", true, 0x0000ffff, 0x0000ffff, false),
358 HOWTO (R_RELLONG_NEG, 0, -2, 32, false,
359 0, complain_overflow_dont, 0,
360 "-v32", true, 0xffffffff, 0xffffffff, false),
365 get_offset (len, ptr)
376 for (i = 1; i < len; i++)
377 val = (val << 8) | *ptr++;
384 process_otr (abfd, otr, pass)
390 unsigned char *srcp = otr->data;
391 unsigned char *endp = (unsigned char *) otr + otr->size;
392 unsigned int bits = (otr->map[0] << 24)
393 | (otr->map[1] << 16)
395 | (otr->map[3] << 0);
397 struct esdid *esdid = &EDATA (abfd, otr->esdid - 1);
398 unsigned char *contents = esdid->contents;
399 int need_contents = 0;
400 unsigned int dst_idx = esdid->pc;
402 for (shift = (1 << 31); shift && srcp < endp; shift >>= 1)
407 int esdids = (flag >> 5) & 0x7;
408 int sizeinwords = ((flag >> 3) & 1) ? 2 : 1;
409 int offsetlen = flag & 0x7;
415 /* A zero esdid means the new pc is the offset given */
416 dst_idx += get_offset (offsetlen, srcp);
421 int val = get_offset (offsetlen, srcp + esdids);
425 for (j = 0; j < sizeinwords * 2; j++)
427 contents[dst_idx + (sizeinwords * 2) - j - 1] = val;
431 for (j = 0; j < esdids; j++)
437 int rn = EDATA (abfd, otr->esdid - 1).relocs++;
440 /* this is the first pass over the data,
441 just remember that we need a reloc */
446 EDATA (abfd, otr->esdid - 1).section->relocation + rn;
447 n->address = dst_idx;
449 n->sym_ptr_ptr = (asymbol **) esdid;
451 n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1);
456 dst_idx += sizeinwords * 2;
462 if (dst_idx < esdid->section->_raw_size)
465 /* absolute code, comes in 16 bit lumps */
466 contents[dst_idx] = srcp[0];
467 contents[dst_idx + 1] = srcp[1];
473 EDATA (abfd, otr->esdid - 1).pc = dst_idx;
475 if (!contents && need_contents)
476 esdid->contents = (unsigned char *) bfd_alloc (abfd, esdid->section->_raw_size);
490 VDATA (abfd)->nrefs = 0;
491 VDATA (abfd)->ndefs = 0;
492 VDATA (abfd)->ref_idx = 0;
493 VDATA (abfd)->def_idx = 0;
498 if (!get_record (abfd, &any))
500 switch (any.header.type)
508 process_esd (abfd, &any.esd, 1);
511 process_otr (abfd, &any.otr, 1);
516 /* Now allocate space for the relocs and sections */
518 VDATA (abfd)->nrefs = VDATA (abfd)->ref_idx;
519 VDATA (abfd)->ndefs = VDATA (abfd)->def_idx;
520 VDATA (abfd)->ref_idx = 0;
521 VDATA (abfd)->def_idx = 0;
523 abfd->symcount = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs;
525 for (i = 0; i < 16; i++)
527 struct esdid *esdid = &EDATA (abfd, i);
530 esdid->section->relocation
531 = (arelent *) bfd_alloc (abfd, sizeof (arelent) * esdid->relocs);
536 esdid->section->flags |= SEC_HAS_CONTENTS | SEC_LOAD;
538 esdid->section->reloc_count = esdid->relocs;
540 esdid->section->flags |= SEC_RELOC;
544 /* Add an entry into the symbol table for it */
546 VDATA (abfd)->stringlen += strlen (esdid->section->name) + 1;
550 abfd->symcount += nsecs;
552 VDATA (abfd)->symbols = (asymbol *) bfd_alloc (abfd,
553 sizeof (asymbol) * (abfd->symcount));
555 VDATA (abfd)->strings = bfd_alloc (abfd, VDATA (abfd)->stringlen);
558 /* Actually fill in the section symbols,
559 we stick them at the end of the table */
561 for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++)
563 struct esdid *esdid = &EDATA (abfd, i);
564 asection *sec = esdid->section;
567 asymbol *s = VDATA (abfd)->symbols + j;
568 s->name = new_symbol_string (abfd, sec->name);
570 s->flags = BSF_LOCAL;
577 abfd->flags |= HAS_SYMS;
579 /* Set this to nsecs - since we've already planted the section
581 VDATA (abfd)->nsecsyms = nsecs;
583 VDATA (abfd)->ref_idx = 0;
590 /* Check whether an existing file is a versados file. */
592 static const bfd_target *
593 versados_object_p (abfd)
596 struct ext_vheader ext;
599 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET))
603 bfd_read (&len, 1, 1, abfd);
604 if (bfd_read (&ext.type, 1, len, abfd) != len
607 bfd_set_error (bfd_error_wrong_format);
611 /* ok, looks like a record, build the tdata and read
614 if (!versados_mkobject (abfd)
615 || !versados_scan (abfd))
623 versados_pass_2 (abfd)
628 if (VDATA (abfd)->pass_2_done)
631 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
634 VDATA (abfd)->es_done = ES_BASE;
637 /* read records till we get to where we want to be */
641 get_record (abfd, &any);
642 switch (any.header.type)
645 VDATA (abfd)->pass_2_done = 1;
648 process_esd (abfd, &any.esd, 2);
651 process_otr (abfd, &any.otr, 2);
658 versados_get_section_contents (abfd, section, location, offset, count)
665 if (!versados_pass_2 (abfd))
669 EDATA (abfd, section->target_index).contents + offset,
676 versados_set_section_contents (abfd, section, location, offset, bytes_to_do)
681 bfd_size_type bytes_to_do;
689 versados_sizeof_headers (abfd, exec)
697 versados_make_empty_symbol (abfd)
700 asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
706 /* Return the amount of memory needed to read the symbol table. */
709 versados_get_symtab_upper_bound (abfd)
712 return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
715 /* Return the symbol table. */
718 versados_get_symtab (abfd, alocation)
722 unsigned int symcount = bfd_get_symcount (abfd);
726 versados_pass_2 (abfd);
728 for (i = 0, s = VDATA (abfd)->symbols;
742 versados_get_symbol_info (ignore_abfd, symbol, ret)
747 bfd_symbol_info (symbol, ret);
752 versados_print_symbol (ignore_abfd, afile, symbol, how)
756 bfd_print_symbol_type how;
758 FILE *file = (FILE *) afile;
761 case bfd_print_symbol_name:
762 fprintf (file, "%s", symbol->name);
765 bfd_print_symbol_vandf ((PTR) file, symbol);
766 fprintf (file, " %-5s %s",
767 symbol->section->name,
774 versados_get_reloc_upper_bound (abfd, asect)
778 return (asect->reloc_count + 1) * sizeof (arelent *);
783 versados_canonicalize_reloc (abfd, section, relptr, symbols)
792 versados_pass_2 (abfd);
793 src = section->relocation;
794 if (!EDATA (abfd, section->target_index).donerel)
796 EDATA (abfd, section->target_index).donerel = 1;
797 /* translate from indexes to symptr ptrs */
798 for (count = 0; count < section->reloc_count; count++)
800 int esdid = (int) src[count].sym_ptr_ptr;
804 src[count].sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
806 else if (esdid < ES_BASE) /* Section relative thing */
808 struct esdid *e = &EDATA (abfd, esdid - 1);
811 /** relocation relative to section which was
814 src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr;
818 src[count].sym_ptr_ptr = symbols + esdid - ES_BASE;
824 for (count = 0; count < section->reloc_count; count++)
829 return section->reloc_count;
832 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
833 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
834 #define versados_new_section_hook _bfd_generic_new_section_hook
836 #define versados_bfd_is_local_label bfd_generic_is_local_label
837 #define versados_get_lineno _bfd_nosymbols_get_lineno
838 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
839 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
840 #define versados_read_minisymbols _bfd_generic_read_minisymbols
841 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
843 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
845 #define versados_set_arch_mach bfd_default_set_arch_mach
847 #define versados_bfd_get_relocated_section_contents \
848 bfd_generic_get_relocated_section_contents
849 #define versados_bfd_relax_section bfd_generic_relax_section
850 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
851 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
852 #define versados_bfd_final_link _bfd_generic_final_link
853 #define versados_bfd_link_split_section _bfd_generic_link_split_section
855 const bfd_target versados_vec =
857 "versados", /* name */
858 bfd_target_versados_flavour,
859 true, /* target byte order */
860 true, /* target headers byte order */
861 (HAS_RELOC | EXEC_P | /* object flags */
862 HAS_LINENO | HAS_DEBUG |
863 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
864 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
865 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
866 0, /* leading underscore */
867 ' ', /* ar_pad_char */
868 16, /* ar_max_namelen */
869 1, /* minimum alignment */
870 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
871 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
872 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
873 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
874 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
875 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
879 versados_object_p, /* bfd_check_format */
886 _bfd_generic_mkarchive,
889 { /* bfd_write_contents */
892 _bfd_write_archive_contents,
896 BFD_JUMP_TABLE_GENERIC (versados),
897 BFD_JUMP_TABLE_COPY (_bfd_generic),
898 BFD_JUMP_TABLE_CORE (_bfd_nocore),
899 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
900 BFD_JUMP_TABLE_SYMBOLS (versados),
901 BFD_JUMP_TABLE_RELOCS (versados),
902 BFD_JUMP_TABLE_WRITE (versados),
903 BFD_JUMP_TABLE_LINK (versados),
904 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),