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