当前位置: 首页 > 工具软件 > ltrace > 使用案例 >

aarch64平台交叉编译ltrace工具

凌宏大
2023-12-01
/*********************************************************************
* Author          :     lile
* Modified        :     2019年12月28日星期六  10:16:13
* Email           :     roger0212@163.com
* HomePage        :     lile777.blog.csdn.net
* CopyRight       :     该文章版权由lile所有。
*                       保留本版权声明的前提下,可在非商业目的下任意传播和复制。
*                       对于商业目的下对本文的任何行为需经作者同意。
*********************************************************************/

1,主机环境

root@ubuntu:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty
root@ubuntu:~# cat /proc/version
Linux version 4.4.0-31-generic (buildd@lgw01-43) (gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) ) #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016
root@ubuntu:~# file /bin/busybox 
/bin/busybox: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=ae3cf83d83d4ed554fa7ba6d5f2b17c92c362710, stripped
root@ubuntu:~# 
root@ubuntu:~# aarch64-himix100-linux-gcc -v
Using built-in specs.
COLLECT_GCC=aarch64-himix100-linux-gcc
COLLECT_LTO_WRAPPER=/opt/hisi-linux/x86-arm/aarch64-himix100-linux/host_bin/../libexec/gcc/aarch64-linux-gnu/6.3.0/lto-wrapper
Target: aarch64-linux-gnu
Configured with: /home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/src/gcc-6.3.0/configure --build=i386-redhat-linux --host=i386-redhat-linux --target=aarch64-linux-gnu --with-arch=armv8-a --prefix=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install --with-specs='%{!ftree-coalesce-vars:-fno-tree-coalesce-vars}' --disable-libmudflap --disable-libssp --enable-gold=yes --disable-libstdcxx-pch --disable-multilib --enable-multiarch --with-gnu-as --with-gnu-ld --enable-libgomp --enable-gnu-indirect-function --enable-checking=yes --enable-lto --enable-c99 --enable-long-long --disable-nls --enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419 --enable-shared --with-pkgversion='HC&C V100R002C00B027_20181107' --enable-languages=c,c++ --with-headers=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install/target/usr/include --with-sysroot=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install/target --with-build-sysroot=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install/target --with-gmp=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/obj/host-libs/usr --with-mpfr=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/obj/host-libs/usr --with-mpc=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/obj/host-libs/usr --with-cloog=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/obj/host-libs/usr --with-build-time-tools=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install/aarch64-linux-gnu/bin --libdir=/home/sying/SDK_CPU_UNIFIED/build/script/aarch64-himix100-linux/aarch64_himix100_build_dir/install/lib
Thread model: posix
gcc version 6.3.0 (HC&C V100R002C00B027_20181107) 

2,文件下载

2.1 ltrace-0.7.91.tar.bz2

https://github.com/dkogan/ltrace
https://src.fedoraproject.org/repo/pkgs/ltrace/?C=M;O=D

2.2 zlib-1.2.11.tar.gz

http://www.zlib.net/fossils/

2.3 elfutils-0.178.tar.bz2

https://sourceware.org/elfutils/ftp/0.178/

3,环境依赖与准备

3.1 交叉编译zlib

time CC=aarch64-himix100-linux-gcc  LD=aarch64-himix100-linux-ld RANLIB=aarch64-himix100-linux-ranlib AR=aarch64-himix100-linux-ar ./configure --prefix=/opt/files/release/zlib/aarch64/

make

make install

root@ubuntu:/opt/files/zlib-1.2.11# tree /opt/files/release/zlib/aarch64/
/opt/files/release/zlib/aarch64/
├── include
│   ├── zconf.h
│   └── zlib.h
├── lib
│   ├── libz.a
│   ├── libz.so -> libz.so.1.2.11
│   ├── libz.so.1 -> libz.so.1.2.11
│   ├── libz.so.1.2.11
│   └── pkgconfig
│       └── zlib.pc
└── share
    └── man
        └── man3
            └── zlib.3

6 directories, 8 files

3.2 交叉编译elfutils

time CC=aarch64-himix100-linux-gcc  LD=aarch64-himix100-linux-ld RANLIB=aarch64-himix100-linux-ranlib AR=aarch64-himix100-linux-ar LDFLAGS='-L/opt/files/release/zlib/aarch64/lib' ./configure --host=aarch64-himix100-linux  --prefix=/opt/files/release/elfutils/aarch64/ --disable-debuginfod 

time make -w CFLAGS+='-I/opt/files/release/zlib/aarch64/include' LDFLAGS="-Wl,-rpath-link,/opt/files/release/zlib/aarch64/lib  -L/opt/files/release/zlib/aarch64/lib"

time make install

root@ubuntu:/opt/files/elfutils-0.178# tree /opt/files/release/elfutils/aarch64/lib/
/opt/files/release/elfutils/aarch64/lib/
├── libasm-0.178.so
├── libasm.a
├── libasm.so -> libasm.so.1
├── libasm.so.1 -> libasm-0.178.so
├── libdw-0.178.so
├── libdw.a
├── libdw.so -> libdw.so.1
├── libdw.so.1 -> libdw-0.178.so
├── libelf-0.178.so
├── libelf.a
├── libelf.so -> libelf.so.1
├── libelf.so.1 -> libelf-0.178.so
└── pkgconfig
    ├── libdebuginfod.pc
    ├── libdw.pc
    └── libelf.pc

1 directory, 15 files

3.3 自动生成编译文件

root@ubuntu:/opt/files/ltrace-0.7.91# bash autogen.sh 

----------
autoreconf
----------

autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force --warnings=all -I config/m4
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `config/autoconf'.
libtoolize: copying file `config/autoconf/ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIR, `config/m4'.
libtoolize: copying file `config/m4/libtool.m4'
libtoolize: copying file `config/m4/ltoptions.m4'
libtoolize: copying file `config/m4/ltsugar.m4'
libtoolize: copying file `config/m4/ltversion.m4'
libtoolize: copying file `config/m4/lt~obsolete.m4'
autoreconf: running: /usr/bin/autoconf --force --warnings=all
autoreconf: running: /usr/bin/autoheader --force --warnings=all
autoreconf: running: automake --add-missing --copy --force-missing --warnings=all
configure.ac:56: installing 'config/autoconf/compile'
configure.ac:32: installing 'config/autoconf/config.guess'
configure.ac:32: installing 'config/autoconf/config.sub'
configure.ac:59: installing 'config/autoconf/install-sh'
configure.ac:59: installing 'config/autoconf/missing'
Makefile.am: installing 'config/autoconf/depcomp'
testsuite/Makefile.am:29: warning: user variable 'RUNTESTDEFAULTFLAGS' defined here ...
/usr/share/automake-1.14/am/dejagnu.am: ... overrides Automake variable 'RUNTESTDEFAULTFLAGS' defined here
autoreconf: Leaving directory `.'

--------
Finished
--------

root@ubuntu:/opt/files/ltrace-0.7.91# 

4,x86编译测试

time ./configure  --prefix=/opt/files/release/ltrace/x86_64

time make CFLAGS+='-Wno-unused-local-typedefs'

time make install

root@ubuntu:/opt/files/ltrace-0.7.3# tree /opt/files/release/ltrace/x86_64
/opt/files/release/ltrace/x86_64
├── bin
│   └── ltrace
├── etc
│   └── ltrace.conf
└── share
    ├── doc
    │   └── ltrace
    │       ├── COPYING
    │       ├── CREDITS
    │       ├── INSTALL
    │       ├── README
    │       └── TODO
    └── man
        ├── man1
        │   └── ltrace.1
        └── man5
            └── ltrace.conf.5

8 directories, 9 files

root@ubuntu:/opt/files/ltrace-0.7.3# file /opt/files/release/ltrace/x86_64/bin/ltrace 
/opt/files/release/ltrace/x86_64/bin/ltrace: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=b46d8b17ed40d165d6e339466237fef6c094f63e, not stripped


make uninstall && make distclean

5,aarch64平台交叉编译

time CC=aarch64-himix100-linux-gcc  LD=aarch64-himix100-linux-ld RANLIB=aarch64-himix100-linux-ranlib AR=aarch64-himix100-linux-ar  ./configure --host=aarch64-himix100-linux  --prefix=/opt/files/release/ltrace/aarch64/ CPPFLAGS='-I/opt/hisi-linux/x86-arm/aarch64-himix100-linux/target/usr/include/ -I/opt/files/release/elfutils/aarch64/include/ -D__LIBELF64=1' LDFLAGS="-Wl,-rpath-link,/opt/files/release/zlib/aarch64/lib  -L/opt/files/release/elfutils/aarch64/lib"

time make CFLAGS+='-Wno-unused-local-typedefs'

time make install


root@ubuntu:/opt/files/ltrace-0.7.91# tree /opt/files/release/ltrace/aarch64/
/opt/files/release/ltrace/aarch64/
├── bin
│   └── ltrace
└── share
    ├── doc
    │   └── ltrace
    │       ├── COPYING
    │       ├── CREDITS
    │       ├── INSTALL
    │       ├── README
    │       └── TODO
    ├── ltrace
    │   ├── libacl.so.conf
    │   ├── libc.so.conf
    │   ├── libc.so-types.conf
    │   ├── libm.so.conf
    │   ├── libpthread.so.conf
    │   ├── libpthread.so-types.conf
    │   └── syscalls.conf
    └── man
        ├── man1
        │   └── ltrace.1
        └── man5
            └── ltrace.conf.5

8 directories, 15 files

root@ubuntu:/opt/files/ltrace-0.7.91# file  /opt/files/release/ltrace/aarch64/bin/ltrace     
/opt/files/release/ltrace/aarch64/bin/ltrace: ELF 64-bit LSB  executable, ARM aarch64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 3.7.0, not stripped
root@ubuntu:/opt/files/ltrace-0.7.91# 

make uninstall && make distclean

6,踩过的坑

6.1 系统自带elf库版本不兼容

checking elf.h usability... no
checking elf.h presence... yes
configure: WARNING: elf.h: present but cannot be compiled
configure: WARNING: elf.h:     check for missing prerequisite headers?
configure: WARNING: elf.h: see the Autoconf documentation
configure: WARNING: elf.h:     section "Present But Cannot Be Compiled"
configure: WARNING: elf.h: proceeding with the compiler's result
configure: WARNING:     ## --------------------------------------------------- ##
configure: WARNING:     ## Report this to ltrace-devel@lists.alioth.debian.org ##
configure: WARNING:     ## --------------------------------------------------- ##
checking for elf.h... no
configure: error: *** libelf.h or gelf.h not found on your system

6.2 ltrace-0.7.3版本过时,不支持aarch64系统,编译不通过

root@ubuntu:/opt/files/ltrace-0.7.3# cat README 
Supported Targets
-----------------

The following targets are currently (at least somewhat) supported.
Some of them may be more or less broken in reality, it is not feasible
to test each release comprehensively on each target.

        i[4567]86-*-linux-gnu
        ia64-*-linux-gnu
        m68k-*-linux-gnu
        mips-*-linux-gnu
        powerpc-*-linux-gnu
        powerpc64-*-linux-gnu
        s390-*-linux-gnu
        s390x-*-linux-gnu
        x86_64-*-linux-gnu

		

6.3 libelf-0.8.13 elf库版本过时,不支持aarch64系统,编译不通过。

(也可能没找到正确指令,看文档也支持64bit ^_^)

libelf_0.8.13.orig.tar.gz	146.1 KiB	
https://launchpad.net/ubuntu/+source/libelf/0.8.13-5

7,经验总结

strace交叉编译2小时就好了,ltrace花了2天时间!!!需要反思的一下:
(1)编译之前先看看文档,确认支持的版本;
(2)出错时认真分析日志文件,比如(config.log、实时打印输出的编译指令),一般会找到出错原因;
(3)交叉编译时思路要清晰,不要把依赖库的版本搞混,用x86_64的库去编译aarch64肯定会有问题;
(4)网上的参考贴好多都是年代久远,对新出的aarch64支持不友好,需要下载最新版本,对照源码文件文档进行编译;
(5)开源软件绝大多数都是用相同的发布机制进行管理的,一般是 autoconf + ./configure 生成Makefile;
(6)需要回炉一下编译、链接的过程,注意 -Wl,-rpath-link,DIR || -Wl,-rpath=DIR || -L DIR|| LD_LIBRARY_PATH=DIR的作用与区别;
(7)尽可能不要动开源软件的代码,大多数问题可以通过指定配置参数来规避。

8,Q&A

libtool: compile:  aarch64-himix100-linux-gcc -DHAVE_CONFIG_H -I. -I../.. -DSYSCONFDIR=\"/opt/files/release/ltrace/aarch64/etc\" -DPKGDATADIR=\"/opt/files/release/ltrace/aarch64/share/ltrace\" -I../../sysdeps/linux-gnu/aarch64 -I../../sysdeps/linux-gnu -I../../sysdeps -I../.. -I/opt/hisi-linux/x86-arm/aarch64-himix100-linux/target/usr/include/ -I/opt/files/release/libelf/aarch64/include/ -L/opt/files/release/libelf/aarch64/lib -D__LIBELF64=1 -Wall -Wsign-compare -Wfloat-equal -Wformat-security -Werror -g -O2 -MT proc.lo -MD -MP -MF .deps/proc.Tpo -c proc.c  -fPIC -DPIC -o .libs/proc.o
proc.c: In function 'process_tasks':
proc.c:247:3: error: 'readdir_r' is deprecated [-Werror=deprecated-declarations]
   if (readdir_r(d, &entry, &result) != 0) {
   ^~
In file included from proc.c:31:0:
/opt/hisi-linux/x86-arm/aarch64-himix100-linux/target/usr/include/dirent.h:183:12: note: declared here
 extern int readdir_r (DIR *__restrict __dirp,

------------
root@ubuntu:/opt/files/ltrace-0.7.91# find . -name "*.c" | xargs grep -rn "readdir_r"
./sysdeps/linux-gnu/proc.c:247:         if (readdir_r(d, &entry, &result) != 0) {
root@ubuntu:/opt/files/ltrace-0.7.91# vi ./sysdeps/linux-gnu/proc.c +247

244         while (1) {
245                 //struct dirent entry;
246                 struct dirent *result;
247                 if ( NULL == (result = readdir(d)) ) { //if (readdir_r(d, &entry, &result) != 0) {
248                 fail:
249                         if(tasks) free(tasks);
250                         closedir(d);
251                         return -1;
252                 }
253                 if (result == NULL)
254                         break;
 

9,参考

-L、-rpath和-rpath-link的区别 https://blog.csdn.net/q1302182594/article/details/42102961

 类似资料: