This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH v2] Misc changes to get it work on MinGW


# Conflicts:
#	configure.ac
---
 .gitignore                       |  1 +
 configure.ac                     | 25 +++++++++++++++++++++++--
 lib/system.h                     | 20 ++++++++++++++++++++
 libelf/elf32_updatefile.c        |  6 +++++-
 libelf/elf_begin.c               | 14 +++++++++++++-
 libelf/elf_end.c                 |  6 +++++-
 libelf/elf_update.c              | 11 +++++++++--
 libelf/{libelf.h => libelf.h.in} |  6 +++---
 libelf/libelfP.h                 |  4 ++++
 9 files changed, 83 insertions(+), 10 deletions(-)
 rename libelf/{libelf.h => libelf.h.in} (99%)

diff --git a/.gitignore b/.gitignore
index c583347..4cda1f9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -44,6 +44,7 @@ Makefile.in
 /libcpu/*_lex.c
 /libcpu/*_parse.[ch]
 /libdw/known-dwarf.h
+/libelf/libelf.h
 /po/*.gmo
 /po/*.pot
 /po/en@boldquot.insert-header
diff --git a/configure.ac b/configure.ac
index 4820d33..47a07f6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -44,7 +44,7 @@ AM_MAINTAINER_MODE
 
 AM_SILENT_RULES([yes])
 
-AC_CONFIG_SRCDIR([libelf/libelf.h])
+AC_CONFIG_SRCDIR([libelf/libelf.h.in])
 AC_CONFIG_FILES([Makefile])
 AC_CONFIG_HEADERS([config.h])
 
@@ -297,10 +297,11 @@ zip_LIBS="$LIBS"
 LIBS="$save_LIBS"
 AC_SUBST([zip_LIBS])
 
-AC_CHECK_DECLS([memrchr, rawmemchr], [], [],
+AC_CHECK_DECLS([fchmod, memrchr, pread, pwrite, rawmemchr], [], [],
 	       [#define _GNU_SOURCE
 		#include <string.h>])
 AC_CHECK_DECLS(powerof2, , , [#include <sys/param.h>])
+AC_FUNC_MMAP
 
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
@@ -474,6 +475,26 @@ if test "$libdw" = yes ; then
 	AC_DEFINE([ENABLE_ARGP])
 fi
 
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+#include <sys/types.h>
+static uid_t x;
+])], uid_t=uid_t, uid_t=int32_t)
+AC_SUBST(UID_T, $uid_t)
+
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+#include <sys/types.h>
+static gid_t x;
+])], gid_t=gid_t, gid_t=int32_t)
+AC_SUBST(GID_T, $gid_t)
+
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+#include <sys/types.h>
+static mode_t x;
+])], mode_t=mode_t, mode_t=int32_t)
+AC_SUBST(MODE_T, $mode_t)
+
+AC_CONFIG_FILES([libelf/libelf.h])
+
 dnl Appended to the config.h file.
 dnl We hide all kinds of configuration magic in lib/eu-config.h.
 AH_BOTTOM([#include <eu-config.h>])
diff --git a/lib/system.h b/lib/system.h
index 25216e6..b963d30 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -91,10 +91,20 @@ pwrite_retry (int fd, const void *buf, size_t len, off_t off)
 {
   ssize_t recvd = 0;
 
+#if !HAVE_DECL_PWRITE
+  off_t lseek_ret = lseek (fd, off, SEEK_SET);
+  if (lseek_ret < 0)
+    return lseek_ret;
+#endif
+
   do
     {
+#if HAVE_DECL_PWRITE
       ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd,
 						off + recvd));
+#else
+      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd));
+#endif
       if (ret <= 0)
 	return ret < 0 ? ret : recvd;
 
@@ -128,10 +138,20 @@ pread_retry (int fd, void *buf, size_t len, off_t off)
 {
   ssize_t recvd = 0;
 
+#if !HAVE_DECL_PREAD
+  off_t lseek_ret = lseek (fd, off, SEEK_SET);
+  if (lseek_ret < 0)
+    return lseek_ret;
+#endif
+
   do
     {
+#if HAVE_DECL_PREAD
       ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd,
 					       off + recvd));
+#else
+      ssize_t ret = TEMP_FAILURE_RETRY (read (fd, buf + recvd, len - recvd));
+#endif
       if (ret <= 0)
 	return ret < 0 ? ret : recvd;
 
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 8dd85d1..6bd6f55 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -38,7 +38,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/mman.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
 
 #include <system.h>
 #include "libelfP.h"
@@ -100,6 +102,7 @@ sort_sections (Elf_Scn **scns, Elf_ScnList *list)
 }
 
 
+#ifdef HAVE_MMAP
 static inline void
 fill_mmap (size_t offset, char *last_position, char *scn_start,
            char *const shdr_start, char *const shdr_end)
@@ -462,6 +465,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 8fdb376..8d3d2c5 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -39,7 +39,9 @@
 #include <stddef.h>
 #include <string.h>
 #include <unistd.h>
-#include <sys/mman.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
 #include <sys/stat.h>
 
 #include <system.h>
@@ -600,6 +602,7 @@ static struct Elf *
 read_file (int fildes, off_t offset, size_t maxsize,
 	   Elf_Cmd cmd, Elf *parent)
 {
+#ifdef HAVE_MMAP
   void *map_address = NULL;
   int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
 		  || cmd == ELF_C_WRITE_MMAP
@@ -663,6 +666,13 @@ read_file (int fildes, off_t offset, size_t maxsize,
 
       return result;
     }
+#else
+  if (cmd == ELF_C_RDWR_MMAP || cmd == ELF_C_WRITE_MMAP)
+    {
+      __libelf_seterrno (ELF_E_INVALID_CMD);
+      return NULL;
+    }
+#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.  */
@@ -1070,12 +1080,14 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
   if (ref != NULL)
     /* Make sure the descriptor is not suddenly going away.  */
     rwlock_rdlock (ref->lock);
+#ifdef F_GETFL
   else if (unlikely (fcntl (fildes, F_GETFL) == -1 && errno == EBADF))
     {
       /* We cannot do anything productive without a file descriptor.  */
       __libelf_seterrno (ELF_E_INVALID_FILE);
       return NULL;
     }
+#endif
 
   switch (cmd)
     {
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 160f0b8..326cbcf 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -34,7 +34,9 @@
 #include <assert.h>
 #include <stddef.h>
 #include <stdlib.h>
-#include <sys/mman.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
 
 #include "libelfP.h"
 
@@ -223,8 +225,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);
+#ifdef HAVE_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 c635eb3..c85a058 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -34,7 +34,9 @@
 #include <libelf.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <sys/mman.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
 #include <sys/stat.h>
 
 #include "libelfP.h"
@@ -67,6 +69,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
       return -1;
     }
 
+#ifdef HAVE_MMAP
   /* Try to map the file if this isn't done yet.  */
   if (elf->map_address == NULL && elf->cmd == ELF_C_WRITE_MMAP)
     {
@@ -107,6 +110,7 @@ write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
 	size = -1;
     }
   else
+#endif
     {
       /* The file is not mmaped.  */
       if ((class == ELFCLASS32
@@ -130,14 +134,17 @@ write_file (Elf *elf, off_t size, int change_bo, size_t shnum)
   /* 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.  */
+#if defined(S_ISUID) && defined(S_ISGID) && HAVE_DECL_FCHMOD
   if (size != -1
       && unlikely (st.st_mode & (S_ISUID | S_ISGID))
       /* fchmod ignores the bits we cannot change.  */
-      && unlikely (fchmod (elf->fildes, st.st_mode) != 0))
+      && unlikely (fchmod (elf->fildes, st.st_mode) != 0)
+     )
     {
       __libelf_seterrno (ELF_E_WRITE_ERROR);
       size = -1;
     }
+#endif
 
   if (size != -1 && elf->parent == NULL)
     elf->maximum_size = size;
diff --git a/libelf/libelf.h b/libelf/libelf.h.in
similarity index 99%
rename from libelf/libelf.h
rename to libelf/libelf.h.in
index c0d6389..066cf61 100644
--- a/libelf/libelf.h
+++ b/libelf/libelf.h.in
@@ -169,9 +169,9 @@ typedef struct
 {
   char *ar_name;		/* Name of archive member.  */
   time_t ar_date;		/* File date.  */
-  uid_t ar_uid;			/* User ID.  */
-  gid_t ar_gid;			/* Group ID.  */
-  mode_t ar_mode;		/* File mode.  */
+  @UID_T@ ar_uid;		/* User ID.  */
+  @GID_T@ ar_gid;		/* Group ID.  */
+  @MODE_T@ ar_mode;		/* File mode.  */
   int64_t ar_size;		/* File size.  */
   char *ar_rawname;		/* Original name of archive member.  */
 } Elf_Arhdr;
diff --git a/libelf/libelfP.h b/libelf/libelfP.h
index 4459982..b90aa42 100644
--- a/libelf/libelfP.h
+++ b/libelf/libelfP.h
@@ -83,7 +83,9 @@
    not part of the library interface, they are only used internally.  */
 enum
 {
+#ifdef HAVE_MMAP
   ELF_F_MMAPPED = 0x40,
+#endif
   ELF_F_MALLOCED = 0x80,
   ELF_F_FILEDATA = 0x100
 };
@@ -501,10 +503,12 @@ extern off_t __elf32_updatenull_wrlock (Elf *elf, int *change_bop,
 extern off_t __elf64_updatenull_wrlock (Elf *elf, int *change_bop,
 					size_t shnum) internal_function;
 
+#ifdef HAVE_MMAP
 extern int __elf32_updatemmap (Elf *elf, int change_bo, size_t shnum)
      internal_function;
 extern int __elf64_updatemmap (Elf *elf, int change_bo, size_t shnum)
      internal_function;
+#endif
 extern int __elf32_updatefile (Elf *elf, int change_bo, size_t shnum)
      internal_function;
 extern int __elf64_updatefile (Elf *elf, int change_bo, size_t shnum)
-- 
2.10.0

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]