Search code examples
gnunasmassemblyi386

Creating plain binary data (no ELF, symbol table etc) using an assembler


I want to turn a data-only input file, i.e. something like this:

 .data
 .org 0
 .equ foo, 42
 .asciz "foo"
label:
 .long 0xffffffff
 .long 0x12345678
 .byte foo
 .long label
 .long bar
 .equ bar, 'x'

into a file with the corresponding byte sequence 'f','o','o', 0, 0xff, 0xff, 0xff, 0xff, 0x78, 0x56, 0x34, 0x12, 42, 4, 0, 0, 0, 'x', 0 , 0, 0.

When I assemble this with GNU as (as -o foo.o -s foo.S), I get an 400+ bytes ELF file. How can I make GNU as (or NASM or any other assembler) give me the plain binary representation? I've studied the GNU as options but to no avail. I can modify the input format, if that makes the answer easier (i.e. use more and different pseudo ops).

Any hints deeply appreciated!

Regards, Jens


Solution

  • I dug around a bit and found a solution using nasm, grabbed from http://www.nasm.us/.

    The equivalent directives for the original data would be something like this:

         org 0
    foo  equ 42
         db "foo", 0
    label:
         dd 0xffffffff
         dd 0x12345678 
         db foo
         dd label
         dd bar
    bar  equ 'x'
    

    Assemble this with nasm -f bin -o file.bin file.S. Voila! Plain binary in file.bin. Guess that makes me a self-learner :-)