link.h revision 9335
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 *
309335Sdfr *	$Id: link.h,v 1.4 1995/03/04 17:49:20 nate Exp $
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)
976887Snate#define N_BIND(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
1026887Snate/*#define BIND_LOCAL	0	not used */
1036887Snate/*#define BIND_GLOBAL	1	not used */
1046887Snate#define BIND_WEAK	2
1051156Sjkh
1061156Sjkh
107697Spaul/*
1081156Sjkh * The `section_dispatch_table' structure contains offsets to various data
109697Spaul * structures needed to do run-time relocation.
110697Spaul */
1111156Sjkhstruct section_dispatch_table {
1121156Sjkh	struct so_map *sdt_loaded;	/* List of loaded objects */
1131156Sjkh	long	sdt_sods;		/* List of shared objects descriptors */
1141156Sjkh	long	sdt_filler1;		/* Unused (was: search rules) */
1151156Sjkh	long	sdt_got;		/* Global offset table */
1161156Sjkh	long	sdt_plt;		/* Procedure linkage table */
1171156Sjkh	long	sdt_rel;		/* Relocation table */
1181156Sjkh	long	sdt_hash;		/* Symbol hash table */
1191156Sjkh	long	sdt_nzlist;		/* Symbol table itself */
1201156Sjkh	long	sdt_filler2;		/* Unused (was: stab_hash) */
1211156Sjkh	long	sdt_buckets;		/* Number of hash buckets */
1221156Sjkh	long	sdt_strings;		/* Symbol strings */
1231156Sjkh	long	sdt_str_sz;		/* Size of symbol strings */
1241156Sjkh	long	sdt_text_sz;		/* Size of text area */
1251156Sjkh	long	sdt_plt_sz;		/* Size of procedure linkage table */
126697Spaul};
127697Spaul
128697Spaul/*
1291156Sjkh * RRS symbol hash table, addressed by `sdt_hash' in section_dispatch_table.
130697Spaul * Used to quickly lookup symbols of the shared object by hashing
131697Spaul * on the symbol's name. `rh_symbolnum' is the index of the symbol
1321156Sjkh * in the shared object's symbol list (`sdt_nzlist'), `rh_next' is
133697Spaul * the next symbol in the hash bucket (in case of collisions).
134697Spaul */
135697Spaulstruct rrs_hash {
1361156Sjkh	int	rh_symbolnum;		/* Symbol number */
1371156Sjkh	int	rh_next;		/* Next hash entry */
138697Spaul};
139697Spaul
140697Spaul/*
141697Spaul * `rt_symbols' is used to keep track of run-time allocated commons
142697Spaul * and data items copied from shared objects.
143697Spaul */
144697Spaulstruct rt_symbol {
1451156Sjkh	struct nzlist		*rt_sp;		/* The symbol */
1461156Sjkh	struct rt_symbol	*rt_next;	/* Next in linear list */
1471156Sjkh	struct rt_symbol	*rt_link;	/* Next in bucket */
1481156Sjkh	caddr_t			rt_srcaddr;	/* Address of "master" copy */
1491156Sjkh	struct so_map		*rt_smp;	/* Originating map */
150697Spaul};
151697Spaul
152697Spaul/*
153697Spaul * Debugger interface structure.
154697Spaul */
1551156Sjkhstruct so_debug {
1561156Sjkh	int	dd_version;		/* Version # of interface */
1571156Sjkh	int	dd_in_debugger;		/* Set when run by debugger */
1581156Sjkh	int	dd_sym_loaded;		/* Run-time linking brought more
1591156Sjkh					   symbols into scope */
1601156Sjkh	char   	 *dd_bpt_addr;		/* Address of rtld-generated bpt */
1611156Sjkh	int	dd_bpt_shadow;		/* Original contents of bpt */
1621156Sjkh	struct rt_symbol *dd_cc;	/* Allocated commons/copied data */
163697Spaul};
164697Spaul
165697Spaul/*
1669335Sdfr * Version returned to crt0 from ld.so
1679335Sdfr */
1689335Sdfr#define LDSO_VERSION_NONE	0	/* FreeBSD2.0, 2.0.5 */
1699335Sdfr#define LDSO_VERSION_HAS_DLEXIT	1	/* includes dlexit in ld_entry */
1709335Sdfr
1719335Sdfr/*
172697Spaul * Entry points into ld.so - user interface to the run-time linker.
1739335Sdfr * Entries are valid for the given version numbers returned by ld.so
1749335Sdfr * to crt0.
175697Spaul */
176697Spaulstruct ld_entry {
1779335Sdfr	void	*(*dlopen) __P((char *, int));		/* NONE */
1789335Sdfr	int	(*dlclose) __P((void *));		/* NONE */
1799335Sdfr	void	*(*dlsym) __P((void *, char *));	/* NONE */
1809335Sdfr	char	*(*dlerror) __P((void));		/* NONE */
1819335Sdfr	void	(*dlexit) __P((void));			/* HAS_DLEXIT */
182697Spaul};
183697Spaul
184697Spaul/*
1851156Sjkh * dl*() prototypes.
1861156Sjkh */
1871156Sjkhextern void	*dlopen __P((char *, int));
1881156Sjkhextern int	dlclose __P((void *));
1891156Sjkhextern void	*dlsym __P((void *, char *));
1906232Sjkhextern char	*dlerror __P((void));
1911156Sjkh
1921156Sjkh
1931156Sjkh/*
194697Spaul * This is the structure pointed at by the __DYNAMIC symbol if an
195697Spaul * executable requires the attention of the run-time link editor.
196697Spaul * __DYNAMIC is given the value zero if no run-time linking needs to
197697Spaul * be done (it is always present in shared objects).
1981156Sjkh * The union `d_un' provides for different versions of the dynamic
1991156Sjkh * linking mechanism (switched on by `d_version'). The last version
200697Spaul * used by Sun is 3. We leave some room here and go to version number
201697Spaul * 8 for NetBSD, the main difference lying in the support for the
202697Spaul * `nz_list' type of symbols.
203697Spaul */
204697Spaul
2051156Sjkhstruct	_dynamic {
2061156Sjkh	int		d_version;	/* version # of this interface */
2071156Sjkh	struct so_debug	*d_debug;
208697Spaul	union {
2091156Sjkh		struct section_dispatch_table *d_sdt;
2101156Sjkh	} d_un;
2116887Snate	struct ld_entry *d_entry;	/* XXX */
212697Spaul};
213697Spaul
214697Spaul#define LD_VERSION_SUN		(3)
215697Spaul#define LD_VERSION_BSD		(8)
216697Spaul#define LD_VERSION_NZLIST_P(v)	((v) >= 8)
217697Spaul
2181156Sjkh#define LD_GOT(x)	((x)->d_un.d_sdt->sdt_got)
2191156Sjkh#define LD_PLT(x)	((x)->d_un.d_sdt->sdt_plt)
2201156Sjkh#define LD_REL(x)	((x)->d_un.d_sdt->sdt_rel)
2211156Sjkh#define LD_SYMBOL(x)	((x)->d_un.d_sdt->sdt_nzlist)
2221156Sjkh#define LD_HASH(x)	((x)->d_un.d_sdt->sdt_hash)
2231156Sjkh#define LD_STRINGS(x)	((x)->d_un.d_sdt->sdt_strings)
2241156Sjkh#define LD_NEED(x)	((x)->d_un.d_sdt->sdt_sods)
2251156Sjkh#define LD_BUCKETS(x)	((x)->d_un.d_sdt->sdt_buckets)
226697Spaul
2271156Sjkh#define LD_GOTSZ(x)	((x)->d_un.d_sdt->sdt_plt - (x)->d_un.d_sdt->sdt_got)
2281156Sjkh#define LD_RELSZ(x)	((x)->d_un.d_sdt->sdt_hash - (x)->d_un.d_sdt->sdt_rel)
2291156Sjkh#define LD_HASHSZ(x)	((x)->d_un.d_sdt->sdt_nzlist - (x)->d_un.d_sdt->sdt_hash)
2301156Sjkh#define LD_STABSZ(x)	((x)->d_un.d_sdt->sdt_strings - (x)->d_un.d_sdt->sdt_nzlist)
2311156Sjkh#define LD_PLTSZ(x)	((x)->d_un.d_sdt->sdt_plt_sz)
2321156Sjkh#define LD_STRSZ(x)	((x)->d_un.d_sdt->sdt_str_sz)
2331156Sjkh#define LD_TEXTSZ(x)	((x)->d_un.d_sdt->sdt_text_sz)
234697Spaul
235697Spaul/*
2361156Sjkh * Interface to ld.so
237697Spaul */
238697Spaulstruct crt_ldso {
239697Spaul	int		crt_ba;		/* Base address of ld.so */
240697Spaul	int		crt_dzfd;	/* "/dev/zero" file decriptor (SunOS) */
241697Spaul	int		crt_ldfd;	/* ld.so file descriptor */
2421156Sjkh	struct _dynamic	*crt_dp;	/* Main's __DYNAMIC */
243697Spaul	char		**crt_ep;	/* environment strings */
244697Spaul	caddr_t		crt_bp;		/* Breakpoint if run from debugger */
2456887Snate	char		*crt_prog;	/* Program name (v3) */
2466887Snate	char		*crt_ldso;	/* Link editor name (v4) */
2476887Snate	struct ld_entry	*crt_ldentry;	/* dl*() access (v4) */
248697Spaul};
249697Spaul
250697Spaul/*
251697Spaul * Version passed from crt0 to ld.so (1st argument to _rtld()).
252697Spaul */
253697Spaul#define CRT_VERSION_SUN		1
2541156Sjkh#define CRT_VERSION_BSD_2	2
2551156Sjkh#define CRT_VERSION_BSD_3	3
2566887Snate#define CRT_VERSION_BSD_4	4
257697Spaul
258697Spaul/*
259697Spaul * Maximum number of recognized shared object version numbers.
260697Spaul */
261697Spaul#define MAXDEWEY	8
262697Spaul
263697Spaul/*
264697Spaul * Header of the hints file.
265697Spaul */
266697Spaulstruct hints_header {
267697Spaul	long		hh_magic;
268697Spaul#define HH_MAGIC	011421044151
269697Spaul	long		hh_version;	/* Interface version number */
270697Spaul#define LD_HINTS_VERSION_1	1
271697Spaul	long		hh_hashtab;	/* Location of hash table */
272697Spaul	long		hh_nbucket;	/* Number of buckets in hashtab */
273697Spaul	long		hh_strtab;	/* Location of strings */
274697Spaul	long		hh_strtab_sz;	/* Size of strings */
275697Spaul	long		hh_ehints;	/* End of hints (max offset in file) */
276697Spaul};
277697Spaul
278697Spaul#define HH_BADMAG(hdr)	((hdr).hh_magic != HH_MAGIC)
279697Spaul
280697Spaul/*
281697Spaul * Hash table element in hints file.
282697Spaul */
283697Spaulstruct hints_bucket {
284697Spaul	/* namex and pathx are indices into the string table */
285697Spaul	int		hi_namex;		/* Library name */
286697Spaul	int		hi_pathx;		/* Full path */
287697Spaul	int		hi_dewey[MAXDEWEY];	/* The versions */
288697Spaul	int		hi_ndewey;		/* Number of version numbers */
289697Spaul#define hi_major hi_dewey[0]
290697Spaul#define hi_minor hi_dewey[1]
291697Spaul	int		hi_next;		/* Next in this bucket */
292697Spaul};
293697Spaul
294697Spaul#define _PATH_LD_HINTS		"/var/run/ld.so.hints"
295697Spaul
296697Spaul#endif /* _LINK_H_ */
297697Spaul
298