提交 a3aacbd3 编写于 作者: qiuyiuestc's avatar qiuyiuestc

fix section copy bug && format code style

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2236 bbd45198-f89e-11dd-88c7-29a3b14d5316
上级 cb9cfb31
......@@ -59,7 +59,8 @@ static void rt_module_free_page(rt_module_t module, void *page_ptr, rt_size_t np
static rt_module_t rt_current_module = RT_NULL;
static struct rt_semaphore mod_sem;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL;
static struct rt_module_symtab *_rt_module_symtab_end = RT_NULL;
rt_list_t rt_module_symbol_list;
/**
......@@ -129,7 +130,8 @@ rt_err_t rt_module_set(rt_module_t module)
return RT_EOK;
}
static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel, Elf32_Addr sym_val)
static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel,
Elf32_Addr sym_val)
{
Elf32_Addr *where, tmp;
Elf32_Sword addend, offset;
......@@ -158,7 +160,8 @@ static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel, Elf3
break;
case R_ARM_REL32:
*where += sym_val - (Elf32_Addr)where;
RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_REL32: %x -> %x, sym %x, offset %x\n", where, *where, sym_val, rel->r_offset));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_REL32: %x -> %x, sym %x, offset %x\n",
where, *where, sym_val, rel->r_offset));
break;
case R_ARM_V4BX:
*where &= 0xf000000f;
......@@ -179,9 +182,9 @@ static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel, Elf3
break;
#endif
case R_ARM_RELATIVE:
*where += (Elf32_Addr)sym_val;
//RT_DEBUG_LOG(RT_DEBUG_MODULE,
//("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val));
*where = (Elf32_Addr)sym_val + *where;
RT_DEBUG_LOG(RT_DEBUG_MODULE,
("R_ARM_RELATIVE: 0x%x -> 0x%x 0x%x\n", where, *where, sym_val));
break;
case R_ARM_THM_CALL:
case R_ARM_THM_JUMP24:
......@@ -378,14 +381,13 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
ptr = module->module_space;
rt_memset(ptr, 0, module_size);
rt_kprintf(" load address at 0x%x\n", ptr);
for (index = 0; index < elf_module->e_phnum; index++)
{
if (phdr[index].p_type == PT_LOAD)
{
rt_memcpy(ptr, (rt_uint8_t *)elf_module + phdr[index].p_offset, phdr[index].p_filesz);
ptr += phdr[index].p_memsz;
rt_memcpy(ptr + phdr[index].p_paddr,
(rt_uint8_t *)elf_module + phdr[index].p_offset,
phdr[index].p_filesz);
}
}
......@@ -394,8 +396,6 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
if (IS_REL(shdr[index]))
{
rt_uint32_t i, nr_reloc;
Elf32_Sym *symtab;
......@@ -403,12 +403,16 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
rt_uint8_t *strtab;
static rt_bool_t unsolved = RT_FALSE;
if (!IS_REL(shdr[index])) continue;
/* get relocate item */
rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .rel.plt and .rel.dyn section */
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;
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;
nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
/* relocate every items */
......@@ -416,11 +420,15 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
{
Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
RT_DEBUG_LOG(RT_DEBUG_MODULE,
("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx));
RT_DEBUG_LOG(RT_DEBUG_MODULE, ("relocate symbol %s shndx %d\n",
strtab + sym->st_name, sym->st_shndx));
if((sym->st_shndx != SHT_NULL) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space + sym->st_value));
if((sym->st_shndx != SHT_NULL) ||
(ELF_ST_BIND(sym->st_info) == STB_LOCAL))
{
rt_module_arm_relocate(module, rel,
(Elf32_Addr)(module->module_space + sym->st_value));
}
else if(!linked)
{
Elf32_Addr addr;
......@@ -432,7 +440,8 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
if (addr == 0)
{
rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
rt_kprintf("can't find %s in kernel symbol table\n",
strtab + sym->st_name);
unsolved = RT_TRUE;
}
else
......@@ -448,13 +457,13 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
return RT_NULL;
}
}
}
/* construct module symbol table */
for (index = 0; index < elf_module->e_shnum; index ++)
{
/* find .dynsym section */
rt_uint8_t *shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
rt_uint8_t *shstrab;
shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0)
break;
}
......@@ -471,26 +480,32 @@ static struct rt_module* _load_shared_object(const char *name, void *module_ptr)
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))
if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) &&
(ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
count ++;
}
module->symtab = (struct rt_module_symtab *)rt_malloc(count * sizeof(struct rt_module_symtab));
module->symtab = (struct rt_module_symtab *)rt_malloc
(count * sizeof(struct rt_module_symtab));
module->nsym = count;
for (i=0, count=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))
{
rt_size_t length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
rt_size_t length;
if ((ELF_ST_BIND(symtab[i].st_info) != STB_GLOBAL) ||
(ELF_ST_TYPE(symtab[i].st_info) != STT_FUNC)) continue;
module->symtab[count].addr = (void *)(module->module_space + symtab[i].st_value);
length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
module->symtab[count].addr =
(void *)(module->module_space + symtab[i].st_value);
module->symtab[count].name = rt_malloc(length);
rt_memset((void *)module->symtab[count].name, 0, length);
rt_memcpy((void *)module->symtab[count].name, strtab + symtab[i].st_name, length);
rt_memcpy((void *)module->symtab[count].name,
strtab + symtab[i].st_name, length);
count ++;
}
}
}
return module;
}
......@@ -533,7 +548,8 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
return RT_NULL;
/* allocate module */
module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module, (const char *)name);
module = (struct rt_module *)rt_object_allocate(RT_Object_Class_Module,
(const char *)name);
if (module == RT_NULL)
return RT_NULL;
......@@ -556,26 +572,32 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
/* load text section */
if (IS_PROG(shdr[index]) && IS_AX(shdr[index]))
{
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load text 0x%x, size %d\n", ptr, shdr[index].sh_size));
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load text 0x%x, size %d\n",
ptr, shdr[index].sh_size));
ptr += shdr[index].sh_size;
}
/* load rodata section */
if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index]))
{
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
rodata_addr = (rt_uint32_t)ptr;
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load rodata 0x%x, size %d, rodata 0x%x\n", ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load rodata 0x%x, size %d, rodata 0x%x\n",
ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
ptr += shdr[index].sh_size;
}
/* load data section */
if (IS_PROG(shdr[index]) && IS_AW(shdr[index]))
{
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset, shdr[index].sh_size);
rt_memcpy(ptr, (rt_uint8_t*)elf_module + shdr[index].sh_offset,
shdr[index].sh_size);
data_addr = (rt_uint32_t)ptr;
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load data 0x%x, size %d, data 0x%x\n", ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load data 0x%x, size %d, data 0x%x\n",
ptr, shdr[index].sh_size, *(rt_uint32_t*)data_addr));
ptr += shdr[index].sh_size;
}
......@@ -584,67 +606,80 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
{
rt_memset(ptr, 0, shdr[index].sh_size);
bss_addr = (rt_uint32_t)ptr;
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n", ptr, shdr[index].sh_size));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("load bss 0x%x, size %d,\n",
ptr, shdr[index].sh_size));
}
}
/* set module entry */
module->module_entry = (rt_uint8_t*)module->module_space + elf_module->e_entry - module_addr;
module->module_entry = (rt_uint8_t*)module->module_space +
elf_module->e_entry - module_addr;
/* handle relocation section */
for (index = 0; index < elf_module->e_shnum; index ++)
{
if (IS_REL(shdr[index]))
{
rt_uint32_t i, nr_reloc;
Elf32_Sym *symtab;
Elf32_Rel *rel;
if (!IS_REL(shdr[index])) continue;
/* get relocate item */
rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset);
/* locate .dynsym and .dynstr */
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;
shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
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;
shstrab = (rt_uint8_t*) module_ptr +
shdr[elf_module->e_shstrndx].sh_offset;
nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel));
/* relocate every items */
for (i = 0; i < nr_reloc; i ++)
{
Elf32_Sym *sym = &symtab[ELF32_R_SYM(rel->r_info)];
RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", strtab + sym->st_name));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n",
strtab + sym->st_name));
if (sym->st_shndx != STN_UNDEF)
{
if((ELF_ST_TYPE(sym->st_info) == STT_SECTION)
|| (ELF_ST_TYPE(sym->st_info) == STT_OBJECT))
{
if (rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
if (rt_strncmp((const char*)(shstrab +
shdr[sym->st_shndx].sh_name), ELF_RODATA, 8) == 0)
{
/* relocate rodata section */
RT_DEBUG_LOG(RT_DEBUG_MODULE,("rodata\n"));
rt_module_arm_relocate(module, rel,(Elf32_Addr)(rodata_addr + sym->st_value));
rt_module_arm_relocate(module, rel,
(Elf32_Addr)(rodata_addr + sym->st_value));
}
else if(rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
else if(rt_strncmp((const char*)
(shstrab + shdr[sym->st_shndx].sh_name), ELF_BSS, 5) == 0)
{
/* relocate bss section */
RT_DEBUG_LOG(RT_DEBUG_MODULE,("bss\n"));
rt_module_arm_relocate(module, rel, (Elf32_Addr)bss_addr + sym->st_value);
rt_module_arm_relocate(module, rel,
(Elf32_Addr)bss_addr + sym->st_value);
}
else if(rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name), ELF_DATA, 6) == 0)
else if(rt_strncmp((const char*)(shstrab + shdr[sym->st_shndx].sh_name),
ELF_DATA, 6) == 0)
{
/* relocate data section */
RT_DEBUG_LOG(RT_DEBUG_MODULE,("data\n"));
rt_module_arm_relocate(module, rel, (Elf32_Addr)data_addr + sym->st_value);
rt_module_arm_relocate(module, rel,
(Elf32_Addr)data_addr + sym->st_value);
}
}
}
else if(ELF_ST_TYPE(sym->st_info) == STT_FUNC )
{
/* relocate function */
rt_module_arm_relocate(module, rel,
(Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value));
rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t*)
module->module_space - module_addr + sym->st_value));
}
else
{
......@@ -652,7 +687,9 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
if(ELF32_R_TYPE(rel->r_info) != R_ARM_V4BX)
{
RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n", strtab + sym->st_name));
RT_DEBUG_LOG(RT_DEBUG_MODULE,("relocate symbol: %s\n",
strtab + sym->st_name));
/* need to resolve symbol in kernel symbol table */
addr = rt_module_symbol_find((const char*)(strtab + sym->st_name));
if (addr != (Elf32_Addr)RT_NULL)
......@@ -661,18 +698,18 @@ static struct rt_module* _load_relocated_object(const char *name, void *module_p
RT_DEBUG_LOG(RT_DEBUG_MODULE,("symbol addr 0x%x\n", addr));
}
else
rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
rt_kprintf("can't find %s in kernel symbol table\n",
strtab + sym->st_name);
}
else
{
rt_module_arm_relocate(module, rel,
(Elf32_Addr)((rt_uint8_t*)module->module_space - module_addr + sym->st_value));
rt_module_arm_relocate(module, rel, (Elf32_Addr)((rt_uint8_t*)
module->module_space - module_addr + sym->st_value));
}
}
rel ++;
}
}
}
return module;
}
......@@ -741,18 +778,20 @@ rt_module_t rt_module_load(const char *name, void *module_ptr)
module->mem_list = RT_NULL;
/* create page array */
module->page_array = (void *)rt_malloc(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
module->page_array = (void *)rt_malloc
(PAGE_COUNT_MAX * sizeof(struct rt_page_info));
module->page_cnt = 0;
#endif
/* create module thread */
module->stack_size = 2048;
module->thread_priority = 25;
module->thread_priority = RT_THREAD_PRIORITY_MAX - 1;
module->module_thread = rt_thread_create(name,
(void(*)(void *))module->module_entry, RT_NULL,
module->stack_size,
module->thread_priority, 10);
rt_kprintf("thread entry 0x%x\n", module->module_entry);
module->module_thread->module_id = (void*)module;
module->parent.flag = RT_MODULE_FLAG_WITHENTRY;
......@@ -1104,7 +1143,8 @@ rt_module_t rt_module_find(const char *name)
/* try to find device object */
information = &rt_object_container[RT_Object_Class_Module];
for (node = information->object_list.next; node != &(information->object_list); node = node->next)
for (node = information->object_list.next;
node != &(information->object_list); node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
......@@ -1222,7 +1262,8 @@ void *rt_module_malloc(rt_size_t size)
rt_sem_take(&mod_sem, RT_WAITING_FOREVER);
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list;
(b = *prev) != RT_NULL; prev = &(b->next))
{
if (b->size > nunits)
{
......@@ -1259,7 +1300,8 @@ void *rt_module_malloc(rt_size_t size)
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
for (prev = (struct rt_mem_head **)&rt_current_module->mem_list;
(b = *prev) != RT_NULL; prev = &(b->next))
{
if (b > up + up->size)
break;
......@@ -1502,7 +1544,8 @@ void list_memlist(const char *name)
if (module == RT_NULL)
return;
for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
for (prev = (struct rt_mem_head **)&module->mem_list;
(b = *prev) != RT_NULL; prev = &(b->next))
{
rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册