[PATCH v2 2/3] bfd/PE: don't write an OptionalHeader to object files

Jan Beulich jbeulich@suse.com
Fri Nov 27 10:36:19 GMT 2026


On 26.11.2026 09:51, Tobias Deiminger wrote:
> The bigobj format per definition has no room for an OptionalHeader. BFD
> writes one anyway if the output is executable or has a start address.
> Conforming readers then read the spurious OptionalHeader when trying to
> parse section headers:
> 
>   $ gcc -g -o testprog testprog.c
>   $ objcopy -I elf64-x86-64 -O pe-bigobj-x86-64 testprog testprog.bigobj
>   $ objdump -b pe-bigobj-x86-64 -h testprog.bigobj
>     0 ^K^B^B/       00001050  ...
> 
> The fix is to avoid writing that spurious header.
> 
> More generally, regular PE objects should not have an OptionalHeader
> either (different reasoning, same conclusion [1]). For consistency the
> fix is placed in common COFF code and stops writing an OptionalHeader to
> any PE object, not only bigobj.
> 
> As a side effect, a PE object written by BFD can no longer record a
> start address. This shouldn't be an issue, since objects are not
> intended to be loaded [2]. In odd cases where executables are converted
> to objects (ld, objcopy), the start address is silently dropped.
> 
> Reading is unchanged. Existing PE objects with an OptionalHeader remain
> readable. PE images and non-PE COFF are also not affected by this patch.
> 
> [1] PE Format, COFF File Header, SizeOfOptionalHeader: "This value
>     should be zero for an object file."
> [2] PE Format, Optional Header: "An object file can have an optional
>     header, but generally this header has no function in an object file
>     except to increase its size."
> 
> Signed-off-by: Tobias Deiminger <tobias.deiminger@linutronix.de>

This is okay, yet ...

> ---
>  bfd/coff-aarch64.c     |  4 ++--
>  bfd/coff-i386.c        |  4 ++--
>  bfd/coff-ia64.c        |  2 +-
>  bfd/coff-loongarch64.c |  2 +-
>  bfd/coff-riscv64.c     |  2 +-
>  bfd/coff-x86_64.c      |  4 ++--
>  bfd/coffcode.h         | 23 ++++++++++++++++-------
>  7 files changed, 25 insertions(+), 16 deletions(-)

... target maintainers may want to have a say, so I'll give it some time
before putting in.

However, again I wonder if we can't get away with less churn ...

> --- a/bfd/coffcode.h
> +++ b/bfd/coffcode.h
> @@ -378,8 +378,10 @@ extern const bfd_target TARGET_SYM_BIG;
>  
>  #if defined(COFF_WITH_PE) && !defined(COFF_IMAGE_WITH_PE)
>  # define COFF_DEFAULT_MAX_NSCNS (IMAGE_SYM_SECTION_MAX + 1)
> +# define COFF_EXEC_P 0
>  #else
>  # define COFF_DEFAULT_MAX_NSCNS 32768
> +# define COFF_EXEC_P EXEC_P
>  #endif
>  
>  #if defined (COFF_LONG_SECTION_NAMES)
> @@ -3055,10 +3057,17 @@ coff_compute_section_file_positions (bfd * abfd)
>      }
>  #endif
>  
> -  if (bfd_get_start_address (abfd))
> -    /*  A start address may have been added to the original file. In this
> -	case it will need an optional header to record it.  */
> -    abfd->flags |= EXEC_P;
> +  if (bfd_applicable_file_flags (abfd) & EXEC_P)
> +    {
> +      /* A start address may have been added to the original file.  In this
> +	 case it will need an optional header to record it.  */
> +      if (bfd_get_start_address (abfd))
> +	abfd->flags |= EXEC_P;
> +    }
> +  else
> +    /* ld may have requested the executable flag even if not supported by
> +       the target format.  */
> +    abfd->flags &= ~EXEC_P;

... here:

  if (!(bfd_applicable_file_flags (abfd) & EXEC_P))
    /* ld may have requested the executable flag even if not supported by
       the target format.  */
    abfd->flags &= ~EXEC_P;
  else if (bfd_get_start_address (abfd))
    /*  A start address may have been added to the original file. In this
	case it will need an optional header to record it.  */
    abfd->flags |= EXEC_P;

Jan


More information about the Binutils mailing list