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. */
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"
33 The file @code{aoutf1.h} contains the code for BFD's
34 a.out back end. Control over the generated back end is given by these
35 two preprocessor names:
38 This value should be either 32 or 64, depending upon the size of an
39 int in the target format. It changes the sizes of the structs which
40 perform the memory/disk mapping of structures.
42 The 64 bit backend may only be used if the host compiler supports 64
43 ints (eg long long with gcc), by defining the name @code{HOST_64_BIT} in @code{bfd.h}.
44 With this name defined, @emph{all} bfd operations are performed with 64bit
45 arithmetic, not just those to a 64bit target.
48 The name put into the target vector.
54 void (*bfd_error_trap)();
60 DEFUN(NAME(sunos,set_arch_mach), (abfd, machtype),
61 bfd *abfd AND int machtype)
63 /* Determine the architecture and machine type of the object file. */
64 enum bfd_architecture arch;
69 /* Some Sun3s make magic numbers without cpu types in them, so
70 we'll default to the 68020. */
88 arch = bfd_arch_sparc;
103 arch = bfd_arch_m68k;
108 arch = bfd_arch_obscure;
112 bfd_set_arch_mach(abfd, arch, machine);
115 #define SET_ARCH_MACH(ABFD, EXEC) \
116 NAME(sunos,set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \
117 choose_reloc_size(ABFD);
119 /* Determine the size of a relocation entry, based on the architecture */
121 DEFUN(choose_reloc_size,(abfd),
124 switch (bfd_get_arch(abfd)) {
127 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
130 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
135 /* Write an object file in SunOS format.
136 Section contents have already been written. We write the
137 file header, symbols, and relocation. */
140 DEFUN(NAME(aout,sunos4_write_object_contents),
144 bfd_size_type data_pad = 0;
145 struct external_exec exec_bytes;
146 struct internal_exec *execp = exec_hdr (abfd);
148 /* Magic number, maestro, please! */
149 switch (bfd_get_arch(abfd)) {
151 switch (bfd_get_mach(abfd)) {
153 N_SET_MACHTYPE(*execp, M_68010);
157 N_SET_MACHTYPE(*execp, M_68020);
162 N_SET_MACHTYPE(*execp, M_SPARC);
165 N_SET_MACHTYPE(*execp, M_386);
168 N_SET_MACHTYPE(*execp, M_29K);
171 N_SET_MACHTYPE(*execp, M_UNKNOWN);
174 choose_reloc_size(abfd);
177 N_SET_FLAGS (*execp, 0x1);
179 WRITE_HEADERS(abfd, execp);
186 #define CORE_MAGIC 0x080456
187 #define CORE_NAMELEN 16
189 /* The core structure is taken from the Sun documentation.
190 Unfortunately, they don't document the FPA structure, or at least I
191 can't find it easily. Fortunately the core header contains its own
192 length. So this shouldn't cause problems, except for c_ucode, which
193 so far we don't use but is easy to find with a little arithmetic. */
195 /* But the reg structure can be gotten from the SPARC processor handbook.
196 This really should be in a GNU include file though so that gdb can use
220 /* Taken from Sun documentation: */
222 /* FIXME: It's worse than we expect. This struct contains TWO substructs
223 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
224 even portably access the stuff in between! */
226 struct external_sparc_core {
227 int c_magic; /* Corefile magic number */
228 int c_len; /* Sizeof (struct core) */
229 #define SPARC_CORE_LEN 432
230 int c_regs[19]; /* General purpose registers -- MACHDEP SIZE */
231 struct external_exec c_aouthdr; /* A.out header */
232 int c_signo; /* Killing signal, if any */
233 int c_tsize; /* Text size (bytes) */
234 int c_dsize; /* Data size (bytes) */
235 int c_ssize; /* Stack size (bytes) */
236 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
237 double fp_stuff[1]; /* external FPU state (size unknown by us) */
238 /* The type "double" is critical here, for alignment.
239 SunOS declares a struct here, but the struct's alignment
240 is double since it contains doubles. */
241 int c_ucode; /* Exception no. from u_code */
242 /* (this member is not accessible by name since we don't
243 portably know the size of fp_stuff.) */
246 struct external_sun3_core {
247 int c_magic; /* Corefile magic number */
248 int c_len; /* Sizeof (struct core) */
249 #define SUN3_CORE_LEN 826 /* As of SunOS 4.1.1 */
250 int c_regs[18]; /* General purpose registers -- MACHDEP SIZE */
251 struct external_exec c_aouthdr; /* A.out header */
252 int c_signo; /* Killing signal, if any */
253 int c_tsize; /* Text size (bytes) */
254 int c_dsize; /* Data size (bytes) */
255 int c_ssize; /* Stack size (bytes) */
256 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
257 double fp_stuff[1]; /* external FPU state (size unknown by us) */
258 /* The type "double" is critical here, for alignment.
259 SunOS declares a struct here, but the struct's alignment
260 is double since it contains doubles. */
261 int c_ucode; /* Exception no. from u_code */
262 /* (this member is not accessible by name since we don't
263 portably know the size of fp_stuff.) */
266 struct internal_sunos_core {
267 int c_magic; /* Corefile magic number */
268 int c_len; /* Sizeof (struct core) */
269 long c_regs_pos; /* file offset of General purpose registers */
270 int c_regs_size; /* size of General purpose registers */
271 struct internal_exec c_aouthdr; /* A.out header */
272 int c_signo; /* Killing signal, if any */
273 int c_tsize; /* Text size (bytes) */
274 int c_dsize; /* Data size (bytes) */
275 int c_ssize; /* Stack size (bytes) */
276 long c_stacktop; /* Stack top (address) */
277 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
278 long fp_stuff_pos; /* file offset of external FPU state (regs) */
279 int fp_stuff_size; /* Size of it */
280 int c_ucode; /* Exception no. from u_code */
283 /* byte-swap in the Sun-3 core structure */
285 DEFUN(swapcore_sun3,(abfd, ext, intcore),
288 struct internal_sunos_core *intcore)
290 struct external_sun3_core *extcore = (struct external_sun3_core *)ext;
292 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
293 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len );
294 intcore->c_regs_pos = (long) (((struct external_sun3_core *)0)->c_regs);
295 intcore->c_regs_size = sizeof (extcore->c_regs);
296 NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
297 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
298 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
299 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
300 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
301 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
302 intcore->fp_stuff_pos = (long) (((struct external_sun3_core *)0)->fp_stuff);
303 /* FP stuff takes up whole rest of struct, except c_ucode. */
304 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
305 (file_ptr)(((struct external_sun3_core *)0)->fp_stuff);
306 /* Ucode is the last thing in the struct -- just before the end */
309 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
310 intcore->c_stacktop = 0x0E000000; /* By experimentation */
314 /* byte-swap in the Sparc core structure */
316 DEFUN(swapcore_sparc,(abfd, ext, intcore),
319 struct internal_sunos_core *intcore)
321 struct external_sparc_core *extcore = (struct external_sparc_core *)ext;
323 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
324 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len );
325 intcore->c_regs_pos = (long) (((struct external_sparc_core *)0)->c_regs);
326 intcore->c_regs_size = sizeof (extcore->c_regs);
327 NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
328 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
329 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
330 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
331 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
332 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
333 intcore->fp_stuff_pos = (long) (((struct external_sparc_core *)0)->fp_stuff);
334 /* FP stuff takes up whole rest of struct, except c_ucode. */
335 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
336 (file_ptr)(((struct external_sparc_core *)0)->fp_stuff);
337 /* Ucode is the last thing in the struct -- just before the end */
340 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
341 /* Supposedly the user stack grows downward from the bottom of kernel memory.
342 Presuming that this remains true, this definition will work. */
343 #define SPARC_USRSTACK (-(128*1024*1024))
344 intcore->c_stacktop = SPARC_USRSTACK; /* By experimentation */
347 /* need this cast because ptr is really void * */
348 #define core_hdr(bfd) ((bfd)->tdata.sun_core_data)
349 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
350 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
351 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
352 #define core_reg2sec(bfd) (core_hdr(bfd)->reg2_section)
354 /* These are stored in the bfd's tdata */
355 struct sun_core_struct {
356 struct internal_sunos_core *hdr; /* core file header */
357 asection *data_section;
358 asection *stack_section;
359 asection *reg_section;
360 asection *reg2_section;
364 DEFUN(sunos4_core_file_p,(abfd),
367 unsigned char longbuf[4]; /* Raw bytes of various header fields */
370 struct internal_sunos_core *core;
373 struct sun_core_struct suncoredata;
374 struct internal_sunos_core internal_sunos_core;
375 char external_core[1];
378 bfd_error = system_call_error;
380 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
383 core_mag = bfd_h_get_32 (abfd, longbuf);
385 if (core_mag != CORE_MAGIC) return 0;
387 /* SunOS core headers can vary in length; second word is size; */
388 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
391 core_size = bfd_h_get_32 (abfd, longbuf);
393 if (core_size > 20000)
396 if (bfd_seek (abfd, 0L, false) < 0) return 0;
398 mergem = (struct mergem *)bfd_zalloc (abfd, core_size + sizeof (struct mergem));
399 if (mergem == NULL) {
400 bfd_error = no_memory;
404 extcore = mergem->external_core;
406 if ((bfd_read ((PTR) extcore, 1, core_size, abfd)) != core_size) {
407 bfd_error = system_call_error;
408 bfd_release (abfd, (char *)mergem);
412 /* Validate that it's a core file we know how to handle, due to sun
413 botching the positioning of registers and other fields in a machine
415 core = &mergem->internal_sunos_core;
418 swapcore_sparc (abfd, extcore, core);
421 swapcore_sun3 (abfd, extcore, core);
424 bfd_error = system_call_error; /* FIXME */
425 bfd_release (abfd, (char *)mergem);
429 abfd->tdata.sun_core_data = &mergem->suncoredata;
430 abfd->tdata.sun_core_data->hdr = core;
432 /* create the sections. This is raunchy, but bfd_close wants to reclaim
434 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
435 if (core_stacksec (abfd) == NULL) {
437 bfd_error = no_memory;
438 bfd_release (abfd, (char *)mergem);
441 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
442 if (core_datasec (abfd) == NULL) {
444 bfd_release (abfd, core_stacksec (abfd));
447 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
448 if (core_regsec (abfd) == NULL) {
450 bfd_release (abfd, core_datasec (abfd));
453 core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
454 if (core_reg2sec (abfd) == NULL) {
455 bfd_release (abfd, core_regsec (abfd));
459 core_stacksec (abfd)->name = ".stack";
460 core_datasec (abfd)->name = ".data";
461 core_regsec (abfd)->name = ".reg";
462 core_reg2sec (abfd)->name = ".reg2";
464 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
465 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
466 core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
467 core_reg2sec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
469 core_stacksec (abfd)->_raw_size = core->c_ssize;
470 core_datasec (abfd)->_raw_size = core->c_dsize;
471 core_regsec (abfd)->_raw_size = core->c_regs_size;
472 core_reg2sec (abfd)->_raw_size = core->fp_stuff_size;
474 core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
475 core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
476 core_regsec (abfd)->vma = 0;
477 core_reg2sec (abfd)->vma = 0;
479 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
480 core_datasec (abfd)->filepos = core->c_len;
481 /* We'll access the regs afresh in the core file, like any section: */
482 core_regsec (abfd)->filepos = (file_ptr)core->c_regs_pos;
483 core_reg2sec (abfd)->filepos = (file_ptr)core->fp_stuff_pos;
485 /* Align to word at least */
486 core_stacksec (abfd)->alignment_power = 2;
487 core_datasec (abfd)->alignment_power = 2;
488 core_regsec (abfd)->alignment_power = 2;
489 core_reg2sec (abfd)->alignment_power = 2;
491 abfd->sections = core_stacksec (abfd);
492 core_stacksec (abfd)->next = core_datasec (abfd);
493 core_datasec (abfd)->next = core_regsec (abfd);
494 core_regsec (abfd)->next = core_reg2sec (abfd);
496 abfd->section_count = 4;
501 static char *sunos4_core_file_failing_command (abfd)
504 return core_hdr (abfd)->hdr->c_cmdname;
508 DEFUN(sunos4_core_file_failing_signal,(abfd),
511 return core_hdr (abfd)->hdr->c_signo;
515 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
519 if (core_bfd->xvec != exec_bfd->xvec) {
520 bfd_error = system_call_error;
524 return (memcmp ((char *)&((core_hdr (core_bfd)->hdr)->c_aouthdr),
525 (char *) exec_hdr (exec_bfd),
526 sizeof (struct internal_exec)) == 0) ? true : false;
529 extern reloc_howto_type aout_32_ext_howto_table[];
531 static reloc_howto_type *
532 DEFUN (sunos4_reloc_type_lookup, (abfd, code),
534 bfd_reloc_code_real_type code)
536 switch (bfd_get_arch (abfd))
545 #define IDX(i,j) case i: return &aout_32_ext_howto_table[j]
546 IDX (BFD_RELOC_CTOR, 2);
547 IDX (BFD_RELOC_32, 2);
548 IDX (BFD_RELOC_HI22, 8);
549 IDX (BFD_RELOC_LO10, 11);
550 IDX (BFD_RELOC_32_PCREL_S2, 6);
556 DEFUN (sunos4_set_sizes, (abfd),
559 switch (bfd_get_arch (abfd))
564 adata(abfd).page_size = 0x2000;
565 adata(abfd).segment_size = 0x2000;
566 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
569 adata(abfd).page_size = 0x2000;
570 adata(abfd).segment_size = 0x20000;
571 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
576 static CONST struct aout_backend_data sunos4_aout_backend = {
577 0, 1, 0, sunos4_set_sizes,
580 #define MY_core_file_failing_command sunos4_core_file_failing_command
581 #define MY_core_file_failing_signal sunos4_core_file_failing_signal
582 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
584 #define MY_bfd_debug_info_start bfd_void
585 #define MY_bfd_debug_info_end bfd_void
586 #define MY_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
587 #define MY_core_file_p sunos4_core_file_p
588 #define MY_write_object_contents NAME(aout,sunos4_write_object_contents)
589 #define MY_backend_data &sunos4_aout_backend
591 #define TARGET_IS_BIG_ENDIAN_P
593 #include "aout-target.h"