* targets.c (bfd_target): Add _bfd_free_cached_info field.
[platform/upstream/binutils.git] / bfd / hppabsd-core.c
1 /* BFD back-end for HPPA BSD core files.
2    Copyright 1993 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20    Written by the Center for Software Science at the University of Utah
21    and by Cygnus Support. 
22
23    The core file structure for the Utah 4.3BSD and OSF1 ports on the
24    PA is a mix between traditional cores and hpux cores -- just
25    different enough that supporting this format would tend to add
26    gross hacks to trad-core.c or hpux-core.c.  So instead we keep any
27    gross hacks isolated to this file.  */
28    
29
30 /* This file can only be compiled on systems which use HPPA-BSD style
31    core files.  In the config/XXXXXX.mh file for such a system add
32    HDEFINES=-DHPPABSD_CORE
33    HDEPFILES=hppabsd-core.o
34
35    I would not expect this to be of use to any other host/target, but
36    you never know.  */
37
38 #include "bfd.h"
39 #include "sysdep.h"
40 #include "libbfd.h"
41
42 #if defined (HOST_HPPABSD)
43
44 #include "machine/vmparam.h"
45
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/dir.h>
50 #include <signal.h>
51 #include <machine/reg.h>
52 #include <sys/user.h>           /* After a.out.h  */
53 #include <sys/file.h>
54 #include <errno.h>
55
56 static asection *make_bfd_asection PARAMS ((bfd *, CONST char *,
57                                             flagword, bfd_size_type,
58                                             bfd_vma, unsigned int));
59 static asymbol *hppabsd_core_make_empty_symbol PARAMS ((bfd *));
60 static bfd_target *hppabsd_core_core_file_p PARAMS ((bfd *));
61 static char *hppabsd_core_core_file_failing_command PARAMS ((bfd *));
62 static int hppabsd_core_core_file_failing_signal PARAMS ((bfd *));
63 static boolean hppabsd_core_core_file_matches_executable_p
64   PARAMS ((bfd *, bfd *));
65 static void swap_abort PARAMS ((void));
66
67 /* These are stored in the bfd's tdata.  */
68
69 struct hppabsd_core_struct
70   {
71     int sig;
72     char cmd[MAXCOMLEN + 1];
73     asection *data_section;
74     asection *stack_section;
75     asection *reg_section;
76   };
77
78 #define core_hdr(bfd) ((bfd)->tdata.hppabsd_core_data)
79 #define core_signal(bfd) (core_hdr(bfd)->sig)
80 #define core_command(bfd) (core_hdr(bfd)->cmd)
81 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
82 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
83 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
84
85 static asection *
86 make_bfd_asection (abfd, name, flags, _raw_size, vma, alignment_power)
87      bfd *abfd;
88      CONST char *name;
89      flagword flags;
90      bfd_size_type _raw_size;
91      bfd_vma vma;
92      unsigned int alignment_power;
93 {
94   asection *asect;
95
96   asect = bfd_make_section (abfd, name);
97   if (!asect)
98     return NULL;
99
100   asect->flags = flags;
101   asect->_raw_size = _raw_size;
102   asect->vma = vma;
103   asect->filepos = bfd_tell (abfd);
104   asect->alignment_power = alignment_power;
105
106   return asect;
107 }
108
109 static asymbol *
110 hppabsd_core_make_empty_symbol (abfd)
111      bfd *abfd;
112 {
113   asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
114   if (new)
115     new->the_bfd = abfd;
116   return new;
117 }
118
119 static bfd_target *
120 hppabsd_core_core_file_p (abfd)
121      bfd *abfd;
122 {
123   int val;
124   struct user u;
125   struct hppabsd_core_struct *coredata;
126   int clicksz;
127
128   /* Try to read in the u-area.  We will need information from this
129      to know how to grok the rest of the core structures.  */
130   val = bfd_read ((void *) &u, 1, sizeof u, abfd);
131   if (val != sizeof u)
132     {
133       bfd_set_error (bfd_error_wrong_format);
134       return NULL;
135     }
136
137   /* Get the page size out of the u structure.  This will be different
138      for PA 1.0 machines and PA 1.1 machines.   Yuk!  */
139   clicksz = u.u_pcb.pcb_pgsz;
140
141   /* Sanity checks.  Make sure the size of the core file matches the
142      the size computed from information within the core itself.  */
143   {
144     FILE *stream = bfd_cache_lookup (abfd);
145     struct stat statbuf;
146     if (stream == NULL || fstat (fileno (stream), &statbuf) < 0)
147       {
148         bfd_set_error (bfd_error_system_call);
149         return NULL;
150       }
151     if (NBPG * (UPAGES + u.u_dsize + u.u_ssize) > statbuf.st_size)
152       {
153         bfd_set_error (bfd_error_file_truncated);
154         return NULL;
155       }
156     if (clicksz * (UPAGES + u.u_dsize + u.u_ssize) < statbuf.st_size)
157       {
158         /* The file is too big.  Maybe it's not a core file
159            or we otherwise have bad values for u_dsize and u_ssize).  */
160         bfd_set_error (bfd_error_wrong_format);
161         return NULL;
162       }
163   }
164
165   /* OK, we believe you.  You're a core file (sure, sure).  */
166
167   coredata = (struct hppabsd_core_struct *)
168     bfd_zalloc (abfd, sizeof (struct hppabsd_core_struct));
169   if (!coredata)
170     {
171       bfd_set_error (bfd_error_no_memory);
172       return NULL;
173     }
174
175   /* Make the core data and available via the tdata part of the BFD.  */
176   abfd->tdata.hppabsd_core_data = coredata;
177
178   /* Create the sections.  */
179   core_stacksec (abfd) = make_bfd_asection (abfd, ".stack",
180                                            SEC_ALLOC + SEC_HAS_CONTENTS,
181                                            clicksz * u.u_ssize,
182                                            NBPG * (USIZE + KSTAKSIZE) 
183                                              + clicksz * u.u_dsize, 2);
184   core_stacksec (abfd)->vma = USRSTACK; 
185
186   core_datasec (abfd) = make_bfd_asection (abfd, ".data",
187                                           SEC_ALLOC + SEC_LOAD
188                                             + SEC_HAS_CONTENTS,
189                                           clicksz * u.u_dsize,
190                                           NBPG * (USIZE + KSTAKSIZE), 2);
191   core_datasec (abfd)->vma = UDATASEG;
192
193   core_regsec (abfd) = make_bfd_asection (abfd, ".reg",
194                                          SEC_ALLOC + SEC_HAS_CONTENTS,
195                                          KSTAKSIZE * NBPG,
196                                          NBPG * USIZE, 2);
197   core_regsec (abfd)->vma = 0;
198
199   strncpy (core_command (abfd), u.u_comm, MAXCOMLEN + 1);
200   core_signal (abfd) = u.u_code;
201   return abfd->xvec;
202 }
203
204 static char *
205 hppabsd_core_core_file_failing_command (abfd)
206      bfd *abfd;
207 {
208   return core_command (abfd);
209 }
210
211 /* ARGSUSED */
212 static int
213 hppabsd_core_core_file_failing_signal (abfd)
214      bfd *abfd;
215 {
216   return core_signal (abfd);
217 }
218
219 /* ARGSUSED */
220 static boolean
221 hppabsd_core_core_file_matches_executable_p (core_bfd, exec_bfd)
222      bfd *core_bfd, *exec_bfd;
223 {
224   /* There's no way to know this...  */
225   return true;
226 }
227
228 \f
229 /* No archive file support via this BFD */
230 #define hppabsd_core_openr_next_archived_file \
231         bfd_generic_openr_next_archived_file
232 #define hppabsd_core_generic_stat_arch_elt      bfd_generic_stat_arch_elt
233 #define hppabsd_core_slurp_armap                bfd_false
234 #define hppabsd_core_slurp_extended_name_table  bfd_true
235 #define hppabsd_core_write_armap                (boolean (*) PARAMS     \
236     ((bfd *arch, unsigned int elength, struct orl *map, \
237       unsigned int orl_count, int stridx))) bfd_false
238 #define hppabsd_core_truncate_arname            bfd_dont_truncate_arname
239
240 #define hppabsd_core_close_and_cleanup          bfd_generic_close_and_cleanup
241 #define hppabsd_core_set_section_contents       (boolean (*) PARAMS     \
242         ((bfd *abfd, asection *section, PTR data, file_ptr offset,      \
243         bfd_size_type count))) bfd_generic_set_section_contents
244 #define hppabsd_core_get_section_contents \
245         bfd_generic_get_section_contents
246 #define hppabsd_core_new_section_hook           (boolean (*) PARAMS     \
247         ((bfd *, sec_ptr))) bfd_true
248 #define hppabsd_core_get_symtab_upper_bound     bfd_0l
249 #define hppabsd_core_get_symtab                 (long (*) PARAMS \
250         ((bfd *, struct symbol_cache_entry **))) bfd_0l
251 #define hppabsd_core_get_reloc_upper_bound      (long (*) PARAMS \
252         ((bfd *, sec_ptr))) bfd_0l
253 #define hppabsd_core_canonicalize_reloc         (long (*) PARAMS \
254         ((bfd *, sec_ptr, arelent **, struct symbol_cache_entry**))) bfd_0l
255 #define hppabsd_core_print_symbol               (void (*) PARAMS        \
256         ((bfd *, PTR, struct symbol_cache_entry  *,                     \
257         bfd_print_symbol_type))) bfd_false
258 #define hppabsd_core_get_symbol_info            (void (*) PARAMS        \
259         ((bfd *, struct symbol_cache_entry  *,                  \
260         symbol_info *))) bfd_false
261 #define hppabsd_core_get_lineno                 (alent * (*) PARAMS     \
262         ((bfd *, struct symbol_cache_entry *))) bfd_nullvoidptr
263 #define hppabsd_core_set_arch_mach              (boolean (*) PARAMS     \
264         ((bfd *, enum bfd_architecture, unsigned long))) bfd_false
265 #define hppabsd_core_find_nearest_line          (boolean (*) PARAMS     \
266         ((bfd *abfd, struct sec  *section,                              \
267          struct symbol_cache_entry  **symbols,bfd_vma offset,           \
268          CONST char **file, CONST char **func, unsigned int *line))) bfd_false
269 #define hppabsd_core_sizeof_headers             (int (*) PARAMS \
270         ((bfd *, boolean))) bfd_0
271
272 #define hppabsd_core_bfd_debug_info_start       bfd_void
273 #define hppabsd_core_bfd_debug_info_end         bfd_void
274 #define hppabsd_core_bfd_debug_info_accumulate  (void (*) PARAMS        \
275         ((bfd *, struct sec *))) bfd_void
276 #define hppabsd_core_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
277 #define hppabsd_core_bfd_relax_section          bfd_generic_relax_section
278 #define hppabsd_core_bfd_reloc_type_lookup \
279   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
280 #define hppabsd_core_bfd_make_debug_symbol \
281   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
282 #define hppabsd_core_bfd_link_hash_table_create \
283   ((struct bfd_link_hash_table *(*) PARAMS ((bfd *))) bfd_nullvoidptr)
284 #define hppabsd_core_bfd_link_add_symbols \
285   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
286 #define hppabsd_core_bfd_final_link \
287   ((boolean (*) PARAMS ((bfd *, struct bfd_link_info *))) bfd_false)
288 #define hppabsd_core_bfd_copy_private_section_data \
289   ((boolean (*) PARAMS ((bfd *, asection *, bfd *, asection *))) bfd_false)
290 #define hppabsd_core_bfd_copy_private_bfd_data \
291   ((boolean (*) PARAMS ((bfd *, bfd *))) bfd_false)
292 #define hppabsd_core_bfd_is_local_label \
293   ((boolean (*) PARAMS ((bfd *, asymbol *))) bfd_false)
294 #define hppabsd_core_bfd_free_cached_info bfd_true
295
296 /* If somebody calls any byte-swapping routines, shoot them.  */
297 static void
298 swap_abort ()
299 {
300   /* This way doesn't require any declaration for ANSI to fuck up.  */
301   abort ();     
302 }
303
304 #define NO_GET  ((bfd_vma (*) PARAMS ((   const bfd_byte *))) swap_abort )
305 #define NO_PUT  ((void    (*) PARAMS ((bfd_vma, bfd_byte *))) swap_abort )
306 #define NO_SIGNED_GET \
307   ((bfd_signed_vma (*) PARAMS ((const bfd_byte *))) swap_abort )
308
309 bfd_target hppabsd_core_vec =
310   {
311     "hppabsd-core",
312     bfd_target_unknown_flavour,
313     true,                       /* target byte order */
314     true,                       /* target headers byte order */
315     (HAS_RELOC | EXEC_P |       /* object flags */
316      HAS_LINENO | HAS_DEBUG |
317      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
318     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
319     0,                                                     /* symbol prefix */
320     ' ',                                                   /* ar_pad_char */
321     16,                                                    /* ar_max_namelen */
322     3,                                                     /* minimum alignment power */
323     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit data */
324     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit data */
325     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit data */
326     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 64 bit hdrs */
327     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 32 bit hdrs */
328     NO_GET, NO_SIGNED_GET, NO_PUT,      /* 16 bit hdrs */
329
330     {                           /* bfd_check_format */
331      _bfd_dummy_target,         /* unknown format */
332      _bfd_dummy_target,         /* object file */
333      _bfd_dummy_target,         /* archive */
334      hppabsd_core_core_file_p   /* a core file */
335     },
336     {                           /* bfd_set_format */
337      bfd_false, bfd_false,
338      bfd_false, bfd_false
339     },
340     {                           /* bfd_write_contents */
341      bfd_false, bfd_false,
342      bfd_false, bfd_false
343     },
344     
345     JUMP_TABLE(hppabsd_core),
346     (PTR) 0                     /* backend_data */
347 };
348 #endif