]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/aoutf1.h
bfd/
[thirdparty/binutils-gdb.git] / bfd / aoutf1.h
1 /* A.out "format 1" file handling code for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26
27 #include "aout/sun4.h"
28 #include "libaout.h" /* BFD a.out internal data structures */
29
30 #include "aout/aout64.h"
31 #include "aout/stab_gnu.h"
32 #include "aout/ar.h"
33
34 /* This is needed to reject a NewsOS file, e.g. in
35 gdb/testsuite/gdb.t10/crossload.exp. <kingdon@cygnus.com>
36 I needed to add M_UNKNOWN to recognize a 68000 object, so this will
37 probably no longer reject a NewsOS object. <ian@cygnus.com>. */
38 #ifndef MACHTYPE_OK
39 #define MACHTYPE_OK(mtype) \
40 (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
41 || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
42 && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
43 #endif
44
45 /*
46 The file @code{aoutf1.h} contains the code for BFD's
47 a.out back end. Control over the generated back end is given by these
48 two preprocessor names:
49 @table @code
50 @item ARCH_SIZE
51 This value should be either 32 or 64, depending upon the size of an
52 int in the target format. It changes the sizes of the structs which
53 perform the memory/disk mapping of structures.
54
55 The 64 bit backend may only be used if the host compiler supports 64
56 ints (eg long long with gcc), by defining the name @code{BFD_HOST_64_BIT} in @code{bfd.h}.
57 With this name defined, @emph{all} bfd operations are performed with 64bit
58 arithmetic, not just those to a 64bit target.
59
60 @item TARGETNAME
61 The name put into the target vector.
62 @item
63 @end table
64
65 */
66
67 /*SUPPRESS558*/
68 /*SUPPRESS529*/
69
70 #if ARCH_SIZE == 64
71 #define sunos_set_arch_mach sunos_64_set_arch_mach
72 #define sunos_write_object_contents aout_64_sunos4_write_object_contents
73 #else
74 #define sunos_set_arch_mach sunos_32_set_arch_mach
75 #define sunos_write_object_contents aout_32_sunos4_write_object_contents
76 #endif
77
78 static bfd_boolean sunos_merge_private_bfd_data
79 PARAMS ((bfd *, bfd *));
80 static void sunos_set_arch_mach
81 PARAMS ((bfd *, enum machine_type));
82 static void choose_reloc_size
83 PARAMS ((bfd *));
84 static bfd_boolean sunos_write_object_contents
85 PARAMS ((bfd *));
86 static const bfd_target *sunos4_core_file_p
87 PARAMS ((bfd *));
88 static char *sunos4_core_file_failing_command
89 PARAMS ((bfd *));
90 static int sunos4_core_file_failing_signal
91 PARAMS ((bfd *));
92 static bfd_boolean sunos4_core_file_matches_executable_p
93 PARAMS ((bfd *, bfd *));
94 static bfd_boolean sunos4_set_sizes
95 PARAMS ((bfd *));
96
97 /* Merge backend data into the output file.
98 This is necessary on sparclet-aout where we want the resultant machine
99 number to be M_SPARCLET if any input file is M_SPARCLET. */
100
101 #define MY_bfd_merge_private_bfd_data sunos_merge_private_bfd_data
102
103 static bfd_boolean
104 sunos_merge_private_bfd_data (ibfd, obfd)
105 bfd *ibfd, *obfd;
106 {
107 if (bfd_get_flavour (ibfd) != bfd_target_aout_flavour
108 || bfd_get_flavour (obfd) != bfd_target_aout_flavour)
109 return TRUE;
110
111 if (bfd_get_arch (obfd) == bfd_arch_sparc)
112 {
113 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
114 bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
115 }
116
117 return TRUE;
118 }
119
120 /* This is either sunos_32_set_arch_mach or sunos_64_set_arch_mach,
121 depending upon ARCH_SIZE. */
122
123 static void
124 sunos_set_arch_mach (abfd, machtype)
125 bfd *abfd;
126 enum machine_type machtype;
127 {
128 /* Determine the architecture and machine type of the object file. */
129 enum bfd_architecture arch;
130 unsigned long machine;
131 switch (machtype)
132 {
133
134 case M_UNKNOWN:
135 /* Some Sun3s make magic numbers without cpu types in them, so
136 we'll default to the 68000. */
137 arch = bfd_arch_m68k;
138 machine = bfd_mach_m68000;
139 break;
140
141 case M_68010:
142 case M_HP200:
143 arch = bfd_arch_m68k;
144 machine = bfd_mach_m68010;
145 break;
146
147 case M_68020:
148 case M_HP300:
149 arch = bfd_arch_m68k;
150 machine = bfd_mach_m68020;
151 break;
152
153 case M_SPARC:
154 arch = bfd_arch_sparc;
155 machine = 0;
156 break;
157
158 case M_SPARCLET:
159 arch = bfd_arch_sparc;
160 machine = bfd_mach_sparc_sparclet;
161 break;
162
163 case M_SPARCLITE_LE:
164 arch = bfd_arch_sparc;
165 machine = bfd_mach_sparc_sparclite_le;
166 break;
167
168 case M_386:
169 case M_386_DYNIX:
170 arch = bfd_arch_i386;
171 machine = 0;
172 break;
173
174 case M_29K:
175 arch = bfd_arch_a29k;
176 machine = 0;
177 break;
178
179 case M_HPUX:
180 arch = bfd_arch_m68k;
181 machine = 0;
182 break;
183
184 default:
185 arch = bfd_arch_obscure;
186 machine = 0;
187 break;
188 }
189 bfd_set_arch_mach (abfd, arch, machine);
190 }
191
192 #define SET_ARCH_MACH(ABFD, EXEC) \
193 NAME(sunos,set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
194 choose_reloc_size(ABFD);
195
196 /* Determine the size of a relocation entry, based on the architecture */
197 static void
198 choose_reloc_size (abfd)
199 bfd *abfd;
200 {
201 switch (bfd_get_arch (abfd))
202 {
203 case bfd_arch_sparc:
204 case bfd_arch_a29k:
205 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
206 break;
207 default:
208 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
209 break;
210 }
211 }
212
213 /* Write an object file in SunOS format. Section contents have
214 already been written. We write the file header, symbols, and
215 relocation. The real name of this function is either
216 aout_64_sunos4_write_object_contents or
217 aout_32_sunos4_write_object_contents, depending upon ARCH_SIZE. */
218
219 static bfd_boolean
220 sunos_write_object_contents (abfd)
221 bfd *abfd;
222 {
223 struct external_exec exec_bytes;
224 struct internal_exec *execp = exec_hdr (abfd);
225
226 /* Magic number, maestro, please! */
227 switch (bfd_get_arch (abfd))
228 {
229 case bfd_arch_m68k:
230 switch (bfd_get_mach (abfd))
231 {
232 case bfd_mach_m68000:
233 N_SET_MACHTYPE (*execp, M_UNKNOWN);
234 break;
235 case bfd_mach_m68010:
236 N_SET_MACHTYPE (*execp, M_68010);
237 break;
238 default:
239 case bfd_mach_m68020:
240 N_SET_MACHTYPE (*execp, M_68020);
241 break;
242 }
243 break;
244 case bfd_arch_sparc:
245 switch (bfd_get_mach (abfd))
246 {
247 case bfd_mach_sparc_sparclet:
248 N_SET_MACHTYPE (*execp, M_SPARCLET);
249 break;
250 case bfd_mach_sparc_sparclite_le:
251 N_SET_MACHTYPE (*execp, M_SPARCLITE_LE);
252 break;
253 default:
254 N_SET_MACHTYPE (*execp, M_SPARC);
255 break;
256 }
257 break;
258 case bfd_arch_i386:
259 N_SET_MACHTYPE (*execp, M_386);
260 break;
261 case bfd_arch_a29k:
262 N_SET_MACHTYPE (*execp, M_29K);
263 break;
264 default:
265 N_SET_MACHTYPE (*execp, M_UNKNOWN);
266 }
267
268 choose_reloc_size (abfd);
269
270 N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
271
272 N_SET_DYNAMIC (*execp, (long)(bfd_get_file_flags (abfd) & DYNAMIC));
273
274 WRITE_HEADERS (abfd, execp);
275
276 return TRUE;
277 }
278 \f
279 /* core files */
280
281 #define CORE_MAGIC 0x080456
282 #define CORE_NAMELEN 16
283
284 /* The core structure is taken from the Sun documentation.
285 Unfortunately, they don't document the FPA structure, or at least I
286 can't find it easily. Fortunately the core header contains its own
287 length. So this shouldn't cause problems, except for c_ucode, which
288 so far we don't use but is easy to find with a little arithmetic. */
289
290 /* But the reg structure can be gotten from the SPARC processor handbook.
291 This really should be in a GNU include file though so that gdb can use
292 the same info. */
293 struct regs
294 {
295 int r_psr;
296 int r_pc;
297 int r_npc;
298 int r_y;
299 int r_g1;
300 int r_g2;
301 int r_g3;
302 int r_g4;
303 int r_g5;
304 int r_g6;
305 int r_g7;
306 int r_o0;
307 int r_o1;
308 int r_o2;
309 int r_o3;
310 int r_o4;
311 int r_o5;
312 int r_o6;
313 int r_o7;
314 };
315
316 /* Taken from Sun documentation: */
317
318 /* FIXME: It's worse than we expect. This struct contains TWO substructs
319 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
320 even portably access the stuff in between! */
321
322 struct external_sparc_core
323 {
324 int c_magic; /* Corefile magic number */
325 int c_len; /* Sizeof (struct core) */
326 #define SPARC_CORE_LEN 432
327 int c_regs[19]; /* General purpose registers -- MACHDEP SIZE */
328 struct external_exec c_aouthdr; /* A.out header */
329 int c_signo; /* Killing signal, if any */
330 int c_tsize; /* Text size (bytes) */
331 int c_dsize; /* Data size (bytes) */
332 int c_ssize; /* Stack size (bytes) */
333 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
334 double fp_stuff[1]; /* external FPU state (size unknown by us) */
335 /* The type "double" is critical here, for alignment.
336 SunOS declares a struct here, but the struct's alignment
337 is double since it contains doubles. */
338 int c_ucode; /* Exception no. from u_code */
339 /* (this member is not accessible by name since we don't
340 portably know the size of fp_stuff.) */
341 };
342
343 /* Core files generated by the BCP (the part of Solaris which allows
344 it to run SunOS4 a.out files). */
345 struct external_solaris_bcp_core
346 {
347 int c_magic; /* Corefile magic number */
348 int c_len; /* Sizeof (struct core) */
349 #define SOLARIS_BCP_CORE_LEN 456
350 int c_regs[19]; /* General purpose registers -- MACHDEP SIZE */
351 int c_exdata_vp; /* exdata structure */
352 int c_exdata_tsize;
353 int c_exdata_dsize;
354 int c_exdata_bsize;
355 int c_exdata_lsize;
356 int c_exdata_nshlibs;
357 short c_exdata_mach;
358 short c_exdata_mag;
359 int c_exdata_toffset;
360 int c_exdata_doffset;
361 int c_exdata_loffset;
362 int c_exdata_txtorg;
363 int c_exdata_datorg;
364 int c_exdata_entloc;
365 int c_signo; /* Killing signal, if any */
366 int c_tsize; /* Text size (bytes) */
367 int c_dsize; /* Data size (bytes) */
368 int c_ssize; /* Stack size (bytes) */
369 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
370 double fp_stuff[1]; /* external FPU state (size unknown by us) */
371 /* The type "double" is critical here, for alignment.
372 SunOS declares a struct here, but the struct's alignment
373 is double since it contains doubles. */
374 int c_ucode; /* Exception no. from u_code */
375 /* (this member is not accessible by name since we don't
376 portably know the size of fp_stuff.) */
377 };
378
379 struct external_sun3_core
380 {
381 int c_magic; /* Corefile magic number */
382 int c_len; /* Sizeof (struct core) */
383 #define SUN3_CORE_LEN 826 /* As of SunOS 4.1.1 */
384 int c_regs[18]; /* General purpose registers -- MACHDEP SIZE */
385 struct external_exec c_aouthdr; /* A.out header */
386 int c_signo; /* Killing signal, if any */
387 int c_tsize; /* Text size (bytes) */
388 int c_dsize; /* Data size (bytes) */
389 int c_ssize; /* Stack size (bytes) */
390 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
391 double fp_stuff[1]; /* external FPU state (size unknown by us) */
392 /* The type "double" is critical here, for alignment.
393 SunOS declares a struct here, but the struct's alignment
394 is double since it contains doubles. */
395 int c_ucode; /* Exception no. from u_code */
396 /* (this member is not accessible by name since we don't
397 portably know the size of fp_stuff.) */
398 };
399
400 struct internal_sunos_core
401 {
402 int c_magic; /* Corefile magic number */
403 int c_len; /* Sizeof (struct core) */
404 long c_regs_pos; /* file offset of General purpose registers */
405 int c_regs_size; /* size of General purpose registers */
406 struct internal_exec c_aouthdr; /* A.out header */
407 int c_signo; /* Killing signal, if any */
408 int c_tsize; /* Text size (bytes) */
409 int c_dsize; /* Data size (bytes) */
410 bfd_vma c_data_addr; /* Data start (address) */
411 int c_ssize; /* Stack size (bytes) */
412 bfd_vma c_stacktop; /* Stack top (address) */
413 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
414 long fp_stuff_pos; /* file offset of external FPU state (regs) */
415 int fp_stuff_size; /* Size of it */
416 int c_ucode; /* Exception no. from u_code */
417 };
418
419 static void swapcore_sun3
420 PARAMS ((bfd *, char *, struct internal_sunos_core *));
421 static void swapcore_sparc
422 PARAMS ((bfd *, char *, struct internal_sunos_core *));
423 static void swapcore_solaris_bcp
424 PARAMS ((bfd *, char *, struct internal_sunos_core *));
425
426 /* byte-swap in the Sun-3 core structure */
427 static void
428 swapcore_sun3 (abfd, ext, intcore)
429 bfd *abfd;
430 char *ext;
431 struct internal_sunos_core *intcore;
432 {
433 struct external_sun3_core *extcore = (struct external_sun3_core *) ext;
434
435 intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
436 intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
437 intcore->c_regs_pos = (long) (((struct external_sun3_core *) 0)->c_regs);
438 intcore->c_regs_size = sizeof (extcore->c_regs);
439 #if ARCH_SIZE == 64
440 aout_64_swap_exec_header_in
441 #else
442 aout_32_swap_exec_header_in
443 #endif
444 (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
445 intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
446 intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
447 intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
448 intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
449 intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
450 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
451 intcore->fp_stuff_pos = (long) (((struct external_sun3_core *) 0)->fp_stuff);
452 /* FP stuff takes up whole rest of struct, except c_ucode. */
453 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
454 (file_ptr) (((struct external_sun3_core *) 0)->fp_stuff);
455 /* Ucode is the last thing in the struct -- just before the end */
456 intcore->c_ucode = H_GET_32 (abfd,
457 (intcore->c_len
458 - sizeof (extcore->c_ucode)
459 + (unsigned char *) extcore));
460 intcore->c_stacktop = 0x0E000000; /* By experimentation */
461 }
462
463 /* byte-swap in the Sparc core structure */
464 static void
465 swapcore_sparc (abfd, ext, intcore)
466 bfd *abfd;
467 char *ext;
468 struct internal_sunos_core *intcore;
469 {
470 struct external_sparc_core *extcore = (struct external_sparc_core *) ext;
471
472 intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
473 intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
474 intcore->c_regs_pos = (long) (((struct external_sparc_core *) 0)->c_regs);
475 intcore->c_regs_size = sizeof (extcore->c_regs);
476 #if ARCH_SIZE == 64
477 aout_64_swap_exec_header_in
478 #else
479 aout_32_swap_exec_header_in
480 #endif
481 (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
482 intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
483 intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
484 intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
485 intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
486 intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
487 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
488 intcore->fp_stuff_pos = (long) (((struct external_sparc_core *) 0)->fp_stuff);
489 /* FP stuff takes up whole rest of struct, except c_ucode. */
490 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
491 (file_ptr) (((struct external_sparc_core *) 0)->fp_stuff);
492 /* Ucode is the last thing in the struct -- just before the end */
493 intcore->c_ucode = H_GET_32 (abfd,
494 (intcore->c_len
495 - sizeof (extcore->c_ucode)
496 + (unsigned char *) extcore));
497
498 /* Supposedly the user stack grows downward from the bottom of kernel memory.
499 Presuming that this remains true, this definition will work. */
500 /* Now sun has provided us with another challenge. The value is different
501 for sparc2 and sparc10 (both running SunOS 4.1.3). We pick one or
502 the other based on the current value of the stack pointer. This
503 loses (a) if the stack pointer has been clobbered, or (b) if the stack
504 is larger than 128 megabytes.
505
506 It's times like these you're glad they're switching to ELF.
507
508 Note that using include files or nlist on /vmunix would be wrong,
509 because we want the value for this core file, no matter what kind of
510 machine we were compiled on or are running on. */
511 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
512 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
513 {
514 bfd_vma sp = H_GET_32 (abfd, &((struct regs *) &extcore->c_regs[0])->r_o6);
515 if (sp < SPARC_USRSTACK_SPARC10)
516 intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
517 else
518 intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
519 }
520 }
521
522 /* byte-swap in the Solaris BCP core structure */
523 static void
524 swapcore_solaris_bcp (abfd, ext, intcore)
525 bfd *abfd;
526 char *ext;
527 struct internal_sunos_core *intcore;
528 {
529 struct external_solaris_bcp_core *extcore =
530 (struct external_solaris_bcp_core *) ext;
531
532 intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
533 intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
534 intcore->c_regs_pos = (long) (((struct external_solaris_bcp_core *) 0)->c_regs);
535 intcore->c_regs_size = sizeof (extcore->c_regs);
536
537 /* The Solaris BCP exdata structure does not contain an a_syms field,
538 so we are unable to synthesize an internal exec header.
539 Luckily we are able to figure out the start address of the data section,
540 which is the only thing needed from the internal exec header,
541 from the exdata structure.
542
543 As of Solaris 2.3, BCP core files for statically linked executables
544 are buggy. The exdata structure is not properly filled in, and
545 the data section is written from address zero instead of the data
546 start address. */
547 memset ((PTR) &intcore->c_aouthdr, 0, sizeof (struct internal_exec));
548 intcore->c_data_addr = H_GET_32 (abfd, &extcore->c_exdata_datorg);
549 intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
550 intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
551 intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
552 intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
553 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
554 intcore->fp_stuff_pos =
555 (long) (((struct external_solaris_bcp_core *) 0)->fp_stuff);
556 /* FP stuff takes up whole rest of struct, except c_ucode. */
557 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
558 (file_ptr) (((struct external_solaris_bcp_core *) 0)->fp_stuff);
559 /* Ucode is the last thing in the struct -- just before the end */
560 intcore->c_ucode = H_GET_32 (abfd,
561 (intcore->c_len
562 - sizeof (extcore->c_ucode)
563 + (unsigned char *) extcore));
564
565 /* Supposedly the user stack grows downward from the bottom of kernel memory.
566 Presuming that this remains true, this definition will work. */
567 /* Now sun has provided us with another challenge. The value is different
568 for sparc2 and sparc10 (both running SunOS 4.1.3). We pick one or
569 the other based on the current value of the stack pointer. This
570 loses (a) if the stack pointer has been clobbered, or (b) if the stack
571 is larger than 128 megabytes.
572
573 It's times like these you're glad they're switching to ELF.
574
575 Note that using include files or nlist on /vmunix would be wrong,
576 because we want the value for this core file, no matter what kind of
577 machine we were compiled on or are running on. */
578 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
579 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
580 {
581 bfd_vma sp = H_GET_32 (abfd, &((struct regs *) &extcore->c_regs[0])->r_o6);
582 if (sp < SPARC_USRSTACK_SPARC10)
583 intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
584 else
585 intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
586 }
587 }
588
589 /* need this cast because ptr is really void * */
590 #define core_hdr(bfd) ((bfd)->tdata.sun_core_data)
591 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
592 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
593 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
594 #define core_reg2sec(bfd) (core_hdr(bfd)->reg2_section)
595
596 /* These are stored in the bfd's tdata */
597 struct sun_core_struct
598 {
599 struct internal_sunos_core *hdr; /* core file header */
600 asection *data_section;
601 asection *stack_section;
602 asection *reg_section;
603 asection *reg2_section;
604 };
605
606 static const bfd_target *
607 sunos4_core_file_p (abfd)
608 bfd *abfd;
609 {
610 unsigned char longbuf[4]; /* Raw bytes of various header fields */
611 bfd_size_type core_size, amt;
612 unsigned long core_mag;
613 struct internal_sunos_core *core;
614 char *extcore;
615 struct mergem
616 {
617 struct sun_core_struct suncoredata;
618 struct internal_sunos_core internal_sunos_core;
619 char external_core[1];
620 } *mergem;
621
622 if (bfd_bread ((PTR) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
623 != sizeof (longbuf))
624 return 0;
625 core_mag = H_GET_32 (abfd, longbuf);
626
627 if (core_mag != CORE_MAGIC)
628 return 0;
629
630 /* SunOS core headers can vary in length; second word is size; */
631 if (bfd_bread ((PTR) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
632 != sizeof (longbuf))
633 return 0;
634 core_size = H_GET_32 (abfd, longbuf);
635 /* Sanity check */
636 if (core_size > 20000)
637 return 0;
638
639 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
640 return 0;
641
642 amt = core_size + sizeof (struct mergem);
643 mergem = (struct mergem *) bfd_zalloc (abfd, amt);
644 if (mergem == NULL)
645 return 0;
646
647 extcore = mergem->external_core;
648
649 if ((bfd_bread ((PTR) extcore, core_size, abfd)) != core_size)
650 {
651 loser:
652 bfd_release (abfd, (char *) mergem);
653 abfd->tdata.any = NULL;
654 bfd_section_list_clear (abfd);
655 return 0;
656 }
657
658 /* Validate that it's a core file we know how to handle, due to sun
659 botching the positioning of registers and other fields in a machine
660 dependent way. */
661 core = &mergem->internal_sunos_core;
662 switch (core_size)
663 {
664 case SPARC_CORE_LEN:
665 swapcore_sparc (abfd, extcore, core);
666 break;
667 case SUN3_CORE_LEN:
668 swapcore_sun3 (abfd, extcore, core);
669 break;
670 case SOLARIS_BCP_CORE_LEN:
671 swapcore_solaris_bcp (abfd, extcore, core);
672 break;
673 default:
674 bfd_set_error (bfd_error_system_call); /* FIXME */
675 goto loser;
676 }
677
678 abfd->tdata.sun_core_data = &mergem->suncoredata;
679 abfd->tdata.sun_core_data->hdr = core;
680
681 /* Create the sections. */
682 core_stacksec (abfd) = bfd_make_section_anyway (abfd, ".stack");
683 if (core_stacksec (abfd) == NULL)
684 /* bfd_release frees everything allocated after it's arg. */
685 goto loser;
686
687 core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
688 if (core_datasec (abfd) == NULL)
689 goto loser;
690
691 core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
692 if (core_regsec (abfd) == NULL)
693 goto loser;
694
695 core_reg2sec (abfd) = bfd_make_section_anyway (abfd, ".reg2");
696 if (core_reg2sec (abfd) == NULL)
697 goto loser;
698
699 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
700 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
701 core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
702 core_reg2sec (abfd)->flags = SEC_HAS_CONTENTS;
703
704 core_stacksec (abfd)->size = core->c_ssize;
705 core_datasec (abfd)->size = core->c_dsize;
706 core_regsec (abfd)->size = core->c_regs_size;
707 core_reg2sec (abfd)->size = core->fp_stuff_size;
708
709 core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
710 core_datasec (abfd)->vma = core->c_data_addr;
711 core_regsec (abfd)->vma = 0;
712 core_reg2sec (abfd)->vma = 0;
713
714 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
715 core_datasec (abfd)->filepos = core->c_len;
716 /* We'll access the regs afresh in the core file, like any section: */
717 core_regsec (abfd)->filepos = (file_ptr) core->c_regs_pos;
718 core_reg2sec (abfd)->filepos = (file_ptr) core->fp_stuff_pos;
719
720 /* Align to word at least */
721 core_stacksec (abfd)->alignment_power = 2;
722 core_datasec (abfd)->alignment_power = 2;
723 core_regsec (abfd)->alignment_power = 2;
724 core_reg2sec (abfd)->alignment_power = 2;
725
726 return abfd->xvec;
727 }
728
729 static char *
730 sunos4_core_file_failing_command (abfd)
731 bfd *abfd;
732 {
733 return core_hdr (abfd)->hdr->c_cmdname;
734 }
735
736 static int
737 sunos4_core_file_failing_signal (abfd)
738 bfd *abfd;
739 {
740 return core_hdr (abfd)->hdr->c_signo;
741 }
742
743 static bfd_boolean
744 sunos4_core_file_matches_executable_p (core_bfd, exec_bfd)
745 bfd *core_bfd;
746 bfd *exec_bfd;
747 {
748 if (core_bfd->xvec != exec_bfd->xvec)
749 {
750 bfd_set_error (bfd_error_system_call);
751 return FALSE;
752 }
753
754 /* Solaris core files do not include an aouthdr. */
755 if ((core_hdr (core_bfd)->hdr)->c_len == SOLARIS_BCP_CORE_LEN)
756 return TRUE;
757
758 return (memcmp ((char *) &((core_hdr (core_bfd)->hdr)->c_aouthdr),
759 (char *) exec_hdr (exec_bfd),
760 sizeof (struct internal_exec)) == 0);
761 }
762
763 #define MY_set_sizes sunos4_set_sizes
764 static bfd_boolean
765 sunos4_set_sizes (abfd)
766 bfd *abfd;
767 {
768 switch (bfd_get_arch (abfd))
769 {
770 default:
771 return FALSE;
772 case bfd_arch_sparc:
773 adata (abfd).page_size = 0x2000;
774 adata (abfd).segment_size = 0x2000;
775 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
776 return TRUE;
777 case bfd_arch_m68k:
778 adata (abfd).page_size = 0x2000;
779 adata (abfd).segment_size = 0x20000;
780 adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
781 return TRUE;
782 }
783 }
784
785 /* We default to setting the toolversion field to 1, as is required by
786 SunOS. */
787 #ifndef MY_exec_hdr_flags
788 #define MY_exec_hdr_flags 1
789 #endif
790
791 #ifndef MY_entry_is_text_address
792 #define MY_entry_is_text_address 0
793 #endif
794 #ifndef MY_add_dynamic_symbols
795 #define MY_add_dynamic_symbols 0
796 #endif
797 #ifndef MY_add_one_symbol
798 #define MY_add_one_symbol 0
799 #endif
800 #ifndef MY_link_dynamic_object
801 #define MY_link_dynamic_object 0
802 #endif
803 #ifndef MY_write_dynamic_symbol
804 #define MY_write_dynamic_symbol 0
805 #endif
806 #ifndef MY_check_dynamic_reloc
807 #define MY_check_dynamic_reloc 0
808 #endif
809 #ifndef MY_finish_dynamic_link
810 #define MY_finish_dynamic_link 0
811 #endif
812
813 static const struct aout_backend_data sunos4_aout_backend =
814 {
815 0, /* zmagic files are not contiguous */
816 1, /* text includes header */
817 MY_entry_is_text_address,
818 MY_exec_hdr_flags,
819 0, /* default text vma */
820 sunos4_set_sizes,
821 0, /* header is counted in zmagic text */
822 MY_add_dynamic_symbols,
823 MY_add_one_symbol,
824 MY_link_dynamic_object,
825 MY_write_dynamic_symbol,
826 MY_check_dynamic_reloc,
827 MY_finish_dynamic_link
828 };
829 \f
830 #define MY_core_file_failing_command sunos4_core_file_failing_command
831 #define MY_core_file_failing_signal sunos4_core_file_failing_signal
832 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
833
834 #define MY_bfd_debug_info_start bfd_void
835 #define MY_bfd_debug_info_end bfd_void
836 #define MY_bfd_debug_info_accumulate \
837 (void (*) PARAMS ((bfd *, struct bfd_section *))) bfd_void
838 #define MY_core_file_p sunos4_core_file_p
839 #define MY_write_object_contents NAME(aout,sunos4_write_object_contents)
840 #define MY_backend_data &sunos4_aout_backend
841
842 #ifndef TARGET_IS_LITTLE_ENDIAN_P
843 #define TARGET_IS_BIG_ENDIAN_P
844 #endif
845
846 #include "aout-target.h"