安装PetaLinux/YOCTO生成的ARM64 SDK,再导入其中的环境变量,按通常的流程执行cmake和make,就能成功编译rfdc_MTS示例程序。
SDK为ARM64软件开发工具包。
可以使用命令"petalinux-build --sdk"编译SDK。编译成功后,sdk.sh在image/linux目录下。
然后执行sdk.sh,安装信息和位置如下,安装完成并source后可以看到:
系统里会增加相关的变量。比如最最重要的编译器的变量,CC/CPP/CXX/LD的变量里,均会增加sysroot的路径,以及相关选项。
同时发现/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux/usr/lib目录下存在下列文件
使用命令"env | grep xxx",查看相应变量。
编译器的名称:aarch64-xilinx-linux-gcc
然后在执行时遇到:
使用 “–verbose | grep SEARCH_DIR” 搜索aarch64-xilinx-linux-gcc工具调用时,使用了哪个目录。
// An highlighted block
lc@ubuntu:~/Documents/rfdc_linux/rfdc_driver$ /opt/petalinux/2020.2/sysroots/x86_64-petalinux-linux/usr/libexec/aarch64-xilinx-linux/gcc/aarch64-xilinx-linux/9.2.0/real-ld --verbose | grep SEARCH_DIR
SEARCH_DIR("=/opt/petalinux/2020.2/sysroots/x86_64-petalinux-linux/usr/aarch64-xilinx-linux/lib64"); SEARCH_DIR("=/usr/local/lib64"); SEARCH_DIR("=/lib64"); SEARCH_DIR("=/usr/lib64");
SEARCH_DIR("=/opt/petalinux/2020.2/sysroots/x86_64-petalinux-linux/usr/aarch64-xilinx-linux/lib"); SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");
使用find /opt/petalinux/2020.2/sysroots/ -name crti*,查找需要的文件在哪个目录
// An highlighted block
lc@ubuntu:~/Documents/rfdc_linux/rfdc_driver$ find /opt/petalinux/2020.2/sysroots/ -name crti*
/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux/usr/lib/crti.o
/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux/usr/src/debug/glibc/2.30-r0/git/sysdeps/aarch64/crti.S
/opt/petalinux/2020.2/sysroots/x86_64-petalinux-linux/usr/lib/crti.o
则编译时加入该路径:–sysroot=“/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux”
// An highlighted block
aarch64-xilinx-linux-gcc xrfdc_mts_example.o -o mts -lmetal -lrfdc --sysroot="/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux"
–sysroot,这个选项是用来设置目标平台根目录的。–sysroot选项的官方说明如下:
// An highlighted block
--sysroot=dir
Use dir as the logical root directory for headers and libraries.
For example, if the compiler normally searches for headers in /usr/include and libraries in /usr/lib, it instead searches dir/usr/include and dir/usr/lib.
If you use both this option and the -isysroot option, then the --sysroot option applies to libraries, but the -isysroot option applies to header files.
The GNU linker (beginning with version 2.16) has the necessary support for this option. If your linker does not support this option, the header file aspect of --sysroot still works, but the library aspect does not.
搜索路径 | 未设置–sysroot | 设置了–sysroot=dir后 |
---|---|---|
头文件搜索路径 | /usr/include | dir/usr/include |
依赖库搜索路径 | /usr/lib | dir/usr/lib |
最后编译命令语句:
// An highlighted block
aarch64-xilinx-linux-gcc xrfdc_mts_example.c -o mts_example -lmetal -lrfdc --sysroot="/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux"
这条语句生成的是共享库文件而不是可执行文件,原因是编译器默认加了" --enable-default-pie "选项
pie即指Position-Independent-Executable是Binutils,glibc和gcc的一个功能,能用来创建介于共享库和通常可执行代码之间的代码–能像共享库一样可重分配地址的程序,这种程序必须连接到Scrt1.o。标准的可执行程序需要固定的地址,并且只有被装载到这个地址时,程序才能正确执行。PIE能使程序像共享库一样在主存任何位置装载,这需要将程序编译成位置无关,并链接为ELF共享对象。
引入PIE的原因是让程序能装载在随机的地址,通常情况下,内核都在固定的地址运行,如果能改用位置无关,那攻击者就很难借助系统中的可执行码实施攻击了。类似缓冲区溢出之类的攻击将无法实施。而且这种安全提升的代价很小。
最后加入**-no-pie** 禁用掉该默认选项
// An highlighted block
aarch64-xilinx-linux-gcc xrfdc_mts_example.c -o mts_example -lmetal -lrfdc --sysroot="/opt/petalinux/2020.2/sysroots/aarch64-xilinx-linux" -no-pie