ldd与otool

### ldd

ldd命令用于打印程序或者库文件所依赖的共享库列表
ldd不是一个可执行程序，而只是一个shell脚本

#### golang 编译链接动态链接库，而不是静态编译

体积小，需要链接动态库，对各平台的兼容性可能不太好

```
go install -buildmode=shared std
go build -linkshared hi.go
```

### Linux

```
ldd /usr/local/bin/openssl

	linux-vdso.so.1 =>  (0x00007fffa4913000)
	libssl.so.1.1 => /usr/local/lib/libssl.so.1.1 (0x00007fc0de4b5000)
	libcrypto.so.1.1 => /usr/local/lib/libcrypto.so.1.1 (0x00007fc0ddfc5000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc0ddda8000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc0dd9de000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc0dd7da000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fc0de748000)
```

### Mac

```
otool -L /usr/local/opt/openssl/bin/openssl

	/usr/local/Cellar/openssl/1.0.2r/lib/libssl.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/local/Cellar/openssl/1.0.2r/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)
```

### LD_TRACE_LOADED_OBJECTS

```
export LD_TRACE_LOADED_OBJECTS=1

ls
  linux-vdso.so.1 =>  (0x00007fffc83fd000)
  libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f159e40e000)
  libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f159e044000)
  libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f159ddd4000)
  libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f159dbd0000)
  /lib64/ld-linux-x86-64.so.2 (0x00007f159e630000)
  libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f159d9b3000)
```

### objdump

```
objdump -s -j .interp `which openssl`

Contents of section .interp:
 400238 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 400248 7838362d 36342e73 6f2e3200           x86-64.so.2.

################################

objdump -s -j .interp `which ls`

Contents of section .interp:
 400238 2f6c6962 36342f6c 642d6c69 6e75782d  /lib64/ld-linux-
 400248 7838362d 36342e73 6f2e3200           x86-64.so.2.
```

### readelf

```
readelf -x .interp `which openssl`

Hex dump of section '.interp':
  0x00400238 2f6c6962 36342f6c 642d6c69 6e75782d /lib64/ld-linux-
  0x00400248 7838362d 36342e73 6f2e3200          x86-64.so.2.

readelf -x .interp `which ls`

Hex dump of section '.interp':
  0x00400238 2f6c6962 36342f6c 642d6c69 6e75782d /lib64/ld-linux-
  0x00400248 7838362d 36342e73 6f2e3200          x86-64.so.2.

readelf -x .interp ./ckb-wallet-linux
Warning: Section '.interp' was not dumped because it does not exist!

```

```
readelf -d `which openssl`

Dynamic section at offset 0x94d88 contains 28 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libssl.so.1.1]
 0x0000000000000001 (NEEDED)             Shared library: [libcrypto.so.1.1]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000001d (RUNPATH)            Library runpath: [/usr/local/lib]
 0x000000000000000c (INIT)               0x4171c8
 0x000000000000000d (FINI)               0x46cc34
 0x0000000000000019 (INIT_ARRAY)         0x6948e8
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x6948f0
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x400298
 0x0000000000000005 (STRTAB)             0x408388
 0x0000000000000006 (SYMTAB)             0x4003f0
 0x000000000000000a (STRSZ)              25298 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x695000
 0x0000000000000002 (PLTRELSZ)           32328 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x40f380
 0x0000000000000007 (RELA)               0x40f200
 0x0000000000000008 (RELASZ)             384 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x40f100
 0x000000006fffffff (VERNEEDNUM)         4
 0x000000006ffffff0 (VERSYM)             0x40e65a
 0x0000000000000000 (NULL)               0x0
```

---

参考：

https://www.linuxsong.org/2015/04/mac-os-x-ldd-strace
http://man.linuxde.net/ldd
https://coolshell.cn/articles/1626.html
https://stackoverflow.com/questions/1685483/how-can-i-examine-contents-of-a-data-section-of-an-elf-file-on-linux
https://www.jianshu.com/p/ef462437b999