xref: /illumos-gate/usr/src/cmd/sgs/include/sgs.h (revision dd72704bd9e794056c558153663c739e2012d721)
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 (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Copyright 2022 Oxide Computer Company
29  *
30  * Global include file for all sgs.
31  */
32 
33 #ifndef	_SGS_H
34 #define	_SGS_H
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /* <assert.h> keys off of NDEBUG */
41 #ifdef	DEBUG
42 #undef	NDEBUG
43 #else
44 #define	NDEBUG
45 #endif
46 
47 #ifndef	_ASM
48 #include <sys/types.h>
49 #include <sys/machelf.h>
50 #include <stdlib.h>
51 #include <stdarg.h>
52 #include <libelf.h>
53 #include <assert.h>
54 #include <alist.h>
55 #endif	/* _ASM */
56 
57 /*
58  * Software identification.
59  */
60 #define	SGS		""
61 #define	SGU_PKG		"Software Generation Utilities"
62 #define	SGU_REL		"(SGU) Solaris-ELF (4.0)"
63 
64 
65 #ifndef _ASM
66 
67 /*
68  * link_ver_string[] contains a version string for use by the link-editor
69  * and all other linker components. It is found in libconv, and is
70  * generated by sgs/libconv/common/bld_vernote.ksh. That script produces
71  * libconv/{plat}/vernote.s, which is in turn assembled/linked into
72  * libconv.
73  */
74 extern const char link_ver_string[];
75 /*
76  * Macro to round to next double word boundary.
77  */
78 #define	S_DROUND(x)	(((x) + sizeof (double) - 1) & ~(sizeof (double) - 1))
79 
80 /*
81  * General align and round macros.
82  */
83 #define	S_ALIGN(x, a)	((x) & ~(((a) ? (a) : 1) - 1))
84 #define	S_ROUND(x, a)   ((x) + (((a) ? (a) : 1) - 1) & ~(((a) ? (a) : 1) - 1))
85 
86 /*
87  * Bit manipulation macros; generic bit mask and is `v' in the range
88  * supportable in `n' bits?
89  */
90 #define	S_MASK(n)	((1 << (n)) -1)
91 #define	S_INRANGE(v, n)	(((-(1 << (n)) - 1) < (v)) && ((v) < (1 << (n))))
92 
93 
94 /*
95  * Yet another definition of the OFFSETOF macro, used with the AVL routines.
96  */
97 #define	SGSOFFSETOF(s, m)	((size_t)(&(((s *)0)->m)))
98 
99 /*
100  * When casting between integer and pointer types, gcc will complain
101  * if the integer type used is not large enough to hold the pointer
102  * value without loss. Although a dubious practice in general, this
103  * is sometimes done by design. In those cases, the general solution
104  * is to introduce an intermediate cast to widen the integer value. The
105  * CAST_PTRINT macro does this, and its use documents the fact that
106  * the programmer is doing that sort of cast.
107  */
108 #ifdef __GNUC__
109 #define	CAST_PTRINT(cast, value) ((cast)(uintptr_t)value)
110 #else
111 #define	CAST_PTRINT(cast, value) ((cast)value)
112 #endif
113 
114 /*
115  * General typedefs.
116  */
117 typedef enum {
118 	FALSE = 0,
119 	TRUE = 1
120 } Boolean;
121 
122 /*
123  * Types of errors (used by veprintf()), together with a generic error return
124  * value.
125  */
126 typedef enum {
127 	ERR_NONE,		/* plain message */
128 	ERR_WARNING_NF,		/* warning that cannot be promoted to fatal */
129 	ERR_WARNING,		/* warning that can be promoted to fatal */
130 	ERR_GUIDANCE,		/* guidance warning that can be promoted */
131 	ERR_FATAL,		/* fatal error */
132 	ERR_ELF,		/* fatal libelf error */
133 	ERR_NUM			/* # of Error codes. Must be last */
134 } Error;
135 
136 /*
137  * Type used to represent line numbers within files, and a corresponding
138  * printing macro for it.
139  */
140 typedef ulong_t Lineno;
141 #define	EC_LINENO(_x) EC_XWORD(_x)			/* "llu" */
142 
143 
144 #if defined(_LP64) && !defined(_ELF64)
145 #define	S_ERROR		(~(uint_t)0)
146 #else
147 #define	S_ERROR		(~(uintptr_t)0)
148 #endif
149 
150 /*
151  * CTF currently does not handle automatic array variables sized via function
152  * arguments (VLA arrays) properly, when the code is compiled with gcc.
153  * Adding 1 to the size is a workaround.  VLA_SIZE, and its use, should be
154  * pulled when CTF is fixed or replaced.
155  */
156 #ifdef __GNUC__
157 #define	VLA_SIZE(_arg)	((_arg) + 1)
158 #else
159 #define	VLA_SIZE(_arg)	(_arg)
160 #endif
161 
162 /*
163  * Structure to maintain rejected files elf information.  Files that are not
164  * applicable to the present link-edit are rejected and a search for an
165  * appropriate file may be resumed.  The first rejected files information is
166  * retained so that a better error diagnostic can be given should an appropriate
167  * file not be located.
168  */
169 typedef struct {
170 	ushort_t	rej_type;	/* SGS_REJ_ value */
171 	ushort_t	rej_flags;	/* additional information */
172 	uint_t		rej_info;	/* numeric and string information */
173 	const char	*rej_str;	/*	associated with error */
174 	const char	*rej_name;	/* object name - expanded library */
175 					/*	name and archive members */
176 } Rej_desc;
177 
178 #define	SGS_REJ_NONE		0
179 #define	SGS_REJ_MACH		1	/* wrong ELF machine type */
180 #define	SGS_REJ_CLASS		2	/* wrong ELF class (32-bit/64-bit) */
181 #define	SGS_REJ_DATA		3	/* wrong ELF data format (MSG/LSB) */
182 #define	SGS_REJ_TYPE		4	/* bad ELF type */
183 #define	SGS_REJ_BADFLAG		5	/* bad ELF flags value */
184 #define	SGS_REJ_MISFLAG		6	/* mismatched ELF flags value */
185 #define	SGS_REJ_VERSION		7	/* mismatched ELF/lib version */
186 #define	SGS_REJ_HAL		8	/* HAL R1 extensions required */
187 #define	SGS_REJ_US3		9	/* Sun UltraSPARC III extensions */
188 					/*	required */
189 #define	SGS_REJ_STR		10	/* generic error - info is a string */
190 #define	SGS_REJ_UNKFILE		11	/* unknown file type */
191 #define	SGS_REJ_UNKCAP		12	/* unknown capabilities */
192 #define	SGS_REJ_HWCAP_1		13	/* hardware capabilities mismatch */
193 #define	SGS_REJ_SFCAP_1		14	/* software capabilities mismatch */
194 #define	SGS_REJ_MACHCAP		15	/* machine capability mismatch */
195 #define	SGS_REJ_PLATCAP		16	/* platform capability mismatch */
196 #define	SGS_REJ_HWCAP_2		17	/* hardware capabilities mismatch */
197 #define	SGS_REJ_ARCHIVE		18	/* archive used in invalid context */
198 #define	SGS_REJ_KMOD		19	/* object is a kernel module */
199 #define	SGS_REJ_HWCAP_3		20	/* hardware capabilities mismatch */
200 #define	SGS_REJ_NUM		21
201 
202 
203 #define	FLG_REJ_ALTER		0x01	/* object name is an alternative */
204 
205 /*
206  * Data structures (defined in libld.h).
207  */
208 typedef	struct audit_desc	Audit_desc;
209 typedef	struct audit_info	Audit_info;
210 typedef	struct audit_list	Audit_list;
211 typedef struct cap_desc		Cap_desc;
212 typedef struct ent_desc		Ent_desc;
213 typedef	struct group_desc	Group_desc;
214 typedef struct ifl_desc		Ifl_desc;
215 typedef struct is_desc		Is_desc;
216 typedef struct isa_desc		Isa_desc;
217 typedef struct isa_opt		Isa_opt;
218 typedef struct os_desc		Os_desc;
219 typedef struct ofl_desc		Ofl_desc;
220 typedef	struct rel_cache	Rel_cache;
221 typedef	struct rel_cachebuf	Rel_cachebuf;
222 typedef	struct rel_aux_cachebuf	Rel_aux_cachebuf;
223 typedef struct rel_aux		Rel_aux;
224 typedef struct rel_desc		Rel_desc;
225 typedef	struct sdf_desc		Sdf_desc;
226 typedef	struct sdv_desc		Sdv_desc;
227 typedef struct sec_order	Sec_order;
228 typedef struct sg_desc		Sg_desc;
229 typedef struct sort_desc	Sort_desc;
230 typedef	struct sym_avlnode	Sym_avlnode;
231 typedef struct sym_aux		Sym_aux;
232 typedef struct sym_desc		Sym_desc;
233 typedef	struct uts_desc		Uts_desc;
234 typedef struct ver_desc		Ver_desc;
235 typedef struct ver_index	Ver_index;
236 
237 /*
238  * Data structures defined in rtld.h.
239  */
240 typedef struct lm_list		Lm_list;
241 #ifdef _SYSCALL32
242 typedef struct lm_list32	Lm_list32;
243 #endif	/* _SYSCALL32 */
244 
245 /*
246  * For the various utilities that include sgs.h
247  */
248 extern int	assfail(const char *, const char *, int);
249 extern void	eprintf(Lm_list *, Error, const char *, ...);
250 extern void	veprintf(Lm_list *, Error, const char *, va_list);
251 extern uint_t	sgs_str_hash(const char *);
252 extern uint_t	findprime(uint_t);
253 
254 #endif /* _ASM */
255 
256 #ifdef	__cplusplus
257 }
258 #endif
259 
260 
261 #endif /* _SGS_H */
262