Pergunta

I am currently writing a simple bootloader as a personal project. I have a working prototype that was built using NASM as my assembler. However I would like to get more familiar with GNU tools, and so I am attempting to rewrite my work using them.

There is this small sample online 'Hello World' Bootloader that demonstrates the use of several tools for writing and building the 'Bootloader'. However, after some reading, it is to my understanding that 'gas' is a backend to gcc, and that it should not be directly invoked. I came across this information when I was trying to write expressions inside my assembly file that used symbols to caclulate the size of the program so I would know how many 0 bytes I would have to write before writing 0x55, and 0xAA, which can be accomplished using this bit of NASM code:

;---------------------------------------------
; Write Zeros up to end of program - 2 then boot signature
;---------------------------------------------
size    equ     $ - entry
        times   (512 - size - 2) db 0
        db      0x55, 0xAA              ;2  byte boot signature

Is there a way to write this equivalent set of expressions using the syntax natively read by GAS? My attempts have failed, for example:

.size len, ( 512 - ( (message + 12) - _start) )

Using this expression and passing it to as gives me the following error:

./gasbootloader.asm:24: Error: invalid sections for operation on `message' and `_start'

Do I need to pass my program through gcc so the symbols can get resolved?

Foi útil?

Solução

The GNU assembler has a .org directive which makes the math you had to do in NASM unnecessary. Here's an example source file:

entry:
  xor %eax, %eax
  ret

  .org 510
  .byte 0x55, 0xAA

And a dump of the object file after building:

$ clang -c -o example.o example.s
$ otool -t example.o
example.o:
(__TEXT,__text) section
0000000000000000 31 c0 c3 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000170 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000180 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
0000000000000190 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001c0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001d0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001e0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00000000000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa 

As you can see, the 0x55 0xAA ends up where you want it to without any special effort.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top