binfmt_flat.c 27 KB

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