support elfloader for shared object and relocatable object

1. elfloader for shared object
example: TencentOS-tiny\examples\elfloader_shared_object
keil: TencentOS-tiny\board\TencentOS_tiny_EVB_MX_Plus\KEIL\elfloader_shared_object

2. elfloader for relocatable object:
example: TencentOS-tiny\examples\elfloader_relocatable_object
keil: TencentOS-tiny\board\TencentOS_tiny_EVB_MX_Plus\KEIL\elfloader_relocatable_object

3. TODO:
- add icache/dcache flush when module is loaded
- support more relocation type in elfloader_arch_relocate
This commit is contained in:
daishengdong
2020-06-09 19:30:38 +08:00
parent dc0c649c7c
commit 0a2d5a4e90
35 changed files with 7342 additions and 10 deletions

View File

@@ -0,0 +1,183 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _ELF32_H_
#define _ELF32_H_
#define EI_NIDENT 16
typedef int32_t elf32_sword;
typedef uint32_t elf32_word;
typedef uint16_t elf32_half;
typedef uint32_t elf32_off;
typedef uint32_t elf32_addr;
/* elf header */
typedef struct elf32_ehdr {
unsigned char e_ident[EI_NIDENT]; /* ident bytes */
elf32_half e_type; /* file type */
elf32_half e_machine; /* target machine */
elf32_word e_version; /* file version */
elf32_addr e_entry; /* start address */
elf32_off e_phoff; /* phdr file offset */
elf32_off e_shoff; /* shdr file offset */
elf32_word e_flags; /* file flags */
elf32_half e_ehsize; /* sizeof ehdr */
elf32_half e_phentsize; /* sizeof phdr */
elf32_half e_phnum; /* number phdrs */
elf32_half e_shentsize; /* sizeof shdr */
elf32_half e_shnum; /* number shdrs */
elf32_half e_shstrndx; /* shdr string index */
} elf32_ehdr_t;
/* values for e_type. */
#define ET_NONE 0 /* unknown type. */
#define ET_REL 1 /* relocatable. */
#define ET_EXEC 2 /* executable. */
#define ET_DYN 3 /* shared object. */
/* section header */
typedef struct elf32_shdr {
elf32_word sh_name; /* section name */
elf32_word sh_type; /* SHT_... */
elf32_word sh_flags; /* SHF_... */
elf32_addr sh_addr; /* virtual address */
elf32_off sh_offset; /* file offset */
elf32_word sh_size; /* section size */
elf32_word sh_link; /* misc info */
elf32_word sh_info; /* misc info */
elf32_word sh_addralign; /* memory alignment */
elf32_word sh_entsize; /* entry size if table */
} elf32_shdr_t;
/* values for sh_type */
#define SHT_NULL 0 /* inactive */
#define SHT_PROGBITS 1 /* program defined information */
#define SHT_SYMTAB 2 /* symbol table section */
#define SHT_STRTAB 3 /* string table section */
#define SHT_RELA 4 /* relocation section with addends*/
#define SHT_HASH 5 /* symbol hash table section */
#define SHT_DYNAMIC 6 /* dynamic section */
#define SHT_NOTE 7 /* note section */
#define SHT_NOBITS 8 /* no space section */
#define SHT_REL 9 /* relation section without addends */
#define SHT_SHLIB 10 /* reserved - purpose unknown */
#define SHT_DYNSYM 11 /* dynamic symbol table section */
#define SHT_LOPROC 0x70000000 /* reserved range for processor */
#define SHT_HIPROC 0x7fffffff /* specific section header types */
#define SHT_LOUSER 0x80000000 /* reserved range for application */
#define SHT_HIUSER 0xffffffff /* specific indexes */
/* values for sh_flags */
#define SHF_WRITE 1 /* writable */
#define SHF_ALLOC 2 /* occupies memory */
#define SHF_EXECINSTR 4 /* executable */
typedef struct elf32_rel {
elf32_addr r_offset; /* location to be relocated. */
elf32_word r_info; /* relocation type and symbol index. */
} elf32_rel_t;
typedef struct elf32_rela {
elf32_addr r_offset; /* location to be relocated. */
elf32_word r_info; /* relocation type and symbol index. */
elf32_sword r_addend; /* addend. */
} elf32_rela_t;
typedef struct elf32_sym {
elf32_word st_name; /* string table index of name. */
elf32_addr st_value; /* symbol value. */
elf32_word st_size; /* size of associated object. */
unsigned char st_info; /* type and binding information. */
unsigned char st_other; /* reserved (not used). */
elf32_half st_shndx; /* section index of symbol. */
} elf32_sym_t;
/* values for st_info(binding) */
#define STB_LOCAL 0
#define STB_GLOBAL 1
#define STB_WEAK 2
/* values for st_info(type) */
#define STT_NOTYPE 0
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
/* values for st_shndx */
#define SHN_ABS 0xFFF1
#define SHN_COMMON 0xFFF2
#define SHN_UNDEF 0x0000
#define ELF32_SYM_BINDING(info) ((info) >> 4)
#define ELF32_SYM_TYPE(info) ((info) & 0x0F)
typedef struct elf32_dyn {
elf32_sword d_tag; /* DT_... */
union {
elf32_word d_val;
elf32_addr d_ptr;
} d_un;
} elf32_dyn_t;
/* values for d_tag */
#define DT_NULL 0x00
#define DT_PLTRELSZ 0x02
#define DT_PLTGOT 0x03
#define DT_HASH 0x04
#define DT_STRTAB 0x05
#define DT_SYMTAB 0x06
#define DT_STRSZ 0x0a
#define DT_SYMENT 0x0b
#define DT_REL 0x11
#define DT_RELSZ 0x12
#define DT_RELENT 0x13
#define DT_PLTREL 0x14
#define DT_JMPREL 0x17
/* program header */
typedef struct elf32_phdr {
elf32_word p_type; /* PHT_... */
elf32_off p_offset; /* file offset */
elf32_addr p_vaddr; /* virtual address */
elf32_addr p_paddr; /* physical address */
elf32_word p_filesz; /* file size */
elf32_word p_memsz; /* memory size */
elf32_word p_flags; /* read write properties */
elf32_word p_align; /* alignment attribute, 2 ^ p_align */
} elf32_phdr_t;
/* values for p_type */
#define PHT_LOAD 0x01
#define PHT_DYNAMIC 0x02
#define ELF32_R_SYM(info) ((info) >> 8)
#define ELF32_R_TYPE(info) ((unsigned char)(info))
#define ELF_MAGIC_HEADER "\177ELF\001\001\001"
#define ELF_MAGIC_HEADER_SIZE 7
static const unsigned char elf_header_magic[] = {
0x7f, 0x45, 0x4c, 0x46, /* Magic: 0x7f, 'E', 'L', 'F' */
0x01, /* Class: ELF32 */
0x01, /* Data: 2's complement, 'little endian */
0x01, /* Version: 1(current) */
};
#endif /* _ELF32_H_ */

View File

@@ -0,0 +1,74 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_H_
#define _TOS_ELFLOADER_H_
#include "tos_k.h"
#include "elf/elf32.h"
#include "tos_elfloader_err.h"
#include "tos_elfloader_fd_read.h"
#include "tos_elfloader_symbol.h"
#include "tos_elfloader_symtab.h"
#include "tos_elfloader_relocate.h"
typedef struct el_section_st {
uint8_t shndx;
void *address;
} el_section_t;
typedef struct el_obj_info_st {
el_section_t bss;
el_section_t data;
el_section_t rodata;
el_section_t text;
} el_obj_info_t;
typedef struct el_so_info_st {
int32_t load_bias;
} el_so_info_t;
typedef union el_info_un {
el_obj_info_t obj;
el_so_info_t so;
} el_info_t;
typedef struct el_module_st {
int fd;
void *base;
el_info_t info;
uint32_t symtab_offset;
uint32_t symtab_size;
uint32_t symtab_entsize;
uint32_t strtab_offset;
} el_module_t;
__API__ el_err_t tos_elfloader_load(el_module_t *module, int fd);
__API__ el_err_t tos_elfloader_unload(el_module_t *module);
__API__ void *tos_elfloader_find_symbol(el_module_t *module, char *symbol);
#endif /* _TOS_ELFLOADER_H_ */

View File

@@ -0,0 +1,38 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_ERR_H_
#define _TOS_ELFLOADER_ERR_H_
typedef enum elfloader_err_en {
ELFLOADER_ERR_NONE,
ELFLOADER_ERR_HEADER_INVALID,
ELFLOADER_ERR_TYPE_INVALID,
ELFLOADER_ERR_NO_DYN,
ELFLOADER_ERR_NO_SYMTAB,
ELFLOADER_ERR_NO_STRTAB,
ELFLOADER_ERR_NO_TEXT,
ELFLOADER_ERR_NO_LOAD_SEGMENTS,
ELFLOADER_ERR_FD_READ_FAILED,
ELFLOADER_ERR_SECTION_NOT_FOUND,
ELFLOADER_ERR_SYM_NOT_FOUND,
ELFLOADER_ERR_OUT_OF_MEMORY,
ELFLOADER_ERR_PTR_NULL,
} el_err_t;
#endif /* _TOS_ELFLOADER_ERR_H_ */

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_FD_READ_H_
#define _TOS_ELFLOADER_FD_READ_H_
__KNL__ el_err_t elfloader_fd_read(int fd, uint32_t offset, void *buf, size_t len);
#endif /* _TOS_ELFLOADER_FD_READ_H_ */

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_RELOCATE_H_
#define _TOS_ELFLOADER_RELOCATE_H_
__KNL__ void elfloader_arch_relocate(uint32_t reloc_addr, int32_t load_bias, uint32_t addr, elf32_rela_t *rela, int is_rela);
#endif /* _TOS_ELFLOADER_RELOCATE_H_ */

View File

@@ -0,0 +1,29 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_SYMBOL_H_
#define _TOS_ELFLOADER_SYMBOL_H_
typedef struct el_symbol_st {
const char *name;
void *value;
} el_symbol_t;
extern const el_symbol_t el_symbols[];
#endif /* _TOS_ELFLOADER_SYMBOL_H_ */

View File

@@ -0,0 +1,24 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_ELFLOADER_SYMTAB_H_
#define _TOS_ELFLOADER_SYMTAB_H_
__KNL__ void *elfloader_symtab_lookup(char *name);
#endif /* _TOS_ELFLOADER_SYMTAB_H_ */

View File

@@ -0,0 +1,40 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
#include "tos_elfloader.h"
/*
ATTENTION:
if you wanna load the so/obj laying on your flash, you should implement "elfloader_fd_read"
with flash driver read/write operation.
very easy to use whether your module is on a file system or raw flash.
*/
__KNL__ __WEAK__ el_err_t elfloader_fd_read(int fd, uint32_t offset, void *buf, size_t len)
{
if (tos_vfs_lseek(fd, (vfs_off_t)offset, VFS_SEEK_SET) < 0) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (tos_vfs_read(fd, buf, len) < 0) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
return ELFLOADER_ERR_NONE;
}

View File

@@ -0,0 +1,70 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_elfloader.h"
/* values for ELF32_R_TYPE(info) */
#define R_ARM_ABS32 2
#define R_ARM_THM_CALL 10
#define R_ARM_GLOB_DAT 21
#define R_ARM_JUMP_SLOT 22
#define R_ARM_RELATIVE 23
/*
- S (when used on its own) is the address of the symbol.
- A is the addend for the relocation.
- P is the address of the place being relocated (derived from r_offset).
- Pa is the adjusted address of the place being relocated, defined as (P & 0xFFFFFFFC).
- T is 1 if the target symbol S has type STT_FUNC and the symbol addresses a Thumb instruction; it is 0
otherwise.
- B(S) is the addressing origin of the output segment defining the symbol S. The origin is not required to be the
base address of the segment. This value must always be word-aligned.
- GOT_ORG is the addressing origin of the Global Offset Table (the indirection table for imported data
addresses). This value must always be word-aligned. See §4.6.1.8, Proxy generating relocations.
- GOT(S) is the address of the GOT entry for the symbol S.
*/
// TODO: support more relocation type
__KNL__ void elfloader_arch_relocate(uint32_t reloc_addr, int32_t load_bias, uint32_t addr, elf32_rela_t *rela, int is_rela)
{
/* ATTENTION:
different reloc_addr calculation algorithm for relocatable object and shared object
*/
switch (ELF32_R_TYPE(rela->r_info)) {
case R_ARM_GLOB_DAT:
case R_ARM_JUMP_SLOT:
/* (S + A) | T */
*(uint32_t *)reloc_addr = addr;
break;
case R_ARM_RELATIVE:
/* B(S) + A */
*(uint32_t *)reloc_addr += load_bias;
break;
case R_ARM_ABS32:
/* (S + A) | T */
*(uint32_t *)reloc_addr += addr;
break;
default:
printf("Unsupported Relocation Type: %d\n", ELF32_R_TYPE(rela->r_info));
break;
}
}

View File

@@ -0,0 +1,32 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_elfloader.h"
__KNL__ void *elfloader_symtab_lookup(char *name)
{
const el_symbol_t *symbol;
for (symbol = &el_symbols[0]; symbol; ++symbol) {
if (strcmp(name, symbol->name) == 0) {
return symbol->value;
}
}
return K_NULL;
}

View File

@@ -0,0 +1,464 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_elfloader.h"
__STATIC__ void *local_symtab_lookup(int fd, char *sym_name,
el_section_t bss, el_section_t data, el_section_t rodata, el_section_t text,
uint32_t symtab_offset, uint32_t symtab_size, uint32_t symtab_entsize,
uint32_t strtab_offset)
{
int i = 0;
elf32_sym_t sym;
#define SYMBOL_NAME_MAX 30
static char name[SYMBOL_NAME_MAX];
for (i = 0; i < symtab_size / symtab_entsize; ++i) {
if (elfloader_fd_read(fd, symtab_offset, &sym, sizeof(elf32_sym_t)) != ELFLOADER_ERR_NONE) {
return K_NULL;
}
if (sym.st_name) {
if (elfloader_fd_read(fd, strtab_offset + sym.st_name, name, sizeof(name)) != ELFLOADER_ERR_NONE) {
return K_NULL;
}
if (strcmp(name, sym_name) == 0) {
if (sym.st_shndx == bss.shndx && bss.address) {
return (void *)((uint32_t)bss.address + sym.st_value);
} else if (sym.st_shndx == data.shndx && data.address) {
return (void *)((uint32_t)data.address + sym.st_value);
} else if (sym.st_shndx == rodata.shndx && rodata.address) {
return (void *)((uint32_t)rodata.address + sym.st_value);
} else if (sym.st_shndx == text.shndx && text.address) {
return (void *)((uint32_t)text.address + sym.st_value);
} else {
return K_NULL;
}
}
}
symtab_offset += symtab_entsize;
}
return K_NULL;
}
__STATIC__ el_err_t elfloader_relocate(int fd, void *address,
el_section_t bss, el_section_t data, el_section_t rodata, el_section_t text,
uint32_t rel_offset, uint32_t rel_size, uint32_t rel_entsize,
uint32_t symtab_offset, uint32_t symtab_size, uint32_t symtab_entsize,
uint32_t strtab_offset, uint32_t strtab_size)
{
int i = 0;
elf32_rela_t rela;
elf32_sym_t sym;
void *addr;
#define SYMBOL_NAME_MAX 30
static char name[SYMBOL_NAME_MAX];
int is_rela = (rel_entsize == sizeof(elf32_rela_t) ? K_TRUE : K_FALSE);
for (i = 0; i < rel_size / rel_entsize; ++i) {
addr = K_NULL;
if (elfloader_fd_read(fd, rel_offset, &rela, rel_entsize) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (elfloader_fd_read(fd,
symtab_offset + ELF32_R_SYM(rela.r_info) * symtab_entsize,
&sym,
symtab_entsize) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (sym.st_name) {
/* load the symbol's name */
if (elfloader_fd_read(fd,
strtab_offset + sym.st_name,
name,
sizeof(name)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/* an external symbol, or a weak one */
if (sym.st_shndx == SHN_UNDEF ||
ELF32_SYM_TYPE(sym.st_info) == STB_WEAK) {
/* look up in the global symbol table */
addr = elfloader_symtab_lookup(name);
}
/* an external symbol but not found in the global symbol table */
if (sym.st_shndx == SHN_UNDEF && !addr) {
return ELFLOADER_ERR_SYM_NOT_FOUND;
}
/* an internal symbol, or a weak symbol without STRONG one in global symbol table */
if (!addr) {
addr = local_symtab_lookup(fd, name,
bss, data, rodata, text,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset);
}
} else {
if (sym.st_shndx == bss.shndx) {
addr = bss.address;
} else if (sym.st_shndx == data.shndx) {
addr = data.address;
} else if (sym.st_shndx == rodata.shndx) {
addr = rodata.address;
} else if (sym.st_shndx == text.shndx) {
addr = text.address;
} else {
return ELFLOADER_ERR_SECTION_NOT_FOUND;
}
}
/* still not found */
if (!addr) {
return ELFLOADER_ERR_SYM_NOT_FOUND;
}
elfloader_arch_relocate(rela.r_offset + (uint32_t)address, 0, (uint32_t)addr, &rela, is_rela);
rel_offset += rel_entsize;
}
return ELFLOADER_ERR_NONE;
}
__API__ el_err_t tos_elfloader_load(el_module_t *module, int fd)
{
int i = 0;
el_err_t err;
elf32_ehdr_t ehdr;
elf32_shdr_t shdr;
elf32_shdr_t shstrtab;
static el_section_t bss, data, rodata, text;
#define SECTION_NAME_MAX 20
static char section_name[SECTION_NAME_MAX];
void *base = K_NULL, *addr_sec2cp; /* ram base for LOAD sections */
uint32_t shdr_offset;
uint32_t shstrtab_offset;
uint32_t strtab_offset, strtab_size = 0;
uint32_t symtab_offset, symtab_size = 0, symtab_entsize;
uint32_t text_offset = 0, text_size = 0;
uint32_t data_offset = 0, data_size = 0;
uint32_t rodata_offset = 0, rodata_size = 0;
uint32_t bss_size = 0;
uint32_t rel_text_offset, rel_text_size = 0, rel_text_entsize;
uint32_t rel_data_offset, rel_data_size = 0, rel_data_entsize;
uint32_t rel_rodata_offset, rel_rodata_size = 0, rel_rodata_entsize;
uint32_t rela_text_offset, rela_text_size = 0, rela_text_entsize;
uint32_t rela_data_offset, rela_data_size = 0, rela_data_entsize;
uint32_t rela_rodata_offset, rela_rodata_size = 0, rela_rodata_entsize;
if (!module) {
return ELFLOADER_ERR_PTR_NULL;
}
memset(module, 0, sizeof(el_module_t));
/* read the elf header */
if (elfloader_fd_read(fd, 0, &ehdr, sizeof(elf32_ehdr_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/* sanity check, magic verify */
if (memcmp(ehdr.e_ident, elf_header_magic, sizeof(elf_header_magic)) != 0) {
return ELFLOADER_ERR_HEADER_INVALID;
}
/* it should be a relocatable object */
if (ehdr.e_type != ET_REL) {
return ELFLOADER_ERR_TYPE_INVALID;
}
if (elfloader_fd_read(fd, ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shstrndx,
&shstrtab, ehdr.e_shentsize) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
shstrtab_offset = shstrtab.sh_offset;
shdr_offset = ehdr.e_shoff;
for (i = 0; i < ehdr.e_shnum; ++i) {
if (elfloader_fd_read(fd, shdr_offset, &shdr, sizeof(elf32_shdr_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (elfloader_fd_read(fd, shstrtab_offset + shdr.sh_name, section_name, sizeof(section_name)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/*
|-----------------------------------------------------|
| Name | sh_type | sh_flag |
|----------|--------------|---------------------------|
| .bss | SHT_NOBITS | SHF_ALLOC + SHF_WRITE |
| .data | SHT_PROGBITS | SHF_ALLOC + SHF_WRITE |
| .rodata | SHT_PROGBITS | SHF_ALLOC |
| .text | SHT_PROGBITS | SHF_ALLOC + SHF_EXECINSTR |
|-----------------------------------------------------|
*/
if (shdr.sh_type == SHT_NULL) {
;
} else if (shdr.sh_type == SHT_STRTAB) {
strtab_offset = shdr.sh_offset;
strtab_size = shdr.sh_size;
} else if (shdr.sh_type == SHT_SYMTAB) {
symtab_offset = shdr.sh_offset;
symtab_size = shdr.sh_size;
symtab_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_NOBITS && shdr.sh_flags == (SHF_ALLOC | SHF_WRITE)) {
bss_size = shdr.sh_size;
bss.shndx = i;
} else if (shdr.sh_type == SHT_PROGBITS && shdr.sh_flags == (SHF_ALLOC | SHF_WRITE)) {
data_offset = shdr.sh_offset;
data_size = shdr.sh_size;
data.shndx = i;
} else if (shdr.sh_type == SHT_PROGBITS && shdr.sh_flags == SHF_ALLOC) {
rodata_offset = shdr.sh_offset;
rodata_size = shdr.sh_size;
rodata.shndx = i;
} else if (shdr.sh_type == SHT_PROGBITS && shdr.sh_flags == (SHF_ALLOC | SHF_EXECINSTR)) {
text_offset = shdr.sh_offset;
text_size = shdr.sh_size;
text.shndx = i;
} else if (shdr.sh_type == SHT_REL &&
strncmp(".rel.data", section_name, 9) == 0) {
rel_data_offset = shdr.sh_offset;
rel_data_size = shdr.sh_size;
rel_data_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_RELA &&
strncmp(".rela.data", section_name, 10) == 0) {
rela_data_offset = shdr.sh_offset;
rela_data_size = shdr.sh_size;
rela_data_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_REL &&
(strncmp(".rel.rodata", section_name, 11) == 0 ||
strncmp(".rel.constdata", section_name, 14) == 0)) {
rel_rodata_offset = shdr.sh_offset;
rel_rodata_size = shdr.sh_size;
rel_rodata_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_RELA &&
(strncmp(".rela.rodata", section_name, 12) == 0 ||
strncmp(".rela.constdata", section_name, 15) == 0)) {
rela_rodata_offset = shdr.sh_offset;
rela_rodata_size = shdr.sh_size;
rela_rodata_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_REL &&
strncmp(".rel.text", section_name, 9) == 0) {
rel_text_offset = shdr.sh_offset;
rel_text_size = shdr.sh_size;
rel_text_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_RELA &&
strncmp(".rela.text", section_name, 10) == 0) {
rela_text_offset = shdr.sh_offset;
rela_text_size = shdr.sh_size;
rela_text_entsize = shdr.sh_entsize;
}
shdr_offset += ehdr.e_shentsize;
}
if (symtab_size == 0) {
return ELFLOADER_ERR_NO_SYMTAB;
}
if (strtab_size == 0) {
return ELFLOADER_ERR_NO_STRTAB;
}
if (text_size == 0) {
return ELFLOADER_ERR_NO_TEXT;
}
base = tos_mmheap_alloc(text_size + data_size + rodata_size + bss_size);
if (!base) {
return ELFLOADER_ERR_OUT_OF_MEMORY;
}
/* do sections load */
addr_sec2cp = base;
if (text_size > 0) {
if (elfloader_fd_read(fd, text_offset, addr_sec2cp, text_size) != ELFLOADER_ERR_NONE) {
err = ELFLOADER_ERR_FD_READ_FAILED;
goto OUT;
}
text.address = addr_sec2cp;
addr_sec2cp = (void *)((uint32_t)addr_sec2cp + text_size);
}
if (rodata_size > 0) {
if (elfloader_fd_read(fd, rodata_offset, addr_sec2cp, rodata_size) != ELFLOADER_ERR_NONE) {
err = ELFLOADER_ERR_FD_READ_FAILED;
goto OUT;
}
rodata.address = addr_sec2cp;
addr_sec2cp = (void *)((uint32_t)addr_sec2cp + rodata_size);
}
if (data_size > 0) {
if (elfloader_fd_read(fd, data_offset, addr_sec2cp, data_size) != ELFLOADER_ERR_NONE) {
err = ELFLOADER_ERR_FD_READ_FAILED;
goto OUT;
}
data.address = addr_sec2cp;
addr_sec2cp = (void *)((uint32_t)addr_sec2cp + data_size);
}
if (bss_size > 0) {
bss.address = addr_sec2cp;
memset(bss.address, 0, bss_size);
}
if (rel_data_size > 0) {
err = elfloader_relocate(fd, data.address,
bss, data, rodata, text,
rel_data_offset, rel_data_size, rel_data_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rela_data_size > 0) {
err = elfloader_relocate(fd, data.address,
bss, data, rodata, text,
rela_data_offset, rela_data_size, rela_data_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rel_rodata_size > 0) {
err = elfloader_relocate(fd, rodata.address,
bss, data, rodata, text,
rel_rodata_offset, rel_rodata_size, rel_rodata_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rela_rodata_size > 0) {
err = elfloader_relocate(fd, rodata.address,
bss, data, rodata, text,
rela_rodata_offset, rela_rodata_size, rela_rodata_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rel_text_size > 0) {
err = elfloader_relocate(fd, text.address,
bss, data, rodata, text,
rel_text_offset, rel_text_size, rel_text_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rela_text_size > 0) {
err = elfloader_relocate(fd, text.address,
bss, data, rodata, text,
rela_text_offset, rela_text_size, rela_text_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
/* TODO: should do icache/dcahe flush here, sth. like:
dcache_flush();
icache_flush();
*/
module->fd = fd;
module->base = base;
module->info.obj.bss = bss;
module->info.obj.data = data;
module->info.obj.rodata = rodata;
module->info.obj.text = text;
module->symtab_offset = symtab_offset;
module->symtab_size = symtab_size;
module->symtab_entsize = symtab_entsize;
module->strtab_offset = strtab_offset;
return ELFLOADER_ERR_NONE;
OUT:
if (base) {
tos_mmheap_free(base);
}
return err;
}
__API__ el_err_t tos_elfloader_unload(el_module_t *module)
{
if (!module || !module->base) {
return ELFLOADER_ERR_PTR_NULL;
}
tos_mmheap_free(module->base);
module->base = K_NULL;
return ELFLOADER_ERR_NONE;
}
__API__ void *tos_elfloader_find_symbol(el_module_t *module, char *symbol)
{
return local_symtab_lookup(module->fd, symbol,
module->info.obj.bss, module->info.obj.data, module->info.obj.rodata, module->info.obj.text,
module->symtab_offset, module->symtab_size, module->symtab_entsize,
module->strtab_offset);
}

View File

@@ -0,0 +1,353 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_elfloader.h"
__STATIC__ void *local_symtab_lookup(int fd, int32_t load_bias, char *sym_name,
uint32_t symtab_offset, uint32_t symtab_size, uint32_t symtab_entsize,
uint32_t strtab_offset)
{
int i = 0;
elf32_sym_t sym;
#define SYMBOL_NAME_MAX 30
static char name[SYMBOL_NAME_MAX];
for (i = 0; i < symtab_size / symtab_entsize; ++i) {
if (elfloader_fd_read(fd, symtab_offset, &sym, sizeof(elf32_sym_t)) != ELFLOADER_ERR_NONE) {
return K_NULL;
}
if (sym.st_name) {
if (elfloader_fd_read(fd, strtab_offset + sym.st_name, name, sizeof(name)) != ELFLOADER_ERR_NONE) {
return K_NULL;
}
if (strcmp(name, sym_name) == 0) {
return (void *)(sym.st_value + load_bias);
}
}
symtab_offset += symtab_entsize;
}
return K_NULL;
}
__STATIC__ el_err_t elfloader_relocate(int fd, int32_t load_bias,
uint32_t rel_offset, uint32_t rel_size, uint32_t rel_entsize,
uint32_t symtab_offset, uint32_t symtab_size, uint32_t symtab_entsize,
uint32_t strtab_offset, uint32_t strtab_size)
{
int i = 0;
elf32_rela_t rela;
elf32_sym_t sym;
void *addr;
#define SYMBOL_NAME_MAX 30
static char name[SYMBOL_NAME_MAX];
int is_rela = (rel_entsize == sizeof(elf32_rela_t) ? K_TRUE : K_FALSE);
for (i = 0; i < rel_size / rel_entsize; ++i) {
addr = K_NULL;
if (elfloader_fd_read(fd, rel_offset, &rela, rel_entsize) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (elfloader_fd_read(fd,
symtab_offset + ELF32_R_SYM(rela.r_info) * symtab_entsize,
&sym,
symtab_entsize) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/* load the symbol's name */
if (elfloader_fd_read(fd,
strtab_offset + sym.st_name,
name,
sizeof(name)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/* an external symbol, or a weak one */
if (sym.st_shndx == SHN_UNDEF ||
ELF32_SYM_TYPE(sym.st_info) == STB_WEAK) {
/* look up in the global symbol table */
addr = elfloader_symtab_lookup(name);
}
/* an external symbol but not found in the global symbol table */
if (sym.st_shndx == SHN_UNDEF && !addr) {
return ELFLOADER_ERR_SYM_NOT_FOUND;
}
/* an internal symbol, or a weak symbol without STRONG one in global symbol table */
if (!addr) {
addr = local_symtab_lookup(fd, load_bias, name,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset);
}
/* still not found */
if (!addr) {
return ELFLOADER_ERR_SYM_NOT_FOUND;
}
elfloader_arch_relocate(rela.r_offset + load_bias, load_bias, (uint32_t)addr, &rela, is_rela);
rel_offset += rel_entsize;
}
return ELFLOADER_ERR_NONE;
}
__API__ el_err_t tos_elfloader_load(el_module_t *module, int fd)
{
int i = 0;
el_err_t err;
elf32_ehdr_t ehdr;
elf32_shdr_t shdr;
elf32_dyn_t dyn;
elf32_phdr_t phdr;
void *base = K_NULL, *addr_seg2cp; /* ram base for LOAD segments */
int32_t load_bias;
uint32_t shdr_offset, phdr_offset;
uint32_t vaddr_start = (uint32_t)-1, vaddr_end = 0;
uint32_t dyn_offset, dyn_size = 0, dyn_entsize;
uint32_t strtab_offset, strtab_size = 0;
uint32_t symtab_offset, symtab_size = 0, symtab_entsize;
uint32_t rel_entsize;
uint32_t rel_dyn_offset, rel_dyn_size = 0;
uint32_t rel_plt_offset, rel_plt_size = 0;
if (!module) {
return ELFLOADER_ERR_PTR_NULL;
}
memset(module, 0, sizeof(el_module_t));
/* read the elf header */
if (elfloader_fd_read(fd, 0, &ehdr, sizeof(elf32_ehdr_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
/* sanity check, magic verify */
if (memcmp(ehdr.e_ident, elf_header_magic, sizeof(elf_header_magic)) != 0) {
return ELFLOADER_ERR_HEADER_INVALID;
}
/* it should be a shared object */
if (ehdr.e_type != ET_DYN) {
return ELFLOADER_ERR_TYPE_INVALID;
}
shdr_offset = ehdr.e_shoff;
for (i = 0; i < ehdr.e_shnum; ++i) {
if (elfloader_fd_read(fd, shdr_offset, &shdr, sizeof(elf32_shdr_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (shdr.sh_type == SHT_DYNAMIC) { /* dynamic section, .dynamic */
dyn_offset = shdr.sh_offset;
dyn_size = shdr.sh_size;
dyn_entsize = shdr.sh_entsize;
} else if (shdr.sh_type == SHT_DYNSYM) { /* .dynsym */
symtab_size = shdr.sh_size;
}
shdr_offset += ehdr.e_shentsize;
}
if (dyn_size == 0) {
return ELFLOADER_ERR_NO_DYN;
}
if (symtab_size == 0) {
return ELFLOADER_ERR_NO_SYMTAB;
}
for (i = 0; i < dyn_size / dyn_entsize; ++i) {
if (elfloader_fd_read(fd, dyn_offset, &dyn, sizeof(elf32_dyn_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (dyn.d_tag == DT_NULL) {
break;
} else if (dyn.d_tag == DT_SYMTAB) { /* dynamic symbol table */
symtab_offset = dyn.d_un.d_ptr;
} else if (dyn.d_tag == DT_SYMENT) { /* entry size of symbol table */
symtab_entsize = dyn.d_un.d_val;
} else if (dyn.d_tag == DT_STRTAB) { /* dynamic string table */
strtab_offset = dyn.d_un.d_ptr;
} else if (dyn.d_tag == DT_STRSZ) { /* size of dynamic string table */
strtab_size = dyn.d_un.d_val;
} else if (dyn.d_tag == DT_REL) { /* dynamic relocation table */
rel_dyn_offset = dyn.d_un.d_ptr;
} else if (dyn.d_tag == DT_RELSZ) { /* size of rel.dyn */
rel_dyn_size = dyn.d_un.d_val;
} else if (dyn.d_tag == DT_RELENT) { /* entry size of rel.dyn */
rel_entsize = dyn.d_un.d_val;
} else if (dyn.d_tag == DT_JMPREL) { /* plt relocation table. why NOT DT_PLTREL, confusing */
rel_plt_offset = dyn.d_un.d_ptr;
} else if (dyn.d_tag == DT_PLTRELSZ) { /* size of rel.plt */
rel_plt_size = dyn.d_un.d_val;
}
dyn_offset += dyn_entsize;
}
if (strtab_size == 0) {
return ELFLOADER_ERR_NO_STRTAB;
}
phdr_offset = ehdr.e_phoff;
for (i = 0; i < ehdr.e_phnum; ++i) {
if (elfloader_fd_read(fd, phdr_offset, &phdr, sizeof(elf32_phdr_t)) != ELFLOADER_ERR_NONE) {
return ELFLOADER_ERR_FD_READ_FAILED;
}
if (phdr.p_type == PHT_LOAD) {
if (phdr.p_vaddr < vaddr_start) {
vaddr_start = phdr.p_vaddr;
}
if (phdr.p_vaddr + phdr.p_memsz > vaddr_end) {
vaddr_end = phdr.p_vaddr + phdr.p_memsz;
}
}
phdr_offset += ehdr.e_phentsize;
}
if (vaddr_start == (uint32_t)-1 || vaddr_end == 0) {
return ELFLOADER_ERR_NO_LOAD_SEGMENTS;
}
/* reserving memory for LOAD segments */
base = tos_mmheap_aligned_alloc(vaddr_end - vaddr_start, 64);
if (!base) {
return ELFLOADER_ERR_OUT_OF_MEMORY;
}
load_bias = (uint32_t)base - vaddr_start;
/* do segments load */
phdr_offset = ehdr.e_phoff;
for (i = 0; i < ehdr.e_phnum; ++i) {
if (elfloader_fd_read(fd, phdr_offset, &phdr, sizeof(elf32_phdr_t)) != ELFLOADER_ERR_NONE) {
err = ELFLOADER_ERR_FD_READ_FAILED;
goto OUT;
}
if (phdr.p_type == PHT_LOAD) {
addr_seg2cp = (void *)(load_bias + phdr.p_vaddr);
if (elfloader_fd_read(fd,
phdr.p_offset,
addr_seg2cp,
phdr.p_filesz) != ELFLOADER_ERR_NONE) {
err = ELFLOADER_ERR_FD_READ_FAILED;
goto OUT;
}
if (phdr.p_memsz > phdr.p_filesz) {
/* clear bss */
memset((void *)((uint8_t *)addr_seg2cp + phdr.p_filesz),
0,
phdr.p_memsz - phdr.p_filesz);
}
}
phdr_offset += ehdr.e_phentsize;
}
if (rel_dyn_size > 0) {
err = elfloader_relocate(fd, load_bias,
rel_dyn_offset, rel_dyn_size, rel_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
if (rel_plt_size > 0) {
err = elfloader_relocate(fd, load_bias,
rel_plt_offset, rel_plt_size, rel_entsize,
symtab_offset, symtab_size, symtab_entsize,
strtab_offset, strtab_size);
if (err != ELFLOADER_ERR_NONE) {
goto OUT;
}
}
/* TODO: should do icache/dcahe flush here, sth. like:
dcache_flush();
icache_flush();
*/
module->fd = fd;
module->base = base;
module->info.so.load_bias = load_bias;
module->symtab_offset = symtab_offset;
module->symtab_size = symtab_size;
module->symtab_entsize = symtab_entsize;
module->strtab_offset = strtab_offset;
return ELFLOADER_ERR_NONE;
OUT:
if (base) {
tos_mmheap_free(base);
}
return err;
}
__API__ el_err_t tos_elfloader_unload(el_module_t *module)
{
if (!module || !module->base) {
return ELFLOADER_ERR_PTR_NULL;
}
tos_mmheap_free(module->base);
module->base = K_NULL;
return ELFLOADER_ERR_NONE;
}
__API__ void *tos_elfloader_find_symbol(el_module_t *module, char *symbol)
{
return local_symtab_lookup(module->fd, module->info.so.load_bias, symbol,
module->symtab_offset, module->symtab_size, module->symtab_entsize,
module->strtab_offset);
}

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_H_
#define _TOS_VFS_H_

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_DEVICE_H_
#define _TOS_VFS_DEVICE_H_

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_ERR_H_
#define _TOS_VFS_ERR_H_

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_FILE_H_
#define _TOS_VFS_FILE_H_
@@ -10,13 +27,13 @@ typedef struct vfs_inode_st vfs_inode_t;
#define VFS_PATH_MAX 31
// open flags(vfs_oflag_t): open method flags (3rd argument of tos_vfs_open)
#define VFS_OFLAG_READ 0x01
#define VFS_OFLAG_WRITE 0x02
#define VFS_OFLAG_EXISTING 0x00
#define VFS_OFLAG_CREATE_NEW 0x04
#define VFS_OFLAG_CREATE_ALWAYS 0x08
#define VFS_OFLAG_OPEN_ALWAYS 0x10
#define VFS_OFLAG_OPEN_APPEND 0x30
#define VFS_OFLAG_READ 0x01
#define VFS_OFLAG_WRITE 0x02
#define VFS_OFLAG_EXISTING 0x00
#define VFS_OFLAG_CREATE_NEW 0x04
#define VFS_OFLAG_CREATE_ALWAYS 0x08
#define VFS_OFLAG_OPEN_ALWAYS 0x10
#define VFS_OFLAG_OPEN_APPEND 0x30
typedef enum vfs_whence_en {
VFS_SEEK_SET, /* the offset is set to offset bytes */

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_FS_H_
#define _TOS_VFS_FS_H_

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_INODE_H_
#define _TOS_VFS_INODE_H_

View File

@@ -1,7 +1,24 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#ifndef _TOS_VFS_TYPES_H_
#define _TOS_VFS_TYPES_H_
typedef void VFS_DIR;
typedef void VFS_DIR;
typedef int32_t vfs_off_t;
typedef uint32_t vfs_oflag_t;
@@ -10,4 +27,5 @@ typedef uint32_t vfs_oflag_t;
#define ssize_t int
#endif
#endif
#endif /* _TOS_VFS_TYPES_H_ */

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
__API__ int tos_vfs_open(const char *pathname, vfs_oflag_t flags)

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
vfs_err_t tos_vfs_block_device_register(const char *device_name, vfs_blkdev_ops_t *ops)

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
__STATIC__ vfs_file_t vfs_file_pool[VFS_FILE_OPEN_MAX] = { { K_NULL, 0 } };

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
extern k_list_t k_vfs_fsmap_list;

View File

@@ -1,3 +1,20 @@
/*----------------------------------------------------------------------------
* Tencent is pleased to support the open source community by making TencentOS
* available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* If you have downloaded a copy of the TencentOS binary from Tencent, please
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
*
* If you have downloaded a copy of the TencentOS source code from Tencent,
* please note that TencentOS source code is licensed under the BSD 3-Clause
* License, except for the third-party components listed below which are
* subject to different license terms. Your integration of TencentOS into your
* own projects may require compliance with the BSD 3-Clause License, as well
* as the other licenses applicable to the third-party components included
* within TencentOS.
*---------------------------------------------------------------------------*/
#include "tos_vfs.h"
__STATIC__ TOS_LIST_DEFINE(k_vfs_inode_list);