佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 深入理解linux執(zhí)行文件提示No such file or directory的背后原因

深入理解linux執(zhí)行文件提示No such file or directory的背后原因

熱門標簽:網(wǎng)站建設 阿里云 百度競價點擊價格的計算公式 智能手機 美圖手機 硅谷的囚徒呼叫中心 使用U盤裝系統(tǒng) 檢查注冊表項

1 背景

最近一直在研究在ZC706-ARM開發(fā)板的linux系統(tǒng)中弄一套編譯系統(tǒng)(不支持apt),剛好發(fā)現(xiàn)公司有一套英偉達的ARM開發(fā)板且?guī)в衭bunut系統(tǒng)(支持apt),此時產(chǎn)生一個想法,英偉達板子上編譯的程序能否在ZC706的板子上運行?

2 過程

在英偉達的開發(fā)板中 gcc a.c生成a.out,然后拷貝到ZC706中執(zhí)行出現(xiàn)“No such file or directory”

以前遇到的是以下原因:

  • 文件本身不存在或者文件損壞
  • 無執(zhí)行權(quán)限 (chmod 777 xxx)
  • 系統(tǒng)位數(shù)與程序位數(shù)不同

但是經(jīng)過以下過程發(fā)現(xiàn)是ZC706缺少xx程序的指定的裝載器:

1.排除文件損壞等問題-->重新生成拷貝驗證
2.排除程序權(quán)限問題--> chmod 777 xx && ls -all
3.通過unanme -a 排除架構(gòu)問題
4.通過readelf file 等命令對比正常執(zhí)行的文件與錯誤執(zhí)行文件的差別

驗證過程:

a.out由英偉達gcc編譯生成且zc706出現(xiàn)上面問題 | b.out由x86 ubunut交叉編譯生成且可以正常執(zhí)行

后來通過google等發(fā)現(xiàn)裝載器也會造成該現(xiàn)象 ,從下面可以發(fā)現(xiàn)兩者的區(qū)別主要在于 interpreter

解決方案:

1.統(tǒng)一編譯器與庫的關系

2. 建立軟鏈接 ln -s /lib/ld-linux.so.3 /lib/ld-linux-armhf.so.3

3. 編譯程序時,加入-static選項靜態(tài)鏈接程序,即不使用動態(tài)庫

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這個路徑下,而且也是系統(tǒng)默認的搜索路徑。

Linux共享庫的搜索路徑先后順序:
1、編譯目標代碼時指定的動態(tài)庫搜索路徑:在編譯的時候指定-Wl,-rpath=路徑
2、環(huán)境變量LD_LIBRARY_PATH指定的動態(tài)庫搜索路徑
3、配置文件/etc/ld.so.conf中指定的動態(tài)庫搜索路徑
4、默認的動態(tài)庫搜索路徑/lib
5、默認的動態(tài)庫搜索路徑 /usr/lib

注意:

1.有些開發(fā)板會發(fā)現(xiàn)/etc沒有l(wèi)d.so.conf,此時運行l(wèi)dconfig會提示 "ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory"

解決:加入庫到環(huán)境變量,然后ldconfig -v (/sbin/ldconfig: relative path `–v' used to build cache)

2.共享庫 cnnot open shared object

測試是否動態(tài)連接,如果列出libtest.so,那么應該是連接正常了

這時候找不到libtest.so, 是動態(tài)鏈接庫的查找路徑出問題,因此加入上面動態(tài)庫查找位置即可

3 ldconfig命令主要是在默認搜尋目錄(/lib和/usr/lib)以及動態(tài)庫配置文件/etc/ld.so.conf內(nèi)所列的目錄下,搜索出可共享的動態(tài)鏈接庫(格式如前介紹,lib*.so*),進而創(chuàng)建出動態(tài)裝入程序(ld.so)所需的連接和緩存文件

4 LD_LIBRARY_PATH:這個環(huán)境變量指示動態(tài)連接器可以裝載動態(tài)庫的路徑。如果有root權(quán)限的話,可以修改/etc/ld.so.conf文件,然后調(diào)用 /sbin/ldconfig來達到同樣的目的,不過如果沒有root權(quán)限,那么只能采用輸出LD_LIBRARY_PATH的方法了,要用bash命令)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:湖北 黃山 賀州 煙臺 懷化 通遼 山南 湘潭

巨人網(wǎng)絡通訊聲明:本文標題《深入理解linux執(zhí)行文件提示No such file or directory的背后原因》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    东台市| 苗栗市| 双城市| 蒲城县| 韩城市| 宁波市| 青龙| 正阳县| 泊头市| 云南省| 临江市| 独山县| 天津市| 正阳县| 桃园县| 抚松县| 怀远县| 北碚区| 新绛县| 巫山县| 云阳县| 恩平市| 博罗县| 遂昌县| 通山县| 铜鼓县| 团风县| 平谷区| 贡嘎县| 卫辉市| 长兴县| 北川| 菏泽市| 怀安县| 汉源县| 苍南县| 阳春市| 洪雅县| 大港区| 望都县| 兴城市|