1 /* $NetBSD: _elftc.h,v 1.11 2025/02/10 15:17:52 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 4042 2024-06-28 12:34:53Z 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 placeholder 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 defined(__CYGWIN__) 309 310 #if defined(__GNUC__) 311 #define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"") 312 #else 313 #define ELFTC_VCSID(ID) /**/ 314 #endif 315 316 #endif 317 318 #endif /* ELFTC_VCSID */ 319 320 /* 321 * The placeholder below is meant to be replaced by a declaration 322 * of the downstream project's revision control macro. 323 * 324 * E.g. on NetBSD, this placeholder would be replaced by: 325 * 326 * #if !defined(__RCSID) 327 * #define __RCSID(ID) 328 * #endif 329 */ 330 #if !defined(__RCSID) 331 #define __RCSID(ID) /**/ 332 #endif /* !defined(__RCSID) */ 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 /* __APPLE__ || __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 /* 376 * Per-OS configuration. 377 * 378 * The following symbols are supported by this configuration fragment, 379 * although not all the OSes so referenced are fully supported. 380 * 381 * Cross-compilation: 382 * 383 * HAVE_NBTOOL_CONFIG_H : cross-compiling NetBSD tools on various OSes. 384 * 385 * Native compilation: 386 * 387 * __APPLE__ : compiling under Mac OS X. 388 * __DragonFly__ : compiling under DragonFlyBSD. 389 * __GLIBC__ : compiling under GNU based systems, such as GNU/kFreeBSD. 390 * __linux__ : compiling under GNU/Linux systems. 391 * __FreeBSD__ : compiling under FreeBSD. 392 * __minix : compiling under Minix3. 393 * __NetBSD__ : compiling (native) under NetBSD. 394 * __OpenBSD__ : compiling under OpenBSD. 395 */ 396 397 #if defined(HAVE_NBTOOL_CONFIG_H) 398 399 #include <sys/param.h> 400 #include <sys/endian.h> 401 402 #ifndef roundup2 403 #define roundup2 roundup 404 #endif 405 406 #define ELFTC_BYTE_ORDER _BYTE_ORDER 407 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 408 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 409 410 #define ELFTC_HAVE_MMAP 1 411 #define ELFTC_HAVE_STRMODE 1 412 413 #elif defined(__APPLE__) 414 415 #include <libkern/OSByteOrder.h> 416 #define htobe32(x) OSSwapHostToBigInt32(x) 417 #define roundup2 roundup 418 419 #define ELFTC_BYTE_ORDER _BYTE_ORDER 420 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 421 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 422 423 #define ELFTC_HAVE_MMAP 1 424 #define ELFTC_HAVE_STRMODE 1 425 426 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1 427 428 #elif defined(__DragonFly__) 429 430 #include <osreldate.h> 431 #include <sys/endian.h> 432 433 #define ELFTC_BYTE_ORDER _BYTE_ORDER 434 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 435 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 436 437 #define ELFTC_HAVE_MMAP 1 438 439 #elif defined(__GLIBC__) || defined(__linux__) 440 441 #include <endian.h> 442 443 #define ELFTC_BYTE_ORDER __BYTE_ORDER 444 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __LITTLE_ENDIAN 445 #define ELFTC_BYTE_ORDER_BIG_ENDIAN __BIG_ENDIAN 446 447 #define ELFTC_HAVE_MMAP 1 448 449 /* 450 * Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3). 451 */ 452 #define ELFTC_HAVE_STRMODE 0 453 454 /* Whether we need to supply {be,le}32dec. */ 455 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1 456 457 #define roundup2 roundup 458 459 #elif defined(__FreeBSD__) 460 461 #include <osreldate.h> 462 #include <sys/endian.h> 463 464 #define ELFTC_BYTE_ORDER _BYTE_ORDER 465 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 466 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 467 468 #define ELFTC_HAVE_MMAP 1 469 #define ELFTC_HAVE_STRMODE 1 470 #if __FreeBSD_version <= 900000 471 #define ELFTC_BROKEN_YY_NO_INPUT 1 472 #endif 473 474 #elif defined(__minix) 475 #define ELFTC_HAVE_MMAP 0 476 477 #elif defined(__NetBSD__) 478 479 #include <sys/param.h> 480 #include <sys/endian.h> 481 482 #define ELFTC_BYTE_ORDER _BYTE_ORDER 483 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 484 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 485 486 #define ELFTC_HAVE_MMAP 1 487 #define ELFTC_HAVE_STRMODE 1 488 #if __NetBSD_Version__ <= 599002100 489 /* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */ 490 /* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */ 491 # define ELFTC_BROKEN_YY_NO_INPUT 1 492 #endif 493 494 #elif defined(__OpenBSD__) 495 496 #include <sys/param.h> 497 #include <sys/endian.h> 498 499 #define ELFTC_BYTE_ORDER _BYTE_ORDER 500 #define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN 501 #define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN 502 503 #define ELFTC_HAVE_MMAP 1 504 #define ELFTC_HAVE_STRMODE 1 505 506 #define ELFTC_NEED_BYTEORDER_EXTENSIONS 1 507 #define roundup2 roundup 508 509 #endif /* __OpenBSD__ */ 510 511 #endif /* _ELFTC_H */ 512