[PATCH v2] SHT_GROUP handling: Use _bfd_elf_link_m[un]map_section_contents

Alan Modra amodra@gmail.com
Fri Jun 28 12:44:42 GMT 2024


On Thu, Jun 27, 2024 at 10:27:59PM -0700, H.J. Lu wrote:
> Use _bfd_elf_link_m[un]map_section_content to mmap in SHT_GROUP section.
> When mmap is used, data to link the 3.0GB clang executable in LLVM 19
> debug build on Linux/x86-64 with 32GB RAM is:

I don't believe any of the SHT_GROUP sections will use mmap.
alan@squeak:~/src/llvm-project/build$ find . -name \*.o | xargs readelf -SW | egrep 'GROUP +[0-9a-f]+ [0-9a-f]+ 0{0,3}[^0]'
  [200] .group            GROUP           0000000000000000 0008e4 0001ec 04     4986 3413  4
  [236] .group            GROUP           0000000000000000 000a94 000274 04     5502 2821  4

So my clang debug build only has one object with SHT_GROUP bigger
than 512 bytes, far smaller than the 16k mmap threshold.  I'll only
believe this is worth doing if you can show a group in real code that
is large enough to actually use mmap.

Likely you're using different compilers to me.  I just used the system
compiler and linker on my Ubuntu-24.04 amd box.
g++ (Ubuntu 13.2.0-23ubuntu4) 13.2.0
GNU ld (GNU Binutils for Ubuntu) 2.42

Here's the two biggest file in bin/
-rwxrwxr-x 1 alan alan 4162474976 Jun 28 20:42 clang-19
-rwxrwxr-x 1 alan alan 4256936064 Jun 28 20:55 clang-repl

Please try with something like
  if (ghdr->bfd_section->mmapped_p)
    abort ();
to prove the point.

> 		  malloc	mmap		improvement
> user		  42.15		42.18		-0.07%
> system		  7.32		7.41		-1.2%
> total		  50.22		49.88		0.68%
> context switches  4933		799		83.8%
> major page faults 487		26		94.7%
> 
> 	* elf.c (process_sht_group_entries): Replace
> 	bfd_malloc_and_get_section and free with
> 	_bfd_elf_mmap_section_contents and
> 	_bfd_elf_munmap_section_contents.
> 
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  bfd/elf.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/bfd/elf.c b/bfd/elf.c
> index 8bb296f9637..cefb92d2595 100644
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -607,7 +607,8 @@ process_sht_group_entries (bfd *abfd,
>    unsigned char *contents;
>  
>    /* Read the raw contents.  */
> -  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
> +  if (!_bfd_elf_mmap_section_contents (abfd, ghdr->bfd_section,
> +				       &contents))
>      {
>        _bfd_error_handler
>  	/* xgettext:c-format */
> @@ -663,7 +664,8 @@ process_sht_group_entries (bfd *abfd,
>  		  gname = group_signature (abfd, ghdr);
>  		  if (gname == NULL)
>  		    {
> -		      free (contents);
> +		      _bfd_elf_munmap_section_contents
> +			(ghdr->bfd_section, contents);
>  		      return false;
>  		    }
>  		}
> @@ -693,7 +695,7 @@ process_sht_group_entries (bfd *abfd,
>  	    }
>  	}
>      }
> -  free (contents);
> +  _bfd_elf_munmap_section_contents (ghdr->bfd_section, contents);
>    return true;
>  }
>  
> -- 
> 2.45.2

-- 
Alan Modra


More information about the Binutils mailing list