Skip to content

Commit

Permalink
Create null mmaps if section paddr + psize exceeds filesize
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 17, 2023
1 parent 8bf7fdf commit 58cf322
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
18 changes: 9 additions & 9 deletions libr/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,11 +2764,10 @@ static RIODesc *findReusableFile(RIO *io, const char *uri, int perm) {
return arg.desc;
}

static bool io_create_mem_map(RIO *io, RBinSection *sec, ut64 at) {
static bool io_create_mem_map(RIO *io, RBinSection *sec, ut64 at, ut64 gap) {
r_return_val_if_fail (io && sec, false);

bool reused = false;
ut64 gap = sec->vsize - sec->size;
char *uri = r_str_newf ("null://%"PFMT64u, gap);
RIODesc *desc = findReusableFile (io, uri, sec->perm);
if (desc) {
Expand Down Expand Up @@ -2800,25 +2799,26 @@ static bool io_create_mem_map(RIO *io, RBinSection *sec, ut64 at) {
return true;
}




static void add_section(RCore *core, RBinSection *sec, ut64 addr, int fd) {
if (!r_io_desc_get (core->io, fd) || UT64_ADD_OVFCHK (sec->size, sec->paddr) ||
UT64_ADD_OVFCHK (sec->size, addr) || !sec->vsize) {
return;
}

ut64 size = sec->vsize;
const ut64 fdsize = r_io_fd_size (core->io, fd);
const ut64 psize = (sec->paddr < fdsize)? R_MIN (sec->size, fdsize - sec->paddr): 0LL;
// if there is some part of the section that needs to be zeroed by the loader
// we add a null map that takes care of it
if (sec->vsize > sec->size) {
if (!io_create_mem_map (core->io, sec, addr + sec->size)) {
return;
}
size = sec->size;
if (!size) {
if (sec->vsize > psize) {
size = psize;
if (!io_create_mem_map (core->io, sec, addr + psize, sec->vsize - psize) || !size) {
return;
}
}

// then we map the part of the section that comes from the physical file
char *map_name = r_str_newf ("fmap.%s", sec->name);
if (!map_name) {
Expand Down
5 changes: 3 additions & 2 deletions test/db/cmd/cmd_open
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ oon
o
EOF
EXPECT=<<EOF
4 * r-- 0x00009730 bins/mach0/mac-ls2
5 * r-- 0x00009730 bins/mach0/mac-ls2
EOF
RUN

Expand Down Expand Up @@ -127,7 +127,7 @@ o bins/mach0/mac-ls
s
obo 3
s
obo 4
obo 5
s
EOF
EXPECT=<<EOF
Expand Down Expand Up @@ -653,6 +653,7 @@ stripped false
subsys darwin
va true
-- o
4 - r-- 0x00003f14 null://16148
-- ob
- 0 3 arm-32 ba:0x00000000 sz:65536 ./bins/mach0/test-arm32
* 1 3 arm-32 ba:0x00004000 sz:65536 ./bins/mach0/test-arm32
Expand Down
3 changes: 2 additions & 1 deletion test/db/formats/elf/tiny
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NAME=ELF: corkami tiny.elf - sections
FILE=bins/elf/analysis/tiny.elf
CMDS=om
EXPECT=<<EOF
* 1 fd: 3 +0x00000000 0x00010000 - 0x0002001f r-- fmap.LOAD0
* 2 fd: 3 +0x00000000 0x00010000 - 0x0001002c r-- fmap.LOAD0
- 1 fd: 4 +0x00000000 0x0001002d - 0x0002001f r-- mmap.LOAD0
EOF
RUN

0 comments on commit 58cf322

Please sign in to comment.