【问题标题】:binary not running on debian wheezy二进制文件未在 debian wheezy 上运行
【发布时间】:2016-05-06 11:43:54
【问题描述】:

我正在 debian wheezy 上编译以下汇编程序,但它不会运行并给出错误:

-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

不知道我做错了什么。

补充说明:

这个例子来自“从头开始编程”一书。我在 Red Hat x86_64 机器上尝试过,带有 as 标志 --32ld 标志 -m elf_i386,它都像在 x86 机器上一样编译得很好,但是执行时给出了同样的错误。

【问题讨论】:

  • 你打错了:.section text 应该是 .section .text(注意点)或者只是 .text
  • @Jester 哇,我希望我能投票给你 100 次。我一直试图弄清楚这一天。文本和数据都没有点!我希望编译器抱怨...

标签: assembly compilation ld cpu-architecture


【解决方案1】:

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

虽然问题是由拼写错误引起的,但我觉得值得解释一下,尤其是因为您提供了所有细节:)

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

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

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2014-10-09
    • 2022-07-04
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多