1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mcpu-archive.h"
#include "mcpu-melf.h"
#include "mcpu-object-edit.h"
#include "mcpu-version.h"
typedef struct cli_action
{
mcpu_object_edit_action edit;
char *name;
char *new_name;
unsigned char *data;
} cli_action;
typedef struct dump_action
{
char *name;
char *path;
} dump_action;
static void
usage( FILE *fp, const char *name )
{
fprintf( fp,
"usage: %s [options] INPUT OUTPUT\n\n"
"Copy and safely edit LibMCPU MELF files and deterministic archives.\n"
"The output format is always MELF. Runtime-visible sections and frozen\n"
"MELF ABI3 execution metadata cannot be removed, renamed or updated.\n\n"
"Options:\n"
" -j, --only-section=NAME - copy only the named sections\n"
" -R, --remove-section=NAME - remove a safe non-runtime section\n"
" --dump-section=NAME=FILE - write section contents to FILE\n"
" --add-section=NAME=FILE - add non-ALLOC SHT_PROGBITS metadata\n"
" --update-section=NAME=FILE - replace safe non-ALLOC section contents\n"
" --rename-section=OLD=NEW - rename a safe non-ALLOC section\n"
" --help - show this help\n"
" --version - show program version\n"
" --dump-version - show version number only\n",
name );
} /* End of usage() */
static int
append_string( char ***items, size_t *count, const char *s )
{
char **q;
char *p;
if( !s || !*s )
return( -1 );
q = (char **)realloc( *items, (*count + 1) * sizeof( *q ) );
if( !q )
return( -1 );
p = strdup( s );
if( !p )
return( -1 );
*items = q;
(*items)[(*count)++] = p;
return( 0 );
} /* End of append_string() */
static void
free_strings( char **items, size_t count )
{
size_t i;
for( i = 0; i < count; ++i )
free( items[i] );
free( items );
} /* End of free_strings() */
static int
split_pair( const char *arg, char **left, char **right )
{
const char *eq = strchr( arg, '=' );
size_t n;
if( !eq || eq == arg || !eq[1] )
return( -1 );
n = (size_t)( eq - arg );
*left = (char *)malloc( n + 1 );
if( !*left )
return( -1 );
memcpy( *left, arg, n );
(*left)[n] = 0;
*right = strdup( eq + 1 );
if( !*right )
{
free( *left );
*left = NULL;
return( -1 );
}
return( 0 );
} /* End of split_pair() */
static int
append_edit_action( cli_action **actions, size_t *count,
mcpu_object_edit_action_type type,
const char *spec, int needs_file,
char *err, size_t errsz )
{
cli_action *q;
cli_action *a;
char *left = NULL;
char *right = NULL;
if( split_pair( spec, &left, &right ) )
{
snprintf( err, errsz, "expected NAME=VALUE: %s", spec );
return( -1 );
}
q = (cli_action *)realloc( *actions, (*count + 1) * sizeof( *q ) );
if( !q )
{
free( left );
free( right );
snprintf( err, errsz, "out of memory" );
return( -1 );
}
*actions = q;
a = &q[*count];
memset( a, 0, sizeof( *a ) );
a->name = left;
a->edit.type = type;
a->edit.name = a->name;
if( needs_file )
{
if( cpu_melf_readall( right, &a->data, &a->edit.size, err, errsz ) )
{
free( right );
free( a->name );
memset( a, 0, sizeof( *a ) );
return( -1 );
}
a->edit.data = a->data;
free( right );
}
else
{
a->new_name = right;
a->edit.new_name = a->new_name;
}
++*count;
return( 0 );
} /* End of append_edit_action() */
static int
append_remove_action( cli_action **actions, size_t *count,
const char *name, char *err, size_t errsz )
{
cli_action *q;
cli_action *a;
if( !name || !*name )
{
snprintf( err, errsz, "empty section name" );
return( -1 );
}
q = (cli_action *)realloc( *actions, (*count + 1) * sizeof( *q ) );
if( !q )
{
snprintf( err, errsz, "out of memory" );
return( -1 );
}
*actions = q;
a = &q[*count];
memset( a, 0, sizeof( *a ) );
a->name = strdup( name );
if( !a->name )
{
snprintf( err, errsz, "out of memory" );
return( -1 );
}
a->edit.type = MCPU_OBJECT_EDIT_REMOVE;
a->edit.name = a->name;
++*count;
return( 0 );
} /* End of append_remove_action() */
static void
free_edit_actions( cli_action *actions, size_t count )
{
size_t i;
for( i = 0; i < count; ++i )
{
free( actions[i].name );
free( actions[i].new_name );
free( actions[i].data );
}
free( actions );
} /* End of free_edit_actions() */
static int
append_dump_action( dump_action **actions, size_t *count,
const char *spec, char *err, size_t errsz )
{
dump_action *q;
char *name = NULL;
char *path = NULL;
if( split_pair( spec, &name, &path ) )
{
snprintf( err, errsz, "expected NAME=FILE: %s", spec );
return( -1 );
}
q = (dump_action *)realloc( *actions, (*count + 1) * sizeof( *q ) );
if( !q )
{
free( name );
free( path );
snprintf( err, errsz, "out of memory" );
return( -1 );
}
*actions = q;
q[*count].name = name;
q[*count].path = path;
++*count;
return( 0 );
} /* End of append_dump_action() */
static void
free_dump_actions( dump_action *actions, size_t count )
{
size_t i;
for( i = 0; i < count; ++i )
{
free( actions[i].name );
free( actions[i].path );
}
free( actions );
} /* End of free_dump_actions() */
static int
write_output( const char *input, const char *output,
const unsigned char *data, size_t size,
char *err, size_t errsz )
{
struct stat st;
if( stat( input, &st ) )
{
snprintf( err, errsz, "%s: %s", input, strerror( errno ) );
return( -1 );
}
if( cpu_melf_writeall( output, data, size, err, errsz ) )
return( -1 );
if( chmod( output, st.st_mode & 07777 ) )
{
snprintf( err, errsz, "%s: %s", output, strerror( errno ) );
unlink( output );
return( -1 );
}
return( 0 );
} /* End of write_output() */
static int
perform_dumps( const unsigned char *data, size_t size,
const dump_action *actions, size_t count,
char *err, size_t errsz )
{
size_t i;
for( i = 0; i < count; ++i )
{
unsigned char *p = NULL;
size_t n = 0;
if( mcpu_object_dump_section( data, size, actions[i].name,
&p, &n, err, errsz ) )
return( -1 );
if( cpu_melf_writeall( actions[i].path, p, n, err, errsz ) )
{
free( p );
return( -1 );
}
free( p );
}
return( 0 );
} /* End of perform_dumps() */
static int
copy_archive( const char *input, const char *output,
const mcpu_object_edit_options *options,
char *err, size_t errsz )
{
mcpu_archive archive;
struct stat st;
size_t i;
int rc = -1;
mcpu_archive_init( &archive );
if( stat( input, &st ) )
{
snprintf( err, errsz, "%s: %s", input, strerror( errno ) );
return( -1 );
}
if( mcpu_archive_read( input, &archive, err, errsz ) )
return( -1 );
for( i = 0; i < archive.member_count; ++i )
{
unsigned char *p = NULL;
size_t n = 0;
int changed = 0;
if( mcpu_object_edit_data( archive.members[i].data,
archive.members[i].size,
options, &p, &n, &changed, err, errsz ) )
goto out;
free( archive.members[i].data );
archive.members[i].data = p;
archive.members[i].size = n;
(void)changed;
}
if( mcpu_archive_write( output, &archive, archive.had_symbol_index,
err, errsz ) )
goto out;
if( chmod( output, st.st_mode & 07777 ) )
{
snprintf( err, errsz, "%s: %s", output, strerror( errno ) );
unlink( output );
goto out;
}
rc = 0;
out:
mcpu_archive_free( &archive );
return( rc );
} /* End of copy_archive() */
int
main( int argc, char **argv )
{
const char *prog = argv[0];
const char *input = NULL;
const char *output = NULL;
char **only = NULL;
size_t only_count = 0;
cli_action *actions = NULL;
size_t action_count = 0;
dump_action *dumps = NULL;
size_t dump_count = 0;
unsigned char *data = NULL;
size_t size = 0;
mcpu_object_edit_options options;
mcpu_object_edit_action *edit_actions = NULL;
char err[512];
int i;
int rc = EXIT_FAILURE;
err[0] = 0;
for( i = 1; i < argc; ++i )
{
const char *arg = argv[i];
if( !strcmp( arg, "--help" ) )
{
usage( stdout, prog );
rc = EXIT_SUCCESS;
goto out;
}
if( !strcmp( arg, "--version" ) ||
!strcmp( arg, "--dump-version" ) )
{
mcpu_version_action version_action;
mcpu_version_parse_option( arg, &version_action );
rc = mcpu_version_print(prog, version_action) == 0 ?
EXIT_SUCCESS : EXIT_FAILURE;
goto out;
}
if( !strcmp( arg, "-j" ) )
{
if( ++i >= argc || append_string( &only, &only_count, argv[i] ) )
{
snprintf( err, sizeof( err ), "option -j requires a section name" );
goto bad_usage;
}
continue;
}
if( !strncmp( arg, "-j", 2 ) && arg[2] )
{
if( append_string( &only, &only_count, arg + 2 ) )
{
snprintf( err, sizeof( err ), "out of memory or empty section name" );
goto bad_usage;
}
continue;
}
if( !strncmp( arg, "--only-section=", 15 ) )
{
if( append_string( &only, &only_count, arg + 15 ) )
{
snprintf( err, sizeof( err ), "--only-section requires a section name" );
goto bad_usage;
}
continue;
}
if( !strcmp( arg, "-R" ) )
{
if( ++i >= argc || append_remove_action( &actions, &action_count,
argv[i], err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "-R", 2 ) && arg[2] )
{
if( append_remove_action( &actions, &action_count, arg + 2,
err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "--remove-section=", 17 ) )
{
if( append_remove_action( &actions, &action_count, arg + 17,
err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "--dump-section=", 15 ) )
{
if( append_dump_action( &dumps, &dump_count, arg + 15,
err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "--add-section=", 14 ) )
{
if( append_edit_action( &actions, &action_count, MCPU_OBJECT_EDIT_ADD,
arg + 14, 1, err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "--update-section=", 17 ) )
{
if( append_edit_action( &actions, &action_count, MCPU_OBJECT_EDIT_UPDATE,
arg + 17, 1, err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( !strncmp( arg, "--rename-section=", 17 ) )
{
if( append_edit_action( &actions, &action_count, MCPU_OBJECT_EDIT_RENAME,
arg + 17, 0, err, sizeof( err ) ) )
goto bad_usage;
continue;
}
if( arg[0] == '-' && arg[1] )
{
snprintf( err, sizeof( err ), "unknown option: %s", arg );
goto bad_usage;
}
if( !input )
input = arg;
else if( !output )
output = arg;
else
{
snprintf( err, sizeof( err ), "exactly INPUT and OUTPUT are required" );
goto bad_usage;
}
}
if( !input || !output )
{
snprintf( err, sizeof( err ), "both INPUT and OUTPUT are required" );
goto bad_usage;
}
if( !strcmp( input, output ) )
{
snprintf( err, sizeof( err ), "INPUT and OUTPUT must be different files" );
goto fail;
}
if( action_count )
{
size_t k;
edit_actions = (mcpu_object_edit_action *)calloc( action_count,
sizeof( *edit_actions ) );
if( !edit_actions )
{
snprintf( err, sizeof( err ), "out of memory" );
goto fail;
}
for( k = 0; k < action_count; ++k )
edit_actions[k] = actions[k].edit;
}
memset( &options, 0, sizeof( options ) );
options.only_sections = (const char *const *)only;
options.only_count = only_count;
options.actions = edit_actions;
options.action_count = action_count;
if( cpu_melf_readall( input, &data, &size, err, sizeof( err ) ) )
goto fail;
if( size >= 8 && !memcmp( data, "!<arch>\n", 8 ) )
{
if( dump_count )
{
snprintf( err, sizeof( err ),
"--dump-section is ambiguous for archive input" );
goto fail;
}
if( !only_count && !action_count )
{
if( write_output( input, output, data, size, err, sizeof( err ) ) )
goto fail;
}
else if( copy_archive( input, output, &options, err, sizeof( err ) ) )
goto fail;
}
else
{
unsigned char *result = NULL;
size_t result_size = 0;
int changed = 0;
if( perform_dumps( data, size, dumps, dump_count, err, sizeof( err ) ) )
goto fail;
if( mcpu_object_edit_data( data, size, &options,
&result, &result_size, &changed,
err, sizeof( err ) ) )
goto fail;
if( write_output( input, output, result, result_size,
err, sizeof( err ) ) )
{
free( result );
goto fail;
}
free( result );
(void)changed;
}
rc = EXIT_SUCCESS;
goto out;
bad_usage:
fprintf( stderr, "%s: %s\n", prog, err );
usage( stderr, prog );
goto out;
fail:
fprintf( stderr, "%s: %s\n", prog, err );
out:
free( data );
free( edit_actions );
free_dump_actions( dumps, dump_count );
free_edit_actions( actions, action_count );
free_strings( only, only_count );
return( rc );
} /* End of main() */
|