1

I produced the assembler below by running

clang -s hello.c

where hello.c is the filename for the following:

#include <stdio.h>

main() {

printf("Hello\n");

}

The assembler is in hello.s:

  .section  __TEXT,__text,regular,pure_instructions
  .build_version macos, 10, 14  sdk_version 10, 14
  .globl  _main                   ## -- Begin function main
  .p2align  4, 0x90
_main:                                  ## @main
  .cfi_startproc
## %bb.0:
  pushq %rbp
...

2 Answers2

0

You can find docs in the GAS manual (clang/LLVM uses the same syntax including directives). https://sourceware.org/binutils/docs/as/

See also links in https://stackoverflow.com/tags/x86/info and https://stackoverflow.com/tags/att/info for more about x86-64 AT&T syntax.

Clang/LLVM can use Intel syntax, just like GCC, but the directives don't change. clang --x86-asm-syntax=intel, or maybe -masm=intel for compatibility with GCC.

There might also be something in the clang or LLVM manual. (But don't confuse this with LLVM-IR assembly language: the asm syntax for LLVM's internal representation.) From a quick search, I didn't find any LLVM docs for how clang handles GAS directives.


.globl _main is not the label itself. Notice that it's before the alignment padding. It's just an attribute modifier for the symbol _main.

_main: is the position in the output file that the label is applied to.

For understanding the instructions, see How to remove "noise" from GCC/clang assembly output? for how to strip out irrelevant directives.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Hey, I was starting to finally stumble across some links on Google. Thanks for your input! And the extra links and info. –  Sep 26 '19 at 03:44
  • Note that this is on macOS. I'm not sure if they use a special assembler there. – fuz Sep 26 '19 at 07:31
  • @fuz: Pretty sure it's still clang/LLVM. IDK if they patch it to add any special directives. I wondered about `.build_version` – Peter Cordes Sep 26 '19 at 07:46
  • @PeterCordes I know that clang ships a reimplementation of GNU as these days. – fuz Sep 26 '19 at 07:53
  • @fuz: Pretty sure that's just clang, running clang/LLVM's built-in assembler. Exactly like you get on Linux if you run `clang -c foo.s`. Not sure if [Installing GNU Assembler in OSX](//stackoverflow.com/q/1712331) is out of date, but the answers there report that GNU binutils `as` can't create MachO64 object files. OTOH I've also seen a report that GNU binutils `objdump` (`gobjdump` on a mac after installing it from macports apparently) does work: [Disassemble into x86\_64 on OSX10.6 (But with \_Intel\_ Syntax)](//stackoverflow.com/posts/comments/5304301) – Peter Cordes Sep 26 '19 at 08:00
0

I think it's called GNU Assembler, and I found documentation for it here: https://www.sourceware.org/binutils/docs-2.12/as.info/index.html.