This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
strip/objcopy segfault with archives
- From: Alan Modra <amodra at gmail dot com>
- To: binutils at sourceware dot org
- Date: Fri, 6 May 2011 22:04:40 +0930
- Subject: strip/objcopy segfault with archives
A recently added testcase to PR12632 exposed a bug in strip/objcopy, a
missing check on the return status from bfd_openw, which led to a
segfault. This patch fixes that and shares code common to both the
copy_object and copy_unknown_object cases, in the process getting rid
of a goto.
Applying mainline, when/if sourceware cvs will talk to me.
* objcopy.c (copy_archive): Check bfd_openw result in unknown object
case. Rewrite without goto.
Index: binutils/objcopy.c
===================================================================
RCS file: /cvs/src/src/binutils/objcopy.c,v
retrieving revision 1.151
diff -u -p -r1.151 objcopy.c
--- binutils/objcopy.c 4 Apr 2011 01:35:59 -0000 1.151
+++ binutils/objcopy.c 6 May 2011 10:15:06 -0000
@@ -2024,6 +2024,7 @@ copy_archive (bfd *ibfd, bfd *obfd, cons
struct stat buf;
int stat_status = 0;
bfd_boolean del = TRUE;
+ bfd_boolean ok_object;
/* Create an output file for this member. */
output_name = concat (dir, "/",
@@ -2061,44 +2062,42 @@ copy_archive (bfd *ibfd, bfd *obfd, cons
l->obfd = NULL;
list = l;
- if (bfd_check_format (this_element, bfd_object))
+ ok_object = bfd_check_format (this_element, bfd_object);
+ if (!ok_object)
+ bfd_nonfatal_message (NULL, this_element, NULL,
+ _("Unable to recognise the format of file"));
+
+ /* PR binutils/3110: Cope with archives
+ containing multiple target types. */
+ if (force_output_target || !ok_object)
+ output_bfd = bfd_openw (output_name, output_target);
+ else
+ output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
+
+ if (output_bfd == NULL)
{
- /* PR binutils/3110: Cope with archives
- containing multiple target types. */
- if (force_output_target)
- output_bfd = bfd_openw (output_name, output_target);
- else
- output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
+ bfd_nonfatal_message (output_name, NULL, NULL, NULL);
+ status = 1;
+ return;
+ }
+
+ if (ok_object)
+ {
+ del = !copy_object (this_element, output_bfd, input_arch);
- if (output_bfd == NULL)
+ if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
+ /* Try again as an unknown object file. */
+ ok_object = FALSE;
+ else if (!bfd_close (output_bfd))
{
bfd_nonfatal_message (output_name, NULL, NULL, NULL);
+ /* Error in new object file. Don't change archive. */
status = 1;
- return;
}
-
- del = ! copy_object (this_element, output_bfd, input_arch);
-
- if (! del
- || bfd_get_arch (this_element) != bfd_arch_unknown)
- {
- if (!bfd_close (output_bfd))
- {
- bfd_nonfatal_message (output_name, NULL, NULL, NULL);
- /* Error in new object file. Don't change archive. */
- status = 1;
- }
- }
- else
- goto copy_unknown_element;
}
- else
- {
- bfd_nonfatal_message (NULL, this_element, NULL,
- _("Unable to recognise the format of file"));
- output_bfd = bfd_openw (output_name, output_target);
-copy_unknown_element:
+ if (!ok_object)
+ {
del = !copy_unknown_object (this_element, output_bfd);
if (!bfd_close_all_done (output_bfd))
{
--
Alan Modra
Australia Development Lab, IBM