Page 1 of 1
[SOLVED] Importing values from LD into C-code
Posted: Fri Aug 21, 2009 4:26 am
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?
Re: Importing values from LD into C-code
Posted: Fri Aug 21, 2009 4:27 am
by f2
Try "extern unsigned long _sztext" in your C code.
Re: Importing values from LD into C-code
Posted: Fri Aug 21, 2009 4:40 am
by wolfram
This post is allowed to delete.
Re: Importing values from LD into C-code
Posted: Fri Aug 21, 2009 5:25 am
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.
Re: Importing values from LD into C-code
Posted: Fri Aug 21, 2009 8:51 am
by wolfram
Hobbes thank you.
Re: [SOLVED] Importing values from LD into C-code
Posted: Mon Aug 24, 2009 2:06 am
by qw
You're welcome!
Re: [SOLVED] Importing values from LD into C-code
Posted: Mon Aug 24, 2009 3:27 am
by qw
By the way, you
can create variables in linker scripts as follows:
and access them in C with:
Code: Select all
extern int myvar;
assert(myvar == 0xdeadbeef);
Re: [SOLVED] Importing values from LD into C-code
Posted: Mon Aug 24, 2009 7:08 am
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.
Re: [SOLVED] Importing values from LD into C-code
Posted: Mon Aug 24, 2009 10:15 am
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.
Re: [SOLVED] Importing values from LD into C-code
Posted: Mon Aug 24, 2009 10:40 am
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.