惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Wu.Country@侠缘

与我相关的一些链接 - Wu.Country@侠缘 [Linux]OpenSuse12图形和文字界面转换 haneWIN NFS Server An Introduction to the Linux-based Assignments Booting Linux with U-Boot on QEMU ARM Using The mkimage Tool To Create U-Boot Images SUSE安全大揭秘之“十诫” [Copy]如何使用qemu执行交叉环境下的内核镜像文件 CT-NG编译错误以及解决办法 无法忍受在SUSE10上安装开发环境了 Windows的路由命令 【CLFS】记录: Linux内核代码学习笔记(2.6.21.7 ARM) -- 内核启动函数start_kernel [读书笔记]Binary Hancks(1) ARM Stack Unwinding Physical Address Extension - PAE Memory and Windows 如何制作grub启动光盘 [译]Kernel Memory Layout on ARM Linux [转]ucLinux下sqlite数据库移植全攻略
[读书笔记]Binary Hancks(2) livepatch在X86下的实践
Wu.Country@侠 · 2010-02-22 · via 博客园 - Wu.Country@侠缘

livepatch是个可以给运行时的进程打热补丁的工具。它可以方便的修改运行进程中的变量,也可以方便的替换运行进程中的函数,使用新的库函数来取代原来主进程中的函数!

1、livepatch源码下载:
http://sourcehoge.net/Software/livepatch/

2、binutil下载(笔者使用的版本为2.15):
http://ftp.gnu.org/gnu/binutils/

3、编译binutil包:
2.15版本的binutil包有一个小BUG,编译时会报这个错误:
gcc -DHAVE_CONFIG_H -I. -Ihttp://www.cnblogs.com/binutils-2.15/gas -I. -D_GNU_SOURCE -I. -Ihttp://www.cnblogs.com/binutils-2.15/gas -I../bfd -Ihttp://www.cnblogs.com/binutils-2.15/gas/config -Ihttp://www.cnblogs.com/binutils-2.15/gas/../include -Ihttp://www.cnblogs.com/binutils-2.15/gas/.. -Ihttp://www.cnblogs.com/binutils-2.15/gas/../bfd -Ihttp://www.cnblogs.com/binutils-2.15/gas/../intl -I../intl -DLOCALEDIR="\"/home/public/study/binutils/target_x86/build/share/locale\""   -W -Wall -Wstrict-prototypes -Wmissing-prototypes -g -O2 -c http://www.cnblogs.com/binutils-2.15/gas/app.c
In file included from ./targ-cpu.h:1,
                 from http://www.cnblogs.com/binutils-2.15/gas/config/obj-elf.h:42,
                 from ./obj-format.h:1,
                 from http://www.cnblogs.com/binutils-2.15/gas/config/te-linux.h:4,
                 from ./targ-env.h:1,
                 from http://www.cnblogs.com/binutils-2.15/gas/as.h:626,
                 from http://www.cnblogs.com/binutils-2.15/gas/app.c:30:
http://www.cnblogs.com/binutils-2.15/gas/config/tc-i386.h:451: error: array type has incomplete element type
make[3]: *** [app.o] Error 1
make[3]: Leaving directory `/home/public/study/binutils/target_x86/gas'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/home/public/study/binutils/target_x86/gas'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/public/study/binutils/target_x86/gas'
make: *** [all-gas] Error 2
jimmy@linux-jimmy:/home/public/study/binutils/target_x86>

修改方法:
(1)、把../binutils-2.15/gas/config/tc-i386.h文件第451行:
extern const struct relax_type md_relax_table[];
修改为:
extern const struct relax_type * md_relax_table;

把../binutils-2.15/gas/config/tc-i386.c中对应的数据结构修改为:const struct relax_type md_relax_table_ex[];
添加:const struct relax_type * md_relax_table = md_relax_table_ex;

补丁: diff -Nur binutils-2.15 binutils-2.15.jimmy/
diff -Nur binutils-2.15/gas/config/tc-i386.c binutils-2.15.jimmy/gas/config/tc-i386.c
--- binutils-2.15/gas/config/tc-i386.c  2004-05-18 03:36:09.000000000 +0800
+++ binutils-2.15.jimmy/gas/config/tc-i386.c    2010-02-22 21:29:41.000000000 +0800
@@ -363,7 +363,7 @@
    prefix), and doesn't work, unless the destination is in the bottom
    64k of the code segment (The top 16 bits of eip are zeroed).  */

-const relax_typeS md_relax_table[] =
+const relax_typeS md_relax_table_ex[] =
 {
   /* The fields are:
      1) most positive reach of this state,
@@ -402,6 +402,8 @@
   {0, 0, 4, 0}
 };

+const relax_typeS * md_relax_table = md_relax_table_ex;
+
 static const arch_entry cpu_arch[] = {
   {"i8086",    Cpu086 },
   {"i186",     Cpu086|Cpu186 },
diff -Nur binutils-2.15/gas/config/tc-i386.h binutils-2.15.jimmy/gas/config/tc-i386.h
--- binutils-2.15/gas/config/tc-i386.h  2004-05-18 03:36:09.000000000 +0800
+++ binutils-2.15.jimmy/gas/config/tc-i386.h    2010-02-22 21:26:12.000000000 +0800
@@ -448,7 +448,7 @@

 #define md_operand(x)

-extern const struct relax_type md_relax_table[];
+extern const struct relax_type * md_relax_table;
 #define TC_GENERIC_RELAX_TABLE md_relax_table

 extern int optimize_align_code;

4、编译livepatch包:
这里要修改一下Makefile:
jimmy@linux-jimmy:/home/public/study/livepatch/source> cat Makefile
#
# Makefile for livepatch
# $Id: Makefile 330 2004-11-03 11:38:02Z ukai $
# Copyright (C) 2004 Fumitoshi UKAI <ukai@debian.or.jp>
# All rights reserved.
# This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of
# the GNU General Public License version 2.
#

BINUTILS_DIR=/home/public/study/binutils/target_x86/build

CFLAGS=-Wall -O2 -g -I$(BINUTILS_DIR)/include

all: livepatch

livepatch: livepatch.o
        $(CC) -o $@ $<  -L$(BINUTILS_DIR)/lib -lbfd -liberty -lopcodes

fixup: fixup.o
        $(CC) -o $@ $< -L$(BINUTILS_DIR)/lib -lbfd -liberty -lopcodes

bfd: bfd.o
        $(CC) -o $@ $< -L$(BIN_UTILS_DIR)/lib -lbfd -liberty -lopcodes

clean:
        -rm -f *.o
        -rm -f livepatch fixup bfd

# EOF

5、测试:
jimmy@linux-jimmy:/home/public/study/livepatch/test> ./test.sh
in main process test_func:0
in main process test_func_x:0
in main process test_func:1
in main process test_func_x:1
in main process test_func:2
in main process test_func_x:2
in main process test_func:3
in main process test_func_x:3
in main process test_func:4
in main process test_func_x:4
bfd_openr: No such file or directory
dl test @ 0xb7f0f000 [8220] libtest.so
jmp 0x804841f 0xb7f0f45c  <- 打上补丁,主进程的调用函数调用到补丁变库中!
in livepatch test_func:5
in main process test_func_x:-5  <- 补丁函数又回调到了主进程中的函数
in livepatch test_func:6
in main process test_func_x:-6
in livepatch test_func:7
in main process test_func_x:-7
in livepatch test_func:8
in main process test_func_x:-8
in livepatch test_func:9
in main process test_func_x:-9
in livepatch test_func:10
in main process test_func_x:-10
./test.sh: line 11:  8195 Killed                  ./test
jimmy@linux-jimmy:/home/public/study/livepatch/test>

 6、完整源码与测试包(不包括binutils):

 https://files.cnblogs.com/WuCountry/livepatch.rar

================================
  /\_/\                        
 (=^o^=)  Wu.Country@侠缘      
 (~)@(~)  一辈子,用心做一件事!
--------------------------------
  学而不思则罔,思而不学则怠!  
================================