PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/binfmt_flat.c

https://github.com/mstsirkin/linux
C | 960 lines | 668 code | 131 blank | 161 comment | 128 complexity | 0702e5a83a82ed1fe33ddebb0e6781d9 MD5 | raw file
  1. /****************************************************************************/
  2. /*
  3. * linux/fs/binfmt_flat.c
  4. *
  5. * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
  6. * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
  7. * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
  8. * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
  9. * based heavily on:
  10. *
  11. * linux/fs/binfmt_aout.c:
  12. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  13. * linux/fs/binfmt_flat.c for 2.0 kernel
  14. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  15. * JAN/99 -- coded full program relocation (gerg@snapgear.com)
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/mman.h>
  22. #include <linux/errno.h>
  23. #include <linux/signal.h>
  24. #include <linux/string.h>
  25. #include <linux/fs.h>
  26. #include <linux/file.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/user.h>
  31. #include <linux/slab.h>
  32. #include <linux/binfmts.h>
  33. #include <linux/personality.h>
  34. #include <linux/init.h>
  35. #include <linux/flat.h>
  36. #include <linux/syscalls.h>
  37. #include <asm/byteorder.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/unaligned.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/page.h>
  43. /****************************************************************************/
  44. #if 0
  45. #define DEBUG 1
  46. #endif
  47. #ifdef DEBUG
  48. #define DBG_FLT(a...) printk(a)
  49. #else
  50. #define DBG_FLT(a...)
  51. #endif
  52. /*
  53. * User data (data section and bss) needs to be aligned.
  54. * We pick 0x20 here because it is the max value elf2flt has always
  55. * used in producing FLAT files, and because it seems to be large
  56. * enough to make all the gcc alignment related tests happy.
  57. */
  58. #define FLAT_DATA_ALIGN (0x20)
  59. /*
  60. * User data (stack) also needs to be aligned.
  61. * Here we can be a bit looser than the data sections since this
  62. * needs to only meet arch ABI requirements.
  63. */
  64. #define FLAT_STACK_ALIGN max_t(unsigned long, sizeof(void *), ARCH_SLAB_MINALIGN)
  65. #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */
  66. #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */
  67. struct lib_info {
  68. struct {
  69. unsigned long start_code; /* Start of text segment */
  70. unsigned long start_data; /* Start of data segment */
  71. unsigned long start_brk; /* End of data segment */
  72. unsigned long text_len; /* Length of text segment */
  73. unsigned long entry; /* Start address for this module */
  74. unsigned long build_date; /* When this one was compiled */
  75. short loaded; /* Has this library been loaded? */
  76. } lib_list[MAX_SHARED_LIBS];
  77. };
  78. #ifdef CONFIG_BINFMT_SHARED_FLAT
  79. static int load_flat_shared_library(int id, struct lib_info *p);
  80. #endif
  81. static int load_flat_binary(struct linux_binprm *, struct pt_regs * regs);
  82. static int flat_core_dump(struct coredump_params *cprm);
  83. static struct linux_binfmt flat_format = {
  84. .module = THIS_MODULE,
  85. .load_binary = load_flat_binary,
  86. .core_dump = flat_core_dump,
  87. .min_coredump = PAGE_SIZE
  88. };
  89. /****************************************************************************/
  90. /*
  91. * Routine writes a core dump image in the current directory.
  92. * Currently only a stub-function.
  93. */
  94. static int flat_core_dump(struct coredump_params *cprm)
  95. {
  96. printk("Process %s:%d received signr %d and should have core dumped\n",
  97. current->comm, current->pid, (int) cprm->signr);
  98. return(1);
  99. }
  100. /****************************************************************************/
  101. /*
  102. * create_flat_tables() parses the env- and arg-strings in new user
  103. * memory and creates the pointer tables from them, and puts their
  104. * addresses on the "stack", returning the new stack pointer value.
  105. */
  106. static unsigned long create_flat_tables(
  107. unsigned long pp,
  108. struct linux_binprm * bprm)
  109. {
  110. unsigned long *argv,*envp;
  111. unsigned long * sp;
  112. char * p = (char*)pp;
  113. int argc = bprm->argc;
  114. int envc = bprm->envc;
  115. char uninitialized_var(dummy);
  116. sp = (unsigned long *)p;
  117. sp -= (envc + argc + 2) + 1 + (flat_argvp_envp_on_stack() ? 2 : 0);
  118. sp = (unsigned long *) ((unsigned long)sp & -FLAT_STACK_ALIGN);
  119. argv = sp + 1 + (flat_argvp_envp_on_stack() ? 2 : 0);
  120. envp = argv + (argc + 1);
  121. if (flat_argvp_envp_on_stack()) {
  122. put_user((unsigned long) envp, sp + 2);
  123. put_user((unsigned long) argv, sp + 1);
  124. }
  125. put_user(argc, sp);
  126. current->mm->arg_start = (unsigned long) p;
  127. while (argc-->0) {
  128. put_user((unsigned long) p, argv++);
  129. do {
  130. get_user(dummy, p); p++;
  131. } while (dummy);
  132. }
  133. put_user((unsigned long) NULL, argv);
  134. current->mm->arg_end = current->mm->env_start = (unsigned long) p;
  135. while (envc-->0) {
  136. put_user((unsigned long)p, envp); envp++;
  137. do {
  138. get_user(dummy, p); p++;
  139. } while (dummy);
  140. }
  141. put_user((unsigned long) NULL, envp);
  142. current->mm->env_end = (unsigned long) p;
  143. return (unsigned long)sp;
  144. }
  145. /****************************************************************************/
  146. #ifdef CONFIG_BINFMT_ZFLAT
  147. #include <linux/zlib.h>
  148. #define LBUFSIZE 4000
  149. /* gzip flag byte */
  150. #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
  151. #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
  152. #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
  153. #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
  154. #define COMMENT 0x10 /* bit 4 set: file comment present */
  155. #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
  156. #define RESERVED 0xC0 /* bit 6,7: reserved */
  157. static int decompress_exec(
  158. struct linux_binprm *bprm,
  159. unsigned long offset,
  160. char *dst,
  161. long len,
  162. int fd)
  163. {
  164. unsigned char *buf;
  165. z_stream strm;
  166. loff_t fpos;
  167. int ret, retval;
  168. DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);
  169. memset(&strm, 0, sizeof(strm));
  170. strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
  171. if (strm.workspace == NULL) {
  172. DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
  173. return -ENOMEM;
  174. }
  175. buf = kmalloc(LBUFSIZE, GFP_KERNEL);
  176. if (buf == NULL) {
  177. DBG_FLT("binfmt_flat: no memory for read buffer\n");
  178. retval = -ENOMEM;
  179. goto out_free;
  180. }
  181. /* Read in first chunk of data and parse gzip header. */
  182. fpos = offset;
  183. ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
  184. strm.next_in = buf;
  185. strm.avail_in = ret;
  186. strm.total_in = 0;
  187. retval = -ENOEXEC;
  188. /* Check minimum size -- gzip header */
  189. if (ret < 10) {
  190. DBG_FLT("binfmt_flat: file too small?\n");
  191. goto out_free_buf;
  192. }
  193. /* Check gzip magic number */
  194. if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
  195. DBG_FLT("binfmt_flat: unknown compression magic?\n");
  196. goto out_free_buf;
  197. }
  198. /* Check gzip method */
  199. if (buf[2] != 8) {
  200. DBG_FLT("binfmt_flat: unknown compression method?\n");
  201. goto out_free_buf;
  202. }
  203. /* Check gzip flags */
  204. if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
  205. (buf[3] & RESERVED)) {
  206. DBG_FLT("binfmt_flat: unknown flags?\n");
  207. goto out_free_buf;
  208. }
  209. ret = 10;
  210. if (buf[3] & EXTRA_FIELD) {
  211. ret += 2 + buf[10] + (buf[11] << 8);
  212. if (unlikely(LBUFSIZE <= ret)) {
  213. DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
  214. goto out_free_buf;
  215. }
  216. }
  217. if (buf[3] & ORIG_NAME) {
  218. while (ret < LBUFSIZE && buf[ret++] != 0)
  219. ;
  220. if (unlikely(LBUFSIZE == ret)) {
  221. DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
  222. goto out_free_buf;
  223. }
  224. }
  225. if (buf[3] & COMMENT) {
  226. while (ret < LBUFSIZE && buf[ret++] != 0)
  227. ;
  228. if (unlikely(LBUFSIZE == ret)) {
  229. DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
  230. goto out_free_buf;
  231. }
  232. }
  233. strm.next_in += ret;
  234. strm.avail_in -= ret;
  235. strm.next_out = dst;
  236. strm.avail_out = len;
  237. strm.total_out = 0;
  238. if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
  239. DBG_FLT("binfmt_flat: zlib init failed?\n");
  240. goto out_free_buf;
  241. }
  242. while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
  243. ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
  244. if (ret <= 0)
  245. break;
  246. len -= ret;
  247. strm.next_in = buf;
  248. strm.avail_in = ret;
  249. strm.total_in = 0;
  250. }
  251. if (ret < 0) {
  252. DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
  253. ret, strm.msg);
  254. goto out_zlib;
  255. }
  256. retval = 0;
  257. out_zlib:
  258. zlib_inflateEnd(&strm);
  259. out_free_buf:
  260. kfree(buf);
  261. out_free:
  262. kfree(strm.workspace);
  263. return retval;
  264. }
  265. #endif /* CONFIG_BINFMT_ZFLAT */
  266. /****************************************************************************/
  267. static unsigned long
  268. calc_reloc(unsigned long r, struct lib_info *p, int curid, int internalp)
  269. {
  270. unsigned long addr;
  271. int id;
  272. unsigned long start_brk;
  273. unsigned long start_data;
  274. unsigned long text_len;
  275. unsigned long start_code;
  276. #ifdef CONFIG_BINFMT_SHARED_FLAT
  277. if (r == 0)
  278. id = curid; /* Relocs of 0 are always self referring */
  279. else {
  280. id = (r >> 24) & 0xff; /* Find ID for this reloc */
  281. r &= 0x00ffffff; /* Trim ID off here */
  282. }
  283. if (id >= MAX_SHARED_LIBS) {
  284. printk("BINFMT_FLAT: reference 0x%x to shared library %d",
  285. (unsigned) r, id);
  286. goto failed;
  287. }
  288. if (curid != id) {
  289. if (internalp) {
  290. printk("BINFMT_FLAT: reloc address 0x%x not in same module "
  291. "(%d != %d)", (unsigned) r, curid, id);
  292. goto failed;
  293. } else if ( ! p->lib_list[id].loaded &&
  294. IS_ERR_VALUE(load_flat_shared_library(id, p))) {
  295. printk("BINFMT_FLAT: failed to load library %d", id);
  296. goto failed;
  297. }
  298. /* Check versioning information (i.e. time stamps) */
  299. if (p->lib_list[id].build_date && p->lib_list[curid].build_date &&
  300. p->lib_list[curid].build_date < p->lib_list[id].build_date) {
  301. printk("BINFMT_FLAT: library %d is younger than %d", id, curid);
  302. goto failed;
  303. }
  304. }
  305. #else
  306. id = 0;
  307. #endif
  308. start_brk = p->lib_list[id].start_brk;
  309. start_data = p->lib_list[id].start_data;
  310. start_code = p->lib_list[id].start_code;
  311. text_len = p->lib_list[id].text_len;
  312. if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
  313. printk("BINFMT_FLAT: reloc outside program 0x%x (0 - 0x%x/0x%x)",
  314. (int) r,(int)(start_brk-start_data+text_len),(int)text_len);
  315. goto failed;
  316. }
  317. if (r < text_len) /* In text segment */
  318. addr = r + start_code;
  319. else /* In data segment */
  320. addr = r - text_len + start_data;
  321. /* Range checked already above so doing the range tests is redundant...*/
  322. return(addr);
  323. failed:
  324. printk(", killing %s!\n", current->comm);
  325. send_sig(SIGSEGV, current, 0);
  326. return RELOC_FAILED;
  327. }
  328. /****************************************************************************/
  329. void old_reloc(unsigned long rl)
  330. {
  331. #ifdef DEBUG
  332. char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
  333. #endif
  334. flat_v2_reloc_t r;
  335. unsigned long *ptr;
  336. r.value = rl;
  337. #if defined(CONFIG_COLDFIRE)
  338. ptr = (unsigned long *) (current->mm->start_code + r.reloc.offset);
  339. #else
  340. ptr = (unsigned long *) (current->mm->start_data + r.reloc.offset);
  341. #endif
  342. #ifdef DEBUG
  343. printk("Relocation of variable at DATASEG+%x "
  344. "(address %p, currently %x) into segment %s\n",
  345. r.reloc.offset, ptr, (int)*ptr, segment[r.reloc.type]);
  346. #endif
  347. switch (r.reloc.type) {
  348. case OLD_FLAT_RELOC_TYPE_TEXT:
  349. *ptr += current->mm->start_code;
  350. break;
  351. case OLD_FLAT_RELOC_TYPE_DATA:
  352. *ptr += current->mm->start_data;
  353. break;
  354. case OLD_FLAT_RELOC_TYPE_BSS:
  355. *ptr += current->mm->end_data;
  356. break;
  357. default:
  358. printk("BINFMT_FLAT: Unknown relocation type=%x\n", r.reloc.type);
  359. break;
  360. }
  361. #ifdef DEBUG
  362. printk("Relocation became %x\n", (int)*ptr);
  363. #endif
  364. }
  365. /****************************************************************************/
  366. static int load_flat_file(struct linux_binprm * bprm,
  367. struct lib_info *libinfo, int id, unsigned long *extra_stack)
  368. {
  369. struct flat_hdr * hdr;
  370. unsigned long textpos = 0, datapos = 0, result;
  371. unsigned long realdatastart = 0;
  372. unsigned long text_len, data_len, bss_len, stack_len, flags;
  373. unsigned long len, memp = 0;
  374. unsigned long memp_size, extra, rlim;
  375. unsigned long *reloc = 0, *rp;
  376. struct inode *inode;
  377. int i, rev, relocs = 0;
  378. loff_t fpos;
  379. unsigned long start_code, end_code;
  380. int ret;
  381. hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
  382. inode = bprm->file->f_path.dentry->d_inode;
  383. text_len = ntohl(hdr->data_start);
  384. data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
  385. bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
  386. stack_len = ntohl(hdr->stack_size);
  387. if (extra_stack) {
  388. stack_len += *extra_stack;
  389. *extra_stack = stack_len;
  390. }
  391. relocs = ntohl(hdr->reloc_count);
  392. flags = ntohl(hdr->flags);
  393. rev = ntohl(hdr->rev);
  394. if (strncmp(hdr->magic, "bFLT", 4)) {
  395. /*
  396. * Previously, here was a printk to tell people
  397. * "BINFMT_FLAT: bad header magic".
  398. * But for the kernel which also use ELF FD-PIC format, this
  399. * error message is confusing.
  400. * because a lot of people do not manage to produce good
  401. */
  402. ret = -ENOEXEC;
  403. goto err;
  404. }
  405. if (flags & FLAT_FLAG_KTRACE)
  406. printk("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
  407. if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
  408. printk("BINFMT_FLAT: bad flat file version 0x%x (supported "
  409. "0x%lx and 0x%lx)\n",
  410. rev, FLAT_VERSION, OLD_FLAT_VERSION);
  411. ret = -ENOEXEC;
  412. goto err;
  413. }
  414. /* Don't allow old format executables to use shared libraries */
  415. if (rev == OLD_FLAT_VERSION && id != 0) {
  416. printk("BINFMT_FLAT: shared libraries are not available before rev 0x%x\n",
  417. (int) FLAT_VERSION);
  418. ret = -ENOEXEC;
  419. goto err;
  420. }
  421. /*
  422. * fix up the flags for the older format, there were all kinds
  423. * of endian hacks, this only works for the simple cases
  424. */
  425. if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
  426. flags = FLAT_FLAG_RAM;
  427. #ifndef CONFIG_BINFMT_ZFLAT
  428. if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
  429. printk("Support for ZFLAT executables is not enabled.\n");
  430. ret = -ENOEXEC;
  431. goto err;
  432. }
  433. #endif
  434. /*
  435. * Check initial limits. This avoids letting people circumvent
  436. * size limits imposed on them by creating programs with large
  437. * arrays in the data or bss.
  438. */
  439. rlim = rlimit(RLIMIT_DATA);
  440. if (rlim >= RLIM_INFINITY)
  441. rlim = ~0;
  442. if (data_len + bss_len > rlim) {
  443. ret = -ENOMEM;
  444. goto err;
  445. }
  446. /* Flush all traces of the currently running executable */
  447. if (id == 0) {
  448. result = flush_old_exec(bprm);
  449. if (result) {
  450. ret = result;
  451. goto err;
  452. }
  453. /* OK, This is the point of no return */
  454. set_personality(PER_LINUX_32BIT);
  455. setup_new_exec(bprm);
  456. }
  457. /*
  458. * calculate the extra space we need to map in
  459. */
  460. extra = max_t(unsigned long, bss_len + stack_len,
  461. relocs * sizeof(unsigned long));
  462. /*
  463. * there are a couple of cases here, the separate code/data
  464. * case, and then the fully copied to RAM case which lumps
  465. * it all together.
  466. */
  467. if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
  468. /*
  469. * this should give us a ROM ptr, but if it doesn't we don't
  470. * really care
  471. */
  472. DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
  473. down_write(&current->mm->mmap_sem);
  474. textpos = do_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC,
  475. MAP_PRIVATE|MAP_EXECUTABLE, 0);
  476. up_write(&current->mm->mmap_sem);
  477. if (!textpos || IS_ERR_VALUE(textpos)) {
  478. if (!textpos)
  479. textpos = (unsigned long) -ENOMEM;
  480. printk("Unable to mmap process text, errno %d\n", (int)-textpos);
  481. ret = textpos;
  482. goto err;
  483. }
  484. len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
  485. len = PAGE_ALIGN(len);
  486. down_write(&current->mm->mmap_sem);
  487. realdatastart = do_mmap(0, 0, len,
  488. PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
  489. up_write(&current->mm->mmap_sem);
  490. if (realdatastart == 0 || IS_ERR_VALUE(realdatastart)) {
  491. if (!realdatastart)
  492. realdatastart = (unsigned long) -ENOMEM;
  493. printk("Unable to allocate RAM for process data, errno %d\n",
  494. (int)-realdatastart);
  495. do_munmap(current->mm, textpos, text_len);
  496. ret = realdatastart;
  497. goto err;
  498. }
  499. datapos = ALIGN(realdatastart +
  500. MAX_SHARED_LIBS * sizeof(unsigned long),
  501. FLAT_DATA_ALIGN);
  502. DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
  503. (int)(data_len + bss_len + stack_len), (int)datapos);
  504. fpos = ntohl(hdr->data_start);
  505. #ifdef CONFIG_BINFMT_ZFLAT
  506. if (flags & FLAT_FLAG_GZDATA) {
  507. result = decompress_exec(bprm, fpos, (char *) datapos,
  508. data_len + (relocs * sizeof(unsigned long)), 0);
  509. } else
  510. #endif
  511. {
  512. result = bprm->file->f_op->read(bprm->file, (char *) datapos,
  513. data_len + (relocs * sizeof(unsigned long)), &fpos);
  514. }
  515. if (IS_ERR_VALUE(result)) {
  516. printk("Unable to read data+bss, errno %d\n", (int)-result);
  517. do_munmap(current->mm, textpos, text_len);
  518. do_munmap(current->mm, realdatastart, len);
  519. ret = result;
  520. goto err;
  521. }
  522. reloc = (unsigned long *) (datapos+(ntohl(hdr->reloc_start)-text_len));
  523. memp = realdatastart;
  524. memp_size = len;
  525. } else {
  526. len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
  527. len = PAGE_ALIGN(len);
  528. down_write(&current->mm->mmap_sem);
  529. textpos = do_mmap(0, 0, len,
  530. PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
  531. up_write(&current->mm->mmap_sem);
  532. if (!textpos || IS_ERR_VALUE(textpos)) {
  533. if (!textpos)
  534. textpos = (unsigned long) -ENOMEM;
  535. printk("Unable to allocate RAM for process text/data, errno %d\n",
  536. (int)-textpos);
  537. ret = textpos;
  538. goto err;
  539. }
  540. realdatastart = textpos + ntohl(hdr->data_start);
  541. datapos = ALIGN(realdatastart +
  542. MAX_SHARED_LIBS * sizeof(unsigned long),
  543. FLAT_DATA_ALIGN);
  544. reloc = (unsigned long *)
  545. (datapos + (ntohl(hdr->reloc_start) - text_len));
  546. memp = textpos;
  547. memp_size = len;
  548. #ifdef CONFIG_BINFMT_ZFLAT
  549. /*
  550. * load it all in and treat it like a RAM load from now on
  551. */
  552. if (flags & FLAT_FLAG_GZIP) {
  553. result = decompress_exec(bprm, sizeof (struct flat_hdr),
  554. (((char *) textpos) + sizeof (struct flat_hdr)),
  555. (text_len + data_len + (relocs * sizeof(unsigned long))
  556. - sizeof (struct flat_hdr)),
  557. 0);
  558. memmove((void *) datapos, (void *) realdatastart,
  559. data_len + (relocs * sizeof(unsigned long)));
  560. } else if (flags & FLAT_FLAG_GZDATA) {
  561. fpos = 0;
  562. result = bprm->file->f_op->read(bprm->file,
  563. (char *) textpos, text_len, &fpos);
  564. if (!IS_ERR_VALUE(result))
  565. result = decompress_exec(bprm, text_len, (char *) datapos,
  566. data_len + (relocs * sizeof(unsigned long)), 0);
  567. }
  568. else
  569. #endif
  570. {
  571. fpos = 0;
  572. result = bprm->file->f_op->read(bprm->file,
  573. (char *) textpos, text_len, &fpos);
  574. if (!IS_ERR_VALUE(result)) {
  575. fpos = ntohl(hdr->data_start);
  576. result = bprm->file->f_op->read(bprm->file, (char *) datapos,
  577. data_len + (relocs * sizeof(unsigned long)), &fpos);
  578. }
  579. }
  580. if (IS_ERR_VALUE(result)) {
  581. printk("Unable to read code+data+bss, errno %d\n",(int)-result);
  582. do_munmap(current->mm, textpos, text_len + data_len + extra +
  583. MAX_SHARED_LIBS * sizeof(unsigned long));
  584. ret = result;
  585. goto err;
  586. }
  587. }
  588. if (flags & FLAT_FLAG_KTRACE)
  589. printk("Mapping is %x, Entry point is %x, data_start is %x\n",
  590. (int)textpos, 0x00ffffff&ntohl(hdr->entry), ntohl(hdr->data_start));
  591. /* The main program needs a little extra setup in the task structure */
  592. start_code = textpos + sizeof (struct flat_hdr);
  593. end_code = textpos + text_len;
  594. if (id == 0) {
  595. current->mm->start_code = start_code;
  596. current->mm->end_code = end_code;
  597. current->mm->start_data = datapos;
  598. current->mm->end_data = datapos + data_len;
  599. /*
  600. * set up the brk stuff, uses any slack left in data/bss/stack
  601. * allocation. We put the brk after the bss (between the bss
  602. * and stack) like other platforms.
  603. * Userspace code relies on the stack pointer starting out at
  604. * an address right at the end of a page.
  605. */
  606. current->mm->start_brk = datapos + data_len + bss_len;
  607. current->mm->brk = (current->mm->start_brk + 3) & ~3;
  608. current->mm->context.end_brk = memp + memp_size - stack_len;
  609. }
  610. if (flags & FLAT_FLAG_KTRACE)
  611. printk("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
  612. id ? "Lib" : "Load", bprm->filename,
  613. (int) start_code, (int) end_code,
  614. (int) datapos,
  615. (int) (datapos + data_len),
  616. (int) (datapos + data_len),
  617. (int) (((datapos + data_len + bss_len) + 3) & ~3));
  618. text_len -= sizeof(struct flat_hdr); /* the real code len */
  619. /* Store the current module values into the global library structure */
  620. libinfo->lib_list[id].start_code = start_code;
  621. libinfo->lib_list[id].start_data = datapos;
  622. libinfo->lib_list[id].start_brk = datapos + data_len + bss_len;
  623. libinfo->lib_list[id].text_len = text_len;
  624. libinfo->lib_list[id].loaded = 1;
  625. libinfo->lib_list[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
  626. libinfo->lib_list[id].build_date = ntohl(hdr->build_date);
  627. /*
  628. * We just load the allocations into some temporary memory to
  629. * help simplify all this mumbo jumbo
  630. *
  631. * We've got two different sections of relocation entries.
  632. * The first is the GOT which resides at the beginning of the data segment
  633. * and is terminated with a -1. This one can be relocated in place.
  634. * The second is the extra relocation entries tacked after the image's
  635. * data segment. These require a little more processing as the entry is
  636. * really an offset into the image which contains an offset into the
  637. * image.
  638. */
  639. if (flags & FLAT_FLAG_GOTPIC) {
  640. for (rp = (unsigned long *)datapos; *rp != 0xffffffff; rp++) {
  641. unsigned long addr;
  642. if (*rp) {
  643. addr = calc_reloc(*rp, libinfo, id, 0);
  644. if (addr == RELOC_FAILED) {
  645. ret = -ENOEXEC;
  646. goto err;
  647. }
  648. *rp = addr;
  649. }
  650. }
  651. }
  652. /*
  653. * Now run through the relocation entries.
  654. * We've got to be careful here as C++ produces relocatable zero
  655. * entries in the constructor and destructor tables which are then
  656. * tested for being not zero (which will always occur unless we're
  657. * based from address zero). This causes an endless loop as __start
  658. * is at zero. The solution used is to not relocate zero addresses.
  659. * This has the negative side effect of not allowing a global data
  660. * reference to be statically initialised to _stext (I've moved
  661. * __start to address 4 so that is okay).
  662. */
  663. if (rev > OLD_FLAT_VERSION) {
  664. unsigned long persistent = 0;
  665. for (i=0; i < relocs; i++) {
  666. unsigned long addr, relval;
  667. /* Get the address of the pointer to be
  668. relocated (of course, the address has to be
  669. relocated first). */
  670. relval = ntohl(reloc[i]);
  671. if (flat_set_persistent (relval, &persistent))
  672. continue;
  673. addr = flat_get_relocate_addr(relval);
  674. rp = (unsigned long *) calc_reloc(addr, libinfo, id, 1);
  675. if (rp == (unsigned long *)RELOC_FAILED) {
  676. ret = -ENOEXEC;
  677. goto err;
  678. }
  679. /* Get the pointer's value. */
  680. addr = flat_get_addr_from_rp(rp, relval, flags,
  681. &persistent);
  682. if (addr != 0) {
  683. /*
  684. * Do the relocation. PIC relocs in the data section are
  685. * already in target order
  686. */
  687. if ((flags & FLAT_FLAG_GOTPIC) == 0)
  688. addr = ntohl(addr);
  689. addr = calc_reloc(addr, libinfo, id, 0);
  690. if (addr == RELOC_FAILED) {
  691. ret = -ENOEXEC;
  692. goto err;
  693. }
  694. /* Write back the relocated pointer. */
  695. flat_put_addr_at_rp(rp, addr, relval);
  696. }
  697. }
  698. } else {
  699. for (i=0; i < relocs; i++)
  700. old_reloc(ntohl(reloc[i]));
  701. }
  702. flush_icache_range(start_code, end_code);
  703. /* zero the BSS, BRK and stack areas */
  704. memset((void*)(datapos + data_len), 0, bss_len +
  705. (memp + memp_size - stack_len - /* end brk */
  706. libinfo->lib_list[id].start_brk) + /* start brk */
  707. stack_len);
  708. return 0;
  709. err:
  710. return ret;
  711. }
  712. /****************************************************************************/
  713. #ifdef CONFIG_BINFMT_SHARED_FLAT
  714. /*
  715. * Load a shared library into memory. The library gets its own data
  716. * segment (including bss) but not argv/argc/environ.
  717. */
  718. static int load_flat_shared_library(int id, struct lib_info *libs)
  719. {
  720. struct linux_binprm bprm;
  721. int res;
  722. char buf[16];
  723. memset(&bprm, 0, sizeof(bprm));
  724. /* Create the file name */
  725. sprintf(buf, "/lib/lib%d.so", id);
  726. /* Open the file up */
  727. bprm.filename = buf;
  728. bprm.file = open_exec(bprm.filename);
  729. res = PTR_ERR(bprm.file);
  730. if (IS_ERR(bprm.file))
  731. return res;
  732. bprm.cred = prepare_exec_creds();
  733. res = -ENOMEM;
  734. if (!bprm.cred)
  735. goto out;
  736. /* We don't really care about recalculating credentials at this point
  737. * as we're past the point of no return and are dealing with shared
  738. * libraries.
  739. */
  740. bprm.cred_prepared = 1;
  741. res = prepare_binprm(&bprm);
  742. if (!IS_ERR_VALUE(res))
  743. res = load_flat_file(&bprm, libs, id, NULL);
  744. abort_creds(bprm.cred);
  745. out:
  746. allow_write_access(bprm.file);
  747. fput(bprm.file);
  748. return(res);
  749. }
  750. #endif /* CONFIG_BINFMT_SHARED_FLAT */
  751. /****************************************************************************/
  752. /*
  753. * These are the functions used to load flat style executables and shared
  754. * libraries. There is no binary dependent code anywhere else.
  755. */
  756. static int load_flat_binary(struct linux_binprm * bprm, struct pt_regs * regs)
  757. {
  758. struct lib_info libinfo;
  759. unsigned long p = bprm->p;
  760. unsigned long stack_len;
  761. unsigned long start_addr;
  762. unsigned long *sp;
  763. int res;
  764. int i, j;
  765. memset(&libinfo, 0, sizeof(libinfo));
  766. /*
  767. * We have to add the size of our arguments to our stack size
  768. * otherwise it's too easy for users to create stack overflows
  769. * by passing in a huge argument list. And yes, we have to be
  770. * pedantic and include space for the argv/envp array as it may have
  771. * a lot of entries.
  772. */
  773. #define TOP_OF_ARGS (PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *))
  774. stack_len = TOP_OF_ARGS - bprm->p; /* the strings */
  775. stack_len += (bprm->argc + 1) * sizeof(char *); /* the argv array */
  776. stack_len += (bprm->envc + 1) * sizeof(char *); /* the envp array */
  777. stack_len += FLAT_STACK_ALIGN - 1; /* reserve for upcoming alignment */
  778. res = load_flat_file(bprm, &libinfo, 0, &stack_len);
  779. if (IS_ERR_VALUE(res))
  780. return res;
  781. /* Update data segment pointers for all libraries */
  782. for (i=0; i<MAX_SHARED_LIBS; i++)
  783. if (libinfo.lib_list[i].loaded)
  784. for (j=0; j<MAX_SHARED_LIBS; j++)
  785. (-(j+1))[(unsigned long *)(libinfo.lib_list[i].start_data)] =
  786. (libinfo.lib_list[j].loaded)?
  787. libinfo.lib_list[j].start_data:UNLOADED_LIB;
  788. install_exec_creds(bprm);
  789. current->flags &= ~PF_FORKNOEXEC;
  790. set_binfmt(&flat_format);
  791. p = ((current->mm->context.end_brk + stack_len + 3) & ~3) - 4;
  792. DBG_FLT("p=%x\n", (int)p);
  793. /* copy the arg pages onto the stack, this could be more efficient :-) */
  794. for (i = TOP_OF_ARGS - 1; i >= bprm->p; i--)
  795. * (char *) --p =
  796. ((char *) page_address(bprm->page[i/PAGE_SIZE]))[i % PAGE_SIZE];
  797. sp = (unsigned long *) create_flat_tables(p, bprm);
  798. /* Fake some return addresses to ensure the call chain will
  799. * initialise library in order for us. We are required to call
  800. * lib 1 first, then 2, ... and finally the main program (id 0).
  801. */
  802. start_addr = libinfo.lib_list[0].entry;
  803. #ifdef CONFIG_BINFMT_SHARED_FLAT
  804. for (i = MAX_SHARED_LIBS-1; i>0; i--) {
  805. if (libinfo.lib_list[i].loaded) {
  806. /* Push previos first to call address */
  807. --sp; put_user(start_addr, sp);
  808. start_addr = libinfo.lib_list[i].entry;
  809. }
  810. }
  811. #endif
  812. /* Stash our initial stack pointer into the mm structure */
  813. current->mm->start_stack = (unsigned long )sp;
  814. #ifdef FLAT_PLAT_INIT
  815. FLAT_PLAT_INIT(regs);
  816. #endif
  817. DBG_FLT("start_thread(regs=0x%x, entry=0x%x, start_stack=0x%x)\n",
  818. (int)regs, (int)start_addr, (int)current->mm->start_stack);
  819. start_thread(regs, start_addr, current->mm->start_stack);
  820. return 0;
  821. }
  822. /****************************************************************************/
  823. static int __init init_flat_binfmt(void)
  824. {
  825. return register_binfmt(&flat_format);
  826. }
  827. /****************************************************************************/
  828. core_initcall(init_flat_binfmt);
  829. /****************************************************************************/