[Solved] Linking problems

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.
Post Reply
User avatar
BluCode
Posts: 22
Joined: Fri May 26, 2017 4:12 pm

[Solved] Linking problems

Post by BluCode »

I am trying to implement a kernel heap, and in doing so I have noticed that something seems 'messed up' with my linking. Here is my link.ld:

Code: Select all

ENTRY(start)
SECTIONS
{
  . = 0x100000;

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

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

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

  end = .; _end = .; __end = .;
}
The issue is that when I print the values of code, data, bss and end in my kernel they don't match. According to my kernel:

Code: Select all

code: 0x1badboo2
data: 0x00105ca0
bss:  0x00000000
end:  0x3a434347
To me end seems abnormally high and they don't seem aligned even though I align them in the linker script
I am using grub-mkrescue to make an ISO file which I am running with QEMU, so maybe that is causing some trouble. My ld command is:

ld -T link.ld -m elf_i386 -o kernel/kernel.elf boot/grubboot.o ${OBJC} ${OBJA}

Any thoughts?
Last edited by BluCode on Sat Jun 17, 2017 9:55 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Linking problems (I think)

Post by iansjack »

My first thought would be that the function you are using to print those values isn't working correctly.
User avatar
BluCode
Posts: 22
Joined: Fri May 26, 2017 4:12 pm

Re: Linking problems (I think)

Post by BluCode »

Well you can certainly have a look at it, but I use it a lot and have never had trouble with it:

Code: Select all

void print_hex(u32int n) {
  s32int tmp;
  print("0x");
  int i;
  for (i = 28; i > 0; i -= 4)
  {
    tmp = (n >> i) & 0xF;
    if (tmp >= 0xA) {
      _print_char(tmp-0xA+'a', -1, -1, WHITE_ON_BLACK);
    } else {
      _print_char( tmp+'0', -1, -1, WHITE_ON_BLACK);
    }
  }
  tmp = n & 0xF;
  if (tmp >= 0xA) {
    _print_char(tmp-0xA+'a', -1, -1, WHITE_ON_BLACK);
  } else {
    _print_char( tmp+'0', -1, -1, WHITE_ON_BLACK);
  }
}
LtG
Member
Member
Posts: 384
Joined: Thu Aug 13, 2015 4:57 pm

Re: Linking problems (I think)

Post by LtG »

As iansjack said, you're probably doing it wrong. Are you trying to use the linker script _symbol_ as a variable and expecting said _variable_ to hold the address? Try taking the _address_ of the symbol..

And here's something in the wiki:
http://wiki.osdev.org/Using_Linker_Script_Values
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Linking problems (I think)

Post by iansjack »

A very simple way to check is to ask the linker to produce a map file and look at where it has placed the various sections.
User avatar
MajickTek
Member
Member
Posts: 101
Joined: Sat Dec 17, 2016 6:58 am
Libera.chat IRC: MajickTek
Location: The Internet
Contact:

Re: Linking problems (I think)

Post by MajickTek »

Here's the thing: it looks like you are invoking ld manually (?)

GCC (or whatever compiler suite you use) should do the linking automatically. The wiki has basic instructions (bare bones) on how to use a linker script.

Sorry if I misunderstood you :D
Everyone should know how to program a computer, because it teaches you how to think! -Steve Jobs

Code: Select all

while ( ! ( succeed = try() ) ); 
User avatar
BluCode
Posts: 22
Joined: Fri May 26, 2017 4:12 pm

Re: Linking problems (I think)

Post by BluCode »

LtG wrote:As iansjack said, you're probably doing it wrong. Are you trying to use the linker script _symbol_ as a variable and expecting said _variable_ to hold the address? Try taking the _address_ of the symbol..

And here's something in the wiki:
http://wiki.osdev.org/Using_Linker_Script_Values
Thank you! That fixed the problem perfectly!
iansjack wrote:A very simple way to check is to ask the linker to produce a map file and look at where it has placed the various sections.
Can you elaborate on how I can do that? That sounds useful in the future.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: [Solved] Linking problems

Post by iansjack »

Post Reply