[PATCH v3] ld: Rename a file on Windows fails if target already exists

Torbjorn SVENSSON torbjorn.svensson@foss.st.com
Tue Jul 22 14:13:44 GMT 2025



On 2025-07-22 15:18, Andreas Schwab wrote:
> On Jul 22 2025, Torbjörn SVENSSON wrote:
> 
>> @@ -10840,10 +10841,21 @@ cmdline_add_object_only_section (bfd_byte *contents, size_t size)
>>         fatal (_("%P: failed to finish output with object-only section\n"));
>>       }
>>   
>> +  if (!bfd_close (ibfd))
>> +    {
>> +      einfo (_("%P%F: failed to close input\n"));
>> +    }
>> +
>>     /* Must be freed after bfd_close ().  */
> 
> Is this comment ...
> 
>>     free (isympp);
>>     free (osympp);
>>   
>> +  /* Must unlink to ensure rename works on Windows.  */
>> +  if (unlink (output_filename) && errno != ENOENT)
>> +    {
>> +      einfo (_("%P%F: failed to unlink %s\n"), output_filename);
>> +    }
>> +
>>     if (rename (ofilename, output_filename))
>>       {
>>         unlink (ofilename);
>> @@ -10858,6 +10870,8 @@ loser:
>>     free (osympp);
>>     if (obfd)
>>       bfd_close (obfd);
>> +  if (ibfd)
>> +    bfd_close (ibfd);
> 
> ... also relevant for this?


Not related to my change, but I will fix this in a v4 of the patch 
regardless.

> Perhaps the function should use bfd_alloc instead of xmalloc for isympp
> and osympp.

This part is over my head. If someone would be willing to take on this 
endeavor, then please do.


Kind regards,
Torbjörn


More information about the Binutils mailing list