binfmt_aout32.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:11k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/fs/binfmt_aout.c
  3.  *
  4.  *  Copyright (C) 1991, 1992, 1996  Linus Torvalds
  5.  *
  6.  *  Hacked a bit by DaveM to make it work with 32-bit SunOS
  7.  *  binaries on the sparc64 port.
  8.  */
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/mman.h>
  14. #include <linux/a.out.h>
  15. #include <linux/errno.h>
  16. #include <linux/signal.h>
  17. #include <linux/string.h>
  18. #include <linux/fs.h>
  19. #include <linux/file.h>
  20. #include <linux/stat.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/user.h>
  24. #include <linux/slab.h>
  25. #include <linux/binfmts.h>
  26. #include <linux/personality.h>
  27. #include <linux/init.h>
  28. #include <asm/system.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgalloc.h>
  31. static int load_aout32_binary(struct linux_binprm *, struct pt_regs * regs);
  32. static int load_aout32_library(struct file*);
  33. static int aout32_core_dump(long signr, struct pt_regs * regs, struct file *file);
  34. extern void dump_thread(struct pt_regs *, struct user *);
  35. static struct linux_binfmt aout32_format = {
  36. NULL, THIS_MODULE, load_aout32_binary, load_aout32_library, aout32_core_dump,
  37. PAGE_SIZE
  38. };
  39. static void set_brk(unsigned long start, unsigned long end)
  40. {
  41. start = PAGE_ALIGN(start);
  42. end = PAGE_ALIGN(end);
  43. if (end <= start)
  44. return;
  45. do_brk(start, end - start);
  46. }
  47. /*
  48.  * These are the only things you should do on a core-file: use only these
  49.  * macros to write out all the necessary info.
  50.  */
  51. static int dump_write(struct file *file, const void *addr, int nr)
  52. {
  53. return file->f_op->write(file, addr, nr, &file->f_pos) == nr;
  54. }
  55. #define DUMP_WRITE(addr, nr)
  56. if (!dump_write(file, (void *)(addr), (nr))) 
  57. goto end_coredump;
  58. #define DUMP_SEEK(offset) 
  59. if (file->f_op->llseek) { 
  60. if (file->f_op->llseek(file,(offset),0) != (offset)) 
  61.   goto end_coredump; 
  62. } else file->f_pos = (offset)
  63. /*
  64.  * Routine writes a core dump image in the current directory.
  65.  * Currently only a stub-function.
  66.  *
  67.  * Note that setuid/setgid files won't make a core-dump if the uid/gid
  68.  * changed due to the set[u|g]id. It's enforced by the "current->mm->dumpable"
  69.  * field, which also makes sure the core-dumps won't be recursive if the
  70.  * dumping of the process results in another error..
  71.  */
  72. static int aout32_core_dump(long signr, struct pt_regs *regs, struct file *file)
  73. {
  74. mm_segment_t fs;
  75. int has_dumped = 0;
  76. unsigned long dump_start, dump_size;
  77. struct user dump;
  78. #       define START_DATA(u)    (u.u_tsize)
  79. #       define START_STACK(u)   ((regs->u_regs[UREG_FP]) & ~(PAGE_SIZE - 1))
  80. fs = get_fs();
  81. set_fs(KERNEL_DS);
  82. has_dumped = 1;
  83. current->flags |= PF_DUMPCORE;
  84.         strncpy(dump.u_comm, current->comm, sizeof(current->comm));
  85. dump.signal = signr;
  86. dump_thread(regs, &dump);
  87. /* If the size of the dump file exceeds the rlimit, then see what would happen
  88.    if we wrote the stack, but not the data area.  */
  89. if ((dump.u_dsize+dump.u_ssize) >
  90.     current->rlim[RLIMIT_CORE].rlim_cur)
  91. dump.u_dsize = 0;
  92. /* Make sure we have enough room to write the stack and data areas. */
  93. if ((dump.u_ssize) >
  94.     current->rlim[RLIMIT_CORE].rlim_cur)
  95. dump.u_ssize = 0;
  96. /* make sure we actually have a data and stack area to dump */
  97. set_fs(USER_DS);
  98. if (verify_area(VERIFY_READ, (void *) START_DATA(dump), dump.u_dsize))
  99. dump.u_dsize = 0;
  100. if (verify_area(VERIFY_READ, (void *) START_STACK(dump), dump.u_ssize))
  101. dump.u_ssize = 0;
  102. set_fs(KERNEL_DS);
  103. /* struct user */
  104. DUMP_WRITE(&dump,sizeof(dump));
  105. /* now we start writing out the user space info */
  106. set_fs(USER_DS);
  107. /* Dump the data area */
  108. if (dump.u_dsize != 0) {
  109. dump_start = START_DATA(dump);
  110. dump_size = dump.u_dsize;
  111. DUMP_WRITE(dump_start,dump_size);
  112. }
  113. /* Now prepare to dump the stack area */
  114. if (dump.u_ssize != 0) {
  115. dump_start = START_STACK(dump);
  116. dump_size = dump.u_ssize;
  117. DUMP_WRITE(dump_start,dump_size);
  118. }
  119. /* Finally dump the task struct.  Not be used by gdb, but could be useful */
  120. set_fs(KERNEL_DS);
  121. DUMP_WRITE(current,sizeof(*current));
  122. end_coredump:
  123. set_fs(fs);
  124. return has_dumped;
  125. }
  126. /*
  127.  * create_aout32_tables() parses the env- and arg-strings in new user
  128.  * memory and creates the pointer tables from them, and puts their
  129.  * addresses on the "stack", returning the new stack pointer value.
  130.  */
  131. #define A(__x) ((unsigned long)(__x))
  132. static u32 *create_aout32_tables(char * p, struct linux_binprm * bprm)
  133. {
  134. u32 *argv, *envp;
  135. u32 *sp;
  136. int argc = bprm->argc;
  137. int envc = bprm->envc;
  138. sp = (u32 *) ((-(unsigned long)sizeof(char *)) & (unsigned long) p);
  139. /* This imposes the proper stack alignment for a new process. */
  140. sp = (u32 *) (((unsigned long) sp) & ~7);
  141. if ((envc+argc+3)&1)
  142. --sp;
  143. sp -= envc+1;
  144. envp = (u32 *) sp;
  145. sp -= argc+1;
  146. argv = (u32 *) sp;
  147. put_user(argc,--sp);
  148. current->mm->arg_start = (unsigned long) p;
  149. while (argc-->0) {
  150. char c;
  151. put_user(((u32)A(p)),argv++);
  152. do {
  153. get_user(c,p++);
  154. } while (c);
  155. }
  156. put_user(NULL,argv);
  157. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  158. while (envc-->0) {
  159. char c;
  160. put_user(((u32)A(p)),envp++);
  161. do {
  162. get_user(c,p++);
  163. } while (c);
  164. }
  165. put_user(NULL,envp);
  166. current->mm->env_end = (unsigned long) p;
  167. return sp;
  168. }
  169. /*
  170.  * These are the functions used to load a.out style executables and shared
  171.  * libraries.  There is no binary dependent code anywhere else.
  172.  */
  173. static int load_aout32_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  174. {
  175. struct exec ex;
  176. unsigned long error;
  177. unsigned long fd_offset;
  178. unsigned long rlim;
  179. int retval;
  180. ex = *((struct exec *) bprm->buf); /* exec-header */
  181. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != OMAGIC &&
  182.      N_MAGIC(ex) != QMAGIC && N_MAGIC(ex) != NMAGIC) ||
  183.     N_TRSIZE(ex) || N_DRSIZE(ex) ||
  184.     bprm->file->f_dentry->d_inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  185. return -ENOEXEC;
  186. }
  187. fd_offset = N_TXTOFF(ex);
  188. /* Check initial limits. This avoids letting people circumvent
  189.  * size limits imposed on them by creating programs with large
  190.  * arrays in the data or bss.
  191.  */
  192. rlim = current->rlim[RLIMIT_DATA].rlim_cur;
  193. if (rlim >= RLIM_INFINITY)
  194. rlim = ~0;
  195. if (ex.a_data + ex.a_bss > rlim)
  196. return -ENOMEM;
  197. /* Flush all traces of the currently running executable */
  198. retval = flush_old_exec(bprm);
  199. if (retval)
  200. return retval;
  201. /* OK, This is the point of no return */
  202. set_personality(PER_SUNOS);
  203. current->mm->end_code = ex.a_text +
  204. (current->mm->start_code = N_TXTADDR(ex));
  205. current->mm->end_data = ex.a_data +
  206. (current->mm->start_data = N_DATADDR(ex));
  207. current->mm->brk = ex.a_bss +
  208. (current->mm->start_brk = N_BSSADDR(ex));
  209. current->mm->rss = 0;
  210. current->mm->mmap = NULL;
  211. compute_creds(bprm);
  212.   current->flags &= ~PF_FORKNOEXEC;
  213. if (N_MAGIC(ex) == NMAGIC) {
  214. loff_t pos = fd_offset;
  215. /* Fuck me plenty... */
  216. error = do_brk(N_TXTADDR(ex), ex.a_text);
  217. bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
  218.   ex.a_text, &pos);
  219. error = do_brk(N_DATADDR(ex), ex.a_data);
  220. bprm->file->f_op->read(bprm->file, (char *) N_DATADDR(ex),
  221.   ex.a_data, &pos);
  222. goto beyond_if;
  223. }
  224. if (N_MAGIC(ex) == OMAGIC) {
  225. loff_t pos = fd_offset;
  226. do_brk(N_TXTADDR(ex) & PAGE_MASK,
  227. ex.a_text+ex.a_data + PAGE_SIZE - 1);
  228. bprm->file->f_op->read(bprm->file, (char *) N_TXTADDR(ex),
  229.   ex.a_text+ex.a_data, &pos);
  230. } else {
  231. static unsigned long error_time;
  232. if ((ex.a_text & 0xfff || ex.a_data & 0xfff) &&
  233.     (N_MAGIC(ex) != NMAGIC) && (jiffies-error_time) > 5*HZ)
  234. {
  235. printk(KERN_NOTICE "executable not page alignedn");
  236. error_time = jiffies;
  237. }
  238. if (!bprm->file->f_op->mmap) {
  239. loff_t pos = fd_offset;
  240. do_brk(0, ex.a_text+ex.a_data);
  241. bprm->file->f_op->read(bprm->file,(char *)N_TXTADDR(ex),
  242.   ex.a_text+ex.a_data, &pos);
  243. goto beyond_if;
  244. }
  245.         down_write(&current->mm->mmap_sem);
  246. error = do_mmap(bprm->file, N_TXTADDR(ex), ex.a_text,
  247. PROT_READ | PROT_EXEC,
  248. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  249. fd_offset);
  250.         up_write(&current->mm->mmap_sem);
  251. if (error != N_TXTADDR(ex)) {
  252. send_sig(SIGKILL, current, 0);
  253. return error;
  254. }
  255.         down_write(&current->mm->mmap_sem);
  256.   error = do_mmap(bprm->file, N_DATADDR(ex), ex.a_data,
  257. PROT_READ | PROT_WRITE | PROT_EXEC,
  258. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE,
  259. fd_offset + ex.a_text);
  260.         up_write(&current->mm->mmap_sem);
  261. if (error != N_DATADDR(ex)) {
  262. send_sig(SIGKILL, current, 0);
  263. return error;
  264. }
  265. }
  266. beyond_if:
  267. set_binfmt(&aout32_format);
  268. set_brk(current->mm->start_brk, current->mm->brk);
  269. retval = setup_arg_pages(bprm);
  270. if (retval < 0) { 
  271. /* Someone check-me: is this error path enough? */ 
  272. send_sig(SIGKILL, current, 0); 
  273. return retval;
  274. }
  275. current->mm->start_stack =
  276. (unsigned long) create_aout32_tables((char *)bprm->p, bprm);
  277. if (!(current->thread.flags & SPARC_FLAG_32BIT)) {
  278. unsigned long pgd_cache;
  279. pgd_cache = ((unsigned long)current->mm->pgd[0])<<11UL;
  280. __asm__ __volatile__("stxat%0, [%1] %2nt"
  281.      "membar #Sync"
  282.      : /* no outputs */
  283.      : "r" (pgd_cache),
  284.        "r" (TSB_REG), "i" (ASI_DMMU));
  285. current->thread.flags |= SPARC_FLAG_32BIT;
  286. }
  287. start_thread32(regs, ex.a_entry, current->mm->start_stack);
  288. if (current->ptrace & PT_PTRACED)
  289. send_sig(SIGTRAP, current, 0);
  290. return 0;
  291. }
  292. /* N.B. Move to .h file and use code in fs/binfmt_aout.c? */
  293. static int load_aout32_library(struct file *file)
  294. {
  295. struct inode * inode;
  296. unsigned long bss, start_addr, len;
  297. unsigned long error;
  298. int retval;
  299. struct exec ex;
  300. inode = file->f_dentry->d_inode;
  301. retval = -ENOEXEC;
  302. error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
  303. if (error != sizeof(ex))
  304. goto out;
  305. /* We come in here for the regular a.out style of shared libraries */
  306. if ((N_MAGIC(ex) != ZMAGIC && N_MAGIC(ex) != QMAGIC) || N_TRSIZE(ex) ||
  307.     N_DRSIZE(ex) || ((ex.a_entry & 0xfff) && N_MAGIC(ex) == ZMAGIC) ||
  308.     inode->i_size < ex.a_text+ex.a_data+N_SYMSIZE(ex)+N_TXTOFF(ex)) {
  309. goto out;
  310. }
  311. if (N_MAGIC(ex) == ZMAGIC && N_TXTOFF(ex) &&
  312.     (N_TXTOFF(ex) < inode->i_sb->s_blocksize)) {
  313. printk("N_TXTOFF < BLOCK_SIZE. Please convert libraryn");
  314. goto out;
  315. }
  316. if (N_FLAGS(ex))
  317. goto out;
  318. /* For  QMAGIC, the starting address is 0x20 into the page.  We mask
  319.    this off to get the starting address for the page */
  320. start_addr =  ex.a_entry & 0xfffff000;
  321. /* Now use mmap to map the library into memory. */
  322. down_write(&current->mm->mmap_sem);
  323. error = do_mmap(file, start_addr, ex.a_text + ex.a_data,
  324. PROT_READ | PROT_WRITE | PROT_EXEC,
  325. MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE,
  326. N_TXTOFF(ex));
  327. up_write(&current->mm->mmap_sem);
  328. retval = error;
  329. if (error != start_addr)
  330. goto out;
  331. len = PAGE_ALIGN(ex.a_text + ex.a_data);
  332. bss = ex.a_text + ex.a_data + ex.a_bss;
  333. if (bss > len) {
  334. error = do_brk(start_addr + len, bss - len);
  335. retval = error;
  336. if (error != start_addr + len)
  337. goto out;
  338. }
  339. retval = 0;
  340. out:
  341. return retval;
  342. }
  343. static int __init init_aout32_binfmt(void)
  344. {
  345. return register_binfmt(&aout32_format);
  346. }
  347. static void __exit exit_aout32_binfmt(void)
  348. {
  349. unregister_binfmt(&aout32_format);
  350. }
  351. EXPORT_NO_SYMBOLS;
  352. module_init(init_aout32_binfmt);
  353. module_exit(exit_aout32_binfmt);