blob: f31cfd03e26f4d7ad99d4ef59bd99952ae857f60 [file] [log] [blame]
Ben Cheng25b3c042013-11-20 14:45:36 -08001/* Declarations for common convenience functions.
2 Copyright (C) 2006-2011 Red Hat, Inc.
Elliott Hughes03333822015-02-18 22:19:45 -08003 This file is part of elfutils.
Ben Cheng25b3c042013-11-20 14:45:36 -08004
Elliott Hughes03333822015-02-18 22:19:45 -08005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Ben Cheng25b3c042013-11-20 14:45:36 -08007
Elliott Hughes03333822015-02-18 22:19:45 -08008 * 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
Ben Cheng25b3c042013-11-20 14:45:36 -080021 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
Elliott Hughes03333822015-02-18 22:19:45 -080025 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/>. */
Ben Cheng25b3c042013-11-20 14:45:36 -080028
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>
Elliott Hughes03333822015-02-18 22:19:45 -080037#include <unistd.h>
Ben Cheng25b3c042013-11-20 14:45:36 -080038
39#if __BYTE_ORDER == __LITTLE_ENDIAN
40# define LE32(n) (n)
Elliott Hughes03333822015-02-18 22:19:45 -080041# define LE64(n) (n)
Ben Cheng25b3c042013-11-20 14:45:36 -080042# define BE32(n) bswap_32 (n)
Elliott Hughes03333822015-02-18 22:19:45 -080043# define BE64(n) bswap_64 (n)
Ben Cheng25b3c042013-11-20 14:45:36 -080044#elif __BYTE_ORDER == __BIG_ENDIAN
45# define BE32(n) (n)
Elliott Hughes03333822015-02-18 22:19:45 -080046# define BE64(n) (n)
Ben Cheng25b3c042013-11-20 14:45:36 -080047# define LE32(n) bswap_32 (n)
Elliott Hughes03333822015-02-18 22:19:45 -080048# define LE64(n) bswap_64 (n)
Ben Cheng25b3c042013-11-20 14:45:36 -080049#else
50# error "Unknown byte order"
51#endif
52
53extern void *xmalloc (size_t) __attribute__ ((__malloc__));
54extern void *xcalloc (size_t, size_t) __attribute__ ((__malloc__));
55extern void *xrealloc (void *, size_t) __attribute__ ((__malloc__));
56
57extern char *xstrdup (const char *) __attribute__ ((__malloc__));
58extern char *xstrndup (const char *, size_t) __attribute__ ((__malloc__));
59
60
61extern uint32_t crc32 (uint32_t crc, unsigned char *buf, size_t len);
62extern 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
Elliott Hughes03333822015-02-18 22:19:45 -080072static inline ssize_t __attribute__ ((unused))
73pwrite_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
91static inline ssize_t __attribute__ ((unused))
92write_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
109static inline ssize_t __attribute__ ((unused))
110pread_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}
Ben Cheng25b3c042013-11-20 14:45:36 -0800127
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++. */
142extern 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. */
150extern const struct argp color_argp;
151
152/* Coloring mode. */
153enum color_enum
154 {
155 color_never = 0,
156 color_always,
157 color_auto
158 } __attribute__ ((packed));
159extern enum color_enum color_mode;
160
161/* Colors to use for the various components. */
162extern char *color_address;
163extern char *color_bytes;
164extern char *color_mnemonic;
165extern char *color_operand1;
166extern char *color_operand2;
167extern char *color_operand3;
168extern char *color_label;
169extern char *color_undef;
170extern char *color_undef_tls;
171extern char *color_undef_weak;
172extern char *color_symbol;
173extern char *color_tls;
174extern char *color_weak;
175
176extern const char color_off[];
177
Elliott Hughes03333822015-02-18 22:19:45 -0800178/* 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
Ben Cheng25b3c042013-11-20 14:45:36 -0800185#endif /* system.h */