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