xref: /openbsd/gnu/usr.bin/binutils/bfd/i386os9k.c (revision db3296cf)
1 /* BFD back-end for os9000 i386 binaries.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
3    Free Software Foundation, Inc.
4    Written by Cygnus Support.
5 
6 This file is part of BFD, the Binary File Descriptor library.
7 
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.
12 
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.
17 
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.  */
21 
22 
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"
29 
30 static const bfd_target *os9k_callback PARAMS ((bfd *));
31 
32 /* Swaps the information in an executable header taken from a raw byte
33    stream memory image, into the internal exec_header structure.  */
34 boolean
35 os9k_swap_exec_header_in (abfd, raw_bytes, execp)
36      bfd *abfd;
37      mh_com *raw_bytes;
38      struct internal_exec *execp;
39 {
40   mh_com *bytes = (mh_com *) raw_bytes;
41   unsigned int dload, dmemsize, dmemstart;
42 
43   /* Now fill in fields in the execp, from the bytes in the raw data.  */
44   execp->a_info = bfd_h_get_16 (abfd, bytes->m_sync);
45   execp->a_syms = 0;
46   execp->a_entry = bfd_h_get_32 (abfd, bytes->m_exec);
47   execp->a_talign = 2;
48   execp->a_dalign = 2;
49   execp->a_balign = 2;
50 
51   dload = bfd_h_get_32 (abfd, bytes->m_idata);
52   execp->a_data = dload + 8;
53 
54   if (bfd_seek (abfd, (file_ptr) dload, SEEK_SET) != 0
55       || (bfd_read (&dmemstart, sizeof (dmemstart), 1, abfd)
56 	  != sizeof (dmemstart))
57       || (bfd_read (&dmemsize, sizeof (dmemsize), 1, abfd)
58 	  != sizeof (dmemsize)))
59     return false;
60 
61   execp->a_tload = 0;
62   execp->a_dload = bfd_h_get_32 (abfd, (unsigned char *) &dmemstart);
63   execp->a_text = dload - execp->a_tload;
64   execp->a_data = bfd_h_get_32 (abfd, (unsigned char *) &dmemsize);
65   execp->a_bss = bfd_h_get_32 (abfd, bytes->m_data) - execp->a_data;
66 
67   execp->a_trsize = 0;
68   execp->a_drsize = 0;
69 
70   return true;
71 }
72 
73 #if 0
74 /* Swaps the information in an internal exec header structure into the
75    supplied buffer ready for writing to disk.  */
76 
77 PROTO (void, os9k_swap_exec_header_out,
78          (bfd * abfd,
79 	  struct internal_exec * execp,
80 	  struct mh_com * raw_bytes));
81 void
82 os9k_swap_exec_header_out (abfd, execp, raw_bytes)
83      bfd *abfd;
84      struct internal_exec *execp;
85      mh_com *raw_bytes;
86 {
87   mh_com *bytes = (mh_com *) raw_bytes;
88 
89   /* Now fill in fields in the raw data, from the fields in the exec struct. */
90   bfd_h_put_32 (abfd, execp->a_info, bytes->e_info);
91   bfd_h_put_32 (abfd, execp->a_text, bytes->e_text);
92   bfd_h_put_32 (abfd, execp->a_data, bytes->e_data);
93   bfd_h_put_32 (abfd, execp->a_bss, bytes->e_bss);
94   bfd_h_put_32 (abfd, execp->a_syms, bytes->e_syms);
95   bfd_h_put_32 (abfd, execp->a_entry, bytes->e_entry);
96   bfd_h_put_32 (abfd, execp->a_trsize, bytes->e_trsize);
97   bfd_h_put_32 (abfd, execp->a_drsize, bytes->e_drsize);
98   bfd_h_put_32 (abfd, execp->a_tload, bytes->e_tload);
99   bfd_h_put_32 (abfd, execp->a_dload, bytes->e_dload);
100   bytes->e_talign[0] = execp->a_talign;
101   bytes->e_dalign[0] = execp->a_dalign;
102   bytes->e_balign[0] = execp->a_balign;
103   bytes->e_relaxable[0] = execp->a_relaxable;
104 }
105 
106 #endif	/* 0 */
107 
108 static const bfd_target *
109 os9k_object_p (abfd)
110      bfd *abfd;
111 {
112   struct internal_exec anexec;
113   mh_com exec_bytes;
114 
115   if (bfd_read ((PTR) & exec_bytes, MHCOM_BYTES_SIZE, 1, abfd)
116       != MHCOM_BYTES_SIZE)
117     {
118       if (bfd_get_error () != bfd_error_system_call)
119 	bfd_set_error (bfd_error_wrong_format);
120       return 0;
121     }
122 
123   anexec.a_info = bfd_h_get_16 (abfd, exec_bytes.m_sync);
124   if (N_BADMAG (anexec))
125     {
126       bfd_set_error (bfd_error_wrong_format);
127       return 0;
128     }
129 
130   if (! os9k_swap_exec_header_in (abfd, &exec_bytes, &anexec))
131     {
132       if (bfd_get_error () != bfd_error_system_call)
133 	bfd_set_error (bfd_error_wrong_format);
134       return NULL;
135     }
136   return aout_32_some_aout_object_p (abfd, &anexec, os9k_callback);
137 }
138 
139 
140 /* Finish up the opening of a b.out file for reading.  Fill in all the
141    fields that are not handled by common code.  */
142 
143 static const bfd_target *
144 os9k_callback (abfd)
145      bfd *abfd;
146 {
147   struct internal_exec *execp = exec_hdr (abfd);
148   unsigned long bss_start;
149 
150   /* Architecture and machine type */
151   bfd_set_arch_mach (abfd, bfd_arch_i386, 0);
152 
153   /* The positions of the string table and symbol table.  */
154   obj_str_filepos (abfd) = 0;
155   obj_sym_filepos (abfd) = 0;
156 
157   /* The alignments of the sections */
158   obj_textsec (abfd)->alignment_power = execp->a_talign;
159   obj_datasec (abfd)->alignment_power = execp->a_dalign;
160   obj_bsssec (abfd)->alignment_power = execp->a_balign;
161 
162   /* The starting addresses of the sections.  */
163   obj_textsec (abfd)->vma = execp->a_tload;
164   obj_datasec (abfd)->vma = execp->a_dload;
165 
166   /* And reload the sizes, since the aout module zaps them */
167   obj_textsec (abfd)->_raw_size = execp->a_text;
168 
169   bss_start = execp->a_dload + execp->a_data;	/* BSS = end of data section */
170   obj_bsssec (abfd)->vma = align_power (bss_start, execp->a_balign);
171 
172   /* The file positions of the sections */
173   obj_textsec (abfd)->filepos = execp->a_entry;
174   obj_datasec (abfd)->filepos = execp->a_dload;
175 
176   /* The file positions of the relocation info ***
177   obj_textsec (abfd)->rel_filepos = N_TROFF(*execp);
178   obj_datasec (abfd)->rel_filepos =  N_DROFF(*execp);
179   */
180 
181   adata (abfd).page_size = 1;	/* Not applicable. */
182   adata (abfd).segment_size = 1;/* Not applicable. */
183   adata (abfd).exec_bytes_size = MHCOM_BYTES_SIZE;
184 
185   return abfd->xvec;
186 }
187 
188 #if 0
189 struct bout_data_struct
190 {
191   struct aoutdata a;
192   struct internal_exec e;
193 };
194 
195 static boolean
196 os9k_mkobject (abfd)
197      bfd *abfd;
198 {
199   struct bout_data_struct *rawptr;
200 
201   rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
202   if (rawptr == NULL)
203     return false;
204 
205   abfd->tdata.bout_data = rawptr;
206   exec_hdr (abfd) = &rawptr->e;
207 
208   obj_textsec (abfd) = (asection *) NULL;
209   obj_datasec (abfd) = (asection *) NULL;
210   obj_bsssec (abfd) = (asection *) NULL;
211 
212   return true;
213 }
214 
215 static boolean
216 os9k_write_object_contents (abfd)
217      bfd *abfd;
218 {
219   struct external_exec swapped_hdr;
220 
221   if (! aout_32_make_sections (abfd))
222     return false;
223 
224   exec_hdr (abfd)->a_info = BMAGIC;
225 
226   exec_hdr (abfd)->a_text = obj_textsec (abfd)->_raw_size;
227   exec_hdr (abfd)->a_data = obj_datasec (abfd)->_raw_size;
228   exec_hdr (abfd)->a_bss = obj_bsssec (abfd)->_raw_size;
229   exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd) * sizeof (struct nlist);
230   exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
231   exec_hdr (abfd)->a_trsize = ((obj_textsec (abfd)->reloc_count) *
232 			       sizeof (struct relocation_info));
233   exec_hdr (abfd)->a_drsize = ((obj_datasec (abfd)->reloc_count) *
234 			       sizeof (struct relocation_info));
235 
236   exec_hdr (abfd)->a_talign = obj_textsec (abfd)->alignment_power;
237   exec_hdr (abfd)->a_dalign = obj_datasec (abfd)->alignment_power;
238   exec_hdr (abfd)->a_balign = obj_bsssec (abfd)->alignment_power;
239 
240   exec_hdr (abfd)->a_tload = obj_textsec (abfd)->vma;
241   exec_hdr (abfd)->a_dload = obj_datasec (abfd)->vma;
242 
243   bout_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
244 
245   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
246       || (bfd_write ((PTR) & swapped_hdr, 1, EXEC_BYTES_SIZE, abfd)
247 	  != EXEC_BYTES_SIZE))
248     return false;
249 
250   /* Now write out reloc info, followed by syms and strings */
251   if (bfd_get_symcount (abfd) != 0)
252     {
253       if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (*exec_hdr (abfd))), SEEK_SET)
254 	  != 0)
255 	return false;
256 
257       if (!aout_32_write_syms (abfd))
258 	return false;
259 
260       if (bfd_seek (abfd, (file_ptr) (N_TROFF (*exec_hdr (abfd))), SEEK_SET)
261 	  != 0)
262 	return false;
263 
264       if (!b_out_squirt_out_relocs (abfd, obj_textsec (abfd)))
265 	return false;
266       if (bfd_seek (abfd, (file_ptr) (N_DROFF (*exec_hdr (abfd))), SEEK_SET)
267 	  != 0)
268 	return false;
269 
270       if (!b_out_squirt_out_relocs (abfd, obj_datasec (abfd)))
271 	return false;
272     }
273   return true;
274 }
275 
276 static boolean
277 os9k_set_section_contents (abfd, section, location, offset, count)
278      bfd *abfd;
279      sec_ptr section;
280      unsigned char *location;
281      file_ptr offset;
282      int count;
283 {
284 
285   if (abfd->output_has_begun == false)
286     {				/* set by bfd.c handler */
287       if (! aout_32_make_sections (abfd))
288 	return false;
289 
290       obj_textsec (abfd)->filepos = sizeof (struct internal_exec);
291       obj_datasec (abfd)->filepos = obj_textsec (abfd)->filepos
292 	+ obj_textsec (abfd)->_raw_size;
293 
294     }
295   /* regardless, once we know what we're doing, we might as well get going */
296   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
297     return false;
298 
299   if (count != 0)
300     {
301       return (bfd_write ((PTR) location, 1, count, abfd) == count) ? true : false;
302     }
303   return true;
304 }
305 #endif	/* 0 */
306 
307 static int
308 os9k_sizeof_headers (ignore_abfd, ignore)
309      bfd *ignore_abfd ATTRIBUTE_UNUSED;
310      boolean ignore ATTRIBUTE_UNUSED;
311 {
312   return sizeof (struct internal_exec);
313 }
314 
315 
316 /***********************************************************************/
317 
318 #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
319 
320 #define aout_32_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
321 
322 #define aout_32_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
323 
324 #define aout_32_get_section_contents_in_window \
325   _bfd_generic_get_section_contents_in_window
326 
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_link_hash_table_create _bfd_generic_link_hash_table_create
332 #define os9k_bfd_link_add_symbols _bfd_generic_link_add_symbols
333 #define os9k_bfd_final_link _bfd_generic_final_link
334 #define os9k_bfd_link_split_section  _bfd_generic_link_split_section
335 
336 const bfd_target i386os9k_vec =
337 {
338   "i386os9k",			/* name */
339   bfd_target_os9k_flavour,
340   BFD_ENDIAN_LITTLE,		/* data byte order is little */
341   BFD_ENDIAN_LITTLE,		/* hdr byte order is little */
342   (HAS_RELOC | EXEC_P | WP_TEXT),	/* object flags */
343   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD),	/* section flags */
344   0,				/* symbol leading char */
345   ' ',				/* ar_pad_char */
346   16,				/* ar_max_namelen */
347 
348   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
349   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
350   bfd_getl16, bfd_getl_signed_16, bfd_putl16,	/* data */
351   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
352   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
353   bfd_getl16, bfd_getl_signed_16, bfd_putl16,	/* hdrs */
354   {_bfd_dummy_target, os9k_object_p,	/* bfd_check_format */
355    bfd_generic_archive_p, _bfd_dummy_target},
356   {bfd_false, bfd_false,	/* bfd_set_format */
357    _bfd_generic_mkarchive, bfd_false},
358   {bfd_false, bfd_false,	/* bfd_write_contents */
359    _bfd_write_archive_contents, bfd_false},
360 
361      BFD_JUMP_TABLE_GENERIC (aout_32),
362      BFD_JUMP_TABLE_COPY (_bfd_generic),
363      BFD_JUMP_TABLE_CORE (_bfd_nocore),
364      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
365      BFD_JUMP_TABLE_SYMBOLS (aout_32),
366      BFD_JUMP_TABLE_RELOCS (aout_32),
367      BFD_JUMP_TABLE_WRITE (aout_32),
368      BFD_JUMP_TABLE_LINK (os9k),
369      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
370 
371   NULL,
372 
373   (PTR) 0,
374 };
375