1 /* A.out "format 1" file handling code for BFD.
2 Copyright 1990, 1991, 1992, 1993 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. */
25 #include "aout/sun4.h"
26 #include "libaout.h" /* BFD a.out internal data structures */
28 #include "aout/aout64.h"
29 #include "aout/stab_gnu.h"
32 /* This is needed to reject a NewsOS file, e.g. in
33 gdb/testsuite/gdb.t10/crossload.exp. <kingdon@cygnus.com>
34 I needed to add M_UNKNOWN to recognize a 68000 object, so this will
35 probably no longer reject a NewsOS object. <ian@cygnus.com>. */
36 #define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \
37 || (mtype) == M_68010 \
38 || (mtype) == M_68020 \
39 || (mtype) == M_SPARC)
42 The file @code{aoutf1.h} contains the code for BFD's
43 a.out back end. Control over the generated back end is given by these
44 two preprocessor names:
47 This value should be either 32 or 64, depending upon the size of an
48 int in the target format. It changes the sizes of the structs which
49 perform the memory/disk mapping of structures.
51 The 64 bit backend may only be used if the host compiler supports 64
52 ints (eg long long with gcc), by defining the name @code{BFD_HOST_64_BIT} in @code{bfd.h}.
53 With this name defined, @emph{all} bfd operations are performed with 64bit
54 arithmetic, not just those to a 64bit target.
57 The name put into the target vector.
68 sunos_64_set_arch_mach
70 sunos_32_set_arch_mach
76 /* Determine the architecture and machine type of the object file. */
77 enum bfd_architecture arch;
83 /* Some Sun3s make magic numbers without cpu types in them, so
84 we'll default to the 68000. */
102 arch = bfd_arch_sparc;
108 arch = bfd_arch_i386;
113 arch = bfd_arch_a29k;
118 arch = bfd_arch_m68k;
123 arch = bfd_arch_obscure;
127 bfd_set_arch_mach (abfd, arch, machine);
130 #define SET_ARCH_MACH(ABFD, EXEC) \
131 NAME(sunos,set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \
132 choose_reloc_size(ABFD);
134 /* Determine the size of a relocation entry, based on the architecture */
136 choose_reloc_size (abfd)
139 switch (bfd_get_arch (abfd))
143 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
146 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
151 /* Write an object file in SunOS format.
152 Section contents have already been written. We write the
153 file header, symbols, and relocation. */
157 aout_64_sunos4_write_object_contents
159 aout_32_sunos4_write_object_contents
164 struct external_exec exec_bytes;
165 struct internal_exec *execp = exec_hdr (abfd);
167 /* Magic number, maestro, please! */
168 switch (bfd_get_arch (abfd))
171 switch (bfd_get_mach (abfd))
174 N_SET_MACHTYPE (*execp, M_UNKNOWN);
177 N_SET_MACHTYPE (*execp, M_68010);
181 N_SET_MACHTYPE (*execp, M_68020);
186 N_SET_MACHTYPE (*execp, M_SPARC);
189 N_SET_MACHTYPE (*execp, M_386);
192 N_SET_MACHTYPE (*execp, M_29K);
195 N_SET_MACHTYPE (*execp, M_UNKNOWN);
198 choose_reloc_size (abfd);
200 N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
202 N_SET_DYNAMIC (*execp, bfd_get_file_flags (abfd) & DYNAMIC);
204 WRITE_HEADERS (abfd, execp);
211 #define CORE_MAGIC 0x080456
212 #define CORE_NAMELEN 16
214 /* The core structure is taken from the Sun documentation.
215 Unfortunately, they don't document the FPA structure, or at least I
216 can't find it easily. Fortunately the core header contains its own
217 length. So this shouldn't cause problems, except for c_ucode, which
218 so far we don't use but is easy to find with a little arithmetic. */
220 /* But the reg structure can be gotten from the SPARC processor handbook.
221 This really should be in a GNU include file though so that gdb can use
246 /* Taken from Sun documentation: */
248 /* FIXME: It's worse than we expect. This struct contains TWO substructs
249 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
250 even portably access the stuff in between! */
252 struct external_sparc_core
254 int c_magic; /* Corefile magic number */
255 int c_len; /* Sizeof (struct core) */
256 #define SPARC_CORE_LEN 432
257 int c_regs[19]; /* General purpose registers -- MACHDEP SIZE */
258 struct external_exec c_aouthdr; /* A.out header */
259 int c_signo; /* Killing signal, if any */
260 int c_tsize; /* Text size (bytes) */
261 int c_dsize; /* Data size (bytes) */
262 int c_ssize; /* Stack size (bytes) */
263 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
264 double fp_stuff[1]; /* external FPU state (size unknown by us) */
265 /* The type "double" is critical here, for alignment.
266 SunOS declares a struct here, but the struct's alignment
267 is double since it contains doubles. */
268 int c_ucode; /* Exception no. from u_code */
269 /* (this member is not accessible by name since we don't
270 portably know the size of fp_stuff.) */
273 struct external_sun3_core
275 int c_magic; /* Corefile magic number */
276 int c_len; /* Sizeof (struct core) */
277 #define SUN3_CORE_LEN 826 /* As of SunOS 4.1.1 */
278 int c_regs[18]; /* General purpose registers -- MACHDEP SIZE */
279 struct external_exec c_aouthdr; /* A.out header */
280 int c_signo; /* Killing signal, if any */
281 int c_tsize; /* Text size (bytes) */
282 int c_dsize; /* Data size (bytes) */
283 int c_ssize; /* Stack size (bytes) */
284 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
285 double fp_stuff[1]; /* external FPU state (size unknown by us) */
286 /* The type "double" is critical here, for alignment.
287 SunOS declares a struct here, but the struct's alignment
288 is double since it contains doubles. */
289 int c_ucode; /* Exception no. from u_code */
290 /* (this member is not accessible by name since we don't
291 portably know the size of fp_stuff.) */
294 struct internal_sunos_core
296 int c_magic; /* Corefile magic number */
297 int c_len; /* Sizeof (struct core) */
298 long c_regs_pos; /* file offset of General purpose registers */
299 int c_regs_size; /* size of General purpose registers */
300 struct internal_exec c_aouthdr; /* A.out header */
301 int c_signo; /* Killing signal, if any */
302 int c_tsize; /* Text size (bytes) */
303 int c_dsize; /* Data size (bytes) */
304 int c_ssize; /* Stack size (bytes) */
305 bfd_vma c_stacktop; /* Stack top (address) */
306 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
307 long fp_stuff_pos; /* file offset of external FPU state (regs) */
308 int fp_stuff_size; /* Size of it */
309 int c_ucode; /* Exception no. from u_code */
312 /* byte-swap in the Sun-3 core structure */
314 swapcore_sun3 (abfd, ext, intcore)
317 struct internal_sunos_core *intcore;
319 struct external_sun3_core *extcore = (struct external_sun3_core *) ext;
321 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_magic);
322 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_len);
323 intcore->c_regs_pos = (long) (((struct external_sun3_core *) 0)->c_regs);
324 intcore->c_regs_size = sizeof (extcore->c_regs);
326 aout_64_swap_exec_header_in
328 aout_32_swap_exec_header_in
330 (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
331 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_signo);
332 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_tsize);
333 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_dsize);
334 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_ssize);
335 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
336 intcore->fp_stuff_pos = (long) (((struct external_sun3_core *) 0)->fp_stuff);
337 /* FP stuff takes up whole rest of struct, except c_ucode. */
338 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
339 (file_ptr) (((struct external_sun3_core *) 0)->fp_stuff);
340 /* Ucode is the last thing in the struct -- just before the end */
343 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *) extcore);
344 intcore->c_stacktop = 0x0E000000; /* By experimentation */
348 /* byte-swap in the Sparc core structure */
350 swapcore_sparc (abfd, ext, intcore)
353 struct internal_sunos_core *intcore;
355 struct external_sparc_core *extcore = (struct external_sparc_core *) ext;
357 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_magic);
358 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_len);
359 intcore->c_regs_pos = (long) (((struct external_sparc_core *) 0)->c_regs);
360 intcore->c_regs_size = sizeof (extcore->c_regs);
362 aout_64_swap_exec_header_in
364 aout_32_swap_exec_header_in
366 (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
367 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_signo);
368 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_tsize);
369 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_dsize);
370 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *) &extcore->c_ssize);
371 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
372 intcore->fp_stuff_pos = (long) (((struct external_sparc_core *) 0)->fp_stuff);
373 /* FP stuff takes up whole rest of struct, except c_ucode. */
374 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
375 (file_ptr) (((struct external_sparc_core *) 0)->fp_stuff);
376 /* Ucode is the last thing in the struct -- just before the end */
379 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *) extcore);
381 /* Supposedly the user stack grows downward from the bottom of kernel memory.
382 Presuming that this remains true, this definition will work. */
383 /* Now sun has provided us with another challenge. The value is different
384 for sparc2 and sparc10 (both running SunOS 4.1.3). We pick one or
385 the other based on the current value of the stack pointer. This
386 loses (a) if the stack pointer has been clobbered, or (b) if the stack
387 is larger than 128 megabytes.
389 It's times like these you're glad they're switching to ELF.
391 Note that using include files or nlist on /vmunix would be wrong,
392 because we want the value for this core file, no matter what kind of
393 machine we were compiled on or are running on. */
394 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
395 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
397 bfd_vma sp = bfd_h_get_32
398 (abfd, (unsigned char *) &((struct regs *) &extcore->c_regs[0])->r_o6);
399 if (sp < SPARC_USRSTACK_SPARC10)
400 intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
402 intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
406 /* need this cast because ptr is really void * */
407 #define core_hdr(bfd) ((bfd)->tdata.sun_core_data)
408 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
409 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
410 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
411 #define core_reg2sec(bfd) (core_hdr(bfd)->reg2_section)
413 /* These are stored in the bfd's tdata */
414 struct sun_core_struct
416 struct internal_sunos_core *hdr; /* core file header */
417 asection *data_section;
418 asection *stack_section;
419 asection *reg_section;
420 asection *reg2_section;
424 sunos4_core_file_p (abfd)
427 unsigned char longbuf[4]; /* Raw bytes of various header fields */
430 struct internal_sunos_core *core;
434 struct sun_core_struct suncoredata;
435 struct internal_sunos_core internal_sunos_core;
436 char external_core[1];
440 if (bfd_read ((PTR) longbuf, 1, sizeof (longbuf), abfd) !=
443 core_mag = bfd_h_get_32 (abfd, longbuf);
445 if (core_mag != CORE_MAGIC)
448 /* SunOS core headers can vary in length; second word is size; */
449 if (bfd_read ((PTR) longbuf, 1, sizeof (longbuf), abfd) !=
452 core_size = bfd_h_get_32 (abfd, longbuf);
454 if (core_size > 20000)
457 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) < 0)
460 mergem = (struct mergem *) bfd_zalloc (abfd, core_size + sizeof (struct mergem));
463 bfd_set_error (bfd_error_no_memory);
467 extcore = mergem->external_core;
469 if ((bfd_read ((PTR) extcore, 1, core_size, abfd)) != core_size)
471 bfd_release (abfd, (char *) mergem);
475 /* Validate that it's a core file we know how to handle, due to sun
476 botching the positioning of registers and other fields in a machine
478 core = &mergem->internal_sunos_core;
482 swapcore_sparc (abfd, extcore, core);
485 swapcore_sun3 (abfd, extcore, core);
488 bfd_set_error (bfd_error_system_call); /* FIXME */
489 bfd_release (abfd, (char *) mergem);
493 abfd->tdata.sun_core_data = &mergem->suncoredata;
494 abfd->tdata.sun_core_data->hdr = core;
496 /* create the sections. This is raunchy, but bfd_close wants to reclaim
498 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
499 if (core_stacksec (abfd) == NULL)
502 bfd_set_error (bfd_error_no_memory);
503 bfd_release (abfd, (char *) mergem);
506 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
507 if (core_datasec (abfd) == NULL)
510 bfd_release (abfd, core_stacksec (abfd));
513 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
514 if (core_regsec (abfd) == NULL)
517 bfd_release (abfd, core_datasec (abfd));
520 core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
521 if (core_reg2sec (abfd) == NULL)
523 bfd_release (abfd, core_regsec (abfd));
527 core_stacksec (abfd)->name = ".stack";
528 core_datasec (abfd)->name = ".data";
529 core_regsec (abfd)->name = ".reg";
530 core_reg2sec (abfd)->name = ".reg2";
532 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
533 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
534 core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
535 core_reg2sec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
537 core_stacksec (abfd)->_raw_size = core->c_ssize;
538 core_datasec (abfd)->_raw_size = core->c_dsize;
539 core_regsec (abfd)->_raw_size = core->c_regs_size;
540 core_reg2sec (abfd)->_raw_size = core->fp_stuff_size;
542 core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
543 core_datasec (abfd)->vma = N_DATADDR (core->c_aouthdr);
544 core_regsec (abfd)->vma = 0;
545 core_reg2sec (abfd)->vma = 0;
547 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
548 core_datasec (abfd)->filepos = core->c_len;
549 /* We'll access the regs afresh in the core file, like any section: */
550 core_regsec (abfd)->filepos = (file_ptr) core->c_regs_pos;
551 core_reg2sec (abfd)->filepos = (file_ptr) core->fp_stuff_pos;
553 /* Align to word at least */
554 core_stacksec (abfd)->alignment_power = 2;
555 core_datasec (abfd)->alignment_power = 2;
556 core_regsec (abfd)->alignment_power = 2;
557 core_reg2sec (abfd)->alignment_power = 2;
559 abfd->sections = core_stacksec (abfd);
560 core_stacksec (abfd)->next = core_datasec (abfd);
561 core_datasec (abfd)->next = core_regsec (abfd);
562 core_regsec (abfd)->next = core_reg2sec (abfd);
564 abfd->section_count = 4;
570 sunos4_core_file_failing_command (abfd)
573 return core_hdr (abfd)->hdr->c_cmdname;
577 sunos4_core_file_failing_signal (abfd)
580 return core_hdr (abfd)->hdr->c_signo;
584 sunos4_core_file_matches_executable_p (core_bfd, exec_bfd)
588 if (core_bfd->xvec != exec_bfd->xvec)
590 bfd_set_error (bfd_error_system_call);
594 return (memcmp ((char *) &((core_hdr (core_bfd)->hdr)->c_aouthdr),
595 (char *) exec_hdr (exec_bfd),
596 sizeof (struct internal_exec)) == 0) ? true : false;
599 #define MY_set_sizes sunos4_set_sizes
601 sunos4_set_sizes (abfd)
604 switch (bfd_get_arch (abfd))
609 adata (abfd).page_size = 0x2000;
610 adata (abfd).segment_size = 0x2000;
611 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
614 adata (abfd).page_size = 0x2000;
615 adata (abfd).segment_size = 0x20000;
616 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
621 /* We default to setting the toolversion field to 1, as is required by
623 #ifndef MY_exec_hdr_flags
624 #define MY_exec_hdr_flags 1
627 #ifndef MY_add_dynamic_symbols
628 #define MY_add_dynamic_symbols 0
630 #ifndef MY_add_one_symbol
631 #define MY_add_one_symbol 0
633 #ifndef MY_link_dynamic_object
634 #define MY_link_dynamic_object 0
636 #ifndef MY_write_dynamic_symbol
637 #define MY_write_dynamic_symbol 0
639 #ifndef MY_check_dynamic_reloc
640 #define MY_check_dynamic_reloc 0
642 #ifndef MY_finish_dynamic_link
643 #define MY_finish_dynamic_link 0
646 static CONST struct aout_backend_data sunos4_aout_backend =
648 0, /* zmagic files are not contiguous */
649 1, /* text includes header */
651 0, /* default text vma */
653 0, /* header is counted in zmagic text */
654 MY_add_dynamic_symbols,
656 MY_link_dynamic_object,
657 MY_write_dynamic_symbol,
658 MY_check_dynamic_reloc,
659 MY_finish_dynamic_link
662 #define MY_core_file_failing_command sunos4_core_file_failing_command
663 #define MY_core_file_failing_signal sunos4_core_file_failing_signal
664 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
666 #define MY_bfd_debug_info_start bfd_void
667 #define MY_bfd_debug_info_end bfd_void
668 #define MY_bfd_debug_info_accumulate \
669 (void (*) PARAMS ((bfd *, struct sec *))) bfd_void
670 #define MY_core_file_p sunos4_core_file_p
671 #define MY_write_object_contents NAME(aout,sunos4_write_object_contents)
672 #define MY_backend_data &sunos4_aout_backend
674 #define TARGET_IS_BIG_ENDIAN_P
676 #include "aout-target.h"