可执行文件SCP传输时,可能导致库丢失原因探讨

可执行文件SCP传输时,可能导致库丢失原因探讨


0. 问题分析

从PC传输文件到嵌入式平台有以下集中方式,当使用SSH传输文件时,发现传输可执行文件压缩包时程序解压都可以正确执行,而直接传输可执行文件后,会出现程序链接库丢失问题。

image-20230413170145453

1. 简述

通过压缩包与直接传输方式,传输可执行文件 tree,比较他们的异同,压缩包方式为 A,直传为 B

2. file指令查看基本信息

注意,此时dynamically linked转变为statically linked

1
2
3
4
5
# A
ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=0c126f1b17601d33513b52c02a5e63358d912512, for GNU/Linux 3.14.0, with debug_info, not stripped

# B
ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, interpreter *empty*, stripped

3. ldd指令查看动态链接库连接情况

1
2
3
4
5
6
# A
linux-vdso.so.1 (0x0000ffff8a54f000)
libc.so.6 => /lib/libc.so.6 (0x0000ffff8a3b7000)
/lib/ld-linux-aarch64.so.1 (0x0000ffff8a521000)
# B
not a dynamic executable

4. nm指令查看符号信息

nm可以列出二进制可执行文件,动态库,静态库中的符号信息,包括符号的类型,符号名称,比如函数名,全局变量等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# A
000000000424b24 B Dflag
0000000000424b0c B Fflag
0000000000406d04 T Ftype
0000000000424b38 B Hflag
0000000000424b28 B Jflag
0000000000424af4 B Level
0000000000424b5c B Nflag
0000000000424b30 B Qflag
0000000000424af8 B Rflag
0000000000424b74 B Xflag
0000000000423e08 d _DYNAMIC
0000000000423fd8 d _GLOBAL_OFFSET_TABLE_
000000000040fcb0 R _IO_stdin_used
0000000000413d7c r __FRAME_END__
00000000004132f8 r __GNU_EH_FRAME_HDR
0000000000424890 D __TMC_END__
0000000000426508 B __bss_end__
0000000000424890 B __bss_start
# B
nm: warning: ./tree001 has a corrupt section with a size (6d616e656c696674) larger than the file size

5. readelf指令分析可执行文件结构

如果二进制文件是ELF格式的,通过file文件可以查看文件格式.使用readelf指令可以方便分析ELF文件的结构,比如节信息,elf头文件信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# A
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: AArch64
Version: 0x1
Entry point address: 0x401410
Start of program headers: 64 (bytes into file)
Start of section headers: 174024 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 36
Section header string table index: 35

# B
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: AArch64
Version: 0x1
Entry point address: 0x401410
Start of program headers: 64 (bytes into file)
Start of section headers: 174024 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 36
Section header string table index: 35
readelf: Warning: Section 0 has an out of range sh_link value of 1282605157
readelf: Warning: Section 0 has an out of range sh_info value of 1818588773
readelf: Warning: Section 1 has an out of range sh_link value of 1702064737

6.结论

传输可执行文件时,将文件压缩后再传输。


可执行文件SCP传输时,可能导致库丢失原因探讨
http://ziyangfu.github.io/2023/04/13/可执行文件SCP传输时,可能导致库丢失原因探讨/
作者
FZY
发布于
2023年4月13日
许可协议