Skip to content

Commit 8b0455c

Browse files
committed
Further clean-up of E9Patch options.
1 parent e683386 commit 8b0455c

10 files changed

Lines changed: 276 additions & 190 deletions

File tree

doc/e9patch-programming-guide.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,6 @@ The `"emit"` message instructs E9Patch to emit the patched binary file.
481481
Supported values include `"binary"` (an ELF binary)
482482
`"patch"` (a binary diff) and
483483
`"patch.gz"`/`"patch.bz2"`/`"patch.xz"` (a compressed binary diff).
484-
* `"mapping_size"`: Determines how big each file mapping should be.
485-
This controls the aggressiveness of the *Physical Page Grouping*
486-
optimization.
487-
Valid values must be a power-of-two times the page size (4096),
488-
where smaller values corresponding to more aggressive space optimization.
489484

490485
#### Example:
491486

@@ -495,8 +490,7 @@ The `"emit"` message instructs E9Patch to emit the patched binary file.
495490
"params":
496491
{
497492
"filename": "a.out",
498-
"format": "binary",
499-
"mapping_size": 4096
493+
"format": "binary"
500494
},
501495
"id": 82535
502496
}

src/e9patch/e9api.cpp

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,9 @@ static void parsePatch(Binary *B, const Message &msg)
389389
*/
390390
static void parseEmit(Binary *B, const Message &msg)
391391
{
392-
size_t mapping_size = PAGE_SIZE;
393392
const char *filename = nullptr;
394393
Format format = FORMAT_BINARY;
395-
bool have_format = false, have_mapping_size = false, dup = false;
394+
bool have_format = false, dup = false;
396395
for (unsigned i = 0; i < msg.num_params; i++)
397396
{
398397
switch (msg.params[i].name)
@@ -406,25 +405,13 @@ static void parseEmit(Binary *B, const Message &msg)
406405
format = (Format)msg.params[i].value.integer;
407406
have_format = true;
408407
break;
409-
case PARAM_MAPPING_SIZE:
410-
dup = dup || have_mapping_size;
411-
mapping_size = (size_t)msg.params[i].value.integer;
412-
have_mapping_size = true;
413-
break;
414408
default:
415409
break;
416410
}
417411
}
418412
if (filename == nullptr)
419413
error("failed to parse \"emit\" message (id=%u); missing "
420414
"\"filename\" parameter", msg.id);
421-
if (mapping_size % PAGE_SIZE != 0)
422-
error("failed to parse \"emit\" message (id=%u); mapping size "
423-
"must be a multiple of the page size (%u), found %zu", msg.id,
424-
PAGE_SIZE, mapping_size);
425-
if ((mapping_size & (mapping_size - 1)) != 0)
426-
error("failed to parse \"emit\" message (id=%u); mapping size "
427-
"must be a power-of-two, found %zu", msg.id, mapping_size);
428415
if (dup)
429416
error("failed to parse \"emit\" message (id=%u); duplicate "
430417
"parameters detected");
@@ -436,12 +423,12 @@ static void parseEmit(Binary *B, const Message &msg)
436423

437424
// Create and optimize the mappings:
438425
MappingSet mappings;
439-
buildMappings(B->allocator, mapping_size, mappings);
426+
buildMappings(B->allocator, option_mem_mapping_size, mappings);
440427
optimizeMappings(mappings);
441428
putchar('\n');
442429

443430
// Create the patched binary:
444-
B->patched.size = emitElf(B, mappings, mapping_size);
431+
B->patched.size = emitElf(B, mappings, option_mem_mapping_size);
445432

446433
// Emit the result:
447434
switch (format)
@@ -634,41 +621,15 @@ static void parseTrampoline(Binary *B, const Message &msg)
634621
*/
635622
static void parseOptions(const Message &msg)
636623
{
637-
bool disable_B1 = false, disable_B2 = false,
638-
disable_T1 = false, disable_T2 = false,
639-
disable_T3 = false;
640-
bool have_disable_B1 = false, have_disable_B2 = false,
641-
have_disable_T1 = false, have_disable_T2 = false,
642-
have_disable_T3 = false;
624+
char **argv = nullptr;
643625
bool dup = false;
644626
for (unsigned i = 0; i < msg.num_params; i++)
645627
{
646628
switch (msg.params[i].name)
647629
{
648-
case PARAM_OPTION_DISABLE_B1:
649-
dup = dup || have_disable_B1;
650-
disable_B1 = msg.params[i].value.boolean;
651-
have_disable_B1 = true;
652-
break;
653-
case PARAM_OPTION_DISABLE_B2:
654-
dup = dup || have_disable_B2;
655-
disable_B2 = msg.params[i].value.boolean;
656-
have_disable_B2 = true;
657-
break;
658-
case PARAM_OPTION_DISABLE_T1:
659-
dup = dup || have_disable_T1;
660-
disable_T1 = msg.params[i].value.boolean;
661-
have_disable_T1 = true;
662-
break;
663-
case PARAM_OPTION_DISABLE_T2:
664-
dup = dup || have_disable_T2;
665-
disable_T2 = msg.params[i].value.boolean;
666-
have_disable_T2 = true;
667-
break;
668-
case PARAM_OPTION_DISABLE_T3:
669-
dup = dup || have_disable_T3;
670-
disable_T3 = msg.params[i].value.boolean;
671-
have_disable_T3 = true;
630+
case PARAM_ARGV:
631+
dup = dup || (argv != nullptr);
632+
argv = msg.params[i].value.strings;
672633
break;
673634
default:
674635
break;
@@ -677,16 +638,11 @@ static void parseOptions(const Message &msg)
677638
if (dup)
678639
error("failed to parse \"option\" message (id=%u); duplicate "
679640
"parameters detected", msg.id);
680-
if (have_disable_B1)
681-
option_tactic_B1 = !disable_B1;
682-
if (have_disable_B2)
683-
option_tactic_B2 = !disable_B2;
684-
if (have_disable_T1)
685-
option_tactic_T1 = !disable_T1;
686-
if (have_disable_T2)
687-
option_tactic_T2 = !disable_T2;
688-
if (have_disable_T3)
689-
option_tactic_T3 = !disable_T3;
641+
int argc;
642+
for (argc = 0; argv[argc] != nullptr; argc++)
643+
;
644+
parseOptions(argc, argv, /*api=*/true);
645+
delete[] argv;
690646
}
691647

692648
/*

src/e9patch/e9json.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ static bool validateParam(Method method, ParamName paramName)
452452
{
453453
case PARAM_FILENAME:
454454
case PARAM_FORMAT:
455-
case PARAM_MAPPING_SIZE:
456455
return true;
457456
default:
458457
return false;
@@ -483,11 +482,7 @@ static bool validateParam(Method method, ParamName paramName)
483482
case METHOD_OPTION:
484483
switch (paramName)
485484
{
486-
case PARAM_OPTION_DISABLE_B1:
487-
case PARAM_OPTION_DISABLE_B2:
488-
case PARAM_OPTION_DISABLE_T1:
489-
case PARAM_OPTION_DISABLE_T2:
490-
case PARAM_OPTION_DISABLE_T3:
485+
case PARAM_ARGV:
491486
return true;
492487
default:
493488
return false;
@@ -876,6 +871,33 @@ static Metadata *parseMetadata(Parser &parser)
876871
return meta;
877872
}
878873

874+
/*
875+
* Parse strings.
876+
*/
877+
static char **parseStrings(Parser &parser)
878+
{
879+
std::vector<char *> strings;
880+
881+
expectToken(parser, '[');
882+
char token = expectToken2(parser, ']', TOKEN_STRING);
883+
while (token != ']')
884+
{
885+
if (token != TOKEN_STRING)
886+
unexpectedToken(parser, "strings entry", token);
887+
strings.push_back((char *)dupString(parser.s));
888+
token = expectToken2(parser, ',', ']');
889+
if (token == ',')
890+
token = getToken(parser);
891+
}
892+
893+
char **ss = new char *[strings.size()+1];
894+
size_t i;
895+
for (i = 0; i < strings.size(); i++)
896+
ss[i] = strings[i];
897+
ss[i] = nullptr;
898+
return ss;
899+
}
900+
879901
/*
880902
* Parse a protection.
881903
*/
@@ -911,23 +933,13 @@ static void parseParams(Parser &parser, Message &msg)
911933
name = PARAM_ADDRESS;
912934
else if (strcmp(parser.s, "absolute") == 0)
913935
name = PARAM_ABSOLUTE;
936+
else if (strcmp(parser.s, "argv") == 0)
937+
name = PARAM_ARGV;
914938
break;
915939
case 'b':
916940
if (strcmp(parser.s, "bytes") == 0)
917941
name = PARAM_BYTES;
918942
break;
919-
case 'd':
920-
if (strcmp(parser.s, "disable-B1") == 0)
921-
name = PARAM_OPTION_DISABLE_B1;
922-
else if (strcmp(parser.s, "disable-B2") == 0)
923-
name = PARAM_OPTION_DISABLE_B2;
924-
else if (strcmp(parser.s, "disable-T1") == 0)
925-
name = PARAM_OPTION_DISABLE_T1;
926-
else if (strcmp(parser.s, "disable-T2") == 0)
927-
name = PARAM_OPTION_DISABLE_T2;
928-
else if (strcmp(parser.s, "disable-T3") == 0)
929-
name = PARAM_OPTION_DISABLE_T3;
930-
break;
931943
case 'f':
932944
if (strcmp(parser.s, "filename") == 0)
933945
name = PARAM_FILENAME;
@@ -953,8 +965,6 @@ static void parseParams(Parser &parser, Message &msg)
953965
case 'm':
954966
if (strcmp(parser.s, "metadata") == 0)
955967
name = PARAM_METADATA;
956-
else if (strcmp(parser.s, "mapping_size") == 0)
957-
name = PARAM_MAPPING_SIZE;
958968
else if (strcmp(parser.s, "mode") == 0)
959969
name = PARAM_MODE;
960970
else if (strcmp(parser.s, "mmap") == 0)
@@ -985,22 +995,19 @@ static void parseParams(Parser &parser, Message &msg)
985995
case PARAM_LENGTH:
986996
case PARAM_INIT:
987997
case PARAM_MMAP:
988-
case PARAM_MAPPING_SIZE:
989998
token = expectToken2(parser, TOKEN_NUMBER, TOKEN_STRING);
990999
if (token == TOKEN_NUMBER)
9911000
value.integer = (intptr_t)parser.i;
9921001
else
9931002
value.integer = stringToNumber(parser);
9941003
break;
9951004
case PARAM_ABSOLUTE:
996-
case PARAM_OPTION_DISABLE_B1:
997-
case PARAM_OPTION_DISABLE_B2:
998-
case PARAM_OPTION_DISABLE_T1:
999-
case PARAM_OPTION_DISABLE_T2:
1000-
case PARAM_OPTION_DISABLE_T3:
10011005
expectToken(parser, TOKEN_BOOL);
10021006
value.boolean = parser.b;
10031007
break;
1008+
case PARAM_ARGV:
1009+
value.strings = parseStrings(parser);
1010+
break;
10041011
case PARAM_FILENAME:
10051012
case PARAM_NAME:
10061013
case PARAM_TRAMPOLINE:

src/e9patch/e9json.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ enum ParamName
4747
PARAM_UNKNOWN,
4848
PARAM_ABSOLUTE,
4949
PARAM_ADDRESS,
50+
PARAM_ARGV,
5051
PARAM_BYTES,
5152
PARAM_FILENAME,
5253
PARAM_FORMAT,
5354
PARAM_INIT,
5455
PARAM_LENGTH,
55-
PARAM_MAPPING_SIZE,
5656
PARAM_METADATA,
5757
PARAM_MMAP,
5858
PARAM_MODE,
@@ -61,13 +61,6 @@ enum ParamName
6161
PARAM_PROTECTION,
6262
PARAM_TEMPLATE,
6363
PARAM_TRAMPOLINE,
64-
65-
// Special option names:
66-
PARAM_OPTION_DISABLE_B1,
67-
PARAM_OPTION_DISABLE_B2,
68-
PARAM_OPTION_DISABLE_T1,
69-
PARAM_OPTION_DISABLE_T2,
70-
PARAM_OPTION_DISABLE_T3,
7164
};
7265

7366
/*
@@ -90,6 +83,7 @@ union ParamValue
9083
bool boolean; // Boolean
9184
int64_t integer; // Integer
9285
const char *string; // String
86+
char **strings; // Strings
9387
Trampoline *trampoline; // Trampoline template
9488
Metadata *metadata; // Instruction metadata
9589
};

0 commit comments

Comments
 (0)