ia32_aout.c 14 KB

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