Использование директивы .org с данными в разделе .data: в связи с ld - PullRequest
3 голосов
/ 07 марта 2012

В моих попытках понять, как использовать GNU binutils для создания простого загрузчика с использованием gas Я столкнулся с вопросом, как вы сообщаете компоновщику, куда поместить ваши данные в файлон использует .org для продвижения счетчика местоположений, сохраняя размер файла в 512 байт.Я не могу найти способ сделать это.

Программа сборки, которая пытается сделать это:

# Author: Matthew Hoggan
# Date Created: Tuesday, Mar 6, 2012

.code16                      # Tell assembler to work in 16 bit mode
.section .data
msg:                         # Message to be printed to the screen
  .asciz "hello, world"

.section .text
.globl _start                # Help linker find start of program
_start:
  xor  %ax,  %ax             # Zero out ax register (ah used to specify bios function to Video Services) 
  movw %ax,  %ds             # Since ax is 0x00 0x00 use it to set data segment to 0
  mov  $msg, %si             # Use source index as pointer to string

loop:
  movb %ds:(%si), %al        # Pass data to BIOS function in low nibble of ax
  inc  %si                   # Advance the pointer
  or   %al, %al              # If byte stored in al is equal to zero then...
  jz   _hang                 # Zero signifies end of string
  call print_char            # Print current char in al
  jmp loop                   # Repeat

#
# Function that uses bios function calls
# to print char to screen
#
print_char:
  movb $0x0e, %ah             # Function to print a character to the screen
  movb $0x07, %bl             # color/style to use for the character
  int  $0x10                  # Video Service Request to Bios
  ret

#
# Function used as infinite loop
#
_hang:
  jmp  _hang

.org 510
.byte 0x55, 0xAA

.end

ОБНОВЛЕНИЕ ИспользованиеСледующие команды я получаю следующую ошибку:

mehoggan@mehoggan-laptop:~/Code/svn_scripts/assembly/bootloader/gas$ ld --oformat binary -o string string.o 
string.o: In function `_start':
(.text+0x5): relocation truncated to fit: R_X86_64_16 against `.data'

1 Ответ

4 голосов
/ 07 марта 2012

Для такого рода работ вы должны написать собственный скрипт компоновщика . Просто передайте его компоновщику с параметром -T.

Мой сценарий для почти идентичной проблемы:

SECTIONS
{
    . = 0x1000;
    bootsec :
    {
        *(.text)
        *(.data)
        *(.rodata)
        *(.bss)
        endbootsec = .;
        . = 0x1FE;
        SHORT(0xAA55)
    }
    endbootsecw = endbootsec / 2;
    image = 0x1200;
}

С этим трюком вам не нужно помещать 0x55 0xAA в ассемблер!

0x1000 в начале: На самом деле все прыжки относительны, поэтому они не используются, но они необходимы позже для перехода в защищенный режим ...

endbootsecw, endbootsec и image - это символы, используемые в других местах кода.

...