From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1039) id 0A19E3846062; Wed, 3 Apr 2024 16:17:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A19E3846062 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1712161077; bh=ub3JlEMEucJrpYxaddN/85zJOlFxh3v70tSaZ9n5dCY=; h=From:To:Subject:Date:From; b=SS4OQ9gX4g0mmgOGh2vYcMhRdUCJ+urIlISB5cADPwG6bbziqqCL0NWhwFTS7B56m AhsOlSU6Yok+v/pVucX1OBiSrrozbRuhUOvdFVLckidDC/BFX3vESYywfeSXVPv8de viLAUHrX1iX25CPw7MwstnNcy/UsaNP3OL08mDjk= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: H.J. Lu To: binutils-cvs@sourceware.org Subject: [binutils-gdb] elf: Add _bfd_elf_link_m[un]map_section_contents X-Act-Checkin: binutils-gdb X-Git-Author: H.J. Lu X-Git-Refname: refs/heads/master X-Git-Oldrev: 3428c771aef0cebe229ac0f02a654d2ef78f2427 X-Git-Newrev: a9505c74206cfb797d3baac526b19ba7055675dc Message-Id: <20240403161757.0A19E3846062@sourceware.org> Date: Wed, 3 Apr 2024 16:17:56 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Da9505c74206c= fb797d3baac526b19ba7055675dc commit a9505c74206cfb797d3baac526b19ba7055675dc Author: H.J. Lu Date: Mon Mar 11 09:51:06 2024 -0700 elf: Add _bfd_elf_link_m[un]map_section_contents =20 To copy input section contents, add _bfd_elf_link_mmap_section_contents and _bfd_elf_link_munmap_section_contents to mmap in the input sections. =20 * elf-bfd.h (_bfd_elf_link_mmap_section_contents): New. (_bfd_elf_link_munmap_section_contents): Likewise. * elf.c (elf_mmap_section_contents): New. (_bfd_elf_mmap_section_contents): Use it. (_bfd_elf_link_mmap_section_contents): New. (_bfd_elf_link_munmap_section_contents): Likewise. * elflink.c (elf_link_input_bfd): Call _bfd_elf_link_mmap_section_contents instead of bfd_get_full_section_contents. Call _bfd_elf_link_munmap_section_contents to munmap the section contents. (bfd_elf_final_link): When mmap is used, initialize max_contents_size to _bfd_minimum_mmap_size and increase it for compressed or linker created sections or sections whose rawsize !=3D size. Diff: --- bfd/elf-bfd.h | 4 ++++ bfd/elf.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++--= -- bfd/elflink.c | 33 +++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 12 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index 01e0da135b9..f31244f2227 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -3146,6 +3146,10 @@ extern bool _bfd_elf_mmap_section_contents (bfd *abfd, asection *section, bfd_byte **buf); extern void _bfd_elf_munmap_section_contents (asection *, void *); +extern bool _bfd_elf_link_mmap_section_contents + (bfd *abfd, asection *section, bfd_byte **buf); +extern void _bfd_elf_link_munmap_section_contents + (asection *); =20 /* Large common section. */ extern asection _bfd_elf_large_com_section; diff --git a/bfd/elf.c b/bfd/elf.c index 868abeccddb..9b98d3b8b2b 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -14366,10 +14366,12 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd= , asection *sec) return result; } =20 -/* Mmap in section contents. */ +/* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL + before calling bfd_get_full_section_contents. */ =20 -bool -_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) +static bool +elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf, + bool final_link) { #ifdef USE_MMAP const struct elf_backend_data *bed =3D get_elf_backend_data (abfd); @@ -14393,16 +14395,41 @@ _bfd_elf_mmap_section_contents (bfd *abfd, sec_pt= r sec, bfd_byte **buf) if (sec->mmapped_p) abort (); sec->mmapped_p =3D 1; + + /* Never use the preallocated buffer if mmapp is used. */ + *buf =3D NULL; } } #endif - *buf =3D NULL; + /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is + true. If FINAL_LINK is false, *BUF is set to the preallocated + buffer if USE_MMAP is undefined and *BUF is set to NULL if + USE_MMAP is defined. */ + if (!final_link) + *buf =3D NULL; bool ret =3D bfd_get_full_section_contents (abfd, sec, buf); if (ret && sec->mmapped_p) *buf =3D sec->contents; return ret; } =20 +/* Mmap in section contents. */ + +bool +_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) +{ + return elf_mmap_section_contents (abfd, sec, buf, false); +} + +/* Mmap in the full section contents for the final link. */ + +bool +_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, + bfd_byte **buf) +{ + return elf_mmap_section_contents (abfd, sec, buf, true); +} + /* Munmap section contents. */ =20 void @@ -14442,3 +14469,25 @@ _bfd_elf_munmap_section_contents (asection *sec AT= TRIBUTE_UNUSED, =20 free (contents); } + +/* Munmap the full section contents for the final link. */ + +void +_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) +{ +#ifdef USE_MMAP + if (sec->mmapped_p && elf_section_data (sec)->contents_addr !=3D NULL) + { + /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as + malloced, CONTENTS_ADDR is set to NULL. */ + /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ + if (munmap (elf_section_data (sec)->contents_addr, + elf_section_data (sec)->contents_size) !=3D 0) + abort (); + sec->mmapped_p =3D 0; + sec->contents =3D NULL; + elf_section_data (sec)->contents_addr =3D NULL; + elf_section_data (sec)->contents_size =3D 0; + } +#endif +} diff --git a/bfd/elflink.c b/bfd/elflink.c index a7b50bfcaf9..2a05299b24d 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -11486,7 +11486,8 @@ elf_link_input_bfd (struct elf_final_link_info *fli= nfo, bfd *input_bfd) else { contents =3D flinfo->contents; - if (! bfd_get_full_section_contents (input_bfd, o, &contents)) + if (! _bfd_elf_link_mmap_section_contents (input_bfd, o, + &contents)) return false; } =20 @@ -12047,6 +12048,9 @@ elf_link_input_bfd (struct elf_final_link_info *fli= nfo, bfd *input_bfd) } break; } + + /* Munmap the section contents for each input section. */ + _bfd_elf_link_munmap_section_contents (o); } =20 return true; @@ -12485,13 +12489,17 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_in= fo *info) /* Count up the number of relocations we will output for each output section, so that we know the sizes of the reloc sections. We also figure out some maximum sizes. */ - max_contents_size =3D 0; #ifdef USE_MMAP /* Mmap is used only if section size >=3D the minimum mmap section - size. max_external_reloc_size covers all relocation sections - smaller than the minimum mmap section size. */ + size. The initial max_contents_size value covers all sections + smaller than the minimum mmap section size. It may be increased + for compressed or linker created sections or sections whose + rawsize !=3D size. max_external_reloc_size covers all relocation + sections smaller than the minimum mmap section size. */ + max_contents_size =3D _bfd_minimum_mmap_size; max_external_reloc_size =3D _bfd_minimum_mmap_size; #else + max_contents_size =3D 0; max_external_reloc_size =3D 0; #endif max_internal_reloc_count =3D 0; @@ -12527,10 +12535,19 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_in= fo *info) if (sec->flags & SEC_MERGE) merged =3D true; =20 - if (sec->rawsize > max_contents_size) - max_contents_size =3D sec->rawsize; - if (sec->size > max_contents_size) - max_contents_size =3D sec->size; +#ifdef USE_MMAP + /* Mmap is used only on non-compressed, non-linker created + sections whose rawsize =3D=3D size. */ + if (sec->compress_status !=3D COMPRESS_SECTION_NONE + || (sec->flags & SEC_LINKER_CREATED) !=3D 0 + || sec->rawsize !=3D sec->size) +#endif + { + if (sec->rawsize > max_contents_size) + max_contents_size =3D sec->rawsize; + if (sec->size > max_contents_size) + max_contents_size =3D sec->size; + } =20 if (bfd_get_flavour (sec->owner) =3D=3D bfd_target_elf_flavour && (sec->owner->flags & DYNAMIC) =3D=3D 0)