daily update
[binutils.git] / bfd / oasys.c
blob9035a4fdddac63e083db93f175023eba463fd983
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
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "safe-ctype.h"
26 #include "libbfd.h"
27 #include "oasys.h"
28 #include "liboasys.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. */
70 static boolean
71 oasys_read_record (abfd, record)
72 bfd *abfd;
73 oasys_record_union_type *record;
75 bfd_size_type amt = sizeof (record->header);
76 if (bfd_bread ((PTR) record, amt, abfd) != amt)
77 return false;
79 amt = record->header.length - sizeof (record->header);
80 if ((long) amt <= 0)
81 return true;
82 if (bfd_bread ((PTR) ((char *) record + sizeof (record->header)), amt, abfd)
83 != amt)
84 return false;
85 return true;
88 static size_t
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
105 refno.
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.
112 static boolean
113 oasys_slurp_symbol_table (abfd)
114 bfd *const abfd;
116 oasys_record_union_type record;
117 oasys_data_type *data = OASYS_DATA (abfd);
118 boolean loop = true;
119 asymbol *dest_defined;
120 asymbol *dest;
121 char *string_ptr;
122 bfd_size_type amt;
124 if (data->symbols != (asymbol *) NULL)
126 return true;
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;
137 #endif
138 data->strings = bfd_alloc (abfd, amt);
140 if (!data->symbols || !data->strings)
141 return false;
143 dest_defined = data->symbols + abfd->symcount - 1;
145 string_ptr = data->strings;
146 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
147 return false;
148 while (loop)
151 if (! oasys_read_record (abfd, &record))
152 return false;
153 switch (record.header.type)
155 case oasys_record_is_header_enum:
156 break;
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;
170 dest->flags = 0;
172 break;
173 case RELOCATION_TYPE_REL:
174 dest = dest_defined--;
175 dest->section =
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
185 section */
186 dest->section = 0;
189 else
192 dest->flags = flag;
194 break;
195 case RELOCATION_TYPE_UND:
196 dest = data->symbols + H_GET_16 (abfd, record.symbol.refno);
197 dest->section = bfd_und_section_ptr;
198 break;
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;
206 break;
207 default:
208 dest = dest_defined--;
209 BFD_ASSERT (0);
210 break;
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] != '_')
220 string_ptr[0] = '_';
221 string_ptr++;
223 #endif
224 memcpy (string_ptr, record.symbol.name, length);
227 string_ptr[length] = 0;
228 string_ptr += length + 1;
230 break;
231 default:
232 loop = false;
235 return true;
238 static long
239 oasys_get_symtab_upper_bound (abfd)
240 bfd *const abfd;
242 if (! oasys_slurp_symbol_table (abfd))
243 return -1;
245 return (abfd->symcount + 1) * (sizeof (oasys_symbol_type *));
248 extern const bfd_target oasys_vec;
250 long
251 oasys_get_symtab (abfd, location)
252 bfd *abfd;
253 asymbol **location;
255 asymbol *symbase;
256 unsigned int counter;
257 if (! oasys_slurp_symbol_table (abfd))
259 return -1;
261 symbase = OASYS_DATA (abfd)->symbols;
262 for (counter = 0; counter < abfd->symcount; counter++)
264 *(location++) = symbase++;
266 *location = 0;
267 return abfd->symcount;
270 /***********************************************************************
271 * archive stuff
274 static const bfd_target *
275 oasys_archive_p (abfd)
276 bfd *abfd;
278 oasys_archive_header_type header;
279 oasys_extarchive_header_type header_ext;
280 unsigned int i;
281 file_ptr filepos;
282 bfd_size_type amt;
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);
290 return NULL;
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
316 the headers.
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);
330 if (!ar || !module)
331 return NULL;
333 abfd->tdata.oasys_ar_data = ar;
334 ar->module = module;
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)
341 return NULL;
343 /* There are two ways of specifying the archive header */
345 if (0)
347 oasys_extmodule_table_type_a_type record_ext;
349 amt = sizeof (record_ext);
350 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
351 return NULL;
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);
361 if (!module[i].name)
362 return NULL;
364 memcpy (module[i].name, record_ext.mod_name, 33);
365 filepos +=
366 sizeof (record_ext) +
367 record.dep_count * 4 +
368 record.depee_count * 4 +
369 record.sect_count * 8 + 187;
371 else
373 oasys_extmodule_table_type_b_type record_ext;
375 amt = sizeof (record_ext);
376 if (bfd_bread ((PTR) &record_ext, amt, abfd) != amt)
377 return NULL;
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);
390 if (!module[i].name)
391 return NULL;
392 if (bfd_bread ((PTR) module[i].name, amt, abfd) != amt)
393 return NULL;
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;
402 module[i].abfd = 0;
405 return abfd->xvec;
408 static boolean
409 oasys_mkobject (abfd)
410 bfd *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;
417 #define MAX_SECS 16
418 static const bfd_target *
419 oasys_object_p (abfd)
420 bfd *abfd;
422 oasys_data_type *oasys;
423 oasys_data_type *save = OASYS_DATA (abfd);
424 boolean loop = true;
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)
434 goto fail;
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;
440 while (loop)
442 oasys_record_union_type record;
443 if (! oasys_read_record (abfd, &record))
444 goto fail;
445 if ((size_t) record.header.length < (size_t) sizeof (record.header))
446 goto fail;
449 switch ((oasys_record_enum_type) (record.header.type))
451 case oasys_record_is_header_enum:
452 had_usefull = true;
453 break;
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 */
457 abfd->symcount++;
458 oasys->symbol_string_length += 1 + oasys_string_length (&record);
459 had_usefull = true;
460 break;
461 case oasys_record_is_section_enum:
463 asection *s;
464 char *buffer;
465 unsigned int section_number;
466 if (record.section.header.length != sizeof (record.section))
468 goto fail;
470 buffer = bfd_alloc (abfd, (bfd_size_type) 3);
471 if (!buffer)
472 goto fail;
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:
481 break;
482 case RELOCATION_TYPE_UND:
483 case RELOCATION_TYPE_COM:
484 BFD_FAIL ();
487 s->_raw_size = H_GET_32 (abfd, record.section.value);
488 s->vma = H_GET_32 (abfd, record.section.vma);
489 s->flags = 0;
490 had_usefull = true;
492 break;
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:
499 if (! had_usefull)
500 goto fail;
501 loop = false;
502 break;
503 default:
504 goto fail;
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);
525 return abfd->xvec;
527 fail:
528 (void) bfd_release (abfd, oasys);
529 abfd->tdata.oasys_obj_data = save;
530 return (const bfd_target *) NULL;
534 static void
535 oasys_get_symbol_info (ignore_abfd, symbol, ret)
536 bfd *ignore_abfd ATTRIBUTE_UNUSED;
537 asymbol *symbol;
538 symbol_info *ret;
540 bfd_symbol_info (symbol, ret);
541 if (!symbol->section)
542 ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
545 static void
546 oasys_print_symbol (abfd, afile, symbol, how)
547 bfd *abfd;
548 PTR afile;
549 asymbol *symbol;
550 bfd_print_symbol_type how;
552 FILE *file = (FILE *) afile;
554 switch (how)
556 case bfd_print_symbol_name:
557 case bfd_print_symbol_more:
558 fprintf (file, "%s", symbol->name);
559 break;
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",
568 section_name,
569 symbol->name);
571 break;
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 */
588 static boolean
589 oasys_slurp_section_data (abfd)
590 bfd *const abfd;
592 oasys_record_union_type record;
593 oasys_data_type *data = OASYS_DATA (abfd);
594 boolean loop = true;
595 oasys_per_section_type *per;
596 asection *s;
597 bfd_size_type amt;
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)
604 return true;
607 if (data->first_data_record == 0)
608 return true;
610 if (bfd_seek (abfd, data->first_data_record, SEEK_SET) != 0)
611 return false;
612 while (loop)
614 if (! oasys_read_record (abfd, &record))
615 return false;
616 switch (record.header.type)
618 case oasys_record_is_header_enum:
619 break;
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;
625 bfd_byte *dst_ptr;
626 bfd_byte *dst_base_ptr;
627 unsigned int relbit;
628 unsigned int count;
629 asection *section =
630 data->sections[record.data.relb & RELOCATION_SECT_BITS];
631 bfd_vma dst_offset;
633 per = oasys_per_section (section);
635 if (! per->initialized)
637 per->data = (bfd_byte *) bfd_zalloc (abfd, section->_raw_size);
638 if (!per->data)
639 return false;
640 per->reloc_tail_ptr
641 = (oasys_reloc_type **) &section->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);
649 if (! per->had_vma)
651 /* Take the first vma we see as the base */
652 section->vma = dst_offset;
653 per->had_vma = true;
656 dst_offset -= section->vma;
658 dst_base_ptr = oasys_per_section (section)->data;
659 dst_ptr = oasys_per_section (section)->data +
660 dst_offset;
662 if (src < end_src)
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;
671 count = 8;
672 if (mod_byte == 0 && gap >= 8)
674 dst_ptr[0] = src[0];
675 dst_ptr[1] = src[1];
676 dst_ptr[2] = src[2];
677 dst_ptr[3] = src[3];
678 dst_ptr[4] = src[4];
679 dst_ptr[5] = src[5];
680 dst_ptr[6] = src[6];
681 dst_ptr[7] = src[7];
682 dst_ptr += 8;
683 src += 8;
685 else
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:
697 break;
699 case RELOCATION_TYPE_REL:
701 /* Relocate the item relative to the section */
702 oasys_reloc_type *r;
704 amt = sizeof (oasys_reloc_type);
705 r = (oasys_reloc_type *) bfd_alloc (abfd,
706 amt);
707 if (!r)
708 return false;
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 */
713 src++;
714 /* There is no symbol */
715 r->symbol = 0;
716 /* Work out the howto */
717 abort ();
718 #if 0
719 r->relent.section =
720 data->sections[reloc &
721 RELOCATION_SECT_BITS];
723 r->relent.addend = -
724 r->relent.section->vma;
725 #endif
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;
739 break;
742 case RELOCATION_TYPE_UND:
744 oasys_reloc_type *r;
746 amt = sizeof (oasys_reloc_type);
747 r = (oasys_reloc_type *) bfd_alloc (abfd,
748 amt);
749 if (!r)
750 return false;
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 */
755 src++;
756 /* Get symbol number */
757 r->symbol = (src[0] << 8) | src[1];
758 /* Work out the howto */
759 abort ();
761 #if 0
762 r->relent.section = (asection
763 *) NULL;
764 #endif
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++;
771 src += 2;
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;
780 break;
781 case RELOCATION_TYPE_COM:
782 BFD_FAIL ();
785 *dst_ptr++ = *src++;
790 break;
791 case oasys_record_is_local_enum:
792 case oasys_record_is_symbol_enum:
793 case oasys_record_is_section_enum:
794 break;
795 default:
796 loop = false;
800 return true;
804 static boolean
805 oasys_new_section_hook (abfd, newsect)
806 bfd *abfd;
807 asection *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)
812 return false;
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);
822 return true;
826 static long
827 oasys_get_reloc_upper_bound (abfd, asect)
828 bfd *abfd;
829 sec_ptr asect;
831 if (! oasys_slurp_section_data (abfd))
832 return -1;
833 return (asect->reloc_count + 1) * sizeof (arelent *);
836 static boolean
837 oasys_get_section_contents (abfd, section, location, offset, count)
838 bfd *abfd;
839 sec_ptr section;
840 PTR location;
841 file_ptr offset;
842 bfd_size_type 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);
850 else
852 (void) memcpy (location, (PTR) (p->data + offset), (size_t) count);
854 return true;
858 long
859 oasys_canonicalize_reloc (ignore_abfd, section, relptr, symbols)
860 bfd *ignore_abfd ATTRIBUTE_UNUSED;
861 sec_ptr section;
862 arelent **relptr;
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)
869 abort ();
871 #if 0
872 if (src->relent.section == (asection *) NULL)
874 src->relent.sym_ptr_ptr = symbols + src->symbol;
876 #endif
878 *relptr++ = &src->relent;
879 src = src->next;
880 reloc_count++;
882 *relptr = (arelent *) NULL;
883 return section->reloc_count = reloc_count;
889 /* Writing */
892 /* Calculate the checksum and write one record */
893 static boolean
894 oasys_write_record (abfd, type, record, size)
895 bfd *abfd;
896 oasys_record_enum_type type;
897 oasys_record_union_type *record;
898 size_t size;
900 int checksum;
901 size_t i;
902 unsigned char *ptr;
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];
909 checksum = 0;
910 for (i = 0; i < size; i++)
912 checksum += *ptr++;
914 record->header.check_sum = 0xff & (-checksum);
915 if (bfd_bwrite ((PTR) record, (bfd_size_type) size, abfd) != size)
916 return false;
917 return true;
921 /* Write out all the symbols */
922 static boolean
923 oasys_write_syms (abfd)
924 bfd *abfd;
926 unsigned int count;
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;
937 unsigned int l = 0;
939 if (bfd_is_com_section (g->section))
941 symbol.relb = RELOCATION_TYPE_COM;
942 H_PUT_16 (abfd, index, symbol.refno);
943 index++;
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 */
956 index++;
958 else if (g->flags & BSF_DEBUGGING)
960 /* throw it away */
961 continue;
963 else
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;
972 else
974 symbol.relb = RELOCATION_TYPE_REL | g->section->output_section->target_index;
976 H_PUT_16 (abfd, 0, symbol.refno);
978 #ifdef UNDERSCORE_HACK
979 if (src[l] == '_')
980 dst[l++] = '.';
981 #endif
982 while (src[l])
984 dst[l] = src[l];
985 l++;
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,
997 name[0]) + l))
998 return false;
1000 else
1002 if (! oasys_write_record (abfd,
1003 oasys_record_is_symbol_enum,
1004 (oasys_record_union_type *) & symbol,
1005 offsetof (oasys_symbol_record_type,
1006 name[0]) + l))
1007 return false;
1009 g->value = index - 1;
1012 return true;
1016 /* Write a section header for each section */
1017 static boolean
1018 oasys_write_sections (abfd)
1019 bfd *abfd;
1021 asection *s;
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);
1032 return false;
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,
1041 sizeof (out)))
1042 return false;
1044 return true;
1047 static boolean
1048 oasys_write_header (abfd)
1049 bfd *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,
1060 abfd->filename,
1061 length);
1062 (void) memset (r.module_name + length,
1063 ' ',
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,
1072 description[0])))
1073 return false;
1075 return true;
1078 static boolean
1079 oasys_write_end (abfd)
1080 bfd *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);
1087 end.zero = 0;
1088 if (! oasys_write_record (abfd,
1089 oasys_record_is_end_enum,
1090 (oasys_record_union_type *) & end,
1091 sizeof (end)))
1092 return false;
1093 if (bfd_bwrite ((PTR) &null, (bfd_size_type) 1, abfd) != 1)
1094 return false;
1095 return true;
1098 static int
1099 comp (ap, bp)
1100 const PTR ap;
1101 const PTR bp;
1103 arelent *a = *((arelent **) ap);
1104 arelent *b = *((arelent **) bp);
1105 return a->address - b->address;
1109 Writing data..
1112 static boolean
1113 oasys_write_data (abfd)
1114 bfd *abfd;
1116 asection *s;
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
1129 data */
1131 qsort (s->orelocation,
1132 s->reloc_count,
1133 sizeof (arelent **),
1134 comp);
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];
1146 unsigned int i = 0;
1147 *mod = 0;
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)
1167 arelent *r = *p;
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;
1174 p++;
1175 relocs_to_go--;
1177 *mod |= (1 << i);
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 */
1184 if (how->size != 2)
1186 bfd_put_16 (abfd,
1187 bfd_get_16 (abfd, raw_data) +
1188 current_byte_index, raw_data);
1191 else
1193 bfd_put_32 (abfd,
1194 bfd_get_32 (abfd, raw_data) +
1195 current_byte_index, raw_data);
1198 else
1200 rel_byte = 0;
1202 if (how->size == 2)
1204 rel_byte |= RELOCATION_32BIT_BIT;
1207 /* Is this a section relative relocation, or a symbol
1208 relative relocation ? */
1209 abort ();
1211 #if 0
1212 if (r->section != (asection *) NULL)
1214 /* The relent has a section attached, so it must be section
1215 relative */
1216 rel_byte |= RELOCATION_TYPE_REL;
1217 rel_byte |= r->section->output_section->target_index;
1218 *dst++ = rel_byte;
1220 else
1221 #endif
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;
1231 rel_byte |=
1232 sym->section->output_section->target_index;
1233 *dst++ = rel_byte;
1235 else
1237 rel_byte |= RELOCATION_TYPE_UND;
1238 *dst++ = rel_byte;
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++;
1253 ADVANCE
1254 * dst++ = *raw_data++;
1255 ADVANCE
1256 if (how->size == 2)
1258 *dst++ = *raw_data++;
1259 ADVANCE
1260 * dst++ = *raw_data++;
1261 ADVANCE
1263 continue;
1266 /* If this is coming from an unloadable section then copy
1267 zeros */
1268 if (raw_data == NULL)
1270 *dst++ = 0;
1272 else
1274 *dst++ = *raw_data++;
1276 ADVANCE
1279 /* Don't write a useless null modification byte */
1280 if (dst == mod + 1)
1282 --dst;
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))))
1289 return false;
1294 return true;
1297 static boolean
1298 oasys_write_object_contents (abfd)
1299 bfd *abfd;
1301 if (! oasys_write_header (abfd))
1302 return false;
1303 if (! oasys_write_syms (abfd))
1304 return false;
1305 if (! oasys_write_sections (abfd))
1306 return false;
1307 if (! oasys_write_data (abfd))
1308 return false;
1309 if (! oasys_write_end (abfd))
1310 return false;
1311 return true;
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.
1322 static boolean
1323 oasys_set_section_contents (abfd, section, location, offset, count)
1324 bfd *abfd;
1325 sec_ptr section;
1326 PTR location;
1327 file_ptr offset;
1328 bfd_size_type count;
1330 if (count != 0)
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)
1337 return false;
1339 (void) memcpy ((PTR) (oasys_per_section (section)->data + offset),
1340 location,
1341 (size_t) count);
1343 return true;
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. */
1354 static asymbol *
1355 oasys_make_empty_symbol (abfd)
1356 bfd *abfd;
1358 bfd_size_type amt = sizeof (oasys_symbol_type);
1359 oasys_symbol_type *new = (oasys_symbol_type *) bfd_zalloc (abfd, amt);
1360 if (!new)
1361 return NULL;
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? */
1372 static bfd *
1373 oasys_openr_next_archived_file (arch, prev)
1374 bfd *arch;
1375 bfd *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;
1387 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;
1400 return p->abfd;
1402 else
1404 bfd_set_error (bfd_error_no_more_archived_files);
1405 return (bfd *) NULL;
1409 static boolean
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;
1420 return false;
1424 static int
1425 oasys_generic_stat_arch_elt (abfd, buf)
1426 bfd *abfd;
1427 struct stat *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);
1433 return -1;
1435 else
1437 buf->st_size = mod->size;
1438 buf->st_mode = 0666;
1439 return 0;
1443 static int
1444 oasys_sizeof_headers (abfd, exec)
1445 bfd *abfd ATTRIBUTE_UNUSED;
1446 boolean exec ATTRIBUTE_UNUSED;
1448 return 0;
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 **))) \
1458 bfd_true)
1459 #define oasys_truncate_arname bfd_dont_truncate_arname
1460 #define oasys_write_armap \
1461 ((boolean (*) \
1462 PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
1463 bfd_true)
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
1494 /*SUPPRESS 460 */
1495 const bfd_target oasys_vec =
1497 "oasys", /* name */
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 */
1516 {_bfd_dummy_target,
1517 oasys_object_p, /* bfd_check_format */
1518 oasys_archive_p,
1519 _bfd_dummy_target,
1521 { /* bfd_set_format */
1522 bfd_false,
1523 oasys_mkobject,
1524 _bfd_generic_mkarchive,
1525 bfd_false
1527 { /* bfd_write_contents */
1528 bfd_false,
1529 oasys_write_object_contents,
1530 _bfd_write_archive_contents,
1531 bfd_false,
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),
1544 NULL,
1546 (PTR) 0