Grub throws "multiboot header not found"

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
animefreak1233
Posts: 19
Joined: Sun Dec 20, 2015 1:18 pm

Grub throws "multiboot header not found"

Post by animefreak1233 »

So, yeah, I was optimizing my IDT, and I thought it'd be agreat idea to replace the idt_entry struct with a uint64_t, and then use bit shifting. But it doesn't work, and grub throws the above error. I'm going to post the relevant snippet here. Please help me out TAT

Code: Select all

#ifndef NO_STRUCT
struct idtEntry{
  uint16_t baseLow;
  uint16_t segSel;
  uint8_t alwaysZero;
  uint8_t flags;
  uint16_t base_high;
  }__attribute__((packed));

  typedef struct idtEntry idtEntry_t;

#else

typedef uint64_t idtEntry_t;
#endif
void idtSetEntry(int entryNo, uint32_t base, uint16_t segSel, uint8_t flags){
#ifndef NO_STRUCT
    idtEntries[entryNo].baseLow = base & ~(0xffff << 16);
    idtEntries[entryNo].segSel = segSel;
    idtEntries[entryNo].flags = flags;
    idtEntries[entryNo].base_high = base >> 16;
    idtEntries[entryNo].alwaysZero = 0;
#else
  idtEntries[entryNo] = (((uint64_t)base & 0xFFFF) | ((uint64_t)segSel << 16) | ((uint64_t)flags << 40) | (((uint64_t)base & 0xFFFF0000) << 32));
#endif
    
}
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

I doubt that is the relevant snippet as it doesn't show where, and how, you are defining your multiboot header. In any case, I'd have said that the struct was the best type for the IDT entries, rather than a simple integer.
animefreak1233
Posts: 19
Joined: Sun Dec 20, 2015 1:18 pm

Re: Grub throws "multiboot header not found"

Post by animefreak1233 »

It shouldn't matter where I've defined my mboot header, since all I'm trying to do is create and identical structure, similar to the struct, but of type uint64_t, and as long as the underlying data structure is the same, it shouldn't matter. And since my OS seems to work with normal structs, I don't see why it wouldn't work with the int
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

animefreak1233 wrote:It shouldn't matter where I've defined my mboot header...
If you say so.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Grub throws "multiboot header not found"

Post by mariuszp »

I have a feeling that the file which contained your multiboot header was linked in first, which allowed it to work, but you havent enforced this rule explicitly and now, for whatever reason, it is not being linked in first.

I put my multiboot header (along with a bunch of 32-bit code which enters long mode and calls the 64-bit kernel etc) in a special section, and i ensure, using a linker script that it is the first section to be linked in.

It definitely has nothing to do with your IDT code.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

Have you examined the object code to confirm that the header is present, correct, and in the correct position?

Have you compared the object code produced in each case to see what differences there are?
animefreak1233
Posts: 19
Joined: Sun Dec 20, 2015 1:18 pm

Re: Grub throws "multiboot header not found"

Post by animefreak1233 »

So, replacing the uint64_t with a uint32_t[2] fixes my problems..... (with obvious changes in the code). Does it have anything to do with the fact that 86 is 32-bit? I was under the impression that the compiler would "assemble" the 64 bit int as most embedded compilers do, and since the intrisical data structure is the same, there shouldn't have been any problem for the CPU to load it.....
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

animefreak1233 wrote:since the intrisical data structure is the same, there shouldn't have been any problem for the CPU to load it.....
Does that mean that you have examined the generated code in each case and confirmed that the data structures are the same? Or are you just assuming this is so?
animefreak1233
Posts: 19
Joined: Sun Dec 20, 2015 1:18 pm

Re: Grub throws "multiboot header not found"

Post by animefreak1233 »

Yes.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Grub throws "multiboot header not found"

Post by Kazinsal »

When you start working with 64-bit numbers in GCC on a 32-bit platform, GCC needs some helper code that it links in from libgcc to handle things like 64-bit bitshifts and division. This code is likely being linked into your binary wherever ld feels it should be linked in, and in this case, it displaces your multiboot header.

This is merely a symptom of a different problem, and is like taking some paracetamol to treat a fever caused by influenza and calling yourself healthy.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

animefreak1233 wrote:Yes.
So the generated code is the same but the behaviour is different? Very strange.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Grub throws "multiboot header not found"

Post by mariuszp »

show us your linker script
animefreak1233
Posts: 19
Joined: Sun Dec 20, 2015 1:18 pm

Re: Grub throws "multiboot header not found"

Post by animefreak1233 »

The linker script is straight up not mine, but here it is-

Code: Select all

/* Link.ld -- Linker script for the kernel - ensure everything goes in the */
/*            Correct place.  */
/*            Original file taken from Bran's Kernel Development */
/*            tutorials: http://www.osdever.net/bkerndev/index.php. */

ENTRY(start)
SECTIONS
{

    .text 0x100000 :
    {
        code = .; _code = .; __code = .;
        *(.text)
        . = ALIGN(4096);
    }

    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        *(.rodata)
        . = ALIGN(4096);
    }

    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Grub throws "multiboot header not found"

Post by mariuszp »

Do this:

Code: Select all

/* Link.ld -- Linker script for the kernel - ensure everything goes in the */
/*            Correct place.  */
/*            Original file taken from Bran's Kernel Development */
/*            tutorials: http://www.osdever.net/bkerndev/index.php. */

ENTRY(start)
SECTIONS
{

    .text 0x100000 :
    {
        code = .; _code = .; __code = .;
        *(.multiboot)
        *(.text)
        . = ALIGN(4096);
    }

    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        *(.rodata)
        . = ALIGN(4096);
    }

    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}
(Notice the change in the ".text" entry).

And explicitly place your multiboot header in a ".multiboot" section
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Grub throws "multiboot header not found"

Post by iansjack »

As you still haven't shown us where and how you define your multiboot header my guess would be:

1. It's in your data section.
2. When using structs the code section is small enough to keep the header near enough the start of the file.
3. When using 64-bit integers the code section is large enough to push the header too far away from the start of the file.

This does contradict your assertions that where you define the header is immaterial and that the code generated in both cases is identical. But I'll ignore that as both are obviously incorrect. And it's just a guess as you haven't given us enough information to work with.

The answer, as has already been said, is to ensure that the header comes at the start of the file.
Post Reply