Bug 31191 - objcopy leaves temporary files (and directories) in case of error
Summary: objcopy leaves temporary files (and directories) in case of error
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.42
: P2 normal
Target Milestone: 2.42
Assignee: Alan Modra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-25 22:08 UTC by Moody Liu
Modified: 2023-12-27 06:07 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed: 2023-12-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moody Liu 2023-12-25 22:08:00 UTC
In case of objdump errors with "symbol `.debug_info' required but not present" when running "objcopy -R .debug_info ./libsomething.a", it leaves temporary files in the folder.

---
$ objcopy -R .debug_info ./libgfortran.a
objcopy: ./stftlphn/unix.o: symbol `.debug_info' required but not present
objcopy: ./stftlphn/unix.o: no symbols
---

OBSERVED: ./stftlphn/unix.o exists.
EXPECTED: no left-over temporary files.

reading the source code reveals: 

1. objcopy.c, in copy_archive() around L3765, bfd_close is called but errored.
2. bfd_nonfatal_message is called and status set to 1
3. L3778, 3779: the bfd_close branch is taken, leaving l->obfd as NULL.
4. in cleanup_and_exit, if l->obfd is NULL, rmdir is called

(however l->name is a file, which leads to an ENOTDIR and the file is not removed)
Comment 1 Moody Liu 2023-12-25 22:09:27 UTC
the following patch fixes the issue, but I'm not sure what implication (or side-effect) it has.


diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index b3c8d0f81ac8..c2f1b9421e4c 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -3766,7 +3766,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
        {
          bfd_nonfatal_message (output_name, NULL, NULL, NULL);
          /* Error in new object file. Don't change archive.  */
-         status = 1;
+         del = 1;
        }
 
       if (del)
Comment 2 Alan Modra 2023-12-26 06:20:06 UTC
Yes, that change looks good.  I'll fix the rmdir problem too.
Comment 3 Sourceware Commits 2023-12-27 06:06:45 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=145c21056abeace6b3f6cafb65c29a7812036852

commit 145c21056abeace6b3f6cafb65c29a7812036852
Author: Alan Modra <amodra@gmail.com>
Date:   Tue Dec 26 16:20:34 2023 +1030

    PR31191, objcopy leaves temporary files
    
    Fix the ENOTDIR rmdir too.
    
            PR 31191
            * objcopy.c (copy_archive): Localise uses of "l".  Remove
            const from name_list.name.  Unlink output element on bfd_close
            error, and NULL list->name to indicate file is removed.  Adjust
            cleanup to prevent rmdir on non-existent file.
Comment 4 Alan Modra 2023-12-27 06:07:25 UTC
Fix for 2.42