1 /* BFD back-end for VERSAdos-E objects.
2 Copyright 1995, 96, 97, 98, 99, 2000 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
)->stringlen
= 0;
487 VDATA (abfd
)->nrefs
= 0;
488 VDATA (abfd
)->ndefs
= 0;
489 VDATA (abfd
)->ref_idx
= 0;
490 VDATA (abfd
)->def_idx
= 0;
491 VDATA (abfd
)->pass_2_done
= 0;
496 if (!get_record (abfd
, &any
))
498 switch (any
.header
.type
)
506 process_esd (abfd
, &any
.esd
, 1);
509 process_otr (abfd
, &any
.otr
, 1);
514 /* Now allocate space for the relocs and sections */
516 VDATA (abfd
)->nrefs
= VDATA (abfd
)->ref_idx
;
517 VDATA (abfd
)->ndefs
= VDATA (abfd
)->def_idx
;
518 VDATA (abfd
)->ref_idx
= 0;
519 VDATA (abfd
)->def_idx
= 0;
521 abfd
->symcount
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
;
523 for (i
= 0; i
< 16; i
++)
525 struct esdid
*esdid
= &EDATA (abfd
, i
);
528 esdid
->section
->relocation
529 = (arelent
*) bfd_alloc (abfd
, sizeof (arelent
) * esdid
->relocs
);
534 esdid
->section
->flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
536 esdid
->section
->reloc_count
= esdid
->relocs
;
538 esdid
->section
->flags
|= SEC_RELOC
;
542 /* Add an entry into the symbol table for it */
544 VDATA (abfd
)->stringlen
+= strlen (esdid
->section
->name
) + 1;
548 abfd
->symcount
+= nsecs
;
550 VDATA (abfd
)->symbols
= (asymbol
*) bfd_alloc (abfd
,
551 sizeof (asymbol
) * (abfd
->symcount
));
553 VDATA (abfd
)->strings
= bfd_alloc (abfd
, VDATA (abfd
)->stringlen
);
555 if ((VDATA (abfd
)->symbols
== NULL
&& abfd
->symcount
> 0)
556 || (VDATA (abfd
)->strings
== NULL
&& VDATA (abfd
)->stringlen
> 0))
559 /* Actually fill in the section symbols,
560 we stick them at the end of the table */
562 for (j
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
, i
= 0; i
< 16; i
++)
564 struct esdid
*esdid
= &EDATA (abfd
, i
);
565 asection
*sec
= esdid
->section
;
568 asymbol
*s
= VDATA (abfd
)->symbols
+ j
;
569 s
->name
= new_symbol_string (abfd
, sec
->name
);
571 s
->flags
= BSF_LOCAL
;
578 abfd
->flags
|= HAS_SYMS
;
580 /* Set this to nsecs - since we've already planted the section
582 VDATA (abfd
)->nsecsyms
= nsecs
;
584 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
))
638 versados_pass_2 (abfd
)
643 if (VDATA (abfd
)->pass_2_done
)
646 if (bfd_seek (abfd
, 0, SEEK_SET
) != 0)
649 VDATA (abfd
)->es_done
= ES_BASE
;
652 /* read records till we get to where we want to be */
656 get_record (abfd
, &any
);
657 switch (any
.header
.type
)
660 VDATA (abfd
)->pass_2_done
= 1;
663 process_esd (abfd
, &any
.esd
, 2);
666 process_otr (abfd
, &any
.otr
, 2);
673 versados_get_section_contents (abfd
, section
, location
, offset
, count
)
680 if (!versados_pass_2 (abfd
))
684 EDATA (abfd
, section
->target_index
).contents
+ offset
,
690 #define versados_get_section_contents_in_window \
691 _bfd_generic_get_section_contents_in_window
694 versados_set_section_contents (abfd
, section
, location
, offset
, bytes_to_do
)
695 bfd
*abfd ATTRIBUTE_UNUSED
;
696 sec_ptr section ATTRIBUTE_UNUSED
;
697 PTR location ATTRIBUTE_UNUSED
;
698 file_ptr offset ATTRIBUTE_UNUSED
;
699 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED
;
707 versados_sizeof_headers (abfd
, exec
)
708 bfd
*abfd ATTRIBUTE_UNUSED
;
709 boolean exec ATTRIBUTE_UNUSED
;
715 versados_make_empty_symbol (abfd
)
718 asymbol
*new = (asymbol
*) bfd_zalloc (abfd
, sizeof (asymbol
));
724 /* Return the amount of memory needed to read the symbol table. */
727 versados_get_symtab_upper_bound (abfd
)
730 return (bfd_get_symcount (abfd
) + 1) * sizeof (asymbol
*);
733 /* Return the symbol table. */
736 versados_get_symtab (abfd
, alocation
)
740 unsigned int symcount
= bfd_get_symcount (abfd
);
744 versados_pass_2 (abfd
);
746 for (i
= 0, s
= VDATA (abfd
)->symbols
;
760 versados_get_symbol_info (ignore_abfd
, symbol
, ret
)
761 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
765 bfd_symbol_info (symbol
, ret
);
770 versados_print_symbol (ignore_abfd
, afile
, symbol
, how
)
771 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
774 bfd_print_symbol_type how
;
776 FILE *file
= (FILE *) afile
;
779 case bfd_print_symbol_name
:
780 fprintf (file
, "%s", symbol
->name
);
783 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
784 fprintf (file
, " %-5s %s",
785 symbol
->section
->name
,
792 versados_get_reloc_upper_bound (abfd
, asect
)
793 bfd
*abfd ATTRIBUTE_UNUSED
;
796 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
801 versados_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
810 versados_pass_2 (abfd
);
811 src
= section
->relocation
;
812 if (!EDATA (abfd
, section
->target_index
).donerel
)
814 EDATA (abfd
, section
->target_index
).donerel
= 1;
815 /* translate from indexes to symptr ptrs */
816 for (count
= 0; count
< section
->reloc_count
; count
++)
818 int esdid
= (int) src
[count
].sym_ptr_ptr
;
822 src
[count
].sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
824 else if (esdid
< ES_BASE
) /* Section relative thing */
826 struct esdid
*e
= &EDATA (abfd
, esdid
- 1);
829 /** relocation relative to section which was
832 src
[count
].sym_ptr_ptr
= e
->section
->symbol_ptr_ptr
;
836 src
[count
].sym_ptr_ptr
= symbols
+ esdid
- ES_BASE
;
842 for (count
= 0; count
< section
->reloc_count
; count
++)
847 return section
->reloc_count
;
850 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
851 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
852 #define versados_new_section_hook _bfd_generic_new_section_hook
854 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
855 #define versados_get_lineno _bfd_nosymbols_get_lineno
856 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
857 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
858 #define versados_read_minisymbols _bfd_generic_read_minisymbols
859 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
861 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
863 #define versados_set_arch_mach bfd_default_set_arch_mach
865 #define versados_bfd_get_relocated_section_contents \
866 bfd_generic_get_relocated_section_contents
867 #define versados_bfd_relax_section bfd_generic_relax_section
868 #define versados_bfd_gc_sections bfd_generic_gc_sections
869 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
870 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
871 #define versados_bfd_final_link _bfd_generic_final_link
872 #define versados_bfd_link_split_section _bfd_generic_link_split_section
874 const bfd_target versados_vec
=
876 "versados", /* name */
877 bfd_target_versados_flavour
,
878 BFD_ENDIAN_BIG
, /* target byte order */
879 BFD_ENDIAN_BIG
, /* target headers byte order */
880 (HAS_RELOC
| EXEC_P
| /* object flags */
881 HAS_LINENO
| HAS_DEBUG
|
882 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
883 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
884 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
885 0, /* leading underscore */
886 ' ', /* ar_pad_char */
887 16, /* ar_max_namelen */
888 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
889 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
890 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
891 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
892 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
893 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
897 versados_object_p
, /* bfd_check_format */
904 _bfd_generic_mkarchive
,
907 { /* bfd_write_contents */
910 _bfd_write_archive_contents
,
914 BFD_JUMP_TABLE_GENERIC (versados
),
915 BFD_JUMP_TABLE_COPY (_bfd_generic
),
916 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
917 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
918 BFD_JUMP_TABLE_SYMBOLS (versados
),
919 BFD_JUMP_TABLE_RELOCS (versados
),
920 BFD_JUMP_TABLE_WRITE (versados
),
921 BFD_JUMP_TABLE_LINK (versados
),
922 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),