Merge branch 'vendor/GREP'
[dragonfly.git] / contrib / binutils-2.20 / bfd / corefile.c
1 /* Core file generic interface routines for BFD.
2    Copyright 1990, 1991, 1992, 1993, 1994, 2000, 2001, 2002, 2003, 2005,
3    2007 Free Software Foundation, Inc.
4    Written by Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23 /*
24 SECTION
25         Core files
26
27 SUBSECTION
28         Core file functions
29
30 DESCRIPTION
31         These are functions pertaining to core files.
32 */
33
34 #include "sysdep.h"
35 #include "bfd.h"
36 #include "libbfd.h"
37
38 /*
39 FUNCTION
40         bfd_core_file_failing_command
41
42 SYNOPSIS
43         const char *bfd_core_file_failing_command (bfd *abfd);
44
45 DESCRIPTION
46         Return a read-only string explaining which program was running
47         when it failed and produced the core file @var{abfd}.
48
49 */
50
51 const char *
52 bfd_core_file_failing_command (bfd *abfd)
53 {
54   if (abfd->format != bfd_core)
55     {
56       bfd_set_error (bfd_error_invalid_operation);
57       return NULL;
58     }
59   return BFD_SEND (abfd, _core_file_failing_command, (abfd));
60 }
61
62 /*
63 FUNCTION
64         bfd_core_file_failing_signal
65
66 SYNOPSIS
67         int bfd_core_file_failing_signal (bfd *abfd);
68
69 DESCRIPTION
70         Returns the signal number which caused the core dump which
71         generated the file the BFD @var{abfd} is attached to.
72 */
73
74 int
75 bfd_core_file_failing_signal (bfd *abfd)
76 {
77   if (abfd->format != bfd_core)
78     {
79       bfd_set_error (bfd_error_invalid_operation);
80       return 0;
81     }
82   return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
83 }
84
85 /*
86 FUNCTION
87         core_file_matches_executable_p
88
89 SYNOPSIS
90         bfd_boolean core_file_matches_executable_p
91           (bfd *core_bfd, bfd *exec_bfd);
92
93 DESCRIPTION
94         Return <<TRUE>> if the core file attached to @var{core_bfd}
95         was generated by a run of the executable file attached to
96         @var{exec_bfd}, <<FALSE>> otherwise.
97 */
98
99 bfd_boolean
100 core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
101 {
102   if (core_bfd->format != bfd_core || exec_bfd->format != bfd_object)
103     {
104       bfd_set_error (bfd_error_wrong_format);
105       return FALSE;
106     }
107
108   return BFD_SEND (core_bfd, _core_file_matches_executable_p,
109                    (core_bfd, exec_bfd));
110 }
111
112 /*
113 FUNCTION
114         generic_core_file_matches_executable_p
115
116 SYNOPSIS
117         bfd_boolean generic_core_file_matches_executable_p
118           (bfd *core_bfd, bfd *exec_bfd);
119
120 DESCRIPTION
121         Return TRUE if the core file attached to @var{core_bfd}
122         was generated by a run of the executable file attached
123         to @var{exec_bfd}.  The match is based on executable
124         basenames only.
125
126         Note: When not able to determine the core file failing
127         command or the executable name, we still return TRUE even
128         though we're not sure that core file and executable match.
129         This is to avoid generating a false warning in situations
130         where we really don't know whether they match or not.
131 */
132
133 bfd_boolean
134 generic_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
135 {
136   char *exec;
137   char *core;
138   char *last_slash;
139
140   if (exec_bfd == NULL || core_bfd == NULL)
141     return TRUE;
142
143   /* The cast below is to avoid a compiler warning due to the assignment
144      of the const char * returned by bfd_core_file_failing_command to a
145      non-const char *.  In this case, the assignement does not lead to
146      breaking the const, as we're only reading the string.  */
147      
148   core = (char *) bfd_core_file_failing_command (core_bfd);
149   if (core == NULL)
150     return TRUE;
151
152   exec = bfd_get_filename (exec_bfd);
153   if (exec == NULL)
154     return TRUE;
155
156   last_slash = strrchr (core, '/');
157   if (last_slash != NULL)
158     core = last_slash + 1;
159
160   last_slash = strrchr (exec, '/');
161   if (last_slash != NULL)
162     exec = last_slash + 1;
163   
164   return strcmp (exec, core) == 0;
165 }
166