1. Baixe a ferramenta patchelf
endereço do github: https://github.com/NixOS/patchelf
Link para download: https://github.com/NixOS/patchelf/releases
Eu baixei o PatchELF 0.12 diretamente
patchelf-0.12.tar.gz
2. Instale a ferramenta patchelf
Descompactar:
$ tar -axf patchelf-0.12.tar.gz
$ cd patchelf-0.12/
$ ls
bootstrap.sh COPYING Makefile.am README.md tests
BUGS flake.lock patchelf.1 release.nix version
configure.ac flake.nix patchelf.spec.in src
instalação:
$ ./bootstrap.sh
./bootstrap.sh: 2: ./bootstrap.sh: autoreconf: not found
Encontre soluções com base no erro:
$ sudo apt-get install autoconf automake libtool
Em seguida, continue a execução:
$ sh bootstrap.sh
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force --warnings=all
autoreconf: configure.ac: tracing
autoreconf: configure.ac: creating directory build-aux
autoreconf: configure.ac: not using Libtool
autoreconf: running: /usr/bin/autoconf --force --warnings=all
autoreconf: configure.ac: not using Autoheader
autoreconf: running: automake --add-missing --copy --force-missing --warnings=all
configure.ac:7: installing 'build-aux/compile'
configure.ac:5: installing 'build-aux/install-sh'
configure.ac:5: installing 'build-aux/missing'
src/Makefile.am: installing 'build-aux/depcomp'
parallel-tests: installing 'build-aux/test-driver'
autoreconf: Leaving directory `.'
$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking dependency style of gcc... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating tests/Makefile
config.status: creating patchelf.spec
config.status: executing depfiles commands
Depois disso:
make
make install
3. Verifique a instalação
patchelf --version
4. Use
(1) Ver bibliotecas dependentes
Supondo que o nome da biblioteca dinâmica ou programa executável compilado por você seja demo, tomamos aqui o demo como exemplo.
$ readelf -d demo
Dynamic section at offset 0x7e3c30 contains 35 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libtorch.so]
0x0000000000000001 (NEEDED) Shared library: [libc10.so]
0x0000000000000001 (NEEDED) Shared library: [libIlmImf-2_2.so.22]
0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]
0x0000000000000001 (NEEDED) Shared library: [librt.so.1]
0x0000000000000001 (NEEDED) Shared library: [libtorch_cpu.so]
0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000f (RPATH) Library rpath: [/home/wmz/Desktop/mypractice/libtorch/lib]
0x000000000000000c (INIT) 0x40c668
0x000000000000000d (FINI) 0xa1745c
0x0000000000000019 (INIT_ARRAY) 0xdd3348
0x000000000000001b (INIT_ARRAYSZ) 200 (bytes)
0x000000000000001a (FINI_ARRAY) 0xdd3410
0x000000000000001c (FINI_ARRAYSZ) 8 (bytes)
0x000000006ffffef5 (GNU_HASH) 0x400298
0x0000000000000005 (STRTAB) 0x4040b0
0x0000000000000006 (SYMTAB) 0x400a08
0x000000000000000a (STRSZ) 22774 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000015 (DEBUG) 0x0
0x0000000000000003 (PLTGOT) 0xde4000
0x0000000000000002 (PLTRELSZ) 8232 (bytes)
0x0000000000000014 (PLTREL) RELA
0x0000000000000017 (JMPREL) 0x40a640
0x0000000000000007 (RELA) 0x409ff8
0x0000000000000008 (RELASZ) 1608 (bytes)
0x0000000000000009 (RELAENT) 24 (bytes)
0x000000006ffffffe (VERNEED) 0x409e38
0x000000006fffffff (VERNEEDNUM) 7
0x000000006ffffff0 (VERSYM) 0x4099a6
0x0000000000000000 (NULL) 0x0
(2) Modificar rpath
patchelf --set-rpath 'ORIGIN' demo
Isso adiciona o caminho onde a demonstração do programa executável está localizada como o caminho de pesquisa da biblioteca dependente.
Referência: https://www.cnblogs.com/ar-cheng/p/13225342.html
Instruções para o site oficial:
README.md
PatchELF é um utilitário simples para modificar executáveis e bibliotecas ELF existentes. Em particular, ele pode fazer o seguinte:
-
Altere o carregador dinâmico ("interpretador ELF") de executáveis:
$ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program
-
Altere o número
RPATH
de executáveis e bibliotecas:$ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program
-
Reduza o número
RPATH
de executáveis e bibliotecas:$ patchelf --shrink-rpath my-program
Isso remove de
RPATH
todos os diretórios que não contêm uma biblioteca referenciada porDT_NEEDED
campos do executável ou biblioteca. Por exemplo, se um executável faz referência a uma bibliotecalibfoo.so
, tem um RPATH/lib:/usr/lib:/foo/lib
elibfoo.so
só pode ser encontrado em/foo/lib
, o novoRPATH
será/foo/lib
.Além disso, a
--allowed-rpath-prefixes
opção pode ser usada para ajustes adicionais do rpath. Por exemplo, se um executável tiver umRPATH
/tmp/build-foo/.libs:/foo/lib
, provavelmente é desejável manter a/foo/lib
referência em vez da/tmp
entrada. Para fazer isso, use:$ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program
-
Remova dependências declaradas em bibliotecas dinâmicas (
DT_NEEDED
entradas):$ patchelf --remove-needed libfoo.so.1 my-program
Esta opção pode ser fornecida várias vezes.
-
Adicione uma dependência declarada em uma biblioteca dinâmica (
DT_NEEDED
):$ patchelf --add-needed libfoo.so.1 my-program
Esta opção pode ser fornecida várias vezes.
-
Substitua uma dependência declarada em uma biblioteca dinâmica por outra (
DT_NEEDED
):$ patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program
Esta opção pode ser fornecida várias vezes.
-
Mudança
SONAME
de uma biblioteca dinâmica:$ patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3
Uso compatível com patchelf:
$ patchelf -h
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]
[--allowed-rpath-prefixes PREFIXES] With '--shrink-rpath', reject rpath entries not starting with the allowed prefix
[--print-rpath]
[--force-rpath]
[--add-needed LIBRARY]
[--remove-needed LIBRARY]
[--replace-needed LIBRARY NEW_LIBRARY]
[--print-needed]
[--no-default-lib]
[--clear-symbol-version SYMBOL]
[--output FILE]
[--debug]
[--version]
FILENAME...
No aplicativo real, ao definir o caminho da biblioteca em tempo de execução, você pode definir vários de uma vez, separados por dois pontos, como segue, este comando especifica três caminhos ao mesmo tempo, o caminho atual, o caminho lib no nível superior e aquele em cuda-10.2. caminho lib64:
$ patchelf --set-rpath 'ORIGIN:../lib/:/usr/local/cuda-10.2/lib64' dbnet_demo
Após a verificação, essas bibliotecas dependentes podem ser encontradas corretamente:
$ ldd dbnet_demo
linux-vdso.so.1 (0x00007ffc96ce1000)
libtorch.so => ../lib/libtorch.so (0x00007fb7014f9000)
libc10.so => ../lib/libc10.so (0x00007fb701275000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb701071000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb700e52000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb700c4a000)
libtorch_cpu.so => ../lib/libtorch_cpu.so (0x00007fb6f0a89000)
libtorch_cuda.so => ../lib/libtorch_cuda.so (0x00007fb6b5e89000)
libnvToolsExt.so.1 => /usr/local/cuda-10.2/lib64/libnvToolsExt.so.1 (0x00007fb6b5c80000)
libcudart.so.10.2 => /usr/local/cuda-10.2/lib64/libcudart.so.10.2 (0x00007fb6b5a02000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb6b5679000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb6b52db000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb6b50c3000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb6b4cd2000)
libgomp-7c85b1e2.so.1 => /home/wmz/Documents/pytorch-dbnet/build2/../lib/libgomp-7c85b1e2.so.1 (0x00007fb6b4aa8000)
/lib64/ld-linux-x86-64.so.2 (0x00007fb702206000)
libtensorpipe.so => /home/wmz/Documents/pytorch-dbnet/build2/../lib/libtensorpipe.so (0x00007fb6b45df000)
libcudart-80664282.so.10.2 => /home/wmz/Documents/pytorch-dbnet/build2/../lib/libcudart-80664282.so.10.2 (0x00007fb6b435e000)
libc10_cuda.so => /home/wmz/Documents/pytorch-dbnet/build2/../lib/libc10_cuda.so (0x00007fb6b412d000)
libnvToolsExt-3965bdd0.so.1 => /home/wmz/Documents/pytorch-dbnet/build2/../lib/libnvToolsExt-3965bdd0.so.1 (0x00007fb6b3f23000)
Execute o programa executável, você também pode obter o resultado correto:
$ ./dbnet_demo
ok
file_names.size():5
0001004.jpg
./data/0001004.jpg
[W TensorIterator.cpp:918] Warning: Mixed memory format inputs detected while calling the operator. The operator will output contiguous tensor even if some of the inputs are in channels_last format. (function operator())
The run time is: 0.408824s
CLOCKS_PER_SEC is: 1000000
0001001.jpg
./data/0001001.jpg
The run time is: 0.112061s
CLOCKS_PER_SEC is: 1000000
0001002.jpg
./data/0001002.jpg
The run time is: 0.100809s
CLOCKS_PER_SEC is: 1000000
0001003.jpg
./data/0001003.jpg
The run time is: 0.101038s
CLOCKS_PER_SEC is: 1000000
0000000.jpg
./data/0000000.jpg
The run time is: 0.095049s
CLOCKS_PER_SEC is: 1000000