xref: /netbsd-src/external/bsd/elftoolchain/dist/common/_elftc.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: _elftc.h,v 1.5 2016/03/13 03:47:41 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 3244 2015-08-31 19:53:08Z emaste
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	LIST_FOREACH_SAFE
82 #define	LIST_FOREACH_SAFE(var, head, field, tvar)		\
83 	for ((var) = LIST_FIRST((head));			\
84 	    (var) && ((tvar) = LIST_NEXT((var), field), 1);	\
85 	    (var) = (tvar))
86 #endif
87 
88 #ifndef	SLIST_FOREACH_SAFE
89 #define	SLIST_FOREACH_SAFE(var, head, field, tvar)		\
90 	for ((var) = SLIST_FIRST((head));			\
91 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);	\
92 	    (var) = (tvar))
93 #endif
94 
95 #ifndef	STAILQ_CONCAT
96 #define	STAILQ_CONCAT(head1, head2) do {			\
97 	if (!STAILQ_EMPTY((head2))) {				\
98 		*(head1)->stqh_last = (head2)->stqh_first;	\
99 		(head1)->stqh_last = (head2)->stqh_last;	\
100 		STAILQ_INIT((head2));				\
101 	}							\
102 } while (/*CONSTCOND*/0)
103 #endif
104 
105 #ifndef	STAILQ_EMPTY
106 #define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
107 #endif
108 
109 #ifndef	STAILQ_ENTRY
110 #define	STAILQ_ENTRY(type)					\
111 struct {							\
112 	struct type *stqe_next;	/* next element */		\
113 }
114 #endif
115 
116 #ifndef	STAILQ_FIRST
117 #define	STAILQ_FIRST(head)	((head)->stqh_first)
118 #endif
119 
120 #ifndef	STAILQ_HEAD
121 #define	STAILQ_HEAD(name, type)					\
122 struct name {							\
123 	struct type *stqh_first; /* first element */		\
124 	struct type **stqh_last; /* addr of last next element */ \
125 }
126 #endif
127 
128 #ifndef	STAILQ_HEAD_INITIALIZER
129 #define	STAILQ_HEAD_INITIALIZER(head)				\
130 	{ NULL, &(head).stqh_first }
131 #endif
132 
133 #ifndef	STAILQ_FOREACH
134 #define	STAILQ_FOREACH(var, head, field)			\
135 	for ((var) = ((head)->stqh_first);			\
136 		(var);						\
137 		(var) = ((var)->field.stqe_next))
138 #endif
139 
140 #ifndef	STAILQ_FOREACH_SAFE
141 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)		\
142        for ((var) = STAILQ_FIRST((head));			\
143 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);	\
144 	    (var) = (tvar))
145 #endif
146 
147 #ifndef	STAILQ_INIT
148 #define	STAILQ_INIT(head) do {					\
149 	(head)->stqh_first = NULL;				\
150 	(head)->stqh_last = &(head)->stqh_first;		\
151 } while (/*CONSTCOND*/0)
152 #endif
153 
154 #ifndef	STAILQ_INSERT_HEAD
155 #define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
156 	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
157 		(head)->stqh_last = &(elm)->field.stqe_next;		\
158 	(head)->stqh_first = (elm);					\
159 } while (/*CONSTCOND*/0)
160 #endif
161 
162 #ifndef	STAILQ_INSERT_TAIL
163 #define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
164 	(elm)->field.stqe_next = NULL;					\
165 	*(head)->stqh_last = (elm);					\
166 	(head)->stqh_last = &(elm)->field.stqe_next;			\
167 } while (/*CONSTCOND*/0)
168 #endif
169 
170 #ifndef	STAILQ_INSERT_AFTER
171 #define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
172 	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
173 		(head)->stqh_last = &(elm)->field.stqe_next;		\
174 	(listelm)->field.stqe_next = (elm);				\
175 } while (/*CONSTCOND*/0)
176 #endif
177 
178 #ifndef	STAILQ_LAST
179 #define STAILQ_LAST(head, type, field)					\
180 	(STAILQ_EMPTY((head)) ?					\
181 	    NULL : ((struct type *)(void *)				\
182 	    ((char *)((head)->stqh_last) - offsetof(struct type, field))))
183 #endif
184 
185 #ifndef	STAILQ_NEXT
186 #define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
187 #endif
188 
189 #ifndef	STAILQ_REMOVE
190 #define	STAILQ_REMOVE(head, elm, type, field) do {			\
191 	if ((head)->stqh_first == (elm)) {				\
192 		STAILQ_REMOVE_HEAD((head), field);			\
193 	} else {							\
194 		struct type *curelm = (head)->stqh_first;		\
195 		while (curelm->field.stqe_next != (elm))		\
196 			curelm = curelm->field.stqe_next;		\
197 		if ((curelm->field.stqe_next =				\
198 			curelm->field.stqe_next->field.stqe_next) == NULL) \
199 			    (head)->stqh_last = &(curelm)->field.stqe_next; \
200 	}								\
201 } while (/*CONSTCOND*/0)
202 #endif
203 
204 #ifndef	STAILQ_REMOVE_HEAD
205 #define	STAILQ_REMOVE_HEAD(head, field) do {				\
206 	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \
207 	    NULL)							\
208 		(head)->stqh_last = &(head)->stqh_first;		\
209 } while (/*CONSTCOND*/0)
210 #endif
211 
212 /*
213  * The STAILQ_SORT macro is adapted from Simon Tatham's O(n*log(n))
214  * mergesort algorithm.
215  */
216 #ifndef	STAILQ_SORT
217 #define	STAILQ_SORT(head, type, field, cmp) do {			\
218 	STAILQ_HEAD(, type) _la, _lb;					\
219 	struct type *_p, *_q, *_e;					\
220 	int _i, _sz, _nmerges, _psz, _qsz;				\
221 									\
222 	_sz = 1;							\
223 	do {								\
224 		_nmerges = 0;						\
225 		STAILQ_INIT(&_lb);					\
226 		while (!STAILQ_EMPTY((head))) {				\
227 			_nmerges++;					\
228 			STAILQ_INIT(&_la);				\
229 			_psz = 0;					\
230 			for (_i = 0; _i < _sz && !STAILQ_EMPTY((head));	\
231 			     _i++) {					\
232 				_e = STAILQ_FIRST((head));		\
233 				if (_e == NULL)				\
234 					break;				\
235 				_psz++;					\
236 				STAILQ_REMOVE_HEAD((head), field);	\
237 				STAILQ_INSERT_TAIL(&_la, _e, field);	\
238 			}						\
239 			_p = STAILQ_FIRST(&_la);			\
240 			_qsz = _sz;					\
241 			_q = STAILQ_FIRST((head));			\
242 			while (_psz > 0 || (_qsz > 0 && _q != NULL)) {	\
243 				if (_psz == 0) {			\
244 					_e = _q;			\
245 					_q = STAILQ_NEXT(_q, field);	\
246 					STAILQ_REMOVE_HEAD((head),	\
247 					    field);			\
248 					_qsz--;				\
249 				} else if (_qsz == 0 || _q == NULL) {	\
250 					_e = _p;			\
251 					_p = STAILQ_NEXT(_p, field);	\
252 					STAILQ_REMOVE_HEAD(&_la, field);\
253 					_psz--;				\
254 				} else if (cmp(_p, _q) <= 0) {		\
255 					_e = _p;			\
256 					_p = STAILQ_NEXT(_p, field);	\
257 					STAILQ_REMOVE_HEAD(&_la, field);\
258 					_psz--;				\
259 				} else {				\
260 					_e = _q;			\
261 					_q = STAILQ_NEXT(_q, field);	\
262 					STAILQ_REMOVE_HEAD((head),	\
263 					    field);			\
264 					_qsz--;				\
265 				}					\
266 				STAILQ_INSERT_TAIL(&_lb, _e, field);	\
267 			}						\
268 		}							\
269 		(head)->stqh_first = _lb.stqh_first;			\
270 		(head)->stqh_last = _lb.stqh_last;			\
271 		_sz *= 2;						\
272 	} while (_nmerges > 1);						\
273 } while (/*CONSTCOND*/0)
274 #endif
275 
276 #ifndef	TAILQ_FOREACH_SAFE
277 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
278 	for ((var) = TAILQ_FIRST((head));                               \
279 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
280 	    (var) = (tvar))
281 #endif
282 
283 /* ]] --QUEUE-MACROS-- */
284 
285 /*
286  * VCS Ids.
287  */
288 
289 #ifndef	ELFTC_VCSID
290 
291 #if defined(__DragonFly__)
292 #define	ELFTC_VCSID(ID)		__RCSID(ID)
293 #endif
294 
295 #if defined(__FreeBSD__)
296 #define	ELFTC_VCSID(ID)		__FBSDID(ID)
297 #endif
298 
299 #if defined(__APPLE__) || defined(__GLIBC__) || defined(__GNU__) || \
300     defined(__linux__)
301 #if defined(__GNUC__)
302 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
303 #else
304 #define	ELFTC_VCSID(ID)		/**/
305 #endif
306 #endif
307 
308 #if defined(__minix)
309 #if defined(__GNUC__)
310 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
311 #else
312 #define	ELFTC_VCSID(ID)		/**/
313 #endif	/* __GNU__ */
314 #endif
315 
316 #if defined(__NetBSD__)
317 #define	ELFTC_VCSID(ID)		__RCSID(ID)
318 #endif
319 
320 #if defined(__OpenBSD__)
321 #if defined(__GNUC__)
322 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
323 #else
324 #define	ELFTC_VCSID(ID)		/**/
325 #endif	/* __GNUC__ */
326 #endif
327 
328 #ifndef ELFTC_VCSID
329 #define	ELFTC_VCSID(ID)		/**/
330 #endif
331 
332 #endif	/* ELFTC_VCSID */
333 
334 /*
335  * Provide an equivalent for getprogname(3).
336  */
337 
338 #ifndef	ELFTC_GETPROGNAME
339 
340 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
341     defined(__minix) || defined(__NetBSD__)
342 
343 #include <stdlib.h>
344 
345 #define	ELFTC_GETPROGNAME()	getprogname()
346 
347 #endif	/* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
348 
349 
350 #if defined(__GLIBC__) || defined(__linux__)
351 #ifndef _GNU_SOURCE
352 /*
353  * GLIBC based systems have a global 'char *' pointer referencing
354  * the executable's name.
355  */
356 extern const char *program_invocation_short_name;
357 #endif	/* !_GNU_SOURCE */
358 
359 #define	ELFTC_GETPROGNAME()	program_invocation_short_name
360 
361 #endif	/* __GLIBC__ || __linux__ */
362 
363 
364 #if defined(__OpenBSD__)
365 
366 extern const char *__progname;
367 
368 #define	ELFTC_GETPROGNAME()	__progname
369 
370 #endif	/* __OpenBSD__ */
371 
372 #endif	/* ELFTC_GETPROGNAME */
373 
374 
375 #ifndef HAVE_NBTOOL_CONFIG_H
376 /**
377  ** Per-OS configuration.
378  **/
379 
380 #if defined(__APPLE__)
381 
382 #include <libkern/OSByteOrder.h>
383 #define	htobe32(x)	OSSwapHostToBigInt32(x)
384 #define	roundup2	roundup
385 
386 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
387 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
388 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
389 
390 #define	ELFTC_HAVE_MMAP				1
391 #define	ELFTC_HAVE_STRMODE			1
392 
393 #define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
394 #endif /* __APPLE__ */
395 
396 
397 #if defined(__DragonFly__)
398 
399 #include <osreldate.h>
400 #include <sys/endian.h>
401 
402 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
403 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
404 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
405 
406 #define	ELFTC_HAVE_MMAP				1
407 
408 #endif
409 
410 #if defined(__GLIBC__) || defined(__linux__)
411 
412 #include <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 
420 /*
421  * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
422  */
423 #define	ELFTC_HAVE_STRMODE			0
424 
425 /* Whether we need to supply {be,le}32dec. */
426 #define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
427 
428 #define	roundup2	roundup
429 
430 #endif	/* __GLIBC__ || __linux__ */
431 
432 
433 #if defined(__FreeBSD__)
434 
435 #include <osreldate.h>
436 #include <sys/endian.h>
437 
438 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
439 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
440 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
441 
442 #define	ELFTC_HAVE_MMAP				1
443 #define	ELFTC_HAVE_STRMODE			1
444 #if __FreeBSD_version <= 900000
445 #define	ELFTC_BROKEN_YY_NO_INPUT		1
446 #endif
447 #endif	/* __FreeBSD__ */
448 
449 
450 #if defined(__minix)
451 #define	ELFTC_HAVE_MMAP				0
452 #endif	/* __minix */
453 
454 
455 #if defined(__NetBSD__)
456 
457 #include <sys/param.h>
458 #include <sys/endian.h>
459 
460 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
461 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
462 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
463 
464 #define	ELFTC_HAVE_MMAP				1
465 #define	ELFTC_HAVE_STRMODE			1
466 
467 #endif	/* __NetBSD __ */
468 
469 
470 #if defined(__OpenBSD__)
471 
472 #include <sys/param.h>
473 #include <sys/endian.h>
474 
475 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
476 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
477 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
478 
479 #define	ELFTC_HAVE_MMAP				1
480 #define	ELFTC_HAVE_STRMODE			1
481 
482 #define	ELFTC_NEED_BYTEORDER_EXTENSIONS		1
483 #define	roundup2	roundup
484 
485 #endif	/* __OpenBSD__ */
486 
487 #else /* Crosscompiling for NetBSD tools */
488 
489 #include <sys/param.h>
490 #include <sys/endian.h>
491 
492 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
493 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
494 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
495 
496 #define	ELFTC_HAVE_MMAP				1
497 #define	ELFTC_HAVE_STRMODE			1
498 
499 #endif /* NBTOOL_CONFIG_H */
500 
501 #endif	/* _ELFTC_H */
502