Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Aug 1999 16:51:46 -0700 (PDT)
From:      John Polstra <jdp@polstra.com>
To:        b84118@ee.ntu.edu.tw
Cc:        hackers@freebsd.org
Subject:   Re: How the `struct linker_set' is used in building an ELF kernel?
Message-ID:  <199908052351.QAA05065@vashon.polstra.com>
In-Reply-To: <87n1wag14v.fsf@JoeLu.m8.ntu.edu.tw>

next in thread | previous in thread | raw e-mail | index | archive | help
In article <87n1wag14v.fsf@JoeLu.m8.ntu.edu.tw>,
Joe Jih-Shien Lu  <b84118@ee.ntu.edu.tw> wrote:
> I started studying 3.2-stable kernel source for days. There are
> some questions I cannot figure out in an ordinary C programmer's
> point of view:
> 
> 	* In cninit(), it references a global variable `cons_set' of
> 	  the type `struct linker_set,' but I don't see its definition
> 	  in any of the source files except the setdef0.c generated by
> 	  /usr/bin/gensetdefs. It is defined by the .long asm psuedo-op,
> 	  and seems to have the size of 4 bytes. However, in
> 	  /sys/i386/i386/cons.h, it is declared as of the type `struct
> 	  linker_set' which is 8-byte long. This inconsistency confused
> 	  me.
> 
> 	* Similar problem is encountered when I'm poking around the
> 	  system initializing for-loop in main().  sysinit_set, declared
> 	  as struct linker_set, is referenced, but I can't get into the
> 	  way how this variable is initialized.
> 
> I guess it is the linker who did all the magic, since the comment in
> /sys/sys/linker_set.h mentioned about it. After studying the linker
> script (/sys/i386/conf/kernel.script) and ld.info, though, I still
> don't have any idea about the details behind the scene.

Linker sets are basically arrays of pointers that are constructed by
the linker using values that can come from many object files.  The
first word contains the number of elements (pointers) in the set.
Then come the pointers themselves.  Finally, a NULL pointer appears
at the end of the set.

The old a.out linker supported linker sets directly, and did all the
bookkeeping necessary to keep track of the set sizes and so forth.
The ELF linker does not directly support linker sets.  However, it
does support an arbitrary number of independent sections.  Using that
along with a little help from gensetdefs, we can get a.out-style
linker sets using the ELF tools.

setdef0.o must appear first on the linker command line.  It
contributes the leading count word to each set.  Then come the
"normal" object files, which may contribute pointers to various
sets.  These are just appended to special sections, one per linker
set.  Finally comes setdef1.o, which emits the terminating NULL
pointers.

> I notice that gensetdefs looks for the sections by the `.set.'-prefixed
> name in all the ELF kernel object files, and produces the setdef[12].c
> accordingly. Does the `.set.'-prefixed section name have any special
> meaning in an ELF object file?

No, it is just a convention we use for naming the sections that
contain linker sets.  gensetdefs knows this convention, and so do the
macros in <sys/linker_set.h>.  The compiler, assembler, and linker
aren't aware of anything special about the names.

John
-- 
  John Polstra                                               jdp@polstra.com
  John D. Polstra & Co., Inc.                        Seattle, Washington USA
  "No matter how cynical I get, I just can't keep up."        -- Nora Ephron


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199908052351.QAA05065>