1 /* BFD backend for generic a.out flavour 1 */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD 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 1, or (at your option)
12 BFD 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 BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include <a.out.sun4.h>
37 void (*bfd_error_trap)();
39 static bfd_target *sunos4_callback ();
45 DEFUN(NAME(sunos,object_p), (abfd),
48 unsigned char magicbuf[4]; /* Raw bytes of magic number from file */
49 unsigned long magic; /* Swapped magic number */
51 bfd_error = system_call_error;
53 if (bfd_read ((PTR)magicbuf, 1 , 4, abfd) !=
56 magic = bfd_h_get_32 (abfd, magicbuf);
58 if (N_BADMAG (*((struct internal_exec *) &magic))) return 0;
60 return NAME(aout,some_aout_object_p) (abfd, sunos4_callback);
63 /* Determine the size of a relocation entry, based on the architecture */
65 DEFUN(choose_reloc_size,(abfd),
68 switch (abfd->obj_arch) {
71 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
74 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
79 /* Set parameters about this a.out file that are machine-dependent.
80 This routine is called from some_aout_object_p just before it returns. */
83 sunos4_callback (abfd)
86 struct internal_exec *execp = exec_hdr (abfd);
88 WORK_OUT_FILE_POSITIONS(abfd, execp);
90 /* Determine the architecture and machine type of the object file. */
91 switch (N_MACHTYPE (*exec_hdr (abfd))) {
94 abfd->obj_arch = bfd_arch_unknown;
95 abfd->obj_machine = 0;
99 abfd->obj_arch = bfd_arch_m68k;
100 abfd->obj_machine = 68010;
104 abfd->obj_arch = bfd_arch_m68k;
105 abfd->obj_machine = 68020;
109 abfd->obj_arch = bfd_arch_sparc;
110 abfd->obj_machine = 0;
114 abfd->obj_arch = bfd_arch_i386;
115 abfd->obj_machine = 0;
119 abfd->obj_arch = bfd_arch_a29k;
120 abfd->obj_machine = 0;
124 abfd->obj_arch = bfd_arch_obscure;
125 abfd->obj_machine = 0;
129 choose_reloc_size(abfd);
134 /* Write an object file in SunOS format.
135 Section contents have already been written. We write the
136 file header, symbols, and relocation. */
139 DEFUN(NAME(aout,sunos4_write_object_contents),(abfd),
143 bfd_size_type data_pad = 0;
144 struct external_exec exec_bytes;
145 struct internal_exec *execp = exec_hdr (abfd);
149 execp->a_text = obj_textsec (abfd)->size;
151 /* Magic number, maestro, please! */
152 switch (bfd_get_architecture(abfd)) {
154 switch (bfd_get_machine(abfd)) {
156 N_SET_MACHTYPE(*execp, M_68010);
160 N_SET_MACHTYPE(*execp, M_68020);
165 N_SET_MACHTYPE(*execp, M_SPARC);
168 N_SET_MACHTYPE(*execp, M_386);
171 N_SET_MACHTYPE(*execp, M_29K);
174 N_SET_MACHTYPE(*execp, M_UNKNOWN);
177 choose_reloc_size(abfd);
180 N_SET_FLAGS (*execp, 0x81);
182 WRITE_HEADERS(abfd, execp);
189 #define CORE_MAGIC 0x080456
190 #define CORE_NAMELEN 16
192 /* The core structure is taken from the Sun documentation.
193 Unfortunately, they don't document the FPA structure, or at least I
194 can't find it easily. Fortunately the core header contains its own
195 length. So this shouldn't cause problems, except for c_ucode, which
196 so far we don't use but is easy to find with a little arithmetic. */
198 /* But the reg structure can be gotten from the SPARC processor handbook.
199 This really should be in a GNU include file though so that gdb can use
223 /* Taken from Sun documentation: */
225 /* FIXME: It's worse than we expect. This struct contains TWO substructs
226 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
227 even portably access the stuff in between! */
230 int c_magic; /* Corefile magic number */
231 int c_len; /* Sizeof (struct core) */
232 struct regs c_regs; /* General purpose registers -- MACHDEP SIZE */
233 struct internal_exec c_aouthdr; /* A.out header */
234 int c_signo; /* Killing signal, if any */
235 int c_tsize; /* Text size (bytes) */
236 int c_dsize; /* Data size (bytes) */
237 int c_ssize; /* Stack size (bytes) */
238 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
239 double fp_stuff[1]; /* external FPU state (size unknown by us) */
240 /* The type "double" is critical here, for alignment.
241 SunOS declares a struct here, but the struct's alignment
242 is double since it contains doubles. */
243 int c_ucode; /* Exception no. from u_code */
244 /* (this member is not accessible by name since we don't
245 portably know the size of fp_stuff.) */
248 /* Supposedly the user stack grows downward from the bottom of kernel memory.
249 Presuming that this remains true, this definition will work. */
250 #define USRSTACK (-(128*1024*1024))
252 PROTO (static void, swapcore, (bfd *abfd, struct core *core));
254 /* need this cast b/c ptr is really void * */
255 #define core_hdr(bfd) (((struct suncordata *) (bfd->tdata))->hdr)
256 #define core_datasec(bfd) (((struct suncordata *) ((bfd)->tdata))->data_section)
257 #define core_stacksec(bfd) (((struct suncordata*)((bfd)->tdata))->stack_section)
258 #define core_regsec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg_section)
259 #define core_reg2sec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg2_section)
261 /* These are stored in the bfd's tdata */
263 struct core *hdr; /* core file header */
264 asection *data_section;
265 asection *stack_section;
266 asection *reg_section;
267 asection *reg2_section;
271 DEFUN(sunos4_core_file_p,(abfd),
274 unsigned char longbuf[4]; /* Raw bytes of various header fields */
280 bfd_error = system_call_error;
282 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
285 core_mag = bfd_h_get_32 (abfd, longbuf);
287 if (core_mag != CORE_MAGIC) return 0;
289 /* SunOS core headers can vary in length; second word is size; */
290 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
293 core_size = bfd_h_get_32 (abfd, longbuf);
295 if (core_size > 20000)
298 if (bfd_seek (abfd, 0L, false) < 0) return 0;
300 rawptr = bfd_zalloc (abfd, core_size + sizeof (struct suncordata));
301 if (rawptr == NULL) {
302 bfd_error = no_memory;
306 core = (struct core *) (rawptr + sizeof (struct suncordata));
308 if ((bfd_read ((PTR) core, 1, core_size, abfd)) != core_size) {
309 bfd_error = system_call_error;
310 bfd_release (abfd, rawptr);
314 swapcore (abfd, core);
315 set_tdata (abfd, ((struct suncordata *) rawptr));
316 core_hdr (abfd) = core;
318 /* create the sections. This is raunchy, but bfd_close wants to reclaim
320 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
321 if (core_stacksec (abfd) == NULL) {
323 bfd_error = no_memory;
324 bfd_release (abfd, rawptr);
327 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
328 if (core_datasec (abfd) == NULL) {
330 bfd_release (abfd, core_stacksec (abfd));
333 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
334 if (core_regsec (abfd) == NULL) {
336 bfd_release (abfd, core_datasec (abfd));
339 core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
340 if (core_reg2sec (abfd) == NULL) {
341 bfd_release (abfd, core_regsec (abfd));
345 core_stacksec (abfd)->name = ".stack";
346 core_datasec (abfd)->name = ".data";
347 core_regsec (abfd)->name = ".reg";
348 core_reg2sec (abfd)->name = ".reg2";
350 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
351 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
352 core_regsec (abfd)->flags = SEC_ALLOC;
353 core_reg2sec (abfd)->flags = SEC_ALLOC;
355 core_stacksec (abfd)->size = core->c_ssize;
356 core_datasec (abfd)->size = core->c_dsize;
357 core_regsec (abfd)->size = (sizeof core->c_regs);
358 /* Float regs take up end of struct, except c_ucode. */
359 core_reg2sec (abfd)->size = core_size - (sizeof core->c_ucode) -
360 (file_ptr)(((struct core *)0)->fp_stuff);
362 core_stacksec (abfd)->vma = (USRSTACK - core->c_ssize);
363 core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
364 core_regsec (abfd)->vma = -1;
365 core_reg2sec (abfd)->vma = -1;
367 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
368 core_datasec (abfd)->filepos = core->c_len;
369 /* In file header: */
370 core_regsec (abfd)->filepos = (file_ptr)(&((struct core *)0)->c_regs);
371 core_reg2sec (abfd)->filepos = (file_ptr)(((struct core *)0)->fp_stuff);
373 /* Align to word at least */
374 core_stacksec (abfd)->alignment_power = 2;
375 core_datasec (abfd)->alignment_power = 2;
376 core_regsec (abfd)->alignment_power = 2;
377 core_reg2sec (abfd)->alignment_power = 2;
379 abfd->sections = core_stacksec (abfd);
380 core_stacksec (abfd)->next = core_datasec (abfd);
381 core_datasec (abfd)->next = core_regsec (abfd);
382 core_regsec (abfd)->next = core_reg2sec (abfd);
384 abfd->section_count = 4;
389 static char *sunos4_core_file_failing_command (abfd)
392 return core_hdr (abfd)->c_cmdname;
396 DEFUN(sunos4_core_file_failing_signal,(abfd),
399 return core_hdr (abfd)->c_signo;
403 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
407 if (core_bfd->xvec != exec_bfd->xvec) {
408 bfd_error = system_call_error;
412 return (bcmp ((char *)&core_hdr (core_bfd), (char*) &exec_hdr (exec_bfd),
413 sizeof (struct internal_exec)) == 0) ? true : false;
416 /* byte-swap core structure */
417 /* FIXME, this needs more work to swap IN a core struct from raw bytes */
419 DEFUN(swapcore,(abfd, core),
423 struct external_exec exec_bytes;
425 core->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&core->c_magic);
426 core->c_len = bfd_h_get_32 (abfd, (unsigned char *)&core->c_len );
427 /* Leave integer registers in target byte order. */
428 bcopy ((char *)&(core->c_aouthdr), (char *)&exec_bytes, EXEC_BYTES_SIZE);
429 NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &core->c_aouthdr);
430 core->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&core->c_signo);
431 core->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_tsize);
432 core->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_dsize);
433 core->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_ssize);
434 /* Leave FP registers in target byte order. */
435 /* Leave "c_ucode" unswapped for now, since we can't find it easily. */
438 /* We use BFD generic archive files. */
439 #define aout_32_openr_next_archived_file bfd_generic_openr_next_archived_file
440 #define aout_32_generic_stat_arch_elt bfd_generic_stat_arch_elt
441 #define aout_32_slurp_armap bfd_slurp_bsd_armap
442 #define aout_32_slurp_extended_name_table bfd_true
443 #define aout_32_write_armap bsd_write_armap
444 #define aout_32_truncate_arname bfd_bsd_truncate_arname
445 #define aout_32_machine_type sunos_machine_type
447 #define aout_32_core_file_failing_command sunos4_core_file_failing_command
448 #define aout_32_core_file_failing_signal sunos4_core_file_failing_signal
449 #define aout_32_core_file_matches_executable_p sunos4_core_file_matches_executable_p
452 #define aout_64_openr_next_archived_file bfd_generic_openr_next_archived_file
453 #define aout_64_generic_stat_arch_elt bfd_generic_stat_arch_elt
454 #define aout_64_slurp_armap bfd_slurp_bsd_armap
455 #define aout_64_slurp_extended_name_table bfd_true
456 #define aout_64_write_armap bsd_write_armap
457 #define aout_64_truncate_arname bfd_bsd_truncate_arname
458 #define aout_64_machine_type sunos_machine_type
460 #define aout_64_core_file_failing_command sunos4_core_file_failing_command
461 #define aout_64_core_file_failing_signal sunos4_core_file_failing_signal
462 #define aout_64_core_file_matches_executable_p sunos4_core_file_matches_executable_p
464 /* We implement these routines ourselves, rather than using the generic
466 #define aout_write_object_contents sunos4_write_object_contents
471 bfd_target_aout_flavour_enum,
472 true, /* target byte order */
473 true, /* target headers byte order */
474 (HAS_RELOC | EXEC_P | /* object flags */
475 HAS_LINENO | HAS_DEBUG |
476 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
477 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
478 ' ', /* ar_pad_char */
479 16, /* ar_max_namelen */
480 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
481 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
483 {_bfd_dummy_target, NAME(sunos,object_p),
484 bfd_generic_archive_p, sunos4_core_file_p},
485 {bfd_false, NAME(aout,mkobject),
486 _bfd_generic_mkarchive, bfd_false},
487 {bfd_false, NAME(aout,sunos4_write_object_contents), /* bfd_write_contents */
488 _bfd_write_archive_contents, bfd_false},
490 JUMP_TABLE(JNAME(aout))