diff options
| author | bernard <[email protected]> | 2017-09-29 10:23:56 +0800 |
|---|---|---|
| committer | bernard <[email protected]> | 2017-09-29 10:23:56 +0800 |
| commit | cccff34fcac04f34233a33e453f2d0ec255bf231 (patch) | |
| tree | d406036f73296b27f3cb96e77ede98dd7803c61d /src/module.c | |
| parent | e9a77b513f0a4b37d539a030592a61b08d03602d (diff) | |
| parent | 3691b67e83ab57196dfc62606b1be18db76994a7 (diff) | |
Merge branch 'master' of https://github.com/RT-Thread/rt-thread
Diffstat (limited to 'src/module.c')
| -rw-r--r-- | src/module.c | 104 |
1 files changed, 58 insertions, 46 deletions
diff --git a/src/module.c b/src/module.c index 79ac0c8688..524eeac263 100644 --- a/src/module.c +++ b/src/module.c @@ -27,6 +27,7 @@ * 2012-11-23 Bernard using RT_DEBUG_LOG instead of rt_kprintf. * 2012-11-28 Bernard remove rt_current_module and user * can use rt_module_unload to remove a module. + * 2017-08-20 parai support intel 386 machine */ #include <rthw.h> @@ -116,7 +117,7 @@ int rt_system_module_init(void) _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit; #elif defined (__IAR_SYSTEMS_ICC__) _rt_module_symtab_begin = __section_begin("RTMSymTab"); - _rt_module_symtab_end = __section_begin("RTMSymTab"); + _rt_module_symtab_end = __section_end("RTMSymTab"); #endif #ifdef RT_USING_SLAB @@ -137,7 +138,7 @@ void list_symbol(void) index != _rt_module_symtab_end; index ++) { - rt_kprintf("%s\n", index->name); + rt_kprintf("%s\n", index->name); } return ; @@ -223,6 +224,10 @@ static int rt_module_arm_relocate(struct rt_module *module, *where &= 0xf000000f; *where |= 0x01a0f000; break; +#ifdef MODULE_USING_386 + case R_386_GLOB_DAT: + case R_386_JUMP_SLOT: +#endif case R_ARM_GLOB_DAT: case R_ARM_JUMP_SLOT: *where = (Elf32_Addr)sym_val; @@ -237,6 +242,9 @@ static int rt_module_arm_relocate(struct rt_module *module, where, *where, sym_val)); break; #endif +#ifdef MODULE_USING_386 + case R_386_RELATIVE: +#endif case R_ARM_RELATIVE: *where = (Elf32_Addr)sym_val + *where; RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", @@ -251,10 +259,10 @@ static int rt_module_arm_relocate(struct rt_module *module, j1 = (lower >> 13) & 1; j2 = (lower >> 11) & 1; offset = (sign << 24) | - ((~(j1 ^ sign) & 1) << 23) | - ((~(j2 ^ sign) & 1) << 22) | - ((upper & 0x03ff) << 12) | - ((lower & 0x07ff) << 1); + ((~(j1 ^ sign) & 1) << 23) | + ((~(j2 ^ sign) & 1) << 22) | + ((upper & 0x03ff) << 12) | + ((lower & 0x07ff) << 1); if (offset & 0x01000000) offset -= 0x02000000; offset += sym_val - (Elf32_Addr)where; @@ -509,7 +517,7 @@ static struct rt_module *_load_shared_object(const char *name, /* set module entry */ module->module_entry = module->module_space - + elf_module->e_entry - vstart_addr; + + elf_module->e_entry - vstart_addr; /* handle relocation section */ for (index = 0; index < elf_module->e_shnum; index ++) @@ -530,7 +538,7 @@ static struct rt_module *_load_shared_object(const char *name, symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset); strtab = (rt_uint8_t *)module_ptr + - shdr[shdr[shdr[index].sh_link].sh_link].sh_offset; + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset; nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel)); /* relocate every items */ @@ -543,7 +551,11 @@ static struct rt_module *_load_shared_object(const char *name, sym->st_shndx)); if ((sym->st_shndx != SHT_NULL) || - (ELF_ST_BIND(sym->st_info) == STB_LOCAL)) + (ELF_ST_BIND(sym->st_info) == STB_LOCAL) +#ifdef MODULE_USING_386 + || ( (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) && (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) ) +#endif + ) { rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space @@ -585,7 +597,7 @@ static struct rt_module *_load_shared_object(const char *name, /* find .dynsym section */ rt_uint8_t *shstrab; shstrab = (rt_uint8_t *)module_ptr + - shdr[elf_module->e_shstrndx].sh_offset; + shdr[elf_module->e_shstrndx].sh_offset; if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0) break; } @@ -597,10 +609,10 @@ static struct rt_module *_load_shared_object(const char *name, Elf32_Sym *symtab = RT_NULL; rt_uint8_t *strtab = RT_NULL; - symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset); + symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset); strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset; - for (i = 0; i < shdr[index].sh_size/sizeof(Elf32_Sym); i++) + for (i = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++) { if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC)) @@ -608,9 +620,9 @@ static struct rt_module *_load_shared_object(const char *name, } module->symtab = (struct rt_module_symtab *)rt_malloc - (count * sizeof(struct rt_module_symtab)); + (count * sizeof(struct rt_module_symtab)); module->nsym = count; - for (i = 0, count = 0; i < shdr[index].sh_size/sizeof(Elf32_Sym); i++) + for (i = 0, count = 0; i < shdr[index].sh_size / sizeof(Elf32_Sym); i++) { rt_size_t length; @@ -674,7 +686,7 @@ static struct rt_module* _load_relocated_object(const char *name, /* allocate module */ module = (struct rt_module *) - rt_object_allocate(RT_Object_Class_Module, (const char *)name); + rt_object_allocate(RT_Object_Class_Module, (const char *)name); if (module == RT_NULL) return RT_NULL; @@ -765,9 +777,9 @@ static struct rt_module* _load_relocated_object(const char *name, symtab = (Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset); strtab = (rt_uint8_t *)module_ptr + - shdr[shdr[shdr[index].sh_link].sh_link].sh_offset; + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset; shstrab = (rt_uint8_t *)module_ptr + - shdr[elf_module->e_shstrndx].sh_offset; + shdr[elf_module->e_shstrndx].sh_offset; nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel)); /* relocate every items */ @@ -791,7 +803,7 @@ static struct rt_module* _load_relocated_object(const char *name, rt_module_arm_relocate(module, rel, (Elf32_Addr)(rodata_addr + sym->st_value)); } - else if (rt_strncmp((const char*) + else if (rt_strncmp((const char *) (shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0) { /* relocate bss section */ @@ -842,7 +854,7 @@ static struct rt_module* _load_relocated_object(const char *name, else { rt_module_arm_relocate(module, rel, - (Elf32_Addr)((rt_uint8_t*) + (Elf32_Addr)((rt_uint8_t *) module->module_space - module_addr + sym->st_value)); @@ -856,7 +868,7 @@ static struct rt_module* _load_relocated_object(const char *name, } #define RT_MODULE_ARG_MAX 8 -static int _rt_module_split_arg(char* cmd, rt_size_t length, char* argv[]) +static int _rt_module_split_arg(char *cmd, rt_size_t length, char *argv[]) { int argc = 0; char *ptr = cmd; @@ -864,10 +876,10 @@ static int _rt_module_split_arg(char* cmd, rt_size_t length, char* argv[]) while ((ptr - cmd) < length) { /* strip bank and tab */ - while ((*ptr == ' ' || *ptr == '\t') && (ptr -cmd)< length) + while ((*ptr == ' ' || *ptr == '\t') && (ptr - cmd) < length) *ptr++ = '\0'; /* check whether it's the end of line */ - if ((ptr - cmd)>= length) break; + if ((ptr - cmd) >= length) break; /* handle string with quote */ if (*ptr == '"') @@ -875,7 +887,7 @@ static int _rt_module_split_arg(char* cmd, rt_size_t length, char* argv[]) argv[argc++] = ++ptr; /* skip this string */ - while (*ptr != '"' && (ptr-cmd) < length) + while (*ptr != '"' && (ptr - cmd) < length) if (*ptr ++ == '\\') ptr ++; if ((ptr - cmd) >= length) break; @@ -896,11 +908,11 @@ static int _rt_module_split_arg(char* cmd, rt_size_t length, char* argv[]) } /* module main thread entry */ -static void module_main_entry(void* parameter) +static void module_main_entry(void *parameter) { int argc; char *argv[RT_MODULE_ARG_MAX]; - typedef int (*main_func_t)(int argc, char** argv); + typedef int (*main_func_t)(int argc, char **argv); rt_module_t module = (rt_module_t) parameter; if (module == RT_NULL) @@ -922,7 +934,7 @@ static void module_main_entry(void* parameter) } rt_memset(argv, 0x00, sizeof(argv)); - argc = _rt_module_split_arg((char*)module->module_cmd_line, + argc = _rt_module_split_arg((char *)module->module_cmd_line, module->module_cmd_size, argv); if (argc == 0) return; @@ -936,19 +948,19 @@ static void module_main_entry(void* parameter) } /** - * This function will load a module with a main function from memory and create a + * This function will load a module with a main function from memory and create a * main thread for it * * @param name the name of module, which shall be unique * @param module_ptr the memory address of module image * @argc the count of argument - * @argd the argument data, which should be a + * @argd the argument data, which should be a * * @return the module object */ rt_module_t rt_module_do_main(const char *name, void *module_ptr, - const char* cmd_line, + const char *cmd_line, int line_size) { rt_module_t module; @@ -998,7 +1010,7 @@ rt_module_t rt_module_do_main(const char *name, if (line_size && cmd_line) { /* set module argument */ - module->module_cmd_line = (rt_uint8_t*)rt_malloc(line_size + 1); + module->module_cmd_line = (rt_uint8_t *)rt_malloc(line_size + 1); if (module->module_cmd_line) { rt_memcpy(module->module_cmd_line, cmd_line, line_size); @@ -1076,7 +1088,7 @@ rt_module_t rt_module_load(const char *name, void *module_ptr) #ifdef RT_USING_DFS #include <dfs_posix.h> -static char* _module_name(const char *path) +static char *_module_name(const char *path) { const char *first, *end, *ptr; char *name; @@ -1124,7 +1136,7 @@ rt_module_t rt_module_open(const char *path) /* check parameters */ RT_ASSERT(path != RT_NULL); - if (stat(path, &s) !=0) + if (stat(path, &s) != 0) { rt_kprintf("Module: access %s failed\n", path); @@ -1185,7 +1197,7 @@ rt_module_t rt_module_open(const char *path) * * @return the module object */ -rt_module_t rt_module_exec_cmd(const char *path, const char* cmd_line, int size) +rt_module_t rt_module_exec_cmd(const char *path, const char *cmd_line, int size) { struct stat s; int fd, length; @@ -1200,7 +1212,7 @@ rt_module_t rt_module_exec_cmd(const char *path, const char* cmd_line, int size) RT_ASSERT(path != RT_NULL); /* get file size */ - if (stat(path, &s) !=0) + if (stat(path, &s) != 0) { rt_kprintf("Module: access %s failed\n", path); goto __exit; @@ -1637,7 +1649,7 @@ static void rt_module_free_page(rt_module_t module, } else if (page[i].npage == npages) { - for (index = i; index < module->page_cnt-1; index ++) + for (index = i; index < module->page_cnt - 1; index ++) { page[index].page_ptr = page[index + 1].page_ptr; page[index].npage = page[index + 1].npage; @@ -1676,8 +1688,8 @@ void *rt_module_malloc(rt_size_t size) RT_DEBUG_NOT_IN_INTERRUPT; nunits = (size + sizeof(struct rt_mem_head) - 1) / - sizeof(struct rt_mem_head) - + 1; + sizeof(struct rt_mem_head) + + 1; RT_ASSERT(size != 0); RT_ASSERT(nunits != 0); @@ -1720,7 +1732,7 @@ void *rt_module_malloc(rt_size_t size) /* allocate pages from system heap */ npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1) / - RT_MM_PAGE_SIZE; + RT_MM_PAGE_SIZE; if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL) return RT_NULL; @@ -1753,7 +1765,7 @@ void rt_module_free(rt_module_t module, void *addr) RT_DEBUG_NOT_IN_INTERRUPT; RT_ASSERT(addr); - RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0); + RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) - 1)) == 0); RT_DEBUG_LOG(RT_DEBUG_MODULE, ("rt_module_free 0x%x\n", addr)); @@ -1786,8 +1798,8 @@ void rt_module_free(rt_module_t module, void *addr) if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0) { rt_size_t nunits = npage * - RT_MM_PAGE_SIZE / - sizeof(struct rt_mem_head); + RT_MM_PAGE_SIZE / + sizeof(struct rt_mem_head); /* split memory */ r = b + nunits; r->next = b->next; @@ -1823,8 +1835,8 @@ void rt_module_free(rt_module_t module, void *addr) if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0) { rt_size_t nunits = npage * - RT_MM_PAGE_SIZE / - sizeof(struct rt_mem_head); + RT_MM_PAGE_SIZE / + sizeof(struct rt_mem_head); /* split memory */ r = n + nunits; r->next = n->next; @@ -1909,8 +1921,8 @@ void *rt_module_realloc(void *ptr, rt_size_t size) } nunits = (size + sizeof(struct rt_mem_head) - 1) / - sizeof(struct rt_mem_head) - +1; + sizeof(struct rt_mem_head) + + 1; b = (struct rt_mem_head *)ptr - 1; if (nunits <= b->size) @@ -1972,7 +1984,7 @@ void *rt_module_realloc(void *ptr, rt_size_t size) { if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL; - rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head))); + rt_memmove(p, (b + 1), ((b->size) * sizeof(struct rt_mem_head))); rt_module_free(self_module, (void *)(b + 1)); return (void *)(p); |
