• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    深入理解linux执行文件提示No such file or directory的背后原因

    1 背景

    最近一直在研究在ZC706-ARM开发板的linux系统中弄一套编译系统(不支持apt),刚好发现公司有一套英伟达的ARM开发板且带有ubunut系统(支持apt),此时产生一个想法,英伟达板子上编译的程序能否在ZC706的板子上运行?

    2 过程

    在英伟达的开发板中 gcc a.c生成a.out,然后拷贝到ZC706中执行出现“No such file or directory”

    以前遇到的是以下原因:

    但是经过以下过程发现是ZC706缺少xx程序的指定的装载器:

    1.排除文件损坏等问题-->重新生成拷贝验证
    2.排除程序权限问题--> chmod 777 xx && ls -all
    3.通过unanme -a 排除架构问题
    4.通过readelf file 等命令对比正常执行的文件与错误执行文件的差别

    验证过程:

    a.out由英伟达gcc编译生成且zc706出现上面问题 | b.out由x86 ubunut交叉编译生成且可以正常执行

    后来通过google等发现装载器也会造成该现象 ,从下面可以发现两者的区别主要在于 interpreter

    解决方案:

    1.统一编译器与库的关系

    2. 建立软链接 ln -s /lib/ld-linux.so.3 /lib/ld-linux-armhf.so.3

    3. 编译程序时,加入-static选项静态链接程序,即不使用动态库

    root@tegra-ubuntu:~# readelf -h a.out
    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:               EXEC (Executable file)
     Machine:              ARM
     Version:              0x1
     Entry point address:        0x8315
     Start of program headers:     52 (bytes into file)
     Start of section headers:     4500 (bytes into file)
     Flags:               0x5000402, has entry point, Version5 EABI, hard-float ABI
     Size of this header:        52 (bytes)
     Size of program headers:      32 (bytes)
     Number of program headers:     9
     Size of section headers:      40 (bytes)
     Number of section headers:     30
     Section header string table index: 27
    root@tegra-ubuntu:~# readelf -h b.out
    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:               EXEC (Executable file)
     Machine:              ARM
     Version:              0x1
     Entry point address:        0x86bc
     Start of program headers:     52 (bytes into file)
     Start of section headers:     4136 (bytes into file)
     Flags:               0x5000202, has entry point, Version5 EABI, soft-float ABI
     Size of this header:        52 (bytes)
     Size of program headers:      32 (bytes)
     Number of program headers:     8
     Size of section headers:      40 (bytes)
     Number of section headers:     31
     Section header string table index: 28
    root@tegra-ubuntu:~# readelf -l helloworld | grep interpreter
    readelf: Error: 'helloworld': No such file
    root@tegra-ubuntu:~# readelf -l a.out | grep interpreter
       [Requesting program interpreter: /lib/ld-linux-armhf.so.3]
    root@tegra-ubuntu:~# readelf -l b.out | grep interpreter
       [Requesting program interpreter: /lib/ld-linux.so.3]

    3 介绍 ld装载器

    Linux 使用这个ld-linux.so*(虚拟机x86的ubuntu 是使用ld-linux.so2)中的来装载(其实这只是一个链接)其他库。所以这个库必须放在 linux中/lib下。对于其他,通常我们共享库放在/lib这个路径下,而且也是系统默认的搜索路径。

    Linux共享库的搜索路径先后顺序:
    1、编译目标代码时指定的动态库搜索路径:在编译的时候指定-Wl,-rpath=路径
    2、环境变量LD_LIBRARY_PATH指定的动态库搜索路径
    3、配置文件/etc/ld.so.conf中指定的动态库搜索路径
    4、默认的动态库搜索路径/lib
    5、默认的动态库搜索路径 /usr/lib

    注意:

    1.有些开发板会发现/etc没有ld.so.conf,此时运行ldconfig会提示 "ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory"

    解决:加入库到环境变量,然后ldconfig -v (/sbin/ldconfig: relative path `–v' used to build cache)

    2.共享库 cnnot open shared object

    测试是否动态连接,如果列出libtest.so,那么应该是连接正常了

    这时候找不到libtest.so, 是动态链接库的查找路径出问题,因此加入上面动态库查找位置即可

    3 ldconfig命令主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件

    4 LD_LIBRARY_PATH:这个环境变量指示动态连接器可以装载动态库的路径。如果有root权限的话,可以修改/etc/ld.so.conf文件,然后调用 /sbin/ldconfig来达到同样的目的,不过如果没有root权限,那么只能采用输出LD_LIBRARY_PATH的方法了,要用bash命令)

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    上一篇:docker安装redis 5.0.7并挂载外部配置和数据问题
    下一篇:深入浅析Centos 7系统的Tomcat服务器
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    深入理解linux执行文件提示No such file or directory的背后原因 深入,理解,linux,执行,文件,