Skip to content

Commit

Permalink
llext: fix overlap checks for aligned regions
Browse files Browse the repository at this point in the history
The overlap checks in the section mapping code were not taking into
account the alignment padding added to the start of each region, which
could lead to false positives when checking for overlaps.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
  • Loading branch information
pillo79 committed Feb 6, 2025
1 parent adf150e commit bcd257f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions subsys/llext/llext_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,

if (region->sh_type == SHT_NULL) {
/* First section of this type, copy all info to the
* region descriptor.
* region descriptor. Clear the 'sh_info' field so
* it holds the number of bytes added at the start of
* the region for alignment corrections.
*/
memcpy(region, shdr, sizeof(*region));
region->sh_info = 0;
} else {
/* Make sure this section is compatible with the region */
if ((shdr->sh_flags & SHF_BASIC_TYPE_MASK) !=
Expand Down Expand Up @@ -384,6 +387,7 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,
}
region->sh_offset -= rest;
region->sh_size += rest;
region->sh_info += rest;
}
region->sh_addralign = shdr->sh_addralign;
}
Expand Down Expand Up @@ -428,7 +432,7 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,
/*
* Test regions that have VMA ranges for overlaps
*/
#define BOT(reg) reg->sh_addr
#define BOT(reg) (reg->sh_addr + reg->sh_info)
#define TOP(reg) (reg->sh_addr + reg->sh_size - 1)
if ((BOT(x) <= BOT(y) && TOP(x) >= BOT(y)) ||
(BOT(y) <= BOT(x) && TOP(y) >= BOT(x))) {
Expand All @@ -451,7 +455,7 @@ static int llext_map_sections(struct llext_loader *ldr, struct llext *ext,
continue;
}

#define BOT(reg) reg->sh_offset
#define BOT(reg) (reg->sh_offset + reg->sh_info)
#define TOP(reg) (reg->sh_offset + reg->sh_size - 1)
if ((BOT(x) <= BOT(y) && TOP(x) >= BOT(y)) ||
(BOT(y) <= BOT(x) && TOP(y) >= BOT(x))) {
Expand Down

0 comments on commit bcd257f

Please sign in to comment.