dadfd1487d963a56f370fa6b8f08637ac1cc8064
[external/binutils.git] / bfd / trad-core.c
1 /* BFD back end for traditional Unix core files (U-area and sections, raw)
2    Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.  */
3
4 /* This file does not define a particular back-end, but it defines routines
5    that can be used by other back-ends.  */
6
7 #include "bfd.h"
8 #include <stdio.h>
9 #include "libbfd.h"
10
11 #include "liba.out.h"           /* BFD a.out internal data structures */
12
13 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <sys/dir.h>
16 #include <signal.h>
17 #include <machine/reg.h>
18
19 #include <sys/user.h>           /* After a.out.h  */
20 #include <sys/file.h>
21 #include <sys/stat.h>
22
23 #include <errno.h>
24
25 /* need this cast b/c ptr is really void * */
26 #define core_hdr(bfd) (((struct core_data *) (bfd->tdata))->hdr)
27 #define core_datasec(bfd) (((struct core_data *) ((bfd)->tdata))->data_section)
28 #define core_stacksec(bfd) (((struct core_data*)((bfd)->tdata))->stack_section)
29 #define core_regsec(bfd) (((struct core_data *) ((bfd)->tdata))->reg_section)
30 #define core_upage(bfd) (((struct core_data *) ((bfd)->tdata))->upage)
31
32 /* These are stored in the bfd's tdata */
33 struct core_data {
34   struct user *upage;             /* core file header */
35   asection *data_section;
36   asection *stack_section;
37   asection *reg_section;
38 };
39
40 bfd_target *
41 trad_unix_core_file_p (abfd)
42      bfd *abfd;
43 {
44 #if HOST_SYS == SUN_SYS
45   return 0;
46 #else
47   int val;
48   char *rawptr;
49   struct user u;
50   unsigned int reg_offset, fp_reg_offset;
51
52   /* 4.2-style (and perhaps also sysV-style) core dump file.  */
53
54   val = bfd_read ((void *)&u, 1, sizeof u, abfd);
55   if (val != sizeof u)
56     return 0;                   /* Too small to be a core file */
57
58   /* Sanity check perhaps??? */
59   if (u.u_dsize > 0x1000000)    /* Remember, it's in pages... */
60     return 0;
61   if (u.u_ssize > 0x1000000)
62     return 0;
63   /* Check that the size claimed is no greater than the file size. FIXME. */
64
65   /* OK, we believe you.  You're a core file (sure, sure).  */
66
67   /* Allocate both the upage and the struct core_data at once, so
68      a single free() will free them both.  */
69   rawptr = (char *)zalloc (sizeof (u) + sizeof (struct core_data));
70   if (rawptr == NULL) {
71     bfd_error = no_memory;
72     return 0;
73   }
74   
75   set_tdata (abfd, (struct core_data *)rawptr);
76   core_upage (abfd) = (struct user *)(rawptr + sizeof (struct core_data));
77   *core_upage (abfd) = u;               /* Save that upage! */
78
79   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
80      them */
81   core_stacksec (abfd) = (asection *) zalloc (sizeof (asection));
82   if (core_stacksec (abfd) == NULL) {
83 loser:
84     bfd_error = no_memory;
85     free ((void *)rawptr);
86     return 0;
87   }
88   core_datasec (abfd) = (asection *) zalloc (sizeof (asection));
89   if (core_datasec (abfd) == NULL) {
90 loser1:
91     free ((void *)core_stacksec (abfd));
92     goto loser;
93   }
94   core_regsec (abfd) = (asection *) zalloc (sizeof (asection));
95   if (core_regsec (abfd) == NULL) {
96 loser2:
97     free ((void *)core_datasec (abfd));
98     goto loser1;
99   }
100
101   core_stacksec (abfd)->name = ".stack";
102   core_datasec (abfd)->name = ".data";
103   core_regsec (abfd)->name = ".reg";
104
105   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
106   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
107   core_regsec (abfd)->flags = SEC_ALLOC;
108
109   core_datasec (abfd)->size =  NBPG * u.u_dsize;
110   core_stacksec (abfd)->size = NBPG * u.u_ssize;
111   core_regsec (abfd)->size = NBPG * UPAGES;  /* Larger than sizeof struct u */
112
113   /* What a hack... we'd like to steal it from the exec file,
114      since the upage does not seem to provide it.  FIXME.  */
115   core_datasec (abfd)->vma = TEXT_START_ADDR + (NBPG * u.u_tsize);
116   core_stacksec (abfd)->vma = STACK_END_ADDR - (NBPG * u.u_ssize);
117   core_regsec (abfd)->vma = -1;
118
119   core_datasec (abfd)->filepos = NBPG * UPAGES;
120   core_stacksec (abfd)->filepos = (NBPG * UPAGES) + NBPG * u.u_dsize;
121   core_regsec (abfd)->filepos = 0;      /* Register segment is the upage */
122
123   /* Align to word at least */
124   core_stacksec (abfd)->alignment_power = 2;
125   core_datasec (abfd)->alignment_power = 2;
126   core_regsec (abfd)->alignment_power = 2;
127
128   abfd->sections = core_stacksec (abfd);
129   core_stacksec (abfd)->next = core_datasec (abfd);
130   core_datasec (abfd)->next = core_regsec (abfd);
131   abfd->section_count = 3;
132
133   return abfd->xvec;
134 #endif
135 }
136
137 char *
138 trad_unix_core_file_failing_command (abfd)
139      bfd *abfd;
140 {
141   if (*core_upage (abfd)->u_comm)
142     return core_upage (abfd)->u_comm;
143   else
144     return 0;
145 }
146
147 int
148 trad_unix_core_file_failing_signal (abfd)
149      bfd *abfd;
150 {
151   return -1;            /* FIXME, where is it? */
152 }
153
154 boolean
155 trad_unix_core_file_matches_executable_p  (core_bfd, exec_bfd)
156      bfd *core_bfd, *exec_bfd;
157 {
158   return true;          /* FIXME, We have no way of telling at this point */
159 }