1 /*	$NetBSD: _elftc.h,v 1.3 2015/09/29 19:43:39 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Id: _elftc.h 2922 2013-03-17 22:53:15Z kaiwang27
29  */
30 
31 /**
32  ** Miscellanous definitions needed by multiple components.
33  **/
34 
35 #ifndef	_ELFTC_H
36 #define	_ELFTC_H
37 
38 #ifndef	NULL
39 #define NULL 	((void *) 0)
40 #endif
41 
42 #ifndef	offsetof
43 #define	offsetof(T, M)		((int) &((T*) 0) -> M)
44 #endif
45 
46 /* --QUEUE-MACROS-- [[ */
47 
48 /*
49  * Supply macros missing from <sys/queue.h>
50  */
51 
52 /*
53  * Copyright (c) 1991, 1993
54  *	The Regents of the University of California.  All rights reserved.
55  *
56  * Redistribution and use in source and binary forms, with or without
57  * modification, are permitted provided that the following conditions
58  * are met:
59  * 1. Redistributions of source code must retain the above copyright
60  *    notice, this list of conditions and the following disclaimer.
61  * 2. Redistributions in binary form must reproduce the above copyright
62  *    notice, this list of conditions and the following disclaimer in the
63  *    documentation and/or other materials provided with the distribution.
64  * 3. Neither the name of the University nor the names of its contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78  * SUCH DAMAGE.
79  */
80 
81 #ifndef	SLIST_FOREACH_SAFE
82 #define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\
83 	for ((var) = SLIST_FIRST((head));				\
84 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);		\
85 	    (var) = (tvar))
86 #endif
87 
88 #ifndef	STAILQ_CONCAT
89 #define	STAILQ_CONCAT(head1, head2) do {			\
90 	if (!STAILQ_EMPTY((head2))) {				\
91 		*(head1)->stqh_last = (head2)->stqh_first;	\
92 		(head1)->stqh_last = (head2)->stqh_last;	\
93 		STAILQ_INIT((head2));				\
94 	}							\
95 } while (/*CONSTCOND*/0)
96 #endif
97 
98 #ifndef	STAILQ_EMPTY
99 #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
100 #endif
101 
102 #ifndef	STAILQ_ENTRY
103 #define	STAILQ_ENTRY(type)					\
104 struct {							\
105 	struct type *stqe_next;	/* next element */		\
106 }
107 #endif
108 
109 #ifndef	STAILQ_FIRST
110 #define	STAILQ_FIRST(head)	((head)->stqh_first)
111 #endif
112 
113 #ifndef	STAILQ_HEAD
114 #define	STAILQ_HEAD(name, type)					\
115 struct name {							\
116 	struct type *stqh_first; /* first element */		\
117 	struct type **stqh_last; /* addr of last next element */ \
118 }
119 #endif
120 
121 #ifndef	STAILQ_HEAD_INITIALIZER
122 #define	STAILQ_HEAD_INITIALIZER(head)				\
123 	{ NULL, &(head).stqh_first }
124 #endif
125 
126 #ifndef	STAILQ_FOREACH
127 #define	STAILQ_FOREACH(var, head, field)			\
128 	for ((var) = ((head)->stqh_first);			\
129 		(var);						\
130 		(var) = ((var)->field.stqe_next))
131 #endif
132 
133 #ifndef	STAILQ_FOREACH_SAFE
134 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)		\
135        for ((var) = STAILQ_FIRST((head));			\
136 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);	\
137 	    (var) = (tvar))
138 #endif
139 
140 #ifndef	STAILQ_INIT
141 #define	STAILQ_INIT(head) do {					\
142 	(head)->stqh_first = NULL;				\
143 	(head)->stqh_last = &(head)->stqh_first;		\
144 } while (/*CONSTCOND*/0)
145 #endif
146 
147 #ifndef	STAILQ_INSERT_HEAD
148 #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
149 	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
150 		(head)->stqh_last = &(elm)->field.stqe_next;		\
151 	(head)->stqh_first = (elm);					\
152 } while (/*CONSTCOND*/0)
153 #endif
154 
155 #ifndef	STAILQ_INSERT_TAIL
156 #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
157 	(elm)->field.stqe_next = NULL;					\
158 	*(head)->stqh_last = (elm);					\
159 	(head)->stqh_last = &(elm)->field.stqe_next;			\
160 } while (/*CONSTCOND*/0)
161 #endif
162 
163 #ifndef	STAILQ_INSERT_AFTER
164 #define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
165 	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
166 		(head)->stqh_last = &(elm)->field.stqe_next;		\
167 	(listelm)->field.stqe_next = (elm);				\
168 } while (/*CONSTCOND*/0)
169 #endif
170 
171 #ifndef	STAILQ_LAST
172 #define STAILQ_LAST(head, type, field)					\
173 	(STAILQ_EMPTY((head)) ?					\
174 	    NULL : ((struct type *)(void *)				\
175 	    ((char *)((head)->stqh_last) - offsetof(struct type, field))))
176 #endif
177 
178 #ifndef	STAILQ_NEXT
179 #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
180 #endif
181 
182 #ifndef	STAILQ_REMOVE
183 #define	STAILQ_REMOVE(head, elm, type, field) do {			\
184 	if ((head)->stqh_first == (elm)) {				\
185 		STAILQ_REMOVE_HEAD((head), field);			\
186 	} else {							\
187 		struct type *curelm = (head)->stqh_first;		\
188 		while (curelm->field.stqe_next != (elm))		\
189 			curelm = curelm->field.stqe_next;		\
190 		if ((curelm->field.stqe_next =				\
191 			curelm->field.stqe_next->field.stqe_next) == NULL) \
192 			    (head)->stqh_last = &(curelm)->field.stqe_next; \
193 	}								\
194 } while (/*CONSTCOND*/0)
195 #endif
196 
197 #ifndef	STAILQ_REMOVE_HEAD
198 #define	STAILQ_REMOVE_HEAD(head, field) do {				\
199 	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
200 	    NULL)							\
201 		(head)->stqh_last = &(head)->stqh_first;		\
202 } while (/*CONSTCOND*/0)
203 #endif
204 
205 /*
206  * The STAILQ_SORT macro is adapted from Simon Tatham's O(n*log(n))
207  * mergesort algorithm.
208  */
209 #ifndef	STAILQ_SORT
210 #define	STAILQ_SORT(head, type, field, cmp) do {			\
211 	STAILQ_HEAD(, type) _la, _lb;					\
212 	struct type *_p, *_q, *_e;					\
213 	int _i, _sz, _nmerges, _psz, _qsz;				\
214 									\
215 	_sz = 1;							\
216 	do {								\
217 		_nmerges = 0;						\
218 		STAILQ_INIT(&_lb);					\
219 		while (!STAILQ_EMPTY((head))) {				\
220 			_nmerges++;					\
221 			STAILQ_INIT(&_la);				\
222 			_psz = 0;					\
223 			for (_i = 0; _i < _sz && !STAILQ_EMPTY((head));	\
224 			     _i++) {					\
225 				_e = STAILQ_FIRST((head));		\
226 				if (_e == NULL)				\
227 					break;				\
228 				_psz++;					\
229 				STAILQ_REMOVE_HEAD((head), field);	\
230 				STAILQ_INSERT_TAIL(&_la, _e, field);	\
231 			}						\
232 			_p = STAILQ_FIRST(&_la);			\
233 			_qsz = _sz;					\
234 			_q = STAILQ_FIRST((head));			\
235 			while (_psz > 0 || (_qsz > 0 && _q != NULL)) {	\
236 				if (_psz == 0) {			\
237 					_e = _q;			\
238 					_q = STAILQ_NEXT(_q, field);	\
239 					STAILQ_REMOVE_HEAD((head),	\
240 					    field);			\
241 					_qsz--;				\
242 				} else if (_qsz == 0 || _q == NULL) {	\
243 					_e = _p;			\
244 					_p = STAILQ_NEXT(_p, field);	\
245 					STAILQ_REMOVE_HEAD(&_la, field);\
246 					_psz--;				\
247 				} else if (cmp(_p, _q) <= 0) {		\
248 					_e = _p;			\
249 					_p = STAILQ_NEXT(_p, field);	\
250 					STAILQ_REMOVE_HEAD(&_la, field);\
251 					_psz--;				\
252 				} else {				\
253 					_e = _q;			\
254 					_q = STAILQ_NEXT(_q, field);	\
255 					STAILQ_REMOVE_HEAD((head),	\
256 					    field);			\
257 					_qsz--;				\
258 				}					\
259 				STAILQ_INSERT_TAIL(&_lb, _e, field);	\
260 			}						\
261 		}							\
262 		(head)->stqh_first = _lb.stqh_first;			\
263 		(head)->stqh_last = _lb.stqh_last;			\
264 		_sz *= 2;						\
265 	} while (_nmerges > 1);						\
266 } while (/*CONSTCOND*/0)
267 #endif
268 
269 #ifndef	TAILQ_FOREACH_SAFE
270 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
271 	for ((var) = TAILQ_FIRST((head));                               \
272 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
273 	    (var) = (tvar))
274 #endif
275 
276 /* ]] --QUEUE-MACROS-- */
277 
278 /*
279  * VCS Ids.
280  */
281 
282 #ifndef	ELFTC_VCSID
283 
284 #if defined(__DragonFly__)
285 #define	ELFTC_VCSID(ID)		__RCSID(ID)
286 #endif
287 
288 #if defined(__FreeBSD__)
289 #define	ELFTC_VCSID(ID)		__FBSDID(ID)
290 #endif
291 
292 #if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
293 #if defined(__GNUC__)
294 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
295 #else
296 #define	ELFTC_VCSID(ID)		/**/
297 #endif
298 #endif
299 
300 #if !defined(__minix)
301 /* MINIX: LSC: Not accurate anymore. */
302 #if defined(__minix)
303 #if defined(__GNUC__)
304 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
305 #else
306 #define	ELFTC_VCSID(ID)		/**/
307 #endif	/* __GNU__ */
308 #endif
309 #endif /* !defined(__minix) */
310 
311 #if defined(__NetBSD__) || defined(__minix)
312 #define	ELFTC_VCSID(ID)		__RCSID(ID)
313 #endif
314 
315 #if defined(__OpenBSD__)
316 #if defined(__GNUC__)
317 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
318 #else
319 #define	ELFTC_VCSID(ID)		/**/
320 #endif	/* __GNUC__ */
321 #endif
322 
323 #ifndef ELFTC_VCSID
324 #define	ELFTC_VCSID(ID)		/**/
325 #endif
326 
327 #endif	/* ELFTC_VCSID */
328 
329 /*
330  * Provide an equivalent for getprogname(3).
331  */
332 
333 #ifndef	ELFTC_GETPROGNAME
334 
335 #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__minix) || \
336     defined(__NetBSD__)
337 
338 #include <stdlib.h>
339 
340 #define	ELFTC_GETPROGNAME()	getprogname()
341 
342 #endif	/* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
343 
344 
345 #if defined(__GLIBC__)
346 
347 /*
348  * GLIBC based systems have a global 'char *' pointer referencing
349  * the executable's name.
350  */
351 extern const char *program_invocation_short_name;
352 
353 #define	ELFTC_GETPROGNAME()	program_invocation_short_name
354 
355 #endif	/* __GLIBC__ */
356 
357 
358 #if defined(__OpenBSD__)
359 
360 extern const char *__progname;
361 
362 #define	ELFTC_GETPROGNAME()	__progname
363 
364 #endif	/* __OpenBSD__ */
365 
366 #endif	/* ELFTC_GETPROGNAME */
367 
368 
369 /**
370  ** Per-OS configuration.
371  **/
372 
373 #if defined(__DragonFly__)
374 
375 #include <osreldate.h>
376 #include <sys/endian.h>
377 
378 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
379 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
380 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
381 
382 #define	ELFTC_HAVE_MMAP				1
383 
384 #endif
385 
386 #if defined(__GLIBC__)
387 
388 #include <endian.h>
389 
390 #define	ELFTC_BYTE_ORDER			__BYTE_ORDER
391 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		__LITTLE_ENDIAN
392 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		__BIG_ENDIAN
393 
394 #define	ELFTC_HAVE_MMAP				1
395 
396 /*
397  * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
398  */
399 #define	ELFTC_HAVE_STRMODE			0
400 
401 /* Whether we need to supply {be,le}32dec. */
402 #define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
403 
404 #define	roundup2	roundup
405 
406 #endif	/* __GLIBC__ */
407 
408 
409 #if defined(__FreeBSD__)
410 
411 #include <osreldate.h>
412 #include <sys/endian.h>
413 
414 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
415 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
416 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
417 
418 #define	ELFTC_HAVE_MMAP				1
419 #define	ELFTC_HAVE_STRMODE			1
420 #if __FreeBSD_version <= 900000
421 #define	ELFTC_BROKEN_YY_NO_INPUT		1
422 #endif
423 #endif	/* __FreeBSD__ */
424 
425 
426 #if !defined(__minix)
427 /* MINIX: LSC: Must have been done for an older version of MINIX. */
428 #if defined(__minix)
429 #define	ELFTC_HAVE_MMAP				0
430 #endif	/* __minix */
431 #endif /* !defined(__minix) */
432 
433 
434 #if defined(__NetBSD__) || defined(__minix)
435 
436 #include <sys/param.h>
437 #include <sys/endian.h>
438 
439 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
440 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
441 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
442 
443 #define	ELFTC_HAVE_MMAP				1
444 #define	ELFTC_HAVE_STRMODE			1
445 #if __NetBSD_Version__ <= 599002100
446 /* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
447 /* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
448 #  define ELFTC_BROKEN_YY_NO_INPUT		1
449 #endif
450 #endif	/* __NetBSD __ */
451 
452 
453 #if defined(__OpenBSD__)
454 
455 #include <sys/param.h>
456 #include <sys/endian.h>
457 
458 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
459 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
460 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
461 
462 #define	ELFTC_HAVE_MMAP				1
463 #define	ELFTC_HAVE_STRMODE			1
464 
465 #define	ELFTC_NEED_BYTEORDER_EXTENSIONS		1
466 #define	roundup2	roundup
467 
468 #endif	/* __OpenBSD__ */
469 
470 #endif	/* _ELFTC_H */
471