在arm下运行libmodbus遇到的众多问题
今天在WSL2工作环境裁剪程序到arm开发板操作,用的是libmodbus库。 当中产生了非常多的问题,在这里做了记录(点击上标reference可以看)0,
第一个问题是so库文件在gcc链接过程文件无法识别 第二个问题是编译程序后的动态库缺失
第一个问题情况
When I use musl-gcc compile modbus file target to the arm platform something like *.so: file not recognized: file format not recognized1 two gcc compile the obj file, two ways is use the libmodbus-3.1.10(libmodbus.so.5.1.0), the third way libmodbus-3.1.6 static lib (libmodbus.a) arm-linux-musleabi-gcc
1
arm-linux-musleabi-gcc ~/lichee/modbuss.c -o ~/lichee/modbuss $(pkg-config --libs --cflags libmodbus)
1
arm-linux-musleabi-gcc /home/gah0/lichee/modbuss.c -I/usr/local/include/modbus -o /home/gah0/lichee/modbuss -L/usr/local/lib -lmodbus
1
arm-linux-musleabi-gcc /home/gah0/lichee/modbuss.c -I/home/gah0/libmodbus-3.1.6/include/modbus -o /home/gah0/lichee/modbuss -L/home/gah0/libmodbus-3.1.6/lib -lmodbus
x86_gcc
1
gcc modbuss.c -o modbuss $(pkg-config --libs --cflags libmodbus)
1
gcc /home/gah0/lichee/modbuss.c -I/usr/local/include/modbus -o /home/gah0/lichee/modbuss -L/usr/local/lib -lmodbus
1
gcc /home/gah0/lichee/modbuss.c -I/home/gah0/libmodbus-3.1.6/include/modbus -o /home/gah0/lichee/modbuss -L/home/gah0/libmodbus-3.1.6/lib -lmodbus
but four ways are failed,but it can compiled and run in WSL2 by x86_x64_gcc compiler, it none of my target
terminal output
/usr/local/lib/libmodbus.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
…
/usr/local/lib/libmodbus.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
…
/usr/local/lib/libmodbus.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
…
$ ./modbuss
Opening /dev/ttyS7 at 9600 bauds (N, 8, 1)
ERROR Can’t open the device /dev/ttyS7 (No such file or directory)
Connection failed: No such file or directory
…
$ ./modbuss
Opening /dev/ttyS7 at 9600 bauds (N, 8, 1)
ERROR Can’t open the device /dev/ttyS7 (No such file or directory)
Connection failed: No such file or directory
…
$ ./modbuss
Opening /dev/ttyS7 at 9600 bauds (N, 8, 1)
ERROR Can’t open the device /dev/ttyS7 (No such file or directory)
Connection failed: No such file or directory
This makes me suspect that there is the gcc compiler and ld libs has some incompatible
原因分析
解决方法在下一个标题,此处可以忽略
check the x86_gcc producted obj file and libmodbus file
the only reason I guess is the elf of libmodbus Machine: Advanced Micro Devices X86-64
,some flags or headers make musl-gcc failed to recognized the file formal
1
$ file modbuss && $ ldd modbuss && readelf -h /usr/lib/libmodbus.so.5.1.0 && readelf -h ~/libmodbus-3.1.6/lib/libmodbus.a && readelf -h ~/libmodbus-3.1.10/libs/libmodbus.so.5.1.0
terminal output
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
modbuss: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=acfe966fe458b0649bdef0a5ef81ef05b32e49c4, with debug_info, not stripped
...
linux-vdso.so.1 (0x00007fff58dc1000)
libmodbus.so.5 => /usr/local/lib/libmodbus.so.5 (0x00007f37278fb000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f372750a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3727d0a000)
....
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x1f5c
Start of program headers: 52 (bytes into file)
Start of section headers: 142712 (bytes into file)
Flags: 0x5000200, Version5 EABI, soft-float ABI
....
File: /home/gah0/libmodbus-3.1.6/lib/libmodbus.a(modbus.o)
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: REL (Relocatable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 116960 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program
headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 27
Section header string table index: 26
File: /home/gah0/libmodbus-3.1.6/lib/libmodbus.a(modbus-data.o)...
File: /home/gah0/libmodbus-3.1.6/lib/libmodbus.a(modbus-rtu.o)...
File: /home/gah0/libmodbus-3.1.6/lib/libmodbus.a(modbus-tcp.o)...
....
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: ARM
Version: 0x1
Entry point address: 0x1f5c
Start of program headers: 52 (bytes into file)
Start of section headers: 142712 (bytes into file)
Flags: 0x5000200, Version5 EABI, soft-float ABI
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 5
Size of section headers: 40 (bytes)
Number of section headers: 33
Section header string table index: 32
finally I try to use musleabi-gcc recompile libmodbus-master source for my computer,Run this command on libmodbus-master source
./configure --prefix=~/libmodbus-3.1.10 --host=arm-linux CC=arm-linux-musleabi-gcc CXX=arm-linux-musleabi-g++ && make install
3
libmodbus-master host by arm-linux-musleabi-gcc4, success solve the file not recognized
第一个问题解决办法
第一步
must use musl-gcc configuration host
1
2
3
4
5
wget https://github.com/richfelker/musl-cross-make/archive/master.tar.gz
tar xzf master.tar.gz
cd musl-cross-make-master
make -j64
make install
you can check the musl-gcc version
arm-linux-musleabi-gcc --version
第二步
为libmodbus配置musleabi交叉编译器,–prefix是安装路径5
1
sudo ./configure --prefix=~/libmodbus --host=arm-linux CC=arm-linux-musleabi-gcc CXX=arm-linux-musleabi-g++ --enable-static && sudo make && sudo make install
1
readelf -a ~/libmodbus-3.1.10/lib/libmodbus.so.5.1.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: ARM
Version: 0x1
Entry point address: 0x1f5c
Start of program headers: 52 (bytes into file)
Start of section headers: 142712 (bytes into file)
Flags: 0x5000200, Version5 EABI, soft-float ABI
1
arm-buildroot-linux-musleabi-gcc -I~/libmodbus/include/modbus modbuss.c -L~/libmodbus/lib -o modbuss -lmodbus
1
2
3
./modbuss
Opening /dev/ttyS7 at 9600 bauds (N, 8, 1)
Connection failed: I/O error
第二个问题情景
however, copy the obj file to arm borad and run
1
./modbuss
terminal output
Error loading shared library libmodbus.so.5: No such file or directory (needed by ./modbuss)
Error relocating ./modbuss: modbus_free: symbol not found
Error relocating ./modbuss: modbus_strerror: symbol not found
Error relocating ./modbuss: modbus_connect: symbol not found
Error relocating ./modbuss: modbus_set_response_timeout: symbol not found
Error relocating ./modbuss: modbus_new_rtu: symbol not found
Error relocating ./modbuss: modbus_read_registers: symbol not found
Error relocating ./modbuss: modbus_set_slave: symbol not found
Error relocating ./modbuss: modbus_set_debug: symbol not found
1
$ readelf -a /usr/lib/libmodbus.so.5.1.0
now the Type of elf is Shared object file
解决方法
finally copy lib of libmodbus to arm borad
1
cp libmodbus.* /usr/lib
the modbuss program can run now
1
2
3
$ ./modbuss
Opening /dev/ttyS7 at 9600 bauds (N, 8, 1)
Connection failed: I/O error
引用
[0] 使用arm-linux-musleabi-gcc交叉工具链编译程序会出现libmodbus.so动态库文件无法识别
[1] cross-compile-c-file-file-format-not-recognized
[2] libmodbus-master
[3] how to configure some libs
[5] the issue tell you how to configuration and fix undefined reference to `rpl_malloc’