From 102a0743326a03cd1a1202ceda21e175b7d3575c Mon Sep 17 00:00:00 2001 From: hc <hc@nodka.com> Date: Tue, 20 Feb 2024 01:20:52 +0000 Subject: [PATCH] add new system file --- kernel/scripts/mod/modpost.c | 919 ++++++++++++++++++++++++++++++++------------------------- 1 files changed, 514 insertions(+), 405 deletions(-) diff --git a/kernel/scripts/mod/modpost.c b/kernel/scripts/mod/modpost.c index 6129898..8a481e1 100644 --- a/kernel/scripts/mod/modpost.c +++ b/kernel/scripts/mod/modpost.c @@ -12,11 +12,11 @@ */ #define _GNU_SOURCE +#include <elf.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <limits.h> -#include <stdbool.h> #include <errno.h> #include "modpost.h" #include "../../include/linux/license.h" @@ -29,16 +29,19 @@ static int all_versions = 0; /* If we are modposting external module set to 1 */ static int external_module = 0; -/* Warn about section mismatch in vmlinux if set to 1 */ -static int vmlinux_section_warnings = 1; +#define MODULE_SCMVERSION_SIZE 64 +static char module_scmversion[MODULE_SCMVERSION_SIZE]; /* Only warn about unresolved symbols */ static int warn_unresolved = 0; /* How a symbol is exported */ static int sec_mismatch_count = 0; -static int sec_mismatch_verbose = 1; -static int sec_mismatch_fatal = 0; +static int sec_mismatch_warn_only = true; /* ignore missing files */ static int ignore_missing_files; +/* If set to 1, only warn (instead of error) about missing ns imports */ +static int allow_missing_ns_imports; + +static bool error_occurred; enum export { export_plain, export_unused, export_gpl, @@ -51,71 +54,101 @@ #define MODULE_NAME_LEN (64 - sizeof(Elf_Addr)) -#define PRINTF __attribute__ ((format (printf, 1, 2))) - -PRINTF void fatal(const char *fmt, ...) +void __attribute__((format(printf, 2, 3))) +modpost_log(enum loglevel loglevel, const char *fmt, ...) { va_list arglist; - fprintf(stderr, "FATAL: "); + switch (loglevel) { + case LOG_WARN: + fprintf(stderr, "WARNING: "); + break; + case LOG_ERROR: + fprintf(stderr, "ERROR: "); + break; + case LOG_FATAL: + fprintf(stderr, "FATAL: "); + break; + default: /* invalid loglevel, ignore */ + break; + } + + fprintf(stderr, "modpost: "); va_start(arglist, fmt); vfprintf(stderr, fmt, arglist); va_end(arglist); - exit(1); -} - -PRINTF void warn(const char *fmt, ...) -{ - va_list arglist; - - fprintf(stderr, "WARNING: "); - - va_start(arglist, fmt); - vfprintf(stderr, fmt, arglist); - va_end(arglist); -} - -PRINTF void merror(const char *fmt, ...) -{ - va_list arglist; - - fprintf(stderr, "ERROR: "); - - va_start(arglist, fmt); - vfprintf(stderr, fmt, arglist); - va_end(arglist); -} - -static inline bool strends(const char *str, const char *postfix) -{ - if (strlen(str) < strlen(postfix)) - return false; - - return strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; -} - -static int is_vmlinux(const char *modname) -{ - const char *myname; - - myname = strrchr(modname, '/'); - if (myname) - myname++; - else - myname = modname; - - return (strcmp(myname, "vmlinux") == 0) || - (strcmp(myname, "vmlinux.o") == 0); + if (loglevel == LOG_FATAL) + exit(1); + if (loglevel == LOG_ERROR) + error_occurred = true; } void *do_nofail(void *ptr, const char *expr) { if (!ptr) - fatal("modpost: Memory allocation failure: %s.\n", expr); + fatal("Memory allocation failure: %s.\n", expr); return ptr; +} + +char *read_text_file(const char *filename) +{ + struct stat st; + size_t nbytes; + int fd; + char *buf; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + perror(filename); + exit(1); + } + + if (fstat(fd, &st) < 0) { + perror(filename); + exit(1); + } + + buf = NOFAIL(malloc(st.st_size + 1)); + + nbytes = st.st_size; + + while (nbytes) { + ssize_t bytes_read; + + bytes_read = read(fd, buf, nbytes); + if (bytes_read < 0) { + perror(filename); + exit(1); + } + + nbytes -= bytes_read; + } + buf[st.st_size] = '\0'; + + close(fd); + + return buf; +} + +char *get_line(char **stringp) +{ + char *orig = *stringp, *next; + + /* do not return the unwanted extra line at EOF */ + if (!orig || *orig == '\0') + return NULL; + + /* don't use strsep here, it is not available everywhere */ + next = strchr(orig, '\n'); + if (next) + *next++ = '\0'; + + *stringp = next; + + return orig; } /* A list of all modules we processed */ @@ -134,26 +167,19 @@ static struct module *new_module(const char *modname) { struct module *mod; - char *p; - mod = NOFAIL(malloc(sizeof(*mod))); + mod = NOFAIL(malloc(sizeof(*mod) + strlen(modname) + 1)); memset(mod, 0, sizeof(*mod)); - p = NOFAIL(strdup(modname)); - - /* strip trailing .o */ - if (strends(p, ".o")) { - p[strlen(p) - 2] = '\0'; - mod->is_dot_o = 1; - } - /* strip trailing .lto */ - if (strends(p, ".lto")) - p[strlen(p) - 4] = '\0'; /* add to list */ - mod->name = p; + strcpy(mod->name, modname); + mod->is_vmlinux = (strcmp(modname, "vmlinux") == 0); mod->gpl_compatible = -1; mod->next = modules; modules = mod; + + if (mod->is_vmlinux) + have_vmlinux = 1; return mod; } @@ -168,13 +194,11 @@ struct module *module; unsigned int crc; int crc_valid; + char *namespace; unsigned int weak:1; - unsigned int vmlinux:1; /* 1 if symbol is defined in vmlinux */ - unsigned int kernel:1; /* 1 if symbol is from kernel - * (only for external modules) **/ - unsigned int preloaded:1; /* 1 if symbol from Module.symvers, or crc */ + unsigned int is_static:1; /* 1 if symbol is not global */ enum export export; /* Type of export */ - char name[0]; + char name[]; }; static struct symbol *symbolhash[SYMBOL_HASH_SIZE]; @@ -205,6 +229,7 @@ strcpy(s->name, name); s->weak = weak; s->next = next; + s->is_static = 1; return s; } @@ -213,13 +238,11 @@ enum export export) { unsigned int hash; - struct symbol *new; hash = tdb_hash(name) % SYMBOL_HASH_SIZE; - new = symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); - new->module = module; - new->export = export; - return new; + symbolhash[hash] = alloc_symbol(name, 0, symbolhash[hash]); + + return symbolhash[hash]; } static struct symbol *find_symbol(const char *name) @@ -235,6 +258,35 @@ return s; } return NULL; +} + +static bool contains_namespace(struct namespace_list *list, + const char *namespace) +{ + for (; list; list = list->next) + if (!strcmp(list->namespace, namespace)) + return true; + + return false; +} + +static void add_namespace(struct namespace_list **list, const char *namespace) +{ + struct namespace_list *ns_entry; + + if (!contains_namespace(*list, namespace)) { + ns_entry = NOFAIL(malloc(sizeof(struct namespace_list) + + strlen(namespace) + 1)); + strcpy(ns_entry->namespace, namespace); + ns_entry->next = *list; + *list = ns_entry; + } +} + +static bool module_imports_namespace(struct module *module, + const char *namespace) +{ + return contains_namespace(module->imported_namespaces, namespace); } static const struct { @@ -268,16 +320,32 @@ return export_unknown; } -static const char *sech_name(struct elf_info *elf, Elf_Shdr *sechdr) +static void *sym_get_data_by_offset(const struct elf_info *info, + unsigned int secindex, unsigned long offset) { - return (void *)elf->hdr + - elf->sechdrs[elf->secindex_strings].sh_offset + - sechdr->sh_name; + Elf_Shdr *sechdr = &info->sechdrs[secindex]; + + if (info->hdr->e_type != ET_REL) + offset -= sechdr->sh_addr; + + return (void *)info->hdr + sechdr->sh_offset + offset; } -static const char *sec_name(struct elf_info *elf, int secindex) +static void *sym_get_data(const struct elf_info *info, const Elf_Sym *sym) { - return sech_name(elf, &elf->sechdrs[secindex]); + return sym_get_data_by_offset(info, get_secindex(info, sym), + sym->st_value); +} + +static const char *sech_name(const struct elf_info *info, Elf_Shdr *sechdr) +{ + return sym_get_data_by_offset(info, info->secindex_strings, + sechdr->sh_name); +} + +static const char *sec_name(const struct elf_info *info, int secindex) +{ + return sech_name(info, &info->sechdrs[secindex]); } #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0) @@ -316,6 +384,32 @@ return export_unknown; } +static const char *namespace_from_kstrtabns(const struct elf_info *info, + const Elf_Sym *sym) +{ + const char *value = sym_get_data(info, sym); + return value[0] ? value : NULL; +} + +static void sym_update_namespace(const char *symname, const char *namespace) +{ + struct symbol *s = find_symbol(symname); + + /* + * That symbol should have been created earlier and thus this is + * actually an assertion. + */ + if (!s) { + error("Could not update namespace(%s) for symbol %s\n", + namespace, symname); + return; + } + + free(s->namespace); + s->namespace = + namespace && namespace[0] ? NOFAIL(strdup(namespace)) : NULL; +} + /** * Add an exported symbol - it may have already been added without a * CRC, in this case just update the CRC @@ -327,39 +421,34 @@ if (!s) { s = new_symbol(name, mod, export); - } else { - if (!s->preloaded) { - warn("%s: '%s' exported twice. Previous export " - "was in %s%s\n", mod->name, name, - s->module->name, - is_vmlinux(s->module->name) ?"":".ko"); - } else { - /* In case Module.symvers was out of date */ - s->module = mod; - } + } else if (!external_module || s->module->is_vmlinux || + s->module == mod) { + fatal("%s: '%s' exported twice. Previous export was in %s%s\n", + mod->name, name, s->module->name, + s->module->is_vmlinux ? "" : ".ko"); } - s->preloaded = 0; - s->vmlinux = is_vmlinux(mod->name); - s->kernel = 0; + + s->module = mod; s->export = export; return s; } -static void sym_update_crc(const char *name, struct module *mod, - unsigned int crc, enum export export) +static void sym_set_crc(const char *name, unsigned int crc) { struct symbol *s = find_symbol(name); - if (!s) { - s = new_symbol(name, mod, export); - /* Don't complain when we find it later. */ - s->preloaded = 1; - } + /* + * Ignore stand-alone __crc_*, which might be auto-generated symbols + * such as __*_veneer in ARM ELF. + */ + if (!s) + return; + s->crc = crc; s->crc_valid = 1; } -void *grab_file(const char *filename, unsigned long *size) +static void *grab_file(const char *filename, size_t *size) { struct stat st; void *map = MAP_FAILED; @@ -381,41 +470,7 @@ return map; } -/** - * Return a copy of the next line in a mmap'ed file. - * spaces in the beginning of the line is trimmed away. - * Return a pointer to a static buffer. - **/ -char *get_next_line(unsigned long *pos, void *file, unsigned long size) -{ - static char line[4096]; - int skip = 1; - size_t len = 0; - signed char *p = (signed char *)file + *pos; - char *s = line; - - for (; *pos < size ; (*pos)++) { - if (skip && isspace(*p)) { - p++; - continue; - } - skip = 0; - if (*p != '\n' && (*pos < size)) { - len++; - *s++ = *p++; - if (len > 4095) - break; /* Too long, stop */ - } else { - /* End of string */ - *s = '\0'; - return line; - } - } - /* End of buffer */ - return NULL; -} - -void release_file(void *file, unsigned long size) +static void release_file(void *file, size_t size) { munmap(file, size); } @@ -471,9 +526,8 @@ /* Check if file offset is correct */ if (hdr->e_shoff > info->size) { - fatal("section header offset=%lu in file '%s' is bigger than " - "filesize=%lu\n", (unsigned long)hdr->e_shoff, - filename, info->size); + fatal("section header offset=%lu in file '%s' is bigger than filesize=%zu\n", + (unsigned long)hdr->e_shoff, filename, info->size); return 0; } @@ -618,36 +672,40 @@ return 0; } -static void handle_modversions(struct module *mod, struct elf_info *info, - Elf_Sym *sym, const char *symname) +static void handle_modversion(const struct module *mod, + const struct elf_info *info, + const Elf_Sym *sym, const char *symname) { unsigned int crc; - enum export export; - bool is_crc = false; - if ((!is_vmlinux(mod->name) || mod->is_dot_o) && - strstarts(symname, "__ksymtab")) + if (sym->st_shndx == SHN_UNDEF) { + warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n", + symname, mod->name, mod->is_vmlinux ? "" : ".ko"); + return; + } + + if (sym->st_shndx == SHN_ABS) { + crc = sym->st_value; + } else { + unsigned int *crcp; + + /* symbol points to the CRC in the ELF object */ + crcp = sym_get_data(info, sym); + crc = TO_NATIVE(*crcp); + } + sym_set_crc(symname, crc); +} + +static void handle_symbol(struct module *mod, struct elf_info *info, + const Elf_Sym *sym, const char *symname) +{ + enum export export; + const char *name; + + if (strstarts(symname, "__ksymtab")) export = export_from_secname(info, get_secindex(info, sym)); else export = export_from_sec(info, get_secindex(info, sym)); - - /* CRC'd symbol */ - if (strstarts(symname, "__crc_")) { - is_crc = true; - crc = (unsigned int) sym->st_value; - if (sym->st_shndx != SHN_UNDEF && sym->st_shndx != SHN_ABS) { - unsigned int *crcp; - - /* symbol points to the CRC in the ELF object */ - crcp = (void *)info->hdr + sym->st_value + - info->sechdrs[sym->st_shndx].sh_offset - - (info->hdr->e_type != ET_REL ? - info->sechdrs[sym->st_shndx].sh_addr : 0); - crc = TO_NATIVE(*crcp); - } - sym_update_crc(symname + strlen("__crc_"), mod, crc, - export); - } switch (sym->st_shndx) { case SHN_COMMON: @@ -663,12 +721,6 @@ break; if (ignore_undef_symbol(info, symname)) break; -/* cope with newer glibc (2.3.4 or higher) STT_ definition in elf.h */ -#if defined(STT_REGISTER) || defined(STT_SPARC_REGISTER) -/* add compatibility with older glibc */ -#ifndef STT_SPARC_REGISTER -#define STT_SPARC_REGISTER STT_REGISTER -#endif if (info->hdr->e_machine == EM_SPARC || info->hdr->e_machine == EM_SPARCV9) { /* Ignore register directives. */ @@ -681,13 +733,7 @@ symname = munged; } } -#endif - if (is_crc) { - const char *e = is_vmlinux(mod->name) ?"":".ko"; - warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n", - symname + strlen("__crc_"), mod->name, e); - } mod->unres = alloc_symbol(symname, ELF_ST_BIND(sym->st_info) == STB_WEAK, mod->unres); @@ -695,8 +741,8 @@ default: /* All exported symbols */ if (strstarts(symname, "__ksymtab_")) { - sym_add_exported(symname + strlen("__ksymtab_"), mod, - export); + name = symname + strlen("__ksymtab_"); + sym_add_exported(name, mod, export); } if (strcmp(symname, "init_module") == 0) mod->has_init = 1; @@ -799,9 +845,9 @@ /* "*foo*" */ if (*p == '*' && *endp == '*') { - char *here, *bare = strndup(p + 1, strlen(p) - 2); + char *bare = NOFAIL(strndup(p + 1, strlen(p) - 2)); + char *here = strstr(sym, bare); - here = strstr(sym, bare); free(bare); if (here != NULL) return 1; @@ -945,7 +991,6 @@ static const char *const linker_symbols[] = { "__init_begin", "_sinittext", "_einittext", NULL }; static const char *const optim_symbols[] = { "*.constprop.*", NULL }; -static const char *const cfi_symbols[] = { "*.cfi", NULL }; enum mismatch { TEXT_TO_ANY_INIT, @@ -1070,7 +1115,7 @@ }, /* Do not export init/exit functions or data */ { - .fromsec = { "__ksymtab*", NULL }, + .fromsec = { "___ksymtab*", NULL }, .bad_tosec = { INIT_SECTIONS, EXIT_SECTIONS, NULL }, .mismatch = EXPORT_TO_INIT_EXIT, .symbol_white_list = { DEFAULT_SYMBOL_WHITE_LIST, NULL }, @@ -1175,17 +1220,6 @@ * whitelisting, which relies on pattern-matching against symbol * names to work. (One situation where gcc can autogenerate ELF * local symbols is when "-fsection-anchors" is used.) - * - * Pattern 7: - * With CONFIG_CFI_CLANG, clang appends .cfi to all indirectly called - * functions and creates a function stub with the original name. This - * stub is always placed in .text, even if the actual function with the - * .cfi postfix is in .init.text or .exit.text. - * This pattern is identified by - * tosec = init or exit section - * fromsec = text section - * tosym = *.cfi - * **/ static int secref_whitelist(const struct sectioncheck *mismatch, const char *fromsec, const char *fromsym, @@ -1228,18 +1262,13 @@ if (strstarts(fromsym, ".L")) return 0; - /* Check for pattern 7 */ - if (match(fromsec, text_sections) && - match(tosec, init_exit_sections) && - match(tosym, cfi_symbols)) - return 0; - return 1; } static inline int is_arm_mapping_symbol(const char *str) { - return str[0] == '$' && strchr("axtd", str[1]) + return str[0] == '$' && + (str[1] == 'a' || str[1] == 'd' || str[1] == 't' || str[1] == 'x') && (str[2] == '\0' || str[2] == '.'); } @@ -1280,6 +1309,10 @@ if (relsym->st_name != 0) return relsym; + /* + * Strive to find a better symbol name, but the resulting name may not + * match the symbol referenced in the original code. + */ relsym_secindex = get_secindex(elf, relsym); for (sym = elf->symtab_start; sym < elf->symtab_stop; sym++) { if (get_secindex(elf, sym) != relsym_secindex) @@ -1427,8 +1460,6 @@ char *prl_to; sec_mismatch_count++; - if (!sec_mismatch_verbose) - return; get_pretty_name(from_is_func, &from, &from_p); get_pretty_name(to_is_func, &to, &to_p); @@ -1586,7 +1617,7 @@ static int is_executable_section(struct elf_info* elf, unsigned int section_index) { - if (section_index > elf->num_sections) + if (section_index >= elf->num_sections) fatal("section_index is outside elf->num_sections!\n"); return ((elf->sechdrs[section_index].sh_flags & SHF_EXECINSTR) == SHF_EXECINSTR); @@ -1676,9 +1707,7 @@ sec_mismatch_count++; - if (sec_mismatch_verbose) - report_extable_warnings(modname, elf, mismatch, r, sym, - fromsec, tosec); + report_extable_warnings(modname, elf, mismatch, r, sym, fromsec, tosec); if (match(tosec, mismatch->bad_tosec)) fatal("The relocation at %s+0x%lx references\n" @@ -1724,11 +1753,7 @@ static unsigned int *reloc_location(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { - Elf_Shdr *sechdrs = elf->sechdrs; - int section = sechdr->sh_info; - - return (void *)elf->hdr + sechdrs[section].sh_offset + - r->r_offset; + return sym_get_data_by_offset(elf, sechdr->sh_info, r->r_offset); } static int addend_386_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) @@ -1767,19 +1792,33 @@ #define R_ARM_THM_JUMP19 51 #endif +static int32_t sign_extend32(int32_t value, int index) +{ + uint8_t shift = 31 - index; + + return (int32_t)(value << shift) >> shift; +} + static int addend_arm_rel(struct elf_info *elf, Elf_Shdr *sechdr, Elf_Rela *r) { unsigned int r_typ = ELF_R_TYPE(r->r_info); + Elf_Sym *sym = elf->symtab_start + ELF_R_SYM(r->r_info); + void *loc = reloc_location(elf, sechdr, r); + uint32_t inst; + int32_t offset; switch (r_typ) { case R_ARM_ABS32: - /* From ARM ABI: (S + A) | T */ - r->r_addend = (int)(long) - (elf->symtab_start + ELF_R_SYM(r->r_info)); + inst = TO_NATIVE(*(uint32_t *)loc); + r->r_addend = inst + sym->st_value; break; case R_ARM_PC24: case R_ARM_CALL: case R_ARM_JUMP24: + inst = TO_NATIVE(*(uint32_t *)loc); + offset = sign_extend32((inst & 0x00ffffff) << 2, 25); + r->r_addend = offset + sym->st_value + 8; + break; case R_ARM_THM_CALL: case R_ARM_THM_JUMP24: case R_ARM_THM_JUMP19: @@ -1958,7 +1997,7 @@ if (n && s[n]) { size_t m = strspn(s + n + 1, "0123456789"); - if (m && (s[n + m] == '.' || s[n + m] == 0)) + if (m && (s[n + m + 1] == '.' || s[n + m + 1] == 0)) s[n] = 0; /* strip trailing .lto */ @@ -1973,6 +2012,7 @@ const char *symname; char *version; char *license; + char *namespace; struct module *mod; struct elf_info info = { }; Elf_Sym *sym; @@ -1980,46 +2020,84 @@ if (!parse_elf(&info, modname)) return; - mod = new_module(modname); + { + char *tmp; - /* When there's no vmlinux, don't print warnings about - * unresolved symbols (since there'll be too many ;) */ - if (is_vmlinux(modname)) { - have_vmlinux = 1; - mod->skip = 1; + /* strip trailing .o */ + tmp = NOFAIL(strdup(modname)); + tmp[strlen(tmp) - 2] = '\0'; + /* strip trailing .lto */ + if (strends(tmp, ".lto")) + tmp[strlen(tmp) - 4] = '\0'; + mod = new_module(tmp); + free(tmp); } - license = get_modinfo(&info, "license"); - if (!license && !is_vmlinux(modname)) - warn("modpost: missing MODULE_LICENSE() in %s\n" - "see include/linux/module.h for " - "more information\n", modname); - while (license) { - if (license_is_gpl_compatible(license)) - mod->gpl_compatible = 1; - else { - mod->gpl_compatible = 0; - break; + if (!mod->is_vmlinux) { + license = get_modinfo(&info, "license"); + if (!license) + error("missing MODULE_LICENSE() in %s\n", modname); + while (license) { + if (license_is_gpl_compatible(license)) + mod->gpl_compatible = 1; + else { + mod->gpl_compatible = 0; + break; + } + license = get_next_modinfo(&info, "license", license); } - license = get_next_modinfo(&info, "license", license); + + namespace = get_modinfo(&info, "import_ns"); + while (namespace) { + add_namespace(&mod->imported_namespaces, namespace); + namespace = get_next_modinfo(&info, "import_ns", + namespace); + } } for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { symname = remove_dot(info.strtab + sym->st_name); - handle_modversions(mod, &info, sym, symname); + handle_symbol(mod, &info, sym, symname); handle_moddevtable(mod, &info, sym, symname); } - if (!is_vmlinux(modname) || vmlinux_section_warnings) - check_sec_ref(mod, modname, &info); - version = get_modinfo(&info, "version"); - if (version) - maybe_frob_rcs_version(modname, version, info.modinfo, - version - (char *)info.hdr); - if (version || (all_versions && !is_vmlinux(modname))) - get_src_version(modname, mod->srcversion, - sizeof(mod->srcversion)-1); + for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { + symname = remove_dot(info.strtab + sym->st_name); + + /* Apply symbol namespaces from __kstrtabns_<symbol> entries. */ + if (strstarts(symname, "__kstrtabns_")) + sym_update_namespace(symname + strlen("__kstrtabns_"), + namespace_from_kstrtabns(&info, + sym)); + + if (strstarts(symname, "__crc_")) + handle_modversion(mod, &info, sym, + symname + strlen("__crc_")); + } + + // check for static EXPORT_SYMBOL_* functions && global vars + for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { + unsigned char bind = ELF_ST_BIND(sym->st_info); + + if (bind == STB_GLOBAL || bind == STB_WEAK) { + struct symbol *s = + find_symbol(remove_dot(info.strtab + + sym->st_name)); + + if (s) + s->is_static = 0; + } + } + + check_sec_ref(mod, modname, &info); + + if (!mod->is_vmlinux) { + version = get_modinfo(&info, "version"); + if (version || all_versions) + get_src_version(modname, mod->srcversion, + sizeof(mod->srcversion) - 1); + } parse_elf_finish(&info); @@ -2083,20 +2161,18 @@ static void check_for_gpl_usage(enum export exp, const char *m, const char *s) { - const char *e = is_vmlinux(m) ?"":".ko"; - switch (exp) { case export_gpl: - fatal("modpost: GPL-incompatible module %s%s " - "uses GPL-only symbol '%s'\n", m, e, s); + error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n", + m, s); break; case export_unused_gpl: - fatal("modpost: GPL-incompatible module %s%s " - "uses GPL-only symbol marked UNUSED '%s'\n", m, e, s); + error("GPL-incompatible module %s.ko uses GPL-only symbol marked UNUSED '%s'\n", + m, s); break; case export_gpl_future: - warn("modpost: GPL-incompatible module %s%s " - "uses future GPL-only symbol '%s'\n", m, e, s); + warn("GPL-incompatible module %s.ko uses future GPL-only symbol '%s'\n", + m, s); break; case export_plain: case export_unused: @@ -2108,13 +2184,11 @@ static void check_for_unused(enum export exp, const char *m, const char *s) { - const char *e = is_vmlinux(m) ?"":".ko"; - switch (exp) { case export_unused: case export_unused_gpl: - warn("modpost: module %s%s " - "uses symbol '%s' marked UNUSED\n", m, e, s); + warn("module %s.ko uses symbol '%s' marked UNUSED\n", + m, s); break; default: /* ignore */ @@ -2129,20 +2203,34 @@ for (s = mod->unres; s; s = s->next) { const char *basename; exp = find_symbol(s->name); - if (!exp || exp->module == mod) + if (!exp || exp->module == mod) { + if (have_vmlinux && !s->weak) + modpost_log(warn_unresolved ? LOG_WARN : LOG_ERROR, + "\"%s\" [%s.ko] undefined!\n", + s->name, mod->name); continue; + } basename = strrchr(mod->name, '/'); if (basename) basename++; else basename = mod->name; + + if (exp->namespace && + !module_imports_namespace(mod, exp->namespace)) { + modpost_log(allow_missing_ns_imports ? LOG_WARN : LOG_ERROR, + "module %s uses symbol %s from namespace %s, but does not import it.\n", + basename, exp->name, exp->namespace); + add_namespace(&mod->missing_namespaces, exp->namespace); + } + if (!mod->gpl_compatible) check_for_gpl_usage(exp->export, basename, exp->name); check_for_unused(exp->export, basename, exp->name); } } -static int check_modname_len(struct module *mod) +static void check_modname_len(struct module *mod) { const char *mod_name; @@ -2151,12 +2239,8 @@ mod_name = mod->name; else mod_name++; - if (strlen(mod_name) >= MODULE_NAME_LEN) { - merror("module name is too long [%s.ko]\n", mod->name); - return 1; - } - - return 0; + if (strlen(mod_name) >= MODULE_NAME_LEN) + error("module name is too long [%s.ko]\n", mod->name); } /** @@ -2169,6 +2253,7 @@ * Include build-salt.h after module.h in order to * inherit the definitions. */ + buf_printf(b, "#define INCLUDE_VERMAGIC\n"); buf_printf(b, "#include <linux/build-salt.h>\n"); buf_printf(b, "#include <linux/vermagic.h>\n"); buf_printf(b, "#include <linux/compiler.h>\n"); @@ -2179,7 +2264,7 @@ buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n"); buf_printf(b, "\n"); buf_printf(b, "__visible struct module __this_module\n"); - buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); + buf_printf(b, "__section(\".gnu.linkonce.this_module\") = {\n"); buf_printf(b, "\t.name = KBUILD_MODNAME,\n"); if (mod->has_init) buf_printf(b, "\t.init = init_module,\n"); @@ -2195,6 +2280,20 @@ { if (is_intree) buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n"); +} + +/** + * add_scmversion() - Adds the MODULE_INFO macro for the scmversion. + * @b: Buffer to append to. + * + * This function fills in the module attribute `scmversion` for the kernel + * module. This is useful for determining a given module's SCM version on + * device via /sys/modules/<module>/scmversion and/or using the modinfo tool. + */ +static void add_scmversion(struct buffer *b) +{ + if (module_scmversion[0] != '\0') + buf_printf(b, "\nMODULE_INFO(scmversion, \"%s\");\n", module_scmversion); } /* Cannot check for assembler */ @@ -2214,38 +2313,25 @@ /** * Record CRCs for unresolved symbols **/ -static int add_versions(struct buffer *b, struct module *mod) +static void add_versions(struct buffer *b, struct module *mod) { struct symbol *s, *exp; - int err = 0; for (s = mod->unres; s; s = s->next) { exp = find_symbol(s->name); - if (!exp || exp->module == mod) { - if (have_vmlinux && !s->weak) { - if (warn_unresolved) { - warn("\"%s\" [%s.ko] undefined!\n", - s->name, mod->name); - } else { - merror("\"%s\" [%s.ko] undefined!\n", - s->name, mod->name); - err = 1; - } - } + if (!exp || exp->module == mod) continue; - } s->module = exp->module; s->crc_valid = exp->crc_valid; s->crc = exp->crc; } if (!modversions) - return err; + return; buf_printf(b, "\n"); buf_printf(b, "static const struct modversion_info ____versions[]\n"); - buf_printf(b, "__used\n"); - buf_printf(b, "__attribute__((section(\"__versions\"))) = {\n"); + buf_printf(b, "__used __section(\"__versions\") = {\n"); for (s = mod->unres; s; s = s->next) { if (!s->module) @@ -2256,9 +2342,8 @@ continue; } if (strlen(s->name) >= MODULE_NAME_LEN) { - merror("too long symbol \"%s\" [%s.ko]\n", - s->name, mod->name); - err = 1; + error("too long symbol \"%s\" [%s.ko]\n", + s->name, mod->name); break; } buf_printf(b, "\t{ %#8x, \"%s\" },\n", @@ -2266,25 +2351,20 @@ } buf_printf(b, "};\n"); - - return err; } -static void add_depends(struct buffer *b, struct module *mod, - struct module *modules) +static void add_depends(struct buffer *b, struct module *mod) { struct symbol *s; - struct module *m; int first = 1; - for (m = modules; m; m = m->next) - m->seen = is_vmlinux(m->name); + /* Clear ->seen flag of modules that own symbols needed by this. */ + for (s = mod->unres; s; s = s->next) + if (s->module) + s->module->seen = s->module->is_vmlinux; buf_printf(b, "\n"); - buf_printf(b, "static const char __module_depends[]\n"); - buf_printf(b, "__used\n"); - buf_printf(b, "__attribute__((section(\".modinfo\"))) =\n"); - buf_printf(b, "\"depends="); + buf_printf(b, "MODULE_INFO(depends, \""); for (s = mod->unres; s; s = s->next) { const char *p; if (!s->module) @@ -2302,7 +2382,7 @@ buf_printf(b, "%s%s", first ? "" : ",", p); first = 0; } - buf_printf(b, "\";\n"); + buf_printf(b, "\");\n"); } static void add_srcversion(struct buffer *b, struct module *mod) @@ -2311,6 +2391,25 @@ buf_printf(b, "\n"); buf_printf(b, "MODULE_INFO(srcversion, \"%s\");\n", mod->srcversion); + } +} + +static void write_buf(struct buffer *b, const char *fname) +{ + FILE *file; + + file = fopen(fname, "w"); + if (!file) { + perror(fname); + exit(1); + } + if (fwrite(b->p, 1, b->pos, file) != b->pos) { + perror(fname); + exit(1); + } + if (fclose(file) != 0) { + perror(fname); + exit(1); } } @@ -2346,33 +2445,25 @@ close_write: fclose(file); write: - file = fopen(fname, "w"); - if (!file) { - perror(fname); - exit(1); - } - if (fwrite(b->p, 1, b->pos, file) != b->pos) { - perror(fname); - exit(1); - } - fclose(file); + write_buf(b, fname); } /* parse Module.symvers file. line format: - * 0x12345678<tab>symbol<tab>module[[<tab>export]<tab>something] + * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace **/ -static void read_dump(const char *fname, unsigned int kernel) +static void read_dump(const char *fname) { - unsigned long size, pos = 0; - void *file = grab_file(fname, &size); - char *line; + char *buf, *pos, *line; - if (!file) + buf = read_text_file(fname); + if (!buf) /* No symbol versions, silently ignore */ return; - while ((line = get_next_line(&pos, file, size))) { - char *symname, *modname, *d, *export, *end; + pos = buf; + + while ((line = get_line(&pos))) { + char *symname, *namespace, *modname, *d, *export; unsigned int crc; struct module *mod; struct symbol *s; @@ -2383,68 +2474,83 @@ if (!(modname = strchr(symname, '\t'))) goto fail; *modname++ = '\0'; - if ((export = strchr(modname, '\t')) != NULL) - *export++ = '\0'; - if (export && ((end = strchr(export, '\t')) != NULL)) - *end = '\0'; + if (!(export = strchr(modname, '\t'))) + goto fail; + *export++ = '\0'; + if (!(namespace = strchr(export, '\t'))) + goto fail; + *namespace++ = '\0'; + crc = strtoul(line, &d, 16); if (*symname == '\0' || *modname == '\0' || *d != '\0') goto fail; mod = find_module(modname); if (!mod) { - if (is_vmlinux(modname)) - have_vmlinux = 1; mod = new_module(modname); - mod->skip = 1; + mod->from_dump = 1; } s = sym_add_exported(symname, mod, export_no(export)); - s->kernel = kernel; - s->preloaded = 1; - sym_update_crc(symname, mod, crc, export_no(export)); + s->is_static = 0; + sym_set_crc(symname, crc); + sym_update_namespace(symname, namespace); } - release_file(file, size); + free(buf); return; fail: - release_file(file, size); + free(buf); fatal("parse error in symbol dump file\n"); -} - -/* For normal builds always dump all symbols. - * For external modules only dump symbols - * that are not read from kernel Module.symvers. - **/ -static int dump_sym(struct symbol *sym) -{ - if (!external_module) - return 1; - if (sym->vmlinux || sym->kernel) - return 0; - return 1; } static void write_dump(const char *fname) { struct buffer buf = { }; struct symbol *symbol; + const char *namespace; int n; for (n = 0; n < SYMBOL_HASH_SIZE ; n++) { symbol = symbolhash[n]; while (symbol) { - if (dump_sym(symbol)) - buf_printf(&buf, "0x%08x\t%s\t%s\t%s\n", - symbol->crc, symbol->name, - symbol->module->name, - export_str(symbol->export)); + if (!symbol->module->from_dump) { + namespace = symbol->namespace; + buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n", + symbol->crc, symbol->name, + symbol->module->name, + export_str(symbol->export), + namespace ? namespace : ""); + } symbol = symbol->next; } } - write_if_changed(&buf, fname); + write_buf(&buf, fname); free(buf.p); } -struct ext_sym_list { - struct ext_sym_list *next; +static void write_namespace_deps_files(const char *fname) +{ + struct module *mod; + struct namespace_list *ns; + struct buffer ns_deps_buf = {}; + + for (mod = modules; mod; mod = mod->next) { + + if (mod->from_dump || !mod->missing_namespaces) + continue; + + buf_printf(&ns_deps_buf, "%s.ko:", mod->name); + + for (ns = mod->missing_namespaces; ns; ns = ns->next) + buf_printf(&ns_deps_buf, " %s", ns->namespace); + + buf_printf(&ns_deps_buf, "\n"); + } + + write_if_changed(&ns_deps_buf, fname); + free(ns_deps_buf.p); +} + +struct dump_list { + struct dump_list *next; const char *file; }; @@ -2452,29 +2558,23 @@ { struct module *mod; struct buffer buf = { }; - char *kernel_read = NULL, *module_read = NULL; + char *missing_namespace_deps = NULL; char *dump_write = NULL, *files_source = NULL; int opt; - int err; - struct ext_sym_list *extsym_iter; - struct ext_sym_list *extsym_start = NULL; + int n; + struct dump_list *dump_read_start = NULL; + struct dump_list **dump_read_iter = &dump_read_start; - while ((opt = getopt(argc, argv, "i:I:e:mnsST:o:awM:K:E")) != -1) { + while ((opt = getopt(argc, argv, "ei:mnT:o:awENd:v:")) != -1) { switch (opt) { - case 'i': - kernel_read = optarg; - break; - case 'I': - module_read = optarg; - external_module = 1; - break; case 'e': external_module = 1; - extsym_iter = - NOFAIL(malloc(sizeof(*extsym_iter))); - extsym_iter->next = extsym_start; - extsym_iter->file = optarg; - extsym_start = extsym_iter; + break; + case 'i': + *dump_read_iter = + NOFAIL(calloc(1, sizeof(**dump_read_iter))); + (*dump_read_iter)->file = optarg; + dump_read_iter = &(*dump_read_iter)->next; break; case 'm': modversions = 1; @@ -2488,12 +2588,6 @@ case 'a': all_versions = 1; break; - case 's': - vmlinux_section_warnings = 0; - break; - case 'S': - sec_mismatch_verbose = 0; - break; case 'T': files_source = optarg; break; @@ -2501,22 +2595,29 @@ warn_unresolved = 1; break; case 'E': - sec_mismatch_fatal = 1; + sec_mismatch_warn_only = false; + break; + case 'N': + allow_missing_ns_imports = 1; + break; + case 'd': + missing_namespace_deps = optarg; + break; + case 'v': + strncpy(module_scmversion, optarg, sizeof(module_scmversion) - 1); break; default: exit(1); } } - if (kernel_read) - read_dump(kernel_read, 1); - if (module_read) - read_dump(module_read, 0); - while (extsym_start) { - read_dump(extsym_start->file, 0); - extsym_iter = extsym_start->next; - free(extsym_start); - extsym_start = extsym_iter; + while (dump_read_start) { + struct dump_list *tmp; + + read_dump(dump_read_start->file); + tmp = dump_read_start->next; + free(dump_read_start); + dump_read_start = tmp; } while (optind < argc) @@ -2525,50 +2626,58 @@ if (files_source) read_symbols_from_files(files_source); - for (mod = modules; mod; mod = mod->next) { - if (mod->skip) - continue; - check_exports(mod); - } - - err = 0; + /* + * When there's no vmlinux, don't print warnings about + * unresolved symbols (since there'll be too many ;) + */ + if (!have_vmlinux) + warn("Symbol info of vmlinux is missing. Unresolved symbol check will be entirely skipped.\n"); for (mod = modules; mod; mod = mod->next) { char fname[PATH_MAX]; - if (mod->skip) + if (mod->is_vmlinux || mod->from_dump) continue; buf.pos = 0; - err |= check_modname_len(mod); + check_modname_len(mod); + check_exports(mod); + add_header(&buf, mod); add_intree_flag(&buf, !external_module); add_retpoline(&buf); add_staging_flag(&buf, mod->name); - err |= add_versions(&buf, mod); - add_depends(&buf, mod, modules); + add_versions(&buf, mod); + add_depends(&buf, mod); add_moddevtable(&buf, mod); add_srcversion(&buf, mod); + add_scmversion(&buf); sprintf(fname, "%s.mod.c", mod->name); write_if_changed(&buf, fname); } + + if (missing_namespace_deps) + write_namespace_deps_files(missing_namespace_deps); + if (dump_write) write_dump(dump_write); - if (sec_mismatch_count) { - if (!sec_mismatch_verbose) { - warn("modpost: Found %d section mismatch(es).\n" - "To see full details build your kernel with:\n" - "'make CONFIG_DEBUG_SECTION_MISMATCH=y'\n", - sec_mismatch_count); - } - if (sec_mismatch_fatal) { - fatal("modpost: Section mismatches detected.\n" - "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); + if (sec_mismatch_count && !sec_mismatch_warn_only) + error("Section mismatches detected.\n" + "Set CONFIG_SECTION_MISMATCH_WARN_ONLY=y to allow them.\n"); + for (n = 0; n < SYMBOL_HASH_SIZE; n++) { + struct symbol *s; + + for (s = symbolhash[n]; s; s = s->next) { + if (s->is_static) + error("\"%s\" [%s] is a static %s\n", + s->name, s->module->name, + export_str(s->export)); } } + free(buf.p); - return err; + return error_occurred ? 1 : 0; } -- Gitblit v1.6.2