+2015-10-07 Mark Wielaard <mjw@redhat.com>
+
+ * dwfl_module_getdwarf.c (MAX): Removed.
+ (find_prelink_address_sync): Allocate exact amount of bytes for
+ phdrs and shdrs.
+ * dwfl_segment_report_module.c (dwfl_segment_report_module):
+ Likewise for phdrs.
+ * elf-from-memory.c (MAX): Removed.
+ (elf_from_remote_memory): Allocate exact amount of bytes for phdrs.
+
2015-10-05 Chih-Hung Hsieh <chh@google.com>
* dwfl_module_getdwarf.c (find_prelink_address_sync): Do not use
#include "../libdw/libdwP.h" /* DWARF_E_* values are here. */
#include "../libelf/libelfP.h"
-#ifndef MAX
-# define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
static inline Dwfl_Error
open_elf_file (Elf **elf, int *fd, char **name)
{
src.d_size = phnum * phentsize;
GElf_Addr undo_interp = 0;
+ bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
{
- if (unlikely (phnum >
- SIZE_MAX / MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr))))
+ size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
+ if (unlikely (phnum > SIZE_MAX / phdr_size))
return DWFL_E_NOMEM;
- const size_t phdrs_bytes =
- phnum * MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr));
+ const size_t phdrs_bytes = phnum * phdr_size;
void *phdrs = malloc (phdrs_bytes);
- Elf32_Phdr (*p32)[phnum] = phdrs;
- Elf64_Phdr (*p64)[phnum] = phdrs;
if (unlikely (phdrs == NULL))
return DWFL_E_NOMEM;
dst.d_buf = phdrs;
free (phdrs);
return DWFL_E_LIBELF;
}
- if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
+ if (class32)
{
+ Elf32_Phdr (*p32)[phnum] = phdrs;
for (uint_fast16_t i = 0; i < phnum; ++i)
if ((*p32)[i].p_type == PT_INTERP)
{
}
else
{
+ Elf64_Phdr (*p64)[phnum] = phdrs;
for (uint_fast16_t i = 0; i < phnum; ++i)
if ((*p64)[i].p_type == PT_INTERP)
{
src.d_type = ELF_T_SHDR;
src.d_size = gelf_fsize (mod->main.elf, ELF_T_SHDR, shnum - 1, EV_CURRENT);
- if (unlikely (shnum - 1 >
- SIZE_MAX / MAX (sizeof (Elf32_Shdr), sizeof (Elf64_Shdr))))
+ size_t shdr_size = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
+ if (unlikely (shnum - 1 > SIZE_MAX / shdr_size))
return DWFL_E_NOMEM;
- const size_t shdrs_bytes =
- (shnum - 1) * MAX (sizeof (Elf32_Shdr), sizeof (Elf64_Shdr));
+ const size_t shdrs_bytes = (shnum - 1) * shdr_size;
void *shdrs = malloc (shdrs_bytes);
- Elf32_Shdr (*s32)[shnum - 1] = shdrs;
- Elf64_Shdr (*s64)[shnum - 1] = shdrs;
if (unlikely (shdrs == NULL))
return DWFL_E_NOMEM;
dst.d_buf = shdrs;
mod->main.address_sync = highest;
highest = 0;
- if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
- for (size_t i = 0; i < shnum - 1; ++i)
- consider_shdr (undo_interp, (*s32)[i].sh_type,
- (*s32)[i].sh_flags, (*s32)[i].sh_addr,
- (*s32)[i].sh_size);
+ if (class32)
+ {
+ Elf32_Shdr (*s32)[shnum - 1] = shdrs;
+ for (size_t i = 0; i < shnum - 1; ++i)
+ consider_shdr (undo_interp, (*s32)[i].sh_type,
+ (*s32)[i].sh_flags, (*s32)[i].sh_addr,
+ (*s32)[i].sh_size);
+ }
else
- for (size_t i = 0; i < shnum - 1; ++i)
- consider_shdr (undo_interp, (*s64)[i].sh_type,
- (*s64)[i].sh_flags, (*s64)[i].sh_addr,
- (*s64)[i].sh_size);
+ {
+ Elf64_Shdr (*s64)[shnum - 1] = shdrs;
+ for (size_t i = 0; i < shnum - 1; ++i)
+ consider_shdr (undo_interp, (*s64)[i].sh_type,
+ (*s64)[i].sh_flags, (*s64)[i].sh_addr,
+ (*s64)[i].sh_size);
+ }
if (highest > file->vaddr)
file->address_sync = highest;
xlatefrom.d_buf = ph_buffer;
- if (unlikely (phnum >
- SIZE_MAX / MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr))))
+ bool class32 = ei_class == ELFCLASS32;
+ size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
+ if (unlikely (phnum > SIZE_MAX / phdr_size))
return finish ();
- const size_t phdrsp_bytes =
- phnum * MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr));
+ const size_t phdrsp_bytes = phnum * phdr_size;
phdrsp = malloc (phdrsp_bytes);
- Elf32_Phdr (*p32)[phnum] = phdrsp;
- Elf64_Phdr (*p64)[phnum] = phdrsp;
if (unlikely (phdrsp == NULL))
return finish ();
break;
}
}
+
+ Elf32_Phdr (*p32)[phnum] = phdrsp;
+ Elf64_Phdr (*p64)[phnum] = phdrsp;
if (ei_class == ELFCLASS32)
{
if (elf32_xlatetom (&xlateto, &xlatefrom, ei_data) == NULL)
#include <stdlib.h>
#include <string.h>
-#ifndef MAX
-# define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
/* Reconstruct an ELF file by reading the segments out of remote memory
based on the ELF file header at EHDR_VMA and the ELF program headers it
points to. If not null, *LOADBASEP is filled in with the difference
xlatefrom.d_buf = buffer;
}
- if (unlikely (phnum >
- SIZE_MAX / MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr))))
+ bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
+ size_t phdr_size = class32 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr);
+ if (unlikely (phnum > SIZE_MAX / phdr_size))
{
free (buffer);
goto no_memory;
}
- const size_t phdrsp_bytes =
- phnum * MAX (sizeof (Elf32_Phdr), sizeof (Elf64_Phdr));
+ const size_t phdrsp_bytes = phnum * phdr_size;
phdrsp = malloc (phdrsp_bytes);
- Elf32_Phdr (*p32)[phnum] = phdrsp;
- Elf64_Phdr (*p64)[phnum] = phdrsp;
if (unlikely (phdrsp == NULL))
{
free (buffer);
GElf_Off segments_end_mem = 0;
GElf_Addr loadbase = ehdr_vma;
bool found_base = false;
+ Elf32_Phdr (*p32)[phnum] = phdrsp;
+ Elf64_Phdr (*p64)[phnum] = phdrsp;
switch (ehdr.e32.e_ident[EI_CLASS])
{
/* Sanity checks segments and calculates segment_end,
+2015-10-07 Mark Wielaard <mjw@redhat.com>
+
+ * unstrip.c (MAX): Removed.
+ (find_alloc_sections_prelink): Allocate exact amount of bytes for
+ shdrs.
+
2015-10-05 Chih-Hung Hsieh <chh@google.com>
* unstrip.c (find_alloc_sections_prelink): Do not allocate
# define _(str) gettext (str)
#endif
-#ifndef MAX
-# define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
/* Name and version of program. */
static void print_version (FILE *stream, struct argp_state *state);
ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
error (EXIT_FAILURE, 0, _("invalid contents in '%s' section"),
".gnu.prelink_undo");
- if (unlikely ((shnum - 1) >
- SIZE_MAX / MAX (sizeof (Elf32_Shdr), sizeof (Elf64_Shdr))))
+ bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32;
+ size_t shsize = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr);
+ if (unlikely ((shnum - 1) > SIZE_MAX / shsize))
error (EXIT_FAILURE, 0, _("overflow with shnum = %zu in '%s' section"),
(size_t) shnum, ".gnu.prelink_undo");
- const size_t shdr_bytes =
- (shnum - 1) * MAX (sizeof (Elf32_Shdr), sizeof (Elf64_Shdr));
+ const size_t shdr_bytes = (shnum - 1) * shsize;
void *shdr = xmalloc (shdr_bytes);
- Elf32_Shdr (*s32)[shnum - 1] = shdr;
- Elf64_Shdr (*s64)[shnum - 1] = shdr;
dst.d_buf = shdr;
dst.d_size = shdr_bytes;
ELF_CHECK (gelf_xlatetom (main, &dst, &src,
for (size_t i = 0; i < shnum - 1; ++i)
{
struct section *sec = &undo_sections[undo_nalloc];
- if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
+ Elf32_Shdr (*s32)[shnum - 1] = shdr;
+ Elf64_Shdr (*s64)[shnum - 1] = shdr;
+ if (class32)
{
#define COPY(field) sec->shdr.field = (*s32)[i].field
COPY (sh_name);