1 /* BFD back-end for VERSAdos-E objects.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001 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 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 *));
49 static asymbol *versados_new_symbol PARAMS ((bfd *, int, const char *, bfd_vma, asection *));
50 static char *new_symbol_string PARAMS ((bfd *, char *));
51 static const bfd_target *versados_object_p PARAMS ((bfd *));
52 static boolean versados_pass_2 PARAMS ((bfd *));
53 static boolean versados_get_section_contents
54 PARAMS ((bfd *, asection *, void *, file_ptr, bfd_size_type));
55 static boolean versados_set_section_contents
56 PARAMS ((bfd *, sec_ptr, void *, file_ptr, bfd_size_type));
57 static int versados_sizeof_headers PARAMS ((bfd *, boolean));
58 static asymbol *versados_make_empty_symbol PARAMS ((bfd *));
59 static long int versados_get_symtab_upper_bound PARAMS ((bfd *));
60 static long int versados_get_symtab PARAMS ((bfd *, asymbol **));
67 #define ES_BASE 17 /* first symbol has esdid 17 */
69 /* Per file target dependent information */
71 /* one for each section */
74 asection *section; /* ptr to bfd version */
75 unsigned char *contents; /* used to build image */
77 int relocs; /* reloc count, valid end of pass 1 */
78 int donerel; /* have relocs been translated */
81 typedef struct versados_data_struct
83 int es_done; /* count of symbol index, starts at ES_BASE */
84 asymbol *symbols; /* pointer to local symbols */
85 char *strings; /* strings of all the above */
86 int stringlen; /* len of string table (valid end of pass1) */
87 int nsecsyms; /* number of sections */
89 int ndefs; /* number of exported symbols (they dont get esdids) */
90 int nrefs; /* number of imported symbols (valid end of pass1) */
92 int ref_idx; /* current processed value of the above */
97 struct esdid e[16]; /* per section info */
98 int alert; /* to see if we're trampling */
99 asymbol *rest[256 - 16]; /* per symbol info */
104 #define VDATA(abfd) (abfd->tdata.versados_data)
105 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
106 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
112 unsigned char map[4];
114 unsigned char data[200];
120 char type; /* record type */
121 char name[10]; /* module name */
122 char rev; /* module rev number */
138 unsigned char esd_entries[1];
142 #define ESD_STD_REL_SEC 2
143 #define ESD_SHRT_REL_SEC 3
144 #define ESD_XDEF_IN_SEC 4
145 #define ESD_XREF_SYM 7
146 #define ESD_XREF_SEC 6
147 #define ESD_XDEF_IN_ABS 5
151 struct ext_vheader header;
156 static int get_record PARAMS ((bfd *, union ext_any *));
157 static int get_4 PARAMS ((unsigned char **));
158 static void get_10 PARAMS ((unsigned char **, char *));
159 static void process_esd PARAMS ((bfd *, struct ext_esd *, int));
160 static int get_offset PARAMS ((int, unsigned char *));
161 static void process_otr PARAMS ((bfd *, struct ext_otr *, int));
163 /* Initialize by filling in the hex conversion array. */
165 /* Set up the tdata information. */
168 versados_mkobject (abfd)
171 if (abfd->tdata.versados_data == NULL)
173 tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
176 abfd->tdata.versados_data = tdata;
177 tdata->symbols = NULL;
178 VDATA (abfd)->alert = 0x12345678;
181 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, 0);
185 /* Report a problem in an S record file. FIXME: This probably should
186 not call fprintf, but we really do need some mechanism for printing
190 versados_new_symbol (abfd, snum, name, val, sec)
197 asymbol *n = VDATA (abfd)->symbols + snum;
207 get_record (abfd, ptr)
211 bfd_read (&ptr->size, 1, 1, abfd);
212 if (bfd_read ((char *) ptr + 1, 1, ptr->size, abfd) != ptr->size)
221 unsigned char *p = *pp;
223 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
231 char *p = (char *) *pp;
244 new_symbol_string (abfd, name)
248 char *n = VDATA (abfd)->strings;
249 strcpy (VDATA (abfd)->strings, name);
250 VDATA (abfd)->strings += strlen (VDATA (abfd)->strings) + 1;
255 process_esd (abfd, esd, pass)
260 /* Read through the ext def for the est entries */
261 int togo = esd->size - 2;
266 unsigned char *ptr = esd->esd_entries;
267 unsigned char *end = ptr + togo;
270 int scn = *ptr & 0xf;
271 int typ = (*ptr >> 4) & 0xf;
273 /* Declare this section */
274 sprintf (name, "%d", scn);
275 sec = bfd_make_section_old_way (abfd, strdup (name));
276 sec->target_index = scn;
277 EDATA (abfd, scn).section = sec;
286 int snum = VDATA (abfd)->ref_idx++;
290 VDATA (abfd)->stringlen += strlen (name) + 1;
296 char *n = new_symbol_string (abfd, name);
297 s = versados_new_symbol (abfd, snum, n, 0,
299 esidx = VDATA (abfd)->es_done++;
300 RDATA (abfd, esidx - ES_BASE) = s;
307 start = get_4 (&ptr);
309 case ESD_STD_REL_SEC:
310 case ESD_SHRT_REL_SEC:
312 sec->_raw_size = get_4 (&ptr);
313 sec->flags |= SEC_ALLOC;
316 case ESD_XDEF_IN_ABS:
317 sec = (asection *) & bfd_abs_section;
318 case ESD_XDEF_IN_SEC:
320 int snum = VDATA (abfd)->def_idx++;
326 /* Just remember the symbol */
327 VDATA (abfd)->stringlen += strlen (name) + 1;
332 char *n = new_symbol_string (abfd, name);
333 s = versados_new_symbol (abfd, snum + VDATA (abfd)->nrefs, n, val, sec);
334 s->flags |= BSF_GLOBAL;
344 #define R_RELWORD_NEG 3
345 #define R_RELLONG_NEG 4
347 reloc_howto_type versados_howto_table[] =
349 HOWTO (R_RELWORD, 0, 1, 16, false,
350 0, complain_overflow_dont, 0,
351 "+v16", true, 0x0000ffff, 0x0000ffff, false),
352 HOWTO (R_RELLONG, 0, 2, 32, false,
353 0, complain_overflow_dont, 0,
354 "+v32", true, 0xffffffff, 0xffffffff, false),
356 HOWTO (R_RELWORD_NEG, 0, -1, 16, false,
357 0, complain_overflow_dont, 0,
358 "-v16", true, 0x0000ffff, 0x0000ffff, false),
359 HOWTO (R_RELLONG_NEG, 0, -2, 32, false,
360 0, complain_overflow_dont, 0,
361 "-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;
414 /* A zero esdid means the new pc is the offset given */
415 dst_idx += get_offset (offsetlen, srcp);
420 int val = get_offset (offsetlen, srcp + esdids);
424 for (j = 0; j < sizeinwords * 2; j++)
426 contents[dst_idx + (sizeinwords * 2) - j - 1] = val;
430 for (j = 0; j < esdids; j++)
436 int rn = EDATA (abfd, otr->esdid - 1).relocs++;
439 /* this is the first pass over the data,
440 just remember that we need a reloc */
445 EDATA (abfd, otr->esdid - 1).section->relocation + rn;
446 n->address = dst_idx;
448 n->sym_ptr_ptr = (asymbol **) esdid;
450 n->howto = versados_howto_table + ((j & 1) * 2) + (sizeinwords - 1);
455 dst_idx += sizeinwords * 2;
461 if (dst_idx < esdid->section->_raw_size)
464 /* absolute code, comes in 16 bit lumps */
465 contents[dst_idx] = srcp[0];
466 contents[dst_idx + 1] = srcp[1];
472 EDATA (abfd, otr->esdid - 1).pc = dst_idx;
474 if (!contents && need_contents)
475 esdid->contents = (unsigned char *) bfd_alloc (abfd, esdid->section->_raw_size);
488 VDATA (abfd)->stringlen = 0;
489 VDATA (abfd)->nrefs = 0;
490 VDATA (abfd)->ndefs = 0;
491 VDATA (abfd)->ref_idx = 0;
492 VDATA (abfd)->def_idx = 0;
493 VDATA (abfd)->pass_2_done = 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);
557 if ((VDATA (abfd)->symbols == NULL && abfd->symcount > 0)
558 || (VDATA (abfd)->strings == NULL && VDATA (abfd)->stringlen > 0))
561 /* Actually fill in the section symbols,
562 we stick them at the end of the table */
564 for (j = VDATA (abfd)->nrefs + VDATA (abfd)->ndefs, i = 0; i < 16; i++)
566 struct esdid *esdid = &EDATA (abfd, i);
567 asection *sec = esdid->section;
570 asymbol *s = VDATA (abfd)->symbols + j;
571 s->name = new_symbol_string (abfd, sec->name);
573 s->flags = BSF_LOCAL;
580 abfd->flags |= HAS_SYMS;
582 /* Set this to nsecs - since we've already planted the section
584 VDATA (abfd)->nsecsyms = nsecs;
586 VDATA (abfd)->ref_idx = 0;
591 /* Check whether an existing file is a versados file. */
593 static const bfd_target *
594 versados_object_p (abfd)
597 struct ext_vheader ext;
600 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
603 if (bfd_read (&len, 1, 1, abfd) != 1)
605 if (bfd_get_error () != bfd_error_system_call)
606 bfd_set_error (bfd_error_wrong_format);
610 if (bfd_read (&ext.type, 1, len, abfd) != len)
612 if (bfd_get_error () != bfd_error_system_call)
613 bfd_set_error (bfd_error_wrong_format);
617 /* We guess that the language field will never be larger than 10.
618 In sample files, it is always either 0 or 1. Checking for this
619 prevents confusion with Intel Hex files. */
620 if (ext.type != VHEADER
623 bfd_set_error (bfd_error_wrong_format);
627 /* OK, looks like a record, build the tdata and read in. */
629 if (!versados_mkobject (abfd)
630 || !versados_scan (abfd))
637 versados_pass_2 (abfd)
642 if (VDATA (abfd)->pass_2_done)
645 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
648 VDATA (abfd)->es_done = ES_BASE;
650 /* read records till we get to where we want to be */
654 get_record (abfd, &any);
655 switch (any.header.type)
658 VDATA (abfd)->pass_2_done = 1;
661 process_esd (abfd, &any.esd, 2);
664 process_otr (abfd, &any.otr, 2);
671 versados_get_section_contents (abfd, section, location, offset, count)
678 if (!versados_pass_2 (abfd))
682 EDATA (abfd, section->target_index).contents + offset,
688 #define versados_get_section_contents_in_window \
689 _bfd_generic_get_section_contents_in_window
692 versados_set_section_contents (abfd, section, location, offset, bytes_to_do)
693 bfd *abfd ATTRIBUTE_UNUSED;
694 sec_ptr section ATTRIBUTE_UNUSED;
695 PTR location ATTRIBUTE_UNUSED;
696 file_ptr offset ATTRIBUTE_UNUSED;
697 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED;
703 versados_sizeof_headers (abfd, exec)
704 bfd *abfd ATTRIBUTE_UNUSED;
705 boolean exec ATTRIBUTE_UNUSED;
711 versados_make_empty_symbol (abfd)
714 asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
720 /* Return the amount of memory needed to read the symbol table. */
723 versados_get_symtab_upper_bound (abfd)
726 return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
729 /* Return the symbol table. */
732 versados_get_symtab (abfd, alocation)
736 unsigned int symcount = bfd_get_symcount (abfd);
740 versados_pass_2 (abfd);
742 for (i = 0, s = VDATA (abfd)->symbols;
755 versados_get_symbol_info (ignore_abfd, symbol, ret)
756 bfd *ignore_abfd ATTRIBUTE_UNUSED;
760 bfd_symbol_info (symbol, ret);
764 versados_print_symbol (abfd, afile, symbol, how)
768 bfd_print_symbol_type how;
770 FILE *file = (FILE *) afile;
773 case bfd_print_symbol_name:
774 fprintf (file, "%s", symbol->name);
777 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
778 fprintf (file, " %-5s %s",
779 symbol->section->name,
786 versados_get_reloc_upper_bound (abfd, asect)
787 bfd *abfd ATTRIBUTE_UNUSED;
790 return (asect->reloc_count + 1) * sizeof (arelent *);
794 versados_canonicalize_reloc (abfd, section, relptr, symbols)
803 versados_pass_2 (abfd);
804 src = section->relocation;
805 if (!EDATA (abfd, section->target_index).donerel)
807 EDATA (abfd, section->target_index).donerel = 1;
808 /* translate from indexes to symptr ptrs */
809 for (count = 0; count < section->reloc_count; count++)
811 int esdid = (int) src[count].sym_ptr_ptr;
815 src[count].sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
817 else if (esdid < ES_BASE) /* Section relative thing */
819 struct esdid *e = &EDATA (abfd, esdid - 1);
822 /** relocation relative to section which was
825 src[count].sym_ptr_ptr = e->section->symbol_ptr_ptr;
829 src[count].sym_ptr_ptr = symbols + esdid - ES_BASE;
835 for (count = 0; count < section->reloc_count; count++)
840 return section->reloc_count;
843 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
844 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
845 #define versados_new_section_hook _bfd_generic_new_section_hook
847 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
848 #define versados_get_lineno _bfd_nosymbols_get_lineno
849 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
850 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
851 #define versados_read_minisymbols _bfd_generic_read_minisymbols
852 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
854 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
856 #define versados_set_arch_mach bfd_default_set_arch_mach
858 #define versados_bfd_get_relocated_section_contents \
859 bfd_generic_get_relocated_section_contents
860 #define versados_bfd_relax_section bfd_generic_relax_section
861 #define versados_bfd_gc_sections bfd_generic_gc_sections
862 #define versados_bfd_merge_sections bfd_generic_merge_sections
863 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
864 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
865 #define versados_bfd_final_link _bfd_generic_final_link
866 #define versados_bfd_link_split_section _bfd_generic_link_split_section
868 const bfd_target versados_vec =
870 "versados", /* name */
871 bfd_target_versados_flavour,
872 BFD_ENDIAN_BIG, /* target byte order */
873 BFD_ENDIAN_BIG, /* target headers byte order */
874 (HAS_RELOC | EXEC_P | /* object flags */
875 HAS_LINENO | HAS_DEBUG |
876 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
877 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
878 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
879 0, /* leading underscore */
880 ' ', /* ar_pad_char */
881 16, /* ar_max_namelen */
882 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
883 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
884 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
885 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
886 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
887 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
891 versados_object_p, /* bfd_check_format */
898 _bfd_generic_mkarchive,
901 { /* bfd_write_contents */
904 _bfd_write_archive_contents,
908 BFD_JUMP_TABLE_GENERIC (versados),
909 BFD_JUMP_TABLE_COPY (_bfd_generic),
910 BFD_JUMP_TABLE_CORE (_bfd_nocore),
911 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
912 BFD_JUMP_TABLE_SYMBOLS (versados),
913 BFD_JUMP_TABLE_RELOCS (versados),
914 BFD_JUMP_TABLE_WRITE (versados),
915 BFD_JUMP_TABLE_LINK (versados),
916 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),