linux 修改 elf 文件的dynamic linker 和 rpath

https://nixos.org/patchelf.html

下载地址

https://nixos.org/releases/patchelf/patchelf-0.9/patchelf-0.9.tar.gz

https://nixos.org/releases/patchelf/patchelf-0.9/patchelf-0.9.tar.bz2

PatchELF

PatchELF is a small utility to modify the dynamic linker and RPATH of ELF executables.

Description

Dynamically linked ELF executables always specify a dynamic linker or interpreter, which is a program that actually loads the executable along with all its dynamically linked libraries. (The kernel just loads the interpreter, not the executable.) For example, on a Linux/x86 system the ELF interpreter is typically the file /lib/ld-linux.so.2. It is sometimes necessary to use a different ELF interpreter, say, when you want to test a version of Glibc installed in a location other than /lib. And in the Nix Packages collection we build our own Glibc, which we obviously want to use. Building programs with a different interpreter is a matter of using the linker flag -dynamic-linker:

gcc -Wl,-dynamic-linker,/my/lib/ld-linux.so.2 ...

However, this doesn’t work with third-party binaries (such as Adobe Reader in Nixpkgs). This is where PatchELF comes in. You can simply rewrite the executable:

patchelf --set-interpreter /my/lib/my-ld-linux.so.2 program

This modifies the interpreter section of program to refer to the specified file. This is not quite trivial because the path of the new interpreter may be longer than the old one, in which case it won’t fit. PatchELF takes care of “growing” the executable with sufficient space at the beginning to contain the new interpreter field. As a result, the resulting program may be one page (4 KiB) larger.

Likewise, you can change the RPATH, the linker search path embedded into executables and dynamic libraries:

patchelf --set-rpath /opt/my-libs/lib:/foo/lib program

This causes the dynamic linker to search in /opt/my-libs/lib and /foo/lib for the shared libraries needed by program. Of course, you could also set the environment variable LD_LIBRARY_PATH, but that’s often inconvenient as it requires a wrapper script to set up the environment.

Finally, it is possible to remove unused paths from the RPATH:

patchelf --shrink-rpath program

A path is considered unused if none of the program’s library dependencies can be found in that path. This is primarily useful in the standard build environment of Nixpkgs, where it is used to get rid of unnecessary runtime dependencies.

Compatibility

PatchELF has been tested on i386-linuxx86_64-linux and powerpc-linux. It should definitely work on all 32 or 64-bit, big or little-endian Linux platforms. With minor modifications it should also work on other ELF platforms.

Download

The most recent stable release is PatchELF 0.9.

Unstable releases

You can get the latest source code of PatchELF from its Git repository. You can also download the latest pre-release, or view a list of all prereleases built in our build farm.

Bugs

You can report bugs in the issue tracker.

$ ./patchelf --help
syntax: ./patchelf
[--set-interpreter FILENAME]
[--page-size SIZE]
[--print-interpreter]
[--print-soname] Prints 'DT_SONAME' entry of .dynamic section. Raises an error if DT_SONAME doesn't exist
[--set-soname SONAME] Sets 'DT_SONAME' entry to SONAME.
[--set-rpath RPATH]
[--remove-rpath]
[--shrink-rpath]
[--print-rpath]
[--force-rpath]
[--add-needed LIBRARY]
[--remove-needed LIBRARY]
[--replace-needed LIBRARY NEW_LIBRARY]
[--print-needed]
[--no-default-lib]
[--debug]
[--version]
FILENAME

================= End

linux 修改 elf 文件的dynamic linker 和 rpath的更多相关文章

  1. Linux修改profile文件改错了,恢复的方法

    Linux修改profile文件改错了,恢复的方法在改profile的时候,改出问题了,除了cd以外的命令基本都不能用了,连vi都不能用了,上网查了下,用export PATH=/usr/bin:/u ...

  2. Linux之ELF文件初探

    对比windowsPE文件与概述 在windows中可执行文件是pe文件格式,Linux中可执行文件是ELF文件,其文件格式是ELF文件格式,在Linux下的ELF文件除了可执行文件(Excutabl ...

  3. linux 修改目录文件权限,目录文件所属用户,用户组

    1:查看命令:ll drwxr-xr-x  4 gamer ftp      4096 Mar  7 16:56 gstore drwxrwxrwx 10 root  ftp      4096 De ...

  4. Linux 修改hostname 文件

    linux 的机器修改hostname: 修改 /etc/hosts 修改 /etc/sysconfig/network 重启机器reboot

  5. Linux 修改默认文件关联打开程序

    从总体上讲 /etc/gnome/defaults.list 保存了全局的打开方式-/.local/share/applications/mimeapps.list 保存了个人的打开方式当这两个文件不 ...

  6. linux 修改hosts文件

    1.修改hostssudo gedit /etc/hosts2.添加解析记录( . )完整案例:127.0.0.1 localhost.localdomain localhost简洁记录:127.0. ...

  7. 利用linux判断elf文件是64位还是32位

    readelf 命令,参数为-h 例如 文件名为python >>>readelf -h python 得到的是ELF Header中的项Magic 第五个数 02时为64位,01时 ...

  8. ELF文件的加载过程(load_elf_binary函数详解)--Linux进程的管理与调度(十三)

    加载和动态链接 从编译/链接和运行的角度看,应用程序和库程序的连接有两种方式. 一种是固定的.静态的连接,就是把需要用到的库函数的目标代码(二进制)代码从程序库中抽取出来,链接进应用软件的目标映像中: ...

  9. Linux 可执行文件 ELF结构 及程序载入执行

    Linux下ELF文件类型分为以下几种: 1.可重定位文件,比如SimpleSection.o: 2.可运行文件,比如/bin/bash. 3.共享目标文件,比如/lib/libc.so. 在Linu ...

随机推荐

  1. CF 1083 B. The Fair Nut and Strings

    B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析:  建出trie树,给定的两个字符串就 ...

  2. vmware因为软件出过一次复制的错误导致不能复制到主机的解决方法

    只需要把vmware的虚拟机进程全部结束掉,然后重置(先设置不勾选复制等,然后保存后在勾选上并保存)一次虚拟机隔离设置(需要在关闭虚拟机的情况下设置,否则就是灰色不允许操作),然后再开启虚拟机,就能正 ...

  3. Selenium2+python自动化-文件上传

    前言 文件上传是web页面上很常见的一个功能,自动化成功中操作起来却不是那么简单. 一般分两个场景:一种是input标签,这种可以用selenium提供的send_keys()方法轻松解决:另外一种非 ...

  4. jmeter逻辑控制器

    刚开始学习,只写几种了解的逻辑控制器 1.简单控制器 只用来组合采样器和其他逻辑控制器,不影响jmeter的运行 2.循环控制器 用来循环执行采样器和其他逻辑控制器,例如一个用户发送特定请求多次,即可 ...

  5. xampp服务器搭建和使用

    1.安装完XAMPP后会出现Apache端口被占用的问题,一下方法解决 错误信息如下: Error: Apache shutdown unexpectedly. 9:37:01  [Apache] T ...

  6. Pyhone学习之环境搭建

    一.python 环境搭建 本章节我们将向大家介绍如何在本地搭建Python开发环境.Python可应用于多平台包括 Linux 和 Mac OS X.你可以通过终端窗口输入 "python ...

  7. 启动tomcat时 一闪而过解决方法(2)

    下面我先跟大家确认一下问题出现的前提条件(本机版本java:1.6.20,tomcat:6.0.32) 1)在eclipse里面启动tomcat时都是正常的. 2)在系统中配置了各种环境变量如下: J ...

  8. C++ 根据图片url 批量 下载图片

    最近需要用到根据图片URL批量下载到本地的操作.查找了相关资料,记录在这儿. 1.首先在CSV文件中提取出url ifstream fin("C:\\Users\\lenovo\\Deskt ...

  9. Kotlin 学习笔记(一)

    (Kotlin 学习笔记的文档结构基本按照 Java 核心技术一书的目录排列) 基本程序设计结构 数据类型 数字 类型 宽度 Double 64 Float 32 Long 64 Int 32 Sho ...

  10. 作业2-MathExam V2.0

    MathExam V2.0 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 20 50 • ...