2 * Copyright (c) 2009 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: _elftc.h 3446 2016-05-03 01:31:17Z emaste $
30 ** Miscellaneous definitions needed by multiple components.
37 #define NULL ((void *) 0)
41 #define offsetof(T, M) ((int) &((T*) 0) -> M)
44 /* --QUEUE-MACROS-- [[ */
47 * Supply macros missing from <sys/queue.h>
51 * Copyright (c) 1991, 1993
52 * The Regents of the University of California. All rights reserved.
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
79 #ifndef LIST_FOREACH_SAFE
80 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
81 for ((var) = LIST_FIRST((head)); \
82 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
86 #ifndef SLIST_FOREACH_SAFE
87 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
88 for ((var) = SLIST_FIRST((head)); \
89 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
94 #define STAILQ_CONCAT(head1, head2) do { \
95 if (!STAILQ_EMPTY((head2))) { \
96 *(head1)->stqh_last = (head2)->stqh_first; \
97 (head1)->stqh_last = (head2)->stqh_last; \
98 STAILQ_INIT((head2)); \
100 } while (/*CONSTCOND*/0)
104 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
108 #define STAILQ_ENTRY(type) \
110 struct type *stqe_next; /* next element */ \
115 #define STAILQ_FIRST(head) ((head)->stqh_first)
119 #define STAILQ_HEAD(name, type) \
121 struct type *stqh_first; /* first element */ \
122 struct type **stqh_last; /* addr of last next element */ \
126 #ifndef STAILQ_HEAD_INITIALIZER
127 #define STAILQ_HEAD_INITIALIZER(head) \
128 { NULL, &(head).stqh_first }
131 #ifndef STAILQ_FOREACH
132 #define STAILQ_FOREACH(var, head, field) \
133 for ((var) = ((head)->stqh_first); \
135 (var) = ((var)->field.stqe_next))
138 #ifndef STAILQ_FOREACH_SAFE
139 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
140 for ((var) = STAILQ_FIRST((head)); \
141 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
146 #define STAILQ_INIT(head) do { \
147 (head)->stqh_first = NULL; \
148 (head)->stqh_last = &(head)->stqh_first; \
149 } while (/*CONSTCOND*/0)
152 #ifndef STAILQ_INSERT_HEAD
153 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
154 if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \
155 (head)->stqh_last = &(elm)->field.stqe_next; \
156 (head)->stqh_first = (elm); \
157 } while (/*CONSTCOND*/0)
160 #ifndef STAILQ_INSERT_TAIL
161 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
162 (elm)->field.stqe_next = NULL; \
163 *(head)->stqh_last = (elm); \
164 (head)->stqh_last = &(elm)->field.stqe_next; \
165 } while (/*CONSTCOND*/0)
168 #ifndef STAILQ_INSERT_AFTER
169 #define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
170 if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
171 (head)->stqh_last = &(elm)->field.stqe_next; \
172 (listelm)->field.stqe_next = (elm); \
173 } while (/*CONSTCOND*/0)
177 #define STAILQ_LAST(head, type, field) \
178 (STAILQ_EMPTY((head)) ? \
179 NULL : ((struct type *)(void *) \
180 ((char *)((head)->stqh_last) - offsetof(struct type, field))))
184 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
187 #ifndef STAILQ_REMOVE
188 #define STAILQ_REMOVE(head, elm, type, field) do { \
189 if ((head)->stqh_first == (elm)) { \
190 STAILQ_REMOVE_HEAD((head), field); \
192 struct type *curelm = (head)->stqh_first; \
193 while (curelm->field.stqe_next != (elm)) \
194 curelm = curelm->field.stqe_next; \
195 if ((curelm->field.stqe_next = \
196 curelm->field.stqe_next->field.stqe_next) == NULL) \
197 (head)->stqh_last = &(curelm)->field.stqe_next; \
199 } while (/*CONSTCOND*/0)
202 #ifndef STAILQ_REMOVE_HEAD
203 #define STAILQ_REMOVE_HEAD(head, field) do { \
204 if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
206 (head)->stqh_last = &(head)->stqh_first; \
207 } while (/*CONSTCOND*/0)
211 * The STAILQ_SORT macro is adapted from Simon Tatham's O(n*log(n))
212 * mergesort algorithm.
215 #define STAILQ_SORT(head, type, field, cmp) do { \
216 STAILQ_HEAD(, type) _la, _lb; \
217 struct type *_p, *_q, *_e; \
218 int _i, _sz, _nmerges, _psz, _qsz; \
224 while (!STAILQ_EMPTY((head))) { \
228 for (_i = 0; _i < _sz && !STAILQ_EMPTY((head)); \
230 _e = STAILQ_FIRST((head)); \
234 STAILQ_REMOVE_HEAD((head), field); \
235 STAILQ_INSERT_TAIL(&_la, _e, field); \
237 _p = STAILQ_FIRST(&_la); \
239 _q = STAILQ_FIRST((head)); \
240 while (_psz > 0 || (_qsz > 0 && _q != NULL)) { \
243 _q = STAILQ_NEXT(_q, field); \
244 STAILQ_REMOVE_HEAD((head), \
247 } else if (_qsz == 0 || _q == NULL) { \
249 _p = STAILQ_NEXT(_p, field); \
250 STAILQ_REMOVE_HEAD(&_la, field);\
252 } else if (cmp(_p, _q) <= 0) { \
254 _p = STAILQ_NEXT(_p, field); \
255 STAILQ_REMOVE_HEAD(&_la, field);\
259 _q = STAILQ_NEXT(_q, field); \
260 STAILQ_REMOVE_HEAD((head), \
264 STAILQ_INSERT_TAIL(&_lb, _e, field); \
267 (head)->stqh_first = _lb.stqh_first; \
268 (head)->stqh_last = _lb.stqh_last; \
270 } while (_nmerges > 1); \
271 } while (/*CONSTCOND*/0)
274 #ifndef TAILQ_FOREACH_SAFE
275 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
276 for ((var) = TAILQ_FIRST((head)); \
277 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
281 /* ]] --QUEUE-MACROS-- */
289 #if defined(__DragonFly__)
290 #define ELFTC_VCSID(ID) __RCSID(ID)
293 #if defined(__FreeBSD__)
294 #define ELFTC_VCSID(ID) __FBSDID(ID)
297 #if defined(__APPLE__) || defined(__GLIBC__) || defined(__GNU__) || \
299 #if defined(__GNUC__)
300 #define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
302 #define ELFTC_VCSID(ID) /**/
307 #if defined(__GNUC__)
308 #define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
310 #define ELFTC_VCSID(ID) /**/
314 #if defined(__NetBSD__)
315 #define ELFTC_VCSID(ID) __RCSID(ID)
318 #if defined(__OpenBSD__)
319 #if defined(__GNUC__)
320 #define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")
322 #define ELFTC_VCSID(ID) /**/
323 #endif /* __GNUC__ */
326 #endif /* ELFTC_VCSID */
329 * Provide an equivalent for getprogname(3).
332 #ifndef ELFTC_GETPROGNAME
334 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
335 defined(__minix) || defined(__NetBSD__)
339 #define ELFTC_GETPROGNAME() getprogname()
341 #endif /* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
344 #if defined(__GLIBC__) || defined(__linux__)
347 * GLIBC based systems have a global 'char *' pointer referencing
348 * the executable's name.
350 extern const char *program_invocation_short_name;
351 #endif /* !_GNU_SOURCE */
353 #define ELFTC_GETPROGNAME() program_invocation_short_name
355 #endif /* __GLIBC__ || __linux__ */
358 #if defined(__OpenBSD__)
360 extern const char *__progname;
362 #define ELFTC_GETPROGNAME() __progname
364 #endif /* __OpenBSD__ */
366 #endif /* ELFTC_GETPROGNAME */
370 ** Per-OS configuration.
373 #if defined(__APPLE__)
375 #include <libkern/OSByteOrder.h>
376 #define htobe32(x) OSSwapHostToBigInt32(x)
377 #define roundup2 roundup
379 #define ELFTC_BYTE_ORDER _BYTE_ORDER
380 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
381 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
383 #define ELFTC_HAVE_MMAP 1
384 #define ELFTC_HAVE_STRMODE 1
386 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
387 #endif /* __APPLE__ */
390 #if defined(__DragonFly__)
392 #include <osreldate.h>
393 #include <sys/endian.h>
395 #define ELFTC_BYTE_ORDER _BYTE_ORDER
396 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
397 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
399 #define ELFTC_HAVE_MMAP 1
403 #if defined(__GLIBC__) || defined(__linux__)
407 #define ELFTC_BYTE_ORDER __BYTE_ORDER
408 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __LITTLE_ENDIAN
409 #define ELFTC_BYTE_ORDER_BIG_ENDIAN __BIG_ENDIAN
411 #define ELFTC_HAVE_MMAP 1
414 * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
416 #define ELFTC_HAVE_STRMODE 0
418 /* Whether we need to supply {be,le}32dec. */
419 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
421 #define roundup2 roundup
423 #endif /* __GLIBC__ || __linux__ */
426 #if defined(__FreeBSD__)
428 #include <osreldate.h>
429 #include <sys/endian.h>
431 #define ELFTC_BYTE_ORDER _BYTE_ORDER
432 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
433 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
435 #define ELFTC_HAVE_MMAP 1
436 #define ELFTC_HAVE_STRMODE 1
437 #if __FreeBSD_version <= 900000
438 #define ELFTC_BROKEN_YY_NO_INPUT 1
440 #endif /* __FreeBSD__ */
444 #define ELFTC_HAVE_MMAP 0
448 #if defined(__NetBSD__)
450 #include <sys/param.h>
451 #include <sys/endian.h>
453 #define ELFTC_BYTE_ORDER _BYTE_ORDER
454 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
455 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
457 #define ELFTC_HAVE_MMAP 1
458 #define ELFTC_HAVE_STRMODE 1
459 #if __NetBSD_Version__ <= 599002100
460 /* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
461 /* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
462 # define ELFTC_BROKEN_YY_NO_INPUT 1
464 #endif /* __NetBSD __ */
467 #if defined(__OpenBSD__)
469 #include <sys/param.h>
470 #include <sys/endian.h>
472 #define ELFTC_BYTE_ORDER _BYTE_ORDER
473 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN
474 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN
476 #define ELFTC_HAVE_MMAP 1
477 #define ELFTC_HAVE_STRMODE 1
479 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1
480 #define roundup2 roundup
482 #endif /* __OpenBSD__ */
484 #endif /* _ELFTC_H */