1 /* BFD back-end for VERSAdos-E objects.
2 Copyright 1995, 96, 97, 98, 1999 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
45 #include "libiberty.h"
48 static boolean versados_mkobject
PARAMS ((bfd
*));
49 static boolean versados_scan
PARAMS ((bfd
*));
50 static const bfd_target
*versados_object_p
PARAMS ((bfd
*));
59 #define ES_BASE 17 /* first symbol has esdid 17 */
61 /* Per file target dependent information */
63 /* one for each section */
66 asection
*section
; /* ptr to bfd version */
67 unsigned char *contents
; /* used to build image */
69 int relocs
; /* reloc count, valid end of pass 1 */
70 int donerel
; /* have relocs been translated */
73 typedef struct versados_data_struct
75 int es_done
; /* count of symbol index, starts at ES_BASE */
76 asymbol
*symbols
; /* pointer to local symbols */
77 char *strings
; /* strings of all the above */
78 int stringlen
; /* len of string table (valid end of pass1) */
79 int nsecsyms
; /* number of sections */
81 int ndefs
; /* number of exported symbols (they dont get esdids) */
82 int nrefs
; /* number of imported symbols (valid end of pass1) */
84 int ref_idx
; /* current processed value of the above */
89 struct esdid e
[16]; /* per section info */
90 int alert
; /* to see if we're trampling */
91 asymbol
*rest
[256 - 16]; /* per symbol info */
96 #define VDATA(abfd) (abfd->tdata.versados_data)
97 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
98 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
104 unsigned char map
[4];
106 unsigned char data
[200];
112 char type
; /* record type */
113 char name
[10]; /* module name */
114 char rev
; /* module rev number */
130 unsigned char esd_entries
[1];
134 #define ESD_STD_REL_SEC 2
135 #define ESD_SHRT_REL_SEC 3
136 #define ESD_XDEF_IN_SEC 4
137 #define ESD_XREF_SYM 7
138 #define ESD_XREF_SEC 6
139 #define ESD_XDEF_IN_ABS 5
143 struct ext_vheader header
;
148 /* Initialize by filling in the hex conversion array. */
154 /* Set up the tdata information. */
157 versados_mkobject (abfd
)
160 if (abfd
->tdata
.versados_data
== NULL
)
162 tdata_type
*tdata
= (tdata_type
*) bfd_alloc (abfd
, sizeof (tdata_type
));
165 abfd
->tdata
.versados_data
= tdata
;
166 tdata
->symbols
= NULL
;
167 VDATA (abfd
)->alert
= 0x12345678;
170 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
175 /* Report a problem in an S record file. FIXME: This probably should
176 not call fprintf, but we really do need some mechanism for printing
182 versados_new_symbol (abfd
, snum
, name
, val
, sec
)
189 asymbol
*n
= VDATA (abfd
)->symbols
+ snum
;
200 get_record (abfd
, ptr
)
204 bfd_read (&ptr
->size
, 1, 1, abfd
);
205 if (bfd_read ((char *) ptr
+ 1, 1, ptr
->size
, abfd
) != ptr
->size
)
214 unsigned char *p
= *pp
;
216 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | (p
[3] << 0);
224 char *p
= (char *) *pp
;
237 new_symbol_string (abfd
, name
)
241 char *n
= VDATA (abfd
)->strings
;
242 strcpy (VDATA (abfd
)->strings
, name
);
243 VDATA (abfd
)->strings
+= strlen (VDATA (abfd
)->strings
) + 1;
249 process_esd (abfd
, esd
, pass
)
254 /* Read through the ext def for the est entries */
255 int togo
= esd
->size
- 2;
260 unsigned char *ptr
= esd
->esd_entries
;
261 unsigned char *end
= ptr
+ togo
;
264 int scn
= *ptr
& 0xf;
265 int typ
= (*ptr
>> 4) & 0xf;
267 /* Declare this section */
268 sprintf (name
, "%d", scn
);
269 sec
= bfd_make_section_old_way (abfd
, strdup (name
));
270 sec
->target_index
= scn
;
271 EDATA (abfd
, scn
).section
= sec
;
280 int snum
= VDATA (abfd
)->ref_idx
++;
284 VDATA (abfd
)->stringlen
+= strlen (name
) + 1;
290 char *n
= new_symbol_string (abfd
, name
);
291 s
= versados_new_symbol (abfd
, snum
, n
, 0,
292 &bfd_und_section
, scn
);
293 esidx
= VDATA (abfd
)->es_done
++;
294 RDATA (abfd
, esidx
- ES_BASE
) = s
;
302 start
= get_4 (&ptr
);
304 case ESD_STD_REL_SEC
:
305 case ESD_SHRT_REL_SEC
:
307 sec
->_raw_size
= get_4 (&ptr
);
308 sec
->flags
|= SEC_ALLOC
;
311 case ESD_XDEF_IN_ABS
:
312 sec
= (asection
*) & bfd_abs_section
;
313 case ESD_XDEF_IN_SEC
:
315 int snum
= VDATA (abfd
)->def_idx
++;
321 /* Just remember the symbol */
322 VDATA (abfd
)->stringlen
+= strlen (name
) + 1;
327 char *n
= new_symbol_string (abfd
, name
);
328 s
= versados_new_symbol (abfd
, snum
+ VDATA (abfd
)->nrefs
, n
, val
, sec
, scn
);
329 s
->flags
|= BSF_GLOBAL
;
339 #define R_RELWORD_NEG 3
340 #define R_RELLONG_NEG 4
342 reloc_howto_type versados_howto_table
[] =
344 HOWTO (R_RELWORD
, 0, 1, 16, false,
345 0, complain_overflow_dont
, 0,
346 "+v16", true, 0x0000ffff, 0x0000ffff, false),
347 HOWTO (R_RELLONG
, 0, 2, 32, false,
348 0, complain_overflow_dont
, 0,
349 "+v32", true, 0xffffffff, 0xffffffff, false),
351 HOWTO (R_RELWORD_NEG
, 0, -1, 16, false,
352 0, complain_overflow_dont
, 0,
353 "-v16", true, 0x0000ffff, 0x0000ffff, false),
354 HOWTO (R_RELLONG_NEG
, 0, -2, 32, false,
355 0, complain_overflow_dont
, 0,
356 "-v32", true, 0xffffffff, 0xffffffff, false),
361 get_offset (len
, ptr
)
372 for (i
= 1; i
< len
; i
++)
373 val
= (val
<< 8) | *ptr
++;
380 process_otr (abfd
, otr
, pass
)
386 unsigned char *srcp
= otr
->data
;
387 unsigned char *endp
= (unsigned char *) otr
+ otr
->size
;
388 unsigned int bits
= (otr
->map
[0] << 24)
389 | (otr
->map
[1] << 16)
391 | (otr
->map
[3] << 0);
393 struct esdid
*esdid
= &EDATA (abfd
, otr
->esdid
- 1);
394 unsigned char *contents
= esdid
->contents
;
395 int need_contents
= 0;
396 unsigned int dst_idx
= esdid
->pc
;
398 for (shift
= (1 << 31); shift
&& srcp
< endp
; shift
>>= 1)
403 int esdids
= (flag
>> 5) & 0x7;
404 int sizeinwords
= ((flag
>> 3) & 1) ? 2 : 1;
405 int offsetlen
= flag
& 0x7;
411 /* A zero esdid means the new pc is the offset given */
412 dst_idx
+= get_offset (offsetlen
, srcp
);
417 int val
= get_offset (offsetlen
, srcp
+ esdids
);
421 for (j
= 0; j
< sizeinwords
* 2; j
++)
423 contents
[dst_idx
+ (sizeinwords
* 2) - j
- 1] = val
;
427 for (j
= 0; j
< esdids
; j
++)
433 int rn
= EDATA (abfd
, otr
->esdid
- 1).relocs
++;
436 /* this is the first pass over the data,
437 just remember that we need a reloc */
442 EDATA (abfd
, otr
->esdid
- 1).section
->relocation
+ rn
;
443 n
->address
= dst_idx
;
445 n
->sym_ptr_ptr
= (asymbol
**) esdid
;
447 n
->howto
= versados_howto_table
+ ((j
& 1) * 2) + (sizeinwords
- 1);
452 dst_idx
+= sizeinwords
* 2;
458 if (dst_idx
< esdid
->section
->_raw_size
)
461 /* absolute code, comes in 16 bit lumps */
462 contents
[dst_idx
] = srcp
[0];
463 contents
[dst_idx
+ 1] = srcp
[1];
469 EDATA (abfd
, otr
->esdid
- 1).pc
= dst_idx
;
471 if (!contents
&& need_contents
)
472 esdid
->contents
= (unsigned char *) bfd_alloc (abfd
, esdid
->section
->_raw_size
);
486 VDATA (abfd
)->nrefs
= 0;
487 VDATA (abfd
)->ndefs
= 0;
488 VDATA (abfd
)->ref_idx
= 0;
489 VDATA (abfd
)->def_idx
= 0;
494 if (!get_record (abfd
, &any
))
496 switch (any
.header
.type
)
504 process_esd (abfd
, &any
.esd
, 1);
507 process_otr (abfd
, &any
.otr
, 1);
512 /* Now allocate space for the relocs and sections */
514 VDATA (abfd
)->nrefs
= VDATA (abfd
)->ref_idx
;
515 VDATA (abfd
)->ndefs
= VDATA (abfd
)->def_idx
;
516 VDATA (abfd
)->ref_idx
= 0;
517 VDATA (abfd
)->def_idx
= 0;
519 abfd
->symcount
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
;
521 for (i
= 0; i
< 16; i
++)
523 struct esdid
*esdid
= &EDATA (abfd
, i
);
526 esdid
->section
->relocation
527 = (arelent
*) bfd_alloc (abfd
, sizeof (arelent
) * esdid
->relocs
);
532 esdid
->section
->flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
534 esdid
->section
->reloc_count
= esdid
->relocs
;
536 esdid
->section
->flags
|= SEC_RELOC
;
540 /* Add an entry into the symbol table for it */
542 VDATA (abfd
)->stringlen
+= strlen (esdid
->section
->name
) + 1;
546 abfd
->symcount
+= nsecs
;
548 VDATA (abfd
)->symbols
= (asymbol
*) bfd_alloc (abfd
,
549 sizeof (asymbol
) * (abfd
->symcount
));
551 VDATA (abfd
)->strings
= bfd_alloc (abfd
, VDATA (abfd
)->stringlen
);
553 if ((VDATA (abfd
)->symbols
== NULL
&& abfd
->symcount
> 0)
554 || (VDATA (abfd
)->strings
== NULL
&& VDATA (abfd
)->stringlen
> 0))
557 /* Actually fill in the section symbols,
558 we stick them at the end of the table */
560 for (j
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
, i
= 0; i
< 16; i
++)
562 struct esdid
*esdid
= &EDATA (abfd
, i
);
563 asection
*sec
= esdid
->section
;
566 asymbol
*s
= VDATA (abfd
)->symbols
+ j
;
567 s
->name
= new_symbol_string (abfd
, sec
->name
);
569 s
->flags
= BSF_LOCAL
;
576 abfd
->flags
|= HAS_SYMS
;
578 /* Set this to nsecs - since we've already planted the section
580 VDATA (abfd
)->nsecsyms
= nsecs
;
582 VDATA (abfd
)->ref_idx
= 0;
589 /* Check whether an existing file is a versados file. */
591 static const bfd_target
*
592 versados_object_p (abfd
)
595 struct ext_vheader ext
;
598 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
601 if (bfd_read (&len
, 1, 1, abfd
) != 1)
603 if (bfd_get_error () != bfd_error_system_call
)
604 bfd_set_error (bfd_error_wrong_format
);
608 if (bfd_read (&ext
.type
, 1, len
, abfd
) != len
)
610 if (bfd_get_error () != bfd_error_system_call
)
611 bfd_set_error (bfd_error_wrong_format
);
615 /* We guess that the language field will never be larger than 10.
616 In sample files, it is always either 0 or 1. Checking for this
617 prevents confusion with Intel Hex files. */
618 if (ext
.type
!= VHEADER
621 bfd_set_error (bfd_error_wrong_format
);
625 /* OK, looks like a record, build the tdata and read in. */
627 if (!versados_mkobject (abfd
)
628 || !versados_scan (abfd
))
636 versados_pass_2 (abfd
)
641 if (VDATA (abfd
)->pass_2_done
)
644 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
647 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
;
705 versados_sizeof_headers (abfd
, exec
)
706 bfd
*abfd ATTRIBUTE_UNUSED
;
707 boolean exec ATTRIBUTE_UNUSED
;
713 versados_make_empty_symbol (abfd
)
716 asymbol
*new = (asymbol
*) bfd_zalloc (abfd
, sizeof (asymbol
));
722 /* Return the amount of memory needed to read the symbol table. */
725 versados_get_symtab_upper_bound (abfd
)
728 return (bfd_get_symcount (abfd
) + 1) * sizeof (asymbol
*);
731 /* Return the symbol table. */
734 versados_get_symtab (abfd
, alocation
)
738 unsigned int symcount
= bfd_get_symcount (abfd
);
742 versados_pass_2 (abfd
);
744 for (i
= 0, s
= VDATA (abfd
)->symbols
;
758 versados_get_symbol_info (ignore_abfd
, symbol
, ret
)
759 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
763 bfd_symbol_info (symbol
, ret
);
768 versados_print_symbol (ignore_abfd
, afile
, symbol
, how
)
769 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
772 bfd_print_symbol_type how
;
774 FILE *file
= (FILE *) afile
;
777 case bfd_print_symbol_name
:
778 fprintf (file
, "%s", symbol
->name
);
781 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
782 fprintf (file
, " %-5s %s",
783 symbol
->section
->name
,
790 versados_get_reloc_upper_bound (abfd
, asect
)
791 bfd
*abfd ATTRIBUTE_UNUSED
;
794 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
799 versados_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
808 versados_pass_2 (abfd
);
809 src
= section
->relocation
;
810 if (!EDATA (abfd
, section
->target_index
).donerel
)
812 EDATA (abfd
, section
->target_index
).donerel
= 1;
813 /* translate from indexes to symptr ptrs */
814 for (count
= 0; count
< section
->reloc_count
; count
++)
816 int esdid
= (int) src
[count
].sym_ptr_ptr
;
820 src
[count
].sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
822 else if (esdid
< ES_BASE
) /* Section relative thing */
824 struct esdid
*e
= &EDATA (abfd
, esdid
- 1);
827 /** relocation relative to section which was
830 src
[count
].sym_ptr_ptr
= e
->section
->symbol_ptr_ptr
;
834 src
[count
].sym_ptr_ptr
= symbols
+ esdid
- ES_BASE
;
840 for (count
= 0; count
< section
->reloc_count
; count
++)
845 return section
->reloc_count
;
848 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
849 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
850 #define versados_new_section_hook _bfd_generic_new_section_hook
852 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
853 #define versados_get_lineno _bfd_nosymbols_get_lineno
854 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
855 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
856 #define versados_read_minisymbols _bfd_generic_read_minisymbols
857 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
859 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
861 #define versados_set_arch_mach bfd_default_set_arch_mach
863 #define versados_bfd_get_relocated_section_contents \
864 bfd_generic_get_relocated_section_contents
865 #define versados_bfd_relax_section bfd_generic_relax_section
866 #define versados_bfd_gc_sections bfd_generic_gc_sections
867 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
868 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
869 #define versados_bfd_final_link _bfd_generic_final_link
870 #define versados_bfd_link_split_section _bfd_generic_link_split_section
872 const bfd_target versados_vec
=
874 "versados", /* name */
875 bfd_target_versados_flavour
,
876 BFD_ENDIAN_BIG
, /* target byte order */
877 BFD_ENDIAN_BIG
, /* target headers byte order */
878 (HAS_RELOC
| EXEC_P
| /* object flags */
879 HAS_LINENO
| HAS_DEBUG
|
880 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
881 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
882 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
883 0, /* leading underscore */
884 ' ', /* ar_pad_char */
885 16, /* ar_max_namelen */
886 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
887 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
888 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
889 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
890 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
891 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
895 versados_object_p
, /* bfd_check_format */
902 _bfd_generic_mkarchive
,
905 { /* bfd_write_contents */
908 _bfd_write_archive_contents
,
912 BFD_JUMP_TABLE_GENERIC (versados
),
913 BFD_JUMP_TABLE_COPY (_bfd_generic
),
914 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
915 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
916 BFD_JUMP_TABLE_SYMBOLS (versados
),
917 BFD_JUMP_TABLE_RELOCS (versados
),
918 BFD_JUMP_TABLE_WRITE (versados
),
919 BFD_JUMP_TABLE_LINK (versados
),
920 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),