c - 如何更改 objdump 输出格式?

标签 c gcc assembly disassembly

嗨,我正在通过这本书学习 C 编译器。 https://www.sigbus.info/compilerbook

我想显示与书中显示的相同的结果。我该怎么办呢?我想我需要更改 gcc、objdump 或选项的版本。

这本书说,也可以从以下预期的汇编输出进行编译。

  • 预计
.intel_syntax noprefix
.global main
main:
        mov rax, 42
        ret
  • 实际
00000000000005fa <main>:
 5fa:   55                      push   rbp
 5fb:   48 89 e5                mov    rbp,rsp
 5fe:   b8 2a 00 00 00          mov    eax,0x2a
 603:   5d                      pop    rbp
 604:   c3                      ret
 605:   66 2e 0f 1f 84 00 00    nop    WORD PTR cs:[rax+rax*1+0x0]
 60c:   00 00 00
 60f:   90                      nop
  • 我做了什么
root@686394c78009:/zcc# uname -a
Linux 686394c78009 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

root@686394c78009:/zcc# objdump -v
GNU objdump (GNU Binutils for Ubuntu) 2.30
Copyright (C) 2018 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

root@686394c78009:/zcc# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)

root@686394c78009:/zcc# cat test1.c
int main() {
  return 42;
}
root@686394c78009:/zcc# gcc -o test1 test1.c
root@686394c78009:/zcc# ./test1
root@686394c78009:/zcc# echo $?
42
root@686394c78009:/zcc# objdump -d -M intel ./test1

更新1

使用 -S 选项生成的汇编代码。根据生成的汇编代码进行编译。

与我的引用书仍然存在一些差异,但我会了解更多。

另一件奇怪的事情是分别使用不同的寄存器名称。我也会调查一下。 (我意识到我需要从基础开始学习..)

// expected
mov rax, 42

// actual
mov eax, 42 
root@686394c78009:/zcc# gcc -S -masm=intel test1.c

root@686394c78009:/zcc# cat test1.s
    .file   "test1.c"
    .intel_syntax noprefix
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov rbp, rsp
    .cfi_def_cfa_register 6
    mov eax, 42
    pop rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
    .section    .note.GNU-stack,"",@progbits

root@686394c78009:/zcc# gcc -o test1 test1.s
root@686394c78009:/zcc# ./test1
root@686394c78009:/zcc# echo $?
42

最佳答案

不要使用 objdump 进行转储,而是尝试使用编译器的 -S 选项直接生成汇编代码。使用-masm=intel,输出应该与您期望的类似。

不过,不要指望编译器会生成完全相同的代码。不同的编译器和不同的编译器版本甚至具有不同标志的同一编译器可能会做出不同的选择并为相同的代码生成不同的程序集。这很正常。

关于c - 如何更改 objdump 输出格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833532/

相关文章:

c++ - 相同代码中的不一致行为

c - 从 C 代码调用汇编函数时出现段错误

c - -masm 中的第一个 m 是什么意思?

c - ARM 汇编器中的可变大小 int 矩阵

c++ - 如何从子进程向所有进程发送信号?

c - HC-05 ⸮ 串行不起作用

c - 使用 GCC 构建的变量参数的 EDK2 Shell 应用程序在运行时会导致页面错误

c - glibc 使用内核函数

c - c中如何存储有符号和无符号整数?

linux -/usr/bin/ld : cannot open output file assimp2json: Is a directory