![執行二進位檔案時出現「沒有這樣的檔案或目錄」錯誤,也顯示權限被拒絕](https://rvso.com/image/1518668/%E5%9F%B7%E8%A1%8C%E4%BA%8C%E9%80%B2%E4%BD%8D%E6%AA%94%E6%A1%88%E6%99%82%E5%87%BA%E7%8F%BE%E3%80%8C%E6%B2%92%E6%9C%89%E9%80%99%E6%A8%A3%E7%9A%84%E6%AA%94%E6%A1%88%E6%88%96%E7%9B%AE%E9%8C%84%E3%80%8D%E9%8C%AF%E8%AA%A4%EF%BC%8C%E4%B9%9F%E9%A1%AF%E7%A4%BA%E6%AC%8A%E9%99%90%E8%A2%AB%E6%8B%92%E7%B5%95.png)
I was just trying to execute a simple program **.c** program on **OPNESUSE-42.1**
I did the following set of commands in which showing **Permission Denied** and also showing **relocatable** i am not able to understand that how it becomes relocatable file.
$ 檔案資料大小.o
datasize.o:ELF 64 位元 LSB 可重定位,x86-64,版本 1 (SYSV),未剝離
無法執行:-
./datasize.o
bash: ./datasize.o: 權限被拒絕
即使有ldd
ldd 資料大小.o
ldd:警告:您沒有「./datasize.o」的執行權限,不是動態執行檔
追蹤的輸出
strace ./datasize.o
execve("./datasize.o", ["./datasize.o"], [/* 87 vars */]) = -1 EACCES (Permission denied)
write(2, "strace: exec: Permission denied\n", 32strace: exec: Permission denied
) = 32
exit_group(1) = ?
+++ exited with 1 +++
readelf 的輸出:-
readelf -a ./datasize.o
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: REL (Relocatable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x0
Start of program headers: 0 (bytes into file)
Start of section headers: 1040 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 0 (bytes)
Number of program headers: 0
Size of section headers: 64 (bytes)
Number of section headers: 13
Section header string table index: 10
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS 0000000000000000 00000040
000000000000009c 0000000000000000 AX 0 0 1
[ 2] .rela.text RELA 0000000000000000 00000318
0000000000000078 0000000000000018 I 11 1 8
[ 3] .data PROGBITS 0000000000000000 000000dc
0000000000000000 0000000000000000 WA 0 0 1
[ 4] .bss NOBITS 0000000000000000 000000dc
0000000000000000 0000000000000000 WA 0 0 1
[ 5] .rodata PROGBITS 0000000000000000 000000e0
0000000000000087 0000000000000000 A 0 0 8
[ 6] .comment PROGBITS 0000000000000000 00000167
0000000000000019 0000000000000001 MS 0 0 1
[ 7] .note.GNU-stack PROGBITS 0000000000000000 00000180
0000000000000000 0000000000000000 0 0 1
[ 8] .eh_frame PROGBITS 0000000000000000 00000180
0000000000000038 0000000000000000 A 0 0 8
[ 9] .rela.eh_frame RELA 0000000000000000 00000390
0000000000000018 0000000000000018 I 11 8 8
[10] .shstrtab STRTAB 0000000000000000 000003a8
0000000000000061 0000000000000000 0 0 1
[11] .symtab SYMTAB 0000000000000000 000001b8
0000000000000138 0000000000000018 12 9 8
[12] .strtab STRTAB 0000000000000000 000002f0
0000000000000023 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
There are no program headers in this file.
Relocation section '.rela.text' at offset 0x318 contains 5 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000023 000a00000002 R_X86_64_PC32 0000000000000000 uname - 4
000000000028 00050000000a R_X86_64_32 0000000000000000 .rodata + 0
00000000002d 000b00000002 R_X86_64_PC32 0000000000000000 puts - 4
000000000087 00050000000a R_X86_64_32 0000000000000000 .rodata + 48
000000000091 000c00000002 R_X86_64_PC32 0000000000000000 printf - 4
Relocation section '.rela.eh_frame' at offset 0x390 contains 1 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000200000002 R_X86_64_PC32 0000000000000000 .text + 0
The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
Symbol table '.symtab' contains 13 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FILE LOCAL DEFAULT ABS datasize.c
2: 0000000000000000 0 SECTION LOCAL DEFAULT 1
3: 0000000000000000 0 SECTION LOCAL DEFAULT 3
4: 0000000000000000 0 SECTION LOCAL DEFAULT 4
5: 0000000000000000 0 SECTION LOCAL DEFAULT 5
6: 0000000000000000 0 SECTION LOCAL DEFAULT 7
7: 0000000000000000 0 SECTION LOCAL DEFAULT 8
8: 0000000000000000 0 SECTION LOCAL DEFAULT 6
9: 0000000000000000 156 FUNC GLOBAL DEFAULT 1 main
10: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND uname
11: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND puts
12: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND printf
No version information found in this file.
答案1
實際上,如果“.o”文件不是真正的“.o”文件,則可以執行它們。即,如果將“ls”複製到“ls.o”,則可以執行ls.o,它將給出“ls”命令的輸出。順便說一句,@jaychris,您可能不想將編譯的輸出放入“C”原始檔中。你可以,但就像有一個可執行檔“ls.o”一樣,它可能會令人困惑。
我在原始帖子中沒有看到用戶嘗試運行的文件具有可執行權限。如果作者可以執行“\ls -l datasize.o”,它們會顯示其擁有的權限。
請注意,如果它確實是可連結文件,則僅設定可執行位(使用“chmod +x datasize.o”)不會使其成為可執行的程式。在這種情況下,如果datasize.o 中有一個“main”,您可以嘗試@jaychris 暗示的內容...使用gcc -o datasize datasize.c ...這不會產生可連結的“datasize.o ” ”,但會嘗試建立可執行檔並將其放入名為“datasize”的檔案中(您仍然需要使用“chmod +x datasize”啟用該檔案的“可執行”權限)。
順便說一句,您可能想閱讀“chmod”線上說明頁面(“man 1 chmod”)。
希望這可以幫助 (?)...
答案2
.o 檔案無法執行。它們需要連結並需要建立最終的可執行檔。通常完成的方式是“gcc -o file.c”,或者編譯和連結是在單獨的步驟中完成的。