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