From owner-freebsd-hackers Thu Aug 5 18:58:11 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from joelu.m8.ntu.edu.tw (JoeLu.m8.ntu.edu.tw [140.112.253.138]) by hub.freebsd.org (Postfix) with SMTP id 6427014D5B for ; Thu, 5 Aug 1999 18:55:57 -0700 (PDT) (envelope-from jdp@polstra.com) Received: (qmail 46190 invoked by uid 1000); 6 Aug 1999 00:16:25 -0000 Delivered-To: JoeLu@JoeLu.m8.ntu.edu.tw Received: (qmail 46131 invoked from network); 6 Aug 1999 00:03:22 -0000 Received: from mail.ee.ntu.edu.tw (@140.112.19.73) by joelu.m8.ntu.edu.tw with SMTP; 6 Aug 1999 00:03:22 -0000 Received: from wall.polstra.com (rtrwan160.accessone.com [206.213.115.74]) by mail.ee.ntu.edu.tw (8.9.3/8.9.1) with ESMTP id HAA16200 for ; Fri, 6 Aug 1999 07:52:00 +0800 (CST) Received: from vashon.polstra.com (vashon.polstra.com [206.213.73.13]) by wall.polstra.com (8.9.3/8.9.1) with ESMTP id QAA26468; Thu, 5 Aug 1999 16:51:46 -0700 (PDT) (envelope-from jdp@polstra.com) From: John Polstra Received: (from jdp@localhost) by vashon.polstra.com (8.9.3/8.9.1) id QAA05065; Thu, 5 Aug 1999 16:51:46 -0700 (PDT) (envelope-from jdp@polstra.com) Date: Thu, 5 Aug 1999 16:51:46 -0700 (PDT) Message-Id: <199908052351.QAA05065@vashon.polstra.com> To: b84118@ee.ntu.edu.tw Subject: Re: How the `struct linker_set' is used in building an ELF kernel? In-Reply-To: <87n1wag14v.fsf@JoeLu.m8.ntu.edu.tw> Organization: Polstra & Co., Seattle, WA Cc: hackers@freebsd.org Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In article <87n1wag14v.fsf@JoeLu.m8.ntu.edu.tw>, Joe Jih-Shien Lu 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 . 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