xref: /illumos-gate/usr/src/cmd/sgs/include/sgs.h (revision 3edf445c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  *
27  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Global include file for all sgs.
31  */
32 
33 #ifndef	_SGS_H
34 #define	_SGS_H
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /* <assert.h> keys off of NDEBUG */
44 #ifdef	DEBUG
45 #undef	NDEBUG
46 #else
47 #define	NDEBUG
48 #endif
49 
50 #ifndef	_ASM
51 #include <sys/types.h>
52 #include <sys/machelf.h>
53 #include <stdlib.h>
54 #include <libelf.h>
55 #include <assert.h>
56 #include <alist.h>
57 #endif	/* _ASM */
58 
59 /*
60  * Software identification.
61  */
62 #define	SGS		""
63 #define	SGU_PKG		"Software Generation Utilities"
64 #define	SGU_REL		"(SGU) Solaris-ELF (4.0)"
65 
66 
67 #ifndef _ASM
68 
69 /*
70  * link_ver_string[] contains a version string for use by the link-editor
71  * and all other linker components. It is found in libconv, and is
72  * generated by sgs/libconv/common/bld_vernote.ksh. That script produces
73  * libconv/{plat}/vernote.s, which is in turn assembled/linked into
74  * libconv.
75  */
76 extern const char link_ver_string[];
77 /*
78  * Macro to round to next double word boundary.
79  */
80 #define	S_DROUND(x)	(((x) + sizeof (double) - 1) & ~(sizeof (double) - 1))
81 
82 /*
83  * General align and round macros.
84  */
85 #define	S_ALIGN(x, a)	((x) & ~(((a) ? (a) : 1) - 1))
86 #define	S_ROUND(x, a)   ((x) + (((a) ? (a) : 1) - 1) & ~(((a) ? (a) : 1) - 1))
87 
88 /*
89  * Bit manipulation macros; generic bit mask and is `v' in the range
90  * supportable in `n' bits?
91  */
92 #define	S_MASK(n)	((1 << (n)) -1)
93 #define	S_INRANGE(v, n)	(((-(1 << (n)) - 1) < (v)) && ((v) < (1 << (n))))
94 
95 
96 /*
97  * Yet another definition of the OFFSETOF macro, used with the AVL routines.
98  */
99 #define	SGSOFFSETOF(s, m)	((size_t)(&(((s *)0)->m)))
100 
101 /*
102  * When casting between integer and pointer types, gcc will complain
103  * if the integer type used is not large enough to hold the pointer
104  * value without loss. Although a dubious practice in general, this
105  * is sometimes done by design. In those cases, the general solution
106  * is to introduce an intermediate cast to widen the integer value. The
107  * CAST_PTRINT macro does this, and its use documents the fact that
108  * the programmer is doing that sort of cast.
109  */
110 #ifdef __GNUC__
111 #define	CAST_PTRINT(cast, value) ((cast)(uintptr_t)value)
112 #else
113 #define	CAST_PTRINT(cast, value) ((cast)value)
114 #endif
115 
116 /*
117  * General typedefs.
118  */
119 typedef enum {
120 	FALSE = 0,
121 	TRUE = 1
122 } Boolean;
123 
124 /*
125  * Types of errors (used by eprintf()), together with a generic error return
126  * value.
127  */
128 typedef enum {
129 	ERR_NONE,
130 	ERR_WARNING,
131 	ERR_FATAL,
132 	ERR_ELF,
133 	ERR_NUM				/* Must be last */
134 } Error;
135 
136 #if defined(_LP64) && !defined(_ELF64)
137 #define	S_ERROR		(~(uint_t)0)
138 #else
139 #define	S_ERROR		(~(uintptr_t)0)
140 #endif
141 
142 /*
143  * LIST_TRAVERSE() is used as the only "argument" of a "for" loop to
144  * traverse a linked list. The node pointer `node' is set to each node in
145  * turn and the corresponding data pointer is copied to `data'.  The macro
146  * is used as in
147  * 	for (LIST_TRAVERSE(List *list, Listnode *node, void *data)) {
148  *		process(data);
149  *	}
150  */
151 #define	LIST_TRAVERSE(L, N, D) \
152 	(void) (((N) = (L)->head) != NULL && ((D) = (N)->data) != NULL); \
153 	(N) != NULL; \
154 	(void) (((N) = (N)->next) != NULL && ((D) = (N)->data) != NULL)
155 
156 typedef	struct listnode	Listnode;
157 typedef	struct list	List;
158 
159 struct	listnode {			/* a node on a linked list */
160 	void		*data;		/* the data item */
161 	Listnode	*next;		/* the next element */
162 };
163 
164 struct	list {				/* a linked list */
165 	Listnode	*head;		/* the first element */
166 	Listnode	*tail;		/* the last element */
167 };
168 
169 
170 #ifdef _SYSCALL32
171 typedef	struct listnode32	Listnode32;
172 typedef	struct list32		List32;
173 
174 struct	listnode32 {			/* a node on a linked list */
175 	Elf32_Addr	data;		/* the data item */
176 	Elf32_Addr	next;		/* the next element */
177 };
178 
179 struct	list32 {			/* a linked list */
180 	Elf32_Addr	head;		/* the first element */
181 	Elf32_Addr	tail;		/* the last element */
182 };
183 #endif	/* _SYSCALL32 */
184 
185 
186 /*
187  * Structure to maintain rejected files elf information.  Files that are not
188  * applicable to the present link-edit are rejected and a search for an
189  * appropriate file may be resumed.  The first rejected files information is
190  * retained so that a better error diagnostic can be given should an appropriate
191  * file not be located.
192  */
193 typedef struct {
194 	ushort_t	rej_type;	/* SGS_REJ_ value */
195 	ushort_t	rej_flag;	/* additional information */
196 	uint_t		rej_info;	/* numeric and string information */
197 	const char	*rej_str;	/*	associated with error */
198 	const char	*rej_name;	/* object name - expanded library */
199 					/*	name and archive members */
200 } Rej_desc;
201 
202 #define	SGS_REJ_NONE		0
203 #define	SGS_REJ_MACH		1	/* wrong ELF machine type */
204 #define	SGS_REJ_CLASS		2	/* wrong ELF class (32-bit/64-bit) */
205 #define	SGS_REJ_DATA		3	/* wrong ELF data format (MSG/LSB) */
206 #define	SGS_REJ_TYPE		4	/* bad ELF type */
207 #define	SGS_REJ_BADFLAG		5	/* bad ELF flags value */
208 #define	SGS_REJ_MISFLAG		6	/* mismatched ELF flags value */
209 #define	SGS_REJ_VERSION		7	/* mismatched ELF/lib version */
210 #define	SGS_REJ_HAL		8	/* HAL R1 extensions required */
211 #define	SGS_REJ_US3		9	/* Sun UltraSPARC III extensions */
212 					/*	required */
213 #define	SGS_REJ_STR		10	/* generic error - info is a string */
214 #define	SGS_REJ_UNKFILE		11	/* unknown file type */
215 #define	SGS_REJ_HWCAP_1		12	/* hardware capabilities mismatch */
216 
217 /*
218  * For those source files used both inside and outside of the
219  * libld source base (tools/common/string_table.c) we can
220  * automatically switch between the allocation models
221  * based off of the 'cc -DUSE_LIBLD_MALLOC' flag.
222  */
223 #ifdef	USE_LIBLD_MALLOC
224 #define	calloc(x, a)		libld_malloc(((size_t)x) * ((size_t)a))
225 #define	free			libld_free
226 #define	malloc			libld_malloc
227 #define	realloc			libld_realloc
228 
229 #define	libld_calloc(x, a)	libld_malloc(((size_t)x) * ((size_t)a))
230 extern void		libld_free(void *);
231 extern void		*libld_malloc(size_t);
232 extern void		*libld_realloc(void *, size_t);
233 #endif
234 
235 
236 /*
237  * Data structures (defined in libld.h).
238  */
239 typedef struct ent_desc		Ent_desc;
240 typedef	struct group_desc	Group_desc;
241 typedef struct ifl_desc		Ifl_desc;
242 typedef struct is_desc		Is_desc;
243 typedef struct isa_desc		Isa_desc;
244 typedef struct isa_opt		Isa_opt;
245 typedef struct mv_desc		Mv_desc;
246 typedef struct ofl_desc		Ofl_desc;
247 typedef struct os_desc		Os_desc;
248 typedef	struct rel_cache	Rel_cache;
249 typedef	struct sdf_desc		Sdf_desc;
250 typedef	struct sdv_desc		Sdv_desc;
251 typedef struct sg_desc		Sg_desc;
252 typedef struct sort_desc	Sort_desc;
253 typedef struct sec_order	Sec_order;
254 typedef struct sym_desc		Sym_desc;
255 typedef struct sym_aux		Sym_aux;
256 typedef	struct sym_avlnode	Sym_avlnode;
257 typedef	struct uts_desc		Uts_desc;
258 typedef struct ver_desc		Ver_desc;
259 typedef struct ver_index	Ver_index;
260 typedef	struct audit_desc	Audit_desc;
261 typedef	struct audit_info	Audit_info;
262 typedef	struct audit_list	Audit_list;
263 
264 /*
265  * Data structures defined in machrel.h.
266  */
267 typedef struct rel_desc		Rel_desc;
268 
269 /*
270  * Data structures defined in rtld.h.
271  */
272 typedef struct lm_list		Lm_list;
273 #ifdef _SYSCALL32
274 typedef struct lm_list32	Lm_list32;
275 #endif	/* _SYSCALL32 */
276 
277 /*
278  * For the various utilities that include sgs.h
279  */
280 extern int	assfail(const char *, const char *, int);
281 extern void	eprintf(Lm_list *, Error, const char *, ...);
282 extern char	*sgs_demangle(char *);
283 extern uint_t	sgs_str_hash(const char *);
284 extern uint_t	findprime(uint_t);
285 
286 #endif /* _ASM */
287 
288 #ifdef	__cplusplus
289 }
290 #endif
291 
292 
293 #endif /* _SGS_H */
294