@samitouri / QOSamiQemu / commits / 1e2ccb69b1

linux-user: Use PGBRange in load_elf_image

Collect into range instead of loaddr+hiaddr. Reviewed-by: Warner Losh <imp@bsdimp.com> Reviewed-by: Helge Deller <deller@gmx.de> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

Richard Henderson committed May 29, 2026 at 15:23 UTC 1e2ccb69b1bb29fb598df490ffe56b469444a384
1 file changed +12 -12
linux-user/elfload.c
+12 -12
@@ -1278,7 +1278,8 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1278 char **pinterp_name)
1279 {
1280 g_autofree struct elf_phdr *phdr = NULL;
1281 - abi_ulong load_addr, load_bias, loaddr, hiaddr, error, align;
1281 + PGBRange range = { -1, 0 };
1282 + abi_ulong load_addr, load_bias, error, align;
1283 size_t reserve_size, align_size;
1284 int i, prot_exec;
1285 Error *err = NULL;
@@ -1318,19 +1319,18 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1319 * Find the maximum size of the image and allocate an appropriate
1320 * amount of memory to handle that. Locate the interpreter, if any.
1321 */
1321 - loaddr = -1, hiaddr = 0;
1322 align = 0;
1323 info->exec_stack = EXSTACK_DEFAULT;
1324 for (i = 0; i < ehdr->e_phnum; ++i) {
1325 struct elf_phdr *eppnt = phdr + i;
1326 if (eppnt->p_type == PT_LOAD) {
1327 abi_ulong a = eppnt->p_vaddr & TARGET_PAGE_MASK;
1328 - if (a < loaddr) {
1329 - loaddr = a;
1328 + if (a < range.lo) {
1329 + range.lo = a;
1330 }
1331 a = eppnt->p_vaddr + eppnt->p_memsz - 1;
1332 - if (a > hiaddr) {
1333 - hiaddr = a;
1332 + if (a > range.hi) {
1333 + range.hi = a;
1334 }
1335 ++info->nsegs;
1336 align |= eppnt->p_align;
@@ -1361,7 +1361,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1361 }
1362 }
1363
1364 - load_addr = loaddr;
1364 + load_addr = range.lo;
1365
1366 align = pow2ceil(align);
1367
@@ -1371,13 +1371,13 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1371 * Make sure that the low address does not conflict with
1372 * MMAP_MIN_ADDR or the QEMU application itself.
1373 */
1374 - probe_guest_base(image_name, loaddr, hiaddr);
1374 + probe_guest_base(image_name, range.lo, range.hi);
1375 } else {
1376 /*
1377 * The binary is dynamic, but we still need to
1378 * select guest_base. In this case we pass a size.
1379 */
1380 - probe_guest_base(image_name, 0, hiaddr - loaddr);
1380 + probe_guest_base(image_name, 0, range.hi - range.lo);
1381
1382 /*
1383 * Avoid collision with the loader by providing a different
@@ -1414,7 +1414,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1414 * In both cases, we will overwrite pages in this range with mappings
1415 * from the executable.
1416 */
1417 - reserve_size = (size_t)hiaddr - loaddr + 1;
1417 + reserve_size = range.hi - range.lo + 1;
1418 align_size = reserve_size;
1419
1420 if (ehdr->e_type != ET_EXEC && align > qemu_real_host_page_size()) {
@@ -1443,7 +1443,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1443 load_addr = align_addr;
1444 }
1445
1446 - load_bias = load_addr - loaddr;
1446 + load_bias = load_addr - range.lo;
1447
1448 if (elf_is_fdpic(ehdr)) {
1449 struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
@@ -1480,7 +1480,7 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1480 info->start_data = -1;
1481 info->end_data = 0;
1482 /* Usual start for brk is after all sections of the main executable. */
1483 - info->brk = TARGET_PAGE_ALIGN(hiaddr + load_bias);
1483 + info->brk = TARGET_PAGE_ALIGN(range.hi + load_bias);
1484 info->elf_flags = ehdr->e_flags;
1485 #ifdef TARGET_MIPS
1486 info->use_k0_tls = (ehdr->e_flags & EF_MIPS_MACH) == EF_MIPS_MACH_OCTEON;