1178525Sjb/*
2178525Sjb * CDDL HEADER START
3178525Sjb *
4178525Sjb * The contents of this file are subject to the terms of the
5178525Sjb * Common Development and Distribution License (the "License").
6178525Sjb * You may not use this file except in compliance with the License.
7178525Sjb *
8178525Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178525Sjb * or http://www.opensolaris.org/os/licensing.
10178525Sjb * See the License for the specific language governing permissions
11178525Sjb * and limitations under the License.
12178525Sjb *
13178525Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178525Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178525Sjb * If applicable, add the following below this CDDL HEADER, with the
16178525Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178525Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178525Sjb *
19178525Sjb * CDDL HEADER END
20178525Sjb */
21178525Sjb
22178525Sjb/*
23178525Sjb *	Copyright (c) 1988 AT&T
24178525Sjb *	  All Rights Reserved
25178525Sjb *
26178525Sjb *
27178528Sjb * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28178525Sjb * Use is subject to license terms.
29178525Sjb *
30178525Sjb * Global include file for all sgs.
31178525Sjb */
32178525Sjb
33178525Sjb#ifndef	_SGS_H
34178525Sjb#define	_SGS_H
35178525Sjb
36178525Sjb#pragma ident	"%Z%%M%	%I%	%E% SMI"
37178525Sjb
38178525Sjb#ifdef	__cplusplus
39178525Sjbextern "C" {
40178525Sjb#endif
41178525Sjb
42178525Sjb/* <assert.h> keys off of NDEBUG */
43178525Sjb#ifdef	DEBUG
44178525Sjb#undef	NDEBUG
45178525Sjb#else
46178525Sjb#define	NDEBUG
47178525Sjb#endif
48178525Sjb
49178525Sjb#ifndef	_ASM
50178525Sjb#include <sys/types.h>
51178540Sjb#if defined(sun)
52178525Sjb#include <sys/machelf.h>
53178540Sjb#else
54178540Sjb#include <elf.h>
55178540Sjb#endif
56178525Sjb#include <stdlib.h>
57178525Sjb#include <libelf.h>
58178525Sjb#include <assert.h>
59178525Sjb#include <alist.h>
60178525Sjb#endif	/* _ASM */
61178525Sjb
62178525Sjb/*
63178525Sjb * Software identification.
64178525Sjb */
65178525Sjb#define	SGS		""
66178525Sjb#define	SGU_PKG		"Software Generation Utilities"
67178525Sjb#define	SGU_REL		"(SGU) Solaris-ELF (4.0)"
68178525Sjb
69178525Sjb
70178525Sjb#ifndef _ASM
71178525Sjb
72178525Sjb/*
73178540Sjb * link_ver_string[] contains a version string for use by the link-editor
74178540Sjb * and all other linker components. It is found in libconv, and is
75178540Sjb * generated by sgs/libconv/common/bld_vernote.ksh. That script produces
76178540Sjb * libconv/{plat}/vernote.s, which is in turn assembled/linked into
77178540Sjb * libconv.
78178540Sjb */
79178540Sjbextern const char link_ver_string[];
80178540Sjb/*
81178525Sjb * Macro to round to next double word boundary.
82178525Sjb */
83178525Sjb#define	S_DROUND(x)	(((x) + sizeof (double) - 1) & ~(sizeof (double) - 1))
84178525Sjb
85178525Sjb/*
86178525Sjb * General align and round macros.
87178525Sjb */
88178525Sjb#define	S_ALIGN(x, a)	((x) & ~(((a) ? (a) : 1) - 1))
89178525Sjb#define	S_ROUND(x, a)   ((x) + (((a) ? (a) : 1) - 1) & ~(((a) ? (a) : 1) - 1))
90178525Sjb
91178525Sjb/*
92178525Sjb * Bit manipulation macros; generic bit mask and is `v' in the range
93178525Sjb * supportable in `n' bits?
94178525Sjb */
95178525Sjb#define	S_MASK(n)	((1 << (n)) -1)
96178525Sjb#define	S_INRANGE(v, n)	(((-(1 << (n)) - 1) < (v)) && ((v) < (1 << (n))))
97178525Sjb
98178525Sjb
99178525Sjb/*
100178525Sjb * Yet another definition of the OFFSETOF macro, used with the AVL routines.
101178525Sjb */
102178525Sjb#define	SGSOFFSETOF(s, m)	((size_t)(&(((s *)0)->m)))
103178525Sjb
104178525Sjb/*
105178525Sjb * When casting between integer and pointer types, gcc will complain
106178525Sjb * if the integer type used is not large enough to hold the pointer
107178525Sjb * value without loss. Although a dubious practice in general, this
108178525Sjb * is sometimes done by design. In those cases, the general solution
109178525Sjb * is to introduce an intermediate cast to widen the integer value. The
110178525Sjb * CAST_PTRINT macro does this, and its use documents the fact that
111178525Sjb * the programmer is doing that sort of cast.
112178525Sjb */
113178525Sjb#ifdef __GNUC__
114178525Sjb#define	CAST_PTRINT(cast, value) ((cast)(uintptr_t)value)
115178525Sjb#else
116178525Sjb#define	CAST_PTRINT(cast, value) ((cast)value)
117178525Sjb#endif
118178525Sjb
119178525Sjb/*
120178525Sjb * General typedefs.
121178525Sjb */
122178525Sjbtypedef enum {
123178525Sjb	FALSE = 0,
124178525Sjb	TRUE = 1
125178525Sjb} Boolean;
126178525Sjb
127178525Sjb/*
128178525Sjb * Types of errors (used by eprintf()), together with a generic error return
129178525Sjb * value.
130178525Sjb */
131178525Sjbtypedef enum {
132178525Sjb	ERR_NONE,
133178525Sjb	ERR_WARNING,
134178525Sjb	ERR_FATAL,
135178525Sjb	ERR_ELF,
136178525Sjb	ERR_NUM				/* Must be last */
137178525Sjb} Error;
138178525Sjb
139178525Sjb#if defined(_LP64) && !defined(_ELF64)
140178525Sjb#define	S_ERROR		(~(uint_t)0)
141178525Sjb#else
142178525Sjb#define	S_ERROR		(~(uintptr_t)0)
143178525Sjb#endif
144178525Sjb
145178525Sjb/*
146178525Sjb * LIST_TRAVERSE() is used as the only "argument" of a "for" loop to
147178525Sjb * traverse a linked list. The node pointer `node' is set to each node in
148178525Sjb * turn and the corresponding data pointer is copied to `data'.  The macro
149178525Sjb * is used as in
150178525Sjb * 	for (LIST_TRAVERSE(List *list, Listnode *node, void *data)) {
151178525Sjb *		process(data);
152178525Sjb *	}
153178525Sjb */
154178525Sjb#define	LIST_TRAVERSE(L, N, D) \
155178525Sjb	(void) (((N) = (L)->head) != NULL && ((D) = (N)->data) != NULL); \
156178525Sjb	(N) != NULL; \
157178525Sjb	(void) (((N) = (N)->next) != NULL && ((D) = (N)->data) != NULL)
158178525Sjb
159178525Sjbtypedef	struct listnode	Listnode;
160178525Sjbtypedef	struct list	List;
161178525Sjb
162178525Sjbstruct	listnode {			/* a node on a linked list */
163178525Sjb	void		*data;		/* the data item */
164178525Sjb	Listnode	*next;		/* the next element */
165178525Sjb};
166178525Sjb
167178525Sjbstruct	list {				/* a linked list */
168178525Sjb	Listnode	*head;		/* the first element */
169178525Sjb	Listnode	*tail;		/* the last element */
170178525Sjb};
171178525Sjb
172178525Sjb
173178525Sjb#ifdef _SYSCALL32
174178525Sjbtypedef	struct listnode32	Listnode32;
175178525Sjbtypedef	struct list32		List32;
176178525Sjb
177178525Sjbstruct	listnode32 {			/* a node on a linked list */
178178525Sjb	Elf32_Addr	data;		/* the data item */
179178525Sjb	Elf32_Addr	next;		/* the next element */
180178525Sjb};
181178525Sjb
182178525Sjbstruct	list32 {			/* a linked list */
183178525Sjb	Elf32_Addr	head;		/* the first element */
184178525Sjb	Elf32_Addr	tail;		/* the last element */
185178525Sjb};
186178525Sjb#endif	/* _SYSCALL32 */
187178525Sjb
188178525Sjb
189178525Sjb/*
190178525Sjb * Structure to maintain rejected files elf information.  Files that are not
191178525Sjb * applicable to the present link-edit are rejected and a search for an
192178525Sjb * appropriate file may be resumed.  The first rejected files information is
193178525Sjb * retained so that a better error diagnostic can be given should an appropriate
194178525Sjb * file not be located.
195178525Sjb */
196178525Sjbtypedef struct {
197178525Sjb	ushort_t	rej_type;	/* SGS_REJ_ value */
198178525Sjb	ushort_t	rej_flag;	/* additional information */
199178525Sjb	uint_t		rej_info;	/* numeric and string information */
200178525Sjb	const char	*rej_str;	/*	associated with error */
201178525Sjb	const char	*rej_name;	/* object name - expanded library */
202178525Sjb					/*	name and archive members */
203178525Sjb} Rej_desc;
204178525Sjb
205178525Sjb#define	SGS_REJ_NONE		0
206178525Sjb#define	SGS_REJ_MACH		1	/* wrong ELF machine type */
207178525Sjb#define	SGS_REJ_CLASS		2	/* wrong ELF class (32-bit/64-bit) */
208178525Sjb#define	SGS_REJ_DATA		3	/* wrong ELF data format (MSG/LSB) */
209178525Sjb#define	SGS_REJ_TYPE		4	/* bad ELF type */
210178525Sjb#define	SGS_REJ_BADFLAG		5	/* bad ELF flags value */
211178525Sjb#define	SGS_REJ_MISFLAG		6	/* mismatched ELF flags value */
212178525Sjb#define	SGS_REJ_VERSION		7	/* mismatched ELF/lib version */
213178525Sjb#define	SGS_REJ_HAL		8	/* HAL R1 extensions required */
214178525Sjb#define	SGS_REJ_US3		9	/* Sun UltraSPARC III extensions */
215178525Sjb					/*	required */
216178525Sjb#define	SGS_REJ_STR		10	/* generic error - info is a string */
217178525Sjb#define	SGS_REJ_UNKFILE		11	/* unknown file type */
218178525Sjb#define	SGS_REJ_HWCAP_1		12	/* hardware capabilities mismatch */
219178525Sjb
220178525Sjb/*
221178525Sjb * For those source files used both inside and outside of the
222178525Sjb * libld source base (tools/common/string_table.c) we can
223178525Sjb * automatically switch between the allocation models
224178525Sjb * based off of the 'cc -DUSE_LIBLD_MALLOC' flag.
225178525Sjb */
226178525Sjb#ifdef	USE_LIBLD_MALLOC
227178525Sjb#define	calloc(x, a)		libld_malloc(((size_t)x) * ((size_t)a))
228178525Sjb#define	free			libld_free
229178525Sjb#define	malloc			libld_malloc
230178525Sjb#define	realloc			libld_realloc
231178525Sjb
232178525Sjb#define	libld_calloc(x, a)	libld_malloc(((size_t)x) * ((size_t)a))
233178525Sjbextern void		libld_free(void *);
234178525Sjbextern void		*libld_malloc(size_t);
235178525Sjbextern void		*libld_realloc(void *, size_t);
236178525Sjb#endif
237178525Sjb
238178525Sjb
239178525Sjb/*
240178525Sjb * Data structures (defined in libld.h).
241178525Sjb */
242178525Sjbtypedef struct ent_desc		Ent_desc;
243178525Sjbtypedef	struct group_desc	Group_desc;
244178525Sjbtypedef struct ifl_desc		Ifl_desc;
245178525Sjbtypedef struct is_desc		Is_desc;
246178525Sjbtypedef struct isa_desc		Isa_desc;
247178525Sjbtypedef struct isa_opt		Isa_opt;
248178525Sjbtypedef struct mv_desc		Mv_desc;
249178525Sjbtypedef struct ofl_desc		Ofl_desc;
250178525Sjbtypedef struct os_desc		Os_desc;
251178525Sjbtypedef	struct rel_cache	Rel_cache;
252178525Sjbtypedef	struct sdf_desc		Sdf_desc;
253178525Sjbtypedef	struct sdv_desc		Sdv_desc;
254178525Sjbtypedef struct sg_desc		Sg_desc;
255178525Sjbtypedef struct sort_desc	Sort_desc;
256178525Sjbtypedef struct sec_order	Sec_order;
257178525Sjbtypedef struct sym_desc		Sym_desc;
258178525Sjbtypedef struct sym_aux		Sym_aux;
259178525Sjbtypedef	struct sym_avlnode	Sym_avlnode;
260178525Sjbtypedef	struct uts_desc		Uts_desc;
261178525Sjbtypedef struct ver_desc		Ver_desc;
262178525Sjbtypedef struct ver_index	Ver_index;
263178525Sjbtypedef	struct audit_desc	Audit_desc;
264178525Sjbtypedef	struct audit_info	Audit_info;
265178525Sjbtypedef	struct audit_list	Audit_list;
266178525Sjb
267178525Sjb/*
268178525Sjb * Data structures defined in machrel.h.
269178525Sjb */
270178525Sjbtypedef struct rel_desc		Rel_desc;
271178525Sjb
272178525Sjb/*
273178525Sjb * Data structures defined in rtld.h.
274178525Sjb */
275178525Sjbtypedef struct lm_list		Lm_list;
276178525Sjb#ifdef _SYSCALL32
277178525Sjbtypedef struct lm_list32	Lm_list32;
278178525Sjb#endif	/* _SYSCALL32 */
279178525Sjb
280178525Sjb/*
281178525Sjb * For the various utilities that include sgs.h
282178525Sjb */
283178525Sjbextern int	assfail(const char *, const char *, int);
284178525Sjbextern void	eprintf(Lm_list *, Error, const char *, ...);
285178525Sjbextern char	*sgs_demangle(char *);
286178525Sjbextern uint_t	sgs_str_hash(const char *);
287178525Sjbextern uint_t	findprime(uint_t);
288178525Sjb
289178525Sjb#endif /* _ASM */
290178525Sjb
291178525Sjb#ifdef	__cplusplus
292178525Sjb}
293178525Sjb#endif
294178525Sjb
295178525Sjb
296178525Sjb#endif /* _SGS_H */
297