Use command style in cmd_show_list
[binutils-gdb.git] / bfd / ptrace-core.c
blob1d44f9f5d021680e1b14440a7ef41fa420cd48ab
1 /* BFD backend for core files which use the ptrace_user structure
2 Copyright (C) 1993-2025 Free Software Foundation, Inc.
3 The structure of this file is based on trad-core.c written by John Gilmore
4 of Cygnus Support.
5 Modified to work with the ptrace_user structure by Kevin A. Buettner.
6 (Longterm it may be better to merge this file with trad-core.c)
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
25 #ifdef PTRACE_CORE
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include "libbfd.h"
31 #include <sys/param.h>
32 #include <sys/dir.h>
33 #include <signal.h>
34 #include <sys/ptrace.h>
36 struct trad_core_struct
38 asection *data_section;
39 asection *stack_section;
40 asection *reg_section;
41 struct ptrace_user u;
44 #define core_upage(bfd) (&((bfd)->tdata.trad_core_data->u))
45 #define core_datasec(bfd) ((bfd)->tdata.trad_core_data->data_section)
46 #define core_stacksec(bfd) ((bfd)->tdata.trad_core_data->stack_section)
47 #define core_regsec(bfd) ((bfd)->tdata.trad_core_data->reg_section)
49 /* forward declarations */
51 bfd_cleanup ptrace_unix_core_file_p (bfd *abfd);
52 char * ptrace_unix_core_file_failing_command (bfd *abfd);
53 int ptrace_unix_core_file_failing_signal (bfd *abfd);
54 #define ptrace_unix_core_file_matches_executable_p generic_core_file_matches_executable_p
55 #define ptrace_unix_core_file_pid _bfd_nocore_core_file_pid
56 static void swap_abort (void);
58 bfd_cleanup
59 ptrace_unix_core_file_p (bfd *abfd)
61 int val;
62 struct ptrace_user u;
63 struct trad_core_struct *rawptr;
64 flagword flags;
66 val = bfd_read (&u, sizeof u, abfd);
67 if (val != sizeof u || u.pt_magic != _BCS_PTRACE_MAGIC
68 || u.pt_rev != _BCS_PTRACE_REV)
70 /* Too small to be a core file */
71 bfd_set_error (bfd_error_wrong_format);
72 return 0;
75 /* OK, we believe you. You're a core file (sure, sure). */
77 /* Allocate both the upage and the struct core_data at once, so
78 a single free() will free them both. */
79 rawptr = bfd_alloc (abfd, sizeof (*rawptr) + 1);
81 if (rawptr == NULL)
82 return 0;
84 abfd->tdata.trad_core_data = rawptr;
86 rawptr->u = u; /*Copy the uarea into the tdata part of the bfd */
88 /* Ensure core_file_failing_command string is terminated. This is
89 just to stop buffer overflows on fuzzed files. */
90 ((char *) rawptr)[sizeof (*rawptr)] = 0;
92 /* Create the sections. */
94 flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
95 core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
96 flags);
97 if (core_stacksec (abfd) == NULL)
98 goto fail;
99 core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
100 flags);
101 if (core_datasec (abfd) == NULL)
102 goto fail;
103 core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
104 SEC_HAS_CONTENTS);
105 if (core_regsec (abfd) == NULL)
106 goto fail;
108 /* FIXME: Need to worry about shared memory, library data, and library
109 text. I don't think that any of these things are supported on the
110 system on which I am developing this for though. */
112 core_datasec (abfd)->size = u.pt_dsize;
113 core_stacksec (abfd)->size = u.pt_ssize;
114 core_regsec (abfd)->size = sizeof (u);
116 core_datasec (abfd)->vma = u.pt_o_data_start;
117 core_stacksec (abfd)->vma = USRSTACK - u.pt_ssize;
118 core_regsec (abfd)->vma = 0 - sizeof (u); /* see trad-core.c */
120 core_datasec (abfd)->filepos = (int) u.pt_dataptr;
121 core_stacksec (abfd)->filepos = (int) (u.pt_dataptr + u.pt_dsize);
122 core_regsec (abfd)->filepos = 0; /* Register segment is ptrace_user */
124 /* Align to word at least */
125 core_stacksec (abfd)->alignment_power = 2;
126 core_datasec (abfd)->alignment_power = 2;
127 core_regsec (abfd)->alignment_power = 2;
129 return _bfd_no_cleanup;
131 fail:
132 bfd_release (abfd, abfd->tdata.any);
133 abfd->tdata.any = NULL;
134 bfd_section_list_clear (abfd);
135 return NULL;
138 char *
139 ptrace_unix_core_file_failing_command (bfd *abfd)
141 char *com = abfd->tdata.trad_core_data->u.pt_comm;
143 if (*com)
144 return com;
145 else
146 return 0;
150 ptrace_unix_core_file_failing_signal (bfd *abfd)
152 return abfd->tdata.trad_core_data->u.pt_sigframe.sig_num;
155 /* If somebody calls any byte-swapping routines, shoot them. */
156 static void
157 swap_abort (void)
159 abort (); /* This way doesn't require any declaration for ANSI to fuck up */
162 #define NO_GET ((bfd_vma (*) (const void *)) swap_abort)
163 #define NO_PUT ((void (*) (bfd_vma, void *)) swap_abort)
164 #define NO_GETS ((bfd_signed_vma (*) (const void *)) swap_abort)
165 #define NO_GET64 ((uint64_t (*) (const void *)) swap_abort)
166 #define NO_PUT64 ((void (*) (uint64_t, void *)) swap_abort)
167 #define NO_GETS64 ((int64_t (*) (const void *)) swap_abort)
169 const bfd_target core_ptrace_vec =
171 "trad-core",
172 bfd_target_unknown_flavour,
173 BFD_ENDIAN_UNKNOWN, /* target byte order */
174 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
175 (HAS_RELOC | EXEC_P | /* object flags */
176 HAS_LINENO | HAS_DEBUG |
177 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
178 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
179 0, /* symbol prefix */
180 ' ', /* ar_pad_char */
181 16, /* ar_max_namelen */
182 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
183 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit data */
184 NO_GET, NO_GETS, NO_PUT, /* 32 bit data */
185 NO_GET, NO_GETS, NO_PUT, /* 16 bit data */
186 NO_GET64, NO_GETS64, NO_PUT64, /* 64 bit hdrs */
187 NO_GET, NO_GETS, NO_PUT, /* 32 bit hdrs */
188 NO_GET, NO_GETS, NO_PUT, /* 16 bit hdrs */
190 { /* bfd_check_format */
191 _bfd_dummy_target, /* unknown format */
192 _bfd_dummy_target, /* object file */
193 _bfd_dummy_target, /* archive */
194 ptrace_unix_core_file_p /* a core file */
196 { /* bfd_set_format */
197 _bfd_bool_bfd_false_error,
198 _bfd_bool_bfd_false_error,
199 _bfd_bool_bfd_false_error,
200 _bfd_bool_bfd_false_error,
202 { /* bfd_write_contents */
203 _bfd_bool_bfd_false_error,
204 _bfd_bool_bfd_false_error,
205 _bfd_bool_bfd_false_error,
206 _bfd_bool_bfd_false_error,
209 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
210 BFD_JUMP_TABLE_COPY (_bfd_generic),
211 BFD_JUMP_TABLE_CORE (ptrace_unix),
212 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
213 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
214 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
215 BFD_JUMP_TABLE_WRITE (_bfd_generic),
216 BFD_JUMP_TABLE_LINK (_bfd_nolink),
217 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
219 NULL,
221 NULL /* backend_data */
224 #endif /* PTRACE_CORE */