GAS (gcc/as) automatically generates .text, .data and .bss sections, even if these are not on the file

274 Views Asked by At

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)
0

There are 0 best solutions below