Skip to content

Commit

Permalink
支持动态链接编译(apps无法运行)
Browse files Browse the repository at this point in the history
  • Loading branch information
copi143 committed Jan 15, 2025
1 parent 749d510 commit 6a0a0c6
Show file tree
Hide file tree
Showing 31 changed files with 440 additions and 182 deletions.
12 changes: 6 additions & 6 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ include_directories("${CMAKE_SOURCE_DIR}/apps/include/c")
include_directories("${CMAKE_SOURCE_DIR}/apps/include/c++")
link_directories("${CMAKE_SOURCE_DIR}/apps/lib")

add_exe_link_flag("-static -Wl,-Ttext=0x80000000 -e __libc_start_main")
add_exe_link_flag("-Wl,--export-dynamic-symbol=main")
add_exe_link_flag("-Wl,-Ttext=0x80000000 -e __libc_start_main")
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12)
add_exe_link_flag("-Wl,--image-base=0x78000000")
Expand All @@ -23,11 +24,10 @@ file(
"${CMAKE_SOURCE_DIR}/apps/*")
list(REMOVE_ITEM SUBDIRS "include" "lib")
foreach(SUBDIR ${SUBDIRS})
if(IS_DIRECTORY "${CMAKE_SOURCE_DIR}/apps/${SUBDIR}")
if(EXISTS "${CMAKE_SOURCE_DIR}/apps/${SUBDIR}/CMakeLists.txt")
add_subdirectory(${SUBDIR})
set(TARGETS ${TARGETS} ${SUBDIR})
endif()
if(EXISTS "${CMAKE_SOURCE_DIR}/apps/${SUBDIR}/CMakeLists.txt")
add_subdirectory(${SUBDIR})
target_sources(${SUBDIR} PRIVATE "${CMAKE_SOURCE_DIR}/apps/crt0.asm")
set(TARGETS ${TARGETS} ${SUBDIR})
elseif(EXISTS "${CMAKE_SOURCE_DIR}/apps/${SUBDIR}/Makefile")
add_custom_target(
${SUBDIR}
Expand Down
15 changes: 15 additions & 0 deletions apps/crt0.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[bits 32]
extern main, __libc_start_main
section .text
_start:
push main
push edx
push esi
push edi
call __libc_start_main
mov ebx, 0xffff
mov eax, 0
int 0x36
ud2
int 3
jmp $
4 changes: 2 additions & 2 deletions apps/file/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_binary(app-file)
target_link_libraries(app-file misc magic)
add_binary(file)
target_link_libraries(file misc magic)
16 changes: 10 additions & 6 deletions cmake/mklib
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
macro(addlib arg1)
# add_library(${arg1} SHARED ${ARGN})
# add_library(${arg1}-s STATIC ${ARGN})
add_library(${arg1} STATIC ${ARGN})
# set_target_properties(${arg1}-s PROPERTIES OUTPUT_NAME ${arg1})
add_library(${arg1} SHARED ${ARGN})
add_library(${arg1}-s STATIC ${ARGN})
set_target_properties(${arg1}-s PROPERTIES OUTPUT_NAME ${arg1})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
target_link_options(${arg1} PRIVATE -Wl,-z,notext)
endif()
endmacro(addlib)

macro(tgtlib arg1)
# target_link_libraries(${arg1} ${ARGN})
# target_link_libraries(${arg1}-s ${ARGN})
target_link_libraries(${arg1} ${ARGN})
set(ARGN_S ${ARGN})
list(TRANSFORM ARGN_S REPLACE "(.+)" "\\1-s")
target_link_libraries(${arg1}-s ${ARGN_S})
endmacro(tgtlib)

macro(libdef arg1)
target_compile_definitions(${arg1} PRIVATE ${ARGN})
target_compile_definitions(${arg1}-s PRIVATE ${ARGN})
endmacro(libdef)

macro(testexec arg1)
Expand Down
2 changes: 1 addition & 1 deletion include/define/config/plos.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#define SYSCALL_USE_INTERRPUT 0

#define PLOS_LOGGING 0
#define PLOS_LOGGING 2

#define PLOS_LOGGING_PRINTS 0

Expand Down
184 changes: 170 additions & 14 deletions include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ typedef struct Elf32ProgramHeader {
u32 type;
u32 offset;
u32 vaddr;
u32 paddr;
u32 paddr; // (ignored)
u32 filesz;
u32 memsz;
u32 flags;
Expand All @@ -177,26 +177,182 @@ typedef struct Elf64ProgramHeader {
u32 flags;
u64 offset;
u64 vaddr;
u64 paddr;
u64 paddr; // (ignored)
u64 filesz;
u64 memsz;
u64 align;
} Elf64ProgramHeader;
#endif

#define ELF_PROGRAM_TYPE_NULL 0 // Program header table entry unused.
#define ELF_PROGRAM_TYPE_LOAD 1 // Loadable segment.
#define ELF_PROGRAM_TYPE_DYNAMIC 2 // Dynamic linking information.
#define ELF_PROGRAM_TYPE_INTERP 3 // Interpreter information.
#define ELF_PROGRAM_TYPE_NOTE 4 // Auxiliary information.
#define ELF_PROGRAM_TYPE_SHLIB 5 // Reserved.
#define ELF_PROGRAM_TYPE_PHDR 6 // Segment containing program header table itself.
#define ELF_PROGRAM_TYPE_TLS 7 // Thread-Local Storage template.
#define ELF_PROGRAM_TYPE_LOOS 0x60000000
#define ELF_PROGRAM_TYPE_HIOS 0x6FFFFFFF
#define ELF_PROGRAM_TYPE_LOPROC 0x70000000
#define ELF_PROGRAM_TYPE_HIPROC 0x7fffffff
#define ELF_PROGRAM_TYPE_NULL 0 // Program header table entry unused.
#define ELF_PROGRAM_TYPE_LOAD 1 // Loadable segment.
#define ELF_PROGRAM_TYPE_DYNAMIC 2 // Dynamic linking information.
#define ELF_PROGRAM_TYPE_INTERP 3 // Interpreter information.
#define ELF_PROGRAM_TYPE_NOTE 4 // Auxiliary information.
#define ELF_PROGRAM_TYPE_SHLIB 5 // Reserved.
#define ELF_PROGRAM_TYPE_PHDR 6 // Segment containing program header table itself.
#define ELF_PROGRAM_TYPE_TLS 7 // Thread-Local Storage template.
#define ELF_PROGRAM_TYPE_LOOS 0x60000000
#define ELF_PROGRAM_TYPE_HIOS 0x6FFFFFFF
#define ELF_PROGRAM_TYPE_LOPROC 0x70000000
#define ELF_PROGRAM_TYPE_HIPROC 0x7fffffff
#define ELF_PROGRAM_TYPE_GNU_STACK 0x6474e551

#define ELF_PROGRAM_FLAG_EXEC MASK(0)
#define ELF_PROGRAM_FLAG_WRITE MASK(1)
#define ELF_PROGRAM_FLAG_READ MASK(2)

// --------------------------------------------------
//; Dynamic Section

typedef struct Elf32Dynamic {
u32 tag;
u32 value;
} Elf32Dynamic;

#ifdef __x86_64__
typedef struct Elf64Dynamic {
u64 tag;
u64 value;
} Elf64Dynamic;
#endif

#define ELF_DYNAMIC_NULL 0 // 结束标记
#define ELF_DYNAMIC_NEEDED 1 // 依赖库
#define ELF_DYNAMIC_PLTRELSZ 2 // 重定位表大小
#define ELF_DYNAMIC_PLTGOT 3 // 重定位表地址
#define ELF_DYNAMIC_HASH 4 // 符号哈希表地址
#define ELF_DYNAMIC_STRTAB 5 // 字符串表地址
#define ELF_DYNAMIC_SYMTAB 6 // 符号表地址
#define ELF_DYNAMIC_RELA 7
#define ELF_DYNAMIC_RELASZ 8
#define ELF_DYNAMIC_RELAENT 9
#define ELF_DYNAMIC_STRSZ 10
#define ELF_DYNAMIC_SYMENT 11
#define ELF_DYNAMIC_INIT 12 // 初始化函数地址
#define ELF_DYNAMIC_FINI 13 // 结束函数地址
#define ELF_DYNAMIC_SONAME 14
#define ELF_DYNAMIC_RPATH 15
#define ELF_DYNAMIC_SYMBOLIC 16
#define ELF_DYNAMIC_REL 17
#define ELF_DYNAMIC_RELSZ 18
#define ELF_DYNAMIC_RELENT 19
#define ELF_DYNAMIC_PLTREL 20
#define ELF_DYNAMIC_DEBUG 21
#define ELF_DYNAMIC_TEXTREL 22
#define ELF_DYNAMIC_JMPREL 23
#define ELF_DYNAMIC_BIND_NOW 24
#define ELF_DYNAMIC_INIT_ARRAY 25
#define ELF_DYNAMIC_FINI_ARRAY 26
#define ELF_DYNAMIC_INIT_ARRAYSZ 27
#define ELF_DYNAMIC_FINI_ARRAYSZ 28
#define ELF_DYNAMIC_RUNPATH 29
#define ELF_DYNAMIC_FLAGS 30
#define ELF_DYNAMIC_ENCODING 32
#define ELF_DYNAMIC_PREINIT_ARRAY 32
#define ELF_DYNAMIC_PREINIT_ARRAYSZ 33
#define ELF_DYNAMIC_MAXPOSTAGS 34
#define ELF_DYNAMIC_GNU_HASH 0x6ffffef5

// --------------------------------------------------
//; Symbol Table

typedef struct Elf32Symbol {
u32 name;
u32 value;
u32 size;
u8 info;
u8 other;
u16 shndx;
} Elf32Symbol;

#ifdef __x86_64__
typedef struct Elf64Symbol {
u32 name;
u8 info;
u8 other;
u16 shndx;
u64 value;
u64 size;
} Elf64Symbol;
#endif

// --------------------------------------------------
//; Hash Table

typedef struct Elf32Hash {
u32 nbucket;
u32 nchain;
u32 bucket;
u32 chain;
} Elf32Hash;

typedef struct Elf64Hash {
u32 nbucket;
u32 nchain;
u32 bucket;
u32 chain;
} Elf64Hash;

finline u32 elf_hash(cstr name) {
u32 h = 0;
for (usize i = 0; name[i] != '\0'; i++) {
h <<= 4;
h += name[i];
u32 g = h & 0xf0000000;
h ^= g;
h ^= g >> 24;
}
return h;
}

typedef struct Elf32GnuHash {
u32 nbuckets;
u32 symndx;
u32 maskwords;
u32 shift2;
u32 bloom[1];
} Elf32GnuHash;

// --------------------------------------------------
//; elf 加载时数据

typedef void (*ElfInitFunc)();
typedef void (*ElfFiniFunc)();

typedef struct Elf {
cstr path; // 文件路径 (不属于结构体所有)
usize size; // 文件大小
union {
const void *data;
const ElfIdent *ident;
const Elf32Header *header32;
#ifdef __x86_64__
const Elf64Header *header64;
#endif
};
i32 errcode;

cstr strtab;

union {
const Elf32Symbol *sym32;
#ifdef __x86_64__
const Elf64Symbol *sym64;
#endif
};

union {
const Elf32Hash *hash32;
#ifdef __x86_64__
const Elf32Hash *hash64;
#endif
};

ElfInitFunc init;
ElfFiniFunc fini;
const ElfInitFunc *init_array;
usize init_array_len;
const ElfFiniFunc *fini_array;
usize fini_array_len;
} Elf;
6 changes: 2 additions & 4 deletions include/kernel/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ typedef struct {
u32 eip;
} stack_frame;

#define SA_RPL_MASK 0xFFFC
#define SA_TI_MASK 0xFFFB
#define SA_TIL 4 // 设置此项,将从LDT中寻找
#define SA_RPL0 0
#define SA_RPL1 1
#define SA_RPL2 2
#define SA_RPL3 3
#define GET_SEL(cs, rpl) ((cs & SA_RPL_MASK & SA_TI_MASK) | (rpl))
#define SA_TIL 4 // 设置此项,将从LDT中寻找
#define GET_SEL(cs, rpl) (((cs) & 0xfff8) | (rpl))

typedef struct __PACKED__ TSS32 {
u32 backlink, esp0, ss0, esp1, ss1, esp2, ss2, cr3;
Expand Down
1 change: 1 addition & 0 deletions include/libc-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
#include "libc-base/asm/cpuid/ids.h"
#include "libc-base/asm/cpuid/macros.h"
#include "libc-base/asm/cr.h"
#include "libc-base/asm/flags.h"
#include "libc-base/asm/io.h"
#include "libc-base/asm/mem.h"
#include "libc-base/asm/msr.h"
Expand Down
1 change: 1 addition & 0 deletions include/libc-base/asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "asm/cpuid/ids.h"
#include "asm/cpuid/macros.h"
#include "asm/cr.h"
#include "asm/flags.h"
#include "asm/io.h"
#include "asm/mem.h"
#include "asm/msr.h"
Expand Down
63 changes: 63 additions & 0 deletions include/libc-base/asm/flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once
#include <define.h>

#define asm_get_flags() \
({ \
size_t flags; \
asm volatile("pushf\n\t" \
"pop %0\n\t" \
: "=r"(flags) \
:); \
flags; \
})

#define asm_set_flags(flags) \
({ \
asm volatile("push %0\n\t" \
"popf\n\t" \
: \
: "r"((size_t)(flags))); \
})

#define asm_is_sti (asm_get_flags() & (1 << 9))

#define asm_is_cli (!asm_is_sti)

#define FLAGS_CF MASK(0) // 进位标志
#define FLAGS_PF MASK(2) // 奇偶标志
#define FLAGS_AF MASK(4) // 辅助进位标志
#define FLAGS_ZF MASK(6) // 零标志
#define FLAGS_SF MASK(7) // 符号标志
#define FLAGS_TF MASK(8) // 跟踪标志
#define FLAGS_IF MASK(9) // 中断允许标志
#define FLAGS_DF MASK(10) // 方向标志
#define FLAGS_OF MASK(11) // 溢出标志
#define FLAGS_IOPL_0 ((usize)0 << 12)
#define FLAGS_IOPL_1 ((usize)1 << 12)
#define FLAGS_IOPL_2 ((usize)2 << 12)
#define FLAGS_IOPL_3 ((usize)3 << 12)

typedef struct FLAGS {
usize cf : 1;
usize reserved_1 : 1;
usize pf : 1;
usize reserved_2 : 1;
usize af : 1;
usize reserved_3 : 1;
usize zf : 1;
usize sf : 1;
usize tf : 1;
usize if_ : 1;
usize df : 1;
usize of : 1;
usize iopl : 2;
usize nt : 1;
usize reserved_4 : 1;
usize rf : 1;
usize vm : 1;
usize ac : 1;
usize vif : 1;
usize vip : 1;
usize id : 1;
usize reserved_5 : 10;
} FLAGS;
Loading

0 comments on commit 6a0a0c6

Please sign in to comment.