1

What is the most straightforward way to determine which asm a server is using? For example, I have the following C program:

int main(void) {                                                                                                                                 
    int i = 4;
    return i;
}

And compiled to assembly via $ gcc -S main.c I get:

  1   .file "main.c"                                                                                                                                 
  2   .text
  3   .globl  main
  4   .type main, @function
  5 main:
  6 .LFB0:
  7   .cfi_startproc
  8   pushq %rbp
  9   .cfi_def_cfa_offset 16
 10   .cfi_offset 6, -16
 11   movq  %rsp, %rbp
 12   .cfi_def_cfa_register 6
 13   movl  $4, -4(%rbp)
 14   movl  -4(%rbp), %eax
 15   popq  %rbp
 16   .cfi_def_cfa 7, 8
 17   ret
 18   .cfi_endproc
 19 .LFE0:
 20   .size main, .-main
 21   .ident  "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4"
 22   .section  .note.GNU-stack,"",@progbits

I would like to use https://godbolt.org/ to select the closest language to compile online, but I'm not sure which one I should select (or even how to figure out how I can determine that). Basically all I know is "it looks like ATT syntax". How can I figure that out?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    Why do not choose Assembly + x86-64 gcc 9.2? – 273K Apr 07 '20 at 01:57
  • @S.M. that's fine -- but how would you come to that number, and not one of the others? – David542 Apr 07 '20 at 01:58
  • 1
    Any version can be chosen. You can choose your `gcc -v`. – 273K Apr 07 '20 at 02:00
  • That's amd64 (aka x86_64) in AT&T syntax. To find out what architecture your server uses, type `uname -m`. Gcc and clang always use AT&T syntax by default. – fuz Apr 07 '20 at 02:01
  • @fuz -- thanks. It's `x86_64`. Are these all essentially the same under x86_64, or do I need to find out what version I have (and if so, how?) -- https://imgur.com/a/ydQJzML – David542 Apr 07 '20 at 02:03
  • @David542 Please do not post links to externally hosted pictures. These are different compiler versions. The instructions chosen might differ from version to version, but generally they are fairly similar. Note that some distributions ship gcc versions as old as 4.2.1, so the best way is to check what gcc version your system uses (type `gcc -v`). Even then, the assembly output is only the same if all compiler flags match, even those implicitly set when the toolchain was built (like `-fomit-frame-pointer`). – fuz Apr 07 '20 at 02:08

1 Answers1

2

GCC identifies itself via metadata; in the asm this is the .ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4" directive.

You could have seen the same thing via gcc -v to just print your GCC version.

That tells you that your server is using crusty old GCC 4.8.4. (I'd really recommend using a newer compiler if you care about optimization; e.g. gcc 8 introduced simpler auto-vectorization that's better for arrays that are aligned, and lets the hardware handle misalignment by just using unaligned loads.)

So on https://godbolt.org/ you can get identical asm to what you get locally by setting the C compiler version to x86-64 GCC 4.8.4, and using -fstack-protector-strong. (Because Ubuntu configures their GCC to use that option). Normally you should also use -Wall -O2 at least, or -O3; looking at un-optimized asm is usually a waste of time. See How to remove "noise" from GCC/clang assembly output? - especially the link to Matt Godbolt's CPPcon talk.)

Pick C or C++ as the "language" on Godbolt; you'll have gcc and g++ 4.8.4 installed on your Ubuntu systmem.

Later Ubuntu versions enable -fPIE as part of the default options so you'd also have to enable that manually on Godbolt to match what you see locally. But I think 14.04 is too old for that.

(We can tell that this is x86-64 because the stack pointer is RSP not ESP. And yes, GCC on x86 defaults to AT&T syntax unless you use -masm=intel. The Godbolt compiler explorer defaults to Intel you can toggle that output option. It's just a different text syntax for the same machine instructions though; if you know how to read both the difference is irrelevant.)

It's possible that Godbolt will compile with slightly different header versions than you'd get locally; I think its setup has different compiler installs using the headers from one install instead of each having their own. I might be wrong about this, though. Usually it's fine.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847