[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 02/12] linux-user: Introduce imgsrc_read, imgsrc_read_alloc
From: |
Richard Henderson |
Subject: |
[PATCH 02/12] linux-user: Introduce imgsrc_read, imgsrc_read_alloc |
Date: |
Fri, 18 Jun 2021 20:43:19 -0700 |
Introduced and initialized, but not yet really used.
These will tidy the current tests vs BPRM_BUF_SIZE.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
linux-user/qemu.h | 50 ++++++++++++++++++++++++++++++++++--------
linux-user/linuxload.c | 46 ++++++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 9 deletions(-)
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 9e5e2aa499..f4cdfb16b3 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -168,6 +168,37 @@ extern unsigned long mmap_min_addr;
/* ??? See if we can avoid exposing so much of the loader internals. */
+typedef struct {
+ const void *cache;
+ unsigned int cache_size;
+ int fd;
+} ImageSource;
+
+/**
+ * imgsrc_read: Read from ImageSource
+ * @dst: destination for read
+ * @offset: offset within file for read
+ * @len: size of the read
+ * @img: ImageSource to read from
+ * @errp: Error details.
+ *
+ * Read into @dst, using the cache when possible.
+ */
+bool imgsrc_read(void *dst, off_t offset, size_t len,
+ const ImageSource *img, Error **errp);
+
+/**
+ * imgsrc_read_alloc: Read from ImageSource
+ * @offset: offset within file for read
+ * @size: size of the read
+ * @img: ImageSource to read from
+ * @errp: Error details.
+ *
+ * Read into newly allocated memory, using the cache when possible.
+ */
+void *imgsrc_read_alloc(off_t offset, size_t len,
+ const ImageSource *img, Error **errp);
+
/* Read a good amount of data initially, to hopefully get all the
program headers loaded. */
#define BPRM_BUF_SIZE 1024
@@ -177,15 +208,16 @@ extern unsigned long mmap_min_addr;
* used when loading binaries.
*/
struct linux_binprm {
- char buf[BPRM_BUF_SIZE] __attribute__((aligned));
- abi_ulong p;
- int fd;
- int e_uid, e_gid;
- int argc, envc;
- char **argv;
- char **envp;
- char * filename; /* Name of binary */
- int (*core_dump)(int, const CPUArchState *); /* coredump routine */
+ char buf[BPRM_BUF_SIZE] __attribute__((aligned));
+ ImageSource src;
+ abi_ulong p;
+ int fd;
+ int e_uid, e_gid;
+ int argc, envc;
+ char **argv;
+ char **envp;
+ char *filename; /* Name of binary */
+ int (*core_dump)(int, const CPUArchState *); /* coredump routine */
};
typedef struct IOCTLEntry IOCTLEntry;
diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c
index 9d4eb5e94b..3b0bafc490 100644
--- a/linux-user/linuxload.c
+++ b/linux-user/linuxload.c
@@ -2,6 +2,7 @@
#include "qemu/osdep.h"
#include "qemu.h"
+#include "qapi/error.h"
#define NGROUPS 32
@@ -74,6 +75,10 @@ static int prepare_binprm(struct linux_binprm *bprm)
/* Make sure the rest of the loader won't read garbage. */
memset(bprm->buf + retval, 0, BPRM_BUF_SIZE - retval);
}
+
+ bprm->src.cache = bprm->buf;
+ bprm->src.cache_size = retval;
+
return retval;
}
@@ -129,6 +134,7 @@ int loader_exec(int fdexec, const char *filename, char
**argv, char **envp,
int retval;
bprm->fd = fdexec;
+ bprm->src.fd = fdexec;
bprm->filename = (char *)filename;
bprm->argc = count(argv);
bprm->argv = argv;
@@ -163,3 +169,43 @@ int loader_exec(int fdexec, const char *filename, char
**argv, char **envp,
return retval;
}
+
+bool imgsrc_read(void *dst, off_t offset, size_t len,
+ const ImageSource *img, Error **errp)
+{
+ ssize_t ret;
+
+ if (offset + len <= img->cache_size) {
+ memcpy(dst, img->cache + offset, len);
+ return true;
+ }
+
+ if (img->fd < 0) {
+ error_setg(errp, "read past end of buffer");
+ return false;
+ }
+
+ ret = pread(img->fd, dst, len, offset);
+ if (ret == len) {
+ return true;
+ }
+ if (ret < 0) {
+ error_setg_errno(errp, errno, "Error reading file header");
+ } else {
+ error_setg(errp, "Incomplete read of file header");
+ }
+ return false;
+}
+
+void *imgsrc_read_alloc(off_t offset, size_t len,
+ const ImageSource *img, Error **errp)
+{
+ void *alloc = g_malloc(len);
+ bool ok = imgsrc_read(alloc, offset, len, img, errp);
+
+ if (!ok) {
+ g_free(alloc);
+ alloc = NULL;
+ }
+ return alloc;
+}
--
2.25.1
- [PATCH 00/12] linux-user: Load a vdso for x86_64 and hppa, Richard Henderson, 2021/06/18
- [PATCH 01/12] linux-user: Fix style problems in linuxload.c, Richard Henderson, 2021/06/18
- [PATCH 02/12] linux-user: Introduce imgsrc_read, imgsrc_read_alloc,
Richard Henderson <=
- [PATCH 03/12] linux-user: Tidy loader_exec, Richard Henderson, 2021/06/18
- [PATCH 04/12] linux-user: Do not clobber bprm_buf swapping ehdr, Richard Henderson, 2021/06/18
- [PATCH 06/12] linux-user: Use ImageSource in load_symbols, Richard Henderson, 2021/06/18
- [PATCH 05/12] linux-user: Use ImageSource in load_elf_image, Richard Henderson, 2021/06/18
- [PATCH 12/12] linux-user/hppa: Add vdso and use it for rt_sigreturn, Richard Henderson, 2021/06/18
- [PATCH 07/12] linux-user: Replace bprm->fd with bprm->src.fd, Richard Henderson, 2021/06/18
- [PATCH 09/12] linux-user: Load vdso image if available, Richard Henderson, 2021/06/18
- [PATCH 11/12] linux-user/x86_64: Add vdso, Richard Henderson, 2021/06/18