2010-12-29 21:16:23

by HATAYAMA Daisuke

[permalink] [raw]
Subject: [PATCH 0/4] elf core: Write section header table first

This patchset changes a position of section header table, if exists,
from the last to the next to ELF header. According to ELF
specification, the order of ELF component layout is not specified
except for ELF header. See:

http://www.sco.com/developers/gabi/latest/ch4.intro.html#file_format

The merits are:
- reducing tracing memory maps from 3 times to 2 times.
- simple offset handling that makes the code easier to read.

arch/ia64/kernel/elfcore.c | 16 -----
arch/um/sys-i386/elfcore.c | 16 -----
fs/binfmt_elf.c | 136 +++++++++++++++++-------------------------
fs/binfmt_elf_fdpic.c | 141 ++++++++++++++++++--------------------------
include/linux/elfcore.h | 1 -
kernel/elfcore.c | 5 --
6 files changed, 111 insertions(+), 204 deletions(-)

I built and tested this patchset on x86_64. I also built it on ia64,
um-i386 and frv using cross compilers to cover the range.

Here's a program useful to generate ELF core with many program header
entries, and three steps to produce such ELF core:

$ sysctl vm.max_map_count=70000
$ ulimit -c unlimted
$ mkmmap 65535

== mkmmap.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int maps_num;
if (argc < 2) {
fprintf(stderr, "mkmmaps [number of maps to be created]\n");
exit(1);
}
if (sscanf(argv[1], "%d", &maps_num) == EOF) {
perror("sscanf");
exit(2);
}
if (maps_num < 0) {
fprintf(stderr, "%d is invalid\n", maps_num);
exit(3);
}
for (; maps_num > 0; --maps_num) {
if (MAP_FAILED == mmap((void *)NULL, (size_t) 1, PROT_READ,
MAP_SHARED | MAP_ANONYMOUS, (int) -1,
(off_t) NULL)) {
perror("mmap");
exit(4);
}
}
abort();
{
char buffer[128];
sprintf(buffer, "wc -l /proc/%u/maps", getpid());
system(buffer);
}
return 0;
}
==

Signed-off-by: HATAYAMA Daisuke <[email protected]>


2010-12-29 21:26:29

by HATAYAMA Daisuke

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

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 <[email protected]>
---
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