ld: read the FreeBSD ELF hints file in either byte order

Message ID 20261030130553.61799-2-pkubaj@anongoth.pl
State New
Headers
Series ld: read the FreeBSD ELF hints file in either byte order |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Piotr Kubaj Oct. 30, 2026, 1:05 p.m. UTC
  From: Piotr Kubaj <pkubaj@FreeBSD.org>

FreeBSD's ldconfig writes the hints file in little-endian byte order on
all architectures unless it is run with -B, so the header is not
necessarily in host byte order.

This causes an issue when trying to link some software on powerpc64
(big-endian POWER).
---
 ld/ldelf.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
  

Comments

Jan Beulich Oct. 30, 2026, 1:25 p.m. UTC | #1
On 30.10.2026 14:05, Piotr Kubaj via Binutils wrote:
> From: Piotr Kubaj <pkubaj@FreeBSD.org>
> 
> FreeBSD's ldconfig writes the hints file in little-endian byte order on
> all architectures unless it is run with -B, so the header is not
> necessarily in host byte order.
> 
> This causes an issue when trying to link some software on powerpc64
> (big-endian POWER).

Okay, preferably with ...

> --- a/ld/ldelf.c
> +++ b/ld/ldelf.c
> @@ -780,18 +780,30 @@ ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
>        if (f != NULL)
>  	{
>  	  struct elfhints_hdr hdr;
> +	  bfd_vma (*get32) (const void *) = NULL;
>  
> -	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
> -	      && hdr.magic == ELFHINTS_MAGIC
> -	      && hdr.version == 1)
> +	  /* FreeBSD's ldconfig writes the hints file in little-endian
> +	     byte order on all architectures unless it is run with -B,
> +	     so the header is not necessarily in host byte order.  */
> +	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr))
>  	    {
> -	      if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
> +	      if (bfd_getl32 (&hdr.magic) == ELFHINTS_MAGIC)
> +		get32 = bfd_getl32;
> +	      else if (bfd_getb32 (&hdr.magic) == ELFHINTS_MAGIC)
> +		get32 = bfd_getb32;
> +	    }
> +
> +	  if (get32 != NULL && get32 (&hdr.version) == 1)
> +	    {
> +	      unsigned long dirlist = get32 (&hdr.strtab) + get32 (&hdr.dirlist);
> +	      unsigned long dirlistlen = get32 (&hdr.dirlistlen);

... this moved ...

> +	      if (fseek (f, dirlist, SEEK_SET) != -1)
>  		{
>  		  char *b;

... into this scope.

Jan

> -		  b = xmalloc (hdr.dirlistlen + 1);
> -		  if (fread (b, 1, hdr.dirlistlen + 1, f) ==
> -		      hdr.dirlistlen + 1)
> +		  b = xmalloc (dirlistlen + 1);
> +		  if (fread (b, 1, dirlistlen + 1, f) == dirlistlen + 1)
>  		    ld_elf_hints = ldelf_add_sysroot (b);
>  
>  		  free (b);
  
Jan Beulich Oct. 30, 2026, 1:27 p.m. UTC | #2
On 30.10.2026 14:25, Jan Beulich wrote:
> On 30.10.2026 14:05, Piotr Kubaj via Binutils wrote:
>> From: Piotr Kubaj <pkubaj@FreeBSD.org>
>>
>> FreeBSD's ldconfig writes the hints file in little-endian byte order on
>> all architectures unless it is run with -B, so the header is not
>> necessarily in host byte order.
>>
>> This causes an issue when trying to link some software on powerpc64
>> (big-endian POWER).
> 
> Okay, preferably with ...

Oh, also - unless you have a copyright assignment in place with the FSF,
this would need a Signed-off-by of yours.

Jan
  
Piotr Kubaj Oct. 31, 2026, 10:54 a.m. UTC | #3
On 26-10-30 14:27:19, Jan Beulich wrote:
> On 30.10.2026 14:25, Jan Beulich wrote:
> > On 30.10.2026 14:05, Piotr Kubaj via Binutils wrote:
> >> From: Piotr Kubaj <pkubaj@FreeBSD.org>
> >>
> >> FreeBSD's ldconfig writes the hints file in little-endian byte order on
> >> all architectures unless it is run with -B, so the header is not
> >> necessarily in host byte order.
> >>
> >> This causes an issue when trying to link some software on powerpc64
> >> (big-endian POWER).
> > 
> > Okay, preferably with ...
> 
> Oh, also - unless you have a copyright assignment in place with the FSF,
> this would need a Signed-off-by of yours.
> 
> Jan

Added missing Signed-off-by.
From af32152af0a5643bda494fd9fce3c492809d0cbe Mon Sep 17 00:00:00 2001
From: Piotr Kubaj <pkubaj@FreeBSD.org>
Date: Sat, 31 Oct 2026 00:21:37 +0100
Subject: [PATCH] ld: read the FreeBSD ELF hints file in either byte order

FreeBSD's ldconfig writes the hints file in little-endian byte order on
all architectures unless it is run with -B, so the header is not
necessarily in host byte order.

This causes an issue when trying to link some software on powerpc64
(big-endian POWER).

Signed-off-by: Piotr Kubaj <pkubaj@FreeBSD.org>
---
v2: pass the directory offset straight to fseek() and declare dirlistlen
in the innermost scope, as suggested by Jan Beulich.

 ld/ldelf.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/ld/ldelf.c b/ld/ldelf.c
index 00923bd81e6..d3c24543d61 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -780,18 +780,29 @@ ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
       if (f != NULL)
 	{
 	  struct elfhints_hdr hdr;
+	  bfd_vma (*get32) (const void *) = NULL;
 
-	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
-	      && hdr.magic == ELFHINTS_MAGIC
-	      && hdr.version == 1)
+	  /* FreeBSD's ldconfig writes the hints file in little-endian
+	     byte order on all architectures unless it is run with -B,
+	     so the header is not necessarily in host byte order.  */
+	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr))
 	    {
-	      if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
+	      if (bfd_getl32 (&hdr.magic) == ELFHINTS_MAGIC)
+		get32 = bfd_getl32;
+	      else if (bfd_getb32 (&hdr.magic) == ELFHINTS_MAGIC)
+		get32 = bfd_getb32;
+	    }
+
+	  if (get32 != NULL && get32 (&hdr.version) == 1)
+	    {
+	      if (fseek (f, get32 (&hdr.strtab) + get32 (&hdr.dirlist),
+			 SEEK_SET) != -1)
 		{
+		  unsigned long dirlistlen = get32 (&hdr.dirlistlen);
 		  char *b;
 
-		  b = xmalloc (hdr.dirlistlen + 1);
-		  if (fread (b, 1, hdr.dirlistlen + 1, f) ==
-		      hdr.dirlistlen + 1)
+		  b = xmalloc (dirlistlen + 1);
+		  if (fread (b, 1, dirlistlen + 1, f) == dirlistlen + 1)
 		    ld_elf_hints = ldelf_add_sysroot (b);
 
 		  free (b);
  

Patch

diff --git a/ld/ldelf.c b/ld/ldelf.c
index 00923bd81e6..670a41e490b 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -780,18 +780,30 @@  ldelf_check_ld_elf_hints (const struct bfd_link_needed_list *l, int force,
       if (f != NULL)
 	{
 	  struct elfhints_hdr hdr;
+	  bfd_vma (*get32) (const void *) = NULL;
 
-	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr)
-	      && hdr.magic == ELFHINTS_MAGIC
-	      && hdr.version == 1)
+	  /* FreeBSD's ldconfig writes the hints file in little-endian
+	     byte order on all architectures unless it is run with -B,
+	     so the header is not necessarily in host byte order.  */
+	  if (fread (&hdr, 1, sizeof (hdr), f) == sizeof (hdr))
 	    {
-	      if (fseek (f, hdr.strtab + hdr.dirlist, SEEK_SET) != -1)
+	      if (bfd_getl32 (&hdr.magic) == ELFHINTS_MAGIC)
+		get32 = bfd_getl32;
+	      else if (bfd_getb32 (&hdr.magic) == ELFHINTS_MAGIC)
+		get32 = bfd_getb32;
+	    }
+
+	  if (get32 != NULL && get32 (&hdr.version) == 1)
+	    {
+	      unsigned long dirlist = get32 (&hdr.strtab) + get32 (&hdr.dirlist);
+	      unsigned long dirlistlen = get32 (&hdr.dirlistlen);
+
+	      if (fseek (f, dirlist, SEEK_SET) != -1)
 		{
 		  char *b;
 
-		  b = xmalloc (hdr.dirlistlen + 1);
-		  if (fread (b, 1, hdr.dirlistlen + 1, f) ==
-		      hdr.dirlistlen + 1)
+		  b = xmalloc (dirlistlen + 1);
+		  if (fread (b, 1, dirlistlen + 1, f) == dirlistlen + 1)
 		    ld_elf_hints = ldelf_add_sysroot (b);
 
 		  free (b);