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