From: Yonggang Luo <luoyonggang@gmail.com>
To: elfutils-devel@sourceware.org
Cc: Yonggang Luo <luoyonggang@gmail.com>
Subject: [PATCH v2 03/16] Use configure to detect HAVE_DECL_MMAP and use it for system doesn't provide sys/mman.h
Date: Sun, 18 Dec 2022 00:52:00 +0800 [thread overview]
Message-ID: <20221217165213.152-4-luoyonggang@gmail.com> (raw)
In-Reply-To: <20221217165213.152-1-luoyonggang@gmail.com>
Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
configure.ac | 1 +
lib/crc32_file.c | 4 ++--
lib/system.h | 2 ++
libelf/elf32_updatefile.c | 3 ++-
libelf/elf_begin.c | 5 ++++-
libelf/elf_end.c | 2 ++
libelf/elf_update.c | 5 ++++-
7 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/configure.ac b/configure.ac
index 59be27ac..b84623fe 100644
--- a/configure.ac
+++ b/configure.ac
@@ -428,6 +428,7 @@ AC_CHECK_DECLS([memrchr, rawmemchr],[],[],
[#define _GNU_SOURCE
#include <string.h>])
AC_CHECK_DECLS([powerof2],[],[],[#include <sys/param.h>])
+AC_CHECK_DECLS([mmap],[],[],[#include <sys/mman.h>])
AC_CHECK_DECLS([mempcpy],[],[],
[#define _GNU_SOURCE
#include <string.h>])
diff --git a/lib/crc32_file.c b/lib/crc32_file.c
index f7607d0b..66833702 100644
--- a/lib/crc32_file.c
+++ b/lib/crc32_file.c
@@ -32,9 +32,7 @@
#include "libeu.h"
#include <errno.h>
-#include <unistd.h>
#include <sys/stat.h>
-#include <sys/mman.h>
#include "system.h"
int
@@ -45,6 +43,7 @@ crc32_file (int fd, uint32_t *resp)
off_t off = 0;
ssize_t count;
+#if HAVE_DECL_MMAP
struct stat st;
if (fstat (fd, &st) == 0)
{
@@ -78,6 +77,7 @@ crc32_file (int fd, uint32_t *resp)
munmap (mapped, mapsize);
}
}
+#endif
while ((count = TEMP_FAILURE_RETRY (pread (fd, buffer, sizeof buffer,
off))) > 0)
diff --git a/lib/system.h b/lib/system.h
index bbbe06c4..561d3e03 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -42,7 +42,9 @@
/* System dependend headers */
#include <byteswap.h>
#include <endian.h>
+#if HAVE_DECL_MMAP
#include <sys/mman.h>
+#endif
#include <sys/param.h>
#include <unistd.h>
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 46afa1f4..8229fd26 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -96,7 +96,7 @@ sort_sections (Elf_Scn **scns, Elf_ScnList *list)
qsort (scns, scnp - scns, sizeof (*scns), compare_sections);
}
-
+#if HAVE_DECL_MMAP
static inline void
fill_mmap (size_t offset, char *last_position, char *scn_start,
char *const shdr_start, char *const shdr_end)
@@ -482,6 +482,7 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
return 0;
}
+#endif
/* Size of the buffer we use to generate the blocks of fill bytes. */
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index fe8c640a..6d31882e 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -645,10 +645,12 @@ static struct Elf *
read_file (int fildes, int64_t offset, size_t maxsize,
Elf_Cmd cmd, Elf *parent)
{
+#if HAVE_DECL_MMAP
void *map_address = NULL;
int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
|| cmd == ELF_C_WRITE_MMAP
|| cmd == ELF_C_READ_MMAP_PRIVATE);
+#endif
if (parent == NULL)
{
@@ -669,7 +671,7 @@ read_file (int fildes, int64_t offset, size_t maxsize,
/* The parent is already loaded. Use it. */
assert (maxsize != ~((size_t) 0));
}
-
+#if HAVE_DECL_MMAP
if (use_mmap)
{
if (parent == NULL)
@@ -713,6 +715,7 @@ read_file (int fildes, int64_t offset, size_t maxsize,
return result;
}
+#endif
/* Otherwise we have to do it the hard way. We read as much as necessary
from the file whenever we need information which is not available. */
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 5c451f36..8023b216 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -222,8 +222,10 @@ elf_end (Elf *elf)
/* The file was read or mapped for this descriptor. */
if ((elf->flags & ELF_F_MALLOCED) != 0)
free (elf->map_address);
+#if HAVE_DECL_MMAP
else if ((elf->flags & ELF_F_MMAPPED) != 0)
munmap (elf->map_address, elf->maximum_size);
+#endif
}
rwlock_unlock (elf->lock);
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 56af3a1c..e81eb6c9 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -64,7 +64,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t shnum)
__libelf_seterrno (ELF_E_WRITE_ERROR);
return -1;
}
-
+#if HAVE_DECL_MMAP
/* Try to map the file if this isn't done yet. */
if (elf->map_address == NULL && elf->cmd == ELF_C_WRITE_MMAP)
{
@@ -125,6 +125,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t shnum)
size = -1;
}
else
+#endif
{
/* The file is not mmaped. */
if ((class == ELFCLASS32
@@ -145,6 +146,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t shnum)
size = -1;
}
+#if HAVE_DECL_MMAP
/* POSIX says that ftruncate and write may clear the S_ISUID and S_ISGID
mode bits. So make sure we restore them afterwards if they were set.
This is not atomic if someone else chmod's the file while we operate. */
@@ -156,6 +158,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t shnum)
__libelf_seterrno (ELF_E_WRITE_ERROR);
size = -1;
}
+#endif
if (size != -1 && elf->parent == NULL)
elf->maximum_size = size;
--
2.36.1.windows.1
next prev parent reply other threads:[~2022-12-17 16:52 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-17 16:51 [PATCH v2 00/16] Patches for building with mingw/gcc msvc/clang-cl Yonggang Luo
2022-12-17 16:51 ` [PATCH v2 01/16] ignore build directory Yonggang Luo
2022-12-17 16:51 ` [PATCH v2 02/16] move platform depended include into system.h of libebl Yonggang Luo
2023-02-23 10:44 ` Mark Wielaard
2022-12-17 16:52 ` Yonggang Luo [this message]
2023-02-23 10:52 ` [PATCH v2 03/16] Use configure to detect HAVE_DECL_MMAP and use it for system doesn't provide sys/mman.h Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 04/16] Fixes usage of basename about prototype differences Yonggang Luo
2023-02-23 11:00 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 05/16] libcpu: Remove the need of NMNES by using enum Yonggang Luo
2022-12-21 17:56 ` Mark Wielaard
2023-02-23 11:50 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 06/16] libcpu: Use __asm instead asm that can be recognized by both clang-cl and gcc Yonggang Luo
2022-12-21 18:07 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 07/16] libdw: Fixes compile of dwarf_whatattr.c and dwarf_whatform.c Yonggang Luo
2023-02-23 12:25 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 08/16] lib: Implement error properly even when not HAVE_ERR_H Yonggang Luo
2023-02-23 12:31 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 09/16] libelf: uid_t, gid_t and mode_t are not comes with msvcrt, so using long/unsigned long instead on win32 Yonggang Luo
2023-03-02 13:24 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 10/16] libasm: stdio_ext.h are not present " Yonggang Luo
2023-02-23 12:35 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 11/16] libebl/libdwelf: define ssize_t and pid_t for MSVC within installed header libdwelf.h and libebl.h Yonggang Luo
2023-03-02 13:32 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 12/16] libasm/debuginfod: fchmod doesn't present on win32 Yonggang Luo
2022-12-21 18:16 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 13/16] lib: isatty is not available on windows Yonggang Luo
2022-12-19 12:32 ` 罗勇刚(Yonggang Luo)
2022-12-17 16:52 ` [PATCH v2 14/16] Add function sys_get_page_size to replace platform dependent sysconf (_SC_PAGESIZE) Yonggang Luo
2023-03-02 16:33 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 15/16] libelf: F_GETFD may not predefined with msvc/mingw, guard the usage of it Yonggang Luo
2022-12-21 22:29 ` Mark Wielaard
2022-12-17 16:52 ` [PATCH v2 16/16] lib: Use HAVE_LIBINTL_H to guard #include <libintl.h> Yonggang Luo
2022-12-21 23:07 ` Mark Wielaard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221217165213.152-4-luoyonggang@gmail.com \
--to=luoyonggang@gmail.com \
--cc=elfutils-devel@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).