1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2002
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
25 #include "safe-ctype.h"
30 static boolean oasys_slurp_section_data
PARAMS ((bfd
* const));
31 static boolean oasys_read_record
PARAMS ((bfd
*, oasys_record_union_type
*));
32 static boolean oasys_write_sections
PARAMS ((bfd
*));
33 static boolean oasys_write_record
34 PARAMS ((bfd
*, oasys_record_enum_type
, oasys_record_union_type
*, size_t));
35 static boolean oasys_write_syms
PARAMS ((bfd
*));
36 static boolean oasys_write_header
PARAMS ((bfd
*));
37 static boolean oasys_write_end
PARAMS ((bfd
*));
38 static boolean oasys_write_data
PARAMS ((bfd
*));
39 static size_t oasys_string_length
PARAMS ((oasys_record_union_type
*));
40 static boolean oasys_slurp_symbol_table
PARAMS ((bfd
*const));
41 static long int oasys_get_symtab_upper_bound
PARAMS ((bfd
*const));
42 static const bfd_target
*oasys_archive_p
PARAMS ((bfd
*));
43 static boolean oasys_mkobject
PARAMS ((bfd
*));
44 static const bfd_target
*oasys_object_p
PARAMS ((bfd
*));
45 static void oasys_get_symbol_info
PARAMS ((bfd
*, asymbol
*, symbol_info
*));
46 static void oasys_print_symbol
47 PARAMS ((bfd
*, void *, asymbol
*, bfd_print_symbol_type
));
48 static boolean oasys_new_section_hook
PARAMS ((bfd
*, asection
*));
49 static long int oasys_get_reloc_upper_bound
PARAMS ((bfd
*, sec_ptr
));
50 static boolean oasys_get_section_contents
51 PARAMS ((bfd
*, sec_ptr
, void *, file_ptr
, bfd_size_type
));
52 static int comp
PARAMS ((const void *, const void *));
53 static boolean oasys_write_object_contents
PARAMS ((bfd
*));
54 static boolean oasys_set_section_contents
55 PARAMS ((bfd
*, sec_ptr
, void *, file_ptr
, bfd_size_type
));
56 static asymbol
*oasys_make_empty_symbol
PARAMS ((bfd
*));
57 static bfd
*oasys_openr_next_archived_file
PARAMS ((bfd
*, bfd
*));
58 static boolean oasys_find_nearest_line
59 PARAMS ((bfd
*, asection
*, asymbol
**, bfd_vma
,
60 const char **, const 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
66 PARAMS ((bfd
*, sec_ptr
, arelent
**, asymbol
**));
68 /* Read in all the section data and relocation stuff too. */
71 oasys_read_record (abfd
, record
)
73 oasys_record_union_type
*record
;
75 bfd_size_type amt
= sizeof (record
->header
);
76 if (bfd_bread ((PTR
) record
, amt
, abfd
) != amt
)
79 amt
= record
->header
.length
- sizeof (record
->header
);
82 if (bfd_bread ((PTR
) ((char *) record
+ sizeof (record
->header
)), amt
, abfd
)
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 */
129 amt
= abfd
->symcount
;
130 amt
*= sizeof (asymbol
);
131 data
->symbols
= (asymbol
*) bfd_alloc (abfd
, amt
);
133 amt
= data
->symbol_string_length
;
134 #ifdef UNDERSCORE_HACK
135 /* buy 1 more char for each symbol to keep the underscore in*/
136 amt
+= abfd
->symcount
;
138 data
->strings
= bfd_alloc (abfd
, amt
);
140 if (!data
->symbols
|| !data
->strings
)
143 dest_defined
= data
->symbols
+ abfd
->symcount
- 1;
145 string_ptr
= data
->strings
;
146 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
151 if (! oasys_read_record (abfd
, &record
))
153 switch (record
.header
.type
)
155 case oasys_record_is_header_enum
:
157 case oasys_record_is_local_enum
:
158 case oasys_record_is_symbol_enum
:
160 int flag
= record
.header
.type
== (int) oasys_record_is_local_enum
?
161 (BSF_LOCAL
) : (BSF_GLOBAL
| BSF_EXPORT
);
164 size_t length
= oasys_string_length (&record
);
165 switch (record
.symbol
.relb
& RELOCATION_TYPE_BITS
)
167 case RELOCATION_TYPE_ABS
:
168 dest
= dest_defined
--;
169 dest
->section
= bfd_abs_section_ptr
;
173 case RELOCATION_TYPE_REL
:
174 dest
= dest_defined
--;
176 OASYS_DATA (abfd
)->sections
[record
.symbol
.relb
&
177 RELOCATION_SECT_BITS
];
178 if (record
.header
.type
== (int) oasys_record_is_local_enum
)
180 dest
->flags
= BSF_LOCAL
;
181 if (dest
->section
== (asection
*) (~0))
183 /* It seems that sometimes internal symbols are tied up, but
184 still get output, even though there is no
195 case RELOCATION_TYPE_UND
:
196 dest
= data
->symbols
+ H_GET_16 (abfd
, record
.symbol
.refno
);
197 dest
->section
= bfd_und_section_ptr
;
199 case RELOCATION_TYPE_COM
:
200 dest
= dest_defined
--;
201 dest
->name
= string_ptr
;
202 dest
->the_bfd
= abfd
;
204 dest
->section
= bfd_com_section_ptr
;
208 dest
= dest_defined
--;
212 dest
->name
= string_ptr
;
213 dest
->the_bfd
= abfd
;
214 dest
->udata
.p
= (PTR
) NULL
;
215 dest
->value
= H_GET_32 (abfd
, record
.symbol
.value
);
217 #ifdef UNDERSCORE_HACK
218 if (record
.symbol
.name
[0] != '_')
224 memcpy (string_ptr
, record
.symbol
.name
, length
);
227 string_ptr
[length
] = 0;
228 string_ptr
+= length
+ 1;
239 oasys_get_symtab_upper_bound (abfd
)
242 if (! oasys_slurp_symbol_table (abfd
))
245 return (abfd
->symcount
+ 1) * (sizeof (oasys_symbol_type
*));
248 extern const bfd_target oasys_vec
;
251 oasys_get_symtab (abfd
, location
)
256 unsigned int counter
;
257 if (! oasys_slurp_symbol_table (abfd
))
261 symbase
= OASYS_DATA (abfd
)->symbols
;
262 for (counter
= 0; counter
< abfd
->symcount
; counter
++)
264 *(location
++) = symbase
++;
267 return abfd
->symcount
;
270 /***********************************************************************
274 static const bfd_target
*
275 oasys_archive_p (abfd
)
278 oasys_archive_header_type header
;
279 oasys_extarchive_header_type header_ext
;
284 amt
= sizeof (header_ext
);
285 if (bfd_seek (abfd
, (file_ptr
) 0, 0) != 0
286 || bfd_bread ((PTR
) &header_ext
, amt
, abfd
) != amt
)
288 if (bfd_get_error () != bfd_error_system_call
)
289 bfd_set_error (bfd_error_wrong_format
);
293 header
.version
= H_GET_32 (abfd
, header_ext
.version
);
294 header
.mod_count
= H_GET_32 (abfd
, header_ext
.mod_count
);
295 header
.mod_tbl_offset
= H_GET_32 (abfd
, header_ext
.mod_tbl_offset
);
296 header
.sym_tbl_size
= H_GET_32 (abfd
, header_ext
.sym_tbl_size
);
297 header
.sym_count
= H_GET_32 (abfd
, header_ext
.sym_count
);
298 header
.sym_tbl_offset
= H_GET_32 (abfd
, header_ext
.sym_tbl_offset
);
299 header
.xref_count
= H_GET_32 (abfd
, header_ext
.xref_count
);
300 header
.xref_lst_offset
= H_GET_32 (abfd
, header_ext
.xref_lst_offset
);
303 There isn't a magic number in an Oasys archive, so the best we
304 can do to verify reasnableness is to make sure that the values in
305 the header are too weird
308 if (header
.version
> 10000 ||
309 header
.mod_count
> 10000 ||
310 header
.sym_count
> 100000 ||
311 header
.xref_count
> 100000)
312 return (const bfd_target
*) NULL
;
315 That all worked, let's buy the space for the header and read in
319 oasys_ar_data_type
*ar
;
320 oasys_module_info_type
*module
;
321 oasys_module_table_type record
;
323 amt
= sizeof (oasys_ar_data_type
);
324 ar
= (oasys_ar_data_type
*) bfd_alloc (abfd
, amt
);
326 amt
= header
.mod_count
;
327 amt
*= sizeof (oasys_module_info_type
);
328 module
= (oasys_module_info_type
*) bfd_alloc (abfd
, amt
);
333 abfd
->tdata
.oasys_ar_data
= ar
;
335 ar
->module_count
= header
.mod_count
;
337 filepos
= header
.mod_tbl_offset
;
338 for (i
= 0; i
< header
.mod_count
; i
++)
340 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
343 /* There are two ways of specifying the archive header */
347 oasys_extmodule_table_type_a_type record_ext
;
349 amt
= sizeof (record_ext
);
350 if (bfd_bread ((PTR
) &record_ext
, amt
, abfd
) != amt
)
353 record
.mod_size
= H_GET_32 (abfd
, record_ext
.mod_size
);
354 record
.file_offset
= H_GET_32 (abfd
, record_ext
.file_offset
);
356 record
.dep_count
= H_GET_32 (abfd
, record_ext
.dep_count
);
357 record
.depee_count
= H_GET_32 (abfd
, record_ext
.depee_count
);
358 record
.sect_count
= H_GET_32 (abfd
, record_ext
.sect_count
);
360 module
[i
].name
= bfd_alloc (abfd
, (bfd_size_type
) 33);
364 memcpy (module
[i
].name
, record_ext
.mod_name
, 33);
366 sizeof (record_ext
) +
367 record
.dep_count
* 4 +
368 record
.depee_count
* 4 +
369 record
.sect_count
* 8 + 187;
373 oasys_extmodule_table_type_b_type record_ext
;
375 amt
= sizeof (record_ext
);
376 if (bfd_bread ((PTR
) &record_ext
, amt
, abfd
) != amt
)
379 record
.mod_size
= H_GET_32 (abfd
, record_ext
.mod_size
);
380 record
.file_offset
= H_GET_32 (abfd
, record_ext
.file_offset
);
382 record
.dep_count
= H_GET_32 (abfd
, record_ext
.dep_count
);
383 record
.depee_count
= H_GET_32 (abfd
, record_ext
.depee_count
);
384 record
.sect_count
= H_GET_32 (abfd
, record_ext
.sect_count
);
385 record
.module_name_size
= H_GET_32 (abfd
,
386 record_ext
.mod_name_length
);
388 amt
= record
.module_name_size
;
389 module
[i
].name
= bfd_alloc (abfd
, amt
+ 1);
392 if (bfd_bread ((PTR
) module
[i
].name
, amt
, abfd
) != amt
)
394 module
[i
].name
[record
.module_name_size
] = 0;
395 filepos
+= (sizeof (record_ext
)
396 + record
.dep_count
* 4
397 + record
.module_name_size
+ 1);
400 module
[i
].size
= record
.mod_size
;
401 module
[i
].pos
= record
.file_offset
;
409 oasys_mkobject (abfd
)
412 bfd_size_type amt
= sizeof (oasys_data_type
);
413 abfd
->tdata
.oasys_obj_data
= (oasys_data_type
*) bfd_alloc (abfd
, amt
);
414 return abfd
->tdata
.oasys_obj_data
!= NULL
;
418 static const bfd_target
*
419 oasys_object_p (abfd
)
422 oasys_data_type
*oasys
;
423 oasys_data_type
*save
= OASYS_DATA (abfd
);
425 boolean had_usefull
= false;
427 abfd
->tdata
.oasys_obj_data
= 0;
428 oasys_mkobject (abfd
);
429 oasys
= OASYS_DATA (abfd
);
430 memset ((PTR
) oasys
->sections
, 0xff, sizeof (oasys
->sections
));
432 /* Point to the start of the file */
433 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
435 oasys
->symbol_string_length
= 0;
436 /* Inspect the records, but only keep the section info -
437 remember the size of the symbols
439 oasys
->first_data_record
= 0;
442 oasys_record_union_type record
;
443 if (! oasys_read_record (abfd
, &record
))
445 if ((size_t) record
.header
.length
< (size_t) sizeof (record
.header
))
449 switch ((oasys_record_enum_type
) (record
.header
.type
))
451 case oasys_record_is_header_enum
:
454 case oasys_record_is_symbol_enum
:
455 case oasys_record_is_local_enum
:
456 /* Count symbols and remember their size for a future malloc */
458 oasys
->symbol_string_length
+= 1 + oasys_string_length (&record
);
461 case oasys_record_is_section_enum
:
465 unsigned int section_number
;
466 if (record
.section
.header
.length
!= sizeof (record
.section
))
470 buffer
= bfd_alloc (abfd
, (bfd_size_type
) 3);
473 section_number
= record
.section
.relb
& RELOCATION_SECT_BITS
;
474 sprintf (buffer
, "%u", section_number
);
475 s
= bfd_make_section (abfd
, buffer
);
476 oasys
->sections
[section_number
] = s
;
477 switch (record
.section
.relb
& RELOCATION_TYPE_BITS
)
479 case RELOCATION_TYPE_ABS
:
480 case RELOCATION_TYPE_REL
:
482 case RELOCATION_TYPE_UND
:
483 case RELOCATION_TYPE_COM
:
487 s
->_raw_size
= H_GET_32 (abfd
, record
.section
.value
);
488 s
->vma
= H_GET_32 (abfd
, record
.section
.vma
);
493 case oasys_record_is_data_enum
:
494 oasys
->first_data_record
= bfd_tell (abfd
) - record
.header
.length
;
495 case oasys_record_is_debug_enum
:
496 case oasys_record_is_module_enum
:
497 case oasys_record_is_named_section_enum
:
498 case oasys_record_is_end_enum
:
507 oasys
->symbols
= (asymbol
*) NULL
;
509 Oasys support several architectures, but I can't see a simple way
510 to discover which one is in a particular file - we'll guess
512 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
513 if (abfd
->symcount
!= 0)
515 abfd
->flags
|= HAS_SYMS
;
519 We don't know if a section has data until we've read it..
522 oasys_slurp_section_data (abfd
);
528 (void) bfd_release (abfd
, oasys
);
529 abfd
->tdata
.oasys_obj_data
= save
;
530 return (const bfd_target
*) NULL
;
535 oasys_get_symbol_info (ignore_abfd
, symbol
, ret
)
536 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
540 bfd_symbol_info (symbol
, ret
);
541 if (!symbol
->section
)
542 ret
->type
= (symbol
->flags
& BSF_LOCAL
) ? 'a' : 'A';
546 oasys_print_symbol (abfd
, afile
, symbol
, how
)
550 bfd_print_symbol_type how
;
552 FILE *file
= (FILE *) afile
;
556 case bfd_print_symbol_name
:
557 case bfd_print_symbol_more
:
558 fprintf (file
, "%s", symbol
->name
);
560 case bfd_print_symbol_all
:
562 const char *section_name
= symbol
->section
== (asection
*) NULL
?
563 (const char *) "*abs" : symbol
->section
->name
;
565 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
567 fprintf (file
, " %-5s %s",
575 The howto table is build using the top two bits of a reloc byte to
576 index into it. The bits are PCREL,WORD/LONG
578 static reloc_howto_type howto_table
[] =
581 HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
582 HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "abs32", true, 0xffffffff, 0xffffffff, false),
583 HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false),
584 HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false)
587 /* Read in all the section data and relocation stuff too */
589 oasys_slurp_section_data (abfd
)
592 oasys_record_union_type record
;
593 oasys_data_type
*data
= OASYS_DATA (abfd
);
595 oasys_per_section_type
*per
;
599 /* See if the data has been slurped already .. */
600 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
602 per
= oasys_per_section (s
);
603 if (per
->initialized
)
607 if (data
->first_data_record
== 0)
610 if (bfd_seek (abfd
, data
->first_data_record
, SEEK_SET
) != 0)
614 if (! oasys_read_record (abfd
, &record
))
616 switch (record
.header
.type
)
618 case oasys_record_is_header_enum
:
620 case oasys_record_is_data_enum
:
623 bfd_byte
*src
= record
.data
.data
;
624 bfd_byte
*end_src
= ((bfd_byte
*) & record
) + record
.header
.length
;
626 bfd_byte
*dst_base_ptr
;
630 data
->sections
[record
.data
.relb
& RELOCATION_SECT_BITS
];
633 per
= oasys_per_section (section
);
635 if (! per
->initialized
)
637 per
->data
= (bfd_byte
*) bfd_zalloc (abfd
, section
->_raw_size
);
641 = (oasys_reloc_type
**) §ion
->relocation
;
642 per
->had_vma
= false;
643 per
->initialized
= true;
644 section
->reloc_count
= 0;
645 section
->flags
= SEC_ALLOC
;
648 dst_offset
= H_GET_32 (abfd
, record
.data
.addr
);
651 /* Take the first vma we see as the base */
652 section
->vma
= dst_offset
;
656 dst_offset
-= section
->vma
;
658 dst_base_ptr
= oasys_per_section (section
)->data
;
659 dst_ptr
= oasys_per_section (section
)->data
+
664 section
->flags
|= SEC_LOAD
| SEC_HAS_CONTENTS
;
666 while (src
< end_src
)
668 unsigned char mod_byte
= *src
++;
669 size_t gap
= end_src
- src
;
672 if (mod_byte
== 0 && gap
>= 8)
687 for (relbit
= 1; count
-- != 0 && src
< end_src
; relbit
<<= 1)
689 if (relbit
& mod_byte
)
691 unsigned char reloc
= *src
;
692 /* This item needs to be relocated */
693 switch (reloc
& RELOCATION_TYPE_BITS
)
695 case RELOCATION_TYPE_ABS
:
699 case RELOCATION_TYPE_REL
:
701 /* Relocate the item relative to the section */
704 amt
= sizeof (oasys_reloc_type
);
705 r
= (oasys_reloc_type
*) bfd_alloc (abfd
,
709 *(per
->reloc_tail_ptr
) = r
;
710 per
->reloc_tail_ptr
= &r
->next
;
711 r
->next
= (oasys_reloc_type
*) NULL
;
712 /* Reference to undefined symbol */
714 /* There is no symbol */
716 /* Work out the howto */
720 data
->sections
[reloc
&
721 RELOCATION_SECT_BITS
];
724 r
->relent
.section
->vma
;
726 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
727 r
->relent
.howto
= &howto_table
[reloc
>> 6];
728 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
729 section
->reloc_count
++;
731 /* Fake up the data to look like
732 it's got the -ve pc in it, this
733 makes it much easier to convert
734 into other formats. This is done
735 by hitting the addend. */
736 if (r
->relent
.howto
->pc_relative
)
737 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
742 case RELOCATION_TYPE_UND
:
746 amt
= sizeof (oasys_reloc_type
);
747 r
= (oasys_reloc_type
*) bfd_alloc (abfd
,
751 *(per
->reloc_tail_ptr
) = r
;
752 per
->reloc_tail_ptr
= &r
->next
;
753 r
->next
= (oasys_reloc_type
*) NULL
;
754 /* Reference to undefined symbol */
756 /* Get symbol number */
757 r
->symbol
= (src
[0] << 8) | src
[1];
758 /* Work out the howto */
762 r
->relent
.section
= (asection
765 r
->relent
.addend
= 0;
766 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
767 r
->relent
.howto
= &howto_table
[reloc
>> 6];
768 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
769 section
->reloc_count
++;
772 /* Fake up the data to look like
773 it's got the -ve pc in it, this
774 makes it much easier to convert
775 into other formats. This is done
776 by hitting the addend. */
777 if (r
->relent
.howto
->pc_relative
)
778 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
781 case RELOCATION_TYPE_COM
:
791 case oasys_record_is_local_enum
:
792 case oasys_record_is_symbol_enum
:
793 case oasys_record_is_section_enum
:
805 oasys_new_section_hook (abfd
, newsect
)
809 newsect
->used_by_bfd
= (PTR
)
810 bfd_alloc (abfd
, (bfd_size_type
) sizeof (oasys_per_section_type
));
811 if (!newsect
->used_by_bfd
)
813 oasys_per_section (newsect
)->data
= (bfd_byte
*) NULL
;
814 oasys_per_section (newsect
)->section
= newsect
;
815 oasys_per_section (newsect
)->offset
= 0;
816 oasys_per_section (newsect
)->initialized
= false;
817 newsect
->alignment_power
= 1;
818 /* Turn the section string into an index */
820 sscanf (newsect
->name
, "%u", &newsect
->target_index
);
827 oasys_get_reloc_upper_bound (abfd
, asect
)
831 if (! oasys_slurp_section_data (abfd
))
833 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
837 oasys_get_section_contents (abfd
, section
, location
, offset
, count
)
844 oasys_per_section_type
*p
= (oasys_per_section_type
*) section
->used_by_bfd
;
845 oasys_slurp_section_data (abfd
);
846 if (! p
->initialized
)
848 (void) memset (location
, 0, (size_t) count
);
852 (void) memcpy (location
, (PTR
) (p
->data
+ offset
), (size_t) count
);
859 oasys_canonicalize_reloc (ignore_abfd
, section
, relptr
, symbols
)
860 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
863 asymbol
**symbols ATTRIBUTE_UNUSED
;
865 unsigned int reloc_count
= 0;
866 oasys_reloc_type
*src
= (oasys_reloc_type
*) (section
->relocation
);
867 while (src
!= (oasys_reloc_type
*) NULL
)
872 if (src
->relent
.section
== (asection
*) NULL
)
874 src
->relent
.sym_ptr_ptr
= symbols
+ src
->symbol
;
878 *relptr
++ = &src
->relent
;
882 *relptr
= (arelent
*) NULL
;
883 return section
->reloc_count
= reloc_count
;
892 /* Calculate the checksum and write one record */
894 oasys_write_record (abfd
, type
, record
, size
)
896 oasys_record_enum_type type
;
897 oasys_record_union_type
*record
;
904 record
->header
.length
= size
;
905 record
->header
.type
= (int) type
;
906 record
->header
.check_sum
= 0;
907 record
->header
.fill
= 0;
908 ptr
= (unsigned char *) &record
->pad
[0];
910 for (i
= 0; i
< size
; i
++)
914 record
->header
.check_sum
= 0xff & (-checksum
);
915 if (bfd_bwrite ((PTR
) record
, (bfd_size_type
) size
, abfd
) != size
)
921 /* Write out all the symbols */
923 oasys_write_syms (abfd
)
927 asymbol
**generic
= bfd_get_outsymbols (abfd
);
928 unsigned int index
= 0;
929 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
932 oasys_symbol_record_type symbol
;
933 asymbol
*const g
= generic
[count
];
935 const char *src
= g
->name
;
936 char *dst
= symbol
.name
;
939 if (bfd_is_com_section (g
->section
))
941 symbol
.relb
= RELOCATION_TYPE_COM
;
942 H_PUT_16 (abfd
, index
, symbol
.refno
);
945 else if (bfd_is_abs_section (g
->section
))
947 symbol
.relb
= RELOCATION_TYPE_ABS
;
948 H_PUT_16 (abfd
, 0, symbol
.refno
);
951 else if (bfd_is_und_section (g
->section
))
953 symbol
.relb
= RELOCATION_TYPE_UND
;
954 H_PUT_16 (abfd
, index
, symbol
.refno
);
955 /* Overload the value field with the output index number */
958 else if (g
->flags
& BSF_DEBUGGING
)
965 if (g
->section
== (asection
*) NULL
)
967 /* Sometime, the oasys tools give out a symbol with illegal
968 bits in it, we'll output it in the same broken way */
970 symbol
.relb
= RELOCATION_TYPE_REL
| 0;
974 symbol
.relb
= RELOCATION_TYPE_REL
| g
->section
->output_section
->target_index
;
976 H_PUT_16 (abfd
, 0, symbol
.refno
);
978 #ifdef UNDERSCORE_HACK
988 H_PUT_32 (abfd
, g
->value
, symbol
.value
);
991 if (g
->flags
& BSF_LOCAL
)
993 if (! oasys_write_record (abfd
,
994 oasys_record_is_local_enum
,
995 (oasys_record_union_type
*) & symbol
,
996 offsetof (oasys_symbol_record_type
,
1002 if (! oasys_write_record (abfd
,
1003 oasys_record_is_symbol_enum
,
1004 (oasys_record_union_type
*) & symbol
,
1005 offsetof (oasys_symbol_record_type
,
1009 g
->value
= index
- 1;
1016 /* Write a section header for each section */
1018 oasys_write_sections (abfd
)
1022 static oasys_section_record_type out
;
1024 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1026 if (!ISDIGIT (s
->name
[0]))
1028 (*_bfd_error_handler
)
1029 (_("%s: can not represent section `%s' in oasys"),
1030 bfd_get_filename (abfd
), s
->name
);
1031 bfd_set_error (bfd_error_nonrepresentable_section
);
1034 out
.relb
= RELOCATION_TYPE_REL
| s
->target_index
;
1035 H_PUT_32 (abfd
, s
->_cooked_size
, out
.value
);
1036 H_PUT_32 (abfd
, s
->vma
, out
.vma
);
1038 if (! oasys_write_record (abfd
,
1039 oasys_record_is_section_enum
,
1040 (oasys_record_union_type
*) & out
,
1048 oasys_write_header (abfd
)
1051 /* Create and write the header */
1052 oasys_header_record_type r
;
1053 size_t length
= strlen (abfd
->filename
);
1054 if (length
> (size_t) sizeof (r
.module_name
))
1056 length
= sizeof (r
.module_name
);
1059 (void) memcpy (r
.module_name
,
1062 (void) memset (r
.module_name
+ length
,
1064 sizeof (r
.module_name
) - length
);
1066 r
.version_number
= OASYS_VERSION_NUMBER
;
1067 r
.rev_number
= OASYS_REV_NUMBER
;
1068 if (! oasys_write_record (abfd
,
1069 oasys_record_is_header_enum
,
1070 (oasys_record_union_type
*) & r
,
1071 offsetof (oasys_header_record_type
,
1079 oasys_write_end (abfd
)
1082 oasys_end_record_type end
;
1083 unsigned char null
= 0;
1084 end
.relb
= RELOCATION_TYPE_ABS
;
1085 H_PUT_32 (abfd
, abfd
->start_address
, end
.entry
);
1086 H_PUT_16 (abfd
, 0, end
.fill
);
1088 if (! oasys_write_record (abfd
,
1089 oasys_record_is_end_enum
,
1090 (oasys_record_union_type
*) & end
,
1093 if (bfd_bwrite ((PTR
) &null
, (bfd_size_type
) 1, abfd
) != 1)
1103 arelent
*a
= *((arelent
**) ap
);
1104 arelent
*b
= *((arelent
**) bp
);
1105 return a
->address
- b
->address
;
1113 oasys_write_data (abfd
)
1117 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1119 if (s
->flags
& SEC_LOAD
)
1121 bfd_byte
*raw_data
= oasys_per_section (s
)->data
;
1122 oasys_data_record_type processed_data
;
1123 bfd_size_type current_byte_index
= 0;
1124 unsigned int relocs_to_go
= s
->reloc_count
;
1125 arelent
**p
= s
->orelocation
;
1126 if (s
->reloc_count
!= 0)
1128 /* Sort the reloc records so it's easy to insert the relocs into the
1131 qsort (s
->orelocation
,
1133 sizeof (arelent
**),
1136 current_byte_index
= 0;
1137 processed_data
.relb
= s
->target_index
| RELOCATION_TYPE_REL
;
1139 while (current_byte_index
< s
->_cooked_size
)
1141 /* Scan forwards by eight bytes or however much is left and see if
1142 there are any relocations going on */
1143 bfd_byte
*mod
= &processed_data
.data
[0];
1144 bfd_byte
*dst
= &processed_data
.data
[1];
1150 H_PUT_32 (abfd
, s
->vma
+ current_byte_index
,
1151 processed_data
.addr
);
1153 /* Don't start a relocation unless you're sure you can finish it
1154 within the same data record. The worst case relocation is a
1155 4-byte relocatable value which is split across two modification
1156 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1157 1 modification byte + 2 data = 8 bytes total). That's where
1158 the magic number 8 comes from.
1160 while (current_byte_index
< s
->_raw_size
&& dst
<=
1161 &processed_data
.data
[sizeof (processed_data
.data
) - 8])
1165 if (relocs_to_go
!= 0)
1168 reloc_howto_type
*const how
= r
->howto
;
1169 /* There is a relocation, is it for this byte ? */
1170 if (r
->address
== current_byte_index
)
1172 unsigned char rel_byte
;
1178 if (how
->pc_relative
)
1180 rel_byte
= RELOCATION_PCREL_BIT
;
1182 /* Also patch the raw data so that it doesn't have
1183 the -ve stuff any more */
1187 bfd_get_16 (abfd
, raw_data
) +
1188 current_byte_index
, raw_data
);
1194 bfd_get_32 (abfd
, raw_data
) +
1195 current_byte_index
, raw_data
);
1204 rel_byte
|= RELOCATION_32BIT_BIT
;
1207 /* Is this a section relative relocation, or a symbol
1208 relative relocation ? */
1212 if (r
->section
!= (asection
*) NULL
)
1214 /* The relent has a section attached, so it must be section
1216 rel_byte
|= RELOCATION_TYPE_REL
;
1217 rel_byte
|= r
->section
->output_section
->target_index
;
1223 asymbol
*sym
= *(r
->sym_ptr_ptr
);
1225 /* If this symbol has a section attached, then it
1226 has already been resolved. Change from a symbol
1227 ref to a section ref */
1228 if (sym
->section
!= (asection
*) NULL
)
1230 rel_byte
|= RELOCATION_TYPE_REL
;
1232 sym
->section
->output_section
->target_index
;
1237 rel_byte
|= RELOCATION_TYPE_UND
;
1239 /* Next two bytes are a symbol index - we can get
1240 this from the symbol value which has been zapped
1241 into the symbol index in the table when the
1242 symbol table was written
1244 *dst
++ = sym
->value
>> 8;
1245 *dst
++ = sym
->value
;
1248 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1249 /* relocations never occur from an unloadable section,
1250 so we can assume that raw_data is not NULL
1252 *dst
++ = *raw_data
++;
1254 * dst
++ = *raw_data
++;
1258 *dst
++ = *raw_data
++;
1260 * dst
++ = *raw_data
++;
1266 /* If this is coming from an unloadable section then copy
1268 if (raw_data
== NULL
)
1274 *dst
++ = *raw_data
++;
1279 /* Don't write a useless null modification byte */
1285 if (! (oasys_write_record
1286 (abfd
, oasys_record_is_data_enum
,
1287 ((oasys_record_union_type
*) &processed_data
),
1288 (size_t) (dst
- (bfd_byte
*) &processed_data
))))
1298 oasys_write_object_contents (abfd
)
1301 if (! oasys_write_header (abfd
))
1303 if (! oasys_write_syms (abfd
))
1305 if (! oasys_write_sections (abfd
))
1307 if (! oasys_write_data (abfd
))
1309 if (! oasys_write_end (abfd
))
1317 /** exec and core file sections */
1319 /* set section contents is complicated with OASYS since the format is
1320 * not a byte image, but a record stream.
1323 oasys_set_section_contents (abfd
, section
, location
, offset
, count
)
1328 bfd_size_type count
;
1332 if (oasys_per_section (section
)->data
== (bfd_byte
*) NULL
)
1334 oasys_per_section (section
)->data
=
1335 (bfd_byte
*) (bfd_alloc (abfd
, section
->_cooked_size
));
1336 if (!oasys_per_section (section
)->data
)
1339 (void) memcpy ((PTR
) (oasys_per_section (section
)->data
+ offset
),
1348 /* Native-level interface to symbols. */
1350 /* We read the symbols into a buffer, which is discarded when this
1351 function exits. We read the strings into a buffer large enough to
1352 hold them all plus all the cached symbol entries. */
1355 oasys_make_empty_symbol (abfd
)
1358 bfd_size_type amt
= sizeof (oasys_symbol_type
);
1359 oasys_symbol_type
*new = (oasys_symbol_type
*) bfd_zalloc (abfd
, amt
);
1362 new->symbol
.the_bfd
= abfd
;
1363 return &new->symbol
;
1369 /* User should have checked the file flags; perhaps we should return
1370 BFD_NO_MORE_SYMBOLS if there are none? */
1373 oasys_openr_next_archived_file (arch
, prev
)
1377 oasys_ar_data_type
*ar
= OASYS_AR_DATA (arch
);
1378 oasys_module_info_type
*p
;
1379 /* take the next one from the arch state, or reset */
1380 if (prev
== (bfd
*) NULL
)
1382 /* Reset the index - the first two entries are bogus*/
1383 ar
->module_index
= 0;
1386 p
= ar
->module
+ ar
->module_index
;
1389 if (ar
->module_index
<= ar
->module_count
)
1391 if (p
->abfd
== (bfd
*) NULL
)
1393 p
->abfd
= _bfd_create_empty_archive_element_shell (arch
);
1394 p
->abfd
->origin
= p
->pos
;
1395 p
->abfd
->filename
= p
->name
;
1397 /* Fixup a pointer to this element for the member */
1398 p
->abfd
->arelt_data
= (PTR
) p
;
1404 bfd_set_error (bfd_error_no_more_archived_files
);
1405 return (bfd
*) NULL
;
1410 oasys_find_nearest_line (abfd
, section
, symbols
, offset
,
1411 filename_ptr
, functionname_ptr
, line_ptr
)
1412 bfd
*abfd ATTRIBUTE_UNUSED
;
1413 asection
*section ATTRIBUTE_UNUSED
;
1414 asymbol
**symbols ATTRIBUTE_UNUSED
;
1415 bfd_vma offset ATTRIBUTE_UNUSED
;
1416 const char **filename_ptr ATTRIBUTE_UNUSED
;
1417 const char **functionname_ptr ATTRIBUTE_UNUSED
;
1418 unsigned int *line_ptr ATTRIBUTE_UNUSED
;
1425 oasys_generic_stat_arch_elt (abfd
, buf
)
1429 oasys_module_info_type
*mod
= (oasys_module_info_type
*) abfd
->arelt_data
;
1430 if (mod
== (oasys_module_info_type
*) NULL
)
1432 bfd_set_error (bfd_error_invalid_operation
);
1437 buf
->st_size
= mod
->size
;
1438 buf
->st_mode
= 0666;
1444 oasys_sizeof_headers (abfd
, exec
)
1445 bfd
*abfd ATTRIBUTE_UNUSED
;
1446 boolean exec ATTRIBUTE_UNUSED
;
1451 #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup
1452 #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1454 #define oasys_slurp_armap bfd_true
1455 #define oasys_slurp_extended_name_table bfd_true
1456 #define oasys_construct_extended_name_table \
1457 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1459 #define oasys_truncate_arname bfd_dont_truncate_arname
1460 #define oasys_write_armap \
1462 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1464 #define oasys_read_ar_hdr bfd_nullvoidptr
1465 #define oasys_get_elt_at_index _bfd_generic_get_elt_at_index
1466 #define oasys_update_armap_timestamp bfd_true
1468 #define oasys_bfd_is_local_label_name bfd_generic_is_local_label_name
1469 #define oasys_get_lineno _bfd_nosymbols_get_lineno
1470 #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1471 #define oasys_read_minisymbols _bfd_generic_read_minisymbols
1472 #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1474 #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1476 #define oasys_set_arch_mach bfd_default_set_arch_mach
1478 #define oasys_get_section_contents_in_window \
1479 _bfd_generic_get_section_contents_in_window
1481 #define oasys_bfd_get_relocated_section_contents \
1482 bfd_generic_get_relocated_section_contents
1483 #define oasys_bfd_relax_section bfd_generic_relax_section
1484 #define oasys_bfd_gc_sections bfd_generic_gc_sections
1485 #define oasys_bfd_merge_sections bfd_generic_merge_sections
1486 #define oasys_bfd_discard_group bfd_generic_discard_group
1487 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1488 #define oasys_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
1489 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1490 #define oasys_bfd_link_just_syms _bfd_generic_link_just_syms
1491 #define oasys_bfd_final_link _bfd_generic_final_link
1492 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1495 const bfd_target oasys_vec
=
1498 bfd_target_oasys_flavour
,
1499 BFD_ENDIAN_BIG
, /* target byte order */
1500 BFD_ENDIAN_BIG
, /* target headers byte order */
1501 (HAS_RELOC
| EXEC_P
| /* object flags */
1502 HAS_LINENO
| HAS_DEBUG
|
1503 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
1504 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1505 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1506 0, /* leading underscore */
1507 ' ', /* ar_pad_char */
1508 16, /* ar_max_namelen */
1509 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1510 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1511 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1512 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1513 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1514 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1517 oasys_object_p
, /* bfd_check_format */
1521 { /* bfd_set_format */
1524 _bfd_generic_mkarchive
,
1527 { /* bfd_write_contents */
1529 oasys_write_object_contents
,
1530 _bfd_write_archive_contents
,
1534 BFD_JUMP_TABLE_GENERIC (oasys
),
1535 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1536 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1537 BFD_JUMP_TABLE_ARCHIVE (oasys
),
1538 BFD_JUMP_TABLE_SYMBOLS (oasys
),
1539 BFD_JUMP_TABLE_RELOCS (oasys
),
1540 BFD_JUMP_TABLE_WRITE (oasys
),
1541 BFD_JUMP_TABLE_LINK (oasys
),
1542 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),