xref: /netbsd-src/external/bsd/elftoolchain/dist/common/_elftc.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: _elftc.h,v 1.8 2021/03/10 21:36:29 jkoshy 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 3933 2021-03-10 21:09:49Z jkoshy
29  */
30 
31 /**
32  ** Miscellaneous 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  * The marker below is intended to be replaced with a project-specific
289  * definition of the ELFTC_VCSID macro.
290  */
291 
292 #ifndef ELFTC_VCSID
293 #define	ELFTC_VCSID(ID)		/**/
294 #endif
295 
296 #ifndef	ELFTC_VCSID
297 
298 #if defined(__DragonFly__) || defined(__NetBSD__)
299 
300 #define	ELFTC_VCSID(ID)		__RCSID(ID)
301 
302 #elif defined(__FreeBSD__)
303 
304 #define	ELFTC_VCSID(ID)		__FBSDID(ID)
305 
306 #elif defined(__APPLE__) || defined(__OpenBSD__) || defined(__GLIBC__) || \
307     defined(__GNU__) || defined(__linux__) || defined(__minix)
308 
309 #if defined(__GNUC__)
310 #define	ELFTC_VCSID(ID)		__asm__(".ident\t\"" ID "\"")
311 #else
312 #define	ELFTC_VCSID(ID)		/**/
313 #endif
314 
315 #endif
316 
317 #endif	/* ELFTC_VCSID */
318 
319 /*
320  * Provide an equivalent for getprogname(3).
321  */
322 
323 #ifndef	ELFTC_GETPROGNAME
324 
325 #if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \
326     defined(__minix) || defined(__NetBSD__)
327 
328 #include <stdlib.h>
329 
330 #define	ELFTC_GETPROGNAME()	getprogname()
331 
332 #endif	/* __APPLE__ || __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */
333 
334 
335 #if defined(__GLIBC__) || defined(__linux__)
336 #ifndef _GNU_SOURCE
337 /*
338  * GLIBC based systems have a global 'char *' pointer referencing
339  * the executable's name.
340  */
341 extern const char *program_invocation_short_name;
342 #endif	/* !_GNU_SOURCE */
343 
344 #define	ELFTC_GETPROGNAME()	program_invocation_short_name
345 
346 #endif	/* __GLIBC__ || __linux__ */
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  * The following symbols are supported by this configuration fragment,
364  * although not all the OSes so referenced are fully supported.
365  *
366  * Cross-compilation:
367  *
368  * HAVE_NBTOOL_CONFIG_H : cross-compiling NetBSD tools on various OSes.
369  *
370  * Native compilation:
371  *
372  * __APPLE__     : compiling under Mac OS X.
373  * __DragonFly__ : compiling under DragonFlyBSD.
374  * __GLIBC__     : compiling under GNU based systems, such as GNU/kFreeBSD.
375  * __linux__     : compiling under GNU/Linux systems.
376  * __FreeBSD__   : compiling under FreeBSD.
377  * __minix       : compiling under Minix3.
378  * __NetBSD__    : compiling (native) under NetBSD.
379  * __OpenBSD__   : compiling under OpenBSD.
380  */
381 
382 #if defined(HAVE_NBTOOL_CONFIG_H)
383 
384 #include <sys/param.h>
385 #include <sys/endian.h>
386 
387 #ifndef	roundup2
388 #define	roundup2	roundup
389 #endif
390 
391 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
392 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
393 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
394 
395 #define	ELFTC_HAVE_MMAP				1
396 #define	ELFTC_HAVE_STRMODE			1
397 
398 #elif defined(__APPLE__)
399 
400 #include <libkern/OSByteOrder.h>
401 #define	htobe32(x)	OSSwapHostToBigInt32(x)
402 #define	roundup2	roundup
403 
404 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
405 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
406 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
407 
408 #define	ELFTC_HAVE_MMAP				1
409 #define	ELFTC_HAVE_STRMODE			1
410 
411 #define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
412 
413 #elif defined(__DragonFly__)
414 
415 #include <osreldate.h>
416 #include <sys/endian.h>
417 
418 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
419 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
420 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
421 
422 #define	ELFTC_HAVE_MMAP				1
423 
424 #elif defined(__GLIBC__) || defined(__linux__)
425 
426 #include <endian.h>
427 
428 #define	ELFTC_BYTE_ORDER			__BYTE_ORDER
429 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		__LITTLE_ENDIAN
430 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		__BIG_ENDIAN
431 
432 #define	ELFTC_HAVE_MMAP				1
433 
434 /*
435  * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).
436  */
437 #define	ELFTC_HAVE_STRMODE			0
438 
439 /* Whether we need to supply {be,le}32dec. */
440 #define ELFTC_NEED_BYTEORDER_EXTENSIONS		1
441 
442 #define	roundup2	roundup
443 
444 #elif defined(__FreeBSD__)
445 
446 #include <osreldate.h>
447 #include <sys/endian.h>
448 
449 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
450 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
451 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
452 
453 #define	ELFTC_HAVE_MMAP				1
454 #define	ELFTC_HAVE_STRMODE			1
455 #if __FreeBSD_version <= 900000
456 #define	ELFTC_BROKEN_YY_NO_INPUT		1
457 #endif
458 
459 #elif defined(__minix)
460 #define	ELFTC_HAVE_MMAP				0
461 
462 #elif defined(__NetBSD__)
463 
464 #include <sys/param.h>
465 #include <sys/endian.h>
466 
467 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
468 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
469 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
470 
471 #define	ELFTC_HAVE_MMAP				1
472 #define	ELFTC_HAVE_STRMODE			1
473 #if __NetBSD_Version__ <= 599002100
474 /* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */
475 /* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */
476 #  define ELFTC_BROKEN_YY_NO_INPUT		1
477 #endif
478 
479 #elif defined(__OpenBSD__)
480 
481 #include <sys/param.h>
482 #include <sys/endian.h>
483 
484 #define	ELFTC_BYTE_ORDER			_BYTE_ORDER
485 #define	ELFTC_BYTE_ORDER_LITTLE_ENDIAN		_LITTLE_ENDIAN
486 #define	ELFTC_BYTE_ORDER_BIG_ENDIAN		_BIG_ENDIAN
487 
488 #define	ELFTC_HAVE_MMAP				1
489 #define	ELFTC_HAVE_STRMODE			1
490 
491 #define	ELFTC_NEED_BYTEORDER_EXTENSIONS		1
492 #define	roundup2	roundup
493 
494 #endif	/* __OpenBSD__ */
495 
496 #endif	/* _ELFTC_H */
497