Reputation: 110267
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?
Upvotes: 1
Views: 120
Reputation: 364438
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.
Upvotes: 2