1 /* A.out "format 1" file handling code
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
24 #include <a.out.sun4.h>
34 inheritd two core files and various implimentation files.
35 The file @code{aoutf1.h} contains the code for BFD's
36 a.out back end. Control over the generated back end is given by these
37 three preprocessor names:
40 This value should be either 32 or 64, depending upon the size of an
41 int in the target format. It changes the sizes of the structs which
42 perform the memory/disk mapping of structures.
44 The 64 bit backend may only be used if the host compiler supports 64
45 ints (eg with gcc), by defining the name @code{HOST_64_BIT}. With this
46 name defined, @emph{all} bfd operations are performed with 64bit
47 arithmetic, not just those to a 64bit target.
50 bit long longsIf bfd is being compiled with gcc, (or any other compiler which gives
53 It is structured in such a way that @code{#define}ing
54 the size of the architecture into a @code{#include}ing
55 it with different @code{#define}s present will alter the definitions
56 of various structures in include files and generate correct code for
61 void (*bfd_error_trap)();
63 static bfd_target *sunos4_callback ();
69 DEFUN(NAME(sunos,object_p), (abfd),
72 unsigned char magicbuf[4]; /* Raw bytes of magic number from file */
73 unsigned long magic; /* Swapped magic number */
75 bfd_error = system_call_error;
77 if (bfd_read ((PTR)magicbuf, 1 , 4, abfd) !=
80 magic = bfd_h_get_32 (abfd, magicbuf);
82 if (N_BADMAG (*((struct internal_exec *) &magic))) return 0;
84 return NAME(aout,some_aout_object_p) (abfd, sunos4_callback);
87 /* Determine the size of a relocation entry, based on the architecture */
89 DEFUN(choose_reloc_size,(abfd),
92 switch (abfd->obj_arch) {
95 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
98 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
103 /* Set parameters about this a.out file that are machine-dependent.
104 This routine is called from some_aout_object_p just before it returns. */
107 sunos4_callback (abfd)
110 struct internal_exec *execp = exec_hdr (abfd);
112 WORK_OUT_FILE_POSITIONS(abfd, execp);
114 /* Determine the architecture and machine type of the object file. */
115 switch (N_MACHTYPE (*exec_hdr (abfd))) {
118 abfd->obj_arch = bfd_arch_unknown;
119 abfd->obj_machine = 0;
123 abfd->obj_arch = bfd_arch_m68k;
124 abfd->obj_machine = 68010;
128 abfd->obj_arch = bfd_arch_m68k;
129 abfd->obj_machine = 68020;
133 abfd->obj_arch = bfd_arch_sparc;
134 abfd->obj_machine = 0;
138 abfd->obj_arch = bfd_arch_i386;
139 abfd->obj_machine = 0;
143 abfd->obj_arch = bfd_arch_a29k;
144 abfd->obj_machine = 0;
148 abfd->obj_arch = bfd_arch_obscure;
149 abfd->obj_machine = 0;
153 choose_reloc_size(abfd);
158 /* Write an object file in SunOS format.
159 Section contents have already been written. We write the
160 file header, symbols, and relocation. */
163 DEFUN(NAME(aout,sunos4_write_object_contents),(abfd),
167 bfd_size_type data_pad = 0;
168 struct external_exec exec_bytes;
169 struct internal_exec *execp = exec_hdr (abfd);
173 execp->a_text = obj_textsec (abfd)->size;
175 /* Magic number, maestro, please! */
176 switch (bfd_get_architecture(abfd)) {
178 switch (bfd_get_machine(abfd)) {
180 N_SET_MACHTYPE(*execp, M_68010);
184 N_SET_MACHTYPE(*execp, M_68020);
189 N_SET_MACHTYPE(*execp, M_SPARC);
192 N_SET_MACHTYPE(*execp, M_386);
195 N_SET_MACHTYPE(*execp, M_29K);
198 N_SET_MACHTYPE(*execp, M_UNKNOWN);
201 choose_reloc_size(abfd);
204 N_SET_FLAGS (*execp, 0x1);
206 WRITE_HEADERS(abfd, execp);
213 #define CORE_MAGIC 0x080456
214 #define CORE_NAMELEN 16
216 /* The core structure is taken from the Sun documentation.
217 Unfortunately, they don't document the FPA structure, or at least I
218 can't find it easily. Fortunately the core header contains its own
219 length. So this shouldn't cause problems, except for c_ucode, which
220 so far we don't use but is easy to find with a little arithmetic. */
222 /* But the reg structure can be gotten from the SPARC processor handbook.
223 This really should be in a GNU include file though so that gdb can use
247 /* Taken from Sun documentation: */
249 /* FIXME: It's worse than we expect. This struct contains TWO substructs
250 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
251 even portably access the stuff in between! */
254 int c_magic; /* Corefile magic number */
255 int c_len; /* Sizeof (struct core) */
256 struct regs c_regs; /* General purpose registers -- MACHDEP SIZE */
257 struct internal_exec c_aouthdr; /* A.out header */
258 int c_signo; /* Killing signal, if any */
259 int c_tsize; /* Text size (bytes) */
260 int c_dsize; /* Data size (bytes) */
261 int c_ssize; /* Stack size (bytes) */
262 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
263 double fp_stuff[1]; /* external FPU state (size unknown by us) */
264 /* The type "double" is critical here, for alignment.
265 SunOS declares a struct here, but the struct's alignment
266 is double since it contains doubles. */
267 int c_ucode; /* Exception no. from u_code */
268 /* (this member is not accessible by name since we don't
269 portably know the size of fp_stuff.) */
272 /* Supposedly the user stack grows downward from the bottom of kernel memory.
273 Presuming that this remains true, this definition will work. */
274 #define USRSTACK (-(128*1024*1024))
276 PROTO (static void, swapcore, (bfd *abfd, struct core *core));
278 /* need this cast b/c ptr is really void * */
279 #define core_hdr(bfd) (((struct suncordata *) (bfd->tdata))->hdr)
280 #define core_datasec(bfd) (((struct suncordata *) ((bfd)->tdata))->data_section)
281 #define core_stacksec(bfd) (((struct suncordata*)((bfd)->tdata))->stack_section)
282 #define core_regsec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg_section)
283 #define core_reg2sec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg2_section)
285 /* These are stored in the bfd's tdata */
287 struct core *hdr; /* core file header */
288 asection *data_section;
289 asection *stack_section;
290 asection *reg_section;
291 asection *reg2_section;
295 DEFUN(sunos4_core_file_p,(abfd),
298 unsigned char longbuf[4]; /* Raw bytes of various header fields */
304 bfd_error = system_call_error;
306 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
309 core_mag = bfd_h_get_32 (abfd, longbuf);
311 if (core_mag != CORE_MAGIC) return 0;
313 /* SunOS core headers can vary in length; second word is size; */
314 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
317 core_size = bfd_h_get_32 (abfd, longbuf);
319 if (core_size > 20000)
322 if (bfd_seek (abfd, 0L, false) < 0) return 0;
324 rawptr = bfd_zalloc (abfd, core_size + sizeof (struct suncordata));
325 if (rawptr == NULL) {
326 bfd_error = no_memory;
330 core = (struct core *) (rawptr + sizeof (struct suncordata));
332 if ((bfd_read ((PTR) core, 1, core_size, abfd)) != core_size) {
333 bfd_error = system_call_error;
334 bfd_release (abfd, rawptr);
338 swapcore (abfd, core);
339 set_tdata (abfd, ((struct suncordata *) rawptr));
340 core_hdr (abfd) = core;
342 /* create the sections. This is raunchy, but bfd_close wants to reclaim
344 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
345 if (core_stacksec (abfd) == NULL) {
347 bfd_error = no_memory;
348 bfd_release (abfd, rawptr);
351 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
352 if (core_datasec (abfd) == NULL) {
354 bfd_release (abfd, core_stacksec (abfd));
357 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
358 if (core_regsec (abfd) == NULL) {
360 bfd_release (abfd, core_datasec (abfd));
363 core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
364 if (core_reg2sec (abfd) == NULL) {
365 bfd_release (abfd, core_regsec (abfd));
369 core_stacksec (abfd)->name = ".stack";
370 core_datasec (abfd)->name = ".data";
371 core_regsec (abfd)->name = ".reg";
372 core_reg2sec (abfd)->name = ".reg2";
374 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
375 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
376 core_regsec (abfd)->flags = SEC_ALLOC;
377 core_reg2sec (abfd)->flags = SEC_ALLOC;
379 core_stacksec (abfd)->size = core->c_ssize;
380 core_datasec (abfd)->size = core->c_dsize;
381 core_regsec (abfd)->size = (sizeof core->c_regs);
382 /* Float regs take up end of struct, except c_ucode. */
383 core_reg2sec (abfd)->size = core_size - (sizeof core->c_ucode) -
384 (file_ptr)(((struct core *)0)->fp_stuff);
386 core_stacksec (abfd)->vma = (USRSTACK - core->c_ssize);
387 core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
388 core_regsec (abfd)->vma = -1;
389 core_reg2sec (abfd)->vma = -1;
391 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
392 core_datasec (abfd)->filepos = core->c_len;
393 /* In file header: */
394 core_regsec (abfd)->filepos = (file_ptr)(&((struct core *)0)->c_regs);
395 core_reg2sec (abfd)->filepos = (file_ptr)(((struct core *)0)->fp_stuff);
397 /* Align to word at least */
398 core_stacksec (abfd)->alignment_power = 2;
399 core_datasec (abfd)->alignment_power = 2;
400 core_regsec (abfd)->alignment_power = 2;
401 core_reg2sec (abfd)->alignment_power = 2;
403 abfd->sections = core_stacksec (abfd);
404 core_stacksec (abfd)->next = core_datasec (abfd);
405 core_datasec (abfd)->next = core_regsec (abfd);
406 core_regsec (abfd)->next = core_reg2sec (abfd);
408 abfd->section_count = 4;
413 static char *sunos4_core_file_failing_command (abfd)
416 return core_hdr (abfd)->c_cmdname;
420 DEFUN(sunos4_core_file_failing_signal,(abfd),
423 return core_hdr (abfd)->c_signo;
427 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
431 if (core_bfd->xvec != exec_bfd->xvec) {
432 bfd_error = system_call_error;
436 return (bcmp ((char *)&core_hdr (core_bfd), (char*) &exec_hdr (exec_bfd),
437 sizeof (struct internal_exec)) == 0) ? true : false;
440 /* byte-swap core structure */
441 /* FIXME, this needs more work to swap IN a core struct from raw bytes */
443 DEFUN(swapcore,(abfd, core),
447 struct external_exec exec_bytes;
449 core->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&core->c_magic);
450 core->c_len = bfd_h_get_32 (abfd, (unsigned char *)&core->c_len );
451 /* Leave integer registers in target byte order. */
452 bcopy ((char *)&(core->c_aouthdr), (char *)&exec_bytes, EXEC_BYTES_SIZE);
453 NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &core->c_aouthdr);
454 core->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&core->c_signo);
455 core->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_tsize);
456 core->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_dsize);
457 core->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_ssize);
458 /* Leave FP registers in target byte order. */
459 /* Leave "c_ucode" unswapped for now, since we can't find it easily. */
462 /* We use BFD generic archive files. */
463 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
464 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
465 #define aout_32_slurp_armap bfd_slurp_bsd_armap
466 #define aout_32_slurp_extended_name_table bfd_true
467 #define aout_32_write_armap bsd_write_armap
468 #define aout_32_truncate_arname bfd_bsd_truncate_arname
469 #define aout_32_machine_type sunos_machine_type
471 #define aout_32_core_file_failing_command sunos4_core_file_failing_command
472 #define aout_32_core_file_failing_signal sunos4_core_file_failing_signal
473 #define aout_32_core_file_matches_executable_p sunos4_core_file_matches_executable_p
476 #define aout_64_openr_next_archived_file bfd_generic_openr_next_archived_file
477 #define aout_64_generic_stat_arch_elt bfd_generic_stat_arch_elt
478 #define aout_64_slurp_armap bfd_slurp_bsd_armap
479 #define aout_64_slurp_extended_name_table bfd_true
480 #define aout_64_write_armap bsd_write_armap
481 #define aout_64_truncate_arname bfd_bsd_truncate_arname
482 #define aout_64_machine_type sunos_machine_type
484 #define aout_64_core_file_failing_command sunos4_core_file_failing_command
485 #define aout_64_core_file_failing_signal sunos4_core_file_failing_signal
486 #define aout_64_core_file_matches_executable_p sunos4_core_file_matches_executable_p
488 #define aout_64_bfd_debug_info_start bfd_void
489 #define aout_64_bfd_debug_info_end bfd_void
490 #define aout_64_bfd_debug_info_accumulate bfd_void
492 #define aout_32_bfd_debug_info_start bfd_void
493 #define aout_32_bfd_debug_info_end bfd_void
494 #define aout_32_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
498 /* We implement these routines ourselves, rather than using the generic
500 #define aout_write_object_contents sunos4_write_object_contents
505 bfd_target_aout_flavour_enum,
506 true, /* target byte order */
507 true, /* target headers byte order */
508 (HAS_RELOC | EXEC_P | /* object flags */
509 HAS_LINENO | HAS_DEBUG |
510 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
511 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
512 ' ', /* ar_pad_char */
513 16, /* ar_max_namelen */
514 3, /* minimum alignment power */
515 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
516 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
518 {_bfd_dummy_target, NAME(sunos,object_p),
519 bfd_generic_archive_p, sunos4_core_file_p},
520 {bfd_false, NAME(aout,mkobject),
521 _bfd_generic_mkarchive, bfd_false},
522 {bfd_false, NAME(aout,sunos4_write_object_contents), /* bfd_write_contents */
523 _bfd_write_archive_contents, bfd_false},
525 JUMP_TABLE(JNAME(aout))