首页 > 其他分享 >57 KVM工具使用指南-制作 LibcarePlus 热补丁

57 KVM工具使用指南-制作 LibcarePlus 热补丁

时间:2023-06-24 22:00:44浏览次数:43  
标签:kpatch bar foobar 57 KVM 补丁 patch LibcarePlus foo

57 KVM工具使用指南-制作 LibcarePlus 热补丁

57.1 概述

LibcarePlus 支持如下方式制作热补丁:

  • 手动制作
  • 通过脚本制作

手动制作热补丁的过程繁琐,对于代码量较大的工程,例如QEMU,手动制作热补丁极其困难。建议使用 LibcarePlus 自带脚本一键式地生成热补丁文件。

57.2 手动制作

本节以原文件 foo.c 和补丁文件 bar.c 为例,给出手动制作热补丁的指导。

  1. 准备 C 语言编写的原文件和补丁文件。例如原文件 foo.c 和补丁文件 bar.c 。

    <details style="margin: 0px; padding: 0px; box-sizing: inherit;"><summary style="margin: 0px; padding: 0px; box-sizing: inherit;">点击展开 foo.c</summary><p style="margin-top: var(--o-spacing-h8); margin-right: 0px; margin-bottom: var(--o-spacing-h8); margin-left: 0px; padding: 0px; box-sizing: inherit; font-size: var(--o-font-size-text); font-weight: normal; color: var(--e-color-text4); line-height: var(--o-line-height-text);"></p><div class="highlight" style="margin: 0px; padding: 0px; box-sizing: inherit;"><pre tabindex="0" style="margin: 0px; padding: 0px; box-sizing: inherit; position: relative; color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); tab-size: 4;"><span class="btn_pre_copy" style="margin: 0px; padding: 0px; box-sizing: inherit; cursor: pointer; width: 24px; height: 24px; position: absolute; top: 10px; right: 10px; background-image: url("/img/common/copy.png"); background-repeat: no-repeat; background-size: 100% 100%;"></span><code class="language-c hljs cpp" data-lang="c" style="margin: 0px; padding: 0.5em; box-sizing: inherit; font-style: normal; font-weight: normal; display: block; overflow-x: auto; background: rgb(39, 40, 34); color: rgb(221, 221, 221);"><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-comment" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-string" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-string" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-title" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-built_in" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(230, 219, 116);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(230, 219, 116);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-title" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></code></pre></div><details style="margin: 0px; padding: 0px; box-sizing: inherit;"><summary style="margin: 0px; padding: 0px; box-sizing: inherit;">点击展开 bar.c</summary><p style="margin-top: var(--o-spacing-h8); margin-right: 0px; margin-bottom: var(--o-spacing-h8); margin-left: 0px; padding: 0px; box-sizing: inherit; font-size: var(--o-font-size-text); font-weight: normal; color: var(--e-color-text4); line-height: var(--o-line-height-text);"></p><div class="highlight" style="margin: 0px; padding: 0px; box-sizing: inherit;"><pre tabindex="0" style="margin: 0px; padding: 0px; box-sizing: inherit; position: relative; color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); tab-size: 4;"><span class="btn_pre_copy" style="margin: 0px; padding: 0px; box-sizing: inherit; cursor: pointer; width: 24px; height: 24px; position: absolute; top: 10px; right: 10px; background-image: url("/img/common/copy.png"); background-repeat: no-repeat; background-size: 100% 100%;"></span><code class="language-c hljs cpp" data-lang="c" style="margin: 0px; padding: 0.5em; box-sizing: inherit; font-style: normal; font-weight: normal; display: block; overflow-x: auto; background: rgb(39, 40, 34); color: rgb(221, 221, 221);"><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-comment" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-string" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta-string" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-title" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-built_in" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(230, 219, 116);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(230, 219, 116);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(230, 219, 116);"><span class="hljs-string" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-title" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span></span></span><span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: inherit;"><span class="hljs-params" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-keyword" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(174, 129, 255);"><span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></code></pre></div></details></details>

  2. 编译得到原文件和补丁文件的汇编文件 foo.sbar.s,参考命令如下:

    # gcc -S foo.c
    # gcc -S bar.c
    # ls
    bar.c  bar.s  foo.c  foo.s
    
  3. 使用 kpatch_gensrc 对比 foo.s 和 bar.s 差异,生成包含原文件的汇编内容和差异内容的 foobar.s,参考命令如下:

    # sed -i 's/bar.c/foo.c/' bar.s
    # kpatch_gensrc --os=rhel6 -i foo.s -i bar.s -o foobar.s --force-global
    

    由于 kpatch_gensrc 默认对同一 C 语言原文件进行对比,所以对比前需要使用 sed 命令将补丁汇编文件 bar.s 中的 bar.c 改为原文件名称 foo.c。随后调用 kpatch_gensrc,指定输入文件为 foo.s 与 bar.s,输出文件为 foobar.s。

  4. 编译原文件的汇编文件 foo.s 和生成的汇编文件 foobar.s,得到可执行文件 foo 和 foobar,参考命令如下:

    # gcc -o foo foo.s
    # gcc -o foobar foobar.s -Wl,-q
    

    链接选项 -Wl, -q 将保留foobar中的重定位节。

  5. 利用 kpatch_strip 去除可执行程序 foo 和 foobar 的相同内容,保留制作热补丁所需要的内容。

    # kpatch_strip --strip foobar foobar.stripped
    # kpatch_strip --rel-fixup foo foobar.stripped
    # strip --strip-unneeded foobar.stripped
    # kpatch_strip --undo-link foo foobar.stripped
    

    上述命令中的各参数含义为:

    • –strip 用于去除 foobar 中对于补丁制作无用的 section;
    • –rel-fixup 用于修复补丁内所访问的变量以及函数的地址;
    • strip –strip-unneeded 用于去除对于热补丁重定位操作无用的符号信息;
    • –undo-link 用于将补丁内符号的地址从绝对地址更改为相对地址。
  6. 制作热补丁文件。

    通过以上操作,已经得到了热补丁制作所需的主要内容。接下来需要使用 kpatch_make 将原可执行文件的 Build ID 以及 kpatch_strip 的输出文件 foobar.stripped 作为参数传递给 kpatch_make,最终生成热补丁文件,参考命令如下:

    # str=$(readelf -n foo | grep 'Build ID')
    # substr=${str##* }
    # kpatch_make -b $substr -i 0001 foobar.stripped -o foo.kpatch
    # ls
    bar.c  bar.s  foo  foobar  foobar.s  foobar.stripped  foo.c  foo.kpatch  foo.s
    

    至此,就得到了patch ID为0001的热补丁文件 foo.kpatch。

57.3 通过脚本制作

本节介绍如何利用 LibcarePlus 自带的 libcare-patch-make 脚本制作热补丁文件,仍以原文件 foo.c 和补丁文件 bar.c 为例。

  1. 利用 diff 命令生成 foo.c 和 bar.c 的对比文件,命令如下所示:

    # diff -up foo.c bar.c > foo.patch
    

    foo.patch 文件内容如下所示:

    <details style="margin: 0px; padding: 0px; box-sizing: inherit;"><summary style="margin: 0px; padding: 0px; box-sizing: inherit;">点击展开 foo.patch</summary><p style="margin-top: var(--o-spacing-h8); margin-right: 0px; margin-bottom: var(--o-spacing-h8); margin-left: 0px; padding: 0px; box-sizing: inherit; font-size: var(--o-font-size-text); font-weight: normal; color: var(--e-color-text4); line-height: var(--o-line-height-text);"></p><div class="highlight" style="margin: 0px; padding: 0px; box-sizing: inherit;"><pre tabindex="0" style="margin: 0px; padding: 0px; box-sizing: inherit; position: relative; color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); tab-size: 4;"><span class="btn_pre_copy" style="margin: 0px; padding: 0px; box-sizing: inherit; cursor: pointer; width: 24px; height: 24px; position: absolute; top: 10px; right: 10px; background-image: url("/img/common/copy.png"); background-repeat: no-repeat; background-size: 100% 100%;"></span><code class="language-diff hljs" data-lang="diff" style="margin: 0px; padding: 0.5em; box-sizing: inherit; font-style: normal; font-weight: normal; display: block; overflow-x: auto; background: rgb(39, 40, 34); color: rgb(221, 221, 221);"><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-comment" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-comment" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"><span class="hljs-meta" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-deletion" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-addition" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-deletion" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(117, 113, 94);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-addition" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></code></pre></div></details>

  2. 编写编译 foo.c 的 Makefile 文件,具体如下所示:

    <details style="margin: 0px; padding: 0px; box-sizing: inherit;"><summary style="margin: 0px; padding: 0px; box-sizing: inherit;">点击展开 Makefile</summary><p style="margin-top: var(--o-spacing-h8); margin-right: 0px; margin-bottom: var(--o-spacing-h8); margin-left: 0px; padding: 0px; box-sizing: inherit; font-size: var(--o-font-size-text); font-weight: normal; color: var(--e-color-text4); line-height: var(--o-line-height-text);"></p><div class="highlight" style="margin: 0px; padding: 0px; box-sizing: inherit;"><pre tabindex="0" style="margin: 0px; padding: 0px; box-sizing: inherit; position: relative; color: rgb(248, 248, 242); background-color: rgb(39, 40, 34); tab-size: 4;"><span class="btn_pre_copy" style="margin: 0px; padding: 0px; box-sizing: inherit; cursor: pointer; width: 24px; height: 24px; position: absolute; top: 10px; right: 10px; background-image: url("/img/common/copy.png"); background-repeat: no-repeat; background-size: 100% 100%;"></span><code class="language-makefile hljs" data-lang="makefile" style="margin: 0px; padding: 0.5em; box-sizing: inherit; font-style: normal; font-weight: normal; display: block; overflow-x: auto; background: rgb(39, 40, 34); color: rgb(221, 221, 221);"><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-variable" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span class="hljs-variable" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(102, 217, 239);"><span class="hljs-variable" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span><span class="hljs-variable" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span><span class="hljs-variable" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span><span class="hljs-section" style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(166, 226, 46); font-weight: bold;"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"><span style="margin: 0px; padding: 0px; box-sizing: inherit; color: rgb(249, 38, 114);"></span></span></span><span style="margin: 0px; padding: 0px; box-sizing: inherit; display: flex;"><span style="margin: 0px; padding: 0px; box-sizing: inherit;"></span></span></code></pre></div></details>

  3. 编写好 Makefile 之后,直接调用 libcare-patch-make 即可。若 libcare-patch-make 询问选择哪个文件进行打补丁操作,输入原文件名即可,具体如下所示:

    # libcare-patch-make --clean -i 0001 foo.patch
    rm -f foo
    BUILDING ORIGINAL CODE
    /usr/local/bin/libcare-cc -o foo foo.c
    INSTALLING ORIGINAL OBJECTS INTO /libcareplus/test/lpmake
    mkdir $DESTDIR || :
    cp foo $DESTDIR
    applying foo.patch...
    can't find file to patch at input line 3
    Perhaps you used the wrong -p or --strip option?
    The text leading up to this was:
    --------------------------
    |--- foo.c	2020-12-10 09:43:04.445375845 +0800
    |+++ bar.c	2020-12-10 09:48:36.778379648 +0800
    --------------------------
    File to patch: foo.c         
    patching file foo.c
    BUILDING PATCHED CODE
    /usr/local/bin/libcare-cc -o foo foo.c
    INSTALLING PATCHED OBJECTS INTO /libcareplus/test/.lpmaketmp/patched
    mkdir $DESTDIR || :
    cp foo $DESTDIR
    MAKING PATCHES
    Fixing up relocation printf@@GLIBC_2.2.5+fffffffffffffffc
    Fixing up relocation print_hello+0
    patch for /libcareplus/test/lpmake/foo is in /libcareplus/test/patchroot/700297b7bc56a11e1d5a6fb564c2a5bc5b282082.kpatch
    

    执行成功之后,输出显示:热补丁文件位于当前目录的 patchroot 目录下,可执行文件则在 lpmake 目录下。脚本生成的热补丁文件默认是采用 Build ID 作为热补丁文件的文件名。

标签:kpatch,bar,foobar,57,KVM,补丁,patch,LibcarePlus,foo
From: https://blog.51cto.com/u_237826/6541944

相关文章

  • 代码随想录算法训练营第十五天| 110.平衡二叉树 (优先掌握递归) 257. 二叉树的所有路径
     110.平衡二叉树(优先掌握递归)难点:要求每个节点的左右字数的高度相减<=1,因此,需要对每个节点都进行检查,难就难在怎么获得任意节点的高度其中递归是最简单的: 1intisB_cursor(TreeNode*node,bool&isBalance)2{3if(isBalance==false)return0;4if......
  • NC20573 [SDOI2011]染色
    题目链接题目题目描述给定一棵有n个节点的无根树和m个操作,操作有2类:1、将节点a到节点b路径上所有点都染成颜色c;2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“112221”由3段组成:“11”、“222”和“1”。请你写一个程序依次完成这m......
  • 56 KVM工具使用指南-安装 LibcarePlus
    56KVM工具使用指南-安装LibcarePlus56.1安装软件依赖LibcarePlus运行依赖于libunwind、elfutils和binutils,在配置了yum源的openEuler系统上,可以参考如下命令安装LibcarePlus的依赖软件。#yuminstall-ybinutilselfutilselfutils-libelf-devellibunwind-deve......
  • STM32-mp157-A核Linux
    A核开发环境网卡连接1.NAT虚拟网卡连接互联网2.桥接(真实)网卡连接开发板ubuntu新建NAT网卡;设置桥接模式;搜索vmnet以管理员身份打开;设置桥接网卡为目标usb网卡;打开虚拟机linux网络设置;设置静态IPv4同一网段;ifconfig命令查看;打开windows网络设置;设置静态IPv4同一网段;打开终......
  • STM32-mp157d-M核裸机
    M核1.时钟配置2.延时函数HAL_delay(unsignedintMS);//实际延时时间:MS+1ms3.IO配置HAL库方法使用mp157时,stm32cubeMX不能生成GPIO初始化代码,需要根据具体分配情况自己完成GPIO初始化。初始化//配置PA5引脚__HAL_RCC_GPIOA_CLK_ENABLE();//使能Z组GPIO时钟GPIO......
  • P5717 【深基3.习8】三角形分类
    【深基3.习8】三角形分类题目描述给出三条线段$a,b,c$的长度,均是不大于$10000$的正整数。打算把这三条线段拼成一个三角形,它可以是什么三角形呢?如果三条线段不能组成一个三角形,输出Nottriangle;如果是直角三角形,输出Righttriangle;如果是锐角三角形,输出Acutetriangle;......
  • 单台服务器搭建不了虚拟化/云平台集群?是时候开启KVM嵌套虚拟化了!
    一、嵌套虚拟化嵌套虚拟化即让虚拟机支持虚拟化,也就是在虚拟机上再跑虚拟机,一般用于测试环境,当我们只有一台服务器,像模拟一个虚拟化集群、云平台的时候经常会使用到嵌套虚拟化。二、开启嵌套虚拟化本文以基于intel处理器为例,如果是AMD处理器,则讲本文的kvm_intel替换为kvm_amd即可。......
  • P5716 【深基3.例9】月份天数
    【深基3.例9】月份天数题目描述输入年份和月份,输出这一年的这一月有多少天。需要考虑闰年。输入格式输入两个正整数,分别表示年份$y$和月数$m$,以空格隔开。输出格式输出一行一个正整数,表示这个月有多少天。样例#1样例输入#119268样例输出#131样例#2样例输入......
  • P5709 【深基2.习6】Apples Prologue / 苹果和虫子
    【深基2.习6】ApplesPrologue/苹果和虫子题目描述八尾勇喜欢吃苹果。她现在有$m$($1\lem\le100$)个苹果,吃完一个苹果需要花费$t$($0\let\le100$)分钟,吃完一个后立刻开始吃下一个。现在时间过去了$s$($1\les\le10000$)分钟,请问她还有几个完整的苹果?输入格式输入三......
  • P5710 【深基3.例2】数的性质
    【深基3.例2】数的性质题目描述一些整数可能拥有以下的性质:性质1:是偶数;性质2:大于$4$且不大于$12$。小A喜欢这两个性质同时成立的整数;Uim喜欢这至少符合其中一种性质的整数;八尾勇喜欢刚好有符合其中一个性质的整数;正妹喜欢不符合这两个性质的整数。现在给出一个整数......