[SOLVED] Importing values from LD into C-code
[SOLVED] Importing values from LD into C-code
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.
Re: Importing values from LD into C-code
Try "extern unsigned long _sztext" in your C code.
"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
Re: Importing values from LD into C-code
This post is allowed to delete.
Last edited by wolfram on Fri Aug 21, 2009 8:50 am, edited 1 time in total.
Re: Importing values from LD into C-code
Symbols defined in linker scripts are not variables. Instead, they appear to the program as addresses. So you should use something similar toand then use SIZEOF_TEXT instead of sztext.
Code: Select all
extern char sztext;
#define SIZEOF_TEXT ((uintptr_t)&sztext)
Re: Importing values from LD into C-code
Hobbes thank you.
Re: [SOLVED] Importing values from LD into C-code
You're welcome!
Re: [SOLVED] Importing values from LD into C-code
By the way, you can create variables in linker scripts as follows:and access them in C with:
Code: Select all
_myvar = .;
LONG(0xdeadbeef)
Code: Select all
extern int myvar;
assert(myvar == 0xdeadbeef);
Re: [SOLVED] Importing values from LD into C-code
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();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Re: [SOLVED] Importing values from LD into C-code
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?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.
P.S. I'm sorry for my English, it's not my native lang.
Re: [SOLVED] Importing values from LD into C-code
Hello,
Granted, my OS-specific loader does a little bit more, but thats the basic idea.
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.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?
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();}
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}