2016-05-06 69 views
2

我编译在Debian喘息以下汇编程序运行,但它不会跑给我的错误:二进制不是Debian的喘息

-bash:./power:不能执行二进制文件

代码

.section data 

.section text 

.global _start 
    _start: 
     # do first calc and save the answer 
     pushl $3 
     pushl $2 
     call power 
     addl $8, %esp 
     pushl %eax 

     # do second calc 
     pushl $2 
     pushl $5 
     call power 
     addl $8, %esp 

     # add both together 
     popl %ebx 
     addl %eax, %ebx 

     # exit with answer as return status 
     movl $1, %eax 
     int $0x80 

.type power, @function 
    power: 
     # ? 
     pushl %ebp 
     movl %esp, %ebp 
     subl $4, %esp 

     # load params 
     movl 8(%ebp), %ebx 
     movl 12(%ebp), %ecx 
     movl %ebx, -4(%ebp) 

    power_loop_start: 
     # have we looped down to 1? 
     cmpl $1, %ecx 
     je end_power 

     # multiply prev result by base and store 
     movl -4(%ebp), %eax 
     imull %ebx, %eax 
     movl %eax, -4(%ebp) 

     # go again 
     decl %ecx 
     jmp power_loop_start 

    end_power: 
     movl -4(%ebp), %eax 
     movl %ebp, %esp 
     popl %ebp 
     ret 

我奔跑着:

as power.s -o power.o 
ld power.o -o power 
./power 

两个uname -march给我i686的,和二进制输出这对objdump -x

$ objdump -x power 

power:  file format elf32-i386 
power 
architecture: i386, flags 0x00000012: 
EXEC_P, HAS_SYMS 
start address 0x00000000 

Sections: 
Idx Name   Size  VMA  LMA  File off Algn 
    0 text   0000004a 00000000 00000000 00000034 2**0 
        CONTENTS, READONLY 
SYMBOL TABLE: 
00000000 l d text 00000000 text 
00000023 l  F text 00000000 power 
00000032 l  text 00000000 power_loop_start 
00000043 l  text 00000000 end_power 
00000000 g  text 00000000 _start 
08049034 g  *ABS* 00000000 __bss_start 
08049034 g  *ABS* 00000000 _edata 
08049034 g  *ABS* 00000000 _end 

不知道我做错了。

其他备注:

本例来自“从头开始编程”一书。我试过一个红帽x86_64机器,as标志--32ld标志-m elf_i386,它所有的编译就像在x86机器上一样,但是执行时会给出相同的错误。

+2

你有一个错字:'.section text'应该是'.section .text'(注意点)或者只是'.text'。 – Jester

+0

@Jester哇,我希望我能投票给你100次。我一直在试图弄清楚这一点。文本和数据都没有点!我希望编译器抱怨... – sprocket12

回答

4

你有一个错字:.section text应该是.section .text(注意点),或只是.text

尽管这一问题是由一个拼写错误造成的,我觉得这是值得一些解释特别是因为你这么好听提供的所有细节:)

虽然你可以命名你的部分,但你喜欢(这就是为什么工具没有抱怨),每个部分都有一些标志。在这种情况下,您会在objdump输出中看到:CONTENTS, READONLY。这意味着这部分不可执行,实际上它甚至没有加载。 (可以说错误信息可能会更精确一点。)

好吧,为什么它不可执行?汇编器识别一些常用的段名称并正确设置标志。对于自定义名称,您必须手动执行此操作,例如通过执行.section text, "ax"设置ALLOCCODE。另请参阅.section directive in the manualthis answer about the various flags

相关问题