Skip to content

Commit

Permalink
Fix #21412 - Implement segment iterator ##shell
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Mar 2, 2023
1 parent acadb6a commit 9523256
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
34 changes: 29 additions & 5 deletions libr/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5133,10 +5133,29 @@ static RList *foreach3list(RCore *core, char type, const char *glob) {
break;
case 'S': // "@@@S"
{
RBinObject *obj = r_bin_cur_object (core->bin);
if (obj) {
RList *sections = r_bin_get_sections (core->bin);
if (sections) {
RBinSection *sec;
r_list_foreach (obj->sections, iter, sec) {
r_list_foreach (sections, iter, sec) {
if (sec->is_segment) {
continue;
}
ut64 addr = va ? sec->vaddr: sec->paddr;
ut64 size = va ? sec->vsize: sec->size;
append_item (list, NULL, addr, size);
}
}
}
break;
case 'G': // "@@@G" // @@@SS - seGments
{
RList *sections = r_bin_get_sections (core->bin);
if (sections) {
RBinSection *sec;
r_list_foreach (sections, iter, sec) {
if (!sec->is_segment) {
continue;
}
ut64 addr = va ? sec->vaddr: sec->paddr;
ut64 size = va ? sec->vsize: sec->size;
append_item (list, NULL, addr, size);
Expand Down Expand Up @@ -5222,12 +5241,16 @@ static RList *foreach3list(RCore *core, char type, const char *glob) {
R_API int r_core_cmd_foreach3(RCore *core, const char *cmd, char *each) { // "@@@"
ForeachListItem *item;
RListIter *iter;
char ch = each[0];
if (r_str_startswith (each, "SS")) {
ch = 'G'; // @@@SS = @@@G
}
char *glob = (each[0] && each[1] == ':')
? r_str_trim_dup (each + 2): NULL;

RList *list = foreach3list (core, *each, glob);
RList *list = foreach3list (core, ch, glob);

switch (each[0]) {
switch (ch) {
case '=':
foreach_pairs (core, cmd, each + 1);
break;
Expand Down Expand Up @@ -5257,6 +5280,7 @@ R_API int r_core_cmd_foreach3(RCore *core, const char *cmd, char *each) { // "@@
case 'z':
case 'R':
case 'S':
case 'G':
case 'r':
case 'i':
{
Expand Down
2 changes: 1 addition & 1 deletion libr/core/cmd_anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -12388,7 +12388,7 @@ static int cmd_anal_all(RCore *core, const char *input) {
R_LOG_INFO ("Enable anal.types.constraint for experimental type propagation");
r_config_set_b (core->config, "anal.types.constraint", true);
if (input[2] == 'a') { // "aaaa"
R_LOG_INFO ("Reanalizing graph references to improve function count (aag)");
R_LOG_INFO ("Reanalizing graph references to improve function count (aarr)");
r_core_cmd0 (core, "aarr");
}
} else {
Expand Down
1 change: 1 addition & 0 deletions libr/core/cmd_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ static RCoreHelpMessage help_msg_at_at_at = {
"x", " @@@F:glob", "functions matching glob expression",
"x", " @@@M", "dbg.maps (See ?$?~size)",
"x", " @@@S", "sections",
"x", " @@@SS", "segments (same as @@@G)",
"x", " @@@b", "basic blocks of current function",
"x", " @@@c:cmd", "Same as @@@=`cmd`, without the backticks",
"x", " @@@e", "entries",
Expand Down
55 changes: 52 additions & 3 deletions test/db/cmd/cmd_foreach
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,55 @@ fcn.00005b10
EOF
RUN

NAME=foreach section and segment
FILE=bins/elf/ls
CMDS=<<EOF
?vi $$ @@@S
?e ---
?vi $$ @@@SS
EOF
EXPECT=<<EOF
0
680
708
740
776
976
4120
5602
5864
5976
16384
16416
92036
94208
114956
117200
135248
135256
135264
137880
138328
139264
139904
0
0
---
64
680
0
16384
94208
135248
137880
708
114956
0
135248
0
EOF
RUN

NAME=foreach iterator on imports at macho
FILE=bins/mach0/mac-ls
CMDS=<<EOF
Expand Down Expand Up @@ -62,7 +111,7 @@ CMDS=<<EOF
?e=1@@@s~?
?e=1@@@s~?
?e=1@@@S~?
?e=1@@@S~?
?e=1@@@SS~?
?e=1@@@f~?
?e=1@@@f~?
?e=1@@@m~?
Expand All @@ -75,8 +124,8 @@ EXPECT=<<EOF
hello
116
116
16
16
13
3
342
342
3
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_repeats
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CMDS=<<EOF
EOF
EXPECT=<<EOF
16
20
21
EOF
RUN

Expand Down

0 comments on commit 9523256

Please sign in to comment.