Library Calls: Difference between revisions
Jump to navigation
Jump to search
Markup fixes, categorisation, removed PFR
[unchecked revision] | [unchecked revision] |
No edit summary |
(Markup fixes, categorisation, removed PFR) |
||
Line 1:
== Introduction ==
Line 31 ⟶ 29:
=== Linker ===
The '''linker''' is a different story. When the compiler is done compiling your source file into object files, it is up to the linker to '''link''' all object files together into an executable. It also '''resolves symbols'''. For the above code, the object code refers to a function
It is defined in the '''system libraries'''. Luckily the linker knows that these exist, and where to find them. Any symbols not resolved in your own code it tries to resolve using the system libraries. It also links in the '''startup code''', since someone has to set up the environment and call your ''int main()'', right?
===
If you did follow the [[GCC Cross-Compiler]] how-to, your cross-compiler does not know about ''<stdio.h>'', and your cross-linker does not know about any system libraries. Which is just as well, since they don't exist.
Line 45 ⟶ 43:
=== Userspace ''printf()'' ===
Well, the usual userspace process for a library call is already explained above, is it? Well, yes, but only at a high level. What actually '''happens''' when you call
Your userland
'''Then''' comes the funny part. The function that does the actual '''printing''' (see "At the Bottom" above) is in '''kernel space'''. You can't just execute kernel functions like that, only the kernel can.
So let's say your standard library implementation of
=== Enter the kernel ===
An interrupt ends userspace processing, and wakes up the kernel - more specifically, the interrupt handler registered by the kernel. Since it was the interrupt reserved for '''system calls''', it knows what to do: The special code tells it that it was
So the interrupt handler passes the ''char *'' to the printing function we mentioned in "At the Bottom" above, then returns control to the caller.
Line 63 ⟶ 61:
=== Done ===
The interrupt handler ends,
=== Kernel is Different ===
If you are in kernel space anyway, you don't want that hassle with the registers and the interrupt. After all, you '''can''' call the print function directly as you '''are''' in kernel space. You also never want to write to a file. The
'''And''' you probably don't want all the code necessary for e.g. floating point conversions or unicode output linked into your kernel binary, either. The userspace
So what do you do? The answer is easy. Just tell the linker that the _system libraries_ are to be found in a different directory than for userspace. (Your ''Makefile'' is already loaded with funny options for compiler and linker, one more wouldn't make a difference. Bad pun warning.)
So you provide a lightweight
<pre>
Line 82 ⟶ 80:
#endif
</pre>
[[Category:FAQ]]
|