binfmt_flat.c 26 KB

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