assembly - 重定位被截断以适合 : 16 against `.text'

标签 assembly ld x86-16 bootloader gnu-assembler

希望你们今天过得愉快。我有一个关于将程序集编译为 .bin 的问题。我正在尝试使用This article来修复它,但即便如此,我还是得到了“重新定位截断以适应:16对“.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

我使用的命令:

as bootReal.s -o bootReal.s

ld.exe -o file.tmp bootReal.o <- 这会引发错误

如有任何帮助,我们将不胜感激!

最佳答案

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

(.text+0x1f): relocation truncated to fit: R_386_16 against `.text'

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

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

我有General Bootloader讨论编写引导加载程序时遇到的许多问题的技巧。您不应该假设当引导加载程序运行时段寄存器具有任何特定值。如果您使用-Ttext=0x7c00作为 VMA (ORG),那么您需要将 DS 设置为零。 segment:offset对 0x0000:0x7c00 = 物理地址 0x07c00 (0x0000<<4+0x7c00)。 0x07c00 是旧版 BIOS 将引导扇区加载到内存中的位置。

如果您遇到类似以下的重定位错误:

(.text+0x1f): relocation truncated to fit: R_386_16 against `.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)

重定位错误中.text+0x1f被认为是问题的根源。如果您查看 OBJDUMP 输出,会发现一个重定位条目:

  1d:   8d 36 02 00             lea    0x2,%si
                        1f: R_386_16    .text

此重定位与其上方的指令相关联。这本质上意味着链接器试图为 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

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

as bootReal.s -o bootReal.o
ld -Ttext=0x7c00 --oformat=binary -o boot.bin bootReal.o

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

as bootReal.s -o bootReal.o
ld -Ttext=0x7c00 -o file.tmp bootReal.o
objcopy -O binary file.tmp boot.bin

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

enter image description here

关于assembly - 重定位被截断以适合 : 16 against `.text' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60118664/

相关文章:

assembly - JMP rel16(而不是 JMP rel32)

c - 如何链接到其中包含 main 的对象?

程序集 8086 : problems with counter

c++ - 编译带头文件的 C++ 程序(新手)

gcc - 链接时 '-lfoo'和 '/path/to/libfoo.so'的区别

assembly - 如何计算8086 IDIV指令的结果?

assembly - 是否可以使用外部宏参数被内部宏使用?

c - 打印控制台页面后,如何暂停我的 C 程序?

assembly - NASM:解析器:指令预期 rep movs

assembly - 是否有任何组装测试框架?