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