重新定位截断以适应:16 对“.text”

avc*_*ado 2 assembly gnu-assembler ld bootloader x86-16

我希望你们度过了愉快的一天。我有一个关于将程序集编译为.bin. 我正在尝试使用这篇文章来修复它,但即便如此,我还是得到了`relocated truncation to fit: 16 against '.text'。

bootReal.s :

#generate 16-bit code
.code16

#hint the assembler that here is the executable code located
.text
.globl _start;
#boot code entry
_start:
      jmp _boot                           #jump to boot code
      welcome: .asciz "Hello, World\n\r"  #here we define the string

     .macro mWriteString str              #macro which calls a function to print a string
          leaw  \str, %si
          call .writeStringIn
     .endm

     #function to print the string
     .writeStringIn:
          lodsb
          orb  %al, %al
          jz   .writeStringOut
          movb $0x0e, %ah
          int  $0x10
          jmp  .writeStringIn
     .writeStringOut:
     ret

_boot:
     mWriteString welcome

     #move to 510th byte from the start and append boot signature
     . = _start + 510
     .byte 0x55
     .byte 0xaa
Run Code Online (Sandbox Code Playgroud)

我使用的命令:

as bootReal.s -o bootReal.s

ld.exe -o file.tmp bootReal.o <- 这个抛出错误

任何帮助,将不胜感激!

Mic*_*tch 5

正如@jester 建议的那样,您需要设置一个虚拟内存地址(VMA)起点。您在 Windows 上使用的链接器使用了一个内部链接器脚本,该脚本设置了 VMA >= 0x10000。因此,对不能容纳 16 位的绝对地址的任何引用都会产生重定位错误。您无法将地址 >= 0x10000 放入 16 位寄存器,因此链接器中止并显示类似于以下内容的错误:

(.text+0x1f): 重定位被截断以适应:R_386_16 对 `.text'

如果您使用的是 64 位工具链,错误可能会略有不同,但它会类似于 R_???????_16 against '.text'

要解决此问题,您可以创建自己的链接描述文件或在 LD 命令行上将基本 VMA 设置为适当的值。我建议-Ttext=0x7c00在引导加载程序中使用DS 并将其设置为 0x0000。

我有一般的引导加载程序技巧,其中讨论了编写引导加载程序的许多问题。您不应假设在引导加载程序运行时段寄存器具有任何特定值。如果您-Ttext=0x7c00用作 VMA (ORG),则需要将 DS 设置为零。的段:偏移量对0×0000:0x7c00 =物理地址0x07c00(0×0000 << 4 + 0x7c00)。0x07c00 是传统 BIOS 将引导扇区加载到内存的位置。

如果您遇到重定位错误,例如:

(.text+0x1f): 重定位被截断以适应:R_386_16 对 `.text'

您始终可以使用 OBJDUMP 来显示目标文件中的重定位条目。在这种情况下,由于您正在编写 16 位代码,因此您需要让 OBJDUMP 转储代码 ( -D); 解码为 16 位指令 ( -Mi8086) 并输出重定位条目 ( -r)。的输出objdump -Mi8086 -Dr bootReal.o看起来与此类似(它可能因所使用的链接器而异):

00000000 <_start>:
   0:   eb 1b                   jmp    1d <_boot>

00000002 <welcome>:
   2:   48                      dec    %ax
   3:   65                      gs
   4:   6c                      insb   (%dx),%es:(%di)
   5:   6c                      insb   (%dx),%es:(%di)
   6:   6f                      outsw  %ds:(%si),(%dx)
   7:   2c 20                   sub    $0x20,%al
   9:   57                      push   %di
   a:   6f                      outsw  %ds:(%si),(%dx)
   b:   72 6c                   jb     79 <_boot+0x5c>
   d:   64 0a 0d                or     %fs:(%di),%cl
        ...

00000011 <.writeStringIn>:
  11:   ac                      lods   %ds:(%si),%al
  12:   08 c0                   or     %al,%al
  14:   74 06                   je     1c <.writeStringOut>
  16:   b4 0e                   mov    $0xe,%ah
  18:   cd 10                   int    $0x10
  1a:   eb f5                   jmp    11 <.writeStringIn>

0000001c <.writeStringOut>:
  1c:   c3                      ret

0000001d <_boot>:
  1d:   8d 36 02 00             lea    0x2,%si
                        1f: R_386_16    .text
  21:   e8 ed ff                call   11 <.writeStringIn>
        ...
 1fc:   00 00                   add    %al,(%bx,%si)
 1fe:   55                      push   %bp
 1ff:   aa                      stos   %al,%es:(%di)
Run Code Online (Sandbox Code Playgroud)

在重定位错误.text+0x1f被引用为问题的根源。如果您查看 OBJDUMP 输出,则会有一个重定位条目:

  1d:   8d 36 02 00             lea    0x2,%si
                        1f: R_386_16    .text
Run Code Online (Sandbox Code Playgroud)

此重定位与其上方的指令相关联。这实质上意味着链接器试图为LEA指令生成偏移量,但该值无法用 16 位值表示。SI是 16 位寄存器,不能在其中放置大于等于 0x10000 的值。


代码问题

  • 如果使用 0x7c00 的 VMA (ORG),您希望将 DS 正确设置为 0
  • 确保字符串指令,如lodsb向前移动字符串。使用该CLD指令清除方向标志(DF=0)。
  • 设置自己的堆栈指针SS:SP通常是个好主意。如果您将更多数据从磁盘读取到内存中,这一点很重要。您不知道 BIOS 在哪里设置SS:SP并且您不想破坏它。设置堆栈的方便位置位于引导加载程序下方 0x0000:0x7c00。
  • 为了防止引导加载程序在最后一条指令后运行半随机代码,您需要将处理器置于某种无限循环中。一个简单的方法是jmp .
  • LD 将以非二进制文件的格式生成可执行文件,并且不能作为引导加载程序运行。您可以让链接器使用该--oformat=binary选项写入二进制文件。

修改后的代码可能如下所示:

#generate 16-bit code
.code16

#hint the assembler that here is the executable code located
.text
.globl _start;
#boot code entry
_start:
      jmp _boot                           #jump to boot code
      welcome: .asciz "Hello, World\n\r"  #here we define the string

     .macro mWriteString str              #macro which calls a function to print a string
          leaw  \str, %si
          call .writeStringIn
     .endm

     #function to print the string
     .writeStringIn:
          lodsb
          orb  %al, %al
          jz   .writeStringOut
          movb $0x0e, %ah
          int  $0x10
          jmp  .writeStringIn
     .writeStringOut:
     ret

_boot:
     xor %ax, %ax
     mov %ax, %ds      # Initialize the DS segment to zero
     mov %ax, %ss      # Set the stack pointer below bootloader at 0x0000:0x7c00
     mov $0x7c00, %sp
     cld               # Clear Direction Flag (DF=0) to set forward movement on string
                       # instructions

     mWriteString welcome
     jmp .             # Enter an infinite loop to stop executing code beyond this point

     #move to 510th byte from the start and append boot signature
     . = _start + 510
     .byte 0x55
     .byte 0xaa
Run Code Online (Sandbox Code Playgroud)

要组装和运行,您可以使用以下内容:

as bootReal.s -o bootReal.o
ld -Ttext=0x7c00 --oformat=binary -o boot.bin bootReal.o
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用 LD 生成可执行文件并使用 OBJCOPY 将可执行文件转换为二进制文件:

as bootReal.s -o bootReal.o
ld -Ttext=0x7c00 -o file.tmp bootReal.o
objcopy -O binary file.tmp boot.bin
Run Code Online (Sandbox Code Playgroud)

无论哪种方式都应该生成一个 512 字节的二进制文件(引导加载程序),名为boot.bin. 如果您使用 QEMU 运行它,qemu-system-i386 -fda boot.bin输出将类似于:

在此处输入图片说明