freemyipod r466 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r465‎ | r466 | r467 >
Date:00:28, 22 January 2011
Author:theseven
Status:new
Tags:
Comment:
emCORE: Make shared libraries actually work
Modified paths:
  • /emcore/trunk/execimage.c (modified) (history)
  • /emcore/trunk/export/emcorelib.h (modified) (history)
  • /emcore/trunk/export/syscallapi.h (modified) (history)
  • /emcore/trunk/export/syscallwrappers.h (modified) (history)
  • /emcore/trunk/init.c (modified) (history)
  • /emcore/trunk/library.c (modified) (history)
  • /emcore/trunk/library.h (modified) (history)
  • /emcore/trunk/syscallapi.c (modified) (history)
  • /tools/elf2emcoreapp/elf2emcoreapp.c (modified) (history)

Diff [purge]

Index: tools/elf2emcoreapp/elf2emcoreapp.c
@@ -148,9 +148,10 @@
149149
150150 int verbose = 0; /* extra output when running */
151151 int pic_with_got = 0; /* do elf/got processing with PIC code */
152 -int load_to_ram = 0; /* instruct loader to allocate everything into RAM */
 152+int load_to_ram = 1; /* instruct loader to allocate everything into RAM */
153153 int ktrace = 0; /* instruct loader output kernel trace on load */
154 -int docompress = 0; /* 1 = compress everything, 2 = compress data only */
 154+int docompress = 0; /* 1 = compress everything */
 155+int lib = 0; /* 1 = this is a shared library */
155156 int use_resolved = 0; /* If true, get the value of symbol references from */
156157 /* the program contents, not from the relocation table. */
157158 /* In this case, the input ELF file must be already */
@@ -1590,10 +1591,8 @@
15911592 fprintf(stderr, "Usage: %s [vrzd] [-p <abs-pic-file>] [-s stack-size] "
15921593 "[-o <output-file>] <elf-file>\n\n"
15931594 " -v : verbose operation\n"
1594 - " -r : force load to RAM\n"
1595 - " -k : enable kernel trace on load (for debug)\n"
 1595+ " -l : the file to be processed is a shared library\n"
15961596 " -z : compress code/data/relocs\n"
1597 - " -d : compress data/relocs\n"
15981597 " -a : use existing symbol references\n"
15991598 " instead of recalculating from\n"
16001599 " relocation info\n"
@@ -1669,23 +1668,17 @@
16701669 stack = 0x2020;
16711670 #endif
16721671
1673 - while ((opt = getopt(argc, argv, "avzdrkp:s:o:R:")) != -1) {
 1672+ while ((opt = getopt(argc, argv, "avzlp:s:o:R:")) != -1) {
16741673 switch (opt) {
16751674 case 'v':
16761675 verbose++;
16771676 break;
1678 - case 'r':
1679 - load_to_ram++;
 1677+ case 'l':
 1678+ lib = 1;
16801679 break;
1681 - case 'k':
1682 - ktrace++;
1683 - break;
16841680 case 'z':
16851681 docompress = 1;
16861682 break;
1687 - case 'd':
1688 - docompress = 2;
1689 - break;
16901683 case 'p':
16911684 pfile = optarg;
16921685 break;
@@ -1711,12 +1704,6 @@
17121705 }
17131706 }
17141707
1715 - /*
1716 - * if neither the -r or -p options was given, default to
1717 - * a RAM load as that is the only option that makes sense.
1718 - */
1719 - if (!load_to_ram && !pfile)
1720 - load_to_ram = 1;
17211708
17221709 fname = argv[argc-1];
17231710
@@ -1865,16 +1852,17 @@
18661853
18671854 /* Fill in the binflt_flat header */
18681855 memcpy(hdr.signature, "emCOexec", 8);
1869 - hdr.version = EMCOREAPP_HEADER_VERSION;
1870 - hdr.textstart = sizeof(hdr);
1871 - hdr.textsize = text_len + data_len;
1872 - hdr.bsssize = bss_len;
1873 - hdr.stacksize = stack;
1874 - hdr.entrypoint = bfd_get_start_address(abs_bfd);
1875 - hdr.relocstart = sizeof(hdr) + text_len + data_len;
1876 - hdr.reloccount = reloc_len;
1877 - hdr.flags = docompress ? EMCOREAPP_FLAG_COMPRESSED : 0;
1878 - hdr.creationtime = (uint32_t)time(NULL);
 1856+ hdr.version = EMCOREAPP_HEADER_VERSION;
 1857+ hdr.textstart = sizeof(hdr);
 1858+ hdr.textsize = text_len + data_len;
 1859+ hdr.bsssize = bss_len;
 1860+ hdr.stacksize = stack;
 1861+ hdr.entrypoint = bfd_get_start_address(abs_bfd);
 1862+ hdr.relocstart = sizeof(hdr) + text_len + data_len;
 1863+ hdr.reloccount = reloc_len;
 1864+ hdr.flags = (docompress ? EMCOREAPP_FLAG_COMPRESSED : 0)
 1865+ | (lib ? EMCOREAPP_FLAG_LIBRARY : 0);
 1866+ hdr.creationtime = (uint32_t)time(NULL);
18791867
18801868 for (i=0; i<reloc_len; i++) reloc[i] = reloc[i];
18811869
@@ -1889,7 +1877,8 @@
18901878 if (!ofile) {
18911879 ofile = xmalloc(strlen(fname) + 10 + 1); /* 10 to add suffix */
18921880 strcpy(ofile, fname);
1893 - strcat(ofile, ".emcoreapp");
 1881+ if (lib) strcat(ofile, ".emcorelib");
 1882+ else strcat(ofile, ".emcoreapp");
18941883 }
18951884
18961885 if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0)
Index: emcore/trunk/init.c
@@ -200,7 +200,6 @@
201201 break;
202202 }
203203 success = execimage(buffer, false) != NULL;
204 - if (!success) free(buffer);
205204 break;
206205 }
207206 #endif
@@ -230,7 +229,6 @@
231230 }
232231 close(fd);
233232 success = execimage(buffer, false) != NULL;
234 - if (!success) free(buffer);
235233 break;
236234 }
237235 #endif
Index: emcore/trunk/execimage.c
@@ -71,17 +71,11 @@
7272 return NULL;
7373 }
7474 uint32_t decompsize;
75 - if (ucl_decompress(image + offset, datasize, alloc, &decompsize))
76 - {
77 - cprintf(CONSOLE_BOOT, "execimage: Decompression failed!\n");
78 - if (!copy) free(image);
79 - free(alloc);
80 - return NULL;
81 - }
 75+ ucl_decompress(image + offset, datasize, alloc, &decompsize);
8276 if (!copy) free(image);
8377 if (datasize != decompsize)
8478 {
85 - cprintf(CONSOLE_BOOT, "execimage: Decompressed size mismatch!\n");
 79+ cprintf(CONSOLE_BOOT, "execimage: Decompression failed!\n");
8680 free(alloc);
8781 return NULL;
8882 }
Index: emcore/trunk/export/syscallwrappers.h
@@ -187,6 +187,16 @@
188188 #define int_dma_set_handler __emcore_syscall->int_dma_set_handler
189189 #define thread_set_name __emcore_syscall->thread_set_name
190190 #define thread_set_priority __emcore_syscall->thread_set_priority
 191+#define malloc __emcore_syscall->malloc
 192+#define memalign __emcore_syscall->memalogn
 193+#define realloc __emcore_syscall->realloc
 194+#define reownalloc __emcore_syscall->reownalloc
 195+#define free __emcore_syscall->free
 196+#define library_unload __emcore_syscall->library_unload
 197+#define get_library __emcore_syscall->get_library
 198+#define get_library_ext __emcore_syscall->get_library_ext
 199+#define release_library __emcore_syscall->release_library
 200+#define release_library_ext __emcore_syscall->release_library_ext
191201
192202
193203 #endif
Index: emcore/trunk/export/emcorelib.h
@@ -1,23 +1,23 @@
22 #include "syscallwrappers.h"
33
44
5 -#define EMCORE_LIB_HEADER(libidentifier, libversion, initfunc, shutdownfunc, apipointer) \
 5+#define EMCORE_LIB_HEADER(lib_identifier, lib_version, init_func, shutdown_func, api_pointer) \
66 struct emcore_syscall_table* __emcore_syscall; \
77 int __emcore_lib_init() \
88 { \
99 asm volatile("swi\t2\n\tldr\tr1, =__emcore_syscall\n\tstr\tr0, [r1]\n\t" \
1010 ::: "r0", "r1", "r2", "r3", "r12", "lr", "cc", "memory"); \
11 - if (__embios_syscall->table_version < EMCORE_API_VERSION \
12 - || __embios_syscall->table_minversion > EMCORE_API_VERSION) \
 11+ if (__emcore_syscall->table_version < EMCORE_API_VERSION \
 12+ || __emcore_syscall->table_minversion > EMCORE_API_VERSION) \
1313 return 0x80000000; \
14 - if (initfunc) return initfunc(); \
 14+ if (init_func) return init_func(); \
1515 } \
16 - struct emcorelib_header __attribute__((section(".emcoreentrypoint"))) __emcore_entrypoint() \
 16+ struct emcorelib_header __emcore_lib_header __attribute__((section(".emcoreentrypoint"))) = \
1717 { \
1818 .headerversion = EMCORELIB_HEADER_VERSION, \
19 - .identifier = libidentifier, \
20 - .version = libversion, \
 19+ .identifier = lib_identifier, \
 20+ .version = lib_version, \
2121 .initfunc = __emcore_lib_init, \
22 - .shutdownfunc = shutdownfunc, \
23 - .api = apipointer \
 22+ .shutdownfunc = shutdown_func, \
 23+ .api = &api_pointer \
2424 };
Index: emcore/trunk/export/syscallapi.h
@@ -57,6 +57,8 @@
5858 #include "../contextswitch.h"
5959 #include "../hwkeyaes.h"
6060 #include "../hmacsha1.h"
 61+#include "../malloc.h"
 62+#include "../library.h"
6163 #include "../libc/include/string.h"
6264 #include "../libc/include/stdlib.h"
6365 #include "../libc/include/stdio.h"
@@ -231,6 +233,16 @@
232234 typeof(int_dma_set_handler) *int_dma_set_handler;
233235 typeof(thread_set_name) *thread_set_name;
234236 typeof(thread_set_priority) *thread_set_priority;
 237+ typeof(malloc) *malloc;
 238+ typeof(memalign) *memalign;
 239+ typeof(realloc) *realloc;
 240+ typeof(reownalloc) *reownalloc;
 241+ typeof(free) *free;
 242+ typeof(library_unload) *library_unload;
 243+ typeof(get_library) *get_library;
 244+ typeof(get_library_ext) *get_library_ext;
 245+ typeof(release_library) *release_library;
 246+ typeof(release_library_ext) *release_library_ext;
235247 };
236248
237249
Index: emcore/trunk/syscallapi.c
@@ -198,5 +198,15 @@
199199 #endif
200200 .int_dma_set_handler = int_dma_set_handler,
201201 .thread_set_name = thread_set_name,
202 - .thread_set_priority = thread_set_priority
 202+ .thread_set_priority = thread_set_priority,
 203+ .malloc = malloc,
 204+ .memalign = memalign,
 205+ .realloc = realloc,
 206+ .reownalloc = reownalloc,
 207+ .free = free,
 208+ .library_unload = library_unload,
 209+ .get_library = get_library,
 210+ .get_library_ext = get_library_ext,
 211+ .release_library = release_library,
 212+ .release_library_ext = release_library_ext
203213 };
Index: emcore/trunk/library.c
@@ -64,6 +64,7 @@
6565 handle->next = library_list_head;
6666 handle->lib = header;
6767 handle->alloc = image;
 68+ library_list_head = handle;
6869 mutex_unlock(&library_mutex);
6970 return handle;
7071 }
@@ -143,8 +144,71 @@
144145 }
145146 if (!best)
146147 {
147 - mutex_unlock(&library_mutex);
148 - return NULL;
 148+ switch (sourcetype)
 149+ {
 150+ case LIBSOURCE_RAM_ALLOCED:
 151+ {
 152+ best = (struct library_handle*)execimage(source, false);
 153+ break;
 154+ }
 155+
 156+ case LIBSOURCE_RAM_NEEDCOPY:
 157+ {
 158+ best = (struct library_handle*)execimage(source, true);
 159+ break;
 160+ }
 161+
 162+#ifdef HAVE_BOOTFLASH
 163+ case LIBSOURCE_BOOTFLASH:
 164+ {
 165+ int size = bootflash_filesize((char*)source);
 166+ if (size <= 0) break;
 167+ void* buffer = memalign(0x10, size);
 168+ if (!buffer) break;
 169+ if (bootflash_read((char*)source, buffer, 0, size) != size)
 170+ {
 171+ free(buffer);
 172+ break;
 173+ }
 174+ best = (struct library_handle*)execimage(buffer, false);
 175+ break;
 176+ }
 177+#endif
 178+
 179+#ifdef HAVE_STORAGE
 180+ case LIBSOURCE_FILESYSTEM:
 181+ {
 182+ int fd = file_open((char*)source, O_RDONLY);
 183+ if (fd <= 0) break;
 184+ int size = filesize(fd);
 185+ if (size <= 0)
 186+ {
 187+ close(fd);
 188+ break;
 189+ }
 190+ void* buffer = memalign(0x10, size);
 191+ if (!buffer)
 192+ {
 193+ close(fd);
 194+ break;
 195+ }
 196+ if (read(fd, buffer, size) != size)
 197+ {
 198+ free(buffer);
 199+ close(fd);
 200+ break;
 201+ }
 202+ close(fd);
 203+ best = (struct library_handle*)execimage(buffer, false);
 204+ break;
 205+ }
 206+#endif
 207+ }
 208+ if (!best)
 209+ {
 210+ mutex_unlock(&library_mutex);
 211+ return NULL;
 212+ }
149213 }
150214 for (i = 0; i < ARRAYLEN(best->users); i++)
151215 if (best->users[i] == NULL)
Index: emcore/trunk/library.h
@@ -56,6 +56,7 @@
5757
5858 enum library_sourcetype
5959 {
 60+ LIBSOURCE_NONE = 0,
6061 LIBSOURCE_RAM_ALLOCED = 1,
6162 LIBSOURCE_RAM_NEEDCOPY = 2,
6263 LIBSOURCE_BOOTFLASH = 3,