bfd: Handle bmmap failure in _bfd_mmap_read_temporary

Message ID 20240403223744.506469-1-hjl.tools@gmail.com
State New
Headers
Series bfd: Handle bmmap failure in _bfd_mmap_read_temporary |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

H.J. Lu April 3, 2024, 10:37 p.m. UTC
  bmmap may return MAP_FAILED.  _bfd_mmap_readonly_temporary returns
MAP_FAILED when called from GDB on an object with opncls_iovec.  Update
_bfd_mmap_read_temporary to handle bmmap failure.

	* libbfd.c (_bfd_mmap_read_temporary): Handle bmmap failure.
---
 bfd/libbfd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
  

Comments

Alan Modra April 4, 2024, 7:15 a.m. UTC | #1
On Wed, Apr 03, 2024 at 03:37:44PM -0700, H.J. Lu wrote:
> bmmap may return MAP_FAILED.  _bfd_mmap_readonly_temporary returns
> MAP_FAILED when called from GDB on an object with opncls_iovec.  Update
> _bfd_mmap_read_temporary to handle bmmap failure.
> 
> 	* libbfd.c (_bfd_mmap_read_temporary): Handle bmmap failure.
> ---
>  bfd/libbfd.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> index 869f0ed5c66..34197b75b5e 100644
> --- a/bfd/libbfd.c
> +++ b/bfd/libbfd.c
> @@ -1207,7 +1207,11 @@ _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
>      {
>        data = _bfd_mmap_readonly_temporary (abfd, size, mmap_base,
>  					   size_p);
> -      if (data == NULL || data == MAP_FAILED)
> +      /* MAP_FAILED is returned when called from GDB on an object with
> +	 opncls_iovec.  Use bfd_read in this case.  */
> +      if (data == MAP_FAILED)
> +	goto mmap_failed;
> +      if (data == NULL)
>  	abort ();
>        *data_p = data;
>        return true;
> @@ -1216,6 +1220,7 @@ _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
>  
>    if (data == NULL)
>      {
> +mmap_failed:

Will this give an unused label warning when !USE_MMAP?  I'm sure you
can rewrite the patch without needing a label if you use another temp
rather than "data" inside the use_mmap block.  That would be a good
idea anyway as it saves anyone wondering whether the !final_link case
can have "data" non-NULL and thus malloc is not needed.

>        data = bfd_malloc (size);
>        if (data == NULL)
>  	return false;
> -- 
> 2.44.0
  
H.J. Lu April 4, 2024, 1:53 p.m. UTC | #2
On Thu, Apr 4, 2024 at 12:15 AM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Apr 03, 2024 at 03:37:44PM -0700, H.J. Lu wrote:
> > bmmap may return MAP_FAILED.  _bfd_mmap_readonly_temporary returns
> > MAP_FAILED when called from GDB on an object with opncls_iovec.  Update
> > _bfd_mmap_read_temporary to handle bmmap failure.
> >
> >       * libbfd.c (_bfd_mmap_read_temporary): Handle bmmap failure.
> > ---
> >  bfd/libbfd.c | 7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> > index 869f0ed5c66..34197b75b5e 100644
> > --- a/bfd/libbfd.c
> > +++ b/bfd/libbfd.c
> > @@ -1207,7 +1207,11 @@ _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
> >      {
> >        data = _bfd_mmap_readonly_temporary (abfd, size, mmap_base,
> >                                          size_p);
> > -      if (data == NULL || data == MAP_FAILED)
> > +      /* MAP_FAILED is returned when called from GDB on an object with
> > +      opncls_iovec.  Use bfd_read in this case.  */
> > +      if (data == MAP_FAILED)
> > +     goto mmap_failed;
> > +      if (data == NULL)
> >       abort ();
> >        *data_p = data;
> >        return true;
> > @@ -1216,6 +1220,7 @@ _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
> >
> >    if (data == NULL)
> >      {
> > +mmap_failed:
>
> Will this give an unused label warning when !USE_MMAP?  I'm sure you
> can rewrite the patch without needing a label if you use another temp
> rather than "data" inside the use_mmap block.  That would be a good
> idea anyway as it saves anyone wondering whether the !final_link case
> can have "data" non-NULL and thus malloc is not needed.
>

The v2 patch:

https://sourceware.org/pipermail/binutils/2024-April/133374.html
  

Patch

diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index 869f0ed5c66..34197b75b5e 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -1207,7 +1207,11 @@  _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
     {
       data = _bfd_mmap_readonly_temporary (abfd, size, mmap_base,
 					   size_p);
-      if (data == NULL || data == MAP_FAILED)
+      /* MAP_FAILED is returned when called from GDB on an object with
+	 opncls_iovec.  Use bfd_read in this case.  */
+      if (data == MAP_FAILED)
+	goto mmap_failed;
+      if (data == NULL)
 	abort ();
       *data_p = data;
       return true;
@@ -1216,6 +1220,7 @@  _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
 
   if (data == NULL)
     {
+mmap_failed:
       data = bfd_malloc (size);
       if (data == NULL)
 	return false;