Linux usa la herramienta patchelf

1. Descarga la herramienta patchelf

dirección de github: https://github.com/NixOS/patchelf

Enlace de descarga: https://github.com/NixOS/patchelf/releases

Descargué PatchELF 0.12 directamente

patchelf-0.12.tar.gz

2. Instale la herramienta patchelf

Abrir la cremallera:

$ 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

instalación:

$ ./bootstrap.sh 
./bootstrap.sh: 2: ./bootstrap.sh: autoreconf: not found

Encuentre soluciones basadas en el error:

$ sudo apt-get install autoconf automake libtool

Luego continúa la ejecución:

$ 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

Después de esto:

make
make install

3. Verifique la instalación

patchelf --version

4. Utilice

(1) Ver bibliotecas dependientes

Suponiendo que el nombre de la biblioteca dinámica o el programa ejecutable compilado por usted es demo, aquí tomamos demo como ejemplo.

$ 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

Esto agrega la ruta donde se encuentra la demostración del programa ejecutable como ruta de búsqueda de la biblioteca dependiente.

Referencia: https://www.cnblogs.com/ar-cheng/p/13225342.html

Instrucciones para el sitio web oficial:

README.md

PatchELF es una sencilla utilidad para modificar bibliotecas y ejecutables ELF existentes. En particular, puede hacer lo siguiente:

  • Cambie el cargador dinámico ("intérprete ELF") de ejecutables:

    $ patchelf --set-interpreter /lib/my-ld-linux.so.2 my-program
  • Cambie el  RPATH de ejecutables y bibliotecas:

    $ patchelf --set-rpath /opt/my-libs/lib:/other-libs my-program
  • Reducir el tamaño  RPATH de ejecutables y bibliotecas:

    $ patchelf --shrink-rpath my-program

    Esto elimina de  RPATH todos los directorios que no contienen una biblioteca referenciada por  DT_NEEDED campos del ejecutable o biblioteca. Por ejemplo, si un ejecutable hace referencia a una biblioteca  libfoo.so, tiene un RPATH  /lib:/usr/lib:/foo/liblibfoo.so solo se puede encontrar en  /foo/lib, el nuevo  RPATH será  /foo/lib.

    Además, la  --allowed-rpath-prefixes opción se puede utilizar para un mayor ajuste de rpath. Por ejemplo, si un ejecutable tiene un  RPATH /tmp/build-foo/.libs:/foo/lib, probablemente sea conveniente mantener la  /foo/lib referencia en lugar de la  /tmp entrada. Para lograr eso, use:

    $ patchelf --shrink-rpath --allowed-rpath-prefixes /usr/lib:/foo/lib my-program
  • Elimine las dependencias declaradas en bibliotecas dinámicas ( DT_NEEDED entradas):

    $ patchelf --remove-needed libfoo.so.1 my-program

    Esta opción se puede dar varias veces.

  • Agregue una dependencia declarada en una biblioteca dinámica ( DT_NEEDED):

    $ patchelf --add-needed libfoo.so.1 my-program

    Esta opción se puede dar varias veces.

  • Reemplace una dependencia declarada en una biblioteca dinámica con otra ( DT_NEEDED):

    $ patchelf --replace-needed liboriginal.so.1 libreplacement.so.1 my-program

    Esta opción se puede dar varias veces.

  • Cambio  SONAME de una biblioteca dinámica:

    $ patchelf --set-soname libnewname.so.3.4.5 path/to/libmylibrary.so.1.2.3

Uso admitido por 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...

En la aplicación real, al configurar la ruta de la biblioteca en tiempo de ejecución, puede configurar varias a la vez, separadas por dos puntos, de la siguiente manera, este comando especifica tres rutas al mismo tiempo, la ruta actual, la ruta lib en el nivel superior y la que está debajo de cuda-10.2. ruta lib64:

$ patchelf --set-rpath 'ORIGIN:../lib/:/usr/local/cuda-10.2/lib64' dbnet_demo

Después de verificar, estas bibliotecas dependientes se pueden encontrar correctamente:

$ 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)

Ejecute el programa ejecutable, también puede obtener el resultado correcto:

 

$ ./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

 

Supongo que te gusta

Origin blog.csdn.net/juluwangriyue/article/details/108617283
Recomendado
Clasificación