【问题标题】:How can I change objdump output format?如何更改 objdump 输出格式?
【发布时间】:2019-07-01 09:53:08
【问题描述】:

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

我想显示与书中显示的结果相同的结果。我应该怎么做?我想我需要更改 gcc、objdump 或 options 的版本。

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

  • 预计
.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 没有输出可以重新组合的语法的选项。 Agner Fog 的objconv x86 反汇编程序确实有这个功能。 agner.org/optimize。试试看。 (但我认为它唯一的 GAS 指令模式是 AT&T 语法;其他是使用不同指令的 NASM 和 MASM。)
  • 谢谢。我找到了文件。 agner.org/optimize/objconv-instructions.pdf会试试的。

标签: c gcc assembly disassembly


【解决方案1】:

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

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

【讨论】:

  • 谢谢。我用你建议的方式得到了类似的输出。并感谢您谈到可重现的构建。这是我要学习的另一个任务。
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多