* aoutf1.h: Add definition of MACHTYPE_OK.
[deliverable/binutils-gdb.git] / bfd / aoutf1.h
1 /* A.out "format 1" file handling code for BFD.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24
25 #include "aout/sun4.h"
26 #include "libaout.h" /* BFD a.out internal data structures */
27
28 #include "aout/aout64.h"
29 #include "aout/stab_gnu.h"
30 #include "aout/ar.h"
31
32 /* This is needed to reject a NewsOS file, e.g. in
33 gdb/testsuite/gdb.t10/crossload.exp. */
34 #define MACHTYPE_OK(mtype) ((mtype) == M_68010 || (mtype) == M_68020 \
35 || (mtype) == M_SPARC)
36
37 /*
38 The file @code{aoutf1.h} contains the code for BFD's
39 a.out back end. Control over the generated back end is given by these
40 two preprocessor names:
41 @table @code
42 @item ARCH_SIZE
43 This value should be either 32 or 64, depending upon the size of an
44 int in the target format. It changes the sizes of the structs which
45 perform the memory/disk mapping of structures.
46
47 The 64 bit backend may only be used if the host compiler supports 64
48 ints (eg long long with gcc), by defining the name @code{HOST_64_BIT} in @code{bfd.h}.
49 With this name defined, @emph{all} bfd operations are performed with 64bit
50 arithmetic, not just those to a 64bit target.
51
52 @item TARGETNAME
53 The name put into the target vector.
54 @item
55 @end table
56
57 */
58
59 void (*bfd_error_trap)();
60
61 /*SUPPRESS558*/
62 /*SUPPRESS529*/
63
64 void
65 DEFUN(NAME(sunos,set_arch_mach), (abfd, machtype),
66 bfd *abfd AND int machtype)
67 {
68 /* Determine the architecture and machine type of the object file. */
69 enum bfd_architecture arch;
70 long machine;
71 switch (machtype) {
72
73 case M_UNKNOWN:
74 /* Some Sun3s make magic numbers without cpu types in them, so
75 we'll default to the 68020. */
76 arch = bfd_arch_m68k;
77 machine = 68020;
78 break;
79
80 case M_68010:
81 case M_HP200:
82 arch = bfd_arch_m68k;
83 machine = 68010;
84 break;
85
86 case M_68020:
87 case M_HP300:
88 arch = bfd_arch_m68k;
89 machine = 68020;
90 break;
91
92 case M_SPARC:
93 arch = bfd_arch_sparc;
94 machine = 0;
95 break;
96
97 case M_386:
98 arch = bfd_arch_i386;
99 machine = 0;
100 break;
101
102 case M_29K:
103 arch = bfd_arch_a29k;
104 machine = 0;
105 break;
106
107 case M_HPUX:
108 arch = bfd_arch_m68k;
109 machine = 0;
110 break;
111
112 default:
113 arch = bfd_arch_obscure;
114 machine = 0;
115 break;
116 }
117 bfd_set_arch_mach(abfd, arch, machine);
118 }
119
120 #define SET_ARCH_MACH(ABFD, EXEC) \
121 NAME(sunos,set_arch_mach)(ABFD, N_MACHTYPE (EXEC)); \
122 choose_reloc_size(ABFD);
123
124 /* Determine the size of a relocation entry, based on the architecture */
125 static void
126 DEFUN(choose_reloc_size,(abfd),
127 bfd *abfd)
128 {
129 switch (bfd_get_arch(abfd)) {
130 case bfd_arch_sparc:
131 case bfd_arch_a29k:
132 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
133 break;
134 default:
135 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
136 break;
137 }
138 }
139
140 /* Write an object file in SunOS format.
141 Section contents have already been written. We write the
142 file header, symbols, and relocation. */
143
144 static boolean
145 DEFUN(NAME(aout,sunos4_write_object_contents),
146 (abfd),
147 bfd *abfd)
148 {
149 struct external_exec exec_bytes;
150 struct internal_exec *execp = exec_hdr (abfd);
151
152 /* Magic number, maestro, please! */
153 switch (bfd_get_arch(abfd)) {
154 case bfd_arch_m68k:
155 switch (bfd_get_mach(abfd)) {
156 case 68010:
157 N_SET_MACHTYPE(*execp, M_68010);
158 break;
159 default:
160 case 68020:
161 N_SET_MACHTYPE(*execp, M_68020);
162 break;
163 }
164 break;
165 case bfd_arch_sparc:
166 N_SET_MACHTYPE(*execp, M_SPARC);
167 break;
168 case bfd_arch_i386:
169 N_SET_MACHTYPE(*execp, M_386);
170 break;
171 case bfd_arch_a29k:
172 N_SET_MACHTYPE(*execp, M_29K);
173 break;
174 default:
175 N_SET_MACHTYPE(*execp, M_UNKNOWN);
176 }
177
178 choose_reloc_size(abfd);
179
180 #if 0
181 /* Some tools want this to be 0, some tools want this to be one.
182 Today, it seems that 0 is the most important setting (PR1927) */
183 N_SET_FLAGS (*execp, 0x0);
184 #else
185
186 /* Fri Jun 11 14:23:31 PDT 1993
187 FIXME
188 Today's optimal setting is 1. This is a pain, since it
189 reopens 1927. This should be readdressed by creating a new
190 target for each each supported, giving perhaps sun3/m68k
191 and sun4/sparc a.out formats.
192 */
193 N_SET_FLAGS (*execp, 1);
194 #endif
195
196 WRITE_HEADERS(abfd, execp);
197
198 return true;
199 }
200 \f
201 /* core files */
202
203 #define CORE_MAGIC 0x080456
204 #define CORE_NAMELEN 16
205
206 /* The core structure is taken from the Sun documentation.
207 Unfortunately, they don't document the FPA structure, or at least I
208 can't find it easily. Fortunately the core header contains its own
209 length. So this shouldn't cause problems, except for c_ucode, which
210 so far we don't use but is easy to find with a little arithmetic. */
211
212 /* But the reg structure can be gotten from the SPARC processor handbook.
213 This really should be in a GNU include file though so that gdb can use
214 the same info. */
215 struct regs {
216 int r_psr;
217 int r_pc;
218 int r_npc;
219 int r_y;
220 int r_g1;
221 int r_g2;
222 int r_g3;
223 int r_g4;
224 int r_g5;
225 int r_g6;
226 int r_g7;
227 int r_o0;
228 int r_o1;
229 int r_o2;
230 int r_o3;
231 int r_o4;
232 int r_o5;
233 int r_o6;
234 int r_o7;
235 };
236
237 /* Taken from Sun documentation: */
238
239 /* FIXME: It's worse than we expect. This struct contains TWO substructs
240 neither of whose size we know, WITH STUFF IN BETWEEN THEM! We can't
241 even portably access the stuff in between! */
242
243 struct external_sparc_core {
244 int c_magic; /* Corefile magic number */
245 int c_len; /* Sizeof (struct core) */
246 #define SPARC_CORE_LEN 432
247 int c_regs[19]; /* General purpose registers -- MACHDEP SIZE */
248 struct external_exec c_aouthdr; /* A.out header */
249 int c_signo; /* Killing signal, if any */
250 int c_tsize; /* Text size (bytes) */
251 int c_dsize; /* Data size (bytes) */
252 int c_ssize; /* Stack size (bytes) */
253 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
254 double fp_stuff[1]; /* external FPU state (size unknown by us) */
255 /* The type "double" is critical here, for alignment.
256 SunOS declares a struct here, but the struct's alignment
257 is double since it contains doubles. */
258 int c_ucode; /* Exception no. from u_code */
259 /* (this member is not accessible by name since we don't
260 portably know the size of fp_stuff.) */
261 };
262
263 struct external_sun3_core {
264 int c_magic; /* Corefile magic number */
265 int c_len; /* Sizeof (struct core) */
266 #define SUN3_CORE_LEN 826 /* As of SunOS 4.1.1 */
267 int c_regs[18]; /* General purpose registers -- MACHDEP SIZE */
268 struct external_exec c_aouthdr; /* A.out header */
269 int c_signo; /* Killing signal, if any */
270 int c_tsize; /* Text size (bytes) */
271 int c_dsize; /* Data size (bytes) */
272 int c_ssize; /* Stack size (bytes) */
273 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
274 double fp_stuff[1]; /* external FPU state (size unknown by us) */
275 /* The type "double" is critical here, for alignment.
276 SunOS declares a struct here, but the struct's alignment
277 is double since it contains doubles. */
278 int c_ucode; /* Exception no. from u_code */
279 /* (this member is not accessible by name since we don't
280 portably know the size of fp_stuff.) */
281 };
282
283 struct internal_sunos_core {
284 int c_magic; /* Corefile magic number */
285 int c_len; /* Sizeof (struct core) */
286 long c_regs_pos; /* file offset of General purpose registers */
287 int c_regs_size; /* size of General purpose registers */
288 struct internal_exec c_aouthdr; /* A.out header */
289 int c_signo; /* Killing signal, if any */
290 int c_tsize; /* Text size (bytes) */
291 int c_dsize; /* Data size (bytes) */
292 int c_ssize; /* Stack size (bytes) */
293 bfd_vma c_stacktop; /* Stack top (address) */
294 char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
295 long fp_stuff_pos; /* file offset of external FPU state (regs) */
296 int fp_stuff_size; /* Size of it */
297 int c_ucode; /* Exception no. from u_code */
298 };
299
300 /* byte-swap in the Sun-3 core structure */
301 static void
302 DEFUN(swapcore_sun3,(abfd, ext, intcore),
303 bfd *abfd AND
304 char *ext AND
305 struct internal_sunos_core *intcore)
306 {
307 struct external_sun3_core *extcore = (struct external_sun3_core *)ext;
308
309 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
310 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len );
311 intcore->c_regs_pos = (long) (((struct external_sun3_core *)0)->c_regs);
312 intcore->c_regs_size = sizeof (extcore->c_regs);
313 NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
314 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
315 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
316 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
317 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
318 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
319 intcore->fp_stuff_pos = (long) (((struct external_sun3_core *)0)->fp_stuff);
320 /* FP stuff takes up whole rest of struct, except c_ucode. */
321 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
322 (file_ptr)(((struct external_sun3_core *)0)->fp_stuff);
323 /* Ucode is the last thing in the struct -- just before the end */
324 intcore->c_ucode =
325 bfd_h_get_32 (abfd,
326 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
327 intcore->c_stacktop = 0x0E000000; /* By experimentation */
328 }
329
330
331 /* byte-swap in the Sparc core structure */
332 static void
333 DEFUN(swapcore_sparc,(abfd, ext, intcore),
334 bfd *abfd AND
335 char *ext AND
336 struct internal_sunos_core *intcore)
337 {
338 struct external_sparc_core *extcore = (struct external_sparc_core *)ext;
339
340 intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
341 intcore->c_len = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len );
342 intcore->c_regs_pos = (long) (((struct external_sparc_core *)0)->c_regs);
343 intcore->c_regs_size = sizeof (extcore->c_regs);
344 NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
345 intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
346 intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
347 intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
348 intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
349 memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
350 intcore->fp_stuff_pos = (long) (((struct external_sparc_core *)0)->fp_stuff);
351 /* FP stuff takes up whole rest of struct, except c_ucode. */
352 intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
353 (file_ptr)(((struct external_sparc_core *)0)->fp_stuff);
354 /* Ucode is the last thing in the struct -- just before the end */
355 intcore->c_ucode =
356 bfd_h_get_32 (abfd,
357 intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
358
359 /* Supposedly the user stack grows downward from the bottom of kernel memory.
360 Presuming that this remains true, this definition will work. */
361 /* Now sun has provided us with another challenge. The value is different
362 for sparc2 and sparc10 (both running SunOS 4.1.3). We pick one or
363 the other based on the current value of the stack pointer. This
364 loses (a) if the stack pointer has been clobbered, or (b) if the stack
365 is larger than 128 megabytes.
366
367 It's times like these you're glad they're switching to ELF.
368
369 Note that using include files or nlist on /vmunix would be wrong,
370 because we want the value for this core file, no matter what kind of
371 machine we were compiled on or are running on. */
372 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
373 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
374 {
375 bfd_vma sp = bfd_h_get_32
376 (abfd, (unsigned char *)&((struct regs *)&extcore->c_regs[0])->r_o6);
377 if (sp < SPARC_USRSTACK_SPARC10)
378 intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
379 else
380 intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
381 }
382 }
383
384 /* need this cast because ptr is really void * */
385 #define core_hdr(bfd) ((bfd)->tdata.sun_core_data)
386 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
387 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
388 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
389 #define core_reg2sec(bfd) (core_hdr(bfd)->reg2_section)
390
391 /* These are stored in the bfd's tdata */
392 struct sun_core_struct {
393 struct internal_sunos_core *hdr; /* core file header */
394 asection *data_section;
395 asection *stack_section;
396 asection *reg_section;
397 asection *reg2_section;
398 };
399
400 static bfd_target *
401 DEFUN(sunos4_core_file_p,(abfd),
402 bfd *abfd)
403 {
404 unsigned char longbuf[4]; /* Raw bytes of various header fields */
405 int core_size;
406 int core_mag;
407 struct internal_sunos_core *core;
408 char *extcore;
409 struct mergem {
410 struct sun_core_struct suncoredata;
411 struct internal_sunos_core internal_sunos_core;
412 char external_core[1];
413 } *mergem;
414
415 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
416 sizeof (longbuf))
417 return 0;
418 core_mag = bfd_h_get_32 (abfd, longbuf);
419
420 if (core_mag != CORE_MAGIC) return 0;
421
422 /* SunOS core headers can vary in length; second word is size; */
423 if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
424 sizeof (longbuf))
425 return 0;
426 core_size = bfd_h_get_32 (abfd, longbuf);
427 /* Sanity check */
428 if (core_size > 20000)
429 return 0;
430
431 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) < 0) return 0;
432
433 mergem = (struct mergem *)bfd_zalloc (abfd, core_size + sizeof (struct mergem));
434 if (mergem == NULL) {
435 bfd_error = no_memory;
436 return 0;
437 }
438
439 extcore = mergem->external_core;
440
441 if ((bfd_read ((PTR) extcore, 1, core_size, abfd)) != core_size) {
442 bfd_error = system_call_error;
443 bfd_release (abfd, (char *)mergem);
444 return 0;
445 }
446
447 /* Validate that it's a core file we know how to handle, due to sun
448 botching the positioning of registers and other fields in a machine
449 dependent way. */
450 core = &mergem->internal_sunos_core;
451 switch (core_size) {
452 case SPARC_CORE_LEN:
453 swapcore_sparc (abfd, extcore, core);
454 break;
455 case SUN3_CORE_LEN:
456 swapcore_sun3 (abfd, extcore, core);
457 break;
458 default:
459 bfd_error = system_call_error; /* FIXME */
460 bfd_release (abfd, (char *)mergem);
461 return 0;
462 }
463
464 abfd->tdata.sun_core_data = &mergem->suncoredata;
465 abfd->tdata.sun_core_data->hdr = core;
466
467 /* create the sections. This is raunchy, but bfd_close wants to reclaim
468 them */
469 core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
470 if (core_stacksec (abfd) == NULL) {
471 loser:
472 bfd_error = no_memory;
473 bfd_release (abfd, (char *)mergem);
474 return 0;
475 }
476 core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
477 if (core_datasec (abfd) == NULL) {
478 loser1:
479 bfd_release (abfd, core_stacksec (abfd));
480 goto loser;
481 }
482 core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
483 if (core_regsec (abfd) == NULL) {
484 loser2:
485 bfd_release (abfd, core_datasec (abfd));
486 goto loser1;
487 }
488 core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
489 if (core_reg2sec (abfd) == NULL) {
490 bfd_release (abfd, core_regsec (abfd));
491 goto loser2;
492 }
493
494 core_stacksec (abfd)->name = ".stack";
495 core_datasec (abfd)->name = ".data";
496 core_regsec (abfd)->name = ".reg";
497 core_reg2sec (abfd)->name = ".reg2";
498
499 core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
500 core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
501 core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
502 core_reg2sec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
503
504 core_stacksec (abfd)->_raw_size = core->c_ssize;
505 core_datasec (abfd)->_raw_size = core->c_dsize;
506 core_regsec (abfd)->_raw_size = core->c_regs_size;
507 core_reg2sec (abfd)->_raw_size = core->fp_stuff_size;
508
509 core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
510 core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
511 core_regsec (abfd)->vma = 0;
512 core_reg2sec (abfd)->vma = 0;
513
514 core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
515 core_datasec (abfd)->filepos = core->c_len;
516 /* We'll access the regs afresh in the core file, like any section: */
517 core_regsec (abfd)->filepos = (file_ptr)core->c_regs_pos;
518 core_reg2sec (abfd)->filepos = (file_ptr)core->fp_stuff_pos;
519
520 /* Align to word at least */
521 core_stacksec (abfd)->alignment_power = 2;
522 core_datasec (abfd)->alignment_power = 2;
523 core_regsec (abfd)->alignment_power = 2;
524 core_reg2sec (abfd)->alignment_power = 2;
525
526 abfd->sections = core_stacksec (abfd);
527 core_stacksec (abfd)->next = core_datasec (abfd);
528 core_datasec (abfd)->next = core_regsec (abfd);
529 core_regsec (abfd)->next = core_reg2sec (abfd);
530
531 abfd->section_count = 4;
532
533 return abfd->xvec;
534 }
535
536 static char *sunos4_core_file_failing_command (abfd)
537 bfd *abfd;
538 {
539 return core_hdr (abfd)->hdr->c_cmdname;
540 }
541
542 static int
543 DEFUN(sunos4_core_file_failing_signal,(abfd),
544 bfd *abfd)
545 {
546 return core_hdr (abfd)->hdr->c_signo;
547 }
548
549 static boolean
550 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
551 bfd *core_bfd AND
552 bfd *exec_bfd)
553 {
554 if (core_bfd->xvec != exec_bfd->xvec) {
555 bfd_error = system_call_error;
556 return false;
557 }
558
559 return (memcmp ((char *)&((core_hdr (core_bfd)->hdr)->c_aouthdr),
560 (char *) exec_hdr (exec_bfd),
561 sizeof (struct internal_exec)) == 0) ? true : false;
562 }
563
564 #define MY_set_sizes sunos4_set_sizes
565 static boolean
566 DEFUN (sunos4_set_sizes, (abfd),
567 bfd *abfd)
568 {
569 switch (bfd_get_arch (abfd))
570 {
571 default:
572 return false;
573 case bfd_arch_sparc:
574 adata(abfd).page_size = 0x2000;
575 adata(abfd).segment_size = 0x2000;
576 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
577 return true;
578 case bfd_arch_m68k:
579 adata(abfd).page_size = 0x2000;
580 adata(abfd).segment_size = 0x20000;
581 adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
582 return true;
583 }
584 }
585
586 static CONST struct aout_backend_data sunos4_aout_backend = {
587 0, 1, 0, sunos4_set_sizes, 0,
588 };
589 \f
590 #define MY_core_file_failing_command sunos4_core_file_failing_command
591 #define MY_core_file_failing_signal sunos4_core_file_failing_signal
592 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
593
594 #define MY_bfd_debug_info_start bfd_void
595 #define MY_bfd_debug_info_end bfd_void
596 #define MY_bfd_debug_info_accumulate \
597 (void (*) PARAMS ((bfd *, struct sec *))) bfd_void
598 #define MY_core_file_p sunos4_core_file_p
599 #define MY_write_object_contents NAME(aout,sunos4_write_object_contents)
600 #define MY_backend_data &sunos4_aout_backend
601
602 #define TARGET_IS_BIG_ENDIAN_P
603
604 #include "aout-target.h"
This page took 0.043227 seconds and 5 git commands to generate.