GAS(GCC/AS)自动生成.TEXT,.DATA和.BSS部分,即使这些部分不在文件中

发布于 2025-02-13 10:18:34 字数 4919 浏览 1 评论 0原文

我正在尝试将OS开发教程从NASM Intel语法移植到GAS(GCC/AS)。

有一个文件只有一个部分(.multiboot_header),该文件仅包含引导加载程序的数据。我在Intel和AT& t语法上都有该文件,但是当我编译它们时,用燃气编译的一个部分会产生源文件中要求的部分:

NASM:

~$ objdump -h multiboot_header.asm.o 

multiboot_header.asm.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .multiboot_header 0000000c  0000000000000000  0000000000000000  00000180  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA

GCC: GCC:

$ objdump -h multiboot_header.S.o

multiboot_header.S.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000000  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000000  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  0000000000000000  0000000000000000  00000040  2**0
                  ALLOC
  3 .multiboot_header 0000000c  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, READONLY

正如您可以看到GCC生成了额外的部分,并且文件偏移也不完全相同。为什么会发生这种情况,我该如何解决?

编辑:

教程我关注: https://os.phil-opp.com/multiboot--multiboot--内核/

这是链接器脚本IM使用:

ENTRY(start)

SECTIONS {
    . = 1M;
    
    .boot :
    {
        /* ensure that the multiboot header is at the beginning */
        *(.multiboot_header)
    }
    
    .text :
    {
        *(.text)
    }
}

另外,这是Multiboot_header.s文件:

.section .multiboot_header
header_start:
    .long 0xe85250d6        # magic number (multiboot 2)
    .long 0             # architecture 0 (protected mode x86)
    .long header_end - header_start # header lenght
    # checksum
    .long 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
    
    # insert optional multiboot tags here
    
    # required end tag
    .word 0 # type
        .word 0 # flags
        .long 8 # size
header_end:

readelf -s的输出-S构建/kernel-x86_64.bin with Gas:

There are 6 section headers, starting at offset 0x1d8:

Section Headers:
  [Nr] Name              Type             Address           Offset         Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000       0000000000000000  0000000000000000           0     0     0
  [ 1] .boot             PROGBITS         0000000000100000  000000ee       0000000000000018  0000000000000000           0     0     1
  [ 2] .text             PROGBITS         0000000000100000  00000078       0000000000000076  0000000000000000  AX       0     0     1
  [ 3] .symtab           SYMTAB           0000000000000000  00000108       0000000000000078  0000000000000018           4     4     8
  [ 4] .strtab           STRTAB           0000000000000000  00000180       000000000000002c  0000000000000000           0     0     1
  [ 5] .shstrtab         STRTAB           0000000000000000  000001ac       0000000000000027  0000000000000000           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), l (large), p (processor specific)

with Nasm:

There are 6 section headers, starting at offset 0x228:

Section Headers:
  [Nr] Name              Type             Address           Offset         Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000       0000000000000000  0000000000000000           0     0     0
  [ 1] .boot             PROGBITS         0000000000100000  00000080       0000000000000018  0000000000000000   A       0     0     1
  [ 2] .text             PROGBITS         0000000000100020  000000a0       0000000000000076  0000000000000000  AX       0     0     16
  [ 3] .symtab           SYMTAB           0000000000000000  00000118       0000000000000090  0000000000000018           4     5     8
  [ 4] .strtab           STRTAB           0000000000000000  000001a8       0000000000000059  0000000000000000           0     0     1
  [ 5] .shstrtab         STRTAB           0000000000000000  00000201       0000000000000027  0000000000000000           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), l (large), p (processor specific)

I am trying to port an OS development tutorial from NASM Intel syntax, to GAS (gcc/as).

There is one file that only has one section (.multiboot_header) that's suposed to only contain data for the bootloader. I have that file both on Intel and AT&T syntax, but when I compile them, the one compiled with GAS produces sections that werent asked for in the source file:

Nasm:

~$ objdump -h multiboot_header.asm.o 

multiboot_header.asm.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .multiboot_header 0000000c  0000000000000000  0000000000000000  00000180  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, DATA

Gcc:

$ objdump -h multiboot_header.S.o

multiboot_header.S.o:     file format elf64-x86-64

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000000  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  1 .data         00000000  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, ALLOC, LOAD, DATA
  2 .bss          00000000  0000000000000000  0000000000000000  00000040  2**0
                  ALLOC
  3 .multiboot_header 0000000c  0000000000000000  0000000000000000  00000040  2**0
                  CONTENTS, READONLY

As you can see gcc generates extra sections, and the file offset is not quite the same too. Why is this happening, and how can i solve it?

Edit:

Tutorial i am following: https://os.phil-opp.com/multiboot-kernel/

Here's the linker script im using:

ENTRY(start)

SECTIONS {
    . = 1M;
    
    .boot :
    {
        /* ensure that the multiboot header is at the beginning */
        *(.multiboot_header)
    }
    
    .text :
    {
        *(.text)
    }
}

Also, here's the multiboot_header.S file:

.section .multiboot_header
header_start:
    .long 0xe85250d6        # magic number (multiboot 2)
    .long 0             # architecture 0 (protected mode x86)
    .long header_end - header_start # header lenght
    # checksum
    .long 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
    
    # insert optional multiboot tags here
    
    # required end tag
    .word 0 # type
        .word 0 # flags
        .long 8 # size
header_end:

Output of readelf -S build/kernel-x86_64.bin with GAS:

There are 6 section headers, starting at offset 0x1d8:

Section Headers:
  [Nr] Name              Type             Address           Offset         Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000       0000000000000000  0000000000000000           0     0     0
  [ 1] .boot             PROGBITS         0000000000100000  000000ee       0000000000000018  0000000000000000           0     0     1
  [ 2] .text             PROGBITS         0000000000100000  00000078       0000000000000076  0000000000000000  AX       0     0     1
  [ 3] .symtab           SYMTAB           0000000000000000  00000108       0000000000000078  0000000000000018           4     4     8
  [ 4] .strtab           STRTAB           0000000000000000  00000180       000000000000002c  0000000000000000           0     0     1
  [ 5] .shstrtab         STRTAB           0000000000000000  000001ac       0000000000000027  0000000000000000           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), l (large), p (processor specific)

and with NASM:

There are 6 section headers, starting at offset 0x228:

Section Headers:
  [Nr] Name              Type             Address           Offset         Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000       0000000000000000  0000000000000000           0     0     0
  [ 1] .boot             PROGBITS         0000000000100000  00000080       0000000000000018  0000000000000000   A       0     0     1
  [ 2] .text             PROGBITS         0000000000100020  000000a0       0000000000000076  0000000000000000  AX       0     0     16
  [ 3] .symtab           SYMTAB           0000000000000000  00000118       0000000000000090  0000000000000018           4     5     8
  [ 4] .strtab           STRTAB           0000000000000000  000001a8       0000000000000059  0000000000000000           0     0     1
  [ 5] .shstrtab         STRTAB           0000000000000000  00000201       0000000000000027  0000000000000000           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), l (large), p (processor specific)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文