[PATCH] ld: pep: Stop emitting reference to the pseudo-relocator function

Pali Rohár pali.rohar@gmail.com
Tue Jul 14 19:14:40 GMT 2026


Hello, referencing the _pei386_runtime_relocator by LD could be used as
a step for making the mingw-w64 crt runtime to not statically include
the whole psuedo reloc in all generated PE binaries by LD, but include
them only to the PE binaries which really needs pseudo reloc
functionality.

But seems that in LD there some bug and LD is not able to find the
_pei386_runtime_relocator symbol in .a object when also tentative
definition is used for on-demand inclusion of symbol.

Year ago I have reported this issue into binutils/ld bugzilla:
https://sourceware.org/bugzilla/show_bug.cgi?id=33180

If this issue is fixed in GNU LD, then mingw-w64-crt code could be
extended to statically include _pei386_runtime_relocator functionality
only into binaries which really needs them.


So the emitting reference to _pei386_runtime_relocator is
not "entirely useless", it is needed for above on-demand functionality.

On Thursday 02 July 2026 15:38:15 Jon Turney wrote:
> The exported _pei386_runtime_relocator function currently only exists as
> a non-functional stub in the Cygwin runtime, merely to satisfy this
> reference synthesized in ld [1].
> 
> The function still has contents in the MinGW runtime [2], but is already
> referenced by being explcitly called at crt startup/dll load.
> 
> We'd like to drop the (slightly confusing) stub from the Cygwin runtime,
> especially in possible future architectures (e.g aarch64), where
> backwards compatibilty concerns don't exist.
> 
> So, for PE+ targets, stop explicitly emitting a reference to
> _pei386_runtime_relocator in the linker.
> 
> (The archeological record is unclear why this functionality exists in
> ld.
> 
> Since --enable-runtime-pseudo-reloc-v1 exists for 32-bit PE arches, and
> out of an abundance of caution, keep emitting it for those targets (i.e.
> i386) where it conceivably might still be needed or useful with an
> ancient runtime.)
> 
> Cc'ed to mingw-w64-public list.
> 
> [1] https://cygwin.com/cgit/newlib-cygwin/tree/winsup/cygwin/lib/pseudo-reloc-dummy.c
> [2] https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/crt/pseudo-reloc.c#l479
> 
> Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
> Cc: mingw-w64-public@lists.sourceforge.net
> ---
>  ld/pe-dll.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/ld/pe-dll.c b/ld/pe-dll.c
> index f43bd14896e..be5722fa8ad 100644
> --- a/ld/pe-dll.c
> +++ b/ld/pe-dll.c
> @@ -170,7 +170,9 @@ static struct bfd_section *edata_s, *reloc_s;
>  static unsigned char *edata_d, *reloc_d;
>  static unsigned char *reloc_d = NULL;
>  static size_t edata_sz, reloc_sz = 0;
> +#ifndef pe_use_plus
>  static int runtime_pseudo_relocs_created = 0;
> +#endif
>  static bool runtime_pseudp_reloc_v2_init = false;
>  
>  typedef struct
> @@ -1773,7 +1775,7 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
>    /* This can happen for example when LTO has eliminated all code.  */
>    if (total_relocs == 0)
>      return;
> -  
> +
>    /* At this point, we have total_relocs relocation addresses in
>       reloc_addresses, which are all suitable for the .reloc section.
>       We must now create the new sections.  */
> @@ -2798,6 +2800,7 @@ make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED,
>    return abfd;
>  }
>  
> +#ifndef pe_use_plus
>  /*	.section	.rdata
>  	.rva		__pei386_runtime_relocator  */
>  
> @@ -2841,6 +2844,7 @@ pe_create_runtime_relocator_reference (bfd *parent)
>    bfd_make_readable (abfd);
>    return abfd;
>  }
> +#endif
>  
>  void
>  pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend, char *name,
> @@ -2894,11 +2898,15 @@ pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend, char *name,
>  				     link_info.output_bfd);
>        add_bfd_to_link (b, bfd_get_filename (b), &link_info);
>  
> +      /* We think this is entirely useless, but emit a reference to the
> +	 relocator on the i386 target, just in case. */
> +#ifndef pe_use_plus
>        if (runtime_pseudo_relocs_created++ == 0)
>  	{
>  	  b = pe_create_runtime_relocator_reference (link_info.output_bfd);
>  	  add_bfd_to_link (b, bfd_get_filename (b), &link_info);
>  	}
> +#endif
>      }
>  
>    else if (addend != 0)
> -- 
> 2.51.0
> 


More information about the Binutils mailing list