binfmt_elf.c 38.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * linux/fs/binfmt_elf.c
 *
 * These are the functions used to load ELF format executables as used
 * on SVr4 machines.  Information on the format may be found in the book
 * "UNIX SYSTEM V RELEASE 4 Programmers Guide: Ansi C and Programming Support
 * Tools".
 *
 * Copyright 1993, 1994: Eric Youngdale (ericy@cais.com).
 */

#include <linux/module.h>
13
#include <linux/kernel.h>
Linus Torvalds's avatar
Linus Torvalds committed
14 15
#include <linux/fs.h>
#include <linux/stat.h>
16
#include <linux/time.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18 19 20 21 22 23 24 25 26
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/a.out.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/binfmts.h>
#include <linux/string.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
28 29 30 31 32
#include <linux/shm.h>
#include <linux/personality.h>
#include <linux/elfcore.h>
#include <linux/init.h>
#include <linux/highuid.h>
33
#include <linux/smp.h>
Linus Torvalds's avatar
Linus Torvalds committed
34
#include <linux/smp_lock.h>
Linus Torvalds's avatar
Linus Torvalds committed
35
#include <linux/compiler.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
#include <linux/highmem.h>
37
#include <linux/pagemap.h>
Andrew Morton's avatar
Andrew Morton committed
38
#include <linux/security.h>
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#include <asm/uaccess.h>
#include <asm/param.h>
#include <asm/pgalloc.h>

#include <linux/elf.h>

static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs);
static int load_elf_library(struct file*);
static unsigned long elf_map (struct file *, unsigned long, struct elf_phdr *, int, int);
extern int dump_fpu (struct pt_regs *, elf_fpregset_t *);

#ifndef elf_addr_t
#define elf_addr_t unsigned long
#endif

/*
 * If we don't support core dumping, then supply a NULL so we
 * don't even try.
 */
#ifdef USE_ELF_CORE_DUMP
static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file);
#else
#define elf_core_dump	NULL
#endif

#if ELF_EXEC_PAGESIZE > PAGE_SIZE
# define ELF_MIN_ALIGN	ELF_EXEC_PAGESIZE
#else
# define ELF_MIN_ALIGN	PAGE_SIZE
#endif

#define ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(ELF_MIN_ALIGN-1))
#define ELF_PAGEOFFSET(_v) ((_v) & (ELF_MIN_ALIGN-1))
#define ELF_PAGEALIGN(_v) (((_v) + ELF_MIN_ALIGN - 1) & ~(ELF_MIN_ALIGN - 1))

static struct linux_binfmt elf_format = {
76 77 78 79 80
		.module		= THIS_MODULE,
		.load_binary	= load_elf_binary,
		.load_shlib	= load_elf_library,
		.core_dump	= elf_core_dump,
		.min_coredump	= ELF_EXEC_PAGESIZE
Linus Torvalds's avatar
Linus Torvalds committed
81 82
};

Linus Torvalds's avatar
Linus Torvalds committed
83 84
#define BAD_ADDR(x)	((unsigned long)(x) > TASK_SIZE)

Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88
static void set_brk(unsigned long start, unsigned long end)
{
	start = ELF_PAGEALIGN(start);
	end = ELF_PAGEALIGN(end);
89 90 91
	if (end > start)
		do_brk(start, end - start);
	current->mm->start_brk = current->mm->brk = end;
Linus Torvalds's avatar
Linus Torvalds committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}


/* We need to explicitly zero any fractional pages
   after the data section (i.e. bss).  This would
   contain the junk from the file that should not
   be in memory */


static void padzero(unsigned long elf_bss)
{
	unsigned long nbyte;

	nbyte = ELF_PAGEOFFSET(elf_bss);
	if (nbyte) {
		nbyte = ELF_MIN_ALIGN - nbyte;
		clear_user((void *) elf_bss, nbyte);
	}
}

112
/* Let's use some macros to make this stack manipulation a litle clearer */
113
#ifdef CONFIG_STACK_GROWSUP
114 115 116
#define STACK_ADD(sp, items) ((elf_addr_t *)(sp) + (items))
#define STACK_ROUND(sp, items) \
	((15 + (unsigned long) ((sp) + (items))) &~ 15UL)
117
#define STACK_ALLOC(sp, len) ({ elf_addr_t *old_sp = sp; sp += len; old_sp; })
118 119 120 121 122 123 124 125 126 127 128
#else
#define STACK_ADD(sp, items) ((elf_addr_t *)(sp) - (items))
#define STACK_ROUND(sp, items) \
	(((unsigned long) (sp - items)) &~ 15UL)
#define STACK_ALLOC(sp, len) sp -= len
#endif

static void
create_elf_tables(struct linux_binprm *bprm, struct elfhdr * exec,
		int interp_aout, unsigned long load_addr,
		unsigned long interp_load_addr)
Linus Torvalds's avatar
Linus Torvalds committed
129
{
130 131 132 133
	unsigned long p = bprm->p;
	int argc = bprm->argc;
	int envc = bprm->envc;
	elf_addr_t *argv, *envp;
134
	elf_addr_t *sp, *u_platform;
135 136
	const char *k_platform = ELF_PLATFORM;
	int items;
137
	elf_addr_t elf_info[40];
138
	int ei_index = 0;
139
	struct task_struct *tsk = current;
Linus Torvalds's avatar
Linus Torvalds committed
140 141

	/*
142 143 144
	 * If this architecture has a platform capability string, copy it
	 * to userspace.  In some cases (Sparc), this info is impossible
	 * for userspace to get any other way, in others (i386) it is
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147
	 * merely difficult.
	 */

148
	u_platform = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
149
	if (k_platform) {
150
		size_t len = strlen(k_platform) + 1;
Linus Torvalds's avatar
Linus Torvalds committed
151

James Bottomley's avatar
James Bottomley committed
152
#ifdef CONFIG_X86_HT
153 154 155 156 157 158 159 160 161 162
		/*
		 * In some cases (e.g. Hyper-Threading), we want to avoid L1
		 * evictions by the processes running on the same package. One
		 * thing we can do is to shuffle the initial stack for them.
		 *
		 * The conditionals here are unneeded, but kept in to make the
		 * code behaviour the same as pre change unless we have
		 * hyperthreaded processors. This should be cleaned up
		 * before 2.6
		 */
Dave Jones's avatar
Dave Jones committed
163
	 
164 165
		if (smp_num_siblings > 1)
			STACK_ALLOC(p, ((current->pid % 64) << 7));
Linus Torvalds's avatar
Linus Torvalds committed
166
#endif
167 168
		u_platform = (elf_addr_t *) STACK_ALLOC(p, len);
		__copy_to_user(u_platform, k_platform, len);
169
	}
Linus Torvalds's avatar
Linus Torvalds committed
170

171 172 173
	/* Create the ELF interpreter info */
#define NEW_AUX_ENT(id, val) \
	do { elf_info[ei_index++] = id; elf_info[ei_index++] = val; } while (0)
Linus Torvalds's avatar
Linus Torvalds committed
174

Linus Torvalds's avatar
Linus Torvalds committed
175 176
#ifdef ARCH_DLINFO
	/* 
177 178
	 * ARCH_DLINFO must come first so PPC can do its special alignment of
	 * AUXV.
Linus Torvalds's avatar
Linus Torvalds committed
179 180 181
	 */
	ARCH_DLINFO;
#endif
182 183 184 185 186 187 188 189 190
	NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP);
	NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE);
	NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC);
	NEW_AUX_ENT(AT_PHDR, load_addr + exec->e_phoff);
	NEW_AUX_ENT(AT_PHENT, sizeof (struct elf_phdr));
	NEW_AUX_ENT(AT_PHNUM, exec->e_phnum);
	NEW_AUX_ENT(AT_BASE, interp_load_addr);
	NEW_AUX_ENT(AT_FLAGS, 0);
	NEW_AUX_ENT(AT_ENTRY, exec->e_entry);
191 192 193 194
	NEW_AUX_ENT(AT_UID, (elf_addr_t) tsk->uid);
	NEW_AUX_ENT(AT_EUID, (elf_addr_t) tsk->euid);
	NEW_AUX_ENT(AT_GID, (elf_addr_t) tsk->gid);
	NEW_AUX_ENT(AT_EGID, (elf_addr_t) tsk->egid);
Andrew Morton's avatar
Andrew Morton committed
195
 	NEW_AUX_ENT(AT_SECURE, (elf_addr_t) security_bprm_secureexec(bprm));
196
	if (k_platform) {
197
		NEW_AUX_ENT(AT_PLATFORM, (elf_addr_t)(long)u_platform);
198 199
	}
	NEW_AUX_ENT(AT_NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
200 201
#undef NEW_AUX_ENT

202 203 204 205 206 207 208 209 210 211 212
	sp = STACK_ADD(p, ei_index);

	items = (argc + 1) + (envc + 1);
	if (interp_aout) {
		items += 3; /* a.out interpreters require argv & envp too */
	} else {
		items += 1; /* ELF interpreters only put argc on the stack */
	}
	bprm->p = STACK_ROUND(sp, items);

	/* Point sp at the lowest address on the stack */
213
#ifdef CONFIG_STACK_GROWSUP
214 215 216 217 218 219 220 221 222 223 224
	sp = (elf_addr_t *)bprm->p - items - ei_index;
	bprm->exec = (unsigned long) sp; /* XXX: PARISC HACK */
#else
	sp = (elf_addr_t *)bprm->p;
#endif

	/* Now, let's put argc (and argv, envp if appropriate) on the stack */
	__put_user(argc, sp++);
	if (interp_aout) {
		argv = sp + 2;
		envp = argv + argc + 1;
225 226
		__put_user((elf_addr_t)(long)argv, sp++);
		__put_user((elf_addr_t)(long)envp, sp++);
227 228 229
	} else {
		argv = sp;
		envp = argv + argc + 1;
Linus Torvalds's avatar
Linus Torvalds committed
230 231
	}

232 233 234 235 236 237
	/* Populate argv and envp */
	p = current->mm->arg_start;
	while (argc-- > 0) {
		size_t len;
		__put_user((elf_addr_t)p, argv++);
		len = strnlen_user((void *)p, PAGE_SIZE*MAX_ARG_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
238
		if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
239
			return;
Linus Torvalds's avatar
Linus Torvalds committed
240
		p += len;
Linus Torvalds's avatar
Linus Torvalds committed
241
	}
242
	__put_user(0, argv);
243 244 245 246 247
	current->mm->arg_end = current->mm->env_start = p;
	while (envc-- > 0) {
		size_t len;
		__put_user((elf_addr_t)p, envp++);
		len = strnlen_user((void *)p, PAGE_SIZE*MAX_ARG_PAGES);
Linus Torvalds's avatar
Linus Torvalds committed
248
		if (!len || len > PAGE_SIZE*MAX_ARG_PAGES)
249
			return;
Linus Torvalds's avatar
Linus Torvalds committed
250
		p += len;
Linus Torvalds's avatar
Linus Torvalds committed
251
	}
252
	__put_user(0, envp);
253 254 255 256 257
	current->mm->env_end = p;

	/* Put the elf_info on the stack in the right place.  */
	sp = (elf_addr_t *)envp + 1;
	copy_to_user(sp, elf_info, ei_index * sizeof(elf_addr_t));
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261
}

#ifndef elf_map

262 263
static unsigned long elf_map(struct file *filep, unsigned long addr,
			struct elf_phdr *eppnt, int prot, int type)
Linus Torvalds's avatar
Linus Torvalds committed
264 265 266
{
	unsigned long map_addr;

Linus Torvalds's avatar
Linus Torvalds committed
267
	down_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270
	map_addr = do_mmap(filep, ELF_PAGESTART(addr),
			   eppnt->p_filesz + ELF_PAGEOFFSET(eppnt->p_vaddr), prot, type,
			   eppnt->p_offset - ELF_PAGEOFFSET(eppnt->p_vaddr));
Linus Torvalds's avatar
Linus Torvalds committed
271
	up_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	return(map_addr);
}

#endif /* !elf_map */

/* This is much more generalized than the library routine read function,
   so we keep this separate.  Technically the library read function
   is only provided so that we can read a.out libraries that have
   an ELF header */

static unsigned long load_elf_interp(struct elfhdr * interp_elf_ex,
				     struct file * interpreter,
				     unsigned long *interp_load_addr)
{
	struct elf_phdr *elf_phdata;
	struct elf_phdr *eppnt;
	unsigned long load_addr = 0;
	int load_addr_set = 0;
	unsigned long last_bss = 0, elf_bss = 0;
	unsigned long error = ~0UL;
	int retval, i, size;

	/* First of all, some simple consistency checks */
	if (interp_elf_ex->e_type != ET_EXEC &&
	    interp_elf_ex->e_type != ET_DYN)
		goto out;
	if (!elf_check_arch(interp_elf_ex))
		goto out;
	if (!interpreter->f_op || !interpreter->f_op->mmap)
		goto out;

	/*
	 * If the size of this structure has changed, then punt, since
	 * we will be doing the wrong thing.
	 */
	if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
309 310
	if (interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr))
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

	/* Now read in all of the header information */

	size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;
	if (size > ELF_MIN_ALIGN)
		goto out;
	elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
	if (!elf_phdata)
		goto out;

	retval = kernel_read(interpreter,interp_elf_ex->e_phoff,(char *)elf_phdata,size);
	error = retval;
	if (retval < 0)
		goto out_close;

	eppnt = elf_phdata;
	for (i=0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
	  if (eppnt->p_type == PT_LOAD) {
	    int elf_type = MAP_PRIVATE | MAP_DENYWRITE;
	    int elf_prot = 0;
	    unsigned long vaddr = 0;
	    unsigned long k, map_addr;

	    if (eppnt->p_flags & PF_R) elf_prot =  PROT_READ;
	    if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
	    if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
	    vaddr = eppnt->p_vaddr;
	    if (interp_elf_ex->e_type == ET_EXEC || load_addr_set)
	    	elf_type |= MAP_FIXED;

	    map_addr = elf_map(interpreter, load_addr + vaddr, eppnt, elf_prot, elf_type);
Linus Torvalds's avatar
Linus Torvalds committed
342 343
	    if (BAD_ADDR(map_addr))
	    	goto out_close;
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

	    if (!load_addr_set && interp_elf_ex->e_type == ET_DYN) {
		load_addr = map_addr - ELF_PAGESTART(vaddr);
		load_addr_set = 1;
	    }

	    /*
	     * Find the end of the file mapping for this phdr, and keep
	     * track of the largest address we see for this.
	     */
	    k = load_addr + eppnt->p_vaddr + eppnt->p_filesz;
	    if (k > elf_bss)
		elf_bss = k;

	    /*
	     * Do the same thing for the memory mapping - between
	     * elf_bss and last_bss is the bss section.
	     */
	    k = load_addr + eppnt->p_memsz + eppnt->p_vaddr;
	    if (k > last_bss)
		last_bss = k;
	  }
	}

	/*
	 * Now fill out the bss section.  First pad the last page up
	 * to the page boundary, and then perform a mmap to make sure
	 * that there are zero-mapped pages up to and including the 
	 * last bss page.
	 */
	padzero(elf_bss);
	elf_bss = ELF_PAGESTART(elf_bss + ELF_MIN_ALIGN - 1);	/* What we have mapped so far */

	/* Map the last of the bss segment */
	if (last_bss > elf_bss)
		do_brk(elf_bss, last_bss - elf_bss);

	*interp_load_addr = load_addr;
	error = ((unsigned long) interp_elf_ex->e_entry) + load_addr;

out_close:
	kfree(elf_phdata);
out:
	return error;
}

static unsigned long load_aout_interp(struct exec * interp_ex,
			     struct file * interpreter)
{
	unsigned long text_data, elf_entry = ~0UL;
	char * addr;
	loff_t offset;

	current->mm->end_code = interp_ex->a_text;
	text_data = interp_ex->a_text + interp_ex->a_data;
	current->mm->end_data = text_data;
	current->mm->brk = interp_ex->a_bss + text_data;

	switch (N_MAGIC(*interp_ex)) {
	case OMAGIC:
		offset = 32;
		addr = (char *) 0;
		break;
	case ZMAGIC:
	case QMAGIC:
		offset = N_TXTOFF(*interp_ex);
		addr = (char *) N_TXTADDR(*interp_ex);
		break;
	default:
		goto out;
	}

	do_brk(0, text_data);
	if (!interpreter->f_op || !interpreter->f_op->read)
		goto out;
419
	if (interpreter->f_op->read(interpreter, addr, text_data, &offset) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		goto out;
	flush_icache_range((unsigned long)addr,
	                   (unsigned long)addr + text_data);

	do_brk(ELF_PAGESTART(text_data + ELF_MIN_ALIGN - 1),
		interp_ex->a_bss);
	elf_entry = interp_ex->a_entry;

out:
	return elf_entry;
}

/*
 * These are the functions used to load ELF style executables and shared
 * libraries.  There is no binary dependent code anywhere else.
 */

#define INTERPRETER_NONE 0
#define INTERPRETER_AOUT 1
#define INTERPRETER_ELF 2


static int load_elf_binary(struct linux_binprm * bprm, struct pt_regs * regs)
{
	struct file *interpreter = NULL; /* to shut gcc up */
Linus Torvalds's avatar
Linus Torvalds committed
445
 	unsigned long load_addr = 0, load_bias = 0;
Linus Torvalds's avatar
Linus Torvalds committed
446 447 448 449 450 451
	int load_addr_set = 0;
	char * elf_interpreter = NULL;
	unsigned int interpreter_type = INTERPRETER_NONE;
	unsigned char ibcs2_interpreter = 0;
	unsigned long error;
	struct elf_phdr * elf_ppnt, *elf_phdata;
452
	unsigned long elf_bss, elf_brk;
Linus Torvalds's avatar
Linus Torvalds committed
453
	int elf_exec_fileno;
Linus Torvalds's avatar
Linus Torvalds committed
454 455
	int retval, i;
	unsigned int size;
Linus Torvalds's avatar
Linus Torvalds committed
456 457
	unsigned long elf_entry, interp_load_addr = 0;
	unsigned long start_code, end_code, start_data, end_data;
458
	unsigned long reloc_func_desc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462
	struct elfhdr elf_ex;
	struct elfhdr interp_elf_ex;
  	struct exec interp_ex;
	char passed_fileno[6];
Linus Torvalds's avatar
Linus Torvalds committed
463
	
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	/* Get the exec-header */
	elf_ex = *((struct elfhdr *) bprm->buf);

	retval = -ENOEXEC;
	/* First of all, some simple consistency checks */
	if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
		goto out;

	if (elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN)
		goto out;
	if (!elf_check_arch(&elf_ex))
		goto out;
	if (!bprm->file->f_op||!bprm->file->f_op->mmap)
		goto out;

	/* Now read in all of the header information */

	retval = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
482
	if (elf_ex.e_phentsize != sizeof(struct elf_phdr))
Linus Torvalds's avatar
Linus Torvalds committed
483
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
484 485 486
	if (elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
		goto out;
	size = elf_ex.e_phnum * sizeof(struct elf_phdr);
Linus Torvalds's avatar
Linus Torvalds committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);
	if (!elf_phdata)
		goto out;

	retval = kernel_read(bprm->file, elf_ex.e_phoff, (char *) elf_phdata, size);
	if (retval < 0)
		goto out_free_ph;

	retval = get_unused_fd();
	if (retval < 0)
		goto out_free_ph;
	get_file(bprm->file);
	fd_install(elf_exec_fileno = retval, bprm->file);

	elf_ppnt = elf_phdata;
	elf_bss = 0;
	elf_brk = 0;

	start_code = ~0UL;
	end_code = 0;
	start_data = 0;
	end_data = 0;

	for (i = 0; i < elf_ex.e_phnum; i++) {
		if (elf_ppnt->p_type == PT_INTERP) {
			/* This is the program interpreter used for
			 * shared libraries - for now assume that this
			 * is an a.out format binary
			 */

			retval = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
518 519
			if (elf_ppnt->p_filesz > PATH_MAX)
				goto out_free_file;
Linus Torvalds's avatar
Linus Torvalds committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
			elf_interpreter = (char *) kmalloc(elf_ppnt->p_filesz,
							   GFP_KERNEL);
			if (!elf_interpreter)
				goto out_free_file;

			retval = kernel_read(bprm->file, elf_ppnt->p_offset,
					   elf_interpreter,
					   elf_ppnt->p_filesz);
			if (retval < 0)
				goto out_free_interp;
			/* If the program interpreter is one of these two,
			 * then assume an iBCS2 image. Otherwise assume
			 * a native linux image.
			 */
			if (strcmp(elf_interpreter,"/usr/lib/libc.so.1") == 0 ||
			    strcmp(elf_interpreter,"/usr/lib/ld.so.1") == 0)
				ibcs2_interpreter = 1;
Linus Torvalds's avatar
Linus Torvalds committed
537

Andrew Morton's avatar
Andrew Morton committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
			/*
			 * The early SET_PERSONALITY here is so that the lookup
			 * for the interpreter happens in the namespace of the 
			 * to-be-execed image.  SET_PERSONALITY can select an
			 * alternate root.
			 *
			 * However, SET_PERSONALITY is NOT allowed to switch
			 * this task into the new images's memory mapping
			 * policy - that is, TASK_SIZE must still evaluate to
			 * that which is appropriate to the execing application.
			 * This is because exit_mmap() needs to have TASK_SIZE
			 * evaluate to the size of the old image.
			 *
			 * So if (say) a 64-bit application is execing a 32-bit
			 * application it is the architecture's responsibility
			 * to defer changing the value of TASK_SIZE until the
			 * switch really is going to happen - do this in
			 * flush_thread().	- akpm
			 */
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559
			SET_PERSONALITY(elf_ex, ibcs2_interpreter);

			interpreter = open_exec(elf_interpreter);
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567 568 569
			retval = PTR_ERR(interpreter);
			if (IS_ERR(interpreter))
				goto out_free_interp;
			retval = kernel_read(interpreter, 0, bprm->buf, BINPRM_BUF_SIZE);
			if (retval < 0)
				goto out_free_dentry;

			/* Get the exec headers */
			interp_ex = *((struct exec *) bprm->buf);
			interp_elf_ex = *((struct elfhdr *) bprm->buf);
Linus Torvalds's avatar
Linus Torvalds committed
570
			break;
Linus Torvalds's avatar
Linus Torvalds committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
		}
		elf_ppnt++;
	}

	/* Some simple consistency checks for the interpreter */
	if (elf_interpreter) {
		interpreter_type = INTERPRETER_ELF | INTERPRETER_AOUT;

		/* Now figure out which format our binary is */
		if ((N_MAGIC(interp_ex) != OMAGIC) &&
		    (N_MAGIC(interp_ex) != ZMAGIC) &&
		    (N_MAGIC(interp_ex) != QMAGIC))
			interpreter_type = INTERPRETER_ELF;

		if (memcmp(interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
			interpreter_type &= ~INTERPRETER_ELF;

		retval = -ELIBBAD;
		if (!interpreter_type)
			goto out_free_dentry;

		/* Make sure only one type was selected */
		if ((interpreter_type & INTERPRETER_ELF) &&
		     interpreter_type != INTERPRETER_ELF) {
Linus Torvalds's avatar
Linus Torvalds committed
595 596
	     		// FIXME - ratelimit this before re-enabling
			// printk(KERN_WARNING "ELF: Ambiguous type, using ELF\n");
Linus Torvalds's avatar
Linus Torvalds committed
597 598
			interpreter_type = INTERPRETER_ELF;
		}
599 600 601
	} else {
		/* Executables without an interpreter also need a personality  */
		SET_PERSONALITY(elf_ex, ibcs2_interpreter);
Linus Torvalds's avatar
Linus Torvalds committed
602 603 604 605 606
	}

	/* OK, we are done with that, now set up the arg stuff,
	   and then start this sucker up */

607 608 609
	if ((!bprm->sh_bang) && (interpreter_type == INTERPRETER_AOUT)) {
		char *passed_p = passed_fileno;
		sprintf(passed_fileno, "%d", elf_exec_fileno);
Linus Torvalds's avatar
Linus Torvalds committed
610

611 612
		if (elf_interpreter) {
			retval = copy_strings_kernel(1, &passed_p, bprm);
Linus Torvalds's avatar
Linus Torvalds committed
613 614
			if (retval)
				goto out_free_dentry; 
615
			bprm->argc++;
Linus Torvalds's avatar
Linus Torvalds committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629
		}
	}

	/* Flush all traces of the currently running executable */
	retval = flush_old_exec(bprm);
	if (retval)
		goto out_free_dentry;

	/* OK, This is the point of no return */
	current->mm->start_data = 0;
	current->mm->end_data = 0;
	current->mm->end_code = 0;
	current->mm->mmap = NULL;
	current->flags &= ~PF_FORKNOEXEC;
630 631 632 633

	/* Do this immediately, since STACK_TOP as used in setup_arg_pages
	   may depend on the personality.  */
	SET_PERSONALITY(elf_ex, ibcs2_interpreter);
Linus Torvalds's avatar
Linus Torvalds committed
634 635 636 637

	/* Do this so that we can load the interpreter, if need be.  We will
	   change some of these later */
	current->mm->rss = 0;
638
	current->mm->free_area_cache = TASK_UNMAPPED_BASE;
Dave Jones's avatar
Dave Jones committed
639 640 641
	retval = setup_arg_pages(bprm);
	if (retval < 0) {
		send_sig(SIGKILL, current, 0);
642
		goto out_free_dentry;
Dave Jones's avatar
Dave Jones committed
643 644
	}
	
Linus Torvalds's avatar
Linus Torvalds committed
645 646 647 648 649 650 651 652 653
	current->mm->start_stack = bprm->p;

	/* Now we do a little grungy work by mmaping the ELF image into
	   the correct location in memory.  At this point, we assume that
	   the image should be loaded at fixed address, not at a variable
	   address. */

	for(i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
		int elf_prot = 0, elf_flags;
654
		unsigned long k, vaddr;
Linus Torvalds's avatar
Linus Torvalds committed
655 656 657 658

		if (elf_ppnt->p_type != PT_LOAD)
			continue;

Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
		if (unlikely (elf_brk > elf_bss)) {
			unsigned long nbyte;
	            
			/* There was a PT_LOAD segment with p_memsz > p_filesz
			   before this one. Map anonymous pages, if needed,
			   and clear the area.  */
			set_brk (elf_bss + load_bias, elf_brk + load_bias);
			nbyte = ELF_PAGEOFFSET(elf_bss);
			if (nbyte) {
				nbyte = ELF_MIN_ALIGN - nbyte;
				if (nbyte > elf_brk - elf_bss)
					nbyte = elf_brk - elf_bss;
				clear_user((void *) elf_bss + load_bias, nbyte);
			}
		}

Linus Torvalds's avatar
Linus Torvalds committed
675 676 677 678 679 680 681 682 683
		if (elf_ppnt->p_flags & PF_R) elf_prot |= PROT_READ;
		if (elf_ppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
		if (elf_ppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;

		elf_flags = MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;

		vaddr = elf_ppnt->p_vaddr;
		if (elf_ex.e_type == ET_EXEC || load_addr_set) {
			elf_flags |= MAP_FIXED;
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686
		} else if (elf_ex.e_type == ET_DYN) {
			/* Try and get dynamic programs out of the way of the default mmap
			   base, as well as whatever program they might try to exec.  This
687
			   is because the brk will follow the loader, and is not movable.  */
Linus Torvalds's avatar
Linus Torvalds committed
688
			load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691
		}

		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags);
Linus Torvalds's avatar
Linus Torvalds committed
692 693
		if (BAD_ADDR(error))
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
694 695 696 697 698 699 700

		if (!load_addr_set) {
			load_addr_set = 1;
			load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
			if (elf_ex.e_type == ET_DYN) {
				load_bias += error -
				             ELF_PAGESTART(load_bias + vaddr);
Linus Torvalds's avatar
Linus Torvalds committed
701
				load_addr += load_bias;
702
				reloc_func_desc = load_bias;
Linus Torvalds's avatar
Linus Torvalds committed
703 704 705 706 707 708 709 710 711 712
			}
		}
		k = elf_ppnt->p_vaddr;
		if (k < start_code) start_code = k;
		if (start_data < k) start_data = k;

		k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;

		if (k > elf_bss)
			elf_bss = k;
713
		if ((elf_ppnt->p_flags & PF_X) && end_code < k)
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716 717 718 719 720 721
			end_code = k;
		if (end_data < k)
			end_data = k;
		k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
		if (k > elf_brk)
			elf_brk = k;
	}

722
	elf_ex.e_entry += load_bias;
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	elf_bss += load_bias;
	elf_brk += load_bias;
	start_code += load_bias;
	end_code += load_bias;
	start_data += load_bias;
	end_data += load_bias;

	if (elf_interpreter) {
		if (interpreter_type == INTERPRETER_AOUT)
			elf_entry = load_aout_interp(&interp_ex,
						     interpreter);
		else
			elf_entry = load_elf_interp(&interp_elf_ex,
						    interpreter,
						    &interp_load_addr);

		allow_write_access(interpreter);
		fput(interpreter);
		kfree(elf_interpreter);

Linus Torvalds's avatar
Linus Torvalds committed
743
		if (BAD_ADDR(elf_entry)) {
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746
			printk(KERN_ERR "Unable to load interpreter\n");
			kfree(elf_phdata);
			send_sig(SIGSEGV, current, 0);
747 748
			retval = -ENOEXEC; /* Nobody gets to see this, but.. */
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
749
		}
750
		reloc_func_desc = interp_load_addr;
751 752
	} else {
		elf_entry = elf_ex.e_entry;
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755 756 757 758 759 760 761 762 763
	}

	kfree(elf_phdata);

	if (interpreter_type != INTERPRETER_AOUT)
		sys_close(elf_exec_fileno);

	set_binfmt(&elf_format);

	compute_creds(bprm);
	current->flags &= ~PF_FORKNOEXEC;
764 765
	create_elf_tables(bprm, &elf_ex, (interpreter_type == INTERPRETER_AOUT),
			load_addr, interp_load_addr);
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	/* N.B. passed_fileno might not be initialized? */
	if (interpreter_type == INTERPRETER_AOUT)
		current->mm->arg_start += strlen(passed_fileno) + 1;
	current->mm->end_code = end_code;
	current->mm->start_code = start_code;
	current->mm->start_data = start_data;
	current->mm->end_data = end_data;
	current->mm->start_stack = bprm->p;

	/* Calling set_brk effectively mmaps the pages that we need
	 * for the bss and break sections
	 */
	set_brk(elf_bss, elf_brk);

	padzero(elf_bss);

Linus Torvalds's avatar
Linus Torvalds committed
782
	if (current->personality & MMAP_PAGE_ZERO) {
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787
		/* Why this, you ask???  Well SVr4 maps page 0 as read-only,
		   and some applications "depend" upon this behavior.
		   Since we do not have the power to recompile these, we
		   emulate the SVr4 behavior.  Sigh.  */
		/* N.B. Shouldn't the size here be PAGE_SIZE?? */
Linus Torvalds's avatar
Linus Torvalds committed
788
		down_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
789 790
		error = do_mmap(NULL, 0, 4096, PROT_READ | PROT_EXEC,
				MAP_FIXED | MAP_PRIVATE, 0);
Linus Torvalds's avatar
Linus Torvalds committed
791
		up_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
792 793 794 795 796 797
	}

#ifdef ELF_PLAT_INIT
	/*
	 * The ABI may specify that certain registers be set up in special
	 * ways (on i386 %edx is the address of a DT_FINI function, for
798 799 800 801 802 803
	 * example.  In addition, it may also specify (eg, PowerPC64 ELF)
	 * that the e_entry field is the address of the function descriptor
	 * for the startup routine, rather than the address of the startup
	 * routine itself.  This macro performs whatever initialization to
	 * the regs structure is required as well as any relocations to the
	 * function descriptor entries when executing dynamically links apps.
Linus Torvalds's avatar
Linus Torvalds committed
804
	 */
805
	ELF_PLAT_INIT(regs, reloc_func_desc);
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808
#endif

	start_thread(regs, elf_entry, bprm->p);
809 810 811 812 813 814
	if (unlikely(current->ptrace & PT_PTRACED)) {
		if (current->ptrace & PT_TRACE_EXEC)
			ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
		else
			send_sig(SIGTRAP, current, 0);
	}
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	retval = 0;
out:
	return retval;

	/* error cleanup */
out_free_dentry:
	allow_write_access(interpreter);
	fput(interpreter);
out_free_interp:
	if (elf_interpreter)
		kfree(elf_interpreter);
out_free_file:
	sys_close(elf_exec_fileno);
out_free_ph:
	kfree(elf_phdata);
	goto out;
}

/* This is really simpleminded and specialized - we are loading an
   a.out library that is given an ELF header. */

static int load_elf_library(struct file *file)
{
	struct elf_phdr *elf_phdata;
Linus Torvalds's avatar
Linus Torvalds committed
839
	unsigned long elf_bss, bss, len;
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	int retval, error, i, j;
	struct elfhdr elf_ex;

	error = -ENOEXEC;
	retval = kernel_read(file, 0, (char *) &elf_ex, sizeof(elf_ex));
	if (retval != sizeof(elf_ex))
		goto out;

	if (memcmp(elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
		goto out;

	/* First of all, some simple consistency checks */
	if (elf_ex.e_type != ET_EXEC || elf_ex.e_phnum > 2 ||
	   !elf_check_arch(&elf_ex) || !file->f_op || !file->f_op->mmap)
		goto out;

	/* Now read in all of the header information */

	j = sizeof(struct elf_phdr) * elf_ex.e_phnum;
Linus Torvalds's avatar
Linus Torvalds committed
859
	/* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */
Linus Torvalds's avatar
Linus Torvalds committed
860 861 862 863 864 865 866

	error = -ENOMEM;
	elf_phdata = (struct elf_phdr *) kmalloc(j, GFP_KERNEL);
	if (!elf_phdata)
		goto out;

	error = -ENOEXEC;
Linus Torvalds's avatar
Linus Torvalds committed
867 868 869 870
	retval = kernel_read(file, elf_ex.e_phoff, (char *) elf_phdata, j);
	if (retval != j)
		goto out_free_ph;

Linus Torvalds's avatar
Linus Torvalds committed
871 872 873 874 875 876 877 878
	for (j = 0, i = 0; i<elf_ex.e_phnum; i++)
		if ((elf_phdata + i)->p_type == PT_LOAD) j++;
	if (j != 1)
		goto out_free_ph;

	while (elf_phdata->p_type != PT_LOAD) elf_phdata++;

	/* Now use mmap to map the library into memory. */
Linus Torvalds's avatar
Linus Torvalds committed
879
	down_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882 883 884 885 886 887
	error = do_mmap(file,
			ELF_PAGESTART(elf_phdata->p_vaddr),
			(elf_phdata->p_filesz +
			 ELF_PAGEOFFSET(elf_phdata->p_vaddr)),
			PROT_READ | PROT_WRITE | PROT_EXEC,
			MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
			(elf_phdata->p_offset -
			 ELF_PAGEOFFSET(elf_phdata->p_vaddr)));
Linus Torvalds's avatar
Linus Torvalds committed
888
	up_write(&current->mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
889 890 891
	if (error != ELF_PAGESTART(elf_phdata->p_vaddr))
		goto out_free_ph;

Linus Torvalds's avatar
Linus Torvalds committed
892
	elf_bss = elf_phdata->p_vaddr + elf_phdata->p_filesz;
Linus Torvalds's avatar
Linus Torvalds committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	padzero(elf_bss);

	len = ELF_PAGESTART(elf_phdata->p_filesz + elf_phdata->p_vaddr + ELF_MIN_ALIGN - 1);
	bss = elf_phdata->p_memsz + elf_phdata->p_vaddr;
	if (bss > len)
		do_brk(len, bss - len);
	error = 0;

out_free_ph:
	kfree(elf_phdata);
out:
	return error;
}

/*
 * Note that some platforms still use traditional core dumps and not
 * the ELF core dump.  Each platform can select it as appropriate.
 */
#ifdef USE_ELF_CORE_DUMP

/*
 * ELF core dumper
 *
 * Modelled on fs/exec.c:aout_core_dump()
 * Jeremy Fitzhardinge <jeremy@sw.oz.au>
 */
/*
 * These are the only things you should do on a core-file: use only these
 * functions to write out all the necessary info.
 */
static int dump_write(struct file *file, const void *addr, int nr)
{
	return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
}

static int dump_seek(struct file *file, off_t off)
{
	if (file->f_op->llseek) {
		if (file->f_op->llseek(file, off, 0) != off)
			return 0;
	} else
		file->f_pos = off;
	return 1;
}

/*
 * Decide whether a segment is worth dumping; default is yes to be
 * sure (missing info is worse than too much; etc).
 * Personally I'd include everything, and use the coredump limit...
 *
 * I think we should skip something. But I am not sure how. H.J.
 */
945
static int maydump(struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
Linus Torvalds's avatar
Linus Torvalds committed
947 948 949 950 951
	/*
	 * If we may not read the contents, don't allow us to dump
	 * them either. "dump_write()" can't handle it anyway.
	 */
	if (!(vma->vm_flags & VM_READ))
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954
		return 0;

	/* Do not dump I/O mapped devices! -DaveM */
Linus Torvalds's avatar
Linus Torvalds committed
955
	if (vma->vm_flags & VM_IO)
Linus Torvalds's avatar
Linus Torvalds committed
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
		return 0;
#if 1
	if (vma->vm_flags & (VM_WRITE|VM_GROWSUP|VM_GROWSDOWN))
		return 1;
	if (vma->vm_flags & (VM_READ|VM_EXEC|VM_EXECUTABLE|VM_SHARED))
		return 0;
#endif
	return 1;
}

#define roundup(x, y)  ((((x)+((y)-1))/(y))*(y))

/* An ELF note in memory */
struct memelfnote
{
	const char *name;
	int type;
	unsigned int datasz;
	void *data;
};

static int notesize(struct memelfnote *en)
{
	int sz;

	sz = sizeof(struct elf_note);
982
	sz += roundup(strlen(en->name) + 1, 4);
Linus Torvalds's avatar
Linus Torvalds committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996
	sz += roundup(en->datasz, 4);

	return sz;
}

#define DUMP_WRITE(addr, nr)	\
	do { if (!dump_write(file, (addr), (nr))) return 0; } while(0)
#define DUMP_SEEK(off)	\
	do { if (!dump_seek(file, (off))) return 0; } while(0)

static int writenote(struct memelfnote *men, struct file *file)
{
	struct elf_note en;

997
	en.n_namesz = strlen(men->name) + 1;
Linus Torvalds's avatar
Linus Torvalds committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
	en.n_descsz = men->datasz;
	en.n_type = men->type;

	DUMP_WRITE(&en, sizeof(en));
	DUMP_WRITE(men->name, en.n_namesz);
	/* XXX - cast from long long to long to avoid need for libgcc.a */
	DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));	/* XXX */
	DUMP_WRITE(men->data, men->datasz);
	DUMP_SEEK(roundup((unsigned long)file->f_pos, 4));	/* XXX */

	return 1;
}
#undef DUMP_WRITE
#undef DUMP_SEEK

#define DUMP_WRITE(addr, nr)	\
	if ((size += (nr)) > limit || !dump_write(file, (addr), (nr))) \
		goto end_coredump;
#define DUMP_SEEK(off)	\
	if (!dump_seek(file, (off))) \
		goto end_coredump;
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

static inline void fill_elf_header(struct elfhdr *elf, int segs)
{
	memcpy(elf->e_ident, ELFMAG, SELFMAG);
	elf->e_ident[EI_CLASS] = ELF_CLASS;
	elf->e_ident[EI_DATA] = ELF_DATA;
	elf->e_ident[EI_VERSION] = EV_CURRENT;
	memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD);

	elf->e_type = ET_CORE;
	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 = 0;
	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;
	return;
}

static inline void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
{
	phdr->p_type = PT_NOTE;
	phdr->p_offset = offset;
	phdr->p_vaddr = 0;
	phdr->p_paddr = 0;
	phdr->p_filesz = sz;
	phdr->p_memsz = 0;
	phdr->p_flags = 0;
	phdr->p_align = 0;
	return;
}

1057
static void fill_note(struct memelfnote *note, const char *name, int type, 
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
		unsigned int sz, void *data)
{
	note->name = name;
	note->type = type;
	note->datasz = sz;
	note->data = data;
	return;
}

/*
 * fill up all the fields in prstatus from the given task struct, except registers
Steven Cole's avatar
Steven Cole committed
1069
 * which need to be filled up separately.
1070
 */
1071 1072
static void fill_prstatus(struct elf_prstatus *prstatus,
			struct task_struct *p, long signr) 
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
{
	prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
	prstatus->pr_sigpend = p->pending.signal.sig[0];
	prstatus->pr_sighold = p->blocked.sig[0];
	prstatus->pr_pid = p->pid;
	prstatus->pr_ppid = p->parent->pid;
	prstatus->pr_pgrp = p->pgrp;
	prstatus->pr_sid = p->session;
	jiffies_to_timeval(p->utime, &prstatus->pr_utime);
	jiffies_to_timeval(p->stime, &prstatus->pr_stime);
	jiffies_to_timeval(p->cutime, &prstatus->pr_cutime);
	jiffies_to_timeval(p->cstime, &prstatus->pr_cstime);
}

1087
static void fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p)
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
{
	int i, len;
	
	/* first copy the parameters from user space */
	memset(psinfo, 0, sizeof(struct elf_prpsinfo));

	len = p->mm->arg_end - p->mm->arg_start;
	if (len >= ELF_PRARGSZ)
		len = ELF_PRARGSZ-1;
	copy_from_user(&psinfo->pr_psargs,
		      (const char *)p->mm->arg_start, len);
	for(i = 0; i < len; i++)
		if (psinfo->pr_psargs[i] == 0)
			psinfo->pr_psargs[i] = ' ';
	psinfo->pr_psargs[len] = 0;

	psinfo->pr_pid = p->pid;
	psinfo->pr_ppid = p->parent->pid;
	psinfo->pr_pgrp = p->pgrp;
	psinfo->pr_sid = p->session;

	i = p->state ? ffz(~p->state) + 1 : 0;
	psinfo->pr_state = i;
1111
	psinfo->pr_sname = (i < 0 || i > 5) ? '.' : "RSDTZW"[i];
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	psinfo->pr_zomb = psinfo->pr_sname == 'Z';
	psinfo->pr_nice = task_nice(p);
	psinfo->pr_flag = p->flags;
	psinfo->pr_uid = NEW_TO_OLD_UID(p->uid);
	psinfo->pr_gid = NEW_TO_OLD_GID(p->gid);
	strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname));
	
	return;
}

/* Here is the structure in which status of each thread is captured. */
struct elf_thread_status
{
	struct list_head list;
	struct elf_prstatus prstatus;	/* NT_PRSTATUS */
	elf_fpregset_t fpu;		/* NT_PRFPREG */
#ifdef ELF_CORE_COPY_XFPREGS
	elf_fpxregset_t xfpu;		/* NT_PRXFPREG */
#endif
	struct memelfnote notes[3];
	int num_notes;
};

/*
 * In order to add the specific thread information for the elf file format,
 * we need to keep a linked list of every threads pr_status and then
 * create a single section for them in the final core file.
 */
static int elf_dump_thread_status(long signr, struct task_struct * p, struct list_head * thread_list)
{

	struct elf_thread_status *t;
	int sz = 0;

	t = kmalloc(sizeof(*t), GFP_ATOMIC);
	if (!t)
		return 0;
	memset(t, 0, sizeof(*t));

	INIT_LIST_HEAD(&t->list);
	t->num_notes = 0;

	fill_prstatus(&t->prstatus, p, signr);
	elf_core_copy_task_regs(p, &t->prstatus.pr_reg);	
	
	fill_note(&t->notes[0], "CORE", NT_PRSTATUS, sizeof(t->prstatus), &(t->prstatus));
	t->num_notes++;
	sz += notesize(&t->notes[0]);

	if ((t->prstatus.pr_fpvalid = elf_core_copy_task_fpregs(p, &t->fpu))) {
		fill_note(&t->notes[1], "CORE", NT_PRFPREG, sizeof(t->fpu), &(t->fpu));
		t->num_notes++;
		sz += notesize(&t->notes[1]);
	}

#ifdef ELF_CORE_COPY_XFPREGS
	if (elf_core_copy_task_xfpregs(p, &t->xfpu)) {
		fill_note(&t->notes[2], "LINUX", NT_PRXFPREG, sizeof(t->xfpu), &t->xfpu);
		t->num_notes++;
		sz += notesize(&t->notes[2]);
	}
#endif	
	list_add(&t->list, thread_list);
	return sz;
}

Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180 1181 1182 1183 1184 1185 1186
/*
 * Actual dumper
 *
 * This is a two-pass process; first we find the offsets of the bits,
 * and then they are actually written out.  If we run out of core limit
 * we just truncate.
 */
static int elf_core_dump(long signr, struct pt_regs * regs, struct file * file)
{
1187
#define	NUM_NOTES	5
Linus Torvalds's avatar
Linus Torvalds committed
1188 1189 1190 1191 1192 1193
	int has_dumped = 0;
	mm_segment_t fs;
	int segs;
	size_t size = 0;
	int i;
	struct vm_area_struct *vma;
1194
	struct elfhdr *elf = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1195 1196
	off_t offset = 0, dataoff;
	unsigned long limit = current->rlim[RLIMIT_CORE].rlim_cur;
1197 1198 1199 1200
	int numnote = NUM_NOTES;
	struct memelfnote *notes = NULL;
	struct elf_prstatus *prstatus = NULL;	/* NT_PRSTATUS */
	struct elf_prpsinfo *psinfo = NULL;	/* NT_PRPSINFO */
1201 1202 1203
 	struct task_struct *g, *p;
 	LIST_HEAD(thread_list);
 	struct list_head *t;
1204
	elf_fpregset_t *fpu = NULL;
1205
#ifdef ELF_CORE_COPY_XFPREGS
1206
	elf_fpxregset_t *xfpu = NULL;
1207
#endif
1208
	int thread_status_size = 0;
1209 1210 1211

	/*
	 * We no longer stop all VM operations.
1212
	 * 
1213 1214
	 * This is because those proceses that could possibly change map_count or
	 * the mmap / vma pages are now blocked in do_exit on current finishing
1215 1216 1217
	 * this core dump.
	 *
	 * Only ptrace can touch these memory addresses, but it doesn't change
1218 1219
	 * the map_count or the pages allocated.  So no possibility of crashing
	 * exists while dumping the mm->vm_next areas to the core file.
1220
	 */
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
  
	/* alloc memory for large data structures: too large to be on stack */
	elf = kmalloc(sizeof(*elf), GFP_KERNEL);
	if (!elf)
		goto cleanup;
	prstatus = kmalloc(sizeof(*prstatus), GFP_KERNEL);
	if (!prstatus)
		goto cleanup;
	psinfo = kmalloc(sizeof(*psinfo), GFP_KERNEL);
	if (!psinfo)
		goto cleanup;
	notes = kmalloc(NUM_NOTES * sizeof(struct memelfnote), GFP_KERNEL);
	if (!notes)
		goto cleanup;
	fpu = kmalloc(sizeof(*fpu), GFP_KERNEL);
	if (!fpu)
		goto cleanup;
#ifdef ELF_CORE_COPY_XFPREGS
	xfpu = kmalloc(sizeof(*xfpu), GFP_KERNEL);
	if (!xfpu)
		goto cleanup;
#endif

	/* capture the status of all other threads */
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	if (signr) {
		read_lock(&tasklist_lock);
		do_each_thread(g,p)
			if (current->mm == p->mm && current != p) {
				int sz = elf_dump_thread_status(signr, p, &thread_list);
				if (!sz) {
					read_unlock(&tasklist_lock);
					goto cleanup;
				} else
					thread_status_size += sz;
			}
		while_each_thread(g,p);
		read_unlock(&tasklist_lock);
1258 1259
	}

1260
	/* now collect the dump for the current */
1261 1262 1263
	memset(prstatus, 0, sizeof(*prstatus));
	fill_prstatus(prstatus, current, signr);
	elf_core_copy_regs(&prstatus->pr_reg, regs);
1264
	
Linus Torvalds's avatar
Linus Torvalds committed
1265
	segs = current->mm->map_count;
1266 1267 1268
#ifdef ELF_CORE_EXTRA_PHDRS
	segs += ELF_CORE_EXTRA_PHDRS;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1269 1270

	/* Set up header */
1271
	fill_elf_header(elf, segs+1);	/* including notes section */
Linus Torvalds's avatar
Linus Torvalds committed
1272 1273 1274 1275 1276 1277 1278 1279 1280

	has_dumped = 1;
	current->flags |= PF_DUMPCORE;

	/*
	 * Set up the notes in similar form to SVR4 core dumps made
	 * with info from their /proc.
	 */

1281
	fill_note(notes +0, "CORE", NT_PRSTATUS, sizeof(*prstatus), prstatus);
1282
	
1283 1284
	fill_psinfo(psinfo, current->group_leader);
	fill_note(notes +1, "CORE", NT_PRPSINFO, sizeof(*psinfo), psinfo);
Linus Torvalds's avatar
Linus Torvalds committed
1285
	
1286
	fill_note(notes +2, "CORE", NT_TASKSTRUCT, sizeof(*current), current);
1287 1288
  
  	/* Try to dump the FPU. */
1289 1290
	if ((prstatus->pr_fpvalid = elf_core_copy_task_fpregs(current, fpu)))
		fill_note(notes +3, "CORE", NT_PRFPREG, sizeof(*fpu), fpu);
1291 1292 1293
	else
		--numnote;
#ifdef ELF_CORE_COPY_XFPREGS
1294 1295
	if (elf_core_copy_task_xfpregs(current, xfpu))
		fill_note(notes +4, "LINUX", NT_PRXFPREG, sizeof(*xfpu), xfpu);
1296 1297 1298
	else
		--numnote;
#else
1299
	numnote--;
1300 1301
#endif	
  
1302 1303 1304
	fs = get_fs();
	set_fs(KERNEL_DS);

1305 1306
	DUMP_WRITE(elf, sizeof(*elf));
	offset += sizeof(*elf);				/* Elf header */
1307 1308
	offset += (segs+1) * sizeof(struct elf_phdr);	/* Program headers */

Linus Torvalds's avatar
Linus Torvalds committed
1309 1310 1311 1312 1313
	/* Write notes phdr entry */
	{
		struct elf_phdr phdr;
		int sz = 0;

1314 1315
		for (i = 0; i < numnote; i++)
			sz += notesize(notes + i);
1316 1317
		
		sz += thread_status_size;
Linus Torvalds's avatar
Linus Torvalds committed
1318

1319 1320
		fill_elf_note_phdr(&phdr, sz, offset);
		offset += sz;
Linus Torvalds's avatar
Linus Torvalds committed
1321 1322 1323 1324 1325 1326 1327
		DUMP_WRITE(&phdr, sizeof(phdr));
	}

	/* Page-align dumped data */
	dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);

	/* Write program headers for segments dump */
1328
	for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
Linus Torvalds's avatar
Linus Torvalds committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		struct elf_phdr phdr;
		size_t sz;

		sz = vma->vm_end - vma->vm_start;

		phdr.p_type = PT_LOAD;
		phdr.p_offset = offset;
		phdr.p_vaddr = vma->vm_start;
		phdr.p_paddr = 0;
		phdr.p_filesz = maydump(vma) ? sz : 0;
		phdr.p_memsz = sz;
		offset += phdr.p_filesz;
		phdr.p_flags = vma->vm_flags & VM_READ ? PF_R : 0;
		if (vma->vm_flags & VM_WRITE) phdr.p_flags |= PF_W;
		if (vma->vm_flags & VM_EXEC) phdr.p_flags |= PF_X;
		phdr.p_align = ELF_EXEC_PAGESIZE;

		DUMP_WRITE(&phdr, sizeof(phdr));
	}

1349 1350 1351 1352
#ifdef ELF_CORE_WRITE_EXTRA_PHDRS
	ELF_CORE_WRITE_EXTRA_PHDRS;
#endif

1353
 	/* write out the notes section */
1354 1355
	for (i = 0; i < numnote; i++)
		if (!writenote(notes + i, file))
Linus Torvalds's avatar
Linus Torvalds committed
1356 1357
			goto end_coredump;

1358 1359 1360 1361 1362 1363 1364 1365
	/* write out the thread status notes section */
	list_for_each(t, &thread_list) {
		struct elf_thread_status *tmp = list_entry(t, struct elf_thread_status, list);
		for (i = 0; i < tmp->num_notes; i++)
			if (!writenote(&tmp->notes[i], file))
				goto end_coredump;
	}
 
Linus Torvalds's avatar
Linus Torvalds committed
1366 1367
	DUMP_SEEK(dataoff);

1368
	for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
Linus Torvalds's avatar
Linus Torvalds committed
1369 1370 1371 1372
		unsigned long addr;

		if (!maydump(vma))
			continue;
1373

Linus Torvalds's avatar
Linus Torvalds committed
1374 1375 1376
		for (addr = vma->vm_start;
		     addr < vma->vm_end;
		     addr += PAGE_SIZE) {
Linus Torvalds's avatar
Linus Torvalds committed
1377 1378 1379 1380 1381
			struct page* page;
			struct vm_area_struct *vma;

			if (get_user_pages(current, current->mm, addr, 1, 0, 1,
						&page, &vma) <= 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383
				DUMP_SEEK (file->f_pos + PAGE_SIZE);
			} else {
Linus Torvalds's avatar
Linus Torvalds committed
1384 1385 1386 1387 1388 1389 1390 1391 1392
				if (page == ZERO_PAGE(addr)) {
					DUMP_SEEK (file->f_pos + PAGE_SIZE);
				} else {
					void *kaddr;
					flush_cache_page(vma, addr);
					kaddr = kmap(page);
					DUMP_WRITE(kaddr, PAGE_SIZE);
					kunmap(page);
				}
1393
				page_cache_release(page);
Linus Torvalds's avatar
Linus Torvalds committed
1394 1395 1396 1397
			}
		}
	}

1398 1399 1400 1401
#ifdef ELF_CORE_WRITE_EXTRA_DATA
	ELF_CORE_WRITE_EXTRA_DATA;
#endif

Linus Torvalds's avatar
Linus Torvalds committed
1402 1403 1404 1405 1406 1407
	if ((off_t) file->f_pos != offset) {
		/* Sanity check */
		printk("elf_core_dump: file->f_pos (%ld) != offset (%ld)\n",
		       (off_t) file->f_pos, offset);
	}

1408
end_coredump:
Linus Torvalds's avatar
Linus Torvalds committed
1409
	set_fs(fs);
1410 1411 1412 1413 1414 1415 1416 1417

cleanup:
	while(!list_empty(&thread_list)) {
		struct list_head *tmp = thread_list.next;
		list_del(tmp);
		kfree(list_entry(tmp, struct elf_thread_status, list));
	}

1418 1419 1420 1421 1422
	kfree(elf);
	kfree(prstatus);
	kfree(psinfo);
	kfree(notes);
	kfree(fpu);
1423
#ifdef ELF_CORE_COPY_XFPREGS
1424
	kfree(xfpu);
1425
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1426
	return has_dumped;
1427
#undef NUM_NOTES
Linus Torvalds's avatar
Linus Torvalds committed
1428
}
1429

Linus Torvalds's avatar
Linus Torvalds committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
#endif		/* USE_ELF_CORE_DUMP */

static int __init init_elf_binfmt(void)
{
	return register_binfmt(&elf_format);
}

static void __exit exit_elf_binfmt(void)
{
	/* Remove the COFF and ELF loaders. */
	unregister_binfmt(&elf_format);
}

module_init(init_elf_binfmt)
module_exit(exit_elf_binfmt)
Dave Jones's avatar
Dave Jones committed
1445
MODULE_LICENSE("GPL");