modpost: remove the unused argument of check_sec_ref()
[linux-2.6-microblaze.git] / scripts / mod / modpost.c
index 085abe5..2806a5c 100644 (file)
@@ -30,7 +30,7 @@ static bool all_versions;
 static bool external_module;
 /* Only warn about unresolved symbols */
 static bool warn_unresolved;
-/* How a symbol is exported */
+
 static int sec_mismatch_count;
 static bool sec_mismatch_warn_only = true;
 /* ignore missing files */
@@ -47,12 +47,6 @@ static bool error_occurred;
 #define MAX_UNRESOLVED_REPORTS 10
 static unsigned int nr_unresolved;
 
-enum export {
-       export_plain,
-       export_gpl,
-       export_unknown
-};
-
 /* In kernel, this size is defined in linux/module.h;
  * here we use Elf_Addr instead of long for covering cross-compile
  */
@@ -219,7 +213,7 @@ struct symbol {
        bool crc_valid;
        bool weak;
        bool is_static;         /* true if symbol is not global */
-       enum export  export;       /* Type of export */
+       bool is_gpl_only;       /* exported by EXPORT_SYMBOL_GPL */
        char name[];
 };
 
@@ -272,7 +266,7 @@ static void sym_add_unresolved(const char *name, struct module *mod, bool weak)
        list_add_tail(&sym->list, &mod->unresolved_symbols);
 }
 
-static struct symbol *find_symbol(const char *name)
+static struct symbol *sym_find_with_module(const char *name, struct module *mod)
 {
        struct symbol *s;
 
@@ -281,12 +275,17 @@ static struct symbol *find_symbol(const char *name)
                name++;
 
        for (s = symbolhash[tdb_hash(name) % SYMBOL_HASH_SIZE]; s; s = s->next) {
-               if (strcmp(s->name, name) == 0)
+               if (strcmp(s->name, name) == 0 && (!mod || s->module == mod))
                        return s;
        }
        return NULL;
 }
 
+static struct symbol *find_symbol(const char *name)
+{
+       return sym_find_with_module(name, NULL);
+}
+
 struct namespace_list {
        struct list_head list;
        char namespace[];
@@ -316,34 +315,6 @@ static void add_namespace(struct list_head *head, const char *namespace)
        }
 }
 
-static const struct {
-       const char *str;
-       enum export export;
-} export_list[] = {
-       { .str = "EXPORT_SYMBOL",            .export = export_plain },
-       { .str = "EXPORT_SYMBOL_GPL",        .export = export_gpl },
-       { .str = "(unknown)",                .export = export_unknown },
-};
-
-
-static const char *export_str(enum export ex)
-{
-       return export_list[ex].str;
-}
-
-static enum export export_no(const char *s)
-{
-       int i;
-
-       if (!s)
-               return export_unknown;
-       for (i = 0; export_list[i].export != export_unknown; i++) {
-               if (strcmp(export_list[i].str, s) == 0)
-                       return export_list[i].export;
-       }
-       return export_unknown;
-}
-
 static void *sym_get_data_by_offset(const struct elf_info *info,
                                    unsigned int secindex, unsigned long offset)
 {
@@ -374,18 +345,6 @@ static const char *sec_name(const struct elf_info *info, int secindex)
 
 #define strstarts(str, prefix) (strncmp(str, prefix, strlen(prefix)) == 0)
 
-static enum export export_from_secname(struct elf_info *elf, unsigned int sec)
-{
-       const char *secname = sec_name(elf, sec);
-
-       if (strstarts(secname, "___ksymtab+"))
-               return export_plain;
-       else if (strstarts(secname, "___ksymtab_gpl+"))
-               return export_gpl;
-       else
-               return export_unknown;
-}
-
 static void sym_update_namespace(const char *symname, const char *namespace)
 {
        struct symbol *s = find_symbol(symname);
@@ -405,7 +364,7 @@ static void sym_update_namespace(const char *symname, const char *namespace)
 }
 
 static struct symbol *sym_add_exported(const char *name, struct module *mod,
-                                      enum export export)
+                                      bool gpl_only)
 {
        struct symbol *s = find_symbol(name);
 
@@ -417,26 +376,17 @@ static struct symbol *sym_add_exported(const char *name, struct module *mod,
 
        s = alloc_symbol(name);
        s->module = mod;
-       s->export    = export;
+       s->is_gpl_only = gpl_only;
        list_add_tail(&s->list, &mod->exported_symbols);
        hash_add_symbol(s);
 
        return s;
 }
 
-static void sym_set_crc(const char *name, unsigned int crc)
+static void sym_set_crc(struct symbol *sym, unsigned int crc)
 {
-       struct symbol *s = find_symbol(name);
-
-       /*
-        * 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 = true;
+       sym->crc = crc;
+       sym->crc_valid = true;
 }
 
 static void *grab_file(const char *filename, size_t *size)
@@ -659,38 +609,9 @@ static int ignore_undef_symbol(struct elf_info *info, const char *symname)
        return 0;
 }
 
-static void handle_modversion(const struct module *mod,
-                             const struct elf_info *info,
-                             const Elf_Sym *sym, const char *symname)
-{
-       unsigned int crc;
-
-       if (sym->st_shndx == SHN_UNDEF) {
-               warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
-                    "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
-                    symname, mod->name, mod->is_vmlinux ? "" : ".ko",
-                    symname);
-
-               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)
 {
-       const char *name;
-
        switch (sym->st_shndx) {
        case SHN_COMMON:
                if (strstarts(symname, "__gnu_lto_")) {
@@ -724,12 +645,15 @@ static void handle_symbol(struct module *mod, struct elf_info *info,
        default:
                /* All exported symbols */
                if (strstarts(symname, "__ksymtab_")) {
-                       enum export export;
+                       const char *name, *secname;
 
                        name = symname + strlen("__ksymtab_");
-                       export = export_from_secname(info,
-                                                    get_secindex(info, sym));
-                       sym_add_exported(name, mod, export);
+                       secname = sec_name(info, get_secindex(info, sym));
+
+                       if (strstarts(secname, "___ksymtab_gpl+"))
+                               sym_add_exported(name, mod, true);
+                       else if (strstarts(secname, "___ksymtab+"))
+                               sym_add_exported(name, mod, false);
                }
                if (strcmp(symname, "init_module") == 0)
                        mod->has_init = true;
@@ -1256,7 +1180,8 @@ static int secref_whitelist(const struct sectioncheck *mismatch,
 
 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] == '.');
 }
 
@@ -1959,8 +1884,7 @@ static void section_rel(const char *modname, struct elf_info *elf,
  * to find all references to a section that reference a section that will
  * be discarded and warns about it.
  **/
-static void check_sec_ref(struct module *mod, const char *modname,
-                         struct elf_info *elf)
+static void check_sec_ref(const char *modname, struct elf_info *elf)
 {
        int i;
        Elf_Shdr *sechdrs = elf->sechdrs;
@@ -1982,7 +1906,7 @@ static char *remove_dot(char *s)
 
        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 .prelink */
@@ -1992,6 +1916,104 @@ static char *remove_dot(char *s)
        return s;
 }
 
+/*
+ * The CRCs are recorded in .*.cmd files in the form of:
+ * #SYMVER <name> <crc>
+ */
+static void extract_crcs_for_object(const char *object, struct module *mod)
+{
+       char cmd_file[PATH_MAX];
+       char *buf, *p;
+       const char *base;
+       int dirlen, ret;
+
+       base = strrchr(object, '/');
+       if (base) {
+               base++;
+               dirlen = base - object;
+       } else {
+               dirlen = 0;
+               base = object;
+       }
+
+       ret = snprintf(cmd_file, sizeof(cmd_file), "%.*s.%s.cmd",
+                      dirlen, object, base);
+       if (ret >= sizeof(cmd_file)) {
+               error("%s: too long path was truncated\n", cmd_file);
+               return;
+       }
+
+       buf = read_text_file(cmd_file);
+       p = buf;
+
+       while ((p = strstr(p, "\n#SYMVER "))) {
+               char *name;
+               size_t namelen;
+               unsigned int crc;
+               struct symbol *sym;
+
+               name = p + strlen("\n#SYMVER ");
+
+               p = strchr(name, ' ');
+               if (!p)
+                       break;
+
+               namelen = p - name;
+               p++;
+
+               if (!isdigit(*p))
+                       continue;       /* skip this line */
+
+               crc = strtol(p, &p, 0);
+               if (*p != '\n')
+                       continue;       /* skip this line */
+
+               name[namelen] = '\0';
+
+               /*
+                * sym_find_with_module() may return NULL here.
+                * It typically occurs when CONFIG_TRIM_UNUSED_KSYMS=y.
+                * Since commit e1327a127703, genksyms calculates CRCs of all
+                * symbols, including trimmed ones. Ignore orphan CRCs.
+                */
+               sym = sym_find_with_module(name, mod);
+               if (sym)
+                       sym_set_crc(sym, crc);
+       }
+
+       free(buf);
+}
+
+/*
+ * The symbol versions (CRC) are recorded in the .*.cmd files.
+ * Parse them to retrieve CRCs for the current module.
+ */
+static void mod_set_crcs(struct module *mod)
+{
+       char objlist[PATH_MAX];
+       char *buf, *p, *obj;
+       int ret;
+
+       if (mod->is_vmlinux) {
+               strcpy(objlist, ".vmlinux.objs");
+       } else {
+               /* objects for a module are listed in the *.mod file. */
+               ret = snprintf(objlist, sizeof(objlist), "%s.mod", mod->name);
+               if (ret >= sizeof(objlist)) {
+                       error("%s: too long path was truncated\n", objlist);
+                       return;
+               }
+       }
+
+       buf = read_text_file(objlist);
+       p = buf;
+
+       while ((obj = strsep(&p, "\n")) && obj[0])
+               extract_crcs_for_object(obj, mod);
+
+       free(buf);
+}
+
 static void read_symbols(const char *modname)
 {
        const char *symname;
@@ -2052,9 +2074,6 @@ static void read_symbols(const char *modname)
                if (strstarts(symname, "__kstrtabns_"))
                        sym_update_namespace(symname + strlen("__kstrtabns_"),
                                             sym_get_data(&info, sym));
-               if (strstarts(symname, "__crc_"))
-                       handle_modversion(mod, &info, sym,
-                                         symname + strlen("__crc_"));
        }
 
        // check for static EXPORT_SYMBOL_* functions && global vars
@@ -2071,7 +2090,7 @@ static void read_symbols(const char *modname)
                }
        }
 
-       check_sec_ref(mod, modname, &info);
+       check_sec_ref(modname, &info);
 
        if (!mod->is_vmlinux) {
                version = get_modinfo(&info, "version");
@@ -2082,12 +2101,17 @@ static void read_symbols(const char *modname)
 
        parse_elf_finish(&info);
 
-       /* Our trick to get versioning for module struct etc. - it's
-        * never passed as an argument to an exported function, so
-        * the automatic versioning doesn't pick it up, but it's really
-        * important anyhow */
-       if (modversions)
+       if (modversions) {
+               /*
+                * Our trick to get versioning for module struct etc. - it's
+                * never passed as an argument to an exported function, so
+                * the automatic versioning doesn't pick it up, but it's really
+                * important anyhow.
+                */
                sym_add_unresolved("module_layout", mod, false);
+
+               mod_set_crcs(mod);
+       }
 }
 
 static void read_symbols_from_files(const char *filename)
@@ -2140,20 +2164,6 @@ void buf_write(struct buffer *buf, const char *s, int len)
        buf->pos += len;
 }
 
-static void check_for_gpl_usage(enum export exp, const char *m, const char *s)
-{
-       switch (exp) {
-       case export_gpl:
-               error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
-                     m, s);
-               break;
-       case export_plain:
-       case export_unknown:
-               /* ignore */
-               break;
-       }
-}
-
 static void check_exports(struct module *mod)
 {
        struct symbol *s, *exp;
@@ -2192,8 +2202,9 @@ static void check_exports(struct module *mod)
                        add_namespace(&mod->missing_namespaces, exp->namespace);
                }
 
-               if (!mod->is_gpl_compatible)
-                       check_for_gpl_usage(exp->export, basename, exp->name);
+               if (!mod->is_gpl_compatible && exp->is_gpl_only)
+                       error("GPL-incompatible module %s.ko uses GPL-only symbol '%s'\n",
+                             basename, exp->name);
        }
 }
 
@@ -2223,6 +2234,7 @@ static void add_header(struct buffer *b, struct module *mod)
        buf_printf(b, "#define INCLUDE_VERMAGIC\n");
        buf_printf(b, "#include <linux/build-salt.h>\n");
        buf_printf(b, "#include <linux/elfnote-lto.h>\n");
+       buf_printf(b, "#include <linux/export-internal.h>\n");
        buf_printf(b, "#include <linux/vermagic.h>\n");
        buf_printf(b, "#include <linux/compiler.h>\n");
        buf_printf(b, "\n");
@@ -2243,26 +2255,41 @@ static void add_header(struct buffer *b, struct module *mod)
                              "#endif\n");
        buf_printf(b, "\t.arch = MODULE_ARCH_INIT,\n");
        buf_printf(b, "};\n");
-}
 
-static void add_intree_flag(struct buffer *b, int is_intree)
-{
-       if (is_intree)
+       if (!external_module)
                buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
-}
 
-/* Cannot check for assembler */
-static void add_retpoline(struct buffer *b)
-{
-       buf_printf(b, "\n#ifdef CONFIG_RETPOLINE\n");
-       buf_printf(b, "MODULE_INFO(retpoline, \"Y\");\n");
-       buf_printf(b, "#endif\n");
+       buf_printf(b,
+                  "\n"
+                  "#ifdef CONFIG_RETPOLINE\n"
+                  "MODULE_INFO(retpoline, \"Y\");\n"
+                  "#endif\n");
+
+       if (strstarts(mod->name, "drivers/staging"))
+               buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
 }
 
-static void add_staging_flag(struct buffer *b, const char *name)
+static void add_exported_symbols(struct buffer *buf, struct module *mod)
 {
-       if (strstarts(name, "drivers/staging"))
-               buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
+       struct symbol *sym;
+
+       if (!modversions)
+               return;
+
+       /* record CRCs for exported symbols */
+       buf_printf(buf, "\n");
+       list_for_each_entry(sym, &mod->exported_symbols, list) {
+               if (!sym->crc_valid) {
+                       warn("EXPORT symbol \"%s\" [%s%s] version generation failed, symbol will not be versioned.\n"
+                            "Is \"%s\" prototyped in <asm/asm-prototypes.h>?\n",
+                            sym->name, mod->name, mod->is_vmlinux ? "" : ".ko",
+                            sym->name);
+                       continue;
+               }
+
+               buf_printf(buf, "SYMBOL_CRC(%s, 0x%08x, \"%s\");\n",
+                          sym->name, sym->crc, sym->is_gpl_only ? "_gpl" : "");
+       }
 }
 
 /**
@@ -2398,6 +2425,47 @@ static void write_if_changed(struct buffer *b, const char *fname)
        write_buf(b, fname);
 }
 
+static void write_vmlinux_export_c_file(struct module *mod)
+{
+       struct buffer buf = { };
+
+       buf_printf(&buf,
+                  "#include <linux/export-internal.h>\n");
+
+       add_exported_symbols(&buf, mod);
+       write_if_changed(&buf, ".vmlinux.export.c");
+       free(buf.p);
+}
+
+/* do sanity checks, and generate *.mod.c file */
+static void write_mod_c_file(struct module *mod)
+{
+       struct buffer buf = { };
+       char fname[PATH_MAX];
+       int ret;
+
+       check_modname_len(mod);
+       check_exports(mod);
+
+       add_header(&buf, mod);
+       add_exported_symbols(&buf, mod);
+       add_versions(&buf, mod);
+       add_depends(&buf, mod);
+       add_moddevtable(&buf, mod);
+       add_srcversion(&buf, mod);
+
+       ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
+       if (ret >= sizeof(fname)) {
+               error("%s: too long path was truncated\n", fname);
+               goto free;
+       }
+
+       write_if_changed(&buf, fname);
+
+free:
+       free(buf.p);
+}
+
 /* parse Module.symvers file. line format:
  * 0x12345678<tab>symbol<tab>module<tab>export<tab>namespace
  **/
@@ -2417,6 +2485,7 @@ static void read_dump(const char *fname)
                unsigned int crc;
                struct module *mod;
                struct symbol *s;
+               bool gpl_only;
 
                if (!(symname = strchr(line, '\t')))
                        goto fail;
@@ -2434,14 +2503,24 @@ static void read_dump(const char *fname)
                crc = strtoul(line, &d, 16);
                if (*symname == '\0' || *modname == '\0' || *d != '\0')
                        goto fail;
+
+               if (!strcmp(export, "EXPORT_SYMBOL_GPL")) {
+                       gpl_only = true;
+               } else if (!strcmp(export, "EXPORT_SYMBOL")) {
+                       gpl_only = false;
+               } else {
+                       error("%s: unknown license %s. skip", symname, export);
+                       continue;
+               }
+
                mod = find_module(modname);
                if (!mod) {
                        mod = new_module(modname);
                        mod->from_dump = true;
                }
-               s = sym_add_exported(symname, mod, export_no(export));
+               s = sym_add_exported(symname, mod, gpl_only);
                s->is_static = false;
-               sym_set_crc(symname, crc);
+               sym_set_crc(s, crc);
                sym_update_namespace(symname, namespace);
        }
        free(buf);
@@ -2461,9 +2540,9 @@ static void write_dump(const char *fname)
                if (mod->from_dump)
                        continue;
                list_for_each_entry(sym, &mod->exported_symbols, list) {
-                       buf_printf(&buf, "0x%08x\t%s\t%s\t%s\t%s\n",
+                       buf_printf(&buf, "0x%08x\t%s\t%s\tEXPORT_SYMBOL%s\t%s\n",
                                   sym->crc, sym->name, mod->name,
-                                  export_str(sym->export),
+                                  sym->is_gpl_only ? "_GPL" : "",
                                   sym->namespace ?: "");
                }
        }
@@ -2502,7 +2581,6 @@ struct dump_list {
 int main(int argc, char **argv)
 {
        struct module *mod;
-       struct buffer buf = { };
        char *missing_namespace_deps = NULL;
        char *dump_write = NULL, *files_source = NULL;
        int opt;
@@ -2565,33 +2643,13 @@ int main(int argc, char **argv)
                read_symbols_from_files(files_source);
 
        list_for_each_entry(mod, &modules, list) {
-               char fname[PATH_MAX];
-               int ret;
-
-               if (mod->is_vmlinux || mod->from_dump)
-                       continue;
-
-               buf.pos = 0;
-
-               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);
-               add_versions(&buf, mod);
-               add_depends(&buf, mod);
-               add_moddevtable(&buf, mod);
-               add_srcversion(&buf, mod);
-
-               ret = snprintf(fname, sizeof(fname), "%s.mod.c", mod->name);
-               if (ret >= sizeof(fname)) {
-                       error("%s: too long path was truncated\n", fname);
+               if (mod->from_dump)
                        continue;
-               }
 
-               write_if_changed(&buf, fname);
+               if (mod->is_vmlinux)
+                       write_vmlinux_export_c_file(mod);
+               else
+                       write_mod_c_file(mod);
        }
 
        if (missing_namespace_deps)
@@ -2607,9 +2665,8 @@ int main(int argc, char **argv)
 
                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));
+                               error("\"%s\" [%s] is a static EXPORT_SYMBOL\n",
+                                     s->name, s->module->name);
                }
        }
 
@@ -2617,7 +2674,5 @@ int main(int argc, char **argv)
                warn("suppressed %u unresolved symbol warnings because there were too many)\n",
                     nr_unresolved - MAX_UNRESOLVED_REPORTS);
 
-       free(buf.p);
-
        return error_occurred ? 1 : 0;
 }