ia32_aout.c 14 KB

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