Hi,
See also the discussion on v1 of this patch:
https://inbox.sourceware.org/elfutils-devel/f352dba78caee57afc17995f8e36d28ae98afbc0.camel@klomp.org/#r
I think we need to think about what the libelf API looks like without
mmap. In particular I think we need to decide what to do with the
Elf_Cmds:
ELF_C_READ_MMAP, /* Read, but mmap the file if possible. */
ELF_C_RDWR_MMAP, /* Read and write, with mmap. */
ELF_C_WRITE_MMAP, /* Write, with mmap. */
ELF_C_READ_MMAP_PRIVATE, /* Read, but memory is writable, results are
not written to the file. */
Specifically because libdw does reply on some of them.
Thanks,
Mark
@@ -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>])
@@ -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)
@@ -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>
@@ -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. */
@@ -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. */
@@ -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);
@@ -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;