深入理解linux執(zhí)行文件提示No such file or directory的背后原因
1 背景
最近一直在研究在ZC706-ARM開(kāi)發(fā)板的linux系統(tǒng)中弄一套編譯系統(tǒng)(不支持apt),剛好發(fā)現(xiàn)公司有一套英偉達(dá)的ARM開(kāi)發(fā)板且?guī)в衭bunut系統(tǒng)(支持apt),此時(shí)產(chǎn)生一個(gè)想法,英偉達(dá)板子上編譯的程序能否在ZC706的板子上運(yùn)行?
2 過(guò)程
在英偉達(dá)的開(kāi)發(fā)板中 gcc a.c生成a.out,然后拷貝到ZC706中執(zhí)行出現(xiàn)“No such file or directory”
以前遇到的是以下原因:
- 文件本身不存在或者文件損壞
- 無(wú)執(zhí)行權(quán)限 (chmod 777 xxx)
- 系統(tǒng)位數(shù)與程序位數(shù)不同
但是經(jīng)過(guò)以下過(guò)程發(fā)現(xiàn)是ZC706缺少xx程序的指定的裝載器:
1.排除文件損壞等問(wèn)題-->重新生成拷貝驗(yàn)證
2.排除程序權(quán)限問(wèn)題--> chmod 777 xx && ls -all
3.通過(guò)unanme -a 排除架構(gòu)問(wèn)題
4.通過(guò)readelf file 等命令對(duì)比正常執(zhí)行的文件與錯(cuò)誤執(zhí)行文件的差別
驗(yàn)證過(guò)程:
a.out由英偉達(dá)gcc編譯生成且zc706出現(xiàn)上面問(wèn)題 | b.out由x86 ubunut交叉編譯生成且可以正常執(zhí)行
后來(lái)通過(guò)google等發(fā)現(xiàn)裝載器也會(huì)造成該現(xiàn)象 ,從下面可以發(fā)現(xiàn)兩者的區(qū)別主要在于 interpreter
解決方案:
1.統(tǒng)一編譯器與庫(kù)的關(guān)系
2. 建立軟鏈接 ln -s /lib/ld-linux.so.3 /lib/ld-linux-armhf.so.3
3. 編譯程序時(shí),加入-static選項(xiàng)靜態(tài)鏈接程序,即不使用動(dòng)態(tài)庫(kù)
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 使用這個(gè)ld-linux.so*(虛擬機(jī)x86的ubuntu 是使用ld-linux.so2)中的來(lái)裝載(其實(shí)這只是一個(gè)鏈接)其他庫(kù)。所以這個(gè)庫(kù)必須放在 linux中/lib下。對(duì)于其他,通常我們共享庫(kù)放在/lib這個(gè)路徑下,而且也是系統(tǒng)默認(rèn)的搜索路徑。
Linux共享庫(kù)的搜索路徑先后順序:
1、編譯目標(biāo)代碼時(shí)指定的動(dòng)態(tài)庫(kù)搜索路徑:在編譯的時(shí)候指定-Wl,-rpath=路徑
2、環(huán)境變量LD_LIBRARY_PATH指定的動(dòng)態(tài)庫(kù)搜索路徑
3、配置文件/etc/ld.so.conf中指定的動(dòng)態(tài)庫(kù)搜索路徑
4、默認(rèn)的動(dòng)態(tài)庫(kù)搜索路徑/lib
5、默認(rèn)的動(dòng)態(tài)庫(kù)搜索路徑 /usr/lib
注意:
1.有些開(kāi)發(fā)板會(huì)發(fā)現(xiàn)/etc沒(méi)有l(wèi)d.so.conf,此時(shí)運(yùn)行l(wèi)dconfig會(huì)提示 "ldconfig: Warning: ignoring configuration file that cannot be opened: /etc/ld.so.conf: No such file or directory"
解決:加入庫(kù)到環(huán)境變量,然后ldconfig -v (/sbin/ldconfig: relative path `–v' used to build cache)
2.共享庫(kù) cnnot open shared object
測(cè)試是否動(dòng)態(tài)連接,如果列出libtest.so,那么應(yīng)該是連接正常了
這時(shí)候找不到libtest.so, 是動(dòng)態(tài)鏈接庫(kù)的查找路徑出問(wèn)題,因此加入上面動(dòng)態(tài)庫(kù)查找位置即可
3 ldconfig命令主要是在默認(rèn)搜尋目錄(/lib和/usr/lib)以及動(dòng)態(tài)庫(kù)配置文件/etc/ld.so.conf內(nèi)所列的目錄下,搜索出可共享的動(dòng)態(tài)鏈接庫(kù)(格式如前介紹,lib*.so*),進(jìn)而創(chuàng)建出動(dòng)態(tài)裝入程序(ld.so)所需的連接和緩存文件
4 LD_LIBRARY_PATH:這個(gè)環(huán)境變量指示動(dòng)態(tài)連接器可以裝載動(dòng)態(tài)庫(kù)的路徑。如果有root權(quán)限的話,可以修改/etc/ld.so.conf文件,然后調(diào)用 /sbin/ldconfig來(lái)達(dá)到同樣的目的,不過(guò)如果沒(méi)有root權(quán)限,那么只能采用輸出LD_LIBRARY_PATH的方法了,要用bash命令)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。