public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] bfd: Use MAP_FAILED for mmap failure
@ 2024-03-13 13:39 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2024-03-13 13:39 UTC (permalink / raw)
  To: binutils

I will check in this patch today.

H.J.
---
Use MAP_FAILED, instead of ((void *) -1), for mmap failure and use
((void *) -1) only if MAP_FAILED is undefined.

	* bfdio.c (bfd_mmap): Replace (void *) -1 with MAP_FAILED for
	mmap failure.
	* bfdwin.c: Don't include <sys/mman.h>.
	(MAP_FILE): Removed.
	(bfd_get_file_window): Replace (void *) -1 with MAP_FAILED for
	mmap failure.
	* cache.c: Don't include <sys/mman.h>.
	(cache_bmmap): Replace (void *) -1 with MAP_FAILED for mmap
	failure.
	* opncls.c (opncls_bmmap): Likewise.
	* sysdep.h: Include <sys/mman.h> if HAVE_MMAP is define.
	(MAP_FILE): New.  Defined as 0 if undefined.
	(MAP_FAILED): New.  Defined as ((void *) -1) if undefined.
---
 bfd/bfdio.c   |  2 +-
 bfd/bfdwin.c  | 10 +---------
 bfd/cache.c   | 10 +++-------
 bfd/opncls.c  |  2 +-
 bfd/sysdep.h  | 12 ++++++++++++
 bfd/vms-lib.c |  2 +-
 6 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/bfd/bfdio.c b/bfd/bfdio.c
index ab3e28ea204..1572de00ad4 100644
--- a/bfd/bfdio.c
+++ b/bfd/bfdio.c
@@ -666,7 +666,7 @@ bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
   if (abfd->iovec == NULL)
     {
       bfd_set_error (bfd_error_invalid_operation);
-      return (void *) -1;
+      return MAP_FAILED;
     }
 
   return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
diff --git a/bfd/bfdwin.c b/bfd/bfdwin.c
index beb17398744..2919c71b3cb 100644
--- a/bfd/bfdwin.c
+++ b/bfd/bfdwin.c
@@ -31,14 +31,6 @@
 
 #undef HAVE_MPROTECT /* code's not tested yet */
 
-#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
-#include <sys/mman.h>
-#endif
-
-#ifndef MAP_FILE
-#define MAP_FILE 0
-#endif
-
 static int debug_windows;
 
 /* The idea behind the next and refcount fields is that one mapped
@@ -234,7 +226,7 @@ bfd_get_file_window (bfd *abfd,
 		       ? MAP_FILE | MAP_PRIVATE
 		       : MAP_FILE | MAP_SHARED),
 		      fd, file_offset);
-      if (i->data == (void *) -1)
+      if (i->data == MAP_FAILED)
 	{
 	  /* An error happened.  Report it, or try using malloc, or
 	     something.  */
diff --git a/bfd/cache.c b/bfd/cache.c
index 4c00c00f8d5..3bc2afc9f36 100644
--- a/bfd/cache.c
+++ b/bfd/cache.c
@@ -45,10 +45,6 @@ SUBSECTION
 #include "libbfd.h"
 #include "libiberty.h"
 
-#ifdef HAVE_MMAP
-#include <sys/mman.h>
-#endif
-
 static FILE *_bfd_open_file_unlocked (bfd *abfd);
 
 /* In some cases we can optimize cache operation when reopening files.
@@ -489,7 +485,7 @@ cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
 	     void **map_addr ATTRIBUTE_UNUSED,
 	     bfd_size_type *map_len ATTRIBUTE_UNUSED)
 {
-  void *ret = (void *) -1;
+  void *ret = MAP_FAILED;
 
   if (!bfd_lock ())
     return ret;
@@ -518,7 +514,7 @@ cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
       pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
 
       ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
-      if (ret == (void *) -1)
+      if (ret == MAP_FAILED)
 	bfd_set_error (bfd_error_system_call);
       else
 	{
@@ -530,7 +526,7 @@ cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
 #endif
 
   if (!bfd_unlock ())
-    return (void *) -1;
+    return MAP_FAILED;
   return ret;
 }
 
diff --git a/bfd/opncls.c b/bfd/opncls.c
index a0a5c40fba8..096672475c4 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -675,7 +675,7 @@ opncls_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
 	      void **map_addr ATTRIBUTE_UNUSED,
 	      bfd_size_type *map_len ATTRIBUTE_UNUSED)
 {
-  return (void *) -1;
+  return MAP_FAILED;
 }
 
 static const struct bfd_iovec opncls_iovec =
diff --git a/bfd/sysdep.h b/bfd/sysdep.h
index a5766657345..b907bc26a09 100644
--- a/bfd/sysdep.h
+++ b/bfd/sysdep.h
@@ -86,6 +86,18 @@
 #define SEEK_CUR 1
 #endif
 
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#endif
+
+#ifndef MAP_FILE
+#define MAP_FILE 0
+#endif
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *) -1)
+#endif
+
 #include "filenames.h"
 
 #if !HAVE_DECL_FFS
diff --git a/bfd/vms-lib.c b/bfd/vms-lib.c
index b0ca9908b17..592a57fbe68 100644
--- a/bfd/vms-lib.c
+++ b/bfd/vms-lib.c
@@ -1277,7 +1277,7 @@ vms_lib_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
 	       void **map_addr ATTRIBUTE_UNUSED,
 	       bfd_size_type *map_len ATTRIBUTE_UNUSED)
 {
-  return (void *) -1;
+  return MAP_FAILED;
 }
 
 static const struct bfd_iovec vms_lib_iovec = {
-- 
2.44.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-03-13 13:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-13 13:39 [PATCH] bfd: Use MAP_FAILED for mmap failure H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).