How can I change objdump output format?

Viewed 836

Hi I'm learning c compiler with this book. https://www.sigbus.info/compilerbook

I want to show the same result as the book shows. What should I do it? I think I need to change the version of gcc, objdump or options.

This book says that it is possible to compile too from the following expected assemble output.

  • expected
.intel_syntax noprefix
.global main
main:
        mov rax, 42
        ret
  • actual
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
  • what I did
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

Update 1

Generated assembly code with the -S option. Compiling worked from the generated assembly code.

Still there are some differences from my reference book but I will learn more.

And one another curious thing is that the different register name is used respectively. I will look into it too. (I have realized I need to learn from basic..)

// 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
1 Answers

Instead of dumping with objdump, try to directly generate assembly code with the -S option for the compiler. With -masm=intel, the output should look similar to what you expect.

Do not expect the compiler to generate the exact same code though. Different compilers and different compiler versions or even the same compiler with different flags may make different choices and generate different assembly for the same code. That's normal.

Related