OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:12 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Solved] Linking problems
PostPosted: Sat Jun 17, 2017 8:45 am 
Offline
User avatar

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

Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 8:49 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
My first thought would be that the function you are using to print those values isn't working correctly.


Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 8:56 am 
Offline
User avatar

Joined: Fri May 26, 2017 4:12 pm
Posts: 22
Well you can certainly have a look at it, but I use it a lot and have never had trouble with it:

Code:
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);
  }
}


Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 8:57 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
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


Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 9:41 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 9:41 am 
Offline
Member
Member
User avatar

Joined: Sat Dec 17, 2016 6:58 am
Posts: 101
Location: The Internet
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:
while ( ! ( succeed = try() ) );


Top
 Profile  
 
 Post subject: Re: Linking problems (I think)
PostPosted: Sat Jun 17, 2017 9:54 am 
Offline
User avatar

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


Top
 Profile  
 
 Post subject: Re: [Solved] Linking problems
PostPosted: Sat Jun 17, 2017 2:37 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
https://www.embeddedrelated.com/showarticle/900.php


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot], Google [Bot] and 77 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group