-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61dbc7c
commit 644b08d
Showing
12 changed files
with
574 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,129 @@ | ||
#include "dlinker.h" | ||
#include "elf_util.h" | ||
#include "klog.h" | ||
|
||
void list_dynamic_library_function(const char* path){ | ||
vfs_node_t file = vfs_open(path); | ||
if(file == NULL){ | ||
printk("Error: Cannot open file '%s'.\n",path); | ||
return; | ||
} | ||
Elf32_Ehdr *ehdr = kmalloc(file->size); | ||
if(vfs_read(file,ehdr,0,file->size) == -1){ | ||
kfree(ehdr); | ||
vfs_close(file); | ||
printk("Error: Cannot read file.\n"); | ||
return; | ||
} | ||
if(check_elf_magic(ehdr)){ | ||
if(ehdr->e_type == ET_DYN){ | ||
Elf32_Phdr *phdr = (Elf32_Phdr *)((char *)ehdr + ehdr->e_phoff); | ||
Elf32_Phdr *dynamic_phdr = NULL; | ||
|
||
for (int i = 0; i < ehdr->e_phnum; i++) { //查找动态段 | ||
if (phdr[i].p_type == PT_DYNAMIC) { | ||
dynamic_phdr = &phdr[i]; | ||
break; | ||
} | ||
} | ||
|
||
if(dynamic_phdr){ | ||
printk("Error: No has dynamic segment.\n"); | ||
kfree(ehdr); | ||
vfs_close(file); | ||
return; | ||
} | ||
|
||
Elf32_Dyn *dyn = (Elf32_Dyn *)(dynamic_phdr->p_vaddr); | ||
Elf32_Sym *symtab = NULL; // 符号表 | ||
const char *strtab = NULL; // 字符串表 | ||
size_t symtab_size = 0; | ||
|
||
// 遍历动态段,找到符号表和字符串表 | ||
while (dyn->d_tag != DT_NULL) { | ||
if (dyn->d_tag == DT_SYMTAB) { | ||
symtab = (Elf32_Sym *)dyn->d_un.d_ptr; | ||
} else if (dyn->d_tag == DT_STRTAB) { | ||
strtab = (const char *)dyn->d_un.d_ptr; | ||
} else if (dyn->d_tag == DT_SYMENT) { | ||
symtab_size = dyn->d_un.d_val; | ||
} | ||
dyn++; | ||
} | ||
|
||
if (!symtab || !strtab || !symtab_size){ | ||
printk("Error: No has symbol table or string table.\n"); | ||
kfree(ehdr); | ||
vfs_close(file); | ||
return; | ||
} | ||
size_t sym_count = symtab_size / sizeof(Elf32_Sym); | ||
for (size_t i = 0; i < sym_count; i++) { | ||
Elf32_Sym *sym = &symtab[i]; | ||
|
||
// 仅输出类型为函数的符号 | ||
if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC && sym->st_name != 0) { | ||
printk("Export function: %s\n", strtab + sym->st_name); | ||
} | ||
} | ||
|
||
kfree(ehdr); | ||
vfs_close(file); | ||
} else printk("Error: not a dynamic library\n"); | ||
} else printk("Error: not a elf file\n"); | ||
} | ||
|
||
void link_dynamic_library(const char* path,pcb_t task){ | ||
vfs_node_t file = vfs_open(path); | ||
if(file == NULL){ | ||
return; | ||
} | ||
Elf32_Ehdr *ehdr = kmalloc(file->size); | ||
if(vfs_read(file,ehdr,0,file->size) == -1){ | ||
kfree(ehdr); | ||
return; | ||
} | ||
if(check_elf_magic(ehdr)){ | ||
if(ehdr->e_type == ET_DYN){ | ||
Elf32_Phdr *phdr = (Elf32_Phdr *)((char *)ehdr + ehdr->e_phoff); | ||
Elf32_Phdr *dynamic_phdr = NULL; | ||
|
||
for (int i = 0; i < ehdr->e_phnum; i++) { //查找动态段 | ||
if (phdr[i].p_type == PT_DYNAMIC) { | ||
dynamic_phdr = &phdr[i]; | ||
break; | ||
} | ||
} | ||
|
||
if(dynamic_phdr) return; | ||
|
||
Elf32_Dyn *dyn = (Elf32_Dyn *)(dynamic_phdr->p_vaddr); | ||
Elf32_Sym *symtab = NULL; // 符号表 | ||
const char *strtab = NULL; // 字符串表 | ||
size_t symtab_size = 0; | ||
|
||
// 遍历动态段,找到符号表和字符串表 | ||
while (dyn->d_tag != DT_NULL) { | ||
if (dyn->d_tag == DT_SYMTAB) { | ||
symtab = (Elf32_Sym *)dyn->d_un.d_ptr; | ||
} else if (dyn->d_tag == DT_STRTAB) { | ||
strtab = (const char *)dyn->d_un.d_ptr; | ||
} else if (dyn->d_tag == DT_SYMENT) { | ||
symtab_size = dyn->d_un.d_val; | ||
} | ||
dyn++; | ||
} | ||
|
||
if (!symtab || !strtab || !symtab_size) return; | ||
size_t sym_count = symtab_size / sizeof(Elf32_Sym); | ||
for (size_t i = 0; i < sym_count; i++) { | ||
Elf32_Sym *sym = &symtab[i]; | ||
|
||
// 仅输出类型为函数的符号 | ||
if (ELF32_ST_TYPE(sym->st_info) == STT_FUNC && sym->st_name != 0) { | ||
printk("Function: %s\n", strtab + sym->st_name); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
#pragma once | ||
|
||
#include "pcb.h" | ||
|
||
void list_dynamic_library_function(const char* path); | ||
void link_dynamic_library(const char* path,pcb_t task); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.