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