|
|
f96e0b |
From b12fd30b36756327edca502b8af4e8eee264d564 Mon Sep 17 00:00:00 2001
|
|
|
f96e0b |
From: Colin Watson <cjwatson@ubuntu.com>
|
|
|
f96e0b |
Date: Sun, 20 Jan 2013 15:54:09 +0000
|
|
|
f96e0b |
Subject: [PATCH 117/482] Remove nested functions from ELF iterators.
|
|
|
f96e0b |
|
|
|
f96e0b |
---
|
|
|
f96e0b |
ChangeLog | 4 +
|
|
|
f96e0b |
grub-core/kern/elf.c | 373 ++++++++++++++++++++++++--------------------
|
|
|
f96e0b |
grub-core/loader/i386/bsd.c | 4 +-
|
|
|
f96e0b |
include/grub/elfload.h | 11 +-
|
|
|
f96e0b |
4 files changed, 219 insertions(+), 173 deletions(-)
|
|
|
f96e0b |
|
|
|
f96e0b |
diff --git a/ChangeLog b/ChangeLog
|
|
|
f96e0b |
index 733b212..3ac8171 100644
|
|
|
f96e0b |
--- a/ChangeLog
|
|
|
f96e0b |
+++ b/ChangeLog
|
|
|
f96e0b |
@@ -1,5 +1,9 @@
|
|
|
f96e0b |
2013-01-20 Colin Watson <cjwatson@ubuntu.com>
|
|
|
f96e0b |
|
|
|
f96e0b |
+ Remove nested functions from ELF iterators.
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+2013-01-20 Colin Watson <cjwatson@ubuntu.com>
|
|
|
f96e0b |
+
|
|
|
f96e0b |
Remove nested functions from device iterators.
|
|
|
f96e0b |
|
|
|
f96e0b |
* include/grub/arc/arc.h (grub_arc_iterate_devs_hook_t): New type.
|
|
|
f96e0b |
diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
|
|
|
f96e0b |
index 682cfbd..f52ca21 100644
|
|
|
f96e0b |
--- a/grub-core/kern/elf.c
|
|
|
f96e0b |
+++ b/grub-core/kern/elf.c
|
|
|
f96e0b |
@@ -149,8 +149,7 @@ grub_elf32_load_phdrs (grub_elf_t elf, const char *filename)
|
|
|
f96e0b |
grub_err_t
|
|
|
f96e0b |
grub_elf32_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
const char *filename,
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf32_Phdr *, void *),
|
|
|
f96e0b |
- void *hook_arg)
|
|
|
f96e0b |
+ grub_elf32_phdr_iterate_hook_t hook, void *hook_arg)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
Elf32_Phdr *phdrs;
|
|
|
f96e0b |
unsigned int i;
|
|
|
f96e0b |
@@ -177,48 +176,58 @@ grub_elf32_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
return grub_errno;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
+struct grub_elf32_size_ctx
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ Elf32_Addr segments_start, segments_end;
|
|
|
f96e0b |
+ int nr_phdrs;
|
|
|
f96e0b |
+ grub_uint32_t curr_align;
|
|
|
f96e0b |
+};
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+/* Run through the program headers to calculate the total memory size we
|
|
|
f96e0b |
+ * should claim. */
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
+grub_elf32_calcsize (grub_elf_t _elf __attribute__ ((unused)),
|
|
|
f96e0b |
+ Elf32_Phdr *phdr, void *data)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf32_size_ctx *ctx = data;
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+ /* Only consider loadable segments. */
|
|
|
f96e0b |
+ if (phdr->p_type != PT_LOAD)
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+ ctx->nr_phdrs++;
|
|
|
f96e0b |
+ if (phdr->p_paddr < ctx->segments_start)
|
|
|
f96e0b |
+ ctx->segments_start = phdr->p_paddr;
|
|
|
f96e0b |
+ if (phdr->p_paddr + phdr->p_memsz > ctx->segments_end)
|
|
|
f96e0b |
+ ctx->segments_end = phdr->p_paddr + phdr->p_memsz;
|
|
|
f96e0b |
+ if (ctx->curr_align < phdr->p_align)
|
|
|
f96e0b |
+ ctx->curr_align = phdr->p_align;
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+}
|
|
|
f96e0b |
+
|
|
|
f96e0b |
/* Calculate the amount of memory spanned by the segments. */
|
|
|
f96e0b |
grub_size_t
|
|
|
f96e0b |
grub_elf32_size (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
Elf32_Addr *base, grub_uint32_t *max_align)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
- Elf32_Addr segments_start = (Elf32_Addr) -1;
|
|
|
f96e0b |
- Elf32_Addr segments_end = 0;
|
|
|
f96e0b |
- int nr_phdrs = 0;
|
|
|
f96e0b |
- grub_uint32_t curr_align = 1;
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- /* Run through the program headers to calculate the total memory size we
|
|
|
f96e0b |
- * should claim. */
|
|
|
f96e0b |
- auto int NESTED_FUNC_ATTR calcsize (grub_elf_t _elf, Elf32_Phdr *phdr, void *_arg);
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR calcsize (grub_elf_t _elf __attribute__ ((unused)),
|
|
|
f96e0b |
- Elf32_Phdr *phdr,
|
|
|
f96e0b |
- void *_arg __attribute__ ((unused)))
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- /* Only consider loadable segments. */
|
|
|
f96e0b |
- if (phdr->p_type != PT_LOAD)
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- nr_phdrs++;
|
|
|
f96e0b |
- if (phdr->p_paddr < segments_start)
|
|
|
f96e0b |
- segments_start = phdr->p_paddr;
|
|
|
f96e0b |
- if (phdr->p_paddr + phdr->p_memsz > segments_end)
|
|
|
f96e0b |
- segments_end = phdr->p_paddr + phdr->p_memsz;
|
|
|
f96e0b |
- if (curr_align < phdr->p_align)
|
|
|
f96e0b |
- curr_align = phdr->p_align;
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
+ struct grub_elf32_size_ctx ctx = {
|
|
|
f96e0b |
+ .segments_start = (Elf32_Addr) -1,
|
|
|
f96e0b |
+ .segments_end = 0,
|
|
|
f96e0b |
+ .nr_phdrs = 0,
|
|
|
f96e0b |
+ .curr_align = 1
|
|
|
f96e0b |
+ };
|
|
|
f96e0b |
|
|
|
f96e0b |
- grub_elf32_phdr_iterate (elf, filename, calcsize, 0);
|
|
|
f96e0b |
+ grub_elf32_phdr_iterate (elf, filename, grub_elf32_calcsize, &ctx;;
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
*base = 0;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (nr_phdrs == 0)
|
|
|
f96e0b |
+ if (ctx.nr_phdrs == 0)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
grub_error (GRUB_ERR_BAD_OS, "no program headers present");
|
|
|
f96e0b |
return 0;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (segments_end < segments_start)
|
|
|
f96e0b |
+ if (ctx.segments_end < ctx.segments_start)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
/* Very bad addresses. */
|
|
|
f96e0b |
grub_error (GRUB_ERR_BAD_OS, "bad program header load addresses");
|
|
|
f96e0b |
@@ -226,76 +235,87 @@ grub_elf32_size (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
- *base = segments_start;
|
|
|
f96e0b |
+ *base = ctx.segments_start;
|
|
|
f96e0b |
if (max_align)
|
|
|
f96e0b |
- *max_align = curr_align;
|
|
|
f96e0b |
- return segments_end - segments_start;
|
|
|
f96e0b |
+ *max_align = ctx.curr_align;
|
|
|
f96e0b |
+ return ctx.segments_end - ctx.segments_start;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
-/* Load every loadable segment into memory specified by `_load_hook'. */
|
|
|
f96e0b |
-grub_err_t
|
|
|
f96e0b |
-grub_elf32_load (grub_elf_t _elf, const char *filename,
|
|
|
f96e0b |
- grub_elf32_load_hook_t _load_hook,
|
|
|
f96e0b |
- grub_addr_t *base, grub_size_t *size)
|
|
|
f96e0b |
+struct grub_elf32_load_ctx
|
|
|
f96e0b |
{
|
|
|
f96e0b |
- grub_addr_t load_base = (grub_addr_t) -1ULL;
|
|
|
f96e0b |
- grub_size_t load_size = 0;
|
|
|
f96e0b |
- grub_err_t err;
|
|
|
f96e0b |
+ const char *filename;
|
|
|
f96e0b |
+ grub_elf32_load_hook_t load_hook;
|
|
|
f96e0b |
+ grub_addr_t load_base;
|
|
|
f96e0b |
+ grub_size_t load_size;
|
|
|
f96e0b |
+};
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
+grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *data)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf32_load_ctx *ctx = data;
|
|
|
f96e0b |
+ grub_addr_t load_addr;
|
|
|
f96e0b |
+ int do_load = 1;
|
|
|
f96e0b |
|
|
|
f96e0b |
- auto int NESTED_FUNC_ATTR grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *hook);
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR grub_elf32_load_segment (grub_elf_t elf, Elf32_Phdr *phdr, void *hook)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- grub_elf32_load_hook_t load_hook = (grub_elf32_load_hook_t) hook;
|
|
|
f96e0b |
- grub_addr_t load_addr;
|
|
|
f96e0b |
- int do_load = 1;
|
|
|
f96e0b |
+ load_addr = phdr->p_paddr;
|
|
|
f96e0b |
+ if (ctx->load_hook && ctx->load_hook (phdr, &load_addr, &do_load))
|
|
|
f96e0b |
+ return 1;
|
|
|
f96e0b |
|
|
|
f96e0b |
- load_addr = phdr->p_paddr;
|
|
|
f96e0b |
- if (load_hook && load_hook (phdr, &load_addr, &do_load))
|
|
|
f96e0b |
- return 1;
|
|
|
f96e0b |
+ if (! do_load)
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (! do_load)
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
+ if (load_addr < ctx->load_base)
|
|
|
f96e0b |
+ ctx->load_base = load_addr;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (load_addr < load_base)
|
|
|
f96e0b |
- load_base = load_addr;
|
|
|
f96e0b |
+ grub_dprintf ("elf", "Loading segment at 0x%llx, size 0x%llx\n",
|
|
|
f96e0b |
+ (unsigned long long) load_addr,
|
|
|
f96e0b |
+ (unsigned long long) phdr->p_memsz);
|
|
|
f96e0b |
|
|
|
f96e0b |
- grub_dprintf ("elf", "Loading segment at 0x%llx, size 0x%llx\n",
|
|
|
f96e0b |
- (unsigned long long) load_addr,
|
|
|
f96e0b |
- (unsigned long long) phdr->p_memsz);
|
|
|
f96e0b |
+ if (grub_file_seek (elf->file, phdr->p_offset) == (grub_off_t) -1)
|
|
|
f96e0b |
+ return grub_errno;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (grub_file_seek (elf->file, phdr->p_offset) == (grub_off_t) -1)
|
|
|
f96e0b |
- return grub_errno;
|
|
|
f96e0b |
+ if (phdr->p_filesz)
|
|
|
f96e0b |
+ {
|
|
|
f96e0b |
+ grub_ssize_t read;
|
|
|
f96e0b |
+ read = grub_file_read (elf->file, (void *) load_addr, phdr->p_filesz);
|
|
|
f96e0b |
+ if (read != (grub_ssize_t) phdr->p_filesz)
|
|
|
f96e0b |
+ {
|
|
|
f96e0b |
+ /* XXX How can we free memory from `ctx->load_hook'? */
|
|
|
f96e0b |
+ if (!grub_errno)
|
|
|
f96e0b |
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
f96e0b |
+ ctx->filename);
|
|
|
f96e0b |
+ return grub_errno;
|
|
|
f96e0b |
+ }
|
|
|
f96e0b |
+ }
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (phdr->p_filesz)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- grub_ssize_t read;
|
|
|
f96e0b |
- read = grub_file_read (elf->file, (void *) load_addr, phdr->p_filesz);
|
|
|
f96e0b |
- if (read != (grub_ssize_t) phdr->p_filesz)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- /* XXX How can we free memory from `load_hook'? */
|
|
|
f96e0b |
- if (!grub_errno)
|
|
|
f96e0b |
- grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
f96e0b |
- filename);
|
|
|
f96e0b |
- return grub_errno;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- if (phdr->p_filesz < phdr->p_memsz)
|
|
|
f96e0b |
- grub_memset ((void *) (long) (load_addr + phdr->p_filesz),
|
|
|
f96e0b |
- 0, phdr->p_memsz - phdr->p_filesz);
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- load_size += phdr->p_memsz;
|
|
|
f96e0b |
+ if (phdr->p_filesz < phdr->p_memsz)
|
|
|
f96e0b |
+ grub_memset ((void *) (long) (load_addr + phdr->p_filesz),
|
|
|
f96e0b |
+ 0, phdr->p_memsz - phdr->p_filesz);
|
|
|
f96e0b |
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
+ ctx->load_size += phdr->p_memsz;
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+}
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+/* Load every loadable segment into memory specified by `_load_hook'. */
|
|
|
f96e0b |
+grub_err_t
|
|
|
f96e0b |
+grub_elf32_load (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
+ grub_elf32_load_hook_t load_hook,
|
|
|
f96e0b |
+ grub_addr_t *base, grub_size_t *size)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf32_load_ctx ctx = {
|
|
|
f96e0b |
+ .filename = filename,
|
|
|
f96e0b |
+ .load_hook = load_hook,
|
|
|
f96e0b |
+ .load_base = (grub_addr_t) -1ULL,
|
|
|
f96e0b |
+ .load_size = 0
|
|
|
f96e0b |
+ };
|
|
|
f96e0b |
+ grub_err_t err;
|
|
|
f96e0b |
|
|
|
f96e0b |
- err = grub_elf32_phdr_iterate (_elf, filename,
|
|
|
f96e0b |
- grub_elf32_load_segment, _load_hook);
|
|
|
f96e0b |
+ err = grub_elf32_phdr_iterate (elf, filename, grub_elf32_load_segment, &ctx;;
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
- *base = load_base;
|
|
|
f96e0b |
+ *base = ctx.load_base;
|
|
|
f96e0b |
if (size)
|
|
|
f96e0b |
- *size = load_size;
|
|
|
f96e0b |
+ *size = ctx.load_size;
|
|
|
f96e0b |
|
|
|
f96e0b |
return err;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
@@ -339,8 +359,7 @@ grub_elf64_load_phdrs (grub_elf_t elf, const char *filename)
|
|
|
f96e0b |
grub_err_t
|
|
|
f96e0b |
grub_elf64_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
const char *filename,
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf64_Phdr *, void *),
|
|
|
f96e0b |
- void *hook_arg)
|
|
|
f96e0b |
+ grub_elf64_phdr_iterate_hook_t hook, void *hook_arg)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
Elf64_Phdr *phdrs;
|
|
|
f96e0b |
unsigned int i;
|
|
|
f96e0b |
@@ -367,48 +386,58 @@ grub_elf64_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
return grub_errno;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
+struct grub_elf64_size_ctx
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ Elf64_Addr segments_start, segments_end;
|
|
|
f96e0b |
+ int nr_phdrs;
|
|
|
f96e0b |
+ grub_uint64_t curr_align;
|
|
|
f96e0b |
+};
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+/* Run through the program headers to calculate the total memory size we
|
|
|
f96e0b |
+ * should claim. */
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
+grub_elf64_calcsize (grub_elf_t _elf __attribute__ ((unused)),
|
|
|
f96e0b |
+ Elf64_Phdr *phdr, void *data)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf64_size_ctx *ctx = data;
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+ /* Only consider loadable segments. */
|
|
|
f96e0b |
+ if (phdr->p_type != PT_LOAD)
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+ ctx->nr_phdrs++;
|
|
|
f96e0b |
+ if (phdr->p_paddr < ctx->segments_start)
|
|
|
f96e0b |
+ ctx->segments_start = phdr->p_paddr;
|
|
|
f96e0b |
+ if (phdr->p_paddr + phdr->p_memsz > ctx->segments_end)
|
|
|
f96e0b |
+ ctx->segments_end = phdr->p_paddr + phdr->p_memsz;
|
|
|
f96e0b |
+ if (ctx->curr_align < phdr->p_align)
|
|
|
f96e0b |
+ ctx->curr_align = phdr->p_align;
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+}
|
|
|
f96e0b |
+
|
|
|
f96e0b |
/* Calculate the amount of memory spanned by the segments. */
|
|
|
f96e0b |
grub_size_t
|
|
|
f96e0b |
grub_elf64_size (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
Elf64_Addr *base, grub_uint64_t *max_align)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
- Elf64_Addr segments_start = (Elf64_Addr) -1;
|
|
|
f96e0b |
- Elf64_Addr segments_end = 0;
|
|
|
f96e0b |
- int nr_phdrs = 0;
|
|
|
f96e0b |
- grub_uint64_t curr_align = 1;
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- /* Run through the program headers to calculate the total memory size we
|
|
|
f96e0b |
- * should claim. */
|
|
|
f96e0b |
- auto int NESTED_FUNC_ATTR calcsize (grub_elf_t _elf, Elf64_Phdr *phdr, void *_arg);
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR calcsize (grub_elf_t _elf __attribute__ ((unused)),
|
|
|
f96e0b |
- Elf64_Phdr *phdr,
|
|
|
f96e0b |
- void *_arg __attribute__ ((unused)))
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- /* Only consider loadable segments. */
|
|
|
f96e0b |
- if (phdr->p_type != PT_LOAD)
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- nr_phdrs++;
|
|
|
f96e0b |
- if (phdr->p_paddr < segments_start)
|
|
|
f96e0b |
- segments_start = phdr->p_paddr;
|
|
|
f96e0b |
- if (phdr->p_paddr + phdr->p_memsz > segments_end)
|
|
|
f96e0b |
- segments_end = phdr->p_paddr + phdr->p_memsz;
|
|
|
f96e0b |
- if (curr_align < phdr->p_align)
|
|
|
f96e0b |
- curr_align = phdr->p_align;
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
+ struct grub_elf64_size_ctx ctx = {
|
|
|
f96e0b |
+ .segments_start = (Elf64_Addr) -1,
|
|
|
f96e0b |
+ .segments_end = 0,
|
|
|
f96e0b |
+ .nr_phdrs = 0,
|
|
|
f96e0b |
+ .curr_align = 1
|
|
|
f96e0b |
+ };
|
|
|
f96e0b |
|
|
|
f96e0b |
- grub_elf64_phdr_iterate (elf, filename, calcsize, 0);
|
|
|
f96e0b |
+ grub_elf64_phdr_iterate (elf, filename, grub_elf64_calcsize, &ctx;;
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
*base = 0;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (nr_phdrs == 0)
|
|
|
f96e0b |
+ if (ctx.nr_phdrs == 0)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
grub_error (GRUB_ERR_BAD_OS, "no program headers present");
|
|
|
f96e0b |
return 0;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (segments_end < segments_start)
|
|
|
f96e0b |
+ if (ctx.segments_end < ctx.segments_start)
|
|
|
f96e0b |
{
|
|
|
f96e0b |
/* Very bad addresses. */
|
|
|
f96e0b |
grub_error (GRUB_ERR_BAD_OS, "bad program header load addresses");
|
|
|
f96e0b |
@@ -416,77 +445,87 @@ grub_elf64_size (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
- *base = segments_start;
|
|
|
f96e0b |
+ *base = ctx.segments_start;
|
|
|
f96e0b |
if (max_align)
|
|
|
f96e0b |
- *max_align = curr_align;
|
|
|
f96e0b |
- return segments_end - segments_start;
|
|
|
f96e0b |
+ *max_align = ctx.curr_align;
|
|
|
f96e0b |
+ return ctx.segments_end - ctx.segments_start;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
-/* Load every loadable segment into memory specified by `_load_hook'. */
|
|
|
f96e0b |
-grub_err_t
|
|
|
f96e0b |
-grub_elf64_load (grub_elf_t _elf, const char *filename,
|
|
|
f96e0b |
- grub_elf64_load_hook_t _load_hook,
|
|
|
f96e0b |
- grub_addr_t *base, grub_size_t *size)
|
|
|
f96e0b |
+struct grub_elf64_load_ctx
|
|
|
f96e0b |
{
|
|
|
f96e0b |
- grub_addr_t load_base = (grub_addr_t) -1ULL;
|
|
|
f96e0b |
- grub_size_t load_size = 0;
|
|
|
f96e0b |
- grub_err_t err;
|
|
|
f96e0b |
+ const char *filename;
|
|
|
f96e0b |
+ grub_elf64_load_hook_t load_hook;
|
|
|
f96e0b |
+ grub_addr_t load_base;
|
|
|
f96e0b |
+ grub_size_t load_size;
|
|
|
f96e0b |
+};
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
+grub_elf64_load_segment (grub_elf_t elf, Elf64_Phdr *phdr, void *data)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf64_load_ctx *ctx = data;
|
|
|
f96e0b |
+ grub_addr_t load_addr;
|
|
|
f96e0b |
+ int do_load = 1;
|
|
|
f96e0b |
|
|
|
f96e0b |
- auto int NESTED_FUNC_ATTR grub_elf64_load_segment (grub_elf_t elf, Elf64_Phdr *phdr,
|
|
|
f96e0b |
- void *hook);
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR grub_elf64_load_segment (grub_elf_t elf, Elf64_Phdr *phdr, void *hook)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- grub_elf64_load_hook_t load_hook = (grub_elf64_load_hook_t) hook;
|
|
|
f96e0b |
- grub_addr_t load_addr;
|
|
|
f96e0b |
- int do_load = 1;
|
|
|
f96e0b |
+ load_addr = phdr->p_paddr;
|
|
|
f96e0b |
+ if (ctx->load_hook && ctx->load_hook (phdr, &load_addr, &do_load))
|
|
|
f96e0b |
+ return 1;
|
|
|
f96e0b |
|
|
|
f96e0b |
- load_addr = phdr->p_paddr;
|
|
|
f96e0b |
- if (load_hook && load_hook (phdr, &load_addr, &do_load))
|
|
|
f96e0b |
- return 1;
|
|
|
f96e0b |
+ if (! do_load)
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (! do_load)
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
+ if (load_addr < ctx->load_base)
|
|
|
f96e0b |
+ ctx->load_base = load_addr;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (load_addr < load_base)
|
|
|
f96e0b |
- load_base = load_addr;
|
|
|
f96e0b |
+ grub_dprintf ("elf", "Loading segment at 0x%llx, size 0x%llx\n",
|
|
|
f96e0b |
+ (unsigned long long) load_addr,
|
|
|
f96e0b |
+ (unsigned long long) phdr->p_memsz);
|
|
|
f96e0b |
|
|
|
f96e0b |
- grub_dprintf ("elf", "Loading segment at 0x%llx, size 0x%llx\n",
|
|
|
f96e0b |
- (unsigned long long) load_addr,
|
|
|
f96e0b |
- (unsigned long long) phdr->p_memsz);
|
|
|
f96e0b |
+ if (grub_file_seek (elf->file, phdr->p_offset) == (grub_off_t) -1)
|
|
|
f96e0b |
+ return grub_errno;
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (grub_file_seek (elf->file, phdr->p_offset) == (grub_off_t) -1)
|
|
|
f96e0b |
- return grub_errno;
|
|
|
f96e0b |
+ if (phdr->p_filesz)
|
|
|
f96e0b |
+ {
|
|
|
f96e0b |
+ grub_ssize_t read;
|
|
|
f96e0b |
+ read = grub_file_read (elf->file, (void *) load_addr, phdr->p_filesz);
|
|
|
f96e0b |
+ if (read != (grub_ssize_t) phdr->p_filesz)
|
|
|
f96e0b |
+ {
|
|
|
f96e0b |
+ /* XXX How can we free memory from `ctx->load_hook'? */
|
|
|
f96e0b |
+ if (!grub_errno)
|
|
|
f96e0b |
+ grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
f96e0b |
+ ctx->filename);
|
|
|
f96e0b |
+ return grub_errno;
|
|
|
f96e0b |
+ }
|
|
|
f96e0b |
+ }
|
|
|
f96e0b |
|
|
|
f96e0b |
- if (phdr->p_filesz)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- grub_ssize_t read;
|
|
|
f96e0b |
- read = grub_file_read (elf->file, (void *) load_addr, phdr->p_filesz);
|
|
|
f96e0b |
- if (read != (grub_ssize_t) phdr->p_filesz)
|
|
|
f96e0b |
- {
|
|
|
f96e0b |
- /* XXX How can we free memory from `load_hook'? */
|
|
|
f96e0b |
- if (!grub_errno)
|
|
|
f96e0b |
- grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"),
|
|
|
f96e0b |
- filename);
|
|
|
f96e0b |
- return grub_errno;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- if (phdr->p_filesz < phdr->p_memsz)
|
|
|
f96e0b |
- grub_memset ((void *) (long) (load_addr + phdr->p_filesz),
|
|
|
f96e0b |
- 0, phdr->p_memsz - phdr->p_filesz);
|
|
|
f96e0b |
-
|
|
|
f96e0b |
- load_size += phdr->p_memsz;
|
|
|
f96e0b |
+ if (phdr->p_filesz < phdr->p_memsz)
|
|
|
f96e0b |
+ grub_memset ((void *) (long) (load_addr + phdr->p_filesz),
|
|
|
f96e0b |
+ 0, phdr->p_memsz - phdr->p_filesz);
|
|
|
f96e0b |
|
|
|
f96e0b |
- return 0;
|
|
|
f96e0b |
- }
|
|
|
f96e0b |
+ ctx->load_size += phdr->p_memsz;
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+ return 0;
|
|
|
f96e0b |
+}
|
|
|
f96e0b |
+
|
|
|
f96e0b |
+/* Load every loadable segment into memory specified by `_load_hook'. */
|
|
|
f96e0b |
+grub_err_t
|
|
|
f96e0b |
+grub_elf64_load (grub_elf_t elf, const char *filename,
|
|
|
f96e0b |
+ grub_elf64_load_hook_t load_hook,
|
|
|
f96e0b |
+ grub_addr_t *base, grub_size_t *size)
|
|
|
f96e0b |
+{
|
|
|
f96e0b |
+ struct grub_elf64_load_ctx ctx = {
|
|
|
f96e0b |
+ .filename = filename,
|
|
|
f96e0b |
+ .load_hook = load_hook,
|
|
|
f96e0b |
+ .load_base = (grub_addr_t) -1ULL,
|
|
|
f96e0b |
+ .load_size = 0
|
|
|
f96e0b |
+ };
|
|
|
f96e0b |
+ grub_err_t err;
|
|
|
f96e0b |
|
|
|
f96e0b |
- err = grub_elf64_phdr_iterate (_elf, filename,
|
|
|
f96e0b |
- grub_elf64_load_segment, _load_hook);
|
|
|
f96e0b |
+ err = grub_elf64_phdr_iterate (elf, filename, grub_elf64_load_segment, &ctx;;
|
|
|
f96e0b |
|
|
|
f96e0b |
if (base)
|
|
|
f96e0b |
- *base = load_base;
|
|
|
f96e0b |
+ *base = ctx.load_base;
|
|
|
f96e0b |
if (size)
|
|
|
f96e0b |
- *size = load_size;
|
|
|
f96e0b |
+ *size = ctx.load_size;
|
|
|
f96e0b |
|
|
|
f96e0b |
return err;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
diff --git a/grub-core/loader/i386/bsd.c b/grub-core/loader/i386/bsd.c
|
|
|
f96e0b |
index 871cf04..9b86158 100644
|
|
|
f96e0b |
--- a/grub-core/loader/i386/bsd.c
|
|
|
f96e0b |
+++ b/grub-core/loader/i386/bsd.c
|
|
|
f96e0b |
@@ -1311,7 +1311,7 @@ grub_bsd_load_aout (grub_file_t file, const char *filename)
|
|
|
f96e0b |
bss_size);
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
-static int NESTED_FUNC_ATTR
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
grub_bsd_elf32_size_hook (grub_elf_t elf __attribute__ ((unused)),
|
|
|
f96e0b |
Elf32_Phdr *phdr, void *arg __attribute__ ((unused)))
|
|
|
f96e0b |
{
|
|
|
f96e0b |
@@ -1353,7 +1353,7 @@ grub_bsd_elf32_hook (Elf32_Phdr * phdr, grub_addr_t * addr, int *do_load)
|
|
|
f96e0b |
return GRUB_ERR_NONE;
|
|
|
f96e0b |
}
|
|
|
f96e0b |
|
|
|
f96e0b |
-static int NESTED_FUNC_ATTR
|
|
|
f96e0b |
+static int
|
|
|
f96e0b |
grub_bsd_elf64_size_hook (grub_elf_t elf __attribute__ ((unused)),
|
|
|
f96e0b |
Elf64_Phdr *phdr, void *arg __attribute__ ((unused)))
|
|
|
f96e0b |
{
|
|
|
f96e0b |
diff --git a/include/grub/elfload.h b/include/grub/elfload.h
|
|
|
f96e0b |
index aae95f5..d1a8d54 100644
|
|
|
f96e0b |
--- a/include/grub/elfload.h
|
|
|
f96e0b |
+++ b/include/grub/elfload.h
|
|
|
f96e0b |
@@ -41,6 +41,11 @@ typedef grub_err_t (*grub_elf32_load_hook_t)
|
|
|
f96e0b |
typedef grub_err_t (*grub_elf64_load_hook_t)
|
|
|
f96e0b |
(Elf64_Phdr *phdr, grub_addr_t *addr, int *load);
|
|
|
f96e0b |
|
|
|
f96e0b |
+typedef int (*grub_elf32_phdr_iterate_hook_t)
|
|
|
f96e0b |
+ (grub_elf_t elf, Elf32_Phdr *phdr, void *arg);
|
|
|
f96e0b |
+typedef int (*grub_elf64_phdr_iterate_hook_t)
|
|
|
f96e0b |
+ (grub_elf_t elf, Elf64_Phdr *phdr, void *arg);
|
|
|
f96e0b |
+
|
|
|
f96e0b |
grub_elf_t grub_elf_open (const char *);
|
|
|
f96e0b |
grub_elf_t grub_elf_file (grub_file_t file, const char *filename);
|
|
|
f96e0b |
grub_err_t grub_elf_close (grub_elf_t);
|
|
|
f96e0b |
@@ -63,12 +68,10 @@ grub_err_t grub_elf64_load (grub_elf_t, const char *filename,
|
|
|
f96e0b |
grub_err_t
|
|
|
f96e0b |
grub_elf32_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
const char *filename,
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf32_Phdr *, void *),
|
|
|
f96e0b |
- void *hook_arg);
|
|
|
f96e0b |
+ grub_elf32_phdr_iterate_hook_t hook, void *hook_arg);
|
|
|
f96e0b |
grub_err_t
|
|
|
f96e0b |
grub_elf64_phdr_iterate (grub_elf_t elf,
|
|
|
f96e0b |
const char *filename,
|
|
|
f96e0b |
- int NESTED_FUNC_ATTR (*hook) (grub_elf_t, Elf64_Phdr *, void *),
|
|
|
f96e0b |
- void *hook_arg);
|
|
|
f96e0b |
+ grub_elf64_phdr_iterate_hook_t hook, void *hook_arg);
|
|
|
f96e0b |
|
|
|
f96e0b |
#endif /* ! GRUB_ELFLOAD_HEADER */
|
|
|
f96e0b |
--
|
|
|
f96e0b |
1.8.2.1
|
|
|
f96e0b |
|