0

I'm on macOS (latest version). I know that gcc can produce assembly source code with the -S option. That works fine.

My problem is that I can't get it to produce assembly source that is annotated with my C source code, so I can see which assembly instructions are relevant to a particular section of my C.

I'm trying to follow the instructions on this page: https://www.systutorials.com/240/generate-a-mixed-source-and-assembly-listing-using-gcc/

But when I run this command:

gcc -Wa,-adhln -g hello.c > hello.s

I get this error:

clang: error: unsupported argument '-adhln' to option 'Wa,'

Is this something that is particular to the macOS version of gcc? I'm assuming that that article was written primarily for Linux users. Anyone else come across an issue like this?

David Mordigal
  • 813
  • 2
  • 9
  • 22
  • The assembler used on macOS is not the GNU assembler. The option you provide is a GNU assembler option. The error message is trying to tell you "all the world is not Linux" — and specifically that macOS is not Linux in disguise. You would need a version of GCC that uses the GNU BinUtils toolchain to manage binary files, etc. (I believe that's the package that provides the GNU Assembler.) Note that `as --version` yields `Apple LLVM version 10.0.1 (clang-1001.0.46.4)` — `Target: x86_64-apple-darwin18.7.0` or something similar (plus some extra information — two lines more for me. – Jonathan Leffler Aug 17 '19 at 05:00
  • Another approach is to compile to an object file (with debugging info) and then disassemble that with `objdump`, as per [this answer](https://stackoverflow.com/a/1289907/1312143). – Ken Thomases Aug 17 '19 at 06:31
  • @JonathanLeffler, I think the sense of the question is to get C annotated assembler code (assember lines with the C code interspersed) That's not an assembler issue, but a compiler issue. I have had very few compilers that did this, and them all were not optimising compilers (where produced code is tweaked to gain more efficiency) – Luis Colorado Aug 19 '19 at 05:57
  • @LuisColorado — The `-Wa,…` indicates an option transmitted to the assembler. It appears that the macOS assembler doesn’t recognize the option. I’m not sure what else it means, or how to fix it. – Jonathan Leffler Aug 19 '19 at 06:01
  • @JonathanLeffler, I'm not guided by the options used in the `gcc` call, but the title of the question. It's clear that the compiler must be instructed to put the source code embedded into the assembler code to get what the PO wants. But there are other means to obtain the desired output, as using the debug info to embed them, after the whole process has finished. – Luis Colorado Aug 20 '19 at 04:07

1 Answers1

3

Apple's developer tools don't really include GCC. They include Clang masquerading as GCC. Evidently, Clang doesn't support the option you're trying to use. You would have to install the real GCC some other way, like with MacPorts or Homebrew, and then make sure that one is earlier in your PATH.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • So, I got the brew version of `gcc`, but trying the same command again yields `clang: warning: argument unused during compilation: '-adhln' [-Wunused-command-line-argument]` - I've made sure that the `bin` directory from the brew installation is the first thing in my PATH variable. Not sure what else to do here. – David Mordigal Aug 17 '19 at 04:22
  • It shows the macOS one, but I’m specifically invoking `gcc-9` which is the one that brew installed. I might just try a Linux VM if I can’t get this to work. – David Mordigal Aug 17 '19 at 16:40