1 /* BFD back-end for oasys objects.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support, <sac@cygnus.com>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #define UNDERSCORE_HACK 1
29 /* XXX - FIXME. offsetof belongs in the system-specific files in
31 /* Define offsetof for those systems which lack it */
34 #define offsetof(type, identifier) (size_t) &(((type *) 0)->identifier)
37 static boolean oasys_read_record
PARAMS ((bfd
*,
38 oasys_record_union_type
*));
39 static boolean oasys_write_sections
PARAMS ((bfd
*));
40 static boolean oasys_write_record
PARAMS ((bfd
*,
41 oasys_record_enum_type
,
42 oasys_record_union_type
*,
44 static boolean oasys_write_syms
PARAMS ((bfd
*));
45 static boolean oasys_write_header
PARAMS ((bfd
*));
46 static boolean oasys_write_end
PARAMS ((bfd
*));
47 static boolean oasys_write_data
PARAMS ((bfd
*));
49 /* Read in all the section data and relocation stuff too */
50 PROTO (static boolean
, oasys_slurp_section_data
, (bfd
* CONST abfd
));
53 oasys_read_record (abfd
, record
)
55 oasys_record_union_type
*record
;
57 if (bfd_read ((PTR
) record
, 1, sizeof (record
->header
), abfd
)
58 != sizeof (record
->header
))
61 if ((size_t) record
->header
.length
<= (size_t) sizeof (record
->header
))
63 if (bfd_read ((PTR
) (((char *) record
) + sizeof (record
->header
)),
64 1, record
->header
.length
- sizeof (record
->header
),
66 != record
->header
.length
- sizeof (record
->header
))
71 oasys_string_length (record
)
72 oasys_record_union_type
*record
;
74 return record
->header
.length
75 - ((char *) record
->symbol
.name
- (char *) record
);
78 /*****************************************************************************/
82 Slurp the symbol table by reading in all the records at the start file
83 till we get to the first section record.
85 We'll sort the symbolss into two lists, defined and undefined. The
86 undefined symbols will be placed into the table according to their
89 We do this by placing all undefined symbols at the front of the table
90 moving in, and the defined symbols at the end of the table moving back.
95 oasys_slurp_symbol_table (abfd
)
98 oasys_record_union_type record
;
99 oasys_data_type
*data
= OASYS_DATA (abfd
);
101 asymbol
*dest_defined
;
106 if (data
->symbols
!= (asymbol
*) NULL
)
110 /* Buy enough memory for all the symbols and all the names */
112 (asymbol
*) bfd_alloc (abfd
, sizeof (asymbol
) * abfd
->symcount
);
113 #ifdef UNDERSCORE_HACK
114 /* buy 1 more char for each symbol to keep the underscore in*/
115 data
->strings
= bfd_alloc (abfd
, data
->symbol_string_length
+
118 data
->strings
= bfd_alloc (abfd
, data
->symbol_string_length
);
120 if (!data
->symbols
|| !data
->strings
)
123 dest_defined
= data
->symbols
+ abfd
->symcount
- 1;
125 string_ptr
= data
->strings
;
126 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
131 if (! oasys_read_record (abfd
, &record
))
133 switch (record
.header
.type
)
135 case oasys_record_is_header_enum
:
137 case oasys_record_is_local_enum
:
138 case oasys_record_is_symbol_enum
:
140 int flag
= record
.header
.type
== (int) oasys_record_is_local_enum
?
141 (BSF_LOCAL
) : (BSF_GLOBAL
| BSF_EXPORT
);
144 size_t length
= oasys_string_length (&record
);
145 switch (record
.symbol
.relb
& RELOCATION_TYPE_BITS
)
147 case RELOCATION_TYPE_ABS
:
148 dest
= dest_defined
--;
149 dest
->section
= bfd_abs_section_ptr
;
153 case RELOCATION_TYPE_REL
:
154 dest
= dest_defined
--;
156 OASYS_DATA (abfd
)->sections
[record
.symbol
.relb
&
157 RELOCATION_SECT_BITS
];
158 if (record
.header
.type
== (int) oasys_record_is_local_enum
)
160 dest
->flags
= BSF_LOCAL
;
161 if (dest
->section
== (asection
*) (~0))
163 /* It seems that sometimes internal symbols are tied up, but
164 still get output, even though there is no
175 case RELOCATION_TYPE_UND
:
176 dest
= data
->symbols
+ bfd_h_get_16 (abfd
, record
.symbol
.refno
);
177 dest
->section
= bfd_und_section_ptr
;
179 case RELOCATION_TYPE_COM
:
180 dest
= dest_defined
--;
181 dest
->name
= string_ptr
;
182 dest
->the_bfd
= abfd
;
184 dest
->section
= bfd_com_section_ptr
;
188 dest
= dest_defined
--;
192 dest
->name
= string_ptr
;
193 dest
->the_bfd
= abfd
;
194 dest
->udata
.p
= (PTR
) NULL
;
195 dest
->value
= bfd_h_get_32 (abfd
, record
.symbol
.value
);
197 #ifdef UNDERSCORE_HACK
198 if (record
.symbol
.name
[0] != '_')
204 memcpy (string_ptr
, record
.symbol
.name
, length
);
207 string_ptr
[length
] = 0;
208 string_ptr
+= length
+ 1;
219 oasys_get_symtab_upper_bound (abfd
)
222 if (! oasys_slurp_symbol_table (abfd
))
225 return (abfd
->symcount
+ 1) * (sizeof (oasys_symbol_type
*));
231 extern const bfd_target oasys_vec
;
234 oasys_get_symtab (abfd
, location
)
239 unsigned int counter
;
240 if (oasys_slurp_symbol_table (abfd
) == false)
244 symbase
= OASYS_DATA (abfd
)->symbols
;
245 for (counter
= 0; counter
< abfd
->symcount
; counter
++)
247 *(location
++) = symbase
++;
250 return abfd
->symcount
;
253 /***********************************************************************
257 static const bfd_target
*
258 oasys_archive_p (abfd
)
261 oasys_archive_header_type header
;
262 oasys_extarchive_header_type header_ext
;
266 if (bfd_seek (abfd
, (file_ptr
) 0, false) != 0
267 || (bfd_read ((PTR
) & header_ext
, 1, sizeof (header_ext
), abfd
)
268 != sizeof (header_ext
)))
270 if (bfd_get_error () != bfd_error_system_call
)
271 bfd_set_error (bfd_error_wrong_format
);
275 header
.version
= bfd_h_get_32 (abfd
, header_ext
.version
);
276 header
.mod_count
= bfd_h_get_32 (abfd
, header_ext
.mod_count
);
277 header
.mod_tbl_offset
= bfd_h_get_32 (abfd
, header_ext
.mod_tbl_offset
);
278 header
.sym_tbl_size
= bfd_h_get_32 (abfd
, header_ext
.sym_tbl_size
);
279 header
.sym_count
= bfd_h_get_32 (abfd
, header_ext
.sym_count
);
280 header
.sym_tbl_offset
= bfd_h_get_32 (abfd
, header_ext
.sym_tbl_offset
);
281 header
.xref_count
= bfd_h_get_32 (abfd
, header_ext
.xref_count
);
282 header
.xref_lst_offset
= bfd_h_get_32 (abfd
, header_ext
.xref_lst_offset
);
285 There isn't a magic number in an Oasys archive, so the best we
286 can do to verify reasnableness is to make sure that the values in
287 the header are too weird
290 if (header
.version
> 10000 ||
291 header
.mod_count
> 10000 ||
292 header
.sym_count
> 100000 ||
293 header
.xref_count
> 100000)
294 return (const bfd_target
*) NULL
;
297 That all worked, let's buy the space for the header and read in
301 oasys_ar_data_type
*ar
=
302 (oasys_ar_data_type
*) bfd_alloc (abfd
, sizeof (oasys_ar_data_type
));
304 oasys_module_info_type
*module
=
305 (oasys_module_info_type
*)
306 bfd_alloc (abfd
, sizeof (oasys_module_info_type
) * header
.mod_count
);
307 oasys_module_table_type record
;
312 abfd
->tdata
.oasys_ar_data
= ar
;
314 ar
->module_count
= header
.mod_count
;
316 filepos
= header
.mod_tbl_offset
;
317 for (i
= 0; i
< header
.mod_count
; i
++)
319 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
322 /* There are two ways of specifying the archive header */
326 oasys_extmodule_table_type_a_type record_ext
;
327 if (bfd_read ((PTR
) & record_ext
, 1, sizeof (record_ext
), abfd
)
328 != sizeof (record_ext
))
331 record
.mod_size
= bfd_h_get_32 (abfd
, record_ext
.mod_size
);
332 record
.file_offset
= bfd_h_get_32 (abfd
, record_ext
.file_offset
);
334 record
.dep_count
= bfd_h_get_32 (abfd
, record_ext
.dep_count
);
335 record
.depee_count
= bfd_h_get_32 (abfd
, record_ext
.depee_count
);
336 record
.sect_count
= bfd_h_get_32 (abfd
, record_ext
.sect_count
);
338 module
[i
].name
= bfd_alloc (abfd
, 33);
342 memcpy (module
[i
].name
, record_ext
.mod_name
, 33);
344 sizeof (record_ext
) +
345 record
.dep_count
* 4 +
346 record
.depee_count
* 4 +
347 record
.sect_count
* 8 + 187;
351 oasys_extmodule_table_type_b_type record_ext
;
352 if (bfd_read ((PTR
) & record_ext
, 1, sizeof (record_ext
), abfd
)
353 != sizeof (record_ext
))
356 record
.mod_size
= bfd_h_get_32 (abfd
, record_ext
.mod_size
);
357 record
.file_offset
= bfd_h_get_32 (abfd
, record_ext
.file_offset
);
359 record
.dep_count
= bfd_h_get_32 (abfd
, record_ext
.dep_count
);
360 record
.depee_count
= bfd_h_get_32 (abfd
, record_ext
.depee_count
);
361 record
.sect_count
= bfd_h_get_32 (abfd
, record_ext
.sect_count
);
362 record
.module_name_size
= bfd_h_get_32 (abfd
, record_ext
.mod_name_length
);
364 module
[i
].name
= bfd_alloc (abfd
, record
.module_name_size
+ 1);
367 if (bfd_read ((PTR
) module
[i
].name
, 1, record
.module_name_size
,
369 != record
.module_name_size
)
371 module
[i
].name
[record
.module_name_size
] = 0;
373 sizeof (record_ext
) +
374 record
.dep_count
* 4 +
375 record
.module_name_size
+ 1;
380 module
[i
].size
= record
.mod_size
;
381 module
[i
].pos
= record
.file_offset
;
390 oasys_mkobject (abfd
)
394 abfd
->tdata
.oasys_obj_data
= (oasys_data_type
*) bfd_alloc (abfd
, sizeof (oasys_data_type
));
395 return abfd
->tdata
.oasys_obj_data
? true : false;
399 static const bfd_target
*
400 oasys_object_p (abfd
)
403 oasys_data_type
*oasys
;
404 oasys_data_type
*save
= OASYS_DATA (abfd
);
406 boolean had_usefull
= false;
408 abfd
->tdata
.oasys_obj_data
= 0;
409 oasys_mkobject (abfd
);
410 oasys
= OASYS_DATA (abfd
);
411 memset ((PTR
) oasys
->sections
, 0xff, sizeof (oasys
->sections
));
413 /* Point to the start of the file */
414 if (bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
) != 0)
416 oasys
->symbol_string_length
= 0;
417 /* Inspect the records, but only keep the section info -
418 remember the size of the symbols
420 oasys
->first_data_record
= 0;
423 oasys_record_union_type record
;
424 if (! oasys_read_record (abfd
, &record
))
426 if ((size_t) record
.header
.length
< (size_t) sizeof (record
.header
))
430 switch ((oasys_record_enum_type
) (record
.header
.type
))
432 case oasys_record_is_header_enum
:
435 case oasys_record_is_symbol_enum
:
436 case oasys_record_is_local_enum
:
437 /* Count symbols and remember their size for a future malloc */
439 oasys
->symbol_string_length
+= 1 + oasys_string_length (&record
);
442 case oasys_record_is_section_enum
:
446 unsigned int section_number
;
447 if (record
.section
.header
.length
!= sizeof (record
.section
))
451 buffer
= bfd_alloc (abfd
, 3);
454 section_number
= record
.section
.relb
& RELOCATION_SECT_BITS
;
455 sprintf (buffer
, "%u", section_number
);
456 s
= bfd_make_section (abfd
, buffer
);
457 oasys
->sections
[section_number
] = s
;
458 switch (record
.section
.relb
& RELOCATION_TYPE_BITS
)
460 case RELOCATION_TYPE_ABS
:
461 case RELOCATION_TYPE_REL
:
463 case RELOCATION_TYPE_UND
:
464 case RELOCATION_TYPE_COM
:
468 s
->_raw_size
= bfd_h_get_32 (abfd
, record
.section
.value
);
469 s
->vma
= bfd_h_get_32 (abfd
, record
.section
.vma
);
474 case oasys_record_is_data_enum
:
475 oasys
->first_data_record
= bfd_tell (abfd
) - record
.header
.length
;
476 case oasys_record_is_debug_enum
:
477 case oasys_record_is_module_enum
:
478 case oasys_record_is_named_section_enum
:
479 case oasys_record_is_end_enum
:
480 if (had_usefull
== false)
488 oasys
->symbols
= (asymbol
*) NULL
;
490 Oasys support several architectures, but I can't see a simple way
491 to discover which one is in a particular file - we'll guess
493 bfd_default_set_arch_mach (abfd
, bfd_arch_m68k
, 0);
494 if (abfd
->symcount
!= 0)
496 abfd
->flags
|= HAS_SYMS
;
500 We don't know if a section has data until we've read it..
503 oasys_slurp_section_data (abfd
);
509 (void) bfd_release (abfd
, oasys
);
510 abfd
->tdata
.oasys_obj_data
= save
;
511 return (const bfd_target
*) NULL
;
516 oasys_get_symbol_info (ignore_abfd
, symbol
, ret
)
521 bfd_symbol_info (symbol
, ret
);
522 if (!symbol
->section
)
523 ret
->type
= (symbol
->flags
& BSF_LOCAL
) ? 'a' : 'A';
527 oasys_print_symbol (ignore_abfd
, afile
, symbol
, how
)
531 bfd_print_symbol_type how
;
533 FILE *file
= (FILE *) afile
;
537 case bfd_print_symbol_name
:
538 case bfd_print_symbol_more
:
539 fprintf (file
, "%s", symbol
->name
);
541 case bfd_print_symbol_all
:
543 CONST
char *section_name
= symbol
->section
== (asection
*) NULL
?
544 (CONST
char *) "*abs" : symbol
->section
->name
;
546 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
548 fprintf (file
, " %-5s %s",
556 The howto table is build using the top two bits of a reloc byte to
557 index into it. The bits are PCREL,WORD/LONG
559 static reloc_howto_type howto_table
[] =
562 HOWTO (0, 0, 1, 16, false, 0, complain_overflow_bitfield
, 0, "abs16", true, 0x0000ffff, 0x0000ffff, false),
563 HOWTO (0, 0, 2, 32, false, 0, complain_overflow_bitfield
, 0, "abs32", true, 0xffffffff, 0xffffffff, false),
564 HOWTO (0, 0, 1, 16, true, 0, complain_overflow_signed
, 0, "pcrel16", true, 0x0000ffff, 0x0000ffff, false),
565 HOWTO (0, 0, 2, 32, true, 0, complain_overflow_signed
, 0, "pcrel32", true, 0xffffffff, 0xffffffff, false)
568 /* Read in all the section data and relocation stuff too */
570 oasys_slurp_section_data (abfd
)
573 oasys_record_union_type record
;
574 oasys_data_type
*data
= OASYS_DATA (abfd
);
577 oasys_per_section_type
*per
;
581 /* See if the data has been slurped already .. */
582 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
584 per
= oasys_per_section (s
);
585 if (per
->initialized
== true)
589 if (data
->first_data_record
== 0)
592 if (bfd_seek (abfd
, data
->first_data_record
, SEEK_SET
) != 0)
596 if (! oasys_read_record (abfd
, &record
))
598 switch (record
.header
.type
)
600 case oasys_record_is_header_enum
:
602 case oasys_record_is_data_enum
:
605 bfd_byte
*src
= record
.data
.data
;
606 bfd_byte
*end_src
= ((bfd_byte
*) & record
) + record
.header
.length
;
608 bfd_byte
*dst_base_ptr
;
612 data
->sections
[record
.data
.relb
& RELOCATION_SECT_BITS
];
615 per
= oasys_per_section (section
);
617 if (per
->initialized
== false)
619 per
->data
= (bfd_byte
*) bfd_zalloc (abfd
, section
->_raw_size
);
622 per
->reloc_tail_ptr
= (oasys_reloc_type
**) & (section
->relocation
);
623 per
->had_vma
= false;
624 per
->initialized
= true;
625 section
->reloc_count
= 0;
626 section
->flags
= SEC_ALLOC
;
629 dst_offset
= bfd_h_get_32 (abfd
, record
.data
.addr
);
630 if (per
->had_vma
== false)
632 /* Take the first vma we see as the base */
633 section
->vma
= dst_offset
;
637 dst_offset
-= section
->vma
;
639 dst_base_ptr
= oasys_per_section (section
)->data
;
640 dst_ptr
= oasys_per_section (section
)->data
+
645 section
->flags
|= SEC_LOAD
| SEC_HAS_CONTENTS
;
647 while (src
< end_src
)
649 unsigned char mod_byte
= *src
++;
650 size_t gap
= end_src
- src
;
653 if (mod_byte
== 0 && gap
>= 8)
668 for (relbit
= 1; count
-- != 0 && src
< end_src
; relbit
<<= 1)
670 if (relbit
& mod_byte
)
672 unsigned char reloc
= *src
;
673 /* This item needs to be relocated */
674 switch (reloc
& RELOCATION_TYPE_BITS
)
676 case RELOCATION_TYPE_ABS
:
680 case RELOCATION_TYPE_REL
:
682 /* Relocate the item relative to the section */
683 oasys_reloc_type
*r
=
686 sizeof (oasys_reloc_type
));
689 *(per
->reloc_tail_ptr
) = r
;
690 per
->reloc_tail_ptr
= &r
->next
;
691 r
->next
= (oasys_reloc_type
*) NULL
;
692 /* Reference to undefined symbol */
694 /* There is no symbol */
696 /* Work out the howto */
700 data
->sections
[reloc
&
701 RELOCATION_SECT_BITS
];
704 r
->relent
.section
->vma
;
706 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
707 r
->relent
.howto
= &howto_table
[reloc
>> 6];
708 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
709 section
->reloc_count
++;
711 /* Fake up the data to look like it's got the -ve pc in it, this makes
712 it much easier to convert into other formats. This is done by
715 if (r
->relent
.howto
->pc_relative
== true)
717 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
725 case RELOCATION_TYPE_UND
:
727 oasys_reloc_type
*r
=
730 sizeof (oasys_reloc_type
));
733 *(per
->reloc_tail_ptr
) = r
;
734 per
->reloc_tail_ptr
= &r
->next
;
735 r
->next
= (oasys_reloc_type
*) NULL
;
736 /* Reference to undefined symbol */
738 /* Get symbol number */
739 r
->symbol
= (src
[0] << 8) | src
[1];
740 /* Work out the howto */
744 r
->relent
.section
= (asection
747 r
->relent
.addend
= 0;
748 r
->relent
.address
= dst_ptr
- dst_base_ptr
;
749 r
->relent
.howto
= &howto_table
[reloc
>> 6];
750 r
->relent
.sym_ptr_ptr
= (asymbol
**) NULL
;
751 section
->reloc_count
++;
754 /* Fake up the data to look like it's got the -ve pc in it, this makes
755 it much easier to convert into other formats. This is done by
758 if (r
->relent
.howto
->pc_relative
== true)
760 r
->relent
.addend
-= dst_ptr
- dst_base_ptr
;
767 case RELOCATION_TYPE_COM
:
777 case oasys_record_is_local_enum
:
778 case oasys_record_is_symbol_enum
:
779 case oasys_record_is_section_enum
:
791 oasys_new_section_hook (abfd
, newsect
)
795 newsect
->used_by_bfd
= (PTR
)
796 bfd_alloc (abfd
, sizeof (oasys_per_section_type
));
797 if (!newsect
->used_by_bfd
)
799 oasys_per_section (newsect
)->data
= (bfd_byte
*) NULL
;
800 oasys_per_section (newsect
)->section
= newsect
;
801 oasys_per_section (newsect
)->offset
= 0;
802 oasys_per_section (newsect
)->initialized
= false;
803 newsect
->alignment_power
= 1;
804 /* Turn the section string into an index */
806 sscanf (newsect
->name
, "%u", &newsect
->target_index
);
813 oasys_get_reloc_upper_bound (abfd
, asect
)
817 if (! oasys_slurp_section_data (abfd
))
819 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
823 oasys_get_section_contents (abfd
, section
, location
, offset
, count
)
830 oasys_per_section_type
*p
= (oasys_per_section_type
*) section
->used_by_bfd
;
831 oasys_slurp_section_data (abfd
);
832 if (p
->initialized
== false)
834 (void) memset (location
, 0, (int) count
);
838 (void) memcpy (location
, (PTR
) (p
->data
+ offset
), (int) count
);
845 oasys_canonicalize_reloc (ignore_abfd
, section
, relptr
, symbols
)
851 unsigned int reloc_count
= 0;
852 oasys_reloc_type
*src
= (oasys_reloc_type
*) (section
->relocation
);
853 while (src
!= (oasys_reloc_type
*) NULL
)
858 if (src
->relent
.section
== (asection
*) NULL
)
860 src
->relent
.sym_ptr_ptr
= symbols
+ src
->symbol
;
864 *relptr
++ = &src
->relent
;
868 *relptr
= (arelent
*) NULL
;
869 return section
->reloc_count
= reloc_count
;
878 /* Calculate the checksum and write one record */
880 oasys_write_record (abfd
, type
, record
, size
)
882 oasys_record_enum_type type
;
883 oasys_record_union_type
*record
;
890 record
->header
.length
= size
;
891 record
->header
.type
= (int) type
;
892 record
->header
.check_sum
= 0;
893 record
->header
.fill
= 0;
894 ptr
= (unsigned char *) &record
->pad
[0];
896 for (i
= 0; i
< size
; i
++)
900 record
->header
.check_sum
= 0xff & (-checksum
);
901 if (bfd_write ((PTR
) record
, 1, size
, abfd
) != size
)
907 /* Write out all the symbols */
909 oasys_write_syms (abfd
)
913 asymbol
**generic
= bfd_get_outsymbols (abfd
);
914 unsigned int index
= 0;
915 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
918 oasys_symbol_record_type symbol
;
919 asymbol
*CONST g
= generic
[count
];
921 CONST
char *src
= g
->name
;
922 char *dst
= symbol
.name
;
925 if (bfd_is_com_section (g
->section
))
927 symbol
.relb
= RELOCATION_TYPE_COM
;
928 bfd_h_put_16 (abfd
, index
, symbol
.refno
);
931 else if (bfd_is_abs_section (g
->section
))
933 symbol
.relb
= RELOCATION_TYPE_ABS
;
934 bfd_h_put_16 (abfd
, 0, symbol
.refno
);
937 else if (bfd_is_und_section (g
->section
))
939 symbol
.relb
= RELOCATION_TYPE_UND
;
940 bfd_h_put_16 (abfd
, index
, symbol
.refno
);
941 /* Overload the value field with the output index number */
944 else if (g
->flags
& BSF_DEBUGGING
)
951 if (g
->section
== (asection
*) NULL
)
953 /* Sometime, the oasys tools give out a symbol with illegal
954 bits in it, we'll output it in the same broken way */
956 symbol
.relb
= RELOCATION_TYPE_REL
| 0;
960 symbol
.relb
= RELOCATION_TYPE_REL
| g
->section
->output_section
->target_index
;
962 bfd_h_put_16 (abfd
, 0, symbol
.refno
);
964 #ifdef UNDERSCORE_HACK
974 bfd_h_put_32 (abfd
, g
->value
, symbol
.value
);
977 if (g
->flags
& BSF_LOCAL
)
979 if (! oasys_write_record (abfd
,
980 oasys_record_is_local_enum
,
981 (oasys_record_union_type
*) & symbol
,
982 offsetof (oasys_symbol_record_type
,
988 if (! oasys_write_record (abfd
,
989 oasys_record_is_symbol_enum
,
990 (oasys_record_union_type
*) & symbol
,
991 offsetof (oasys_symbol_record_type
,
995 g
->value
= index
- 1;
1002 /* Write a section header for each section */
1004 oasys_write_sections (abfd
)
1008 static oasys_section_record_type out
;
1010 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1012 if (!isdigit (s
->name
[0]))
1014 (*_bfd_error_handler
)
1015 ("%s: can not represent section `%s' in oasys",
1016 bfd_get_filename (abfd
), s
->name
);
1017 bfd_set_error (bfd_error_nonrepresentable_section
);
1020 out
.relb
= RELOCATION_TYPE_REL
| s
->target_index
;
1021 bfd_h_put_32 (abfd
, s
->_cooked_size
, out
.value
);
1022 bfd_h_put_32 (abfd
, s
->vma
, out
.vma
);
1024 if (! oasys_write_record (abfd
,
1025 oasys_record_is_section_enum
,
1026 (oasys_record_union_type
*) & out
,
1034 oasys_write_header (abfd
)
1037 /* Create and write the header */
1038 oasys_header_record_type r
;
1039 size_t length
= strlen (abfd
->filename
);
1040 if (length
> (size_t) sizeof (r
.module_name
))
1042 length
= sizeof (r
.module_name
);
1045 (void) memcpy (r
.module_name
,
1048 (void) memset (r
.module_name
+ length
,
1050 sizeof (r
.module_name
) - length
);
1052 r
.version_number
= OASYS_VERSION_NUMBER
;
1053 r
.rev_number
= OASYS_REV_NUMBER
;
1054 if (! oasys_write_record (abfd
,
1055 oasys_record_is_header_enum
,
1056 (oasys_record_union_type
*) & r
,
1057 offsetof (oasys_header_record_type
,
1065 oasys_write_end (abfd
)
1068 oasys_end_record_type end
;
1069 unsigned char null
= 0;
1070 end
.relb
= RELOCATION_TYPE_ABS
;
1071 bfd_h_put_32 (abfd
, abfd
->start_address
, end
.entry
);
1072 bfd_h_put_16 (abfd
, 0, end
.fill
);
1074 if (! oasys_write_record (abfd
,
1075 oasys_record_is_end_enum
,
1076 (oasys_record_union_type
*) & end
,
1079 if (bfd_write ((PTR
) & null
, 1, 1, abfd
) != 1)
1089 arelent
*a
= *((arelent
**) ap
);
1090 arelent
*b
= *((arelent
**) bp
);
1091 return a
->address
- b
->address
;
1099 oasys_write_data (abfd
)
1103 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1105 if (s
->flags
& SEC_LOAD
)
1107 bfd_byte
*raw_data
= oasys_per_section (s
)->data
;
1108 oasys_data_record_type processed_data
;
1109 bfd_size_type current_byte_index
= 0;
1110 unsigned int relocs_to_go
= s
->reloc_count
;
1111 arelent
**p
= s
->orelocation
;
1112 if (s
->reloc_count
!= 0)
1114 /* Sort the reloc records so it's easy to insert the relocs into the
1117 qsort (s
->orelocation
,
1119 sizeof (arelent
**),
1122 current_byte_index
= 0;
1123 processed_data
.relb
= s
->target_index
| RELOCATION_TYPE_REL
;
1125 while (current_byte_index
< s
->_cooked_size
)
1127 /* Scan forwards by eight bytes or however much is left and see if
1128 there are any relocations going on */
1129 bfd_byte
*mod
= &processed_data
.data
[0];
1130 bfd_byte
*dst
= &processed_data
.data
[1];
1136 bfd_h_put_32 (abfd
, s
->vma
+ current_byte_index
,
1137 processed_data
.addr
);
1139 /* Don't start a relocation unless you're sure you can finish it
1140 within the same data record. The worst case relocation is a
1141 4-byte relocatable value which is split across two modification
1142 bytes (1 relocation byte + 2 symbol reference bytes + 2 data +
1143 1 modification byte + 2 data = 8 bytes total). That's where
1144 the magic number 8 comes from.
1146 while (current_byte_index
< s
->_raw_size
&& dst
<=
1147 &processed_data
.data
[sizeof (processed_data
.data
) - 8])
1151 if (relocs_to_go
!= 0)
1154 reloc_howto_type
*const how
= r
->howto
;
1155 /* There is a relocation, is it for this byte ? */
1156 if (r
->address
== current_byte_index
)
1158 unsigned char rel_byte
;
1164 if (how
->pc_relative
)
1166 rel_byte
= RELOCATION_PCREL_BIT
;
1168 /* Also patch the raw data so that it doesn't have
1169 the -ve stuff any more */
1173 bfd_get_16 (abfd
, raw_data
) +
1174 current_byte_index
, raw_data
);
1180 bfd_get_32 (abfd
, raw_data
) +
1181 current_byte_index
, raw_data
);
1190 rel_byte
|= RELOCATION_32BIT_BIT
;
1193 /* Is this a section relative relocation, or a symbol
1194 relative relocation ? */
1198 if (r
->section
!= (asection
*) NULL
)
1200 /* The relent has a section attached, so it must be section
1202 rel_byte
|= RELOCATION_TYPE_REL
;
1203 rel_byte
|= r
->section
->output_section
->target_index
;
1209 asymbol
*p
= *(r
->sym_ptr_ptr
);
1211 /* If this symbol has a section attached, then it
1212 has already been resolved. Change from a symbol
1213 ref to a section ref */
1214 if (p
->section
!= (asection
*) NULL
)
1216 rel_byte
|= RELOCATION_TYPE_REL
;
1218 p
->section
->output_section
->target_index
;
1223 rel_byte
|= RELOCATION_TYPE_UND
;
1225 /* Next two bytes are a symbol index - we can get
1226 this from the symbol value which has been zapped
1227 into the symbol index in the table when the
1228 symbol table was written
1230 *dst
++ = p
->value
>> 8;
1234 #define ADVANCE { if (++i >= 8) { i = 0; mod = dst++; *mod = 0; } current_byte_index++; }
1235 /* relocations never occur from an unloadable section,
1236 so we can assume that raw_data is not NULL
1238 *dst
++ = *raw_data
++;
1240 * dst
++ = *raw_data
++;
1244 *dst
++ = *raw_data
++;
1246 * dst
++ = *raw_data
++;
1252 /* If this is coming from an unloadable section then copy
1254 if (raw_data
== NULL
)
1260 *dst
++ = *raw_data
++;
1265 /* Don't write a useless null modification byte */
1271 if (! oasys_write_record (abfd
,
1272 oasys_record_is_data_enum
,
1273 ((oasys_record_union_type
*)
1275 dst
- (bfd_byte
*) & processed_data
))
1285 oasys_write_object_contents (abfd
)
1288 if (! oasys_write_header (abfd
))
1290 if (! oasys_write_syms (abfd
))
1292 if (! oasys_write_sections (abfd
))
1294 if (! oasys_write_data (abfd
))
1296 if (! oasys_write_end (abfd
))
1304 /** exec and core file sections */
1306 /* set section contents is complicated with OASYS since the format is
1307 * not a byte image, but a record stream.
1310 oasys_set_section_contents (abfd
, section
, location
, offset
, count
)
1315 bfd_size_type count
;
1319 if (oasys_per_section (section
)->data
== (bfd_byte
*) NULL
)
1321 oasys_per_section (section
)->data
=
1322 (bfd_byte
*) (bfd_alloc (abfd
, section
->_cooked_size
));
1323 if (!oasys_per_section (section
)->data
)
1326 (void) memcpy ((PTR
) (oasys_per_section (section
)->data
+ offset
),
1335 /* Native-level interface to symbols. */
1337 /* We read the symbols into a buffer, which is discarded when this
1338 function exits. We read the strings into a buffer large enough to
1339 hold them all plus all the cached symbol entries. */
1342 oasys_make_empty_symbol (abfd
)
1346 oasys_symbol_type
*new =
1347 (oasys_symbol_type
*) bfd_zalloc (abfd
, sizeof (oasys_symbol_type
));
1350 new->symbol
.the_bfd
= abfd
;
1351 return &new->symbol
;
1357 /* User should have checked the file flags; perhaps we should return
1358 BFD_NO_MORE_SYMBOLS if there are none? */
1361 oasys_openr_next_archived_file (arch
, prev
)
1365 oasys_ar_data_type
*ar
= OASYS_AR_DATA (arch
);
1366 oasys_module_info_type
*p
;
1367 /* take the next one from the arch state, or reset */
1368 if (prev
== (bfd
*) NULL
)
1370 /* Reset the index - the first two entries are bogus*/
1371 ar
->module_index
= 0;
1374 p
= ar
->module
+ ar
->module_index
;
1377 if (ar
->module_index
<= ar
->module_count
)
1379 if (p
->abfd
== (bfd
*) NULL
)
1381 p
->abfd
= _bfd_create_empty_archive_element_shell (arch
);
1382 p
->abfd
->origin
= p
->pos
;
1383 p
->abfd
->filename
= p
->name
;
1385 /* Fixup a pointer to this element for the member */
1386 p
->abfd
->arelt_data
= (PTR
) p
;
1392 bfd_set_error (bfd_error_no_more_archived_files
);
1393 return (bfd
*) NULL
;
1398 oasys_find_nearest_line (abfd
,
1409 char **filename_ptr
;
1410 char **functionname_ptr
;
1411 unsigned int *line_ptr
;
1418 oasys_generic_stat_arch_elt (abfd
, buf
)
1422 oasys_module_info_type
*mod
= (oasys_module_info_type
*) abfd
->arelt_data
;
1423 if (mod
== (oasys_module_info_type
*) NULL
)
1425 bfd_set_error (bfd_error_invalid_operation
);
1430 buf
->st_size
= mod
->size
;
1431 buf
->st_mode
= 0666;
1437 oasys_sizeof_headers (abfd
, exec
)
1444 #define oasys_close_and_cleanup _bfd_generic_close_and_cleanup
1445 #define oasys_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1447 #define oasys_slurp_armap bfd_true
1448 #define oasys_slurp_extended_name_table bfd_true
1449 #define oasys_construct_extended_name_table \
1450 ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
1452 #define oasys_truncate_arname bfd_dont_truncate_arname
1453 #define oasys_write_armap \
1455 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1457 #define oasys_read_ar_hdr bfd_nullvoidptr
1458 #define oasys_get_elt_at_index _bfd_generic_get_elt_at_index
1459 #define oasys_update_armap_timestamp bfd_true
1461 #define oasys_bfd_is_local_label bfd_generic_is_local_label
1462 #define oasys_get_lineno _bfd_nosymbols_get_lineno
1463 #define oasys_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1464 #define oasys_read_minisymbols _bfd_generic_read_minisymbols
1465 #define oasys_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1467 #define oasys_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1469 #define oasys_set_arch_mach bfd_default_set_arch_mach
1471 #define oasys_get_section_contents_in_window \
1472 _bfd_generic_get_section_contents_in_window
1474 #define oasys_bfd_get_relocated_section_contents \
1475 bfd_generic_get_relocated_section_contents
1476 #define oasys_bfd_relax_section bfd_generic_relax_section
1477 #define oasys_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1478 #define oasys_bfd_link_add_symbols _bfd_generic_link_add_symbols
1479 #define oasys_bfd_final_link _bfd_generic_final_link
1480 #define oasys_bfd_link_split_section _bfd_generic_link_split_section
1483 const bfd_target oasys_vec
=
1486 bfd_target_oasys_flavour
,
1487 BFD_ENDIAN_BIG
, /* target byte order */
1488 BFD_ENDIAN_BIG
, /* target headers byte order */
1489 (HAS_RELOC
| EXEC_P
| /* object flags */
1490 HAS_LINENO
| HAS_DEBUG
|
1491 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
1492 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
1493 | SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
1494 0, /* leading underscore */
1495 ' ', /* ar_pad_char */
1496 16, /* ar_max_namelen */
1497 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1498 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1499 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1500 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1501 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1502 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1505 oasys_object_p
, /* bfd_check_format */
1509 { /* bfd_set_format */
1512 _bfd_generic_mkarchive
,
1515 { /* bfd_write_contents */
1517 oasys_write_object_contents
,
1518 _bfd_write_archive_contents
,
1522 BFD_JUMP_TABLE_GENERIC (oasys
),
1523 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1524 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1525 BFD_JUMP_TABLE_ARCHIVE (oasys
),
1526 BFD_JUMP_TABLE_SYMBOLS (oasys
),
1527 BFD_JUMP_TABLE_RELOCS (oasys
),
1528 BFD_JUMP_TABLE_WRITE (oasys
),
1529 BFD_JUMP_TABLE_LINK (oasys
),
1530 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),