aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Baldwin <jhb@FreeBSD.org>2026-07-31 17:51:37 +0000
committerJohn Baldwin <jhb@FreeBSD.org>2026-07-31 17:51:37 +0000
commit80ee17c0285e5520e9c1db6760e84b7727778df5 (patch)
treeb1e38443cda849377c16b93cbcfe1d220d0164ba
parent8aec309852b5285cfa03424f7776a0bf432dff7a (diff)
stand: Improve error handling when loading ELF files
Previously all the 'goto out' statements after the image was loaded into memory returned success rather than an error. This is despite comments indicating some of these conditions were in fact errors, and some of these error conditions (such as missing PT_DYNAMIC) are treated as errors in the kernel linker. In addition, when failing to looking up the symbols for the linker set, those cases returned failure leaking memory (though it's clear from the original code from commit ca49b3342d1e that only the second failure was intended to be an actual error). To avoid more confusion, move the assignment of `ret` to just before the `out` label so that `goto out` always returns an error. This is a more consistent pattern with other code in the tree that tends to use labels for the error case. Restructure some other code to avoid a few bogus errors. Specifically, a symbol table is not required so don't treat lack of a symbol table as an error. Also, if the start symbol for the module metadata linker set is not found, don't treat that as an error either. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D58540
-rw-r--r--stand/common/load_elf.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/stand/common/load_elf.c b/stand/common/load_elf.c
index 97467094750c..14fc0893f088 100644
--- a/stand/common/load_elf.c
+++ b/stand/common/load_elf.c
@@ -838,7 +838,6 @@ nosyms:
if (module_verbose > MODULE_VERBOSE_SILENT)
printf("\n");
- ret = lastaddr - firstaddr;
fp->f_addr = firstaddr;
php = NULL;
@@ -901,25 +900,28 @@ nosyms:
break;
}
}
- if (ef->hashtab == NULL || ef->symtab == NULL ||
- ef->strtab == NULL || ef->strsz == 0)
- goto out;
- COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
- COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
- ef->buckets = ef->hashtab + 2;
- ef->chains = ef->buckets + ef->nbuckets;
+ if (ef->hashtab != NULL && ef->symtab != NULL &&
+ ef->strtab != NULL && ef->strsz != 0) {
+ COPYOUT(ef->hashtab, &ef->nbuckets, sizeof(ef->nbuckets));
+ COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
+ ef->buckets = ef->hashtab + 2;
+ ef->chains = ef->buckets + ef->nbuckets;
+ }
- if (__elfN(lookup_symbol)(ef, "__start_set_modmetadata_set", &sym,
- STT_NOTYPE) != 0)
- return 0;
- p_start = sym.st_value + ef->off;
- if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set", &sym,
- STT_NOTYPE) != 0)
- return 0;
- p_end = sym.st_value + ef->off;
+ /* Don't emit a warning if there is no symbol table. */
+ if (ef->buckets != 0 && __elfN(lookup_symbol)(ef,
+ "__start_set_modmetadata_set", &sym, STT_NOTYPE) == 0) {
+ p_start = sym.st_value + ef->off;
+ if (__elfN(lookup_symbol)(ef, "__stop_set_modmetadata_set",
+ &sym, STT_NOTYPE) != 0)
+ goto out;
+ p_end = sym.st_value + ef->off;
- if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) == 0)
- goto out;
+ if (__elfN(parse_modmetadata)(fp, ef, p_start, p_end) != 0)
+ goto out;
+ }
+
+ ret = lastaddr - firstaddr;
out:
if (dp)