* alpha.c, basic_blocks.c, basic_blocks.h, bb_exit_func.c,
[binutils.git] / bfd / i386os9k.c
blob3be6bdd92b028ee19a92b8fb963dfb0c1a0cce7a
1 /* BFD back-end for os9000 i386 binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002,
3 2004 Free Software Foundation, Inc.
4 Written by Cygnus Support.
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. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "libaout.h" /* BFD a.out internal data structures */
28 #include "os9k.h"
30 static const bfd_target * os9k_callback
31 PARAMS ((bfd *));
32 static const bfd_target * os9k_object_p
33 PARAMS ((bfd *));
34 static int os9k_sizeof_headers
35 PARAMS ((bfd *, bfd_boolean));
36 bfd_boolean os9k_swap_exec_header_in
37 PARAMS ((bfd *, mh_com *, struct internal_exec *));
39 /* Swaps the information in an executable header taken from a raw byte
40 stream memory image, into the internal exec_header structure. */
41 bfd_boolean
42 os9k_swap_exec_header_in (abfd, raw_bytes, execp)
43 bfd *abfd;
44 mh_com *raw_bytes;
45 struct internal_exec *execp;
47 mh_com *bytes = (mh_com *) raw_bytes;
48 unsigned int dload, dmemsize, dmemstart;
50 /* Now fill in fields in the execp, from the bytes in the raw data. */
51 execp->a_info = H_GET_16 (abfd, bytes->m_sync);
52 execp->a_syms = 0;
53 execp->a_entry = H_GET_32 (abfd, bytes->m_exec);
54 execp->a_talign = 2;
55 execp->a_dalign = 2;
56 execp->a_balign = 2;
58 dload = H_GET_32 (abfd, bytes->m_idata);
59 execp->a_data = dload + 8;
61 if (bfd_seek (abfd, (file_ptr) dload, SEEK_SET) != 0
62 || (bfd_bread (&dmemstart, (bfd_size_type) sizeof (dmemstart), abfd)
63 != sizeof (dmemstart))
64 || (bfd_bread (&dmemsize, (bfd_size_type) sizeof (dmemsize), abfd)
65 != sizeof (dmemsize)))
66 return FALSE;
68 execp->a_tload = 0;
69 execp->a_dload = H_GET_32 (abfd, (unsigned char *) &dmemstart);
70 execp->a_text = dload - execp->a_tload;
71 execp->a_data = H_GET_32 (abfd, (unsigned char *) &dmemsize);
72 execp->a_bss = H_GET_32 (abfd, bytes->m_data) - execp->a_data;
74 execp->a_trsize = 0;
75 execp->a_drsize = 0;
77 return TRUE;
80 #if 0
81 /* Swaps the information in an internal exec header structure into the
82 supplied buffer ready for writing to disk. */
84 void os9k_swap_exec_header_out
85 PARAMS ((bfd *, struct internal_exec *, struct mh_com *));
87 void
88 os9k_swap_exec_header_out (abfd, execp, raw_bytes)
89 bfd *abfd;
90 struct internal_exec *execp;
91 mh_com *raw_bytes;
93 mh_com *bytes = (mh_com *) raw_bytes;
95 /* Now fill in fields in the raw data, from the fields in the exec struct. */
96 H_PUT_32 (abfd, execp->a_info, bytes->e_info);
97 H_PUT_32 (abfd, execp->a_text, bytes->e_text);
98 H_PUT_32 (abfd, execp->a_data, bytes->e_data);
99 H_PUT_32 (abfd, execp->a_bss, bytes->e_bss);
100 H_PUT_32 (abfd, execp->a_syms, bytes->e_syms);
101 H_PUT_32 (abfd, execp->a_entry, bytes->e_entry);
102 H_PUT_32 (abfd, execp->a_trsize, bytes->e_trsize);
103 H_PUT_32 (abfd, execp->a_drsize, bytes->e_drsize);
104 H_PUT_32 (abfd, execp->a_tload, bytes->e_tload);
105 H_PUT_32 (abfd, execp->a_dload, bytes->e_dload);
106 bytes->e_talign[0] = execp->a_talign;
107 bytes->e_dalign[0] = execp->a_dalign;
108 bytes->e_balign[0] = execp->a_balign;
109 bytes->e_relaxable[0] = execp->a_relaxable;
112 #endif /* 0 */
114 static const bfd_target *
115 os9k_object_p (abfd)
116 bfd *abfd;
118 struct internal_exec anexec;
119 mh_com exec_bytes;
121 if (bfd_bread ((PTR) &exec_bytes, (bfd_size_type) MHCOM_BYTES_SIZE, abfd)
122 != MHCOM_BYTES_SIZE)
124 if (bfd_get_error () != bfd_error_system_call)
125 bfd_set_error (bfd_error_wrong_format);
126 return 0;
129 anexec.a_info = H_GET_16 (abfd, exec_bytes.m_sync);
130 if (N_BADMAG (anexec))
132 bfd_set_error (bfd_error_wrong_format);
133 return 0;
136 if (! os9k_swap_exec_header_in (abfd, &exec_bytes, &anexec))
138 if (bfd_get_error () != bfd_error_system_call)
139 bfd_set_error (bfd_error_wrong_format);
140 return NULL;
142 return aout_32_some_aout_object_p (abfd, &anexec, os9k_callback);
146 /* Finish up the opening of a b.out file for reading. Fill in all the
147 fields that are not handled by common code. */
149 static const bfd_target *
150 os9k_callback (abfd)
151 bfd *abfd;
153 struct internal_exec *execp = exec_hdr (abfd);
154 unsigned long bss_start;
156 /* Architecture and machine type. */
157 bfd_set_arch_mach (abfd, bfd_arch_i386, 0);
159 /* The positions of the string table and symbol table. */
160 obj_str_filepos (abfd) = 0;
161 obj_sym_filepos (abfd) = 0;
163 /* The alignments of the sections. */
164 obj_textsec (abfd)->alignment_power = execp->a_talign;
165 obj_datasec (abfd)->alignment_power = execp->a_dalign;
166 obj_bsssec (abfd)->alignment_power = execp->a_balign;
168 /* The starting addresses of the sections. */
169 obj_textsec (abfd)->vma = execp->a_tload;
170 obj_datasec (abfd)->vma = execp->a_dload;
172 /* And reload the sizes, since the aout module zaps them. */
173 obj_textsec (abfd)->_raw_size = execp->a_text;
175 bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section. */
176 obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
178 /* The file positions of the sections. */
179 obj_textsec (abfd)->filepos = execp->a_entry;
180 obj_datasec (abfd)->filepos = execp->a_dload;
182 /* The file positions of the relocation info ***
183 obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
184 obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); */
186 adata (abfd).page_size = 1; /* Not applicable. */
187 adata (abfd).segment_size = 1;/* Not applicable. */
188 adata (abfd).exec_bytes_size = MHCOM_BYTES_SIZE;
190 return abfd->xvec;
193 #if 0
194 struct bout_data_struct
196 struct aoutdata a;
197 struct internal_exec e;
200 static bfd_boolean
201 os9k_mkobject (abfd)
202 bfd *abfd;
204 struct bout_data_struct *rawptr;
205 bfd_size_type amt = sizeof (struct bout_data_struct);
207 rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, amt);
208 if (rawptr == NULL)
209 return FALSE;
211 abfd->tdata.bout_data = rawptr;
212 exec_hdr (abfd) = &rawptr->e;
214 obj_textsec (abfd) = (asection *) NULL;
215 obj_datasec (abfd) = (asection *) NULL;
216 obj_bsssec (abfd) = (asection *) NULL;
218 return TRUE;
221 static bfd_boolean
222 os9k_write_object_contents (abfd)
223 bfd *abfd;
225 struct external_exec swapped_hdr;
227 if (! aout_32_make_sections (abfd))
228 return FALSE;
230 exec_hdr (abfd)->a_info = BMAGIC;
232 exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
233 exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
234 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
235 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
236 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
237 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
238 sizeof (struct relocation_info));
239 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
240 sizeof (struct relocation_info));
242 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
243 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
244 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
246 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
247 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
249 bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
251 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
252 || bfd_bwrite ((PTR) & swapped_hdr, (bfd_size_type) EXEC_BYTES_SIZE,
253 abfd) != EXEC_BYTES_SIZE)
254 return FALSE;
256 /* Now write out reloc info, followed by syms and strings. */
257 if (bfd_get_symcount (abfd) != 0)
259 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
260 != 0)
261 return FALSE;
263 if (!aout_32_write_syms (abfd))
264 return FALSE;
266 if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
267 != 0)
268 return FALSE;
270 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
271 return FALSE;
272 if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
273 != 0)
274 return FALSE;
276 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
277 return FALSE;
279 return TRUE;
282 static bfd_boolean
283 os9k_set_section_contents (abfd, section, location, offset, count)
284 bfd *abfd;
285 sec_ptr section;
286 unsigned char *location;
287 file_ptr offset;
288 int count;
291 if (! abfd->output_has_begun)
292 { /* set by bfd.c handler */
293 if (! aout_32_make_sections (abfd))
294 return FALSE;
296 obj_textsec (abfd)->filepos = sizeof (struct internal_exec);
297 obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos
298 + obj_textsec (abfd)->_raw_size;
301 /* Regardless, once we know what we're doing, we might as well get going. */
302 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
303 return FALSE;
305 if (count != 0)
306 return bfd_bwrite ((PTR) location, (bfd_size_type) count, abfd) == count;
308 return TRUE;
310 #endif /* 0 */
312 static int
313 os9k_sizeof_headers (ignore_abfd, ignore)
314 bfd *ignore_abfd ATTRIBUTE_UNUSED;
315 bfd_boolean ignore ATTRIBUTE_UNUSED;
317 return sizeof (struct internal_exec);
322 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
324 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
326 #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
328 #define aout_32_get_section_contents_in_window \
329 _bfd_generic_get_section_contents_in_window
331 #define os9k_bfd_get_relocated_section_contents \
332 bfd_generic_get_relocated_section_contents
333 #define os9k_bfd_relax_section bfd_generic_relax_section
334 #define os9k_bfd_gc_sections bfd_generic_gc_sections
335 #define os9k_bfd_merge_sections bfd_generic_merge_sections
336 #define os9k_bfd_is_group_section bfd_generic_is_group_section
337 #define os9k_bfd_discard_group bfd_generic_discard_group
338 #define os9k_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
339 #define os9k_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
340 #define os9k_bfd_link_add_symbols _bfd_generic_link_add_symbols
341 #define os9k_bfd_link_just_syms _bfd_generic_link_just_syms
342 #define os9k_bfd_final_link _bfd_generic_final_link
343 #define os9k_bfd_link_split_section _bfd_generic_link_split_section
345 const bfd_target i386os9k_vec =
347 "i386os9k", /* name */
348 bfd_target_os9k_flavour,
349 BFD_ENDIAN_LITTLE, /* data byte order is little */
350 BFD_ENDIAN_LITTLE, /* hdr byte order is little */
351 (HAS_RELOC | EXEC_P | WP_TEXT), /* object flags */
352 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD), /* section flags */
353 0, /* symbol leading char */
354 ' ', /* ar_pad_char */
355 16, /* ar_max_namelen */
357 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
358 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
359 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
360 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
361 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
362 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
363 {_bfd_dummy_target, os9k_object_p, /* bfd_check_format */
364 bfd_generic_archive_p, _bfd_dummy_target},
365 {bfd_false, bfd_false, /* bfd_set_format */
366 _bfd_generic_mkarchive, bfd_false},
367 {bfd_false, bfd_false, /* bfd_write_contents */
368 _bfd_write_archive_contents, bfd_false},
370 BFD_JUMP_TABLE_GENERIC (aout_32),
371 BFD_JUMP_TABLE_COPY (_bfd_generic),
372 BFD_JUMP_TABLE_CORE (_bfd_nocore),
373 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
374 BFD_JUMP_TABLE_SYMBOLS (aout_32),
375 BFD_JUMP_TABLE_RELOCS (aout_32),
376 BFD_JUMP_TABLE_WRITE (aout_32),
377 BFD_JUMP_TABLE_LINK (os9k),
378 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
380 NULL,
382 (PTR) 0,