From owner-freebsd-hackers Thu Jan 28 10:09:20 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id KAA20412 for freebsd-hackers-outgoing; Thu, 28 Jan 1999 10:09:20 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from wall.polstra.com (rtrwan160.accessone.com [206.213.115.74]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id KAA20407 for ; Thu, 28 Jan 1999 10:09:18 -0800 (PST) (envelope-from jdp@polstra.com) Received: from vashon.polstra.com (vashon.polstra.com [206.213.73.13]) by wall.polstra.com (8.9.1/8.9.1) with ESMTP id KAA07571; Thu, 28 Jan 1999 10:09:07 -0800 (PST) (envelope-from jdp@polstra.com) From: John Polstra Received: (from jdp@localhost) by vashon.polstra.com (8.9.1/8.9.1) id KAA11835; Thu, 28 Jan 1999 10:09:07 -0800 (PST) (envelope-from jdp@polstra.com) Date: Thu, 28 Jan 1999 10:09:07 -0800 (PST) Message-Id: <199901281809.KAA11835@vashon.polstra.com> To: wilson@ensim.com Subject: Re: need help with linker_set In-Reply-To: <36AC3072.E5EEF339@ensim.com> Organization: Polstra & Co., Seattle, WA Cc: hackers@FreeBSD.ORG Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG In article <36AC3072.E5EEF339@ensim.com>, Xun Wilson Huang wrote: > Hi, > > I have been reading codes from the FreeBSD kernel and discover this > neat thing "linker_set". As I understand, linker_set allows different > modules/subsystems/files to put data into a "set" by inserting an > assembly directive .stabs in the c code. At the linking stage, the > linker puts all these set elements into an array, and one can write code > to enumerate this array of set elements. ( Am I getting this right?) Yes, exactly right. You end up with a NULL-terminated vector of addresses, immediately preceded by an int that gives the number of elements in the set. We also support this for ELF, using a somewhat different technique. > I have been digging for something similar to this on other platform and > sadly, I can't find anything. I am wondering if any BSD guru here can > point me to something that leads to a more system independent version of > this trick? thanks a million. The most portable way to do it would be to use C++ and include global constructors which at start-up time would register each module into a data structure representing the set. You wouldn't need to write all the code in C++, just the tiny portion that contains the constructor. The rest of the code could still be in C. We used something similar to this in "src/lib/libc_r/uthread/uthread_autoinit.cc". If you wanted to use only C code, you could use GCC's "constructor" extension. See the section "Declaring Attributes of Functions" section of the gcc info pages. I don't know of a portable way to get what you want using strictly ANSI/ISO C only. John -- John Polstra jdp@polstra.com John D. Polstra & Co., Inc. Seattle, Washington USA "Nobody ever went broke underestimating the taste of the American public." -- H. L. Mencken To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message