[v2] bfd: Handle bmmap failure in _bfd_mmap_read_temporary

Message ID 20240404135227.204529-1-hjl.tools@gmail.com
State New
Headers
Series [v2] 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 4, 2024, 1:52 p.m. UTC
  iovec->bmmap may return MAP_FAILED, which happens in GDB on objects with
iovec == opncls_iovec.  Update _bfd_mmap_read_temporary to handle
iovec->bmmap failure.

	* libbfd.c (_bfd_mmap_read_temporary): Handle iovec->bmmap
	failure.
---
 bfd/libbfd.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
  

Comments

Alan Modra April 4, 2024, 10:24 p.m. UTC | #1
On Thu, Apr 04, 2024 at 06:52:27AM -0700, H.J. Lu wrote:
> iovec->bmmap may return MAP_FAILED, which happens in GDB on objects with
> iovec == opncls_iovec.  Update _bfd_mmap_read_temporary to handle
> iovec->bmmap failure.
> 
> 	* libbfd.c (_bfd_mmap_read_temporary): Handle iovec->bmmap
> 	failure.

OK.

>  bfd/libbfd.c | 18 ++++++++++++------
>  1 file changed, 12 insertions(+), 6 deletions(-)
> 
> diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> index 869f0ed5c66..5126ee207a8 100644
> --- a/bfd/libbfd.c
> +++ b/bfd/libbfd.c
> @@ -1205,12 +1205,18 @@ _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
>  		 && (abfd->flags & BFD_PLUGIN) == 0);
>    if (use_mmmap)
>      {
> -      data = _bfd_mmap_readonly_temporary (abfd, size, mmap_base,
> -					   size_p);
> -      if (data == NULL || data == MAP_FAILED)
> -	abort ();
> -      *data_p = data;
> -      return true;
> +      void *mmaped = _bfd_mmap_readonly_temporary (abfd, size,
> +						   mmap_base,
> +						   size_p);
> +      /* MAP_FAILED is returned when called from GDB on an object with
> +	 opncls_iovec.  Use bfd_read in this case.  */
> +      if (mmaped != MAP_FAILED)
> +	{
> +	  if (mmaped == NULL)
> +	    abort ();
> +	  *data_p = mmaped;
> +	  return true;
> +	}
>      }
>  #endif
>  
> -- 
> 2.44.0
  

Patch

diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index 869f0ed5c66..5126ee207a8 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -1205,12 +1205,18 @@  _bfd_mmap_read_temporary (void **data_p, size_t *size_p,
 		 && (abfd->flags & BFD_PLUGIN) == 0);
   if (use_mmmap)
     {
-      data = _bfd_mmap_readonly_temporary (abfd, size, mmap_base,
-					   size_p);
-      if (data == NULL || data == MAP_FAILED)
-	abort ();
-      *data_p = data;
-      return true;
+      void *mmaped = _bfd_mmap_readonly_temporary (abfd, size,
+						   mmap_base,
+						   size_p);
+      /* MAP_FAILED is returned when called from GDB on an object with
+	 opncls_iovec.  Use bfd_read in this case.  */
+      if (mmaped != MAP_FAILED)
+	{
+	  if (mmaped == NULL)
+	    abort ();
+	  *data_p = mmaped;
+	  return true;
+	}
     }
 #endif