A compromise solution for insmod
[oweals/busybox.git] / insmod.c
index fe4e06a6caaf4d0edb0ef98209359c8a32f1ff8e..0644affd194b3c021cef30296828b426b83f3e6e 100644 (file)
--- a/insmod.c
+++ b/insmod.c
@@ -1,6 +1,7 @@
 /* vi: set sw=4 ts=4: */
 /*
  * Mini insmod implementation for busybox
+ * This version of insmod now supports x86, ARM, SH3/4, powerpc, and MIPS.
  *
  * Copyright (C) 1999,2000,2001 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>
 #include <string.h>
 #include <getopt.h>
 #include <sys/utsname.h>
-#include <sys/syscall.h>
-#include <linux/unistd.h>
 #include "busybox.h"
 
+#ifdef BB_FEATURE_NEW_MODULE_INTERFACE
+# define new_sys_init_module   init_module
+#else
+# define old_sys_init_module   init_module
+#endif
+
 #if defined(__powerpc__)
 #define BB_USE_PLT_ENTRIES
 #define BB_PLT_ENTRY_SIZE 16
 #ifndef MODUTILS_MODULE_H
 static const int MODUTILS_MODULE_H = 1;
 
-#ident "$Id: insmod.c,v 1.52 2001/03/19 19:28:24 andersen Exp $"
+#ident "$Id: insmod.c,v 1.63 2001/05/14 20:03:04 andersen Exp $"
 
 /* This file contains the structures used by the 2.0 and 2.1 kernels.
    We do not use the kernel headers directly because we do not wish
@@ -325,7 +330,7 @@ int delete_module(const char *);
 #ifndef MODUTILS_OBJ_H
 static const int MODUTILS_OBJ_H = 1;
 
-#ident "$Id: insmod.c,v 1.52 2001/03/19 19:28:24 andersen Exp $"
+#ident "$Id: insmod.c,v 1.63 2001/05/14 20:03:04 andersen Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -373,6 +378,15 @@ static const int MODUTILS_OBJ_H = 1;
 
 #elif defined(__mips__)
 
+/* Account for ELF spec changes.  */
+#ifndef EM_MIPS_RS3_LE
+#ifdef EM_MIPS_RS4_BE
+#define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
+#else
+#define EM_MIPS_RS3_LE 10
+#endif
+#endif /* !EM_MIPS_RS3_LE */
+
 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
 #define SHT_RELM       SHT_REL
 #define Elf32_RelM     Elf32_Rel
@@ -663,85 +677,35 @@ size_t nksyms;
 struct external_module *ext_modules;
 int n_ext_modules;
 int n_ext_modules_used;
-
-
-
-/* Some firendly syscalls to cheer everyone's day...  */
-#define __NR_new_sys_init_module  __NR_init_module
-_syscall2(int, new_sys_init_module, const char *, name,
-                 const struct new_module *, info)
-#define __NR_old_sys_init_module  __NR_init_module
-_syscall5(int, old_sys_init_module, const char *, name, char *, code,
-                 unsigned, codesize, struct old_mod_routines *, routines,
-                 struct old_symbol_table *, symtab)
-#ifndef BB_RMMOD
-_syscall1(int, delete_module, const char *, name)
-#else
 extern int delete_module(const char *);
-#endif
-
-/* This is kind of troublesome. See, we don't actually support
-   the m68k or the arm the same way we support i386 and (now)
-   sh. In doing my SH patch, I just assumed that whatever works
-   for i386 also works for m68k and arm since currently insmod.c
-   does nothing special for them. If this isn't true, the below
-   line is rather misleading IMHO, and someone should either
-   change it or add more proper architecture-dependent support
-   for these boys.
-
-   -- Bryan Rittmeyer <bryan@ixiacom.com>                    */
 
-#ifdef BB_FEATURE_OLD_MODULE_INTERFACE
-_syscall1(int, get_kernel_syms, struct old_kernel_sym *, ks)
-#endif
+static char m_filename[FILENAME_MAX + 1];
+static char m_fullName[FILENAME_MAX + 1];
 
-#if defined(__i386__) || defined(__m68k__) || defined(__arm__) \
- || defined(__powerpc__)
-/* Jump through hoops to fixup error return codes */
-#define __NR__create_module  __NR_create_module
-static inline _syscall2(long, _create_module, const char *, name, size_t,
-                                               size)
-unsigned long create_module(const char *name, size_t size)
-{
-       long ret = _create_module(name, size);
 
-       if (ret == -1 && errno > 125) {
-               ret = -errno;
-               errno = 0;
-       }
-       return ret;
-}
-#else
-_syscall2(unsigned long, create_module, const char *, name, size_t, size)
-#endif
-static char m_filename[BUFSIZ + 1];
-static char m_fullName[BUFSIZ + 1];
 
 /*======================================================================*/
 
 
-static int findNamedModule(const char *fileName, struct stat *statbuf,
-                                                  void *userDate)
+static int check_module_name_match(const char *filename, struct stat *statbuf,
+                                                  void *userdata)
 {
-       char *fullName = (char *) userDate;
+       char *fullname = (char *) userdata;
 
-
-       if (fullName[0] == '\0')
+       if (fullname[0] == '\0')
                return (FALSE);
        else {
-               char *tmp = strrchr((char *) fileName, '/');
-
-               if (tmp == NULL)
-                       tmp = (char *) fileName;
-               else
-                       tmp++;
-               if (check_wildcard_match(tmp, fullName) == TRUE) {
+               char *tmp, *tmp1 = strdup(filename);
+               tmp = get_last_path_component(tmp1);
+               if (strcmp(tmp, fullname) == 0) {
+                       free(tmp1);
                        /* Stop searching if we find a match */
-                       memcpy(m_filename, fileName, strlen(fileName)+1);
-                       return (FALSE);
+                       safe_strncpy(m_filename, filename, sizeof(m_filename));
+                       return (TRUE);
                }
+               free(tmp1);
        }
-       return (TRUE);
+       return (FALSE);
 }
 
 
@@ -793,7 +757,9 @@ arch_apply_relocation(struct obj_file *f,
                                      ElfW(RelM) *rel, ElfW(Addr) v)
 {
        struct arch_file *ifile = (struct arch_file *) f;
+#if !(defined(__mips__))
        struct arch_symbol *isym = (struct arch_symbol *) sym;
+#endif
 
        ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
        ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
@@ -1199,18 +1165,18 @@ int arch_create_got(struct obj_file *f)
 
 #if defined(BB_USE_GOT_ENTRIES)
        if (got_offset) {
-               struct obj_section* relsec = obj_find_section(f, ".got");
+               struct obj_section* myrelsec = obj_find_section(f, ".got");
 
-               if (relsec) {
-                       obj_extend_section(relsec, got_offset);
+               if (myrelsec) {
+                       obj_extend_section(myrelsec, got_offset);
                } else {
-                       relsec = obj_create_alloced_section(f, ".got", 
+                       myrelsec = obj_create_alloced_section(f, ".got", 
                                                            BB_GOT_ENTRY_SIZE,
                                                            got_offset);
-                       assert(relsec);
+                       assert(myrelsec);
                }
 
-               ifile->got = relsec;
+               ifile->got = myrelsec;
        }
 #endif
 
@@ -1555,7 +1521,9 @@ struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
 void *obj_extend_section(struct obj_section *sec, unsigned long more)
 {
        unsigned long oldsize = sec->header.sh_size;
-       sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
+       if (more) { 
+               sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
+       }
        return sec->contents + oldsize;
 }
 
@@ -1737,19 +1705,19 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv)
                        while (*q++ == ',');
                } else {
                        char *contents = f->sections[sym->secidx]->contents;
-                       char *loc = contents + sym->value;
+                       char *myloc = contents + sym->value;
                        char *r;                        /* To search for commas */
 
                        /* Break the string with comas */
                        while ((r = strchr(q, ',')) != (char *) NULL) {
                                *r++ = '\0';
-                               obj_string_patch(f, sym->secidx, loc - contents, q);
-                               loc += sizeof(char *);
+                               obj_string_patch(f, sym->secidx, myloc - contents, q);
+                               myloc += sizeof(char *);
                                q = r;
                        }
 
                        /* last part */
-                       obj_string_patch(f, sym->secidx, loc - contents, q);
+                       obj_string_patch(f, sym->secidx, myloc - contents, q);
                }
 
                argc--, argv++;
@@ -1806,8 +1774,11 @@ static int old_get_kernel_symbols(const char *m_name)
        int nks, nms, nmod, i;
 
        nks = get_kernel_syms(NULL);
-       if (nks < 0) {
-               perror_msg("get_kernel_syms: %s", m_name);
+       if (nks <= 0) {
+               if (nks)
+                       perror_msg("get_kernel_syms: %s", m_name);
+               else
+                       error_msg("No kernel symbols");
                return 0;
        }
 
@@ -1827,7 +1798,6 @@ static int old_get_kernel_symbols(const char *m_name)
 
        while (k->name[0] == '#' && k->name[1]) {
                struct old_kernel_sym *k2;
-               struct new_module_symbol *s;
 
                /* Find out how many symbols this module has.  */
                for (k2 = k + 1; k2->name[0] != '#'; ++k2)
@@ -2297,7 +2267,7 @@ static int new_get_kernel_symbols(void)
        module_names = xmalloc(bufsize = 256);
   retry_modules_load:
        if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
-               if (errno == ENOSPC) {
+               if (errno == ENOSPC && bufsize < ret) {
                        module_names = xrealloc(module_names, bufsize = ret);
                        goto retry_modules_load;
                }
@@ -2358,7 +2328,7 @@ static int new_get_kernel_symbols(void)
        syms = xmalloc(bufsize = 16 * 1024);
   retry_kern_sym_load:
        if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
-               if (errno == ENOSPC) {
+               if (errno == ENOSPC && bufsize < ret) {
                        syms = xrealloc(syms, bufsize = ret);
                        goto retry_kern_sym_load;
                }
@@ -2491,6 +2461,9 @@ new_init_module(const char *m_name, struct obj_file *f,
        tgt_long m_addr;
 
        sec = obj_find_section(f, ".this");
+       if (!sec || !sec->contents) { 
+               perror_msg_and_die("corrupt module %s?",m_name);
+       }
        module = (struct new_module *) sec->contents;
        m_addr = sec->header.sh_addr;
 
@@ -3042,6 +3015,12 @@ struct obj_file *obj_load(FILE * fp)
        for (i = 0; i < shnum; ++i) {
                struct obj_section *sec = f->sections[i];
 
+               /* .modinfo should be contents only but gcc has no attribute for that.
+                * The kernel may have marked .modinfo as ALLOC, ignore this bit.
+                */
+               if (strcmp(sec->name, ".modinfo") == 0)
+                       sec->header.sh_flags &= ~SHF_ALLOC;
+
                if (sec->header.sh_flags & SHF_ALLOC)
                        obj_insert_section_load_order(f, sec);
 
@@ -3065,22 +3044,20 @@ struct obj_file *obj_load(FILE * fp)
 
                                /* Allocate space for a table of local symbols.  */
                                j = f->local_symtab_size = sec->header.sh_info;
-                               f->local_symtab = xmalloc(j *=
-                                                                                 sizeof(struct obj_symbol *));
-                               memset(f->local_symtab, 0, j);
+                               f->local_symtab = xcalloc(j, sizeof(struct obj_symbol *));
 
                                /* Insert all symbols into the hash table.  */
                                for (j = 1, ++sym; j < nsym; ++j, ++sym) {
                                        const char *name;
                                        if (sym->st_name)
                                                name = strtab + sym->st_name;
-               else
+                                       else
                                                name = f->sections[sym->st_shndx]->name;
 
                                        obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
                                                                   sym->st_value, sym->st_size);
-               }
-       }
+                               }
+                       }
                        break;
 
                case SHT_RELM:
@@ -3091,6 +3068,10 @@ struct obj_file *obj_load(FILE * fp)
                                return NULL;
                        }
                        break;
+                       /* XXX  Relocation code from modutils-2.3.19 is not here.
+                        * Why?  That's about 20 lines of code from obj/obj_load.c,
+                        * which gets done in a second pass through the sections.
+                        * This BusyBox insmod does similar work in obj_relocate(). */
                }
        }
 
@@ -3129,7 +3110,7 @@ extern int insmod_main( int argc, char **argv)
        FILE *fp;
        struct obj_file *f;
        struct stat st;
-       char m_name[BUFSIZ + 1] = "\0";
+       char m_name[FILENAME_MAX + 1] = "\0";
        int exit_status = EXIT_FAILURE;
        int m_has_modinfo;
 #ifdef BB_FEATURE_INSMOD_VERSION_CHECKING
@@ -3156,7 +3137,7 @@ extern int insmod_main( int argc, char **argv)
                                flag_export = 0;
                                break;
                        case 'o':                       /* name the output module */
-                               strncpy(m_name, optarg, BUFSIZ);
+                               strncpy(m_name, optarg, FILENAME_MAX);
                                break;
                        case 'L':                       /* Stub warning */
                                /* This is needed for compatibility with modprobe.
@@ -3183,30 +3164,60 @@ extern int insmod_main( int argc, char **argv)
 
        if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
                len -= 2;
-       strncpy(m_fullName, tmp, len);
+       memcpy(m_fullName, tmp, len);
+       m_fullName[len]='\0';
        if (*m_name == '\0') {
                strcpy(m_name, m_fullName);
        }
        strcat(m_fullName, ".o");
 
-       /* Get a filedesc for the module */
+       /* Get a filedesc for the module.  Check we we have a complete path */
        if (stat(argv[optind], &st) < 0 || !S_ISREG(st.st_mode) ||
                        (fp = fopen(argv[optind], "r")) == NULL) {
-               /* Hmpf.  Could not open it. Search through _PATH_MODULES to find a module named m_name */
-               if (recursive_action(_PATH_MODULES, TRUE, FALSE, FALSE,
-                                                       findNamedModule, 0, m_fullName) == FALSE) 
+               struct utsname myuname;
+
+               /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
+                * but do not error out yet if we fail to find it... */
+               if (uname(&myuname) == 0) {
+                       char module_dir[FILENAME_MAX];
+                       char real_module_dir[FILENAME_MAX];
+                       snprintf (module_dir, sizeof(module_dir), "%s/%s", 
+                                       _PATH_MODULES, myuname.release);
+                       /* Jump through hoops in case /lib/modules/`uname -r`
+                        * is a symlink.  We do not want recursive_action to
+                        * follow symlinks, but we do want to follow the
+                        * /lib/modules/`uname -r` dir, So resolve it ourselves
+                        * if it is a link... */
+                       if (realpath (module_dir, real_module_dir) == NULL)
+                               strcpy(real_module_dir, module_dir);
+                       recursive_action(real_module_dir, TRUE, FALSE, FALSE,
+                                       check_module_name_match, 0, m_fullName);
+               }
+
+               /* Check if we have found anything yet */
+               if (m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) 
                {
-                       if (m_filename[0] == '\0'
-                               || ((fp = fopen(m_filename, "r")) == NULL)) 
+                       char module_dir[FILENAME_MAX];
+                       if (realpath (_PATH_MODULES, module_dir) == NULL)
+                               strcpy(module_dir, _PATH_MODULES);
+                       /* No module found under /lib/modules/`uname -r`, this
+                        * time cast the net a bit wider.  Search /lib/modules/ */
+                       if (recursive_action(module_dir, TRUE, FALSE, FALSE,
+                                               check_module_name_match, 0, m_fullName) == FALSE) 
                        {
-                               error_msg("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
-                               return EXIT_FAILURE;
-                       }
-               } else
-                       error_msg_and_die("No module named '%s' found in '%s'", m_fullName, _PATH_MODULES);
-       } else
-               memcpy(m_filename, argv[optind], strlen(argv[optind]));
+                               if (m_filename[0] == '\0'
+                                               || ((fp = fopen(m_filename, "r")) == NULL)) 
+                               {
+                                       error_msg("%s: no module by that name found", m_fullName);
+                                       return EXIT_FAILURE;
+                               }
+                       } else
+                               error_msg_and_die("%s: no module by that name found", m_fullName);
+               }
+       } else 
+               safe_strncpy(m_filename, argv[optind], sizeof(m_filename));
 
+       printf("Using %s\n", m_filename);
 
        if ((f = obj_load(fp)) == NULL)
                perror_msg_and_die("Could not load the module");