link.h revision 1156
1697Spaul/*
21156Sjkh * Copyright (c) 1993 Paul Kranenburg
31156Sjkh * All rights reserved.
41156Sjkh *
51156Sjkh * Redistribution and use in source and binary forms, with or without
61156Sjkh * modification, are permitted provided that the following conditions
71156Sjkh * are met:
81156Sjkh * 1. Redistributions of source code must retain the above copyright
91156Sjkh *    notice, this list of conditions and the following disclaimer.
101156Sjkh * 2. Redistributions in binary form must reproduce the above copyright
111156Sjkh *    notice, this list of conditions and the following disclaimer in the
121156Sjkh *    documentation and/or other materials provided with the distribution.
131156Sjkh * 3. All advertising materials mentioning features or use of this software
141156Sjkh *    must display the following acknowledgement:
151156Sjkh *      This product includes software developed by Paul Kranenburg.
161156Sjkh * 4. The name of the author may not be used to endorse or promote products
171156Sjkh *    derived from this software withough specific prior written permission
181156Sjkh *
191156Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
201156Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
211156Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
221156Sjkh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
231156Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
241156Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251156Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261156Sjkh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271156Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
281156Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291156Sjkh *
301156Sjkh *	$Id$
311156Sjkh */
321156Sjkh
331156Sjkh/*
34697Spaul * RRS section definitions.
35697Spaul *
361156Sjkh * The layout of some data structures defined in this header file is
371156Sjkh * such that we can provide compatibility with the SunOS 4.x shared
381156Sjkh * library scheme.
39697Spaul */
40697Spaul
41697Spaul#ifndef _LINK_H_
42697Spaul#define _LINK_H_
43697Spaul
44697Spaul/*
451156Sjkh * A `Shared Object Descriptor' descibes a shared object that is needed
46697Spaul * to complete the link edit process of the object containing it.
471156Sjkh * A list of such objects (chained through `sod_next') is pointed at
481156Sjkh * by `sdt_sods' in the section_dispatch_table structure.
49697Spaul */
50697Spaul
511156Sjkhstruct sod {	/* Shared Object Descriptor */
521156Sjkh	long	sod_name;		/* name (relative to load address) */
531156Sjkh	u_int	sod_library  : 1,	/* Searched for by library rules */
541156Sjkh		sod_reserved : 31;
551156Sjkh	short	sod_major;		/* major version number */
561156Sjkh	short	sod_minor;		/* minor version number */
571156Sjkh	long	sod_next;		/* next sod */
58697Spaul};
59697Spaul
60697Spaul/*
611156Sjkh * `Shared Object Map's are used by the run-time link editor (ld.so) to
621156Sjkh * keep track of all shared objects loaded into a process' address space.
63697Spaul * These structures are only used at run-time and do not occur within
64697Spaul * the text or data segment of an executable or shared library.
65697Spaul */
661156Sjkhstruct so_map {		/* Shared Object Map */
671156Sjkh	caddr_t		som_addr;	/* Address at which object mapped */
681156Sjkh	char 		*som_path;	/* Path to mmap'ed file */
691156Sjkh	struct so_map	*som_next;	/* Next map in chain */
701156Sjkh	struct sod	*som_sod;	/* Sod responsible for this map */
711156Sjkh	caddr_t		som_sodbase;	/* Base address of this sod */
721156Sjkh	u_int		som_write : 1;	/* Text is currently writable */
731156Sjkh	struct _dynamic	*som_dynamic;	/* _dynamic structure */
741156Sjkh	caddr_t		som_spd;	/* Private data */
75697Spaul};
76697Spaul
77697Spaul/*
78697Spaul * Symbol description with size. This is simply an `nlist' with
79697Spaul * one field (nz_size) added.
80697Spaul * Used to convey size information on items in the data segment
81697Spaul * of shared objects. An array of these live in the shared object's
821156Sjkh * text segment and is addressed by the `sdt_nzlist' field.
83697Spaul */
84697Spaulstruct nzlist {
85697Spaul	struct nlist	nlist;
86697Spaul	u_long		nz_size;
87697Spaul#define nz_un		nlist.n_un
88697Spaul#define nz_strx		nlist.n_un.n_strx
89697Spaul#define nz_name		nlist.n_un.n_name
90697Spaul#define nz_type		nlist.n_type
91697Spaul#define nz_value	nlist.n_value
92697Spaul#define nz_desc		nlist.n_desc
93697Spaul#define nz_other	nlist.n_other
94697Spaul};
95697Spaul
961156Sjkh#define N_AUX(p)	((p)->n_other & 0xf)
971156Sjkh#define N_RESERVED(p)	(((unsigned int)(p)->n_other >> 4) & 0xf)
981156Sjkh#define N_OTHER(r, v)	(((unsigned int)(r) << 4) | ((v) & 0xf))
991156Sjkh
1001156Sjkh#define AUX_OBJECT	1
1011156Sjkh#define AUX_FUNC	2
1021156Sjkh
1031156Sjkh
104697Spaul/*
1051156Sjkh * The `section_dispatch_table' structure contains offsets to various data
106697Spaul * structures needed to do run-time relocation.
107697Spaul */
1081156Sjkhstruct section_dispatch_table {
1091156Sjkh	struct so_map *sdt_loaded;	/* List of loaded objects */
1101156Sjkh	long	sdt_sods;		/* List of shared objects descriptors */
1111156Sjkh	long	sdt_filler1;		/* Unused (was: search rules) */
1121156Sjkh	long	sdt_got;		/* Global offset table */
1131156Sjkh	long	sdt_plt;		/* Procedure linkage table */
1141156Sjkh	long	sdt_rel;		/* Relocation table */
1151156Sjkh	long	sdt_hash;		/* Symbol hash table */
1161156Sjkh	long	sdt_nzlist;		/* Symbol table itself */
1171156Sjkh	long	sdt_filler2;		/* Unused (was: stab_hash) */
1181156Sjkh	long	sdt_buckets;		/* Number of hash buckets */
1191156Sjkh	long	sdt_strings;		/* Symbol strings */
1201156Sjkh	long	sdt_str_sz;		/* Size of symbol strings */
1211156Sjkh	long	sdt_text_sz;		/* Size of text area */
1221156Sjkh	long	sdt_plt_sz;		/* Size of procedure linkage table */
123697Spaul};
124697Spaul
125697Spaul/*
1261156Sjkh * RRS symbol hash table, addressed by `sdt_hash' in section_dispatch_table.
127697Spaul * Used to quickly lookup symbols of the shared object by hashing
128697Spaul * on the symbol's name. `rh_symbolnum' is the index of the symbol
1291156Sjkh * in the shared object's symbol list (`sdt_nzlist'), `rh_next' is
130697Spaul * the next symbol in the hash bucket (in case of collisions).
131697Spaul */
132697Spaulstruct rrs_hash {
1331156Sjkh	int	rh_symbolnum;		/* Symbol number */
1341156Sjkh	int	rh_next;		/* Next hash entry */
135697Spaul};
136697Spaul
137697Spaul/*
138697Spaul * `rt_symbols' is used to keep track of run-time allocated commons
139697Spaul * and data items copied from shared objects.
140697Spaul */
141697Spaulstruct rt_symbol {
1421156Sjkh	struct nzlist		*rt_sp;		/* The symbol */
1431156Sjkh	struct rt_symbol	*rt_next;	/* Next in linear list */
1441156Sjkh	struct rt_symbol	*rt_link;	/* Next in bucket */
1451156Sjkh	caddr_t			rt_srcaddr;	/* Address of "master" copy */
1461156Sjkh	struct so_map		*rt_smp;	/* Originating map */
147697Spaul};
148697Spaul
149697Spaul/*
150697Spaul * Debugger interface structure.
151697Spaul */
1521156Sjkhstruct so_debug {
1531156Sjkh	int	dd_version;		/* Version # of interface */
1541156Sjkh	int	dd_in_debugger;		/* Set when run by debugger */
1551156Sjkh	int	dd_sym_loaded;		/* Run-time linking brought more
1561156Sjkh					   symbols into scope */
1571156Sjkh	char   	 *dd_bpt_addr;		/* Address of rtld-generated bpt */
1581156Sjkh	int	dd_bpt_shadow;		/* Original contents of bpt */
1591156Sjkh	struct rt_symbol *dd_cc;	/* Allocated commons/copied data */
160697Spaul};
161697Spaul
162697Spaul/*
163697Spaul * Entry points into ld.so - user interface to the run-time linker.
164697Spaul */
165697Spaulstruct ld_entry {
1661156Sjkh	void	*(*dlopen) __P((char *, int));
1671156Sjkh	int	(*dlclose) __P((void *));
1681156Sjkh	void	*(*dlsym) __P((void *, char *));
1691156Sjkh	int	(*dlctl) __P((void *, int, void *));
170697Spaul};
171697Spaul
172697Spaul/*
1731156Sjkh * dlctl() commands
1741156Sjkh */
1751156Sjkh#define DL_GETERRNO	1
1761156Sjkh
1771156Sjkh/*
1781156Sjkh * dl*() prototypes.
1791156Sjkh */
1801156Sjkhextern void	*dlopen __P((char *, int));
1811156Sjkhextern int	dlclose __P((void *));
1821156Sjkhextern void	*dlsym __P((void *, char *));
1831156Sjkhextern int	dlctl __P((void *, int, void *));
1841156Sjkh
1851156Sjkh
1861156Sjkh/*
187697Spaul * This is the structure pointed at by the __DYNAMIC symbol if an
188697Spaul * executable requires the attention of the run-time link editor.
189697Spaul * __DYNAMIC is given the value zero if no run-time linking needs to
190697Spaul * be done (it is always present in shared objects).
1911156Sjkh * The union `d_un' provides for different versions of the dynamic
1921156Sjkh * linking mechanism (switched on by `d_version'). The last version
193697Spaul * used by Sun is 3. We leave some room here and go to version number
194697Spaul * 8 for NetBSD, the main difference lying in the support for the
195697Spaul * `nz_list' type of symbols.
196697Spaul */
197697Spaul
1981156Sjkhstruct	_dynamic {
1991156Sjkh	int		d_version;	/* version # of this interface */
2001156Sjkh	struct so_debug	*d_debug;
201697Spaul	union {
2021156Sjkh		struct section_dispatch_table *d_sdt;
2031156Sjkh	} d_un;
2041156Sjkh	struct ld_entry *d_entry;
205697Spaul};
206697Spaul
207697Spaul#define LD_VERSION_SUN		(3)
208697Spaul#define LD_VERSION_BSD		(8)
209697Spaul#define LD_VERSION_NZLIST_P(v)	((v) >= 8)
210697Spaul
2111156Sjkh#define LD_GOT(x)	((x)->d_un.d_sdt->sdt_got)
2121156Sjkh#define LD_PLT(x)	((x)->d_un.d_sdt->sdt_plt)
2131156Sjkh#define LD_REL(x)	((x)->d_un.d_sdt->sdt_rel)
2141156Sjkh#define LD_SYMBOL(x)	((x)->d_un.d_sdt->sdt_nzlist)
2151156Sjkh#define LD_HASH(x)	((x)->d_un.d_sdt->sdt_hash)
2161156Sjkh#define LD_STRINGS(x)	((x)->d_un.d_sdt->sdt_strings)
2171156Sjkh#define LD_NEED(x)	((x)->d_un.d_sdt->sdt_sods)
2181156Sjkh#define LD_BUCKETS(x)	((x)->d_un.d_sdt->sdt_buckets)
219697Spaul
2201156Sjkh#define LD_GOTSZ(x)	((x)->d_un.d_sdt->sdt_plt - (x)->d_un.d_sdt->sdt_got)
2211156Sjkh#define LD_RELSZ(x)	((x)->d_un.d_sdt->sdt_hash - (x)->d_un.d_sdt->sdt_rel)
2221156Sjkh#define LD_HASHSZ(x)	((x)->d_un.d_sdt->sdt_nzlist - (x)->d_un.d_sdt->sdt_hash)
2231156Sjkh#define LD_STABSZ(x)	((x)->d_un.d_sdt->sdt_strings - (x)->d_un.d_sdt->sdt_nzlist)
2241156Sjkh#define LD_PLTSZ(x)	((x)->d_un.d_sdt->sdt_plt_sz)
2251156Sjkh#define LD_STRSZ(x)	((x)->d_un.d_sdt->sdt_str_sz)
2261156Sjkh#define LD_TEXTSZ(x)	((x)->d_un.d_sdt->sdt_text_sz)
227697Spaul
228697Spaul/*
2291156Sjkh * Interface to ld.so
230697Spaul */
231697Spaulstruct crt_ldso {
232697Spaul	int		crt_ba;		/* Base address of ld.so */
233697Spaul	int		crt_dzfd;	/* "/dev/zero" file decriptor (SunOS) */
234697Spaul	int		crt_ldfd;	/* ld.so file descriptor */
2351156Sjkh	struct _dynamic	*crt_dp;	/* Main's __DYNAMIC */
236697Spaul	char		**crt_ep;	/* environment strings */
237697Spaul	caddr_t		crt_bp;		/* Breakpoint if run from debugger */
2381156Sjkh	char		*crt_prog;	/* Program name */
239697Spaul};
240697Spaul
241697Spaul/*
242697Spaul * Version passed from crt0 to ld.so (1st argument to _rtld()).
243697Spaul */
244697Spaul#define CRT_VERSION_SUN		1
245697Spaul#define CRT_VERSION_BSD		2
2461156Sjkh#define CRT_VERSION_BSD_2	2
2471156Sjkh#define CRT_VERSION_BSD_3	3
248697Spaul
249697Spaul
250697Spaul/*
251697Spaul * Maximum number of recognized shared object version numbers.
252697Spaul */
253697Spaul#define MAXDEWEY	8
254697Spaul
255697Spaul/*
256697Spaul * Header of the hints file.
257697Spaul */
258697Spaulstruct hints_header {
259697Spaul	long		hh_magic;
260697Spaul#define HH_MAGIC	011421044151
261697Spaul	long		hh_version;	/* Interface version number */
262697Spaul#define LD_HINTS_VERSION_1	1
263697Spaul	long		hh_hashtab;	/* Location of hash table */
264697Spaul	long		hh_nbucket;	/* Number of buckets in hashtab */
265697Spaul	long		hh_strtab;	/* Location of strings */
266697Spaul	long		hh_strtab_sz;	/* Size of strings */
267697Spaul	long		hh_ehints;	/* End of hints (max offset in file) */
268697Spaul};
269697Spaul
270697Spaul#define HH_BADMAG(hdr)	((hdr).hh_magic != HH_MAGIC)
271697Spaul
272697Spaul/*
273697Spaul * Hash table element in hints file.
274697Spaul */
275697Spaulstruct hints_bucket {
276697Spaul	/* namex and pathx are indices into the string table */
277697Spaul	int		hi_namex;		/* Library name */
278697Spaul	int		hi_pathx;		/* Full path */
279697Spaul	int		hi_dewey[MAXDEWEY];	/* The versions */
280697Spaul	int		hi_ndewey;		/* Number of version numbers */
281697Spaul#define hi_major hi_dewey[0]
282697Spaul#define hi_minor hi_dewey[1]
283697Spaul	int		hi_next;		/* Next in this bucket */
284697Spaul};
285697Spaul
286697Spaul#define _PATH_LD_HINTS		"/var/run/ld.so.hints"
287697Spaul
288697Spaul#endif /* _LINK_H_ */
289697Spaul
290