Subject: Re: _KERNEL cpp symbol in kernel source
To: None <nigel@ind.tansu.com.au>
From: Bill Studenmund <wrstuden@nas.nasa.gov>
List: port-mac68k
Date: 07/12/1999 13:20:42
On Tue, 13 Jul 1999 nigel@ind.tansu.com.au wrote:

> 
> 	OK. For those that don't remember, I am trying to use the
> kernel source code, _unmodified_, in the Booter.
> 
> 	I have got the ext2fs, ffs, mfs & ufs source code to compile
> under Metrowerks Codewarrior 11 on the Mac. In the end, I only had to
> modify sys/syscallargs.h, and add a header file (about 20 lines) as a
> prefix. (At the moment, though, the lfs code is just too messy to use)
> 
> 
> 	Now, to get a lot of it to compile, I had to #define _KERNEL
> in my prefix file. This seemed pretty reasonable to me since this
> source came from the kernel tarball. The problem is that when I link
> against this code, many kernel globals are defined in each .o file.
> 
> e.g. kernel_pmap_store, kmem_object and kernel_object are defined in
>      both ext2fs_balloc.c and ext2fs_alloc.c
> 
> 
> 1) Can someone who is familiar with the kernel source work out
>    how this code is normally compiled when a kernel is build ?
>    I cannot work out how it would actually compile, because at
>    link time the same thing would happen.

Let me see if I can explain things coherently.

In traditional unix executables, there are two types of data. Ones
initialized to a specific value which go in the .data section, and ones
which are implicitly initialized to zero, which go into the .bss section.
The .bss section isn't really in the executable, it's just a chunk of
memory which is zeroed on exec.

The lines you're talking about are:

struct pmap kernel_pmap_store;   etc. i.e. struct foo bar.

As they don't initialize bar to a specific value, they go in .bss.

If one had been something like 'struct foo bar = {"zippy", "zappy",
"zoo"}', as it sets a specific value, it'd go into .data.

What the gnu linker will do is merge multiple "identical" .bss symbols
together, and will merge them with one .data symbol. 

So 100 files can say "struct foo bar" and you'll get exactly one struct
foo bar in the output binary.

It's kinda like saying "extern struct foo bar" everywhere but without
having to remember to say "struct foo bar" explicitly in one file.

Does that help? I'm not sure how to get Metrowerks to do this... Are there
linker options?


> 2) What is the _KERNEL symbol meant to denote ?
>    When is & isn't it used ?

It says you're including the file into a program file which is going into
the kernel. So all the kernel-specific stuff gets turned on, and userland
specific stuff gets turned off.

Like in /sys/sys/fcntl.h. Defining _KERNEL turns on all the
kernel-internal flag defentitions, and turns OFF the defenition of the
for-userland syscall prototypes.

You probably also want to define _LKM so none of the opt_foo.h files get
included.

Take care,

Bill