summaryrefslogtreecommitdiffstats
path: root/libelf/elf_begin.c
diff options
context:
space:
mode:
Diffstat (limited to 'libelf/elf_begin.c')
-rw-r--r--libelf/elf_begin.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 04670a45..896d86b6 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1,5 +1,5 @@
/* Create descriptor for processing file.
- Copyright (C) 1998-2005, 2006, 2007, 2008 Red Hat, Inc.
+ Copyright (C) 1998-2010 Red Hat, Inc.
This file is part of Red Hat elfutils.
Written by Ulrich Drepper <[email protected]>, 1998.
@@ -285,13 +285,22 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
/* Could not determine the number of sections. */
return NULL;
- /* We can now allocate the memory. */
+ /* We can now allocate the memory. Even if there are no section headers,
+ we allocate space for a zeroth section in case we need it later. */
+ const size_t scnmax = (scncnt ?: (cmd == ELF_C_RDWR || cmd == ELF_C_RDWR_MMAP)
+ ? 1 : 0);
Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
- ELF_K_ELF, scncnt * sizeof (Elf_Scn));
+ ELF_K_ELF, scnmax * sizeof (Elf_Scn));
if (elf == NULL)
/* Not enough memory. */
return NULL;
+ assert ((unsigned int) scncnt == scncnt);
+ assert (offsetof (struct Elf, state.elf32.scns)
+ == offsetof (struct Elf, state.elf64.scns));
+ elf->state.elf32.scns.cnt = scncnt;
+ elf->state.elf32.scns.max = scnmax;
+
/* Some more or less arbitrary value. */
elf->state.elf.scnincr = 10;
@@ -304,9 +313,6 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
not sufficient for the architecture. */
Elf32_Ehdr *ehdr = (Elf32_Ehdr *) ((char *) map_address + offset);
- assert ((unsigned int) scncnt == scncnt);
- elf->state.elf32.scns.cnt = elf->state.elf32.scns.max = scncnt;
-
/* This is a 32-bit binary. */
if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
&& (ALLOW_UNALIGNED
@@ -392,9 +398,6 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
not sufficient for the architecture. */
Elf64_Ehdr *ehdr = (Elf64_Ehdr *) ((char *) map_address + offset);
- assert ((unsigned int) scncnt == scncnt);
- elf->state.elf64.scns.cnt = elf->state.elf64.scns.max = scncnt;
-
/* This is a 64-bit binary. */
if (map_address != NULL && e_ident[EI_DATA] == MY_ELFDATA
&& (ALLOW_UNALIGNED