88#include <stdlib.h>
99#include <fcntl.h>
1010#include <unistd.h>
11+ #include <errno.h>
1112#include <sys/stat.h>
1213#include <sys/sendfile.h>
1314#include <objtool/builtin.h>
1415#include <objtool/objtool.h>
15-
16- #define ERROR (format , ...) \
17- fprintf(stderr, \
18- "error: objtool: " format "\n", \
19- ##__VA_ARGS__)
16+ #include <objtool/warn.h>
2017
2118const char * objname ;
2219
@@ -139,22 +136,22 @@ int cmd_parse_options(int argc, const char **argv, const char * const usage[])
139136static bool opts_valid (void )
140137{
141138 if (opts .mnop && !opts .mcount ) {
142- ERROR ("--mnop requires --mcount" );
139+ WARN ("--mnop requires --mcount" );
143140 return false;
144141 }
145142
146143 if (opts .noinstr && !opts .link ) {
147- ERROR ("--noinstr requires --link" );
144+ WARN ("--noinstr requires --link" );
148145 return false;
149146 }
150147
151148 if (opts .ibt && !opts .link ) {
152- ERROR ("--ibt requires --link" );
149+ WARN ("--ibt requires --link" );
153150 return false;
154151 }
155152
156153 if (opts .unret && !opts .link ) {
157- ERROR ("--unret requires --link" );
154+ WARN ("--unret requires --link" );
158155 return false;
159156 }
160157
@@ -171,7 +168,7 @@ static bool opts_valid(void)
171168 opts .static_call ||
172169 opts .uaccess ) {
173170 if (opts .dump_orc ) {
174- ERROR ("--dump can't be combined with other actions" );
171+ WARN ("--dump can't be combined with other actions" );
175172 return false;
176173 }
177174
@@ -181,7 +178,7 @@ static bool opts_valid(void)
181178 if (opts .dump_orc )
182179 return true;
183180
184- ERROR ("At least one action required" );
181+ WARN ("At least one action required" );
185182 return false;
186183}
187184
@@ -194,30 +191,30 @@ static int copy_file(const char *src, const char *dst)
194191
195192 src_fd = open (src , O_RDONLY );
196193 if (src_fd == -1 ) {
197- ERROR ("can't open '%s' for reading" , src );
194+ WARN ("can't open %s for reading: %s " , src , strerror ( errno ) );
198195 return 1 ;
199196 }
200197
201198 dst_fd = open (dst , O_WRONLY | O_CREAT | O_TRUNC , 0400 );
202199 if (dst_fd == -1 ) {
203- ERROR ("can't open '%s' for writing" , dst );
200+ WARN ("can't open %s for writing: %s " , dst , strerror ( errno ) );
204201 return 1 ;
205202 }
206203
207204 if (fstat (src_fd , & stat ) == -1 ) {
208- perror ("fstat" );
205+ WARN_GLIBC ("fstat" );
209206 return 1 ;
210207 }
211208
212209 if (fchmod (dst_fd , stat .st_mode ) == -1 ) {
213- perror ("fchmod" );
210+ WARN_GLIBC ("fchmod" );
214211 return 1 ;
215212 }
216213
217214 for (to_copy = stat .st_size ; to_copy > 0 ; to_copy -= copied ) {
218215 copied = sendfile (dst_fd , src_fd , & offset , to_copy );
219216 if (copied == -1 ) {
220- perror ("sendfile" );
217+ WARN_GLIBC ("sendfile" );
221218 return 1 ;
222219 }
223220 }
@@ -233,14 +230,14 @@ static char **save_argv(int argc, const char **argv)
233230
234231 orig_argv = calloc (argc , sizeof (char * ));
235232 if (!orig_argv ) {
236- perror ("calloc" );
233+ WARN_GLIBC ("calloc" );
237234 return NULL ;
238235 }
239236
240237 for (int i = 0 ; i < argc ; i ++ ) {
241238 orig_argv [i ] = strdup (argv [i ]);
242239 if (!orig_argv [i ]) {
243- perror ("strdup" );
240+ WARN_GLIBC ("strdup(%s)" , orig_argv [ i ] );
244241 return NULL ;
245242 }
246243 };
@@ -285,7 +282,7 @@ int objtool_run(int argc, const char **argv)
285282 goto err ;
286283
287284 if (!opts .link && has_multiple_files (file -> elf )) {
288- ERROR ("Linked object requires --link" );
285+ WARN ("Linked object requires --link" );
289286 goto err ;
290287 }
291288
@@ -313,7 +310,7 @@ int objtool_run(int argc, const char **argv)
313310 */
314311 backup = malloc (strlen (objname ) + strlen (ORIG_SUFFIX ) + 1 );
315312 if (!backup ) {
316- perror ("malloc" );
313+ WARN_GLIBC ("malloc" );
317314 return 1 ;
318315 }
319316
0 commit comments