Imported Upstream version 0.155
[platform/upstream/elfutils.git] / lib / system.h
1 /* Declarations for common convenience functions.
2    Copyright (C) 2006-2011 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11
12    or
13
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17
18    or both in parallel, as here.
19
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28
29 #ifndef LIB_SYSTEM_H
30 #define LIB_SYSTEM_H    1
31
32 #include <argp.h>
33 #include <stddef.h>
34 #include <stdint.h>
35 #include <endian.h>
36 #include <byteswap.h>
37
38 #if __BYTE_ORDER == __LITTLE_ENDIAN
39 # define LE32(n)        (n)
40 # define BE32(n)        bswap_32 (n)
41 #elif __BYTE_ORDER == __BIG_ENDIAN
42 # define BE32(n)        (n)
43 # define LE32(n)        bswap_32 (n)
44 #else
45 # error "Unknown byte order"
46 #endif
47
48 extern void *xmalloc (size_t) __attribute__ ((__malloc__));
49 extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
50 extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
51
52 extern char *xstrdup (const char *) __attribute__ ((__malloc__));
53 extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
54
55
56 extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
57 extern int crc32_file (int fd, uint32_t *resp);
58
59 /* A special gettext function we use if the strings are too short.  */
60 #define sgettext(Str) \
61   ({ const char *__res = strrchr (gettext (Str), '|');                        \
62      __res ? __res + 1 : Str; })
63
64 #define gettext_noop(Str) Str
65
66
67 #define pwrite_retry(fd, buf,  len, off) \
68   TEMP_FAILURE_RETRY (pwrite (fd, buf, len, off))
69 #define write_retry(fd, buf, n) \
70      TEMP_FAILURE_RETRY (write (fd, buf, n))
71 #define pread_retry(fd, buf,  len, off) \
72   TEMP_FAILURE_RETRY (pread (fd, buf, len, off))
73
74
75 /* We need define two variables, argp_program_version_hook and
76    argp_program_bug_address, in all programs.  argp.h declares these
77    variables as non-const (which is correct in general).  But we can
78    do better, it is not going to change.  So we want to move them into
79    the .rodata section.  Define macros to do the trick.  */
80 #define ARGP_PROGRAM_VERSION_HOOK_DEF \
81   void (*const apvh) (FILE *, struct argp_state *) \
82    __asm ("argp_program_version_hook")
83 #define ARGP_PROGRAM_BUG_ADDRESS_DEF \
84   const char *const apba__ __asm ("argp_program_bug_address")
85
86
87 /* The demangler from libstdc++.  */
88 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
89                              size_t *length, int *status);
90
91
92
93 /* Color handling.  */
94
95 /* Command line parser.  */
96 extern const struct argp color_argp;
97
98 /* Coloring mode.  */
99 enum color_enum
100   {
101     color_never = 0,
102     color_always,
103     color_auto
104   } __attribute__ ((packed));
105 extern enum color_enum color_mode;
106
107 /* Colors to use for the various components.  */
108 extern char *color_address;
109 extern char *color_bytes;
110 extern char *color_mnemonic;
111 extern char *color_operand1;
112 extern char *color_operand2;
113 extern char *color_operand3;
114 extern char *color_label;
115 extern char *color_undef;
116 extern char *color_undef_tls;
117 extern char *color_undef_weak;
118 extern char *color_symbol;
119 extern char *color_tls;
120 extern char *color_weak;
121
122 extern const char color_off[];
123
124 #endif /* system.h */