ELF Linker script quick question

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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

ELF Linker script quick question

Post by Neo »

Does anyone have a ELF linker script for a kernel to be loaded at the 3 Gig virtual address?
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

I am using this one right now,

Code: Select all

OUTPUT_FORMAT("elf32-i386")

ENTRY(start)

physical = 0x100000 ;    /* 1 meg */
virtual = 0xC0000000;    /* 3 gig */

SECTIONS
{
  .text virtual : AT(physical)
  {
    code = .; _code = .;
    *(.text)
    *(.rodata*)
    . = ALIGN(4096);
  }

  .bss (ADDR(.text) + SIZEOF(.text)) : /*AT(physical  + (code - bss))*/
  {
    bss = .; _bss = .;
    *(.bss)
    *(COMMON)
    . = ALIGN(4096);
    _bssend = .;
  }
    
  .data (ADDR(.bss) + SIZEOF(.bss)) : /*AT(physical + (code - data))*/
  {
    data = .; _data = .;
    
    start_ctors = .;
    *(.ctors*)
    end_ctors = .;
    
    start_dtors = .;
    *(.dtors)
    end_dtors = .;
    
    *(.data)
    . = ALIGN(0x1000);    
  }
  

  . = ALIGN(4);
  end = .; _end = .;
}
but GRUB is complaining with
Error 28: Selected item cannot fit into memory
and I seem to have forgotten a lot. :)
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

That gives me
elf.ld:39 non constant expression for load base
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

Ok has anyone used the linker script given at the link? and if so have you faced any problems?
Would be nice to know if you were able to workaround them.
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:ELF Linker script quick question

Post by Pype.Clicker »

any chance you're using a too old version of GRUB ? iirc, previous version had a bug in the way physical/virtual addresses were used that prevented things like higher-half OSes to be executed directly.

HigherHalfBareBones compiles like a charm on Linux with GNU ld 2.16.1 and gcc 4.0.2.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

I am using CYGWIN with ld 2.16.91 and gcc 4.0.2.
Are there any special linker options that should be passed on?
I am using only the -no-undefined right now.
Only Human
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

Ok, noticed something funny right now.
I replaced all the phsyical address expressions (AT(....)) with constant addresses but I get the same error
elf.ld:58 non constant expression for load base
Am begginning to think there is somethin wrong with ld. ::)


<edit>
Tried with ld 2.16.1. Still same result.
Any ideas??
Has anyone used an ELF kernel that is booted by GRUB to the 3 gig virtual address??
</edit>
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:ELF Linker script quick question

Post by Pype.Clicker »

googling for the error message pointed me to http://www.skyfree.org/linux/references/ld.pdf ...

Sec. 3.10.5 may help ... maybe not. sorry: i don't have any cygwin at hand here, so i fear i cannot help more. Maybe changelogs will help...
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

Thanks Pype. I had seen something similar somewhere on the net (think it was the ld manual) when I searched, but it wasn't of much use.

Also this problem occurs only when the OUTPUT_FORMAT is set to "elf-i386" when I changed this to "binary" it linked properly (of course GRUB couldn't load it).

Not sure what else to do. :(
Only Human
mystran

Re:ELF Linker script quick question

Post by mystran »

Do you have some specific reason to put .bss before .data? That seems a bit weird to me..

Anyway...

I remember having similar issues when I tried to give addresses to sections. Instead adding a .=loadaddress before .text section (like in the FAQ sample script) loads fine with me.

I'm not trying to put my kernel to upper half though, so YMMV.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

I was moving the sections around trying to see what if it was causing the probelms.
Only Human
Mark

Re:ELF Linker script quick question

Post by Mark »

In my little OS I use:
Note, I located my boot code seperately so I can reclaim the code once booted.

OUTPUT_FORMAT("elf32-i386")
ENTRY (_loader)
SECTIONS
{
. = 0x00100000;
   .boot :
{
   src/obj/startup.o(.text)
   src/obj/boot.o(.text)
      src/obj/bootscreen.o(.text)
      src/obj/startup.o(.rodata)
   src/obj/boot.o(.rodata)
      src/obj/bootscreen.o(.rodata)
}
   .boot_bss ALIGN(0x1000) :
   {
      src/obj/startup.o(.bss)
   src/obj/boot.o(.bss)
      src/obj/bootscreen.o(.bss)
}
   .boot_data ALIGN(0x1000) :
   {
      src/obj/startup.o(.data)
   }
   .bss 0xD0000000 : AT(0x0010c000)
{
   *(.bss)
}
   .data 0xD0001000 : AT (0x0010d000)
{
*(.data)
}
   .text 0xC0000000 : AT(0x00110000)
{
      *(.text)
      *(.rodata)
}
}   
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:ELF Linker script quick question

Post by Neo »

@Mark: Your script gave the same ld error (non constant expression for load base).

This definitely looks like an 'ld' bug now.

Just out of curiosity. Hasn't anyone here used an ELF kernel that is loaded at some virtual address different from the phsyical address? :o
Only Human
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:ELF Linker script quick question

Post by Colonel Kernel »

Neo wrote:Just out of curiosity. Hasn't anyone here used an ELF kernel that is loaded at some virtual address different from the phsyical address? :o
Yes -- the HigherHalfBareBones tutorial, when I was writing it. It worked for me. <shrug/>
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
Post Reply