xref: /titanic_51/usr/src/cmd/sgs/include/sgs.h (revision 5c46c6a053defc1bf25f2e08df2653bc7717ed0b)
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 2006 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 extern const char link_ver_string[];	/* Linker version id  */
70 					/*   see libconv/{plat}/vernote.s */
71 /*
72  * Macro to round to next double word boundary.
73  */
74 #define	S_DROUND(x)	(((x) + sizeof (double) - 1) & ~(sizeof (double) - 1))
75 
76 /*
77  * General align and round macros.
78  */
79 #define	S_ALIGN(x, a)	((x) & ~(((a) ? (a) : 1) - 1))
80 #define	S_ROUND(x, a)   ((x) + (((a) ? (a) : 1) - 1) & ~(((a) ? (a) : 1) - 1))
81 
82 /*
83  * Bit manipulation macros; generic bit mask and is `v' in the range
84  * supportable in `n' bits?
85  */
86 #define	S_MASK(n)	((1 << (n)) -1)
87 #define	S_INRANGE(v, n)	(((-(1 << (n)) - 1) < (v)) && ((v) < (1 << (n))))
88 
89 
90 /*
91  * Yet another definition of the OFFSETOF macro, used with the AVL routines.
92  */
93 #define	SGSOFFSETOF(s, m)	((size_t)(&(((s *)0)->m)))
94 
95 /*
96  * When casting between integer and pointer types, gcc will complain
97  * if the integer type used is not large enough to hold the pointer
98  * value without loss. Although a dubious practice in general, this
99  * is sometimes done by design. In those cases, the general solution
100  * is to introduce an intermediate cast to widen the integer value. The
101  * CAST_PTRINT macro does this, and its use documents the fact that
102  * the programmer is doing that sort of cast.
103  */
104 #ifdef __GNUC__
105 #define	CAST_PTRINT(cast, value) ((cast)(uintptr_t)value)
106 #else
107 #define	CAST_PTRINT(cast, value) ((cast)value)
108 #endif
109 
110 /*
111  * General typedefs.
112  */
113 typedef enum {
114 	FALSE = 0,
115 	TRUE = 1
116 } Boolean;
117 
118 /*
119  * Types of errors (used by eprintf()), together with a generic error return
120  * value.
121  */
122 typedef enum {
123 	ERR_NONE,
124 	ERR_WARNING,
125 	ERR_FATAL,
126 	ERR_ELF,
127 	ERR_NUM				/* Must be last */
128 } Error;
129 
130 #if defined(_LP64) && !defined(_ELF64)
131 #define	S_ERROR		(~(uint_t)0)
132 #else
133 #define	S_ERROR		(~(uintptr_t)0)
134 #endif
135 
136 /*
137  * LIST_TRAVERSE() is used as the only "argument" of a "for" loop to
138  * traverse a linked list. The node pointer `node' is set to each node in
139  * turn and the corresponding data pointer is copied to `data'.  The macro
140  * is used as in
141  * 	for (LIST_TRAVERSE(List *list, Listnode *node, void *data)) {
142  *		process(data);
143  *	}
144  */
145 #define	LIST_TRAVERSE(L, N, D) \
146 	(void) (((N) = (L)->head) != NULL && ((D) = (N)->data) != NULL); \
147 	(N) != NULL; \
148 	(void) (((N) = (N)->next) != NULL && ((D) = (N)->data) != NULL)
149 
150 typedef	struct listnode	Listnode;
151 typedef	struct list	List;
152 
153 struct	listnode {			/* a node on a linked list */
154 	void		*data;		/* the data item */
155 	Listnode	*next;		/* the next element */
156 };
157 
158 struct	list {				/* a linked list */
159 	Listnode	*head;		/* the first element */
160 	Listnode	*tail;		/* the last element */
161 };
162 
163 
164 #ifdef _SYSCALL32
165 typedef	struct listnode32	Listnode32;
166 typedef	struct list32		List32;
167 
168 struct	listnode32 {			/* a node on a linked list */
169 	Elf32_Addr	data;		/* the data item */
170 	Elf32_Addr	next;		/* the next element */
171 };
172 
173 struct	list32 {			/* a linked list */
174 	Elf32_Addr	head;		/* the first element */
175 	Elf32_Addr	tail;		/* the last element */
176 };
177 #endif	/* _SYSCALL32 */
178 
179 
180 /*
181  * Structure to maintain rejected files elf information.  Files that are not
182  * applicable to the present link-edit are rejected and a search for an
183  * appropriate file may be resumed.  The first rejected files information is
184  * retained so that a better error diagnostic can be given should an appropriate
185  * file not be located.
186  */
187 typedef struct {
188 	ushort_t	rej_type;	/* SGS_REJ_ value */
189 	ushort_t	rej_flag;	/* additional information */
190 	uint_t		rej_info;	/* numeric and string information */
191 	const char	*rej_str;	/*	associated with error */
192 	const char	*rej_name;	/* object name - expanded library */
193 					/*	name and archive members */
194 } Rej_desc;
195 
196 #define	SGS_REJ_NONE		0
197 #define	SGS_REJ_MACH		1	/* wrong ELF machine type */
198 #define	SGS_REJ_CLASS		2	/* wrong ELF class (32-bit/64-bit) */
199 #define	SGS_REJ_DATA		3	/* wrong ELF data format (MSG/LSB) */
200 #define	SGS_REJ_TYPE		4	/* bad ELF type */
201 #define	SGS_REJ_BADFLAG		5	/* bad ELF flags value */
202 #define	SGS_REJ_MISFLAG		6	/* mismatched ELF flags value */
203 #define	SGS_REJ_VERSION		7	/* mismatched ELF/lib version */
204 #define	SGS_REJ_HAL		8	/* HAL R1 extensions required */
205 #define	SGS_REJ_US3		9	/* Sun UltraSPARC III extensions */
206 					/*	required */
207 #define	SGS_REJ_STR		10	/* generic error - info is a string */
208 #define	SGS_REJ_UNKFILE		11	/* unknown file type */
209 #define	SGS_REJ_HWCAP_1		12	/* hardware capabilities mismatch */
210 
211 /*
212  * For those source files used both inside and outside of the
213  * libld source base (tools/common/string_table.c) we can
214  * automatically switch between the allocation models
215  * based off of the 'cc -DUSE_LIBLD_MALLOC' flag.
216  */
217 #ifdef	USE_LIBLD_MALLOC
218 #define	calloc(x, a)		libld_malloc(((size_t)x) * ((size_t)a))
219 #define	free			libld_free
220 #define	malloc			libld_malloc
221 #define	realloc			libld_realloc
222 
223 #define	libld_calloc(x, a)	libld_malloc(((size_t)x) * ((size_t)a))
224 extern void		libld_free(void *);
225 extern void		*libld_malloc(size_t);
226 extern void		*libld_realloc(void *, size_t);
227 #endif
228 
229 
230 /*
231  * Data structures (defined in libld.h).
232  */
233 typedef struct ent_desc		Ent_desc;
234 typedef	struct group_desc	Group_desc;
235 typedef struct ifl_desc		Ifl_desc;
236 typedef struct is_desc		Is_desc;
237 typedef struct isa_desc		Isa_desc;
238 typedef struct isa_opt		Isa_opt;
239 typedef struct mv_desc		Mv_desc;
240 typedef struct ofl_desc		Ofl_desc;
241 typedef struct os_desc		Os_desc;
242 typedef	struct rel_cache	Rel_cache;
243 typedef	struct sdf_desc		Sdf_desc;
244 typedef	struct sdv_desc		Sdv_desc;
245 typedef struct sg_desc		Sg_desc;
246 typedef struct sort_desc	Sort_desc;
247 typedef struct sec_order	Sec_order;
248 typedef struct sym_desc		Sym_desc;
249 typedef struct sym_aux		Sym_aux;
250 typedef	struct sym_avlnode	Sym_avlnode;
251 typedef	struct uts_desc		Uts_desc;
252 typedef struct ver_desc		Ver_desc;
253 typedef struct ver_index	Ver_index;
254 typedef	struct audit_desc	Audit_desc;
255 typedef	struct audit_info	Audit_info;
256 typedef	struct audit_list	Audit_list;
257 
258 /*
259  * Data structures defined in machrel.h.
260  */
261 typedef struct rel_desc		Rel_desc;
262 
263 /*
264  * Data structures defined in rtld.h.
265  */
266 typedef struct lm_list		Lm_list;
267 #ifdef _SYSCALL32
268 typedef struct lm_list32	Lm_list32;
269 #endif	/* _SYSCALL32 */
270 
271 /*
272  * For the various utilities that include sgs.h
273  */
274 extern int	assfail(const char *, const char *, int);
275 extern void	eprintf(Lm_list *, Error, const char *, ...);
276 extern char	*sgs_demangle(char *);
277 extern uint_t	sgs_str_hash(const char *);
278 extern uint_t	findprime(uint_t);
279 
280 #endif /* _ASM */
281 
282 #ifdef	__cplusplus
283 }
284 #endif
285 
286 
287 #endif /* _SGS_H */
288