[PATCH 1/4] elf core: Write section header table first

From: HATAYAMA Daisuke
Date: Wed Dec 29 2010 - 16:26:32 EST


Change a position of section header table from the last to the next to
ELF header. Because section header table offset is located at next to
ELF header and ELF header size is constant, section header table
offset is also constant if exists; so is program header table
offset. We no longer need to calculate vma data size in advance.

Signed-off-by: HATAYAMA Daisuke <d.hatayama@xxxxxxxxxxxxxx>
---
fs/binfmt_elf.c | 42 ++++++++++++++++++++++--------------------
fs/binfmt_elf_fdpic.c | 43 +++++++++++++++++++++++--------------------
2 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 6884e19..5ab062c 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1234,11 +1234,17 @@ static void fill_elf_header(struct elfhdr
*elf, int segs,
elf->e_type = ET_CORE;
elf->e_machine = machine;
elf->e_version = EV_CURRENT;
- elf->e_phoff = sizeof(struct elfhdr);
elf->e_flags = flags;
elf->e_ehsize = sizeof(struct elfhdr);
elf->e_phentsize = sizeof(struct elf_phdr);
- elf->e_phnum = segs;
+
+ if (segs < PN_XNUM) {
+ elf->e_phoff = sizeof(struct elfhdr);
+ elf->e_phnum = segs;
+ } else {
+ elf->e_phoff = sizeof(struct elfhdr) + sizeof(struct elf_shdr);
+ elf->e_phnum = PN_XNUM;
+ }

return;
}
@@ -1839,12 +1845,13 @@ static struct vm_area_struct *next_vma(struct
vm_area_struct *this_vma,
}

static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
- elf_addr_t e_shoff, int segs)
+ int segs)
{
- elf->e_shoff = e_shoff;
+ elf->e_shoff = sizeof(*elf);
elf->e_shentsize = sizeof(*shdr4extnum);
elf->e_shnum = 1;
elf->e_shstrndx = SHN_UNDEF;
+ elf->e_phoff = elf->e_shoff + elf->e_shnum * elf->e_shentsize;

memset(shdr4extnum, 0, sizeof(*shdr4extnum));

@@ -1886,7 +1893,6 @@ static int elf_core_dump(struct coredump_params *cprm)
struct elf_phdr *phdr4note = NULL;
struct elf_shdr *shdr4extnum = NULL;
Elf_Half e_phnum;
- elf_addr_t e_shoff;

/*
* We no longer stop all VM operations.
@@ -1937,6 +1943,8 @@ static int elf_core_dump(struct coredump_params *cprm)
set_fs(KERNEL_DS);

offset += sizeof(*elf); /* Elf header */
+ if (e_phnum == PN_XNUM) /* Section header */
+ offset += sizeof(struct elf_shdr);
offset += segs * sizeof(struct elf_phdr); /* Program headers */
foffset = offset;

@@ -1956,23 +1964,25 @@ static int elf_core_dump(struct coredump_params *cprm)

dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);

- offset += elf_core_vma_data_size(gate_vma, cprm->mm_flags);
- offset += elf_core_extra_data_size();
- e_shoff = offset;
-
if (e_phnum == PN_XNUM) {
shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
if (!shdr4extnum)
goto end_coredump;
- fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
+ fill_extnum_info(elf, shdr4extnum, segs);
}

- offset = dataoff;
-
size += sizeof(*elf);
if (size > cprm->limit || !dump_write(cprm->file, elf, sizeof(*elf)))
goto end_coredump;

+ if (e_phnum == PN_XNUM) {
+ size += sizeof(*shdr4extnum);
+ if (size > cprm->limit
+ || !dump_write(cprm->file, shdr4extnum,
+ sizeof(*shdr4extnum)))
+ goto end_coredump;
+ }
+
size += sizeof(*phdr4note);
if (size > cprm->limit
|| !dump_write(cprm->file, phdr4note, sizeof(*phdr4note)))
@@ -2046,14 +2056,6 @@ static int elf_core_dump(struct coredump_params *cprm)
if (!elf_core_write_extra_data(cprm->file, &size, cprm->limit))
goto end_coredump;

- if (e_phnum == PN_XNUM) {
- size += sizeof(*shdr4extnum);
- if (size > cprm->limit
- || !dump_write(cprm->file, shdr4extnum,
- sizeof(*shdr4extnum)))
- goto end_coredump;
- }
-
end_coredump:
set_fs(fs);

diff --git a/fs/binfmt_elf_fdpic.c b/fs/binfmt_elf_fdpic.c
index 63039ed..9ff6bef 100644
--- a/fs/binfmt_elf_fdpic.c
+++ b/fs/binfmt_elf_fdpic.c
@@ -1326,15 +1326,22 @@ static inline void
fill_elf_fdpic_header(struct elfhdr *elf, int segs)
elf->e_machine = ELF_ARCH;
elf->e_version = EV_CURRENT;
elf->e_entry = 0;
- elf->e_phoff = sizeof(struct elfhdr);
elf->e_shoff = 0;
elf->e_flags = ELF_FDPIC_CORE_EFLAGS;
elf->e_ehsize = sizeof(struct elfhdr);
elf->e_phentsize = sizeof(struct elf_phdr);
- elf->e_phnum = segs;
elf->e_shentsize = 0;
elf->e_shnum = 0;
elf->e_shstrndx = 0;
+
+ if (segs < PN_XNUM) {
+ elf->e_phoff = sizeof(struct elfhdr);
+ elf->e_phnum = segs;
+ } else {
+ elf->e_phoff = sizeof(struct elfhdr) + sizeof(struct elf_shdr);
+ elf->e_phnum = PN_XNUM;
+ }
+
return;
}

@@ -1495,12 +1502,13 @@ static int elf_dump_thread_status(long signr,
struct elf_thread_status *t)
}

static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,
- elf_addr_t e_shoff, int segs)
+ int segs)
{
- elf->e_shoff = e_shoff;
+ elf->e_shoff = sizeof(*elf);
elf->e_shentsize = sizeof(*shdr4extnum);
elf->e_shnum = 1;
elf->e_shstrndx = SHN_UNDEF;
+ elf->e_phoff = elf->e_shoff + elf->e_shnum * elf->e_shentsize;

memset(shdr4extnum, 0, sizeof(*shdr4extnum));

@@ -1618,7 +1626,6 @@ static int elf_fdpic_core_dump(struct
coredump_params *cprm)
struct elf_phdr *phdr4note = NULL;
struct elf_shdr *shdr4extnum = NULL;
Elf_Half e_phnum;
- elf_addr_t e_shoff;

/*
* We no longer stop all VM operations.
@@ -1734,6 +1741,8 @@ static int elf_fdpic_core_dump(struct
coredump_params *cprm)
set_fs(KERNEL_DS);

offset += sizeof(*elf); /* Elf header */
+ if (e_phnum == PN_XNUM) /* Section header */
+ offset += sizeof(struct elf_shdr);
offset += segs * sizeof(struct elf_phdr); /* Program headers */
foffset = offset;

@@ -1757,23 +1766,25 @@ static int elf_fdpic_core_dump(struct
coredump_params *cprm)
/* Page-align dumped data */
dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);

- offset += elf_core_vma_data_size(cprm->mm_flags);
- offset += elf_core_extra_data_size();
- e_shoff = offset;
-
if (e_phnum == PN_XNUM) {
shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
if (!shdr4extnum)
goto end_coredump;
- fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
+ fill_extnum_info(elf, shdr4extnum, segs);
}

- offset = dataoff;
-
size += sizeof(*elf);
if (size > cprm->limit || !dump_write(cprm->file, elf, sizeof(*elf)))
goto end_coredump;

+ if (e_phnum == PN_XNUM) {
+ size += sizeof(*shdr4extnum);
+ if (size > cprm->limit
+ || !dump_write(cprm->file, shdr4extnum,
+ sizeof(*shdr4extnum)))
+ goto end_coredump;
+ }
+
size += sizeof(*phdr4note);
if (size > cprm->limit
|| !dump_write(cprm->file, phdr4note, sizeof(*phdr4note)))
@@ -1834,14 +1845,6 @@ static int elf_fdpic_core_dump(struct
coredump_params *cprm)
if (!elf_core_write_extra_data(cprm->file, &size, cprm->limit))
goto end_coredump;

- if (e_phnum == PN_XNUM) {
- size += sizeof(*shdr4extnum);
- if (size > cprm->limit
- || !dump_write(cprm->file, shdr4extnum,
- sizeof(*shdr4extnum)))
- goto end_coredump;
- }
-
if (cprm->file->f_pos != offset) {
/* Sanity check */
printk(KERN_WARNING
--
1.7.1
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/