binfmt_flat.c 27 KB

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