Tue Apr 9 15:13:10 2002 J"orn Rennecke <joern.rennecke@superh.com>
[binutils.git] / bfd / i386os9k.c
blob099a7142fbae3f0a5f3bbf003129d8cb760a8def
1 /* BFD back-end for os9000 i386 binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999, 2001, 2002
3 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 PARAMS ((bfd *));
31 static const bfd_target * os9k_object_p PARAMS ((bfd *));
32 static int os9k_sizeof_headers PARAMS ((bfd *, boolean));
33 boolean os9k_swap_exec_header_in PARAMS ((bfd *, mh_com *, struct internal_exec *));
35 /* Swaps the information in an executable header taken from a raw byte
36 stream memory image, into the internal exec_header structure. */
37 boolean
38 os9k_swap_exec_header_in (abfd, raw_bytes, execp)
39 bfd *abfd;
40 mh_com *raw_bytes;
41 struct internal_exec *execp;
43 mh_com *bytes = (mh_com *) raw_bytes;
44 unsigned int dload, dmemsize, dmemstart;
46 /* Now fill in fields in the execp, from the bytes in the raw data. */
47 execp->a_info = H_GET_16 (abfd, bytes->m_sync);
48 execp->a_syms = 0;
49 execp->a_entry = H_GET_32 (abfd, bytes->m_exec);
50 execp->a_talign = 2;
51 execp->a_dalign = 2;
52 execp->a_balign = 2;
54 dload = H_GET_32 (abfd, bytes->m_idata);
55 execp->a_data = dload + 8;
57 if (bfd_seek (abfd, (file_ptr) dload, SEEK_SET) != 0
58 || (bfd_bread (&dmemstart, (bfd_size_type) sizeof (dmemstart), abfd)
59 != sizeof (dmemstart))
60 || (bfd_bread (&dmemsize, (bfd_size_type) sizeof (dmemsize), abfd)
61 != sizeof (dmemsize)))
62 return false;
64 execp->a_tload = 0;
65 execp->a_dload = H_GET_32 (abfd, (unsigned char *) &dmemstart);
66 execp->a_text = dload - execp->a_tload;
67 execp->a_data = H_GET_32 (abfd, (unsigned char *) &dmemsize);
68 execp->a_bss = H_GET_32 (abfd, bytes->m_data) - execp->a_data;
70 execp->a_trsize = 0;
71 execp->a_drsize = 0;
73 return true;
76 #if 0
77 /* Swaps the information in an internal exec header structure into the
78 supplied buffer ready for writing to disk. */
80 void os9k_swap_exec_header_out
81 PARAMS ((bfd *, struct internal_exec *, struct mh_com *));
83 void
84 os9k_swap_exec_header_out (abfd, execp, raw_bytes)
85 bfd *abfd;
86 struct internal_exec *execp;
87 mh_com *raw_bytes;
89 mh_com *bytes = (mh_com *) raw_bytes;
91 /* Now fill in fields in the raw data, from the fields in the exec struct. */
92 H_PUT_32 (abfd, execp->a_info, bytes->e_info);
93 H_PUT_32 (abfd, execp->a_text, bytes->e_text);
94 H_PUT_32 (abfd, execp->a_data, bytes->e_data);
95 H_PUT_32 (abfd, execp->a_bss, bytes->e_bss);
96 H_PUT_32 (abfd, execp->a_syms, bytes->e_syms);
97 H_PUT_32 (abfd, execp->a_entry, bytes->e_entry);
98 H_PUT_32 (abfd, execp->a_trsize, bytes->e_trsize);
99 H_PUT_32 (abfd, execp->a_drsize, bytes->e_drsize);
100 H_PUT_32 (abfd, execp->a_tload, bytes->e_tload);
101 H_PUT_32 (abfd, execp->a_dload, bytes->e_dload);
102 bytes->e_talign[0] = execp->a_talign;
103 bytes->e_dalign[0] = execp->a_dalign;
104 bytes->e_balign[0] = execp->a_balign;
105 bytes->e_relaxable[0] = execp->a_relaxable;
108 #endif /* 0 */
110 static const bfd_target *
111 os9k_object_p (abfd)
112 bfd *abfd;
114 struct internal_exec anexec;
115 mh_com exec_bytes;
117 if (bfd_bread ((PTR) &exec_bytes, (bfd_size_type) MHCOM_BYTES_SIZE, abfd)
118 != MHCOM_BYTES_SIZE)
120 if (bfd_get_error () != bfd_error_system_call)
121 bfd_set_error (bfd_error_wrong_format);
122 return 0;
125 anexec.a_info = H_GET_16 (abfd, exec_bytes.m_sync);
126 if (N_BADMAG (anexec))
128 bfd_set_error (bfd_error_wrong_format);
129 return 0;
132 if (! os9k_swap_exec_header_in (abfd, &exec_bytes, &anexec))
134 if (bfd_get_error () != bfd_error_system_call)
135 bfd_set_error (bfd_error_wrong_format);
136 return NULL;
138 return aout_32_some_aout_object_p (abfd, &anexec, os9k_callback);
142 /* Finish up the opening of a b.out file for reading. Fill in all the
143 fields that are not handled by common code. */
145 static const bfd_target *
146 os9k_callback (abfd)
147 bfd *abfd;
149 struct internal_exec *execp = exec_hdr (abfd);
150 unsigned long bss_start;
152 /* Architecture and machine type. */
153 bfd_set_arch_mach (abfd, bfd_arch_i386, 0);
155 /* The positions of the string table and symbol table. */
156 obj_str_filepos (abfd) = 0;
157 obj_sym_filepos (abfd) = 0;
159 /* The alignments of the sections. */
160 obj_textsec (abfd)->alignment_power = execp->a_talign;
161 obj_datasec (abfd)->alignment_power = execp->a_dalign;
162 obj_bsssec (abfd)->alignment_power = execp->a_balign;
164 /* The starting addresses of the sections. */
165 obj_textsec (abfd)->vma = execp->a_tload;
166 obj_datasec (abfd)->vma = execp->a_dload;
168 /* And reload the sizes, since the aout module zaps them. */
169 obj_textsec (abfd)->_raw_size = execp->a_text;
171 bss_start = execp->a_dload + execp->a_data; /* BSS = end of data section. */
172 obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
174 /* The file positions of the sections. */
175 obj_textsec (abfd)->filepos = execp->a_entry;
176 obj_datasec (abfd)->filepos = execp->a_dload;
178 /* The file positions of the relocation info ***
179 obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
180 obj_datasec (abfd)->rel_filepos = N_DROFF(*execp); */
182 adata (abfd).page_size = 1; /* Not applicable. */
183 adata (abfd).segment_size = 1;/* Not applicable. */
184 adata (abfd).exec_bytes_size = MHCOM_BYTES_SIZE;
186 return abfd->xvec;
189 #if 0
190 struct bout_data_struct
192 struct aoutdata a;
193 struct internal_exec e;
196 static boolean
197 os9k_mkobject (abfd)
198 bfd *abfd;
200 struct bout_data_struct *rawptr;
201 bfd_size_type amt = sizeof (struct bout_data_struct);
203 rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, amt);
204 if (rawptr == NULL)
205 return false;
207 abfd->tdata.bout_data = rawptr;
208 exec_hdr (abfd) = &rawptr->e;
210 obj_textsec (abfd) = (asection *) NULL;
211 obj_datasec (abfd) = (asection *) NULL;
212 obj_bsssec (abfd) = (asection *) NULL;
214 return true;
217 static boolean
218 os9k_write_object_contents (abfd)
219 bfd *abfd;
221 struct external_exec swapped_hdr;
223 if (! aout_32_make_sections (abfd))
224 return false;
226 exec_hdr (abfd)->a_info = BMAGIC;
228 exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
229 exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
230 exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
231 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
232 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
233 exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
234 sizeof (struct relocation_info));
235 exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
236 sizeof (struct relocation_info));
238 exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
239 exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
240 exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
242 exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
243 exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
245 bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
247 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
248 || bfd_bwrite ((PTR) & swapped_hdr, (bfd_size_type) EXEC_BYTES_SIZE,
249 abfd) != EXEC_BYTES_SIZE)
250 return false;
252 /* Now write out reloc info, followed by syms and strings. */
253 if (bfd_get_symcount (abfd) != 0)
255 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
256 != 0)
257 return false;
259 if (!aout_32_write_syms (abfd))
260 return false;
262 if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
263 != 0)
264 return false;
266 if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
267 return false;
268 if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
269 != 0)
270 return false;
272 if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
273 return false;
275 return true;
278 static boolean
279 os9k_set_section_contents (abfd, section, location, offset, count)
280 bfd *abfd;
281 sec_ptr section;
282 unsigned char *location;
283 file_ptr offset;
284 int count;
287 if (abfd->output_has_begun == false)
288 { /* set by bfd.c handler */
289 if (! aout_32_make_sections (abfd))
290 return false;
292 obj_textsec (abfd)->filepos = sizeof (struct internal_exec);
293 obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos
294 + obj_textsec (abfd)->_raw_size;
297 /* Regardless, once we know what we're doing, we might as well get going. */
298 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
299 return false;
301 if (count != 0)
302 return bfd_bwrite ((PTR) location, (bfd_size_type) count, abfd) == count;
304 return true;
306 #endif /* 0 */
308 static int
309 os9k_sizeof_headers (ignore_abfd, ignore)
310 bfd *ignore_abfd ATTRIBUTE_UNUSED;
311 boolean ignore ATTRIBUTE_UNUSED;
313 return sizeof (struct internal_exec);
318 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
320 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
322 #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
324 #define aout_32_get_section_contents_in_window \
325 _bfd_generic_get_section_contents_in_window
327 #define os9k_bfd_get_relocated_section_contents \
328 bfd_generic_get_relocated_section_contents
329 #define os9k_bfd_relax_section bfd_generic_relax_section
330 #define os9k_bfd_gc_sections bfd_generic_gc_sections
331 #define os9k_bfd_merge_sections bfd_generic_merge_sections
332 #define os9k_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
333 #define os9k_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
334 #define os9k_bfd_link_add_symbols _bfd_generic_link_add_symbols
335 #define os9k_bfd_final_link _bfd_generic_final_link
336 #define os9k_bfd_link_split_section _bfd_generic_link_split_section
338 const bfd_target i386os9k_vec =
340 "i386os9k", /* name */
341 bfd_target_os9k_flavour,
342 BFD_ENDIAN_LITTLE, /* data byte order is little */
343 BFD_ENDIAN_LITTLE, /* hdr byte order is little */
344 (HAS_RELOC | EXEC_P | WP_TEXT), /* object flags */
345 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD), /* section flags */
346 0, /* symbol leading char */
347 ' ', /* ar_pad_char */
348 16, /* ar_max_namelen */
350 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
351 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
352 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
353 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
354 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
355 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
356 {_bfd_dummy_target, os9k_object_p, /* bfd_check_format */
357 bfd_generic_archive_p, _bfd_dummy_target},
358 {bfd_false, bfd_false, /* bfd_set_format */
359 _bfd_generic_mkarchive, bfd_false},
360 {bfd_false, bfd_false, /* bfd_write_contents */
361 _bfd_write_archive_contents, bfd_false},
363 BFD_JUMP_TABLE_GENERIC (aout_32),
364 BFD_JUMP_TABLE_COPY (_bfd_generic),
365 BFD_JUMP_TABLE_CORE (_bfd_nocore),
366 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
367 BFD_JUMP_TABLE_SYMBOLS (aout_32),
368 BFD_JUMP_TABLE_RELOCS (aout_32),
369 BFD_JUMP_TABLE_WRITE (aout_32),
370 BFD_JUMP_TABLE_LINK (os9k),
371 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
373 NULL,
375 (PTR) 0,