5

I cannot believe how hard this seems to be. I am working from SPARC Solaris 8. and we have some kind of GNU-gcc (3.4.2) and 'as' assembler (Sun WorkShop 6 2003/12/18 Compiler Common 6.0).

Anyways, I've tried a few ways to output a pure binary file. such as

gcc -c yadda.s

or invoking solely the assembler

as yadda.s

I have also investigated the assembler man page, but I did not find much breaking news. :( AS Man page: http://pastebin.com/0FSNxhq1

So, I still get some Bloat in my resulting object file. I don't want this leading 50 bytes of ELF related * or whatever the assembler thought would help my initialization or whatever it's thinking.

What I am looking for is a pure binary output of JUST my code.

Thanks in advance :)

bazz
  • 413
  • 6
  • 14
  • In the meantime I am working on compiling Gnu binutils. maybe their assembler will work the way I want it to... – bazz Oct 30 '12 at 03:26
  • binutils's assembler *is* `as`. – ughoavgfhw Oct 30 '12 at 04:09
  • thanks ughoavgfhw, but I think I still might need the binutils. if not only for 'objcopy' as in the answer below, but also because this newer 'as' may have additional functionality that the one on this box doesn't have, – bazz Oct 30 '12 at 04:49
  • yes, I have just installed binutils and it's looking very awesome!!! I'm going to check it out! bb with more info – bazz Oct 30 '12 at 04:59
  • Yes, so with the binutils package, the 'as' is 1000% more equipped. and the objcopy answer below is doing exactly what I want :) My only last wonder if this newer 'as' is capable of producing a pure binary product without the need of objcopy – bazz Oct 30 '12 at 05:07
  • Possible duplicate of [How to generate plain binaries like nasm -f bin with the GNU GAS assembler?](http://stackoverflow.com/questions/6828631/how-to-generate-plain-binaries-like-nasm-f-bin-with-the-gnu-gas-assembler) – Ciro Santilli OurBigBook.com Nov 09 '15 at 12:05

2 Answers2

8

The assembler does not output executable code, it outputs an object file. To make it executable, you need to link it using ld. This command allows you to specify the output format, which can be binary (if your build supports it).

as yadda.s -o yadda.o
ld yadda.o -o yadda --oformat=binary

You should be able to do this in one line using gcc:

gcc yadda.s -o yadda -Xlinker --oformat=binary
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
4

If you really want a raw binary file, you can generate one from an ELF file using:

objcopy -O binary yadda.elf yadda.bin

Note that you won't be able to run the resulting file normally, since the kernel will no longer have any idea how to load it.

  • ah, this system does not have objcopy :( or at least not in my PATH. I tried a whereis objcopy, that did not help either. :( I also tried installing binutils but my 'make' failed :( and there's almost no helpful output .. make[1]: Entering directory `/tmp/binutils-2.9.1/libiberty' gcc -c -g -I. -I./../include -DHAVE_SYSCONF ./dummy.c 2>/dev/null make[1]: *** [dummy.o] Error 1 make[1]: Leaving directory `/tmp/binutils-2.9.1/libiberty' make: *** [all-libiberty] Error 2 – bazz Oct 30 '12 at 03:47
  • Ok I got the errors because (how was I supposed to know that) binutils-2.23 is actually NEWER than 2.9.1 lol. How do they expect people to follow that logic??? So, with the new binutils package and the objcopy command, it works flawlessly!! this is exactly what I want.. Although I am eager to see if the new 'as' can output pure binary in and of itself. – bazz Oct 30 '12 at 05:05
  • 1
    @bazz - it's common version numbering for open source projects, the numbers are not decimals in which 2.23 < 2.9, but dot separated integers, in which 9 < 23. – alanc Oct 30 '12 at 18:07
  • doesnt make much sense to me. I hate to go against the grain but I probably will. I like when the first decimal maintains precedence. so 2.9 > 2.32, where 2.32 is a sub-improvement on 2.3 .. .this makes sense to me. But thank you for expanding my knowledge on standards :) especially open source standards ;) – bazz Jan 10 '13 at 03:15