1 /* BFD back-end for VERSAdos-E objects.
2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
6 Versados is a Motorola trademark.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
27 VERSAdos-E relocatable object file format
31 This module supports reading of VERSAdos relocatable
34 A VERSAdos file looks like contains
36 o Identification Record
37 o External Symbol Definition Record
44 #include "libiberty.h"
52 #define ES_BASE 17 /* First symbol has esdid 17. */
54 /* Per file target dependent information. */
56 /* One for each section. */
59 asection
*section
; /* Ptr to bfd version. */
60 unsigned char *contents
; /* Used to build image. */
62 int relocs
; /* Reloc count, valid end of pass 1. */
63 int donerel
; /* Have relocs been translated. */
66 typedef struct versados_data_struct
68 int es_done
; /* Count of symbol index, starts at ES_BASE. */
69 asymbol
*symbols
; /* Pointer to local symbols. */
70 char *strings
; /* Strings of all the above. */
71 int stringlen
; /* Len of string table (valid end of pass1). */
72 int nsecsyms
; /* Number of sections. */
74 int ndefs
; /* Number of exported symbols (they dont get esdids). */
75 int nrefs
; /* Number of imported symbols (valid end of pass1). */
77 int ref_idx
; /* Current processed value of the above. */
82 struct esdid e
[16]; /* Per section info. */
83 int alert
; /* To see if we're trampling. */
84 asymbol
*rest
[256 - 16]; /* Per symbol info. */
88 #define VDATA(abfd) (abfd->tdata.versados_data)
89 #define EDATA(abfd, n) (abfd->tdata.versados_data->e[n])
90 #define RDATA(abfd, n) (abfd->tdata.versados_data->rest[n])
98 unsigned char data
[200];
104 char type
; /* Record type. */
105 char name
[10]; /* Module name. */
106 char rev
; /* Module rev number. */
122 unsigned char esd_entries
[1];
127 #define ESD_STD_REL_SEC 2
128 #define ESD_SHRT_REL_SEC 3
129 #define ESD_XDEF_IN_SEC 4
130 #define ESD_XDEF_IN_ABS 5
131 #define ESD_XREF_SEC 6
132 #define ESD_XREF_SYM 7
137 struct ext_vheader header
;
142 /* Initialize by filling in the hex conversion array. */
144 /* Set up the tdata information. */
147 versados_mkobject (bfd
*abfd
)
149 if (abfd
->tdata
.versados_data
== NULL
)
151 bfd_size_type amt
= sizeof (tdata_type
);
152 tdata_type
*tdata
= bfd_alloc (abfd
, amt
);
156 abfd
->tdata
.versados_data
= tdata
;
157 tdata
->symbols
= NULL
;
158 VDATA (abfd
)->alert
= 0x12345678;
161 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
165 /* Report a problem in an S record file. FIXME: This probably should
166 not call fprintf, but we really do need some mechanism for printing
170 versados_new_symbol (bfd
*abfd
,
176 asymbol
*n
= VDATA (abfd
)->symbols
+ snum
;
186 get_record (bfd
*abfd
, union ext_any
*ptr
)
188 if (bfd_bread (&ptr
->size
, (bfd_size_type
) 1, abfd
) != 1
189 || (bfd_bread ((char *) ptr
+ 1, (bfd_size_type
) ptr
->size
, abfd
)
196 get_4 (unsigned char **pp
)
198 unsigned char *p
= *pp
;
201 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | (p
[3] << 0);
205 get_10 (unsigned char **pp
, char *name
)
207 char *p
= (char *) *pp
;
211 while (*p
!= ' ' && len
)
220 new_symbol_string (bfd
*abfd
, const char *name
)
222 char *n
= VDATA (abfd
)->strings
;
224 strcpy (VDATA (abfd
)->strings
, name
);
225 VDATA (abfd
)->strings
+= strlen (VDATA (abfd
)->strings
) + 1;
230 process_esd (bfd
*abfd
, struct ext_esd
*esd
, int pass
)
232 /* Read through the ext def for the est entries. */
233 int togo
= esd
->size
- 2;
238 unsigned char *ptr
= esd
->esd_entries
;
239 unsigned char *end
= ptr
+ togo
;
243 int scn
= *ptr
& 0xf;
244 int typ
= (*ptr
>> 4) & 0xf;
246 /* Declare this section. */
247 sprintf (name
, "%d", scn
);
248 sec
= bfd_make_section_old_way (abfd
, strdup (name
));
249 sec
->target_index
= scn
;
250 EDATA (abfd
, scn
).section
= sec
;
260 int snum
= VDATA (abfd
)->ref_idx
++;
263 VDATA (abfd
)->stringlen
+= strlen (name
) + 1;
268 char *n
= new_symbol_string (abfd
, name
);
270 s
= versados_new_symbol (abfd
, snum
, n
, (bfd_vma
) 0,
271 bfd_und_section_ptr
);
272 esidx
= VDATA (abfd
)->es_done
++;
273 RDATA (abfd
, esidx
- ES_BASE
) = s
;
280 start
= get_4 (&ptr
);
282 case ESD_STD_REL_SEC
:
283 case ESD_SHRT_REL_SEC
:
284 sec
->size
= get_4 (&ptr
);
285 sec
->flags
|= SEC_ALLOC
;
287 case ESD_XDEF_IN_ABS
:
288 sec
= (asection
*) & bfd_abs_section
;
289 case ESD_XDEF_IN_SEC
:
291 int snum
= VDATA (abfd
)->def_idx
++;
297 /* Just remember the symbol. */
298 VDATA (abfd
)->stringlen
+= strlen (name
) + 1;
302 char *n
= new_symbol_string (abfd
, name
);
304 s
= versados_new_symbol (abfd
, snum
+ VDATA (abfd
)->nrefs
, n
,
306 s
->flags
|= BSF_GLOBAL
;
316 #define R_RELWORD_NEG 3
317 #define R_RELLONG_NEG 4
319 reloc_howto_type versados_howto_table
[] =
321 HOWTO (R_RELWORD
, 0, 1, 16, FALSE
,
322 0, complain_overflow_dont
, 0,
323 "+v16", TRUE
, 0x0000ffff, 0x0000ffff, FALSE
),
324 HOWTO (R_RELLONG
, 0, 2, 32, FALSE
,
325 0, complain_overflow_dont
, 0,
326 "+v32", TRUE
, 0xffffffff, 0xffffffff, FALSE
),
328 HOWTO (R_RELWORD_NEG
, 0, -1, 16, FALSE
,
329 0, complain_overflow_dont
, 0,
330 "-v16", TRUE
, 0x0000ffff, 0x0000ffff, FALSE
),
331 HOWTO (R_RELLONG_NEG
, 0, -2, 32, FALSE
,
332 0, complain_overflow_dont
, 0,
333 "-v32", TRUE
, 0xffffffff, 0xffffffff, FALSE
),
337 get_offset (int len
, unsigned char *ptr
)
348 for (i
= 1; i
< len
; i
++)
349 val
= (val
<< 8) | *ptr
++;
356 process_otr (bfd
*abfd
, struct ext_otr
*otr
, int pass
)
359 unsigned char *srcp
= otr
->data
;
360 unsigned char *endp
= (unsigned char *) otr
+ otr
->size
;
361 unsigned int bits
= (otr
->map
[0] << 24)
362 | (otr
->map
[1] << 16)
364 | (otr
->map
[3] << 0);
366 struct esdid
*esdid
= &EDATA (abfd
, otr
->esdid
- 1);
367 unsigned char *contents
= esdid
->contents
;
368 int need_contents
= 0;
369 unsigned int dst_idx
= esdid
->pc
;
371 for (shift
= ((unsigned long) 1 << 31); shift
&& srcp
< endp
; shift
>>= 1)
376 int esdids
= (flag
>> 5) & 0x7;
377 int sizeinwords
= ((flag
>> 3) & 1) ? 2 : 1;
378 int offsetlen
= flag
& 0x7;
383 /* A zero esdid means the new pc is the offset given. */
384 dst_idx
+= get_offset (offsetlen
, srcp
);
389 int val
= get_offset (offsetlen
, srcp
+ esdids
);
394 for (j
= 0; j
< sizeinwords
* 2; j
++)
396 contents
[dst_idx
+ (sizeinwords
* 2) - j
- 1] = val
;
400 for (j
= 0; j
< esdids
; j
++)
406 int rn
= EDATA (abfd
, otr
->esdid
- 1).relocs
++;
410 /* This is the first pass over the data,
411 just remember that we need a reloc. */
416 EDATA (abfd
, otr
->esdid
- 1).section
->relocation
+ rn
;
417 n
->address
= dst_idx
;
419 n
->sym_ptr_ptr
= (asymbol
**) (size_t) esdid
;
421 n
->howto
= versados_howto_table
+ ((j
& 1) * 2) + (sizeinwords
- 1);
426 dst_idx
+= sizeinwords
* 2;
432 if (dst_idx
< esdid
->section
->size
)
435 /* Absolute code, comes in 16 bit lumps. */
436 contents
[dst_idx
] = srcp
[0];
437 contents
[dst_idx
+ 1] = srcp
[1];
443 EDATA (abfd
, otr
->esdid
- 1).pc
= dst_idx
;
445 if (!contents
&& need_contents
)
447 bfd_size_type size
= esdid
->section
->size
;
448 esdid
->contents
= bfd_alloc (abfd
, size
);
453 versados_scan (bfd
*abfd
)
461 VDATA (abfd
)->stringlen
= 0;
462 VDATA (abfd
)->nrefs
= 0;
463 VDATA (abfd
)->ndefs
= 0;
464 VDATA (abfd
)->ref_idx
= 0;
465 VDATA (abfd
)->def_idx
= 0;
466 VDATA (abfd
)->pass_2_done
= 0;
472 if (!get_record (abfd
, &any
))
474 switch (any
.header
.type
)
482 process_esd (abfd
, &any
.esd
, 1);
485 process_otr (abfd
, &any
.otr
, 1);
490 /* Now allocate space for the relocs and sections. */
491 VDATA (abfd
)->nrefs
= VDATA (abfd
)->ref_idx
;
492 VDATA (abfd
)->ndefs
= VDATA (abfd
)->def_idx
;
493 VDATA (abfd
)->ref_idx
= 0;
494 VDATA (abfd
)->def_idx
= 0;
496 abfd
->symcount
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
;
498 for (i
= 0; i
< 16; i
++)
500 struct esdid
*esdid
= &EDATA (abfd
, i
);
504 amt
= (bfd_size_type
) esdid
->relocs
* sizeof (arelent
);
505 esdid
->section
->relocation
= bfd_alloc (abfd
, amt
);
510 esdid
->section
->flags
|= SEC_HAS_CONTENTS
| SEC_LOAD
;
512 esdid
->section
->reloc_count
= esdid
->relocs
;
514 esdid
->section
->flags
|= SEC_RELOC
;
518 /* Add an entry into the symbol table for it. */
520 VDATA (abfd
)->stringlen
+= strlen (esdid
->section
->name
) + 1;
524 abfd
->symcount
+= nsecs
;
526 amt
= abfd
->symcount
;
527 amt
*= sizeof (asymbol
);
528 VDATA (abfd
)->symbols
= bfd_alloc (abfd
, amt
);
530 amt
= VDATA (abfd
)->stringlen
;
531 VDATA (abfd
)->strings
= bfd_alloc (abfd
, amt
);
533 if ((VDATA (abfd
)->symbols
== NULL
&& abfd
->symcount
> 0)
534 || (VDATA (abfd
)->strings
== NULL
&& VDATA (abfd
)->stringlen
> 0))
537 /* Actually fill in the section symbols,
538 we stick them at the end of the table. */
539 for (j
= VDATA (abfd
)->nrefs
+ VDATA (abfd
)->ndefs
, i
= 0; i
< 16; i
++)
541 struct esdid
*esdid
= &EDATA (abfd
, i
);
542 asection
*sec
= esdid
->section
;
546 asymbol
*s
= VDATA (abfd
)->symbols
+ j
;
547 s
->name
= new_symbol_string (abfd
, sec
->name
);
549 s
->flags
= BSF_LOCAL
;
557 abfd
->flags
|= HAS_SYMS
;
559 /* Set this to nsecs - since we've already planted the section
561 VDATA (abfd
)->nsecsyms
= nsecs
;
563 VDATA (abfd
)->ref_idx
= 0;
568 /* Check whether an existing file is a versados file. */
570 static const bfd_target
*
571 versados_object_p (bfd
*abfd
)
573 struct ext_vheader ext
;
575 tdata_type
*tdata_save
;
577 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
580 if (bfd_bread (&len
, (bfd_size_type
) 1, abfd
) != 1)
582 if (bfd_get_error () != bfd_error_system_call
)
583 bfd_set_error (bfd_error_wrong_format
);
587 if (bfd_bread (&ext
.type
, (bfd_size_type
) len
, abfd
) != len
)
589 if (bfd_get_error () != bfd_error_system_call
)
590 bfd_set_error (bfd_error_wrong_format
);
594 /* We guess that the language field will never be larger than 10.
595 In sample files, it is always either 0 or 1. Checking for this
596 prevents confusion with Intel Hex files. */
597 if (ext
.type
!= VHEADER
600 bfd_set_error (bfd_error_wrong_format
);
604 /* OK, looks like a record, build the tdata and read in. */
605 tdata_save
= abfd
->tdata
.versados_data
;
606 if (!versados_mkobject (abfd
) || !versados_scan (abfd
))
608 abfd
->tdata
.versados_data
= tdata_save
;
616 versados_pass_2 (bfd
*abfd
)
620 if (VDATA (abfd
)->pass_2_done
)
623 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
626 VDATA (abfd
)->es_done
= ES_BASE
;
628 /* Read records till we get to where we want to be. */
631 get_record (abfd
, &any
);
632 switch (any
.header
.type
)
635 VDATA (abfd
)->pass_2_done
= 1;
638 process_esd (abfd
, &any
.esd
, 2);
641 process_otr (abfd
, &any
.otr
, 2);
648 versados_get_section_contents (bfd
*abfd
,
654 if (!versados_pass_2 (abfd
))
658 EDATA (abfd
, section
->target_index
).contents
+ offset
,
664 #define versados_get_section_contents_in_window \
665 _bfd_generic_get_section_contents_in_window
668 versados_set_section_contents (bfd
*abfd ATTRIBUTE_UNUSED
,
669 sec_ptr section ATTRIBUTE_UNUSED
,
670 const void * location ATTRIBUTE_UNUSED
,
671 file_ptr offset ATTRIBUTE_UNUSED
,
672 bfd_size_type bytes_to_do ATTRIBUTE_UNUSED
)
678 versados_sizeof_headers (bfd
*abfd ATTRIBUTE_UNUSED
,
679 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
684 /* Return the amount of memory needed to read the symbol table. */
687 versados_get_symtab_upper_bound (bfd
*abfd
)
689 return (bfd_get_symcount (abfd
) + 1) * sizeof (asymbol
*);
692 /* Return the symbol table. */
695 versados_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
697 unsigned int symcount
= bfd_get_symcount (abfd
);
701 versados_pass_2 (abfd
);
703 for (i
= 0, s
= VDATA (abfd
)->symbols
;
714 versados_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
718 bfd_symbol_info (symbol
, ret
);
722 versados_print_symbol (bfd
*abfd
,
725 bfd_print_symbol_type how
)
727 FILE *file
= (FILE *) afile
;
731 case bfd_print_symbol_name
:
732 fprintf (file
, "%s", symbol
->name
);
735 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
736 fprintf (file
, " %-5s %s",
737 symbol
->section
->name
,
743 versados_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
746 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
750 versados_canonicalize_reloc (bfd
*abfd
,
758 versados_pass_2 (abfd
);
759 src
= section
->relocation
;
760 if (!EDATA (abfd
, section
->target_index
).donerel
)
762 EDATA (abfd
, section
->target_index
).donerel
= 1;
763 /* Translate from indexes to symptr ptrs. */
764 for (count
= 0; count
< section
->reloc_count
; count
++)
766 int esdid
= (int) (size_t) src
[count
].sym_ptr_ptr
;
769 src
[count
].sym_ptr_ptr
= bfd_abs_section
.symbol_ptr_ptr
;
770 else if (esdid
< ES_BASE
)
772 /* Section relative thing. */
773 struct esdid
*e
= &EDATA (abfd
, esdid
- 1);
775 src
[count
].sym_ptr_ptr
= e
->section
->symbol_ptr_ptr
;
778 src
[count
].sym_ptr_ptr
= symbols
+ esdid
- ES_BASE
;
782 for (count
= 0; count
< section
->reloc_count
; count
++)
786 return section
->reloc_count
;
789 #define versados_close_and_cleanup _bfd_generic_close_and_cleanup
790 #define versados_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
791 #define versados_new_section_hook _bfd_generic_new_section_hook
792 #define versados_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
793 #define versados_bfd_is_local_label_name bfd_generic_is_local_label_name
794 #define versados_get_lineno _bfd_nosymbols_get_lineno
795 #define versados_find_nearest_line _bfd_nosymbols_find_nearest_line
796 #define versados_find_inliner_info _bfd_nosymbols_find_inliner_info
797 #define versados_make_empty_symbol _bfd_generic_make_empty_symbol
798 #define versados_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
799 #define versados_read_minisymbols _bfd_generic_read_minisymbols
800 #define versados_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
801 #define versados_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
802 #define versados_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
803 #define versados_set_arch_mach bfd_default_set_arch_mach
804 #define versados_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
805 #define versados_bfd_relax_section bfd_generic_relax_section
806 #define versados_bfd_gc_sections bfd_generic_gc_sections
807 #define versados_bfd_merge_sections bfd_generic_merge_sections
808 #define versados_bfd_is_group_section bfd_generic_is_group_section
809 #define versados_bfd_discard_group bfd_generic_discard_group
810 #define versados_section_already_linked _bfd_generic_section_already_linked
811 #define versados_bfd_define_common_symbol bfd_generic_define_common_symbol
812 #define versados_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
813 #define versados_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
814 #define versados_bfd_link_add_symbols _bfd_generic_link_add_symbols
815 #define versados_bfd_link_just_syms _bfd_generic_link_just_syms
816 #define versados_bfd_final_link _bfd_generic_final_link
817 #define versados_bfd_link_split_section _bfd_generic_link_split_section
819 const bfd_target versados_vec
=
821 "versados", /* Name. */
822 bfd_target_versados_flavour
,
823 BFD_ENDIAN_BIG
, /* Target byte order. */
824 BFD_ENDIAN_BIG
, /* Target headers byte order. */
825 (HAS_RELOC
| EXEC_P
| /* Object flags. */
826 HAS_LINENO
| HAS_DEBUG
|
827 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
828 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
829 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* Section flags. */
830 0, /* Leading underscore. */
831 ' ', /* AR_pad_char. */
832 16, /* AR_max_namelen. */
833 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
834 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
835 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* Data. */
836 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
837 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
838 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* Headers. */
842 versados_object_p
, /* bfd_check_format. */
849 _bfd_generic_mkarchive
,
852 { /* bfd_write_contents. */
855 _bfd_write_archive_contents
,
859 BFD_JUMP_TABLE_GENERIC (versados
),
860 BFD_JUMP_TABLE_COPY (_bfd_generic
),
861 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
862 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
863 BFD_JUMP_TABLE_SYMBOLS (versados
),
864 BFD_JUMP_TABLE_RELOCS (versados
),
865 BFD_JUMP_TABLE_WRITE (versados
),
866 BFD_JUMP_TABLE_LINK (versados
),
867 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),