1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define UNDERSCORE_HACK 1
30 static boolean oasys_read_record
PARAMS ((bfd
*,
31 oasys_record_union_type
*));
32 static boolean oasys_write_sections
PARAMS ((bfd
*));
33 static boolean oasys_write_record
PARAMS ((bfd
*,
34 oasys_record_enum_type
,
35 oasys_record_union_type
*,
37 static boolean oasys_write_syms
PARAMS ((bfd
*));
38 static boolean oasys_write_header
PARAMS ((bfd
*));
39 static boolean oasys_write_end
PARAMS ((bfd
*));
40 static boolean oasys_write_data
PARAMS ((bfd
*));
41 static size_t oasys_string_length
PARAMS ((oasys_record_union_type
*));
42 static boolean oasys_slurp_symbol_table
PARAMS ((bfd
*const));
43 static long int oasys_get_symtab_upper_bound
PARAMS ((bfd
*const));
44 static const bfd_target
*oasys_archive_p
PARAMS ((bfd
*));
45 static boolean oasys_mkobject
PARAMS ((bfd
*));
46 static const bfd_target
*oasys_object_p
PARAMS ((bfd
*));
47 static void oasys_get_symbol_info
PARAMS ((bfd
*, asymbol
*, symbol_info
*));
48 static void oasys_print_symbol
PARAMS ((bfd
*, void *, asymbol
*, bfd_print_symbol_type
));
49 static boolean oasys_new_section_hook
PARAMS ((bfd
*, asection
*));
50 static long int oasys_get_reloc_upper_bound
PARAMS ((bfd
*, sec_ptr
));
51 static boolean oasys_get_section_contents
52 PARAMS ((bfd
*, sec_ptr
, void *, file_ptr
, bfd_size_type
));
53 static int comp
PARAMS ((const void *, const void *));
54 static boolean oasys_write_object_contents
PARAMS ((bfd
*));
55 static boolean oasys_set_section_contents
56 PARAMS ((bfd
*, sec_ptr
, void *, file_ptr
, bfd_size_type
));
57 static asymbol
*oasys_make_empty_symbol
PARAMS ((bfd
*));
58 static bfd
*oasys_openr_next_archived_file
PARAMS ((bfd
*, bfd
*));
59 static boolean oasys_find_nearest_line
60 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
, char **, char **, unsigned int *));
61 static int oasys_generic_stat_arch_elt
PARAMS ((bfd
*, struct stat
*));
62 static int oasys_sizeof_headers
PARAMS ((bfd
*, boolean
));
64 long oasys_get_symtab
PARAMS ((bfd
*, asymbol
**));
65 long oasys_canonicalize_reloc
PARAMS ((bfd
*, sec_ptr
, arelent
**, asymbol
**));
67 /* Read in all the section data and relocation stuff too. */
68 PROTO (static boolean
, oasys_slurp_section_data
, (bfd
* CONST abfd
));
71 oasys_read_record (abfd
, record
)
73 oasys_record_union_type
*record
;
75 if (bfd_read ((PTR
) record
, 1, sizeof (record
->header
), abfd
)
76 != sizeof (record
->header
))
79 if ((size_t) record
->header
.length
<= (size_t) sizeof (record
->header
))
81 if (bfd_read ((PTR
) (((char *) record
) + sizeof (record
->header
)),
82 1, record
->header
.length
- sizeof (record
->header
),
84 != record
->header
.length
- sizeof (record
->header
))
89 oasys_string_length (record
)
90 oasys_record_union_type
*record
;
92 return record
->header
.length
93 - ((char *) record
->symbol
.name
- (char *) record
);
96 /*****************************************************************************/
100 Slurp the symbol table by reading in all the records at the start file
101 till we get to the first section record.
103 We'll sort the symbolss into two lists, defined and undefined. The
104 undefined symbols will be placed into the table according to their
107 We do this by placing all undefined symbols at the front of the table
108 moving in, and the defined symbols at the end of the table moving back.
113 oasys_slurp_symbol_table (abfd
)
116 oasys_record_union_type record
;
117 oasys_data_type
*data
= OASYS_DATA (abfd
);
119 asymbol
*dest_defined
;
124 if (data
->symbols
!= (asymbol
*) NULL
)
128 /* Buy enough memory for all the symbols and all the names */
130 (asymbol
*) bfd_alloc (abfd
, sizeof (asymbol
) * abfd
->symcount
);
131 #ifdef UNDERSCORE_HACK
132 /* buy 1 more char for each symbol to keep the underscore in*/
133 data
->strings
= bfd_alloc (abfd
, data
->symbol_string_length
+
136 data
->strings
= bfd_alloc (abfd
, data
->symbol_string_length
);
138 if (!data
->symbols
|| !data
->strings
)
141 dest_defined
= data
->symbols
+ abfd
->symcount
- 1;
143 string_ptr
= data
->strings
;
144 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
149 if (! oasys_read_record (abfd
, &record
))
151 switch (record
.header
.type
)
153 case oasys_record_is_header_enum
:
155 case oasys_record_is_local_enum
:
156 case oasys_record_is_symbol_enum
:
158 int flag
= record
.header
.type
== (int) oasys_record_is_local_enum
?
159 (BSF_LOCAL
) : (BSF_GLOBAL
| BSF_EXPORT
);
162 size_t length
= oasys_string_length (&record
);
163 switch (record
.symbol
.relb
& RELOCATION_TYPE_BITS
)
165 case RELOCATION_TYPE_ABS
:
166 dest
= dest_defined
--;
167 dest
->section
= bfd_abs_section_ptr
;
171 case RELOCATION_TYPE_REL
:
172 dest
= dest_defined
--;
174 OASYS_DATA (abfd
)->sections
[record
.symbol
.relb
&
175 RELOCATION_SECT_BITS
];
176 if (record
.header
.type
== (int) oasys_record_is_local_enum
)
178 dest
->flags
= BSF_LOCAL
;
179 if (dest
->section
== (asection
*) (~0))
181 /* It seems that sometimes internal symbols are tied up, but
182 still get output, even though there is no
193 case RELOCATION_TYPE_UND
:
194 dest
= data
->symbols
+ bfd_h_get_16 (abfd
, record
.symbol
.refno
);
195 dest
->section
= bfd_und_section_ptr
;
197 case RELOCATION_TYPE_COM
:
198 dest
= dest_defined
--;
199 dest
->name
= string_ptr
;
200 dest
->the_bfd
= abfd
;
202 dest
->section
= bfd_com_section_ptr
;
206 dest
= dest_defined
--;
210 dest
->name
= string_ptr
;
211 dest
->the_bfd
= abfd
;
212 dest
->udata
.p
= (PTR
) NULL
;
213 dest
->value
= bfd_h_get_32 (abfd
, record
.symbol
.value
);
215 #ifdef UNDERSCORE_HACK
216 if (record
.symbol
.name
[0] != '_')
222 memcpy (string_ptr
, record
.symbol
.name
, length
);
225 string_ptr
[length
] = 0;
226 string_ptr
+= length
+ 1;
237 oasys_get_symtab_upper_bound (abfd
)
240 if (! oasys_slurp_symbol_table (abfd
))
243 return (abfd
->symcount
+ 1) * (sizeof (oasys_symbol_type
*));
246 extern const bfd_target oasys_vec
;
249 oasys_get_symtab (abfd
, location
)
254 unsigned int counter
;
255 if (oasys_slurp_symbol_table (abfd
) == false)
259 symbase
= OASYS_DATA (abfd
)->symbols
;
260 for (counter
= 0; counter
< abfd
->symcount
; counter
++)
262 *(location
++) = symbase
++;
265 return abfd
->symcount
;
268 /***********************************************************************
272 static const bfd_target
*
273 oasys_archive_p (abfd
)
276 oasys_archive_header_type header
;
277 oasys_extarchive_header_type header_ext
;
281 if (bfd_seek (abfd
, (file_ptr
) 0, false) != 0
282 || (bfd_read ((PTR
) & header_ext
, 1, sizeof (header_ext
), abfd
)
283 != sizeof (header_ext
)))
285 if (bfd_get_error () != bfd_error_system_call
)
286 bfd_set_error (bfd_error_wrong_format
);
290 header
.version
= bfd_h_get_32 (abfd
, header_ext
.version
);
291 header
.mod_count
= bfd_h_get_32 (abfd
, header_ext
.mod_count
);
292 header
.mod_tbl_offset
= bfd_h_get_32 (abfd
, header_ext
.mod_tbl_offset
);
293 header
.sym_tbl_size
= bfd_h_get_32 (abfd
, header_ext
.sym_tbl_size
);
294 header
.sym_count
= bfd_h_get_32 (abfd
, header_ext
.sym_count
);
295 header
.sym_tbl_offset
= bfd_h_get_32 (abfd
, header_ext
.sym_tbl_offset
);
296 header
.xref_count
= bfd_h_get_32 (abfd
, header_ext
.xref_count
);
297 header
.xref_lst_offset
= bfd_h_get_32 (abfd
, header_ext
.xref_lst_offset
);
300 There isn't a magic number in an Oasys archive, so the best we
301 can do to verify reasnableness is to make sure that the values in
302 the header are too weird
305 if (header
.version
> 10000 ||
306 header
.mod_count
> 10000 ||
307 header
.sym_count
> 100000 ||
308 header
.xref_count
> 100000)
309 return (const bfd_target
*) NULL
;
312 That all worked, let's buy the space for the header and read in
316 oasys_ar_data_type
*ar
=
317 (oasys_ar_data_type
*) bfd_alloc (abfd
, sizeof (oasys_ar_data_type
));
319 oasys_module_info_type
*module
=
320 (oasys_module_info_type
*)
321 bfd_alloc (abfd
, sizeof (oasys_module_info_type
) * header
.mod_count
);
322 oasys_module_table_type record
;
327 abfd
->tdata
.oasys_ar_data
= ar
;
329 ar
->module_count
= header
.mod_count
;
331 filepos
= header
.mod_tbl_offset
;
332 for (i
= 0; i
< header
.mod_count
; i
++)
334 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
337 /* There are two ways of specifying the archive header */
341 oasys_extmodule_table_type_a_type record_ext
;
342 if (bfd_read ((PTR
) & record_ext
, 1, sizeof (record_ext
), abfd
)
343 != sizeof (record_ext
))
346 record
.mod_size
= bfd_h_get_32 (abfd
, record_ext
.mod_size
);
347 record
.file_offset
= bfd_h_get_32 (abfd
, record_ext
.file_offset
);
349 record
.dep_count
= bfd_h_get_32 (abfd
, record_ext
.dep_count
);
350 record
.depee_count
= bfd_h_get_32 (abfd
, record_ext
.depee_count
);
351 record
.sect_count
= bfd_h_get_32 (abfd
, record_ext
.sect_count
);
353 module
[i
].name
= bfd_alloc (abfd
, 33);
357 memcpy (module
[i
].name
, record_ext
.mod_name
, 33);
359 sizeof (record_ext
) +
360 record
.dep_count
* 4 +
361 record
.depee_count
* 4 +
362 record
.sect_count
* 8 + 187;
366 oasys_extmodule_table_type_b_type record_ext
;
367 if (bfd_read ((PTR
) & record_ext
, 1, sizeof (record_ext
), abfd
)
368 != sizeof (record_ext
))
371 record
.mod_size
= bfd_h_get_32 (abfd
, record_ext
.mod_size
);
372 record
.file_offset
= bfd_h_get_32 (abfd
, record_ext
.file_offset
);
374 record
.dep_count
= bfd_h_get_32 (abfd
, record_ext
.dep_count
);
375 record
.depee_count
= bfd_h_get_32 (abfd
, record_ext
.depee_count
);
376 record
.sect_count
= bfd_h_get_32 (abfd
, record_ext
.sect_count
);
377 record
.module_name_size
= bfd_h_get_32 (abfd
, record_ext
.mod_name_length
);
379 module
[i
].name
= bfd_alloc (abfd
, record
.module_name_size
+ 1);
382 if (bfd_read ((PTR
) module
[i
].name
, 1, record
.module_name_size
,
384 != record
.module_name_size
)
386 module
[i
].name
[record
.module_name_size
] = 0;
388 sizeof (record_ext
) +
389 record
.dep_count
* 4 +
390 record
.module_name_size
+ 1;
395 module
[i
].size
= record
.mod_size
;
396 module
[i
].pos
= record
.file_offset
;
405 oasys_mkobject (abfd
)
409 abfd
->tdata
.oasys_obj_data
= (oasys_data_type
*) bfd_alloc (abfd
, sizeof (oasys_data_type
));
410 return abfd
->tdata
.oasys_obj_data
? true : false;
414 static const bfd_target
*
415 oasys_object_p (abfd
)
418 oasys_data_type
*oasys
;
419 oasys_data_type
*save
= OASYS_DATA (abfd
);
421 boolean had_usefull
= false;
423 abfd
->tdata
.oasys_obj_data
= 0;
424 oasys_mkobject (abfd
);
425 oasys
= OASYS_DATA (abfd
);
426 memset ((PTR
) oasys
->sections
, 0xff, sizeof (oasys
->sections
));
428 /* Point to the start of the file */
429 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
431 oasys
->symbol_string_length
= 0;
432 /* Inspect the records, but only keep the section info -
433 remember the size of the symbols
435 oasys
->first_data_record
= 0;
438 oasys_record_union_type record
;
439 if (! oasys_read_record (abfd
, &record
))
441 if ((size_t) record
.header
.length
< (size_t) sizeof (record
.header
))
445 switch ((oasys_record_enum_type
) (record
.header
.type
))
447 case oasys_record_is_header_enum
:
450 case oasys_record_is_symbol_enum
:
451 case oasys_record_is_local_enum
:
452 /* Count symbols and remember their size for a future malloc */
454 oasys
->symbol_string_length
+= 1 + oasys_string_length (&record
);
457 case oasys_record_is_section_enum
:
461 unsigned int section_number
;
462 if (record
.section
.header
.length
!= sizeof (record
.section
))
466 buffer
= bfd_alloc (abfd
, 3);
469 section_number
= record
.section
.relb
& RELOCATION_SECT_BITS
;
470 sprintf (buffer
, "%u", section_number
);
471 s
= bfd_make_section (abfd
, buffer
);
472 oasys
->sections
[section_number
] = s
;
473 switch (record
.section
.relb
& RELOCATION_TYPE_BITS
)
475 case RELOCATION_TYPE_ABS
:
476 case RELOCATION_TYPE_REL
:
478 case RELOCATION_TYPE_UND
:
479 case RELOCATION_TYPE_COM
:
483 s
->_raw_size
= bfd_h_get_32 (abfd
, record
.section
.value
);
484 s
->vma
= bfd_h_get_32 (abfd
, record
.section
.vma
);
489 case oasys_record_is_data_enum
:
490 oasys
->first_data_record
= bfd_tell (abfd
) - record
.header
.length
;
491 case oasys_record_is_debug_enum
:
492 case oasys_record_is_module_enum
:
493 case oasys_record_is_named_section_enum
:
494 case oasys_record_is_end_enum
:
495 if (had_usefull
== false)
503 oasys
->symbols
= (asymbol
*) NULL
;
505 Oasys support several architectures, but I can't see a simple way
506 to discover which one is in a particular file - we'll guess
508 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
509 if (abfd
->symcount
!= 0)
511 abfd
->flags
|= HAS_SYMS
;
515 We don't know if a section has data until we've read it..
518 oasys_slurp_section_data (abfd
);
524 (void) bfd_release (abfd
, oasys
);
525 abfd
->tdata
.oasys_obj_data
= save
;
526 return (const bfd_target
*) NULL
;
531 oasys_get_symbol_info (ignore_abfd
, symbol
, ret
)
532 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
536 bfd_symbol_info (symbol
, ret
);
537 if (!symbol
->section
)
538 ret
->type
= (symbol
->flags
& BSF_LOCAL
) ? 'a' : 'A';
542 oasys_print_symbol (abfd
, afile
, symbol
, how
)
546 bfd_print_symbol_type how
;
548 FILE *file
= (FILE *) afile
;
552 case bfd_print_symbol_name
:
553 case bfd_print_symbol_more
:
554 fprintf (file
, "%s", symbol
->name
);
556 case bfd_print_symbol_all
:
558 CONST
char *section_name
= symbol
->section
== (asection
*) NULL
?
559 (CONST
char *) "*abs" : symbol
->section
->name
;
561 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
563 fprintf (file
, " %-5s %s",
571 The howto table is build using the top two bits of a reloc byte to
572 index into it. The bits are PCREL,WORD/LONG
574 static reloc_howto_type howto_table
[] =
577 HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
578 HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "abs32", true, 0xffffffff, 0xffffffff, false),
579 HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false),
580 HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false)
583 /* Read in all the section data and relocation stuff too */
585 oasys_slurp_section_data (abfd
)
588 oasys_record_union_type record
;
589 oasys_data_type
*data
= OASYS_DATA (abfd
);
592 oasys_per_section_type
*per
;
596 /* See if the data has been slurped already .. */
597 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
599 per
= oasys_per_section (s
);
600 if (per
->initialized
== true)
604 if (data
->first_data_record
== 0)
607 if (bfd_seek (abfd
, data
->first_data_record
, SEEK_SET
) != 0)
611 if (! oasys_read_record (abfd
, &record
))
613 switch (record
.header
.type
)
615 case oasys_record_is_header_enum
:
617 case oasys_record_is_data_enum
:
620 bfd_byte
*src
= record
.data
.data
;
621 bfd_byte
*end_src
= ((bfd_byte
*) & record
) + record
.header
.length
;
623 bfd_byte
*dst_base_ptr
;
627 data
->sections
[record
.data
.relb
& RELOCATION_SECT_BITS
];
630 per
= oasys_per_section (section
);
632 if (per
->initialized
== false)
634 per
->data
= (bfd_byte
*) bfd_zalloc (abfd
, section
->_raw_size
);
637 per
->reloc_tail_ptr
= (oasys_reloc_type
**) & (section
->relocation
);
638 per
->had_vma
= false;
639 per
->initialized
= true;
640 section
->reloc_count
= 0;
641 section
->flags
= SEC_ALLOC
;
644 dst_offset
= bfd_h_get_32 (abfd
, record
.data
.addr
);
645 if (per
->had_vma
== false)
647 /* Take the first vma we see as the base */
648 section
->vma
= dst_offset
;
652 dst_offset
-= section
->vma
;
654 dst_base_ptr
= oasys_per_section (section
)->data
;
655 dst_ptr
= oasys_per_section (section
)->data
+
660 section
->flags
|= SEC_LOAD
| SEC_HAS_CONTENTS
;
662 while (src
< end_src
)
664 unsigned char mod_byte
= *src
++;
665 size_t gap
= end_src
- src
;
668 if (mod_byte
== 0 && gap
>= 8)
683 for (relbit
= 1; count
-- != 0 && src
< end_src
; relbit
<<= 1)
685 if (relbit
& mod_byte
)
687 unsigned char reloc
= *src
;
688 /* This item needs to be relocated */
689 switch (reloc
& RELOCATION_TYPE_BITS
)
691 case RELOCATION_TYPE_ABS
:
695 case RELOCATION_TYPE_REL
:
697 /* Relocate the item relative to the section */
698 oasys_reloc_type
*r
=
701 sizeof (oasys_reloc_type
));
704 *(per
->reloc_tail_ptr
) = r
;
705 per
->reloc_tail_ptr
= &r
->next
;
706 r
->next
= (oasys_reloc_type
*) NULL
;
707 /* Reference to undefined symbol */
709 /* There is no symbol */
711 /* Work out the howto */
715 data
->sections
[reloc
&
716 RELOCATION_SECT_BITS
];
719 r
->relent
.section
->vma
;
721 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
722 r
->relent
.howto
= &howto_table
[reloc
>> 6];
723 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
724 section
->reloc_count
++;
726 /* Fake up the data to look like it's got the -ve pc in it, this makes
727 it much easier to convert into other formats. This is done by
730 if (r
->relent
.howto
->pc_relative
== true)
732 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
740 case RELOCATION_TYPE_UND
:
742 oasys_reloc_type
*r
=
745 sizeof (oasys_reloc_type
));
748 *(per
->reloc_tail_ptr
) = r
;
749 per
->reloc_tail_ptr
= &r
->next
;
750 r
->next
= (oasys_reloc_type
*) NULL
;
751 /* Reference to undefined symbol */
753 /* Get symbol number */
754 r
->symbol
= (src
[0] << 8) | src
[1];
755 /* Work out the howto */
759 r
->relent
.section
= (asection
762 r
->relent
.addend
= 0;
763 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
764 r
->relent
.howto
= &howto_table
[reloc
>> 6];
765 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
766 section
->reloc_count
++;
769 /* Fake up the data to look like it's got the -ve pc in it, this makes
770 it much easier to convert into other formats. This is done by
773 if (r
->relent
.howto
->pc_relative
== true)
775 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
782 case RELOCATION_TYPE_COM
:
792 case oasys_record_is_local_enum
:
793 case oasys_record_is_symbol_enum
:
794 case oasys_record_is_section_enum
:
806 oasys_new_section_hook (abfd
, newsect
)
810 newsect
->used_by_bfd
= (PTR
)
811 bfd_alloc (abfd
, sizeof (oasys_per_section_type
));
812 if (!newsect
->used_by_bfd
)
814 oasys_per_section (newsect
)->data
= (bfd_byte
*) NULL
;
815 oasys_per_section (newsect
)->section
= newsect
;
816 oasys_per_section (newsect
)->offset
= 0;
817 oasys_per_section (newsect
)->initialized
= false;
818 newsect
->alignment_power
= 1;
819 /* Turn the section string into an index */
821 sscanf (newsect
->name
, "%u", &newsect
->target_index
);
828 oasys_get_reloc_upper_bound (abfd
, asect
)
832 if (! oasys_slurp_section_data (abfd
))
834 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
838 oasys_get_section_contents (abfd
, section
, location
, offset
, count
)
845 oasys_per_section_type
*p
= (oasys_per_section_type
*) section
->used_by_bfd
;
846 oasys_slurp_section_data (abfd
);
847 if (p
->initialized
== false)
849 (void) memset (location
, 0, (int) count
);
853 (void) memcpy (location
, (PTR
) (p
->data
+ offset
), (int) count
);
860 oasys_canonicalize_reloc (ignore_abfd
, section
, relptr
, symbols
)
861 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
864 asymbol
**symbols ATTRIBUTE_UNUSED
;
866 unsigned int reloc_count
= 0;
867 oasys_reloc_type
*src
= (oasys_reloc_type
*) (section
->relocation
);
868 while (src
!= (oasys_reloc_type
*) NULL
)
873 if (src
->relent
.section
== (asection
*) NULL
)
875 src
->relent
.sym_ptr_ptr
= symbols
+ src
->symbol
;
879 *relptr
++ = &src
->relent
;
883 *relptr
= (arelent
*) NULL
;
884 return section
->reloc_count
= reloc_count
;
893 /* Calculate the checksum and write one record */
895 oasys_write_record (abfd
, type
, record
, size
)
897 oasys_record_enum_type type
;
898 oasys_record_union_type
*record
;
905 record
->header
.length
= size
;
906 record
->header
.type
= (int) type
;
907 record
->header
.check_sum
= 0;
908 record
->header
.fill
= 0;
909 ptr
= (unsigned char *) &record
->pad
[0];
911 for (i
= 0; i
< size
; i
++)
915 record
->header
.check_sum
= 0xff & (-checksum
);
916 if (bfd_write ((PTR
) record
, 1, size
, abfd
) != size
)
922 /* Write out all the symbols */
924 oasys_write_syms (abfd
)
928 asymbol
**generic
= bfd_get_outsymbols (abfd
);
929 unsigned int index
= 0;
930 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
933 oasys_symbol_record_type symbol
;
934 asymbol
*CONST g
= generic
[count
];
936 CONST
char *src
= g
->name
;
937 char *dst
= symbol
.name
;
940 if (bfd_is_com_section (g
->section
))
942 symbol
.relb
= RELOCATION_TYPE_COM
;
943 bfd_h_put_16 (abfd
, index
, symbol
.refno
);
946 else if (bfd_is_abs_section (g
->section
))
948 symbol
.relb
= RELOCATION_TYPE_ABS
;
949 bfd_h_put_16 (abfd
, 0, symbol
.refno
);
952 else if (bfd_is_und_section (g
->section
))
954 symbol
.relb
= RELOCATION_TYPE_UND
;
955 bfd_h_put_16 (abfd
, index
, symbol
.refno
);
956 /* Overload the value field with the output index number */
959 else if (g
->flags
& BSF_DEBUGGING
)
966 if (g
->section
== (asection
*) NULL
)
968 /* Sometime, the oasys tools give out a symbol with illegal
969 bits in it, we'll output it in the same broken way */
971 symbol
.relb
= RELOCATION_TYPE_REL
| 0;
975 symbol
.relb
= RELOCATION_TYPE_REL
| g
->section
->output_section
->target_index
;
977 bfd_h_put_16 (abfd
, 0, symbol
.refno
);
979 #ifdef UNDERSCORE_HACK
989 bfd_h_put_32 (abfd
, g
->value
, symbol
.value
);
992 if (g
->flags
& BSF_LOCAL
)
994 if (! oasys_write_record (abfd
,
995 oasys_record_is_local_enum
,
996 (oasys_record_union_type
*) & symbol
,
997 offsetof (oasys_symbol_record_type
,
1003 if (! oasys_write_record (abfd
,
1004 oasys_record_is_symbol_enum
,
1005 (oasys_record_union_type
*) & symbol
,
1006 offsetof (oasys_symbol_record_type
,
1010 g
->value
= index
- 1;
1017 /* Write a section header for each section */
1019 oasys_write_sections (abfd
)
1023 static oasys_section_record_type out
;
1025 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1027 if (!isdigit ((unsigned char) s
->name
[0]))
1029 (*_bfd_error_handler
)
1030 (_("%s: can not represent section `%s' in oasys"),
1031 bfd_get_filename (abfd
), s
->name
);
1032 bfd_set_error (bfd_error_nonrepresentable_section
);
1035 out
.relb
= RELOCATION_TYPE_REL
| s
->target_index
;
1036 bfd_h_put_32 (abfd
, s
->_cooked_size
, out
.value
);
1037 bfd_h_put_32 (abfd
, s
->vma
, out
.vma
);
1039 if (! oasys_write_record (abfd
,
1040 oasys_record_is_section_enum
,
1041 (oasys_record_union_type
*) & out
,
1049 oasys_write_header (abfd
)
1052 /* Create and write the header */
1053 oasys_header_record_type r
;
1054 size_t length
= strlen (abfd
->filename
);
1055 if (length
> (size_t) sizeof (r
.module_name
))
1057 length
= sizeof (r
.module_name
);
1060 (void) memcpy (r
.module_name
,
1063 (void) memset (r
.module_name
+ length
,
1065 sizeof (r
.module_name
) - length
);
1067 r
.version_number
= OASYS_VERSION_NUMBER
;
1068 r
.rev_number
= OASYS_REV_NUMBER
;
1069 if (! oasys_write_record (abfd
,
1070 oasys_record_is_header_enum
,
1071 (oasys_record_union_type
*) & r
,
1072 offsetof (oasys_header_record_type
,
1080 oasys_write_end (abfd
)
1083 oasys_end_record_type end
;
1084 unsigned char null
= 0;
1085 end
.relb
= RELOCATION_TYPE_ABS
;
1086 bfd_h_put_32 (abfd
, abfd
->start_address
, end
.entry
);
1087 bfd_h_put_16 (abfd
, 0, end
.fill
);
1089 if (! oasys_write_record (abfd
,
1090 oasys_record_is_end_enum
,
1091 (oasys_record_union_type
*) & end
,
1094 if (bfd_write ((PTR
) & null
, 1, 1, abfd
) != 1)
1104 arelent
*a
= *((arelent
**) ap
);
1105 arelent
*b
= *((arelent
**) bp
);
1106 return a
->address
- b
->address
;
1114 oasys_write_data (abfd
)
1118 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1120 if (s
->flags
& SEC_LOAD
)
1122 bfd_byte
*raw_data
= oasys_per_section (s
)->data
;
1123 oasys_data_record_type processed_data
;
1124 bfd_size_type current_byte_index
= 0;
1125 unsigned int relocs_to_go
= s
->reloc_count
;
1126 arelent
**p
= s
->orelocation
;
1127 if (s
->reloc_count
!= 0)
1129 /* Sort the reloc records so it's easy to insert the relocs into the
1132 qsort (s
->orelocation
,
1134 sizeof (arelent
**),
1137 current_byte_index
= 0;
1138 processed_data
.relb
= s
->target_index
| RELOCATION_TYPE_REL
;
1140 while (current_byte_index
< s
->_cooked_size
)
1142 /* Scan forwards by eight bytes or however much is left and see if
1143 there are any relocations going on */
1144 bfd_byte
*mod
= &processed_data
.data
[0];
1145 bfd_byte
*dst
= &processed_data
.data
[1];
1151 bfd_h_put_32 (abfd
, s
->vma
+ current_byte_index
,
1152 processed_data
.addr
);
1154 /* Don't start a relocation unless you're sure you can finish it
1155 within the same data record. The worst case relocation is a
1156 4-byte relocatable value which is split across two modification
1157 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1158 1 modification byte + 2 data = 8 bytes total). That's where
1159 the magic number 8 comes from.
1161 while (current_byte_index
< s
->_raw_size
&& dst
<=
1162 &processed_data
.data
[sizeof (processed_data
.data
) - 8])
1166 if (relocs_to_go
!= 0)
1169 reloc_howto_type
*const how
= r
->howto
;
1170 /* There is a relocation, is it for this byte ? */
1171 if (r
->address
== current_byte_index
)
1173 unsigned char rel_byte
;
1179 if (how
->pc_relative
)
1181 rel_byte
= RELOCATION_PCREL_BIT
;
1183 /* Also patch the raw data so that it doesn't have
1184 the -ve stuff any more */
1188 bfd_get_16 (abfd
, raw_data
) +
1189 current_byte_index
, raw_data
);
1195 bfd_get_32 (abfd
, raw_data
) +
1196 current_byte_index
, raw_data
);
1205 rel_byte
|= RELOCATION_32BIT_BIT
;
1208 /* Is this a section relative relocation, or a symbol
1209 relative relocation ? */
1213 if (r
->section
!= (asection
*) NULL
)
1215 /* The relent has a section attached, so it must be section
1217 rel_byte
|= RELOCATION_TYPE_REL
;
1218 rel_byte
|= r
->section
->output_section
->target_index
;
1224 asymbol
*p
= *(r
->sym_ptr_ptr
);
1226 /* If this symbol has a section attached, then it
1227 has already been resolved. Change from a symbol
1228 ref to a section ref */
1229 if (p
->section
!= (asection
*) NULL
)
1231 rel_byte
|= RELOCATION_TYPE_REL
;
1233 p
->section
->output_section
->target_index
;
1238 rel_byte
|= RELOCATION_TYPE_UND
;
1240 /* Next two bytes are a symbol index - we can get
1241 this from the symbol value which has been zapped
1242 into the symbol index in the table when the
1243 symbol table was written
1245 *dst
++ = p
->value
>> 8;
1249 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1250 /* relocations never occur from an unloadable section,
1251 so we can assume that raw_data is not NULL
1253 *dst
++ = *raw_data
++;
1255 * dst
++ = *raw_data
++;
1259 *dst
++ = *raw_data
++;
1261 * dst
++ = *raw_data
++;
1267 /* If this is coming from an unloadable section then copy
1269 if (raw_data
== NULL
)
1275 *dst
++ = *raw_data
++;
1280 /* Don't write a useless null modification byte */
1286 if (! oasys_write_record (abfd
,
1287 oasys_record_is_data_enum
,
1288 ((oasys_record_union_type
*)
1290 dst
- (bfd_byte
*) & processed_data
))
1300 oasys_write_object_contents (abfd
)
1303 if (! oasys_write_header (abfd
))
1305 if (! oasys_write_syms (abfd
))
1307 if (! oasys_write_sections (abfd
))
1309 if (! oasys_write_data (abfd
))
1311 if (! oasys_write_end (abfd
))
1319 /** exec and core file sections */
1321 /* set section contents is complicated with OASYS since the format is
1322 * not a byte image, but a record stream.
1325 oasys_set_section_contents (abfd
, section
, location
, offset
, count
)
1330 bfd_size_type count
;
1334 if (oasys_per_section (section
)->data
== (bfd_byte
*) NULL
)
1336 oasys_per_section (section
)->data
=
1337 (bfd_byte
*) (bfd_alloc (abfd
, section
->_cooked_size
));
1338 if (!oasys_per_section (section
)->data
)
1341 (void) memcpy ((PTR
) (oasys_per_section (section
)->data
+ offset
),
1350 /* Native-level interface to symbols. */
1352 /* We read the symbols into a buffer, which is discarded when this
1353 function exits. We read the strings into a buffer large enough to
1354 hold them all plus all the cached symbol entries. */
1357 oasys_make_empty_symbol (abfd
)
1361 oasys_symbol_type
*new =
1362 (oasys_symbol_type
*) bfd_zalloc (abfd
, sizeof (oasys_symbol_type
));
1365 new->symbol
.the_bfd
= abfd
;
1366 return &new->symbol
;
1372 /* User should have checked the file flags; perhaps we should return
1373 BFD_NO_MORE_SYMBOLS if there are none? */
1376 oasys_openr_next_archived_file (arch
, prev
)
1380 oasys_ar_data_type
*ar
= OASYS_AR_DATA (arch
);
1381 oasys_module_info_type
*p
;
1382 /* take the next one from the arch state, or reset */
1383 if (prev
== (bfd
*) NULL
)
1385 /* Reset the index - the first two entries are bogus*/
1386 ar
->module_index
= 0;
1389 p
= ar
->module
+ ar
->module_index
;
1392 if (ar
->module_index
<= ar
->module_count
)
1394 if (p
->abfd
== (bfd
*) NULL
)
1396 p
->abfd
= _bfd_create_empty_archive_element_shell (arch
);
1397 p
->abfd
->origin
= p
->pos
;
1398 p
->abfd
->filename
= p
->name
;
1400 /* Fixup a pointer to this element for the member */
1401 p
->abfd
->arelt_data
= (PTR
) p
;
1407 bfd_set_error (bfd_error_no_more_archived_files
);
1408 return (bfd
*) NULL
;
1413 oasys_find_nearest_line (abfd
,
1420 bfd
*abfd ATTRIBUTE_UNUSED
;
1421 asection
*section ATTRIBUTE_UNUSED
;
1422 asymbol
**symbols ATTRIBUTE_UNUSED
;
1423 bfd_vma offset ATTRIBUTE_UNUSED
;
1424 char **filename_ptr ATTRIBUTE_UNUSED
;
1425 char **functionname_ptr ATTRIBUTE_UNUSED
;
1426 unsigned int *line_ptr ATTRIBUTE_UNUSED
;
1433 oasys_generic_stat_arch_elt (abfd
, buf
)
1437 oasys_module_info_type
*mod
= (oasys_module_info_type
*) abfd
->arelt_data
;
1438 if (mod
== (oasys_module_info_type
*) NULL
)
1440 bfd_set_error (bfd_error_invalid_operation
);
1445 buf
->st_size
= mod
->size
;
1446 buf
->st_mode
= 0666;
1452 oasys_sizeof_headers (abfd
, exec
)
1453 bfd
*abfd ATTRIBUTE_UNUSED
;
1454 boolean exec ATTRIBUTE_UNUSED
;
1459 #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup
1460 #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1462 #define oasys_slurp_armap bfd_true
1463 #define oasys_slurp_extended_name_table bfd_true
1464 #define oasys_construct_extended_name_table \
1465 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1467 #define oasys_truncate_arname bfd_dont_truncate_arname
1468 #define oasys_write_armap \
1470 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1472 #define oasys_read_ar_hdr bfd_nullvoidptr
1473 #define oasys_get_elt_at_index _bfd_generic_get_elt_at_index
1474 #define oasys_update_armap_timestamp bfd_true
1476 #define oasys_bfd_is_local_label_name bfd_generic_is_local_label_name
1477 #define oasys_get_lineno _bfd_nosymbols_get_lineno
1478 #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1479 #define oasys_read_minisymbols _bfd_generic_read_minisymbols
1480 #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1482 #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1484 #define oasys_set_arch_mach bfd_default_set_arch_mach
1486 #define oasys_get_section_contents_in_window \
1487 _bfd_generic_get_section_contents_in_window
1489 #define oasys_bfd_get_relocated_section_contents \
1490 bfd_generic_get_relocated_section_contents
1491 #define oasys_bfd_relax_section bfd_generic_relax_section
1492 #define oasys_bfd_gc_sections bfd_generic_gc_sections
1493 #define oasys_bfd_merge_sections bfd_generic_merge_sections
1494 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1495 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1496 #define oasys_bfd_final_link _bfd_generic_final_link
1497 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1500 const bfd_target oasys_vec
=
1503 bfd_target_oasys_flavour
,
1504 BFD_ENDIAN_BIG
, /* target byte order */
1505 BFD_ENDIAN_BIG
, /* target headers byte order */
1506 (HAS_RELOC
| EXEC_P
| /* object flags */
1507 HAS_LINENO
| HAS_DEBUG
|
1508 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
1509 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1510 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1511 0, /* leading underscore */
1512 ' ', /* ar_pad_char */
1513 16, /* ar_max_namelen */
1514 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1515 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1516 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1517 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1518 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1519 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1522 oasys_object_p
, /* bfd_check_format */
1526 { /* bfd_set_format */
1529 _bfd_generic_mkarchive
,
1532 { /* bfd_write_contents */
1534 oasys_write_object_contents
,
1535 _bfd_write_archive_contents
,
1539 BFD_JUMP_TABLE_GENERIC (oasys
),
1540 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1541 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1542 BFD_JUMP_TABLE_ARCHIVE (oasys
),
1543 BFD_JUMP_TABLE_SYMBOLS (oasys
),
1544 BFD_JUMP_TABLE_RELOCS (oasys
),
1545 BFD_JUMP_TABLE_WRITE (oasys
),
1546 BFD_JUMP_TABLE_LINK (oasys
),
1547 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),