ia32_aout.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <linux/perf_event.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. static int load_aout_binary(struct linux_binprm *);
  34. static int load_aout_library(struct file *);
  35. #ifdef CONFIG_COREDUMP
  36. static int aout_core_dump(struct coredump_params *);
  37. static unsigned long get_dr(int n)
  38. {
  39. struct perf_event *bp = current->thread.ptrace_bps[n];
  40. return bp ? bp->hw.info.address : 0;
  41. }
  42. /*
  43. * fill in the user structure for a core dump..
  44. */
  45. static void fill_dump(struct pt_regs *regs, struct user32 *dump)
  46. {
  47. u32 fs, gs;
  48. memset(dump, 0, sizeof(*dump));
  49. /* changed the size calculations - should hopefully work better. lbt */
  50. dump->magic = CMAGIC;
  51. dump->start_code = 0;
  52. dump->start_stack = regs->sp & ~(PAGE_SIZE - 1);
  53. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  54. dump->u_dsize = ((unsigned long)
  55. (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  56. dump->u_dsize -= dump->u_tsize;
  57. dump->u_debugreg[0] = get_dr(0);
  58. dump->u_debugreg[1] = get_dr(1);
  59. dump->u_debugreg[2] = get_dr(2);
  60. dump->u_debugreg[3] = get_dr(3);
  61. dump->u_debugreg[6] = current->thread.debugreg6;
  62. dump->u_debugreg[7] = current->thread.ptrace_dr7;
  63. if (dump->start_stack < 0xc0000000) {
  64. unsigned long tmp;
  65. tmp = (unsigned long) (0xc0000000 - dump->start_stack);
  66. dump->u_ssize = tmp >> PAGE_SHIFT;
  67. }
  68. dump->regs.ebx = regs->bx;
  69. dump->regs.ecx = regs->cx;
  70. dump->regs.edx = regs->dx;
  71. dump->regs.esi = regs->si;
  72. dump->regs.edi = regs->di;
  73. dump->regs.ebp = regs->bp;
  74. dump->regs.eax = regs->ax;
  75. dump->regs.ds = current->thread.ds;
  76. dump->regs.es = current->thread.es;
  77. savesegment(fs, fs);
  78. dump->regs.fs = fs;
  79. savesegment(gs, gs);
  80. dump->regs.gs = gs;
  81. dump->regs.orig_eax = regs->orig_ax;
  82. dump->regs.eip = regs->ip;
  83. dump->regs.cs = regs->cs;
  84. dump->regs.eflags = regs->flags;
  85. dump->regs.esp = regs->sp;
  86. dump->regs.ss = regs->ss;
  87. #if 1 /* FIXME */
  88. dump->u_fpvalid = 0;
  89. #else
  90. dump->u_fpvalid = dump_fpu(regs, &dump->i387);
  91. #endif
  92. }
  93. #endif
  94. static struct linux_binfmt aout_format = {
  95. .module = THIS_MODULE,
  96. .load_binary = load_aout_binary,
  97. .load_shlib = load_aout_library,
  98. #ifdef CONFIG_COREDUMP
  99. .core_dump = aout_core_dump,
  100. #endif
  101. .min_coredump = PAGE_SIZE
  102. };
  103. static void set_brk(unsigned long start, unsigned long end)
  104. {
  105. start = PAGE_ALIGN(start);
  106. end = PAGE_ALIGN(end);
  107. if (end <= start)
  108. return;
  109. vm_brk(start, end - start);
  110. }
  111. #ifdef CONFIG_COREDUMP
  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 START_DATA(u) (u.u_tsize << PAGE_SHIFT)
  118. #define START_STACK(u) (u.start_stack)
  119. /*
  120. * Routine writes a core dump image in the current directory.
  121. * Currently only a stub-function.
  122. *
  123. * Note that setuid/setgid files won't make a core-dump if the uid/gid
  124. * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  125. * field, which also makes sure the core-dumps won't be recursive if the
  126. * dumping of the process results in another error..
  127. */
  128. static int aout_core_dump(struct coredump_params *cprm)
  129. {
  130. mm_segment_t fs;
  131. int has_dumped = 0;
  132. unsigned long dump_start, dump_size;
  133. struct user32 dump;
  134. fs = get_fs();
  135. set_fs(KERNEL_DS);
  136. has_dumped = 1;
  137. fill_dump(cprm->regs, &dump);
  138. strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  139. dump.u_ar0 = offsetof(struct user32, regs);
  140. dump.signal = cprm->siginfo->si_signo;
  141. /*
  142. * If the size of the dump file exceeds the rlimit, then see
  143. * what would happen if we wrote the stack, but not the data
  144. * area.
  145. */
  146. if ((dump.u_dsize + dump.u_ssize + 1) * PAGE_SIZE > cprm->limit)
  147. dump.u_dsize = 0;
  148. /* Make sure we have enough room to write the stack and data areas. */
  149. if ((dump.u_ssize + 1) * PAGE_SIZE > cprm->limit)
  150. dump.u_ssize = 0;
  151. /* make sure we actually have a data and stack area to dump */
  152. set_fs(USER_DS);
  153. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_DATA(dump),
  154. dump.u_dsize << PAGE_SHIFT))
  155. dump.u_dsize = 0;
  156. if (!access_ok(VERIFY_READ, (void *) (unsigned long)START_STACK(dump),
  157. dump.u_ssize << PAGE_SHIFT))
  158. dump.u_ssize = 0;
  159. set_fs(KERNEL_DS);
  160. /* struct user */
  161. if (!dump_emit(cprm, &dump, sizeof(dump)))
  162. goto end_coredump;
  163. /* Now dump all of the user data. Include malloced stuff as well */
  164. if (!dump_skip(cprm, PAGE_SIZE - sizeof(dump)))
  165. goto end_coredump;
  166. /* now we start writing out the user space info */
  167. set_fs(USER_DS);
  168. /* Dump the data area */
  169. if (dump.u_dsize != 0) {
  170. dump_start = START_DATA(dump);
  171. dump_size = dump.u_dsize << PAGE_SHIFT;
  172. if (!dump_emit(cprm, (void *)dump_start, dump_size))
  173. goto end_coredump;
  174. }
  175. /* Now prepare to dump the stack area */
  176. if (dump.u_ssize != 0) {
  177. dump_start = START_STACK(dump);
  178. dump_size = dump.u_ssize << PAGE_SHIFT;
  179. if (!dump_emit(cprm, (void *)dump_start, dump_size))
  180. goto end_coredump;
  181. }
  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, *envp, *sp;
  195. int argc = bprm->argc, envc = bprm->envc;
  196. sp = (u32 __user *) ((-(unsigned long)sizeof(u32)) & (unsigned long) p);
  197. sp -= envc+1;
  198. envp = sp;
  199. sp -= argc+1;
  200. argv = sp;
  201. put_user((unsigned long) envp, --sp);
  202. put_user((unsigned long) argv, --sp);
  203. put_user(argc, --sp);
  204. current->mm->arg_start = (unsigned long) p;
  205. while (argc-- > 0) {
  206. char c;
  207. put_user((u32)(unsigned long)p, argv++);
  208. do {
  209. get_user(c, p++);
  210. } while (c);
  211. }
  212. put_user(0, argv);
  213. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  214. while (envc-- > 0) {
  215. char c;
  216. put_user((u32)(unsigned long)p, envp++);
  217. do {
  218. get_user(c, p++);
  219. } while (c);
  220. }
  221. put_user(0, envp);
  222. current->mm->env_end = (unsigned long) p;
  223. return sp;
  224. }
  225. /*
  226. * These are the functions used to load a.out style executables and shared
  227. * libraries. There is no binary dependent code anywhere else.
  228. */
  229. static int load_aout_binary(struct linux_binprm *bprm)
  230. {
  231. unsigned long error, fd_offset, rlim;
  232. struct pt_regs *regs = current_pt_regs();
  233. struct exec ex;
  234. int retval;
  235. ex = *((struct exec *) bprm->buf); /* exec-header */
  236. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  237. N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  238. N_TRSIZE(ex) || N_DRSIZE(ex) ||
  239. i_size_read(file_inode(bprm->file)) <
  240. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  241. return -ENOEXEC;
  242. }
  243. fd_offset = N_TXTOFF(ex);
  244. /* Check initial limits. This avoids letting people circumvent
  245. * size limits imposed on them by creating programs with large
  246. * arrays in the data or bss.
  247. */
  248. rlim = rlimit(RLIMIT_DATA);
  249. if (rlim >= RLIM_INFINITY)
  250. rlim = ~0;
  251. if (ex.a_data + ex.a_bss > rlim)
  252. return -ENOMEM;
  253. /* Flush all traces of the currently running executable */
  254. retval = flush_old_exec(bprm);
  255. if (retval)
  256. return retval;
  257. /* OK, This is the point of no return */
  258. set_personality(PER_LINUX);
  259. set_personality_ia32(false);
  260. setup_new_exec(bprm);
  261. regs->cs = __USER32_CS;
  262. regs->r8 = regs->r9 = regs->r10 = regs->r11 = regs->r12 =
  263. regs->r13 = regs->r14 = regs->r15 = 0;
  264. current->mm->end_code = ex.a_text +
  265. (current->mm->start_code = N_TXTADDR(ex));
  266. current->mm->end_data = ex.a_data +
  267. (current->mm->start_data = N_DATADDR(ex));
  268. current->mm->brk = ex.a_bss +
  269. (current->mm->start_brk = N_BSSADDR(ex));
  270. retval = setup_arg_pages(bprm, IA32_STACK_TOP, EXSTACK_DEFAULT);
  271. if (retval < 0)
  272. return retval;
  273. install_exec_creds(bprm);
  274. if (N_MAGIC(ex) == OMAGIC) {
  275. unsigned long text_addr, map_size;
  276. text_addr = N_TXTADDR(ex);
  277. map_size = ex.a_text+ex.a_data;
  278. error = vm_brk(text_addr & PAGE_MASK, map_size);
  279. if (error != (text_addr & PAGE_MASK))
  280. return error;
  281. error = read_code(bprm->file, text_addr, 32,
  282. ex.a_text + ex.a_data);
  283. if ((signed long)error < 0)
  284. return error;
  285. } else {
  286. #ifdef WARN_OLD
  287. static unsigned long error_time, error_time2;
  288. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  289. (N_MAGIC(ex) != NMAGIC) &&
  290. time_after(jiffies, error_time2 + 5*HZ)) {
  291. printk(KERN_NOTICE "executable not page aligned\n");
  292. error_time2 = jiffies;
  293. }
  294. if ((fd_offset & ~PAGE_MASK) != 0 &&
  295. time_after(jiffies, error_time + 5*HZ)) {
  296. printk(KERN_WARNING
  297. "fd_offset is not page aligned. Please convert "
  298. "program: %pD\n",
  299. bprm->file);
  300. error_time = jiffies;
  301. }
  302. #endif
  303. if (!bprm->file->f_op->mmap || (fd_offset & ~PAGE_MASK) != 0) {
  304. vm_brk(N_TXTADDR(ex), ex.a_text+ex.a_data);
  305. read_code(bprm->file, N_TXTADDR(ex), fd_offset,
  306. ex.a_text+ex.a_data);
  307. goto beyond_if;
  308. }
  309. error = vm_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  310. PROT_READ | PROT_EXEC,
  311. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  312. MAP_EXECUTABLE | MAP_32BIT,
  313. fd_offset);
  314. if (error != N_TXTADDR(ex))
  315. return error;
  316. error = vm_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  317. PROT_READ | PROT_WRITE | PROT_EXEC,
  318. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE |
  319. MAP_EXECUTABLE | MAP_32BIT,
  320. fd_offset + ex.a_text);
  321. if (error != N_DATADDR(ex))
  322. return error;
  323. }
  324. beyond_if:
  325. set_binfmt(&aout_format);
  326. set_brk(current->mm->start_brk, current->mm->brk);
  327. current->mm->start_stack =
  328. (unsigned long)create_aout_tables((char __user *)bprm->p, bprm);
  329. /* start thread */
  330. loadsegment(fs, 0);
  331. loadsegment(ds, __USER32_DS);
  332. loadsegment(es, __USER32_DS);
  333. load_gs_index(0);
  334. (regs)->ip = ex.a_entry;
  335. (regs)->sp = current->mm->start_stack;
  336. (regs)->flags = 0x200;
  337. (regs)->cs = __USER32_CS;
  338. (regs)->ss = __USER32_DS;
  339. regs->r8 = regs->r9 = regs->r10 = regs->r11 =
  340. regs->r12 = regs->r13 = regs->r14 = regs->r15 = 0;
  341. set_fs(USER_DS);
  342. return 0;
  343. }
  344. static int load_aout_library(struct file *file)
  345. {
  346. unsigned long bss, start_addr, len, error;
  347. int retval;
  348. struct exec ex;
  349. retval = -ENOEXEC;
  350. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  351. if (error != sizeof(ex))
  352. goto out;
  353. /* We come in here for the regular a.out style of shared libraries */
  354. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  355. N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  356. i_size_read(file_inode(file)) <
  357. ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  358. goto out;
  359. }
  360. if (N_FLAGS(ex))
  361. goto out;
  362. /* For QMAGIC, the starting address is 0x20 into the page. We mask
  363. this off to get the starting address for the page */
  364. start_addr = ex.a_entry & 0xfffff000;
  365. if ((N_TXTOFF(ex) & ~PAGE_MASK) != 0) {
  366. #ifdef WARN_OLD
  367. static unsigned long error_time;
  368. if (time_after(jiffies, error_time + 5*HZ)) {
  369. printk(KERN_WARNING
  370. "N_TXTOFF is not page aligned. Please convert "
  371. "library: %pD\n",
  372. file);
  373. error_time = jiffies;
  374. }
  375. #endif
  376. vm_brk(start_addr, ex.a_text + ex.a_data + ex.a_bss);
  377. read_code(file, start_addr, N_TXTOFF(ex),
  378. ex.a_text + ex.a_data);
  379. retval = 0;
  380. goto out;
  381. }
  382. /* Now use mmap to map the library into memory. */
  383. error = vm_mmap(file, start_addr, ex.a_text + ex.a_data,
  384. PROT_READ | PROT_WRITE | PROT_EXEC,
  385. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_32BIT,
  386. N_TXTOFF(ex));
  387. retval = error;
  388. if (error != start_addr)
  389. goto out;
  390. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  391. bss = ex.a_text + ex.a_data + ex.a_bss;
  392. if (bss > len) {
  393. error = vm_brk(start_addr + len, bss - len);
  394. retval = error;
  395. if (error != start_addr + len)
  396. goto out;
  397. }
  398. retval = 0;
  399. out:
  400. return retval;
  401. }
  402. static int __init init_aout_binfmt(void)
  403. {
  404. register_binfmt(&aout_format);
  405. return 0;
  406. }
  407. static void __exit exit_aout_binfmt(void)
  408. {
  409. unregister_binfmt(&aout_format);
  410. }
  411. module_init(init_aout_binfmt);
  412. module_exit(exit_aout_binfmt);
  413. MODULE_LICENSE("GPL");