Path: blob/main/contrib/elftoolchain/common/_elftc.h
39483 views
/*-1* Copyright (c) 2009 Joseph Koshy2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25* $Id: _elftc.h 3446 2016-05-03 01:31:17Z emaste $26*/2728/**29** Miscellaneous definitions needed by multiple components.30**/3132#ifndef _ELFTC_H33#define _ELFTC_H3435#ifndef NULL36#define NULL ((void *) 0)37#endif3839#ifndef offsetof40#define offsetof(T, M) ((int) &((T*) 0) -> M)41#endif4243/* --QUEUE-MACROS-- [[ */4445/*46* Supply macros missing from <sys/queue.h>47*/4849/*50* Copyright (c) 1991, 199351* The Regents of the University of California. All rights reserved.52*53* Redistribution and use in source and binary forms, with or without54* modification, are permitted provided that the following conditions55* are met:56* 1. Redistributions of source code must retain the above copyright57* notice, this list of conditions and the following disclaimer.58* 2. Redistributions in binary form must reproduce the above copyright59* notice, this list of conditions and the following disclaimer in the60* documentation and/or other materials provided with the distribution.61* 3. Neither the name of the University nor the names of its contributors62* may be used to endorse or promote products derived from this software63* without specific prior written permission.64*65* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND66* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE67* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE68* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE69* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL70* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS71* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)72* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT73* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY74* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF75* SUCH DAMAGE.76*/7778#ifndef LIST_FOREACH_SAFE79#define LIST_FOREACH_SAFE(var, head, field, tvar) \80for ((var) = LIST_FIRST((head)); \81(var) && ((tvar) = LIST_NEXT((var), field), 1); \82(var) = (tvar))83#endif8485#ifndef SLIST_FOREACH_SAFE86#define SLIST_FOREACH_SAFE(var, head, field, tvar) \87for ((var) = SLIST_FIRST((head)); \88(var) && ((tvar) = SLIST_NEXT((var), field), 1); \89(var) = (tvar))90#endif9192#ifndef STAILQ_CONCAT93#define STAILQ_CONCAT(head1, head2) do { \94if (!STAILQ_EMPTY((head2))) { \95*(head1)->stqh_last = (head2)->stqh_first; \96(head1)->stqh_last = (head2)->stqh_last; \97STAILQ_INIT((head2)); \98} \99} while (/*CONSTCOND*/0)100#endif101102#ifndef STAILQ_EMPTY103#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)104#endif105106#ifndef STAILQ_ENTRY107#define STAILQ_ENTRY(type) \108struct { \109struct type *stqe_next; /* next element */ \110}111#endif112113#ifndef STAILQ_FIRST114#define STAILQ_FIRST(head) ((head)->stqh_first)115#endif116117#ifndef STAILQ_HEAD118#define STAILQ_HEAD(name, type) \119struct name { \120struct type *stqh_first; /* first element */ \121struct type **stqh_last; /* addr of last next element */ \122}123#endif124125#ifndef STAILQ_HEAD_INITIALIZER126#define STAILQ_HEAD_INITIALIZER(head) \127{ NULL, &(head).stqh_first }128#endif129130#ifndef STAILQ_FOREACH131#define STAILQ_FOREACH(var, head, field) \132for ((var) = ((head)->stqh_first); \133(var); \134(var) = ((var)->field.stqe_next))135#endif136137#ifndef STAILQ_FOREACH_SAFE138#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \139for ((var) = STAILQ_FIRST((head)); \140(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \141(var) = (tvar))142#endif143144#ifndef STAILQ_INIT145#define STAILQ_INIT(head) do { \146(head)->stqh_first = NULL; \147(head)->stqh_last = &(head)->stqh_first; \148} while (/*CONSTCOND*/0)149#endif150151#ifndef STAILQ_INSERT_HEAD152#define STAILQ_INSERT_HEAD(head, elm, field) do { \153if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \154(head)->stqh_last = &(elm)->field.stqe_next; \155(head)->stqh_first = (elm); \156} while (/*CONSTCOND*/0)157#endif158159#ifndef STAILQ_INSERT_TAIL160#define STAILQ_INSERT_TAIL(head, elm, field) do { \161(elm)->field.stqe_next = NULL; \162*(head)->stqh_last = (elm); \163(head)->stqh_last = &(elm)->field.stqe_next; \164} while (/*CONSTCOND*/0)165#endif166167#ifndef STAILQ_INSERT_AFTER168#define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \169if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\170(head)->stqh_last = &(elm)->field.stqe_next; \171(listelm)->field.stqe_next = (elm); \172} while (/*CONSTCOND*/0)173#endif174175#ifndef STAILQ_LAST176#define STAILQ_LAST(head, type, field) \177(STAILQ_EMPTY((head)) ? \178NULL : ((struct type *)(void *) \179((char *)((head)->stqh_last) - offsetof(struct type, field))))180#endif181182#ifndef STAILQ_NEXT183#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)184#endif185186#ifndef STAILQ_REMOVE187#define STAILQ_REMOVE(head, elm, type, field) do { \188if ((head)->stqh_first == (elm)) { \189STAILQ_REMOVE_HEAD((head), field); \190} else { \191struct type *curelm = (head)->stqh_first; \192while (curelm->field.stqe_next != (elm)) \193curelm = curelm->field.stqe_next; \194if ((curelm->field.stqe_next = \195curelm->field.stqe_next->field.stqe_next) == NULL) \196(head)->stqh_last = &(curelm)->field.stqe_next; \197} \198} while (/*CONSTCOND*/0)199#endif200201#ifndef STAILQ_REMOVE_HEAD202#define STAILQ_REMOVE_HEAD(head, field) do { \203if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == \204NULL) \205(head)->stqh_last = &(head)->stqh_first; \206} while (/*CONSTCOND*/0)207#endif208209/*210* The STAILQ_SORT macro is adapted from Simon Tatham's O(n*log(n))211* mergesort algorithm.212*/213#ifndef STAILQ_SORT214#define STAILQ_SORT(head, type, field, cmp) do { \215STAILQ_HEAD(, type) _la, _lb; \216struct type *_p, *_q, *_e; \217int _i, _sz, _nmerges, _psz, _qsz; \218\219_sz = 1; \220do { \221_nmerges = 0; \222STAILQ_INIT(&_lb); \223while (!STAILQ_EMPTY((head))) { \224_nmerges++; \225STAILQ_INIT(&_la); \226_psz = 0; \227for (_i = 0; _i < _sz && !STAILQ_EMPTY((head)); \228_i++) { \229_e = STAILQ_FIRST((head)); \230if (_e == NULL) \231break; \232_psz++; \233STAILQ_REMOVE_HEAD((head), field); \234STAILQ_INSERT_TAIL(&_la, _e, field); \235} \236_p = STAILQ_FIRST(&_la); \237_qsz = _sz; \238_q = STAILQ_FIRST((head)); \239while (_psz > 0 || (_qsz > 0 && _q != NULL)) { \240if (_psz == 0) { \241_e = _q; \242_q = STAILQ_NEXT(_q, field); \243STAILQ_REMOVE_HEAD((head), \244field); \245_qsz--; \246} else if (_qsz == 0 || _q == NULL) { \247_e = _p; \248_p = STAILQ_NEXT(_p, field); \249STAILQ_REMOVE_HEAD(&_la, field);\250_psz--; \251} else if (cmp(_p, _q) <= 0) { \252_e = _p; \253_p = STAILQ_NEXT(_p, field); \254STAILQ_REMOVE_HEAD(&_la, field);\255_psz--; \256} else { \257_e = _q; \258_q = STAILQ_NEXT(_q, field); \259STAILQ_REMOVE_HEAD((head), \260field); \261_qsz--; \262} \263STAILQ_INSERT_TAIL(&_lb, _e, field); \264} \265} \266(head)->stqh_first = _lb.stqh_first; \267(head)->stqh_last = _lb.stqh_last; \268_sz *= 2; \269} while (_nmerges > 1); \270} while (/*CONSTCOND*/0)271#endif272273#ifndef TAILQ_FOREACH_SAFE274#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \275for ((var) = TAILQ_FIRST((head)); \276(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \277(var) = (tvar))278#endif279280/* ]] --QUEUE-MACROS-- */281282/*283* VCS Ids.284*/285286#ifndef ELFTC_VCSID287288#if defined(__DragonFly__)289#define ELFTC_VCSID(ID) __RCSID(ID)290#endif291292#if defined(__FreeBSD__)293#define ELFTC_VCSID(ID) __FBSDID(ID)294#endif295296#if defined(__APPLE__) || defined(__GLIBC__) || defined(__GNU__) || \297defined(__linux__)298#if defined(__GNUC__)299#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")300#else301#define ELFTC_VCSID(ID) /**/302#endif303#endif304305#if defined(__minix)306#if defined(__GNUC__)307#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")308#else309#define ELFTC_VCSID(ID) /**/310#endif /* __GNU__ */311#endif312313#if defined(__NetBSD__)314#define ELFTC_VCSID(ID) __RCSID(ID)315#endif316317#if defined(__OpenBSD__)318#if defined(__GNUC__)319#define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"")320#else321#define ELFTC_VCSID(ID) /**/322#endif /* __GNUC__ */323#endif324325#endif /* ELFTC_VCSID */326327/*328* Provide an equivalent for getprogname(3).329*/330331#ifndef ELFTC_GETPROGNAME332333#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \334defined(__minix) || defined(__NetBSD__)335336#include <stdlib.h>337338#define ELFTC_GETPROGNAME() getprogname()339340#endif /* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */341342343#if defined(__GLIBC__) || defined(__linux__)344#ifndef _GNU_SOURCE345/*346* GLIBC based systems have a global 'char *' pointer referencing347* the executable's name.348*/349extern const char *program_invocation_short_name;350#endif /* !_GNU_SOURCE */351352#define ELFTC_GETPROGNAME() program_invocation_short_name353354#endif /* __GLIBC__ || __linux__ */355356357#if defined(__OpenBSD__)358359extern const char *__progname;360361#define ELFTC_GETPROGNAME() __progname362363#endif /* __OpenBSD__ */364365#endif /* ELFTC_GETPROGNAME */366367368/**369** Per-OS configuration.370**/371372#if defined(__APPLE__)373374#include <libkern/OSByteOrder.h>375#define htobe32(x) OSSwapHostToBigInt32(x)376#define htole32(x) OSSwapHostToLittleInt32(x)377#ifndef roundup2378#define roundup2 roundup379#endif380381#define ELFTC_BYTE_ORDER __DARWIN_BYTE_ORDER382#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN383#define ELFTC_BYTE_ORDER_BIG_ENDIAN __DARWIN_BIG_ENDIAN384385#define ELFTC_HAVE_MMAP 1386#define ELFTC_HAVE_STRMODE 1387388#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1389#endif /* __APPLE__ */390391392#if defined(__DragonFly__)393394#include <osreldate.h>395#include <sys/endian.h>396397#define ELFTC_BYTE_ORDER _BYTE_ORDER398#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN399#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN400401#define ELFTC_HAVE_MMAP 1402403#endif404405#if defined(__GLIBC__) || defined(__linux__)406407#include <endian.h>408409#define ELFTC_BYTE_ORDER __BYTE_ORDER410#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN __LITTLE_ENDIAN411#define ELFTC_BYTE_ORDER_BIG_ENDIAN __BIG_ENDIAN412413#define ELFTC_HAVE_MMAP 1414415/*416* Debian GNU/Linux and Debian GNU/kFreeBSD do not have strmode(3).417*/418#define ELFTC_HAVE_STRMODE 0419420/* Whether we need to supply {be,le}32dec. */421#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1422423#ifndef roundup2424#define roundup2 roundup425#endif426427#endif /* __GLIBC__ || __linux__ */428429430#if defined(__FreeBSD__)431432#include <osreldate.h>433#include <sys/endian.h>434435#define ELFTC_BYTE_ORDER _BYTE_ORDER436#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN437#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN438439#define ELFTC_HAVE_MMAP 1440#define ELFTC_HAVE_STRMODE 1441#if __FreeBSD_version <= 900000442#define ELFTC_BROKEN_YY_NO_INPUT 1443#endif444#endif /* __FreeBSD__ */445446447#if defined(__minix)448#define ELFTC_HAVE_MMAP 0449#endif /* __minix */450451452#if defined(__NetBSD__)453454#include <sys/param.h>455#include <sys/endian.h>456457#define ELFTC_BYTE_ORDER _BYTE_ORDER458#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN459#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN460461#define ELFTC_HAVE_MMAP 1462#define ELFTC_HAVE_STRMODE 1463#if __NetBSD_Version__ <= 599002100464/* from src/doc/CHANGES: flex(1): Import flex-2.5.35 [christos 20091025] */465/* and 5.99.21 was from Wed Oct 21 21:28:36 2009 UTC */466# define ELFTC_BROKEN_YY_NO_INPUT 1467#endif468#endif /* __NetBSD __ */469470471#if defined(__OpenBSD__)472473#include <sys/param.h>474#include <sys/endian.h>475476#define ELFTC_BYTE_ORDER _BYTE_ORDER477#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN478#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN479480#define ELFTC_HAVE_MMAP 1481#define ELFTC_HAVE_STRMODE 1482483#define ELFTC_NEED_BYTEORDER_EXTENSIONS 1484#define roundup2 roundup485486#endif /* __OpenBSD__ */487488#endif /* _ELFTC_H */489490491