125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Declarations for common convenience functions. 225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Copyright (C) 2006-2011 Red Hat, Inc. 325b3c049e70834cf33790a28643ab058b507b35cBen Cheng This file is part of Red Hat elfutils. 425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 525b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is free software; you can redistribute it and/or modify 625b3c049e70834cf33790a28643ab058b507b35cBen Cheng it under the terms of the GNU General Public License as published by the 725b3c049e70834cf33790a28643ab058b507b35cBen Cheng Free Software Foundation; version 2 of the License. 825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is distributed in the hope that it will be useful, but 1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng WITHOUT ANY WARRANTY; without even the implied warranty of 1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng General Public License for more details. 1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng You should have received a copy of the GNU General Public License along 1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng with Red Hat elfutils; if not, write to the Free Software Foundation, 1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng In addition, as a special exception, Red Hat, Inc. gives You the 1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng additional right to link the code of Red Hat elfutils with code licensed 2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng under an Open Source Initiative certified open source license 2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng (http://www.opensource.org/licenses/index.php) and to distribute linked 2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng combinations including the two. Non-GPL Code permitted under this 2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception must only link to the code of Red Hat elfutils through those 2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng well defined interfaces identified in the file named EXCEPTION found in 2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng the source code files (the "Approved Interfaces"). The files of Non-GPL 2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Code may instantiate templates or use macros or inline functions from 2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng the Approved Interfaces without causing the resulting work to be covered 2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng by the GNU General Public License. Only Red Hat, Inc. may make changes 2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng or additions to the list of Approved Interfaces. Red Hat's grant of 3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng this exception is conditioned upon your not adding any new exceptions. 3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng If you wish to add a new Approved Interface or exception, please contact 3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat. You must obey the GNU General Public License in all respects 3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng for all of the Red Hat elfutils code and other code used in conjunction 3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng with Red Hat elfutils except the Non-GPL Code covered by this exception. 3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng If you modify this file, you may extend this exception to your version 3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng of the file, but you are not obligated to do so. If you do not wish to 3725b3c049e70834cf33790a28643ab058b507b35cBen Cheng provide this exception without modification, you must delete this 3825b3c049e70834cf33790a28643ab058b507b35cBen Cheng exception statement from your version and license this file solely under 3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng the GPL without exception. 4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Red Hat elfutils is an included package of the Open Invention Network. 4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng An included package of the Open Invention Network is a package for which 4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng Open Invention Network licensees cross-license their patents. No patent 4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng license is granted, either expressly or impliedly, by designation as an 4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng included package. Should you wish to participate in the Open Invention 4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng Network licensing program, please visit www.openinventionnetwork.com 4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng <http://www.openinventionnetwork.com>. */ 4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifndef LIB_SYSTEM_H 5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define LIB_SYSTEM_H 1 5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <argp.h> 5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stddef.h> 5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdint.h> 5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <endian.h> 5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <byteswap.h> 5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#if __BYTE_ORDER == __LITTLE_ENDIAN 5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng# define LE32(n) (n) 6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng# define BE32(n) bswap_32 (n) 6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#elif __BYTE_ORDER == __BIG_ENDIAN 6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng# define BE32(n) (n) 6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng# define LE32(n) bswap_32 (n) 6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#else 6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng# error "Unknown byte order" 6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif 6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6825b3c049e70834cf33790a28643ab058b507b35cBen Chengextern void *xmalloc (size_t) __attribute__ ((__malloc__)); 6925b3c049e70834cf33790a28643ab058b507b35cBen Chengextern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__)); 7025b3c049e70834cf33790a28643ab058b507b35cBen Chengextern void *xrealloc (void *, size_t) __attribute__ ((__malloc__)); 7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7225b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *xstrdup (const char *) __attribute__ ((__malloc__)); 7325b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__)); 7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7625b3c049e70834cf33790a28643ab058b507b35cBen Chengextern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len); 7725b3c049e70834cf33790a28643ab058b507b35cBen Chengextern int crc32_file (int fd, uint32_t *resp); 7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* A special gettext function we use if the strings are too short. */ 8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define sgettext(Str) \ 8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng ({ const char *__res = strrchr (gettext (Str), '|'); \ 8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng __res ? __res + 1 : Str; }) 8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define gettext_noop(Str) Str 8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define pwrite_retry(fd, buf, len, off) \ 8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng TEMP_FAILURE_RETRY (pwrite (fd, buf, len, off)) 8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define write_retry(fd, buf, n) \ 9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng TEMP_FAILURE_RETRY (write (fd, buf, n)) 9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define pread_retry(fd, buf, len, off) \ 9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng TEMP_FAILURE_RETRY (pread (fd, buf, len, off)) 9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* We need define two variables, argp_program_version_hook and 9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng argp_program_bug_address, in all programs. argp.h declares these 9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng variables as non-const (which is correct in general). But we can 9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng do better, it is not going to change. So we want to move them into 9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng the .rodata section. Define macros to do the trick. */ 10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define ARGP_PROGRAM_VERSION_HOOK_DEF \ 10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng void (*const apvh) (FILE *, struct argp_state *) \ 10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng __asm ("argp_program_version_hook") 10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define ARGP_PROGRAM_BUG_ADDRESS_DEF \ 10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng const char *const apba__ __asm ("argp_program_bug_address") 10525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* The demangler from libstdc++. */ 10825b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *__cxa_demangle (const char *mangled_name, char *output_buffer, 10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng size_t *length, int *status); 11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Color handling. */ 11425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11525b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Command line parser. */ 11625b3c049e70834cf33790a28643ab058b507b35cBen Chengextern const struct argp color_argp; 11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Coloring mode. */ 11925b3c049e70834cf33790a28643ab058b507b35cBen Chengenum color_enum 12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng { 12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng color_never = 0, 12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng color_always, 12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng color_auto 12425b3c049e70834cf33790a28643ab058b507b35cBen Cheng } __attribute__ ((packed)); 12525b3c049e70834cf33790a28643ab058b507b35cBen Chengextern enum color_enum color_mode; 12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12725b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Colors to use for the various components. */ 12825b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_address; 12925b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_bytes; 13025b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_mnemonic; 13125b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_operand1; 13225b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_operand2; 13325b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_operand3; 13425b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_label; 13525b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_undef; 13625b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_undef_tls; 13725b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_undef_weak; 13825b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_symbol; 13925b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_tls; 14025b3c049e70834cf33790a28643ab058b507b35cBen Chengextern char *color_weak; 14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 14225b3c049e70834cf33790a28643ab058b507b35cBen Chengextern const char color_off[]; 14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif /* system.h */ 145