[SOLVED] Importing values from LD into C-code

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
wolfram
Posts: 19
Joined: Wed Aug 19, 2009 11:37 am
Location: Saint-Petersburg, Russia

[SOLVED] Importing values from LD into C-code

Post by wolfram »

Hello. I have got some labels in my linker script, for example _sztext = SIZEOF(.text); etc. And I need to use them in my C-code. I know, what I must use keyword "export". But I don't know, what type I have to use? extern int sztext or extern char sztext?
Last edited by wolfram on Fri Aug 21, 2009 8:52 am, edited 1 time in total.
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: Importing values from LD into C-code

Post by f2 »

Try "extern unsigned long _sztext" in your C code.
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
wolfram
Posts: 19
Joined: Wed Aug 19, 2009 11:37 am
Location: Saint-Petersburg, Russia

Re: Importing values from LD into C-code

Post by wolfram »

This post is allowed to delete.
Last edited by wolfram on Fri Aug 21, 2009 8:50 am, edited 1 time in total.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Importing values from LD into C-code

Post by qw »

Symbols defined in linker scripts are not variables. Instead, they appear to the program as addresses. So you should use something similar to

Code: Select all

extern char sztext;
#define SIZEOF_TEXT ((uintptr_t)&sztext)
and then use SIZEOF_TEXT instead of sztext.
wolfram
Posts: 19
Joined: Wed Aug 19, 2009 11:37 am
Location: Saint-Petersburg, Russia

Re: Importing values from LD into C-code

Post by wolfram »

Hobbes thank you.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: [SOLVED] Importing values from LD into C-code

Post by qw »

You're welcome!
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: [SOLVED] Importing values from LD into C-code

Post by qw »

By the way, you can create variables in linker scripts as follows:

Code: Select all

_myvar = .;
LONG(0xdeadbeef)
and access them in C with:

Code: Select all

extern int myvar;
assert(myvar == 0xdeadbeef);
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: [SOLVED] Importing values from LD into C-code

Post by neon »

I personally do not recommend relying on a linker script to define symbols. That creates an unnecessary dependency between your code and that linker. If this is not a concern, then it might be an option. If it is, then I would not recommend it.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
wolfram
Posts: 19
Joined: Wed Aug 19, 2009 11:37 am
Location: Saint-Petersburg, Russia

Re: [SOLVED] Importing values from LD into C-code

Post by wolfram »

neon wrote:I personally do not recommend relying on a linker script to define symbols. That creates an unnecessary dependency between your code and that linker. If this is not a concern, then it might be an option. If it is, then I would not recommend it.
I use symbols from linker script for create Higher Half kernel. I need know, where start my text and data sections for correctly map theirs from additional section, where be my loading code. Have you got another idea, how create HHalf kernel, independent from linker?

P.S. I'm sorry for my English, it's not my native lang.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: [SOLVED] Importing values from LD into C-code

Post by neon »

Hello,
wolfram wrote:I use symbols from linker script for create Higher Half kernel. I need know, where start my text and data sections for correctly map theirs from additional section, where be my loading code. Have you got another idea, how create HHalf kernel, independent from linker?
The way I do it is a bit different sense I have a microkernel. Basically the generic boot loader loads and executes an OS-specific loader that both creates the initial environment for the kernel and kernel modules to run, and build a boot information structure to the kernel before passing control to the kernel.

Granted, my OS-specific loader does a little bit more, but thats the basic idea.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply