binfmt_flat.c 26 KB

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