Calculating fill length using the GAS AT & T directives for the boot sector?

So, I want to add padding to the bootsector. For example, currently there is only an endless cycle jmp .. This sector must be 512 bytes long. In addition, a magic number is required 0xaa55, which is added at the end.

jmp .
.skip 508, 0
.word 0xaa55

But what if I want to print something, but donโ€™t want to read all the bytes in order to insert it in the right size?
In Intel / NASM syntax, this will be:

; print something
times 510-($-$$) db 0
dw 0xaa55

But in the AT & T syntax? Well loop ( .rept) does not work here because it .does not give the absolute value that is needed here. We have the same problem with .skip/ .space, they also need an absolute value.

- / .align/.skip/etc?

EDIT: as ld -Ttext 0x7c00 --oformat binary , yasm AT & T.

+4
2

AT & T , - :

.global _start
.text
.code16
_start:
    jmp .

.space 510-(.-_start)
.word 0xaa55

. - . . _start , .

GCC ( LD), , :

gcc -Wl,--oformat=binary -Wl,-Ttext=0x7c00 -Wl,--build-id=none \
    -nostartfiles -nostdlib -m32 -o boot.bin boot.s

-Wl,--oformat=binary , . -Wl,-Ttext=0x7c00 , 0x07c00. -Wl,--build-id=none , , GCC. 0x7c00 - , , , . C runtime, -nostartfiles -nostdlib

, . script. , .


a > . . 0x7c00, , , , DS 0. , , , .

GNU , . .code16 , 16- . .code32 32- , .code64 64- . as .code16.


, , . script, Origin 0x7c00 . , - , . script, , .text, .data, .rodata. , :

bootloader.ld

OUTPUT_FORMAT("elf32-i386");
ENTRY(_start);
SECTIONS
{
    . = 0x7C00;
    /* Code section, .text.bootentry code before other code */
    .text : SUBALIGN(0) {
        *(.text.bootentry);
        *(.text)
    }

    /* Read only data section with no alignment */
    .rodata : SUBALIGN(0) {
        *(.rodata)
    }

    /* Data section with no alignment */
    .data : SUBALIGN(0) {
        *(.data)
    }

    /* Boot signature at 510th byte from 0x7c00 */
    .sig : AT(0x7DFE) {
        SHORT(0xaa55);
    }

    /DISCARD/ : {
        *(.eh_frame);
        *(.comment);
        *(.note*);
    }
}

boot.s, :

# Section .text.bootentry is always placed before all other code and data
# in the linker script. If using multiple object files only specify
# one .text.bootentry as that will be the code that will start executing
# at 0x7c00

.section .text.bootentry
.code16
.global _start
_start:
    # Initialize the segments especially DS and set the stack to grow down from
    # start of bootloader at _start. SS:SP=0x0000:0x7c00
    xor %ax, %ax
    mov %ax, %ds
    mov %ax, %ss
    mov $_start, %sp
    cld                   # Set direction flag forward for string instructions

    mov  $0x20, %al       # 1st param: Attribute black on green
    xor  %cx, %cx         # 2nd param: Screen cell index to write to. (0, 0) = upper left
    mov  $boot_msg, %dx   # 3rd param: String pointer
    call print_str

    # Infinite loop to end bootloader
    cli
.endloop:
    hlt
    jmp .endloop

.section .rodata
boot_msg: .asciz "My bootloader is running"

aux.s :

.global print_str         # Make this available to other modules
.section .text
.code16

# print_str (uint8_t attribute, char *str, uint16_t cellindex)
#
# Print a NUL terminated string directly to video memory at specified screen cell
# using a specified attribute (foreground/background)
#
# Calling convention:
#     Watcom
# Inputs:
#     AL = Attribute of characters to print
#     CX = Pointer to NUL terminated string to print
#     DX = Screen cell index to start printing at (cells are 2 bytes wide)
# Clobbers:
#     AX, ES
# Returns:
#    Nothing

print_str:
    push %di
    push %si

    mov  $0xb800, %di     # Segment b800 = text video memory
    mov  %di, %es
    mov  %cx, %di         # DI = screen cell index (0 = upper left corner)
    mov  %dx, %si         # SI = pointer to string (2nd parameter)
    mov  %al, %ah         # AH = attribute (3rd parameter)
    jmp  .testchar

# Print each character until NUL terminator found
.nextchar:
    stosw                 # Store current attrib(AH) and char(AL) to screen
                          # Advances DI by 2. Each text mode cell is 2 bytes
.testchar:
    lodsb                 # Load current char from string into AL(advances SI by 1)
    test %al, %al
    jne  .nextchar        # If we haven't reach NUL terminator display character
                          #     and advance to the next one

    pop %si
    pop %di
    ret

boot.bin, - :

as --32 aux.s -o aux.o
as --32 boot.s -o boot.o
ld -melf_i386 --oformat=binary -Tlink.ld -nostartfiles -nostdlib \
    aux.o boot.o -o boot.bin

.text.bootentry script. , , 0x7c00. script VMA () 0x7dfe (0xaa55). 0x7dfe - 2 512 . .

.

+7

.org:

    .code16
    .text
    jmp     .
    .org    510
    .word   0xaa55

.org (.) , ( ).

, . .org , .space 510-(.-.text), .text - .text , . , .

+4

Source: https://habr.com/ru/post/1690937/


All Articles