* Makefile.in (MAKEOVERRIDES): Define to be empty.
[deliverable/binutils-gdb.git] / bfd / aout-adobe.c
CommitLineData
294eaca4 1/* BFD back-end for a.out.adobe binaries.
0dc1bc8b 2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
294eaca4
SC
3 Written by Cygnus Support. Based on bout.c.
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
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24
25#include "aout/adobe.h"
26
27#include "aout/stab_gnu.h"
28#include "libaout.h" /* BFD a.out internal data structures */
29
30extern bfd_target a_out_adobe_vec; /* Forward decl */
31
32PROTO (static bfd_target *, aout_adobe_callback, (bfd *));
33
34PROTO (boolean, aout_32_slurp_symbol_table, (bfd *abfd));
35PROTO (void , aout_32_write_syms, ());
36PROTO (static void, aout_adobe_write_section, (bfd *abfd, sec_ptr sect));
37
38/* Swaps the information in an executable header taken from a raw byte
39 stream memory image, into the internal exec_header structure. */
40
41PROTO(void, aout_adobe_swap_exec_header_in,
42 (bfd *abfd,
43 struct external_exec *raw_bytes,
44 struct internal_exec *execp));
45
46void
47DEFUN(aout_adobe_swap_exec_header_in,(abfd, raw_bytes, execp),
48 bfd *abfd AND
49 struct external_exec *raw_bytes AND
50 struct internal_exec *execp)
51{
52 struct external_exec *bytes = (struct external_exec *)raw_bytes;
53
54 /* Now fill in fields in the execp, from the bytes in the raw data. */
55 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
56 execp->a_text = GET_WORD (abfd, bytes->e_text);
57 execp->a_data = GET_WORD (abfd, bytes->e_data);
58 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
59 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
60 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
61 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
62 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
63}
64
65/* Swaps the information in an internal exec header structure into the
66 supplied buffer ready for writing to disk. */
67
68PROTO(void, aout_adobe_swap_exec_header_out,
69 (bfd *abfd,
70 struct internal_exec *execp,
71 struct external_exec *raw_bytes));
72void
73DEFUN(aout_adobe_swap_exec_header_out,(abfd, execp, raw_bytes),
74 bfd *abfd AND
75 struct internal_exec *execp AND
76 struct external_exec *raw_bytes)
77{
78 struct external_exec *bytes = (struct external_exec *)raw_bytes;
79
80 /* Now fill in fields in the raw data, from the fields in the exec struct. */
81 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
82 PUT_WORD (abfd, execp->a_text , bytes->e_text);
83 PUT_WORD (abfd, execp->a_data , bytes->e_data);
84 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
85 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
86 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
87 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
88 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
89}
90
91
92static bfd_target *
93aout_adobe_object_p (abfd)
94 bfd *abfd;
95{
96 struct internal_exec anexec;
97 struct external_exec exec_bytes;
98 char *targ;
99
100 if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
101 != EXEC_BYTES_SIZE) {
102 bfd_error = wrong_format;
103 return 0;
104 }
105
106 anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
107
108 /* Normally we just compare for the magic number.
109 However, a bunch of Adobe tools aren't fixed up yet; they generate
110 files using ZMAGIC(!).
111 If the environment variable GNUTARGET is set to "a.out.adobe", we will
112 take just about any a.out file as an Adobe a.out file. FIXME! */
113
114 if (N_BADMAG (anexec)) {
115 targ = getenv ("GNUTARGET");
0dc1bc8b 116 if (targ && !strcmp (targ, a_out_adobe_vec.name))
294eaca4
SC
117 ; /* Just continue anyway, if specifically set to this format */
118 else
119 {
120 bfd_error = wrong_format;
121 return 0;
122 }
123 }
124
125 aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
126 return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
127}
128
129
130/* Finish up the opening of a b.out file for reading. Fill in all the
131 fields that are not handled by common code. */
132
133static bfd_target *
134aout_adobe_callback (abfd)
135 bfd *abfd;
136{
137 struct internal_exec *execp = exec_hdr (abfd);
294eaca4
SC
138 asection *sect;
139 struct external_segdesc ext[1];
140 char *section_name;
141 char try_again[30]; /* name and number */
142 char *newname;
143 int trynum;
144 flagword flags;
145
146 /* Architecture and machine type -- unknown in this format. */
147 bfd_set_arch_mach(abfd, bfd_arch_unknown, 0);
148
149 /* The positions of the string table and symbol table. */
150 obj_str_filepos (abfd) = N_STROFF (*execp);
151 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
152
153 /* Suck up the section information from the file, one section at a time. */
154
155 for (;;) {
156 if (bfd_read ((PTR) ext, 1, sizeof (*ext), abfd) != sizeof (*ext)) {
157 bfd_error = wrong_format;
158 return 0;
159 }
160 switch (ext->e_type[0]) {
161 case N_TEXT:
162 section_name = ".text";
163 flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
164 break;
165
166 case N_DATA:
167 section_name = ".data";
168 flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
169 break;
170
171 case N_BSS:
172 section_name = ".bss";
173 flags = SEC_DATA | SEC_HAS_CONTENTS;
174 break;
175
176 case 0:
177 goto no_more_sections;
178
179 default:
180 fprintf (stderr, "Unknown section type in a.out.adobe file: %x\n",
0dc1bc8b 181 ext->e_type[0]);
294eaca4
SC
182 goto no_more_sections;
183 }
184
185 /* First one is called ".text" or whatever; subsequent ones are
186 ".text1", ".text2", ... */
187
188 bfd_error = no_error;
189 sect = bfd_make_section (abfd, section_name);
190 trynum = 0;
191 while (!sect) {
192 if (bfd_error != no_error)
193 return 0; /* Some other error -- slide into the sunset */
194 sprintf (try_again, "%s%d", section_name, ++trynum);
195 sect = bfd_make_section (abfd, try_again);
196 }
197
198 /* Fix the name, if it is a sprintf'd name. */
199 if (sect->name == try_again) {
200 newname = (char *) bfd_zalloc(abfd, strlen (sect->name));
201 if (newname == NULL) {
202 bfd_error = no_memory;
203 return 0;
204 }
205 strcpy (newname, sect->name);
206 sect->name = newname;
207 }
208
209 /* Now set the section's attributes. */
210 bfd_set_section_flags (abfd, sect, flags);
211 sect->_raw_size = ((ext->e_size[0] << 8) /* Assumed big-endian */
212 | ext->e_size[1] << 8)
213 | ext->e_size[2];
214 sect->_cooked_size = sect->_raw_size;
215 sect->vma = bfd_h_get_32 (abfd, ext->e_virtbase);
216 sect->filepos = bfd_h_get_32 (abfd, ext->e_filebase);
217 /* FIXME XXX alignment? */
218
219 /* Set relocation information for first section of each type. */
220 if (trynum == 0) switch (ext->e_type[0]) {
221 case N_TEXT:
222 sect->rel_filepos = N_TRELOFF (*execp);
223 sect->reloc_count = execp->a_trsize;
224 break;
225
226 case N_DATA:
227 sect->rel_filepos = N_DRELOFF (*execp);
228 sect->reloc_count = execp->a_drsize;
229 break;
230 }
231 }
232no_more_sections:
233
234 adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
235 adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
236 adata(abfd).page_size = 1; /* Not applicable. */
237 adata(abfd).segment_size = 1; /* Not applicable. */
238 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
239
240 return abfd->xvec;
241}
242
243struct bout_data_struct {
244 struct aoutdata a;
245 struct internal_exec e;
246};
247
248static boolean
249aout_adobe_mkobject (abfd)
250 bfd *abfd;
251{
252 struct bout_data_struct *rawptr;
253
254 rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
255 if (rawptr == NULL) {
256 bfd_error = no_memory;
257 return false;
258 }
259
260 abfd->tdata.bout_data = rawptr;
261 exec_hdr (abfd) = &rawptr->e;
262
263 adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
264 adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
265 adata(abfd).page_size = 1; /* Not applicable. */
266 adata(abfd).segment_size = 1; /* Not applicable. */
267 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
268
269 return true;
270}
271
272
273static boolean
274aout_adobe_write_object_contents (abfd)
275 bfd *abfd;
276{
277 struct external_exec swapped_hdr;
278 static struct external_segdesc sentinel[1] = {0};
279 asection *sect;
280
281 exec_hdr (abfd)->a_info = ZMAGIC;
282
283 /* Calculate text size as total of text sections, etc. */
284
285 exec_hdr (abfd)->a_text = 0;
286 exec_hdr (abfd)->a_data = 0;
287 exec_hdr (abfd)->a_bss = 0;
288 exec_hdr (abfd)->a_trsize = 0;
289 exec_hdr (abfd)->a_drsize = 0;
290
291 for (sect = abfd->sections; sect; sect = sect->next) {
292 if (sect->flags & SEC_CODE) {
293 exec_hdr (abfd)->a_text += sect->_raw_size;
294 exec_hdr (abfd)->a_trsize += sect->reloc_count *
295 sizeof (struct reloc_std_external);
296 } else if (sect->flags & SEC_DATA) {
297 exec_hdr (abfd)->a_data += sect->_raw_size;
298 exec_hdr (abfd)->a_drsize += sect->reloc_count *
299 sizeof (struct reloc_std_external);
300 } else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD)) {
301 exec_hdr (abfd)->a_bss += sect->_raw_size;
302 }
303 }
304
305 exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
306 * sizeof (struct external_nlist);
307 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
308
309 aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
310
f8e01940 311 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
294eaca4
SC
312 bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd);
313
314 /* Now write out the section information. Text first, data next, rest
315 afterward. */
316
317 for (sect = abfd->sections; sect; sect = sect->next) {
318 if (sect->flags & SEC_CODE) {
319 aout_adobe_write_section (abfd, sect);
320 }
321 }
322 for (sect = abfd->sections; sect; sect = sect->next) {
323 if (sect->flags & SEC_DATA) {
324 aout_adobe_write_section (abfd, sect);
325 }
326 }
327 for (sect = abfd->sections; sect; sect = sect->next) {
328 if (!(sect->flags & (SEC_CODE|SEC_DATA))) {
329 aout_adobe_write_section (abfd, sect);
330 }
331 }
332
333 /* Write final `sentinel` section header (with type of 0). */
334 bfd_write ((PTR) sentinel, 1, sizeof (*sentinel), abfd);
335
336 /* Now write out reloc info, followed by syms and strings */
337 if (bfd_get_symcount (abfd) != 0)
338 {
f8e01940 339 bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET);
294eaca4
SC
340
341 aout_32_write_syms (abfd);
342
f8e01940 343 bfd_seek (abfd, (file_ptr)(N_TRELOFF(*exec_hdr(abfd))), SEEK_SET);
294eaca4
SC
344
345 for (sect = abfd->sections; sect; sect = sect->next) {
346 if (sect->flags & SEC_CODE) {
347 if (!aout_32_squirt_out_relocs (abfd, sect))
348 return false;
349 }
350 }
351
f8e01940 352 bfd_seek (abfd, (file_ptr)(N_DRELOFF(*exec_hdr(abfd))), SEEK_SET);
294eaca4
SC
353
354 for (sect = abfd->sections; sect; sect = sect->next) {
355 if (sect->flags & SEC_DATA) {
356 if (!aout_32_squirt_out_relocs (abfd, sect))
357 return false;
358 }
359 }
360 }
361 return true;
362}
363
364static void
365aout_adobe_write_section (abfd, sect)
366 bfd *abfd;
367 sec_ptr sect;
368{
369 /* FIXME XXX */
370}
371\f
372static boolean
373aout_adobe_set_section_contents (abfd, section, location, offset, count)
374 bfd *abfd;
375 sec_ptr section;
376 unsigned char *location;
377 file_ptr offset;
378 int count;
379{
380 file_ptr section_start;
381 sec_ptr sect;
382
383 if (abfd->output_has_begun == false) { /* set by bfd.c handler */
384
385 /* Assign file offsets to sections. Text sections are first, and
386 are contiguous. Then data sections. Everything else at the end. */
387
388 section_start = N_TXTOFF (ignore<-->me);
389
390 for (sect = abfd->sections; sect; sect = sect->next) {
391 if (sect->flags & SEC_CODE) {
392 sect->filepos = section_start;
393 /* FIXME: Round to alignment */
394 section_start += sect->_raw_size;
395 }
396 }
397
398 for (sect = abfd->sections; sect; sect = sect->next) {
399 if (sect->flags & SEC_DATA) {
400 sect->filepos = section_start;
401 /* FIXME: Round to alignment */
402 section_start += sect->_raw_size;
403 }
404 }
405
406 for (sect = abfd->sections; sect; sect = sect->next) {
407 if (sect->flags & SEC_HAS_CONTENTS &&
408 !(sect->flags & (SEC_CODE|SEC_DATA))) {
409 sect->filepos = section_start;
410 /* FIXME: Round to alignment */
411 section_start += sect->_raw_size;
412 }
413 }
414 }
415
416 /* regardless, once we know what we're doing, we might as well get going */
417 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
418
419 if (count != 0) {
420 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
421 }
422 return true;
423}
424
425static boolean
426aout_adobe_set_arch_mach (abfd, arch, machine)
427 bfd *abfd;
428 enum bfd_architecture arch;
429 unsigned long machine;
430{
431 bfd_default_set_arch_mach(abfd, arch, machine);
432
433 if (arch == bfd_arch_unknown) /* Unknown machine arch is OK */
434 return true;
435
436 return false;
437}
438
439static int
440DEFUN(aout_adobe_sizeof_headers,(ignore_abfd, ignore),
441 bfd *ignore_abfd AND
442 boolean ignore)
443{
444 return sizeof(struct internal_exec);
445}
446
447
448
449
450/* Build the transfer vector for Adobe A.Out files. */
451
452/* We don't have core files. */
453#define aout_32_core_file_failing_command _bfd_dummy_core_file_failing_command
454#define aout_32_core_file_failing_signal _bfd_dummy_core_file_failing_signal
455#define aout_32_core_file_matches_executable_p \
456 _bfd_dummy_core_file_matches_executable_p
457
458/* We use BSD-Unix generic archive files. */
459#define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
460#define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
461#define aout_32_slurp_armap bfd_slurp_bsd_armap
462#define aout_32_slurp_extended_name_table bfd_true
463#define aout_32_write_armap bsd_write_armap
464#define aout_32_truncate_arname bfd_bsd_truncate_arname
465
466/* We override these routines from the usual a.out file routines. */
467#define aout_32_set_section_contents aout_adobe_set_section_contents
468#define aout_32_set_arch_mach aout_adobe_set_arch_mach
469#define aout_32_sizeof_headers aout_adobe_sizeof_headers
470
471#define aout_32_bfd_debug_info_start bfd_void
472#define aout_32_bfd_debug_info_end bfd_void
473#define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
474
475#define aout_32_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
476#define aout_32_bfd_relax_section bfd_generic_relax_section
0dc1bc8b 477#define aout_32_bfd_seclet_link bfd_generic_seclet_link
294eaca4
SC
478
479bfd_target a_out_adobe_vec =
480{
481 "a.out.adobe", /* name */
482 bfd_target_aout_flavour,
483 true, /* data byte order is unknown (big assumed) */
484 true, /* hdr byte order is big */
485 (HAS_RELOC | EXEC_P | /* object flags */
486 HAS_LINENO | HAS_DEBUG |
487 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT ),
488 /* section flags */
489 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
490 '_', /* symbol leading char */
491 ' ', /* ar_pad_char */
492 16, /* ar_max_namelen */
493 2, /* minumum alignment power */
494
495 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
496 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
497 {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format */
498 bfd_generic_archive_p, _bfd_dummy_target},
499 {bfd_false, aout_adobe_mkobject, /* bfd_set_format */
500 _bfd_generic_mkarchive, bfd_false},
501 {bfd_false, aout_adobe_write_object_contents, /* bfd_write_contents */
502 _bfd_write_archive_contents, bfd_false},
503
504 JUMP_TABLE(aout_32)
505 };
506
This page took 0.049244 seconds and 4 git commands to generate.