ELF 文件格式分析

定义: ELF: Executable and Linkable Format ELF文件常见的三种文件类型: relocatable (可重定位) executable(可执行) shared object(共享目标) core dumps (核心转储) 1 [root@centosgpt 10]# file process.oprocess.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped[root@centosgpt 10]# file dynamiccreateprocessdynamiccreateprocess: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=d6fa7d670a47b7aaf05d17c92c82508bcabc48dc, not stripped[root@centosgpt 10]# file libdynamicprocess.solibdynamicprocess.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0a9fb4457819ecf54d9762f3aff5eda0c19600fc, not stripped 组成: ...

2019-06-11 · 29 min · 6085 words · Garlic Space

Linux下实现一个系统调用

环境: 环境: cenos7 kernel: 3.10.0-862.el7.x86_64 gcc: gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 新版内核: linux-5.2-rc2 准备: 安装相关软件: 1 yum install make automake vim perl openssl* elfutils-libelf- curl gcc wget flex git build-essential ncurses-devel xz-utils libssl-dev bc flex libelf-dev bison elfutils-libelf-devel -y 下载内核源码并解压: tar -xf linux-5.2-rc2.tar.gz 编写系统调用: 新增系统调用表条目: cp syscall_64.tbl syscall_64.tbl.backup linux-5.2-rc2/arch/x86/entry/syscalls/syscall_64.tbl 新增下面一条 + 434 common iadd_test __x64_sys_iadd_test 其中434为新增的调用号 增加函数声明 linux-5.2-rc2/include/linux/syscalls.h:新增一行 asmlinkage long sys_iadd_test(int one, int two); 新增系统调用定义 kernel/linux-5.2-rc2/fs/iadd_test.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <linux/printk.h> #include <linux/syscalls.h> #include "internal.h" long do_iadd(const int one, const int two ) { long sum = 0L; sum = one + two; return sum; } SYSCALL_DEFINE2(iadd_test, const int, one, const int, two) { printk("call iaddtest..."); return do_iadd(one, two); } 修改Makfile 增加iadd_test.o ...

2019-06-02 · 2 min · 406 words · Garlic Space

linux内核中的进程列表

Linux链表数据结构及相关操作 定义 1 /include/linux/types.hstruct list_head { struct list_head *next, *prev; };与响应的结构组成双向链表。 初始化 1 2 3 4 /include/linux/list.h#define LIST_HEAD_INIT(name) { &(name), &(name) } #define LIST_HEAD(name) \ struct list_head name = LIST_HEAD_INIT(name) 方法 插入 1 static inline void list_add(struct list_head *new, struct list_head *head)static inline void list_add_tail(struct list_head *new, struct list_head *head) 删除 1 static inline void list_del(struct list_head *entry) 替换 1 2 static inline void list_replace(struct list_head *old, struct list_head *new) 交换 1 static inline void list_swap(struct list_head *entry1, struct list_head *entry2) 删除 1 2 static inline void list_move(struct list_head *list, struct list_head *head) 遍历 1 2 #define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) 获取节点某一成员 1 2 #define list_entry(ptr, type, member) \ container_of(ptr, type, member) 以上是基于2.4内核版本的宏, 2.6版本后引入RCU新的锁机制,相关宏定义后新增_rcu. ...

2019-05-26 · 3 min · 608 words · Garlic Space