public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] objcopy: check input flavor before setting PE/COFF section alignment
@ 2024-04-19  9:27 Jan Beulich
  2024-04-19 10:37 ` Nick Clifton
  0 siblings, 1 reply; 4+ messages in thread
From: Jan Beulich @ 2024-04-19  9:27 UTC (permalink / raw)
  To: Binutils; +Cc: Nick Clifton

coff_section_data() and elf_section_data() use the same underlying
field. The pointer being non-NULL therefore isn't sufficient to know
that pei_section_data() can validly be used on the incoming object.
Apparently in 64-bit-host builds the resulting memory corruption is
benign, whereas in 32-bit-host builds a segmentation fault occurs upon
de-referencing pei_section_data()'s return value.
---
Of course the value (first) being set on the input bfd is suspicious
in the first place: When copying e.g. ELF to PE/COFF, the option ought
to be similarly respected, yet clearly it can't be set like this then on
the incoming object. The change here is merely to address the testsuite
failures observed for Arm64 and RISC-V ("Check if efi app format is
recognized") as well as the (latent) memory corruption.

--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4310,6 +4310,7 @@ setup_section (bfd *ibfd, sec_ptr isecti
   if (p != NULL)
     alignment = p->alignment;
   else if (pe_section_alignment != (bfd_vma) -1
+	   && bfd_get_flavour (ibfd) == bfd_target_coff_flavour
 	   && bfd_get_flavour (obfd) == bfd_target_coff_flavour)
     {
       alignment = power_of_two (pe_section_alignment);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objcopy: check input flavor before setting PE/COFF section alignment
  2024-04-19  9:27 [PATCH] objcopy: check input flavor before setting PE/COFF section alignment Jan Beulich
@ 2024-04-19 10:37 ` Nick Clifton
  2024-04-22  7:15   ` Jan Beulich
  0 siblings, 1 reply; 4+ messages in thread
From: Nick Clifton @ 2024-04-19 10:37 UTC (permalink / raw)
  To: Jan Beulich, Binutils

Hi Jan,

> coff_section_data() and elf_section_data() use the same underlying
> field. The pointer being non-NULL therefore isn't sufficient to know
> that pei_section_data() can validly be used on the incoming object.
> Apparently in 64-bit-host builds the resulting memory corruption is
> benign, whereas in 32-bit-host builds a segmentation fault occurs upon
> de-referencing pei_section_data()'s return value.
> ---
> Of course the value (first) being set on the input bfd is suspicious
> in the first place: When copying e.g. ELF to PE/COFF, the option ought
> to be similarly respected, yet clearly it can't be set like this then on
> the incoming object. The change here is merely to address the testsuite
> failures observed for Arm64 and RISC-V ("Check if efi app format is
> recognized") as well as the (latent) memory corruption.

Thanks for fixing my oversight!

Cheers
   Nick


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objcopy: check input flavor before setting PE/COFF section alignment
  2024-04-19 10:37 ` Nick Clifton
@ 2024-04-22  7:15   ` Jan Beulich
  2024-05-08 10:26     ` Nick Clifton
  0 siblings, 1 reply; 4+ messages in thread
From: Jan Beulich @ 2024-04-22  7:15 UTC (permalink / raw)
  To: Nick Clifton; +Cc: Binutils

On 19.04.2024 12:37, Nick Clifton wrote:
>> coff_section_data() and elf_section_data() use the same underlying
>> field. The pointer being non-NULL therefore isn't sufficient to know
>> that pei_section_data() can validly be used on the incoming object.
>> Apparently in 64-bit-host builds the resulting memory corruption is
>> benign, whereas in 32-bit-host builds a segmentation fault occurs upon
>> de-referencing pei_section_data()'s return value.
>> ---
>> Of course the value (first) being set on the input bfd is suspicious
>> in the first place: When copying e.g. ELF to PE/COFF, the option ought
>> to be similarly respected, yet clearly it can't be set like this then on
>> the incoming object. The change here is merely to address the testsuite
>> failures observed for Arm64 and RISC-V ("Check if efi app format is
>> recognized") as well as the (latent) memory corruption.
> 
> Thanks for fixing my oversight!

Well, before putting it in - any thoughts on the post-commit-message remark
above? Is it really meant to stay the way of the input bfd's data is being
altered, rather than keeping that intact and fiddling only with the output?
And thus - afaict - rendering the command line option (silently) useless
when copying ELF to PE?

Jan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] objcopy: check input flavor before setting PE/COFF section alignment
  2024-04-22  7:15   ` Jan Beulich
@ 2024-05-08 10:26     ` Nick Clifton
  0 siblings, 0 replies; 4+ messages in thread
From: Nick Clifton @ 2024-05-08 10:26 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Binutils

Hi Jan,

>>> Of course the value (first) being set on the input bfd is suspicious
>>> in the first place: When copying e.g. ELF to PE/COFF, the option ought
>>> to be similarly respected, yet clearly it can't be set like this then on
>>> the incoming object. The change here is merely to address the testsuite
>>> failures observed for Arm64 and RISC-V ("Check if efi app format is
>>> recognized") as well as the (latent) memory corruption.

> Well, before putting it in - any thoughts on the post-commit-message remark
> above?

Sorry.  Well in the first place converting from ELF to PE is always going to
be a difficult process.  So trying to combine it with adjustments to other
properties, such as alignment, is just asking for trouble.  My feeling therefore
is that we ought to be have a warning in the documentation telling users
to be careful and maybe take things one step at a time.

> Is it really meant to stay the way of the input bfd's data is being
> altered, rather than keeping that intact and fiddling only with the output?
> And thus - afaict - rendering the command line option (silently) useless
> when copying ELF to PE?

You are right.  For this particular case maybe we should add a test for
ELF->PE format changing and refuse to adjust the alignments.

Cheers
   Nick




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-05-08 10:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-19  9:27 [PATCH] objcopy: check input flavor before setting PE/COFF section alignment Jan Beulich
2024-04-19 10:37 ` Nick Clifton
2024-04-22  7:15   ` Jan Beulich
2024-05-08 10:26     ` Nick Clifton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).