• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unistd.h>
38 
39 #if __BYTE_ORDER == __LITTLE_ENDIAN
40 # define LE32(n)	(n)
41 # define LE64(n)	(n)
42 # define BE32(n)	bswap_32 (n)
43 # define BE64(n)	bswap_64 (n)
44 #elif __BYTE_ORDER == __BIG_ENDIAN
45 # define BE32(n)	(n)
46 # define BE64(n)	(n)
47 # define LE32(n)	bswap_32 (n)
48 # define LE64(n)	bswap_64 (n)
49 #else
50 # error "Unknown byte order"
51 #endif
52 
53 extern void *xmalloc (size_t) __attribute__ ((__malloc__));
54 extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
55 extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
56 
57 extern char *xstrdup (const char *) __attribute__ ((__malloc__));
58 extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
59 
60 
61 extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
62 extern int crc32_file (int fd, uint32_t *resp);
63 
64 /* A special gettext function we use if the strings are too short.  */
65 #define sgettext(Str) \
66   ({ const char *__res = strrchr (gettext (Str), '|');			      \
67      __res ? __res + 1 : Str; })
68 
69 #define gettext_noop(Str) Str
70 
71 
72 static inline ssize_t __attribute__ ((unused))
pwrite_retry(int fd,const void * buf,size_t len,off_t off)73 pwrite_retry (int fd, const void *buf, size_t len, off_t off)
74 {
75   ssize_t recvd = 0;
76 
77   do
78     {
79       ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd,
80 						off + recvd));
81       if (ret <= 0)
82 	return ret < 0 ? ret : recvd;
83 
84       recvd += ret;
85     }
86   while ((size_t) recvd < len);
87 
88   return recvd;
89 }
90 
91 static inline ssize_t __attribute__ ((unused))
write_retry(int fd,const void * buf,size_t len)92 write_retry (int fd, const void *buf, size_t len)
93 {
94   ssize_t recvd = 0;
95 
96   do
97     {
98       ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd));
99       if (ret <= 0)
100 	return ret < 0 ? ret : recvd;
101 
102       recvd += ret;
103     }
104   while ((size_t) recvd < len);
105 
106   return recvd;
107 }
108 
109 static inline ssize_t __attribute__ ((unused))
pread_retry(int fd,void * buf,size_t len,off_t off)110 pread_retry (int fd, void *buf, size_t len, off_t off)
111 {
112   ssize_t recvd = 0;
113 
114   do
115     {
116       ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd,
117 					       off + recvd));
118       if (ret <= 0)
119 	return ret < 0 ? ret : recvd;
120 
121       recvd += ret;
122     }
123   while ((size_t) recvd < len);
124 
125   return recvd;
126 }
127 
128 
129 /* We need define two variables, argp_program_version_hook and
130    argp_program_bug_address, in all programs.  argp.h declares these
131    variables as non-const (which is correct in general).  But we can
132    do better, it is not going to change.  So we want to move them into
133    the .rodata section.  Define macros to do the trick.  */
134 #define ARGP_PROGRAM_VERSION_HOOK_DEF \
135   void (*const apvh) (FILE *, struct argp_state *) \
136    __asm ("argp_program_version_hook")
137 #define ARGP_PROGRAM_BUG_ADDRESS_DEF \
138   const char *const apba__ __asm ("argp_program_bug_address")
139 
140 
141 /* The demangler from libstdc++.  */
142 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
143 			     size_t *length, int *status);
144 
145 
146 
147 /* Color handling.  */
148 
149 /* Command line parser.  */
150 extern const struct argp color_argp;
151 
152 /* Coloring mode.  */
153 enum color_enum
154   {
155     color_never = 0,
156     color_always,
157     color_auto
158   } __attribute__ ((packed));
159 extern enum color_enum color_mode;
160 
161 /* Colors to use for the various components.  */
162 extern char *color_address;
163 extern char *color_bytes;
164 extern char *color_mnemonic;
165 extern char *color_operand1;
166 extern char *color_operand2;
167 extern char *color_operand3;
168 extern char *color_label;
169 extern char *color_undef;
170 extern char *color_undef_tls;
171 extern char *color_undef_weak;
172 extern char *color_symbol;
173 extern char *color_tls;
174 extern char *color_weak;
175 
176 extern const char color_off[];
177 
178 /* A static assertion.  This will cause a compile-time error if EXPR,
179    which must be a compile-time constant, is false.  */
180 
181 #define eu_static_assert(expr)						\
182   extern int never_defined_just_used_for_checking[(expr) ? 1 : -1]	\
183     __attribute__ ((unused))
184 
185 #endif /* system.h */
186