ia32_aout.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * a.out loader for x86-64
  3. *
  4. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  5. * Hacked together by Andi Kleen
  6. */
  7. #include <linux/module.h>
  8. #include <linux/time.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/mman.h>
  12. #include <linux/a.out.h>
  13. #include <linux/errno.h>
  14. #include <linux/signal.h>
  15. #include <linux/string.h>
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/stat.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/user.h>
  22. #include <linux/slab.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/personality.h>
  25. #include <linux/init.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/pgalloc.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/user32.h>
  31. #include <asm/ia32.h>
  32. #undef WARN_OLD
  33. #undef CORE_DUMP /* probably broken */
  34. static int load_aout_binary(struct linux_binprm *, struct pt_regs * regs);
  35. static int load_aout_library(struct file*);
  36. #ifdef CORE_DUMP
  37. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit);
  38. /*
  39. * fill in the user structure for a core dump..
  40. */
  41. static void dump_thread32(struct pt_regs * regs, struct user32 * dump)
  42. {
  43. u32 fs,gs;
  44. /* changed the size calculations - should hopefully work better. lbt */
  45. dump->magic = CMAGIC;
  46. dump->start_code = 0;
  47. dump->start_stack = regs->rsp & ~(PAGE_SIZE - 1);
  48. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  49. dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  50. dump->u_dsize -= dump->u_tsize;
  51. dump->u_ssize = 0;
  52. dump->u_debugreg[0] = current->thread.debugreg0;
  53. dump->u_debugreg[1] = current->thread.debugreg1;
  54. dump->u_debugreg[2] = current->thread.debugreg2;
  55. dump->u_debugreg[3] = current->thread.debugreg3;
  56. dump->u_debugreg[4] = 0;
  57. dump->u_debugreg[5] = 0;
  58. dump->u_debugreg[6] = current->thread.debugreg6;
  59. dump->u_debugreg[7] = current->thread.debugreg7;
  60. if (dump->start_stack < 0xc0000000)
  61. dump->u_ssize = ((unsigned long) (0xc0000000 - dump->start_stack)) >> PAGE_SHIFT;
  62. dump->regs.ebx = regs->rbx;
  63. dump->regs.ecx = regs->rcx;
  64. dump->regs.edx = regs->rdx;
  65. dump->regs.esi = regs->rsi;
  66. dump->regs.edi = regs->rdi;
  67. dump->regs.ebp = regs->rbp;
  68. dump->regs.eax = regs->rax;
  69. dump->regs.ds = current->thread.ds;
  70. dump->regs.es = current->thread.es;
  71. asm("movl %%fs,%0" : "=r" (fs)); dump->regs.fs = fs;
  72. asm("movl %%gs,%0" : "=r" (gs)); dump->regs.gs = gs;
  73. dump->regs.orig_eax = regs->orig_rax;
  74. dump->regs.eip = regs->rip;
  75. dump->regs.cs = regs->cs;
  76. dump->regs.eflags = regs->eflags;
  77. dump->regs.esp = regs->rsp;
  78. dump->regs.ss = regs->ss;
  79. #if 1 /* FIXME */
  80. dump->u_fpvalid = 0;
  81. #else
  82. dump->u_fpvalid = dump_fpu (regs, &dump->i387);
  83. #endif
  84. }
  85. #endif
  86. static struct linux_binfmt aout_format = {
  87. .module = THIS_MODULE,
  88. .load_binary = load_aout_binary,
  89. .load_shlib = load_aout_library,
  90. #ifdef CORE_DUMP
  91. .core_dump = aout_core_dump,
  92. #endif
  93. .min_coredump = PAGE_SIZE
  94. };
  95. static void set_brk(unsigned long start, unsigned long end)
  96. {
  97. start = PAGE_ALIGN(start);
  98. end = PAGE_ALIGN(end);
  99. if (end <= start)
  100. return;
  101. down_write(&current->mm->mmap_sem);
  102. do_brk(start, end - start);
  103. up_write(&current->mm->mmap_sem);
  104. }
  105. #ifdef CORE_DUMP
  106. /*
  107. * These are the only things you should do on a core-file: use only these
  108. * macros to write out all the necessary info.
  109. */
  110. static int dump_write(struct file *file, const void *addr, int nr)
  111. {
  112. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  113. }
  114. #define DUMP_WRITE(addr, nr) \
  115. if (!dump_write(file, (void *)(addr), (nr))) \
  116. goto end_coredump;
  117. #define DUMP_SEEK(offset) \
  118. if (file->f_op->llseek) { \
  119. if (file->f_op->llseek(file,(offset),0) != (offset)) \
  120. goto end_coredump; \
  121. } else file->f_pos = (offset)
  122. /*
  123. * Routine writes a core dump image in the current directory.
  124. * Currently only a stub-function.
  125. *
  126. * Note that setuid/setgid files won't make a core-dump if the uid/gid
  127. * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  128. * field, which also makes sure the core-dumps won't be recursive if the
  129. * dumping of the process results in another error..
  130. */
  131. static int aout_core_dump(long signr, struct pt_regs *regs, struct file *file, unsigned long limit)
  132. {
  133. mm_segment_t fs;
  134. int has_dumped = 0;
  135. unsigned long dump_start, dump_size;
  136. struct user32 dump;
  137. # define START_DATA(u) (u.u_tsize << PAGE_SHIFT)
  138. # define START_STACK(u) (u.start_stack)
  139. fs = get_fs();
  140. set_fs(KERNEL_DS);
  141. has_dumped = 1;
  142. current->flags |= PF_DUMPCORE;
  143. strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  144. dump.u_ar0 = (u32)(((unsigned long)(&dump.regs)) - ((unsigned long)(&dump)));
  145. dump.signal = signr;
  146. dump_thread32(regs, &dump);
  147. /* If the size of the dump file exceeds the rlimit, then see what would happen
  148. if we wrote the stack, but not the data area. */
  149. if ((dump.u_dsize + dump.u_ssize + 1) * PAGE_SIZE > limit)
  150. dump.u_dsize = 0;
  151. /* Make sure we have enough room to write the stack and data areas. */
  152. if ((dump.u_ssize + 1) * PAGE_SIZE > limit)
  153. dump.u_ssize = 0;
  154. /* make sure we actually have a data and stack area to dump */
  155. set_fs(USER_DS);
  156. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump), dump.u_dsize << PAGE_SHIFT))
  157. dump.u_dsize = 0;
  158. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump), dump.u_ssize << PAGE_SHIFT))
  159. dump.u_ssize = 0;
  160. set_fs(KERNEL_DS);
  161. /* struct user */
  162. DUMP_WRITE(&dump,sizeof(dump));
  163. /* Now dump all of the user data. Include malloced stuff as well */
  164. DUMP_SEEK(PAGE_SIZE);
  165. /* now we start writing out the user space info */
  166. set_fs(USER_DS);
  167. /* Dump the data area */
  168. if (dump.u_dsize != 0) {
  169. dump_start = START_DATA(dump);
  170. dump_size = dump.u_dsize << PAGE_SHIFT;
  171. DUMP_WRITE(dump_start,dump_size);
  172. }
  173. /* Now prepare to dump the stack area */
  174. if (dump.u_ssize != 0) {
  175. dump_start = START_STACK(dump);
  176. dump_size = dump.u_ssize << PAGE_SHIFT;
  177. DUMP_WRITE(dump_start,dump_size);
  178. }
  179. /* Finally dump the task struct. Not be used by gdb, but could be useful */
  180. set_fs(KERNEL_DS);
  181. DUMP_WRITE(current,sizeof(*current));
  182. end_coredump:
  183. set_fs(fs);
  184. return has_dumped;
  185. }
  186. #endif
  187. /*
  188. * create_aout_tables() parses the env- and arg-strings in new user
  189. * memory and creates the pointer tables from them, and puts their
  190. * addresses on the "stack", returning the new stack pointer value.
  191. */
  192. static u32 __user *create_aout_tables(char __user *p, struct linux_binprm *bprm)
  193. {
  194. u32 __user *argv;
  195. u32 __user *envp;
  196. u32 __user *sp;
  197. int argc = bprm->argc;
  198. int envc = bprm->envc;
  199. sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
  200. sp -= envc+1;
  201. envp = sp;
  202. sp -= argc+1;
  203. argv = sp;
  204. put_user((unsigned long) envp,--sp);
  205. put_user((unsigned long) argv,--sp);
  206. put_user(argc,--sp);
  207. current->mm->arg_start = (unsigned long) p;
  208. while (argc-->0) {
  209. char c;
  210. put_user((u32)(unsigned long)p,argv++);
  211. do {
  212. get_user(c,p++);
  213. } while (c);
  214. }
  215. put_user(0, argv);
  216. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  217. while (envc-->0) {
  218. char c;
  219. put_user((u32)(unsigned long)p,envp++);
  220. do {
  221. get_user(c,p++);
  222. } while (c);
  223. }
  224. put_user(0, envp);
  225. current->mm->env_end = (unsigned long) p;
  226. return sp;
  227. }
  228. /*
  229. * These are the functions used to load a.out style executables and shared
  230. * libraries. There is no binary dependent code anywhere else.
  231. */
  232. static int load_aout_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  233. {
  234. struct exec ex;
  235. unsigned long error;
  236. unsigned long fd_offset;
  237. unsigned long rlim;
  238. int retval;
  239. ex = *((struct exec *) bprm->buf); /* exec-header */
  240. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  241. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  242. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  243. i_size_read(bprm->file->f_path.dentry->d_inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  244. return -ENOEXEC;
  245. }
  246. fd_offset = N_TXTOFF(ex);
  247. /* Check initial limits. This avoids letting people circumvent
  248. * size limits imposed on them by creating programs with large
  249. * arrays in the data or bss.
  250. */
  251. rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
  252. if (rlim >= RLIM_INFINITY)
  253. rlim = ~0;
  254. if (ex.a_data + ex.a_bss > rlim)
  255. return -ENOMEM;
  256. /* Flush all traces of the currently running executable */
  257. retval = flush_old_exec(bprm);
  258. if (retval)
  259. return retval;
  260. regs->cs = __USER32_CS;
  261. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  262. regs->r13 = regs->r14 = regs->r15 = 0;
  263. /* OK, This is the point of no return */
  264. set_personality(PER_LINUX);
  265. set_thread_flag(TIF_IA32);
  266. clear_thread_flag(TIF_ABI_PENDING);
  267. current->mm->end_code = ex.a_text +
  268. (current->mm->start_code = N_TXTADDR(ex));
  269. current->mm->end_data = ex.a_data +
  270. (current->mm->start_data = N_DATADDR(ex));
  271. current->mm->brk = ex.a_bss +
  272. (current->mm->start_brk = N_BSSADDR(ex));
  273. current->mm->free_area_cache = TASK_UNMAPPED_BASE;
  274. current->mm->cached_hole_size = 0;
  275. current->mm->mmap = NULL;
  276. compute_creds(bprm);
  277. current->flags &= ~PF_FORKNOEXEC;
  278. if (N_MAGIC(ex) == OMAGIC) {
  279. unsigned long text_addr, map_size;
  280. loff_t pos;
  281. text_addr = N_TXTADDR(ex);
  282. pos = 32;
  283. map_size = ex.a_text+ex.a_data;
  284. down_write(&current->mm->mmap_sem);
  285. error = do_brk(text_addr & PAGE_MASK, map_size);
  286. up_write(&current->mm->mmap_sem);
  287. if (error != (text_addr & PAGE_MASK)) {
  288. send_sig(SIGKILL, current, 0);
  289. return error;
  290. }
  291. error = bprm->file->f_op->read(bprm->file,
  292. (char __user *)text_addr,
  293. ex.a_text+ex.a_data, &pos);
  294. if ((signed long)error < 0) {
  295. send_sig(SIGKILL, current, 0);
  296. return error;
  297. }
  298. flush_icache_range(text_addr, text_addr+ex.a_text+ex.a_data);
  299. } else {
  300. #ifdef WARN_OLD
  301. static unsigned long error_time, error_time2;
  302. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  303. (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time2) > 5*HZ)
  304. {
  305. printk(KERN_NOTICE "executable not page aligned\n");
  306. error_time2 = jiffies;
  307. }
  308. if ((fd_offset & ~PAGE_MASK) != 0 &&
  309. (jiffies-error_time) > 5*HZ)
  310. {
  311. printk(KERN_WARNING
  312. "fd_offset is not page aligned. Please convert program: %s\n",
  313. bprm->file->f_path.dentry->d_name.name);
  314. error_time = jiffies;
  315. }
  316. #endif
  317. if (!bprm->file->f_op->mmap||((fd_offset & ~PAGE_MASK) != 0)) {
  318. loff_t pos = fd_offset;
  319. down_write(&current->mm->mmap_sem);
  320. do_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  321. up_write(&current->mm->mmap_sem);
  322. bprm->file->f_op->read(bprm->file,
  323. (char __user *)N_TXTADDR(ex),
  324. ex.a_text+ex.a_data, &pos);
  325. flush_icache_range((unsigned long) N_TXTADDR(ex),
  326. (unsigned long) N_TXTADDR(ex) +
  327. ex.a_text+ex.a_data);
  328. goto beyond_if;
  329. }
  330. down_write(&current->mm->mmap_sem);
  331. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  332. PROT_READ | PROT_EXEC,
  333. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE | MAP_32BIT,
  334. fd_offset);
  335. up_write(&current->mm->mmap_sem);
  336. if (error != N_TXTADDR(ex)) {
  337. send_sig(SIGKILL, current, 0);
  338. return error;
  339. }
  340. down_write(&current->mm->mmap_sem);
  341. error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  342. PROT_READ | PROT_WRITE | PROT_EXEC,
  343. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE | MAP_32BIT,
  344. fd_offset + ex.a_text);
  345. up_write(&current->mm->mmap_sem);
  346. if (error != N_DATADDR(ex)) {
  347. send_sig(SIGKILL, current, 0);
  348. return error;
  349. }
  350. }
  351. beyond_if:
  352. set_binfmt(&aout_format);
  353. set_brk(current->mm->start_brk, current->mm->brk);
  354. retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
  355. if (retval < 0) {
  356. /* Someone check-me: is this error path enough? */
  357. send_sig(SIGKILL, current, 0);
  358. return retval;
  359. }
  360. current->mm->start_stack =
  361. (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
  362. /* start thread */
  363. asm volatile("movl %0,%%fs" :: "r" (0)); \
  364. asm volatile("movl %0,%%es; movl %0,%%ds": :"r" (__USER32_DS));
  365. load_gs_index(0);
  366. (regs)->rip = ex.a_entry;
  367. (regs)->rsp = current->mm->start_stack;
  368. (regs)->eflags = 0x200;
  369. (regs)->cs = __USER32_CS;
  370. (regs)->ss = __USER32_DS;
  371. regs->r8 = regs->r9 = regs->r10 = regs->r11 =
  372. regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
  373. set_fs(USER_DS);
  374. if (unlikely(current->ptrace & PT_PTRACED)) {
  375. if (current->ptrace & PT_TRACE_EXEC)
  376. ptrace_notify ((PTRACE_EVENT_EXEC << 8) | SIGTRAP);
  377. else
  378. send_sig(SIGTRAP, current, 0);
  379. }
  380. return 0;
  381. }
  382. static int load_aout_library(struct file *file)
  383. {
  384. struct inode * inode;
  385. unsigned long bss, start_addr, len;
  386. unsigned long error;
  387. int retval;
  388. struct exec ex;
  389. inode = file->f_path.dentry->d_inode;
  390. retval = -ENOEXEC;
  391. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  392. if (error != sizeof(ex))
  393. goto out;
  394. /* We come in here for the regular a.out style of shared libraries */
  395. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  396. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  397. i_size_read(inode) < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  398. goto out;
  399. }
  400. if (N_FLAGS(ex))
  401. goto out;
  402. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  403. this off to get the starting address for the page */
  404. start_addr = ex.a_entry & 0xfffff000;
  405. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  406. loff_t pos = N_TXTOFF(ex);
  407. #ifdef WARN_OLD
  408. static unsigned long error_time;
  409. if ((jiffies-error_time) > 5*HZ)
  410. {
  411. printk(KERN_WARNING
  412. "N_TXTOFF is not page aligned. Please convert library: %s\n",
  413. file->f_path.dentry->d_name.name);
  414. error_time = jiffies;
  415. }
  416. #endif
  417. down_write(&current->mm->mmap_sem);
  418. do_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  419. up_write(&current->mm->mmap_sem);
  420. file->f_op->read(file, (char __user *)start_addr,
  421. ex.a_text + ex.a_data, &pos);
  422. flush_icache_range((unsigned long) start_addr,
  423. (unsigned long) start_addr + ex.a_text + ex.a_data);
  424. retval = 0;
  425. goto out;
  426. }
  427. /* Now use mmap to map the library into memory. */
  428. down_write(&current->mm->mmap_sem);
  429. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  430. PROT_READ | PROT_WRITE | PROT_EXEC,
  431. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
  432. N_TXTOFF(ex));
  433. up_write(&current->mm->mmap_sem);
  434. retval = error;
  435. if (error != start_addr)
  436. goto out;
  437. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  438. bss = ex.a_text + ex.a_data + ex.a_bss;
  439. if (bss > len) {
  440. down_write(&current->mm->mmap_sem);
  441. error = do_brk(start_addr + len, bss - len);
  442. up_write(&current->mm->mmap_sem);
  443. retval = error;
  444. if (error != start_addr + len)
  445. goto out;
  446. }
  447. retval = 0;
  448. out:
  449. return retval;
  450. }
  451. static int __init init_aout_binfmt(void)
  452. {
  453. return register_binfmt(&aout_format);
  454. }
  455. static void __exit exit_aout_binfmt(void)
  456. {
  457. unregister_binfmt(&aout_format);
  458. }
  459. module_init(init_aout_binfmt);
  460. module_exit(exit_aout_binfmt);
  461. MODULE_LICENSE("GPL");