我可以阻止链接器添加填充字节吗?

问题描述 投票:0回答:1

我正在尝试一些 x86 裸机代码,特别是一个简单的引导加载程序,它加载第二阶段等,所有这些都打包到一个映像中。

现在,我有以下链接器脚本:

OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)
ENTRY(_start)

SECTIONS {
        . = 0x7c00;
        .boot : {
                boot.o (.boot);
        }
        . = 0x7c00 + 510;
        .magic : {
                SHORT(0xaa55);
        }

        . = 0x10000;
        .text32 : {
                boot.o (.boot32);
                main32.o (.text);
        }
}

这里的想法是,第一阶段加载的代码应该假设在

0x10000
运行。 链接器将用零填充
.boot
.text32
之间的间隙。 这有点没用,因为第一阶段无论如何都会加载它,所以不需要填充。 我可以以某种方式让链接器忽略间隙吗? (就好像我在复制两个二进制 blob)。理想情况下,如果
.text32
部分从结果文件中的偏移量 512 开始就足够了。但代码应该链接起来,就像它从
0x10000
开始一样。

这有可能吗?

c x86 linker ld bare-metal
1个回答
0
投票

您可以使用链接器开关

-n, --nmagic
。如果没有此开关,各部分将按页面对齐,因此链接器会用零填充之间的空间。

(以下示例适用于

ARM
,但
-n, --nmagic
的行为在
x86
上应该是相同的。)

没有

-n, --nmagic

ELF 头和符号表:

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8000299
  Start of program headers:          52 (bytes into file)
  Start of section headers:          9012 (bytes into file)
  Flags:                             0x5000200, Version5 EABI, soft-float ABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         3
  Size of section headers:           40 (bytes)
  Number of section headers:         11
  Section header string table index: 10

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08000000 001000 00038c 00  AX  0   0  4
  [ 2] .rodata           PROGBITS        24000000 002000 000000 00  WA  0   0  1
  [ 3] .data             PROGBITS        24000000 002000 000000 00  WA  0   0  1
  [ 4] .bss              NOBITS          24000000 002000 000000 00  WA  0   0  1
  [ 5] .heap             NOBITS          24000000 002000 07f000 00  WA  0   0  1
  [ 6] .stack            NOBITS          2407f000 002000 001000 00  WA  0   0  1
  [ 7] .ARM.attributes   ARM_ATTRIBUTES  00000000 002000 000021 00      0   0  1
  [ 8] .symtab           SYMTAB          00000000 002024 000200 10      9  14  4
  [ 9] .strtab           STRTAB          00000000 002224 0000bd 00      0   0  1
  [10] .shstrtab         STRTAB          00000000 0022e1 000051 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), y (purecode), p (processor specific)

文件大小:

-rwxrwxr-x 1 jk jk 9452 Oct 22 04:42 test.elf

-n, --nmagic

ELF 头和符号表:

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8000299
  Start of program headers:          52 (bytes into file)
  Start of section headers:          1880 (bytes into file)
  Flags:                             0x5000200, Version5 EABI, soft-float ABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         3
  Size of section headers:           40 (bytes)
  Number of section headers:         11
  Section header string table index: 10

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08000000 000094 000390 00  AX  0   0  4
  [ 2] .rodata           PROGBITS        24000000 000424 000000 00  WA  0   0  1
  [ 3] .data             PROGBITS        24000000 000424 000000 00  WA  0   0  1
  [ 4] .bss              NOBITS          24000000 000424 000000 00  WA  0   0  1
  [ 5] .heap             NOBITS          24000000 000424 07f000 00  WA  0   0  1
  [ 6] .stack            NOBITS          2407f000 000424 001000 00  WA  0   0  1
  [ 7] .ARM.attributes   ARM_ATTRIBUTES  00000000 000424 000021 00      0   0  1
  [ 8] .symtab           SYMTAB          00000000 000448 000200 10      9  14  4
  [ 9] .strtab           STRTAB          00000000 000648 0000bd 00      0   0  1
  [10] .shstrtab         STRTAB          00000000 000705 000051 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), y (purecode), p (processor specific)

文件大小:

-rwxrwxr-x 1 jk jk 2320 Oct 22 04:50 test.elf

正如您通过开关

-n, --nmagic
所看到的,生成的二进制文件小了
7132
字节。

© www.soinside.com 2019 - 2024. All rights reserved.