4

I wrote some assembly code for x86, and wanted to assemble it into plain binary file (not Mach-O) by just using Mac's default assembler (the 'as'). After several googlings and tries, I failed.

Though I know how to do it with NASM (using option: -f binary), I don't want to do so. Because my code was written in and I was used to AT&T syntax.

Kilobyte
  • 43
  • 7

1 Answers1

5

Use objcopy (which is part of binutils as is as) with --output-target set to binary.

as --64 test.s -o test.o
objcopy -O binary test.o test.bin

And to disassemble the output:

objdump -D -m i386:x86-64:intel -b binary test.bin

(Substitute --32 and remove x86-64: for 32-bit versions)

user786653
  • 29,780
  • 4
  • 43
  • 53
  • I think this is not easy to do with Mac, since the binutils included in Xcode Developer Tools uses modified `as` and even has no `objdump` packed in. – Kilobyte Nov 03 '11 at 15:57
  • Hmm, looks like you're right, sorry. Perhaps [Agner Fogs objconv](http://www.agner.org/optimize/#objconv) can help you until I figure something better out. – user786653 Nov 03 '11 at 16:08
  • Thanks. It's a great tool, but it seems that it do not support the conversion between Mach-O and flat binary format according to its manual. Or am I missing something? Actually, after a little research I find there is no obvious way to do it with Mac's limited tools and I'll try out your first approach some day by building a cross-compile environment of GNU tool chain. At the current time, NASM looks like the most quick workaround to me. – Kilobyte Nov 03 '11 at 17:07
  • You also need to use `ld` between `as` and `objcopy`. (See [this answer on another question](https://stackoverflow.com/a/32594933/497934) for an example.) Without it, the binary will be missing relocations. For very small, simple assemblies that happen not to generate any relocations, things might appear to work regardless. – Maxpm May 20 '20 at 02:29