Linux 使用.org指令处理.data节中的数据:与ld关联
在我努力理解如何使用GNUBinautils构建一个简单的引导加载程序时,使用 我遇到了这样一个问题:如何告诉链接器将数据放在一个文件中,该文件使用.org来推进位置计数器,同时保持文件大小为512字节。我似乎找不到办法来做这件事 尝试执行此操作的汇编程序是:Linux 使用.org指令处理.data节中的数据:与ld关联,linux,ld,gnu-assembler,att,Linux,Ld,Gnu Assembler,Att,在我努力理解如何使用GNUBinautils构建一个简单的引导加载程序时,使用 我遇到了这样一个问题:如何告诉链接器将数据放在一个文件中,该文件使用.org来推进位置计数器,同时保持文件大小为512字节。我似乎找不到办法来做这件事 尝试执行此操作的汇编程序是: # Author: Matthew Hoggan # Date Created: Tuesday, Mar 6, 2012 .code16 # Tell assembler to work in
# 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'
对于这种工作,你应该自己写。只需使用
-T
选项将其传递给链接器
我针对一个几乎相同的问题的脚本是:
SECTIONS
{
. = 0x1000;
bootsec :
{
*(.text)
*(.data)
*(.rodata)
*(.bss)
endbootsec = .;
. = 0x1FE;
SHORT(0xAA55)
}
endbootsecw = endbootsec / 2;
image = 0x1200;
}
使用该技巧,您不需要将0x55 0xAA
放入汇编程序
开始时的0x1000
:实际上所有的跳转都是相对的,因此不使用它,但稍后需要跳转到保护模式
endbootsecw
、endbootsecw
和image
是代码中其他地方使用的符号。I get:a.ld:11无法使用Binutils 2.24上的链接器脚本向后移动位置计数器(从000000000000 1200到000000 11fe)
。@CiroSantilli六四事件法轮功纳米比亚威视: 好吧,看来你的版块已经满了。也就是说,代码以0x1200
结尾,然后在链接器中尝试将指针移回0x11FE
,这是不允许的。如果你的asm中有太多的代码,或者你同时在做我的链接器技巧和.org
的事情,可能会发生这种情况。“你同时在做我的链接器技巧和.org的事情”DOH!生成此错误的最小示例及其含义:具有工作引导扇区的存储库+BIOS示例: