summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/modutils
diff options
context:
space:
mode:
Diffstat (limited to 'release/src/router/busybox/modutils')
-rw-r--r--[-rwxr-xr-x]release/src/router/busybox/modutils/Config.in238
-rw-r--r--release/src/router/busybox/modutils/Kbuild14
-rw-r--r--release/src/router/busybox/modutils/Makefile30
-rwxr-xr-xrelease/src/router/busybox/modutils/Makefile.in37
-rw-r--r--release/src/router/busybox/modutils/depmod.c245
-rw-r--r--release/src/router/busybox/modutils/insmod.c4236
-rw-r--r--release/src/router/busybox/modutils/lsmod.c198
-rw-r--r--release/src/router/busybox/modutils/modprobe-small.c798
-rw-r--r--release/src/router/busybox/modutils/modprobe.c859
-rw-r--r--release/src/router/busybox/modutils/modutils-24.c3905
-rw-r--r--release/src/router/busybox/modutils/modutils.c129
-rw-r--r--release/src/router/busybox/modutils/modutils.h64
-rw-r--r--release/src/router/busybox/modutils/rmmod.c98
13 files changed, 5794 insertions, 5057 deletions
diff --git a/release/src/router/busybox/modutils/Config.in b/release/src/router/busybox/modutils/Config.in
index 71298bbf..ef8d9692 100755..100644
--- a/release/src/router/busybox/modutils/Config.in
+++ b/release/src/router/busybox/modutils/Config.in
@@ -5,67 +5,156 @@
menu "Linux Module Utilities"
-config CONFIG_INSMOD
+config MODPROBE_SMALL
+ bool "Simplified modutils"
+ default n
+ help
+ Simplified modutils.
+
+ With this option modprobe does not require modules.dep file
+ and does not use /etc/modules.conf file.
+ It scans module files in /lib/modules/`uname -r` and
+ determines dependencies and module alias names on the fly.
+ This may make module loading slower, most notably
+ when one needs to load module by alias (this requires
+ scanning through module _bodies_).
+
+ At the first attempt to load a module by alias modprobe
+ will try to generate modules.dep.bb file in order to speed up
+ future loads by alias. Failure to do so (read-only /lib/modules,
+ etc) is not reported, and future modprobes will be slow too.
+
+ NB: modules.dep.bb file format is not compatible
+ with modules.dep file as created/used by standard module tools.
+
+ Additional module parameters can be stored in
+ /etc/modules/$module_name files.
+
+ Apart from modprobe, other utilities are also provided:
+ - insmod is an alias to modprobe
+ - rmmod is an alias to modprobe -r
+ - depmod generates modules.dep.bb
+
+ As of 2008-07, this code is experimental. It is 14kb smaller
+ than "non-small" modutils.
+
+config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
+ bool "Accept module options on modprobe command line"
+ default n
+ depends on MODPROBE_SMALL
+ help
+ Allow insmod and modprobe take module options from command line.
+
+config FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
+ bool "Skip loading of already loaded modules"
+ default n
+ depends on MODPROBE_SMALL
+ help
+ Check if the module is already loaded.
+
+config INSMOD
bool "insmod"
default n
+ depends on !MODPROBE_SMALL
help
insmod is used to load specified modules in the running kernel.
-config CONFIG_FEATURE_OLD_MODULE_INTERFACE
- bool " Support older (pre 2.1) Linux kernels"
+config RMMOD
+ bool "rmmod"
default n
- depends on CONFIG_INSMOD
+ depends on !MODPROBE_SMALL
help
- Provide insmod support for older (pre 2.1) Linux kernels.
+ rmmod is used to unload specified modules from the kernel.
-if CONFIG_INSMOD && !CONFIG_FEATURE_OLD_MODULE_INTERFACE
- config CONFIG_FEATURE_NEW_MODULE_INTERFACE
- default y
- comment " Support new (post 2.1) Linux kernels (Forced enabled)"
-endif
+config LSMOD
+ bool "lsmod"
+ default n
+ depends on !MODPROBE_SMALL
+ help
+ lsmod is used to display a list of loaded modules.
-if CONFIG_FEATURE_OLD_MODULE_INTERFACE
-config CONFIG_FEATURE_NEW_MODULE_INTERFACE
- bool " Support new (post 2.1) Linux kernels"
- default y
- depends on CONFIG_INSMOD
+config FEATURE_LSMOD_PRETTY_2_6_OUTPUT
+ bool "Pretty output"
+ default n
+ depends on LSMOD
+ help
+ This option makes output format of lsmod adjusted to
+ the format of module-init-tools for Linux kernel 2.6.
+ Increases size somewhat.
+
+config MODPROBE
+ bool "modprobe"
+ default n
+ depends on !MODPROBE_SMALL
+ help
+ Handle the loading of modules, and their dependencies on a high
+ level.
+
+config FEATURE_MODPROBE_BLACKLIST
+ bool
+ prompt "Blacklist support"
+ default n
+ depends on MODPROBE
+ help
+ Say 'y' here to enable support for the 'blacklist' command in
+ modprobe.conf. This prevents the alias resolver to resolve
+ blacklisted modules. This is useful if you want to prevent your
+ hardware autodetection scripts to load modules like evdev, frame
+ buffer drivers etc.
+
+config DEPMOD
+ bool "depmod"
+ default n
+ depends on !MODPROBE_SMALL
+ help
+ depmod generates modules.dep (and potentially modules.alias
+ and modules.symbols) that contain dependency information
+ for modprobe.
+
+comment "Options common to multiple modutils"
+
+config FEATURE_2_4_MODULES
+ bool "Support version 2.2/2.4 Linux kernels"
+ default n
+ depends on INSMOD || RMMOD || LSMOD
help
- Support module loading for newer (post 2.1) Linux kernels.
-endif
+ Support module loading for 2.2.x and 2.4.x Linux kernels.
+ This increases size considerably. Say N unless you plan
+ to run ancient kernels.
-config CONFIG_FEATURE_INSMOD_VERSION_CHECKING
- bool " Module version checking"
+config FEATURE_INSMOD_VERSION_CHECKING
+ bool "Enable module version checking"
default n
- depends on CONFIG_INSMOD
+ depends on FEATURE_2_4_MODULES && (INSMOD || MODPROBE)
help
- Support checking of versions for modules. This is used to
+ Support checking of versions for modules. This is used to
ensure that the kernel and module are made for each other.
-config CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
- bool " Add module symbols to kernel symbol table"
+config FEATURE_INSMOD_KSYMOOPS_SYMBOLS
+ bool "Add module symbols to kernel symbol table"
default n
- depends on CONFIG_INSMOD
+ depends on FEATURE_2_4_MODULES && (INSMOD || MODPROBE)
help
- By adding module symbols to the kernel symbol table, Oops messages
- occuring within kernel modules can be properly debugged. By enabling
- this feature, module symbols will always be added to the kernel symbol
- table for properly debugging support. If you are not interested in
+ By adding module symbols to the kernel symbol table, Oops messages
+ occuring within kernel modules can be properly debugged. By enabling
+ this feature, module symbols will always be added to the kernel symbol
+ table for proper debugging support. If you are not interested in
Oops messages from kernel modules, say N.
-config CONFIG_FEATURE_INSMOD_LOADINKMEM
- bool " In kernel memory optimization (uClinux only)"
+config FEATURE_INSMOD_LOADINKMEM
+ bool "In kernel memory optimization (uClinux only)"
default n
- depends on CONFIG_INSMOD
+ depends on FEATURE_2_4_MODULES && (INSMOD || MODPROBE)
help
This is a special uClinux only memory optimization that lets insmod
load the specified kernel module directly into kernel space, reducing
memory usage by preventing the need for two copies of the module
being loaded into memory.
-config CONFIG_FEATURE_INSMOD_LOAD_MAP
- bool " Enable load map (-m) option"
+config FEATURE_INSMOD_LOAD_MAP
+ bool "Enable insmod load map (-m) option"
default n
- depends on CONFIG_INSMOD
+ depends on FEATURE_2_4_MODULES && INSMOD
help
Enabling this, one would be able to get a load map
output on stdout. This makes kernel module debugging
@@ -73,53 +162,66 @@ config CONFIG_FEATURE_INSMOD_LOAD_MAP
If you don't plan to debug kernel modules, you
don't need this option.
-config CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
- bool " Symbols in load map"
+config FEATURE_INSMOD_LOAD_MAP_FULL
+ bool "Symbols in load map"
default y
- depends on CONFIG_FEATURE_INSMOD_LOAD_MAP
+ depends on FEATURE_INSMOD_LOAD_MAP && !MODPROBE_SMALL
help
Without this option, -m will only output section
- load map.
- With this option, -m will also output symbols
- load map.
+ load map. With this option, -m will also output
+ symbols load map.
-config CONFIG_LSMOD
- bool "lsmod"
- default n
+config FEATURE_CHECK_TAINTED_MODULE
+ bool "Support tainted module checking with new kernels"
+ default y
+ depends on (LSMOD || FEATURE_2_4_MODULES) && !MODPROBE_SMALL
help
- lsmod is used to display a list of loaded modules.
+ Support checking for tainted modules. These are usually binary
+ only modules that will make the linux-kernel list ignore your
+ support request.
+ This option is required to support GPLONLY modules.
-config CONFIG_FEATURE_QUERY_MODULE_INTERFACE
- bool " Support lsmod query_module interface (add 638 bytes)"
+config FEATURE_MODUTILS_ALIAS
+ bool "Support for module.aliases file"
default y
- depends on CONFIG_LSMOD && CONFIG_FEATURE_NEW_MODULE_INTERFACE
+ depends on DEPMOD || MODPROBE
help
- This will provide some extra information about each module when
- running lsmod. The fields provided are address, size, flags and
- usage count.
+ Generate and parse modules.alias containing aliases for bus
+ identifiers:
+ alias pcmcia:m*c*f03fn*pfn*pa*pb*pc*pd* parport_cs
-config CONFIG_MODPROBE
- bool "modprobe"
- default n
- help
- Handle the loading of modules, and their dependancies on a high
- level.
+ and aliases for logical modules names e.g.:
+ alias padlock_aes aes
+ alias aes_i586 aes
+ alias aes_generic aes
-config CONFIG_RMMOD
- bool "rmmod"
- default n
- help
- rmmod is used to unload specified modules from the kernel.
+ Say Y if unsure.
-config CONFIG_FEATURE_CHECK_TAINTED_MODULE
- bool "Support tainted module checking with new kernels"
+config FEATURE_MODUTILS_SYMBOLS
+ bool "Support for module.symbols file"
default y
- depends on CONFIG_INSMOD || CONFIG_LSMOD
+ depends on DEPMOD || MODPROBE
help
- Support checking for tainted modules. These are usually binary
- only modules that will make the linux-kernel list ignore your
- support request.
+ Generate and parse modules.symbols containing aliases for
+ symbol_request() kernel calls, such as:
+ alias symbol:usb_sg_init usbcore
+ Say Y if unsure.
-endmenu
+config DEFAULT_MODULES_DIR
+ string "Default directory containing modules"
+ default "/lib/modules"
+ depends on DEPMOD || MODPROBE || MODPROBE_SMALL
+ help
+ Directory that contains kernel modules.
+ Defaults to "/lib/modules"
+config DEFAULT_DEPMOD_FILE
+ string "Default name of modules.dep"
+ default "modules.dep"
+ depends on DEPMOD || MODPROBE || MODPROBE_SMALL
+ help
+ Filename that contains kernel modules dependencies.
+ Defaults to "modules.dep"
+
+endmenu
diff --git a/release/src/router/busybox/modutils/Kbuild b/release/src/router/busybox/modutils/Kbuild
new file mode 100644
index 00000000..31f7cbf9
--- /dev/null
+++ b/release/src/router/busybox/modutils/Kbuild
@@ -0,0 +1,14 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o
+lib-$(CONFIG_DEPMOD) += depmod.o modutils.o
+lib-$(CONFIG_INSMOD) += insmod.o modutils.o
+lib-$(CONFIG_LSMOD) += lsmod.o modutils.o
+lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o
+lib-$(CONFIG_RMMOD) += rmmod.o modutils.o
+lib-$(CONFIG_FEATURE_2_4_MODULES) += modutils-24.o
diff --git a/release/src/router/busybox/modutils/Makefile b/release/src/router/busybox/modutils/Makefile
deleted file mode 100644
index 646e4489..00000000
--- a/release/src/router/busybox/modutils/Makefile
+++ /dev/null
@@ -1,30 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-#
-
-TOPDIR:= ../
-MODUTILS_DIR:=./
-include $(TOPDIR).config
-include $(TOPDIR)Rules.mak
-include Makefile.in
-all: $(libraries-y)
--include $(TOPDIR).depend
-
-clean:
- rm -f *.o *.a $(AR_TARGET)
-
diff --git a/release/src/router/busybox/modutils/Makefile.in b/release/src/router/busybox/modutils/Makefile.in
deleted file mode 100755
index defe621f..00000000
--- a/release/src/router/busybox/modutils/Makefile.in
+++ /dev/null
@@ -1,37 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-#
-
-MODUTILS_AR:=modutils.a
-ifndef $(MODUTILS_DIR)
-MODUTILS_DIR:=$(TOPDIR)modutils/
-endif
-
-MODUTILS-y:=
-MODUTILS-$(CONFIG_INSMOD) += insmod.o
-MODUTILS-$(CONFIG_LSMOD) += lsmod.o
-MODUTILS-$(CONFIG_MODPROBE) += modprobe.o
-MODUTILS-$(CONFIG_RMMOD) += rmmod.o
-
-modutils/insmod.o: OPTIMIZATIONS=-g
-
-libraries-y+=$(MODUTILS_DIR)$(MODUTILS_AR)
-
-$(MODUTILS_DIR)$(MODUTILS_AR): $(patsubst %,$(MODUTILS_DIR)%, $(MODUTILS-y))
- $(AR) -ro $@ $(patsubst %,$(MODUTILS_DIR)%, $(MODUTILS-y))
-
diff --git a/release/src/router/busybox/modutils/depmod.c b/release/src/router/busybox/modutils/depmod.c
new file mode 100644
index 00000000..5ec2a51d
--- /dev/null
+++ b/release/src/router/busybox/modutils/depmod.c
@@ -0,0 +1,245 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * depmod - generate modules.dep
+ * Copyright (c) 2008 Bernhard Reutner-Fischer
+ * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
+ * Copyright (c) 2008 Vladimir Dronnikov
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#undef _GNU_SOURCE
+#define _GNU_SOURCE
+#include <libbb.h>
+#include <sys/utsname.h> /* uname() */
+#include "modutils.h"
+
+/*
+ * Theory of operation:
+ * - iterate over all modules and record their full path
+ * - iterate over all modules looking for "depends=" entries
+ * for each depends, look through our list of full paths and emit if found
+ */
+
+typedef struct module_info {
+ struct module_info *next;
+ char *name, *modname;
+ llist_t *dependencies;
+ llist_t *aliases;
+ llist_t *symbols;
+ struct module_info *dnext, *dprev;
+} module_info;
+
+enum {
+ ARG_a = (1<<0), /* All modules, ignore mods in argv */
+ ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
+ ARG_b = (1<<2), /* base directory when modules are in staging area */
+ ARG_e = (1<<3), /* with -F, print unresolved symbols */
+ ARG_F = (1<<4), /* System.map that contains the symbols */
+ ARG_n = (1<<5), /* dry-run, print to stdout only */
+ ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */
+};
+
+static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
+ void *data, int depth UNUSED_PARAM)
+{
+ char modname[MODULE_NAME_LEN];
+ module_info **first = (module_info **) data;
+ char *image, *ptr;
+ module_info *info;
+ /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
+ size_t len = (64*1024*1024 - 4096);
+
+ if (strrstr(fname, ".ko") == NULL)
+ return TRUE;
+
+ image = xmalloc_open_zipped_read_close(fname, &len);
+ info = xzalloc(sizeof(*info));
+
+ info->next = *first;
+ *first = info;
+
+ info->dnext = info->dprev = info;
+ info->name = xasprintf("/%s", fname);
+ info->modname = xstrdup(filename2modname(fname, modname));
+ for (ptr = image; ptr < image + len - 10; ptr++) {
+ if (strncmp(ptr, "depends=", 8) == 0) {
+ char *u;
+
+ ptr += 8;
+ for (u = ptr; *u; u++)
+ if (*u == '-')
+ *u = '_';
+ ptr += string_to_llist(ptr, &info->dependencies, ",");
+ } else if (ENABLE_FEATURE_MODUTILS_ALIAS
+ && strncmp(ptr, "alias=", 6) == 0
+ ) {
+ llist_add_to(&info->aliases, xstrdup(ptr + 6));
+ ptr += strlen(ptr);
+ } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
+ && strncmp(ptr, "__ksymtab_", 10) == 0
+ ) {
+ ptr += 10;
+ if (strncmp(ptr, "gpl", 3) == 0 ||
+ strcmp(ptr, "strings") == 0)
+ continue;
+ llist_add_to(&info->symbols, xstrdup(ptr));
+ ptr += strlen(ptr);
+ }
+ }
+ free(image);
+
+ return TRUE;
+}
+
+static module_info *find_module(module_info *modules, const char *modname)
+{
+ module_info *m;
+
+ for (m = modules; m != NULL; m = m->next)
+ if (strcmp(m->modname, modname) == 0)
+ return m;
+ return NULL;
+}
+
+static void order_dep_list(module_info *modules, module_info *start,
+ llist_t *add)
+{
+ module_info *m;
+ llist_t *n;
+
+ for (n = add; n != NULL; n = n->link) {
+ m = find_module(modules, n->data);
+ if (m == NULL)
+ continue;
+
+ /* unlink current entry */
+ m->dnext->dprev = m->dprev;
+ m->dprev->dnext = m->dnext;
+
+ /* and add it to tail */
+ m->dnext = start;
+ m->dprev = start->dprev;
+ start->dprev->dnext = m;
+ start->dprev = m;
+
+ /* recurse */
+ order_dep_list(modules, start, m->dependencies);
+ }
+}
+
+static void xfreopen_write(const char *file, FILE *f)
+{
+ if (freopen(file, "w", f) == NULL)
+ bb_perror_msg_and_die("can't open '%s'", file);
+}
+
+int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int depmod_main(int argc UNUSED_PARAM, char **argv)
+{
+ module_info *modules = NULL, *m, *dep;
+ const char *moddir_base = "/";
+ char *moddir, *version;
+ struct utsname uts;
+ int tmp;
+
+ getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
+ argv += optind;
+
+ /* goto modules location */
+ xchdir(moddir_base);
+
+ /* If a version is provided, then that kernel version's module directory
+ * is used, rather than the current kernel version (as returned by
+ * "uname -r"). */
+ if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
+ version = *argv++;
+ } else {
+ uname(&uts);
+ version = uts.release;
+ }
+ moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
+
+ /* Scan modules */
+ if (*argv) {
+ char *modfile;
+ struct stat sb;
+ do {
+ modfile = concat_path_file(moddir, *argv);
+ xstat(modfile, &sb);
+ parse_module(modfile, &sb, &modules, 0);
+ free(modfile);
+ } while (*(++argv));
+ } else {
+ recursive_action(moddir, ACTION_RECURSE,
+ parse_module, NULL, &modules, 0);
+ }
+
+ /* Prepare for writing out the dep files */
+ xchdir(moddir);
+ if (ENABLE_FEATURE_CLEAN_UP)
+ free(moddir);
+
+ /* Generate dependency and alias files */
+ if (!(option_mask32 & ARG_n))
+ xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
+ for (m = modules; m != NULL; m = m->next) {
+ printf("%s:", m->name);
+
+ order_dep_list(modules, m, m->dependencies);
+ while (m->dnext != m) {
+ dep = m->dnext;
+ printf(" %s", dep->name);
+
+ /* unlink current entry */
+ dep->dnext->dprev = dep->dprev;
+ dep->dprev->dnext = dep->dnext;
+ dep->dnext = dep->dprev = dep;
+ }
+ bb_putchar('\n');
+ }
+
+#if ENABLE_FEATURE_MODUTILS_ALIAS
+ if (!(option_mask32 & ARG_n))
+ xfreopen_write("modules.alias", stdout);
+ for (m = modules; m != NULL; m = m->next) {
+ const char *fname = bb_basename(m->name);
+ int fnlen = strchrnul(fname, '.') - fname;
+ while (m->aliases) {
+ /* Last word can well be m->modname instead,
+ * but depmod from module-init-tools 3.4
+ * uses module basename, i.e., no s/-/_/g.
+ * (pathname and .ko.* are still stripped)
+ * Mimicking that... */
+ printf("alias %s %.*s\n",
+ (char*)llist_pop(&m->aliases),
+ fnlen, fname);
+ }
+ }
+#endif
+#if ENABLE_FEATURE_MODUTILS_SYMBOLS
+ if (!(option_mask32 & ARG_n))
+ xfreopen_write("modules.symbols", stdout);
+ for (m = modules; m != NULL; m = m->next) {
+ const char *fname = bb_basename(m->name);
+ int fnlen = strchrnul(fname, '.') - fname;
+ while (m->symbols) {
+ printf("alias symbol:%s %.*s\n",
+ (char*)llist_pop(&m->symbols),
+ fnlen, fname);
+ }
+ }
+#endif
+
+ if (ENABLE_FEATURE_CLEAN_UP) {
+ while (modules) {
+ module_info *old = modules;
+ modules = modules->next;
+ free(old->name);
+ free(old->modname);
+ free(old);
+ }
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/release/src/router/busybox/modutils/insmod.c b/release/src/router/busybox/modutils/insmod.c
index e917b9b0..90ed87a7 100644
--- a/release/src/router/busybox/modutils/insmod.c
+++ b/release/src/router/busybox/modutils/insmod.c
@@ -2,4228 +2,40 @@
/*
* Mini insmod implementation for busybox
*
- * This version of insmod supports x86, ARM, SH3/4, powerpc, m68k,
- * MIPS, and v850e.
+ * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
*
- * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
- * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
- * and Ron Alder <alder@lineo.com>
- *
- * Miles Bader <miles@gnu.org> added NEC V850E support.
- *
- * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
- * and (theoretically) SH3. I have only tested SH4 in little endian mode.
- *
- * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
- * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
- * very minor changes required to also work with StrongArm and presumably
- * all ARM based systems.
- *
- * Magnus Damm <damm@opensource.se> 22-May-2002.
- * The plt and got code are now using the same structs.
- * Added generic linked list code to fully support PowerPC.
- * Replaced the mess in arch_apply_relocation() with architecture blocks.
- * The arch_create_got() function got cleaned up with architecture blocks.
- * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
- *
- * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
- * PowerPC specific code stolen from modutils-2.3.16,
- * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
- * I've only tested the code on mpc8xx platforms in big-endian mode.
- * Did some cleanup and added CONFIG_USE_xxx_ENTRIES...
- *
- * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
- * based on modutils-2.4.2
- * MIPS specific support for Elf loading and relocation.
- * Copyright 1996, 1997 Linux International.
- * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
- *
- * Based almost entirely on the Linux modutils-2.3.11 implementation.
- * Copyright 1996, 1997 Linux International.
- * New implementation contributed by Richard Henderson <rth@tamu.edu>
- * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
- * Restructured (and partly rewritten) by:
- * Björn Ekwall <bj0rn@blox.se> February 1999
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <errno.h>
-#include <unistd.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <assert.h>
-#include <string.h>
-#include <getopt.h>
-#include <fcntl.h>
-#include <sys/utsname.h>
-#include "busybox.h"
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-# undef CONFIG_FEATURE_OLD_MODULE_INTERFACE
-# define new_sys_init_module init_module
-#else
-# define old_sys_init_module init_module
-#endif
-
-#ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
-#define LOADBITS 0
-#else
-#define LOADBITS 1
-#endif
-
-
-#if defined(__arm__)
-#define CONFIG_USE_PLT_ENTRIES
-#define CONFIG_PLT_ENTRY_SIZE 8
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 8
-#define CONFIG_USE_SINGLE
-
-#define MATCH_MACHINE(x) (x == EM_ARM)
-#define SHT_RELM SHT_REL
-#define Elf32_RelM Elf32_Rel
-#define ELFCLASSM ELFCLASS32
-#endif
-
-#if defined(__s390__)
-#define CONFIG_USE_PLT_ENTRIES
-#define CONFIG_PLT_ENTRY_SIZE 8
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 8
-#define CONFIG_USE_SINGLE
-
-#define MATCH_MACHINE(x) (x == EM_S390)
-#define SHT_RELM SHT_RELA
-#define Elf32_RelM Elf32_Rela
-#define ELFCLASSM ELFCLASS32
-#endif
-
-#if defined(__i386__)
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 4
-#define CONFIG_USE_SINGLE
-
-#ifndef EM_486
-#define MATCH_MACHINE(x) (x == EM_386)
-#else
-#define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
-#endif
-
-#define SHT_RELM SHT_REL
-#define Elf32_RelM Elf32_Rel
-#define ELFCLASSM ELFCLASS32
-#endif
-
-#if defined(__mc68000__)
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 4
-#define CONFIG_USE_SINGLE
-
-#define MATCH_MACHINE(x) (x == EM_68K)
-#define SHT_RELM SHT_RELA
-#define Elf32_RelM Elf32_Rela
-#endif
-
-#if 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
-#define ELFCLASSM ELFCLASS32
-#define ARCHDATAM "__dbe_table"
-#endif
-
-#if defined(__powerpc__)
-#define CONFIG_USE_PLT_ENTRIES
-#define CONFIG_PLT_ENTRY_SIZE 16
-#define CONFIG_USE_PLT_LIST
-#define CONFIG_LIST_ARCHTYPE ElfW(Addr)
-#define CONFIG_USE_LIST
-
-#define MATCH_MACHINE(x) (x == EM_PPC)
-#define SHT_RELM SHT_RELA
-#define Elf32_RelM Elf32_Rela
-#define ELFCLASSM ELFCLASS32
-#define ARCHDATAM "__ftr_fixup"
-#endif
-
-#if defined(__sh__)
-#define CONFIG_USE_GOT_ENTRIES
-#define CONFIG_GOT_ENTRY_SIZE 4
-#define CONFIG_USE_SINGLE
-
-#define MATCH_MACHINE(x) (x == EM_SH)
-#define SHT_RELM SHT_RELA
-#define Elf32_RelM Elf32_Rela
-#define ELFCLASSM ELFCLASS32
-
-/* the SH changes have only been tested on the SH4 in =little endian= mode */
-/* I'm not sure about big endian, so let's warn: */
-
-#if (defined(__SH4__) || defined(__SH3__)) && defined(__BIG_ENDIAN__)
-#error insmod.c may require changes for use on big endian SH4/SH3
-#endif
-
-/* it may or may not work on the SH1/SH2... So let's error on those
- also */
-#if (defined(__sh__) && (!(defined(__SH3__) || defined(__SH4__))))
-#error insmod.c may require changes for non-SH3/SH4 use
-#endif
-#endif
-
-#if defined (__v850e__)
-#define CONFIG_USE_PLT_ENTRIES
-#define CONFIG_PLT_ENTRY_SIZE 8
-#define CONFIG_USE_SINGLE
-
-#ifndef EM_CYGNUS_V850 /* grumble */
-#define EM_CYGNUS_V850 0x9080
-#endif
-
-#define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
-#define SHT_RELM SHT_RELA
-#define Elf32_RelM Elf32_Rela
-#define ELFCLASSM ELFCLASS32
-
-#define SYMBOL_PREFIX "_"
-#endif
-
-#ifndef SHT_RELM
-#error Sorry, but insmod.c does not yet support this architecture...
-#endif
-
-
-//----------------------------------------------------------------------------
-//--------modutils module.h, lines 45-242
-//----------------------------------------------------------------------------
-
-/* Definitions for the Linux module syscall interface.
- Copyright 1996, 1997 Linux International.
-
- Contributed by Richard Henderson <rth@tamu.edu>
-
- This file is part of the Linux modutils.
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-
-
-#ifndef MODUTILS_MODULE_H
-static const int MODUTILS_MODULE_H = 1;
-
-#ident "$Id: insmod.c,v 1.1.3.1 2004/12/29 07:07:45 honor 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
- to be dependant on a particular kernel version to compile insmod. */
-
-
-/*======================================================================*/
-/* The structures used by Linux 2.0. */
-
-/* The symbol format used by get_kernel_syms(2). */
-struct old_kernel_sym
-{
- unsigned long value;
- char name[60];
-};
-
-struct old_module_ref
-{
- unsigned long module; /* kernel addresses */
- unsigned long next;
-};
-
-struct old_module_symbol
-{
- unsigned long addr;
- unsigned long name;
-};
-
-struct old_symbol_table
-{
- int size; /* total, including string table!!! */
- int n_symbols;
- int n_refs;
- struct old_module_symbol symbol[0]; /* actual size defined by n_symbols */
- struct old_module_ref ref[0]; /* actual size defined by n_refs */
-};
-
-struct old_mod_routines
-{
- unsigned long init;
- unsigned long cleanup;
-};
-
-struct old_module
-{
- unsigned long next;
- unsigned long ref; /* the list of modules that refer to me */
- unsigned long symtab;
- unsigned long name;
- int size; /* size of module in pages */
- unsigned long addr; /* address of module */
- int state;
- unsigned long cleanup; /* cleanup routine */
-};
-
-/* Sent to init_module(2) or'ed into the code size parameter. */
-static const int OLD_MOD_AUTOCLEAN = 0x40000000; /* big enough, but no sign problems... */
-
-int get_kernel_syms(struct old_kernel_sym *);
-int old_sys_init_module(const char *name, char *code, unsigned codesize,
- struct old_mod_routines *, struct old_symbol_table *);
-
-/*======================================================================*/
-/* For sizeof() which are related to the module platform and not to the
- environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
-
-#define tgt_sizeof_char sizeof(char)
-#define tgt_sizeof_short sizeof(short)
-#define tgt_sizeof_int sizeof(int)
-#define tgt_sizeof_long sizeof(long)
-#define tgt_sizeof_char_p sizeof(char *)
-#define tgt_sizeof_void_p sizeof(void *)
-#define tgt_long long
-
-#if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
-#undef tgt_sizeof_long
-#undef tgt_sizeof_char_p
-#undef tgt_sizeof_void_p
-#undef tgt_long
-static const int tgt_sizeof_long = 8;
-static const int tgt_sizeof_char_p = 8;
-static const int tgt_sizeof_void_p = 8;
-#define tgt_long long long
-#endif
-
-/*======================================================================*/
-/* The structures used in Linux 2.1. */
-
-/* Note: new_module_symbol does not use tgt_long intentionally */
-struct new_module_symbol
-{
- unsigned long value;
- unsigned long name;
-};
-
-struct new_module_persist;
-
-struct new_module_ref
-{
- unsigned tgt_long dep; /* kernel addresses */
- unsigned tgt_long ref;
- unsigned tgt_long next_ref;
-};
-
-struct new_module
-{
- unsigned tgt_long size_of_struct; /* == sizeof(module) */
- unsigned tgt_long next;
- unsigned tgt_long name;
- unsigned tgt_long size;
-
- tgt_long usecount;
- unsigned tgt_long flags; /* AUTOCLEAN et al */
-
- unsigned nsyms;
- unsigned ndeps;
-
- unsigned tgt_long syms;
- unsigned tgt_long deps;
- unsigned tgt_long refs;
- unsigned tgt_long init;
- unsigned tgt_long cleanup;
- unsigned tgt_long ex_table_start;
- unsigned tgt_long ex_table_end;
-#ifdef __alpha__
- unsigned tgt_long gp;
-#endif
- /* Everything after here is extension. */
- unsigned tgt_long persist_start;
- unsigned tgt_long persist_end;
- unsigned tgt_long can_unload;
- unsigned tgt_long runsize;
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
- const char *kallsyms_start; /* All symbols for kernel debugging */
- const char *kallsyms_end;
- const char *archdata_start; /* arch specific data for module */
- const char *archdata_end;
- const char *kernel_data; /* Reserved for kernel internal use */
-#endif
-};
-
-#ifdef ARCHDATAM
-#define ARCHDATA_SEC_NAME ARCHDATAM
-#else
-#define ARCHDATA_SEC_NAME "__archdata"
-#endif
-#define KALLSYMS_SEC_NAME "__kallsyms"
-
-
-struct new_module_info
-{
- unsigned long addr;
- unsigned long size;
- unsigned long flags;
- long usecount;
-};
-
-/* Bits of module.flags. */
-static const int NEW_MOD_RUNNING = 1;
-static const int NEW_MOD_DELETED = 2;
-static const int NEW_MOD_AUTOCLEAN = 4;
-static const int NEW_MOD_VISITED = 8;
-static const int NEW_MOD_USED_ONCE = 16;
-
-int new_sys_init_module(const char *name, const struct new_module *);
-int query_module(const char *name, int which, void *buf, size_t bufsize,
- size_t *ret);
-
-/* Values for query_module's which. */
-
-static const int QM_MODULES = 1;
-static const int QM_DEPS = 2;
-static const int QM_REFS = 3;
-static const int QM_SYMBOLS = 4;
-static const int QM_INFO = 5;
-
-/*======================================================================*/
-/* The system calls unchanged between 2.0 and 2.1. */
-
-unsigned long create_module(const char *, size_t);
-int delete_module(const char *);
-
-
-#endif /* module.h */
-
-//----------------------------------------------------------------------------
-//--------end of modutils module.h
-//----------------------------------------------------------------------------
-
-
-
-//----------------------------------------------------------------------------
-//--------modutils obj.h, lines 253-462
-//----------------------------------------------------------------------------
-
-/* Elf object file loading and relocation routines.
- Copyright 1996, 1997 Linux International.
-
- Contributed by Richard Henderson <rth@tamu.edu>
-
- This file is part of the Linux modutils.
-
- This program is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 2 of the License, or (at your
- option) any later version.
-
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
-
-
-#ifndef MODUTILS_OBJ_H
-static const int MODUTILS_OBJ_H = 1;
-
-#ident "$Id: insmod.c,v 1.1.3.1 2004/12/29 07:07:45 honor Exp $"
-
-/* The relocatable object is manipulated using elfin types. */
-
-#include <stdio.h>
-#include <elf.h>
-#include <endian.h>
-
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-#define ELFDATAM ELFDATA2LSB
-#elif __BYTE_ORDER == __BIG_ENDIAN
-#define ELFDATAM ELFDATA2MSB
-#endif
-
-#ifndef ElfW
-# if ELFCLASSM == ELFCLASS32
-# define ElfW(x) Elf32_ ## x
-# define ELFW(x) ELF32_ ## x
-# else
-# define ElfW(x) Elf64_ ## x
-# define ELFW(x) ELF64_ ## x
-# endif
-#endif
-
-/* For some reason this is missing from some ancient C libraries.... */
-#ifndef ELF32_ST_INFO
-# define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
-#endif
-
-#ifndef ELF64_ST_INFO
-# define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
-#endif
-
-struct obj_string_patch;
-struct obj_symbol_patch;
-
-struct obj_section
-{
- ElfW(Shdr) header;
- const char *name;
- char *contents;
- struct obj_section *load_next;
- int idx;
-};
-
-struct obj_symbol
-{
- struct obj_symbol *next; /* hash table link */
- const char *name;
- unsigned long value;
- unsigned long size;
- int secidx; /* the defining section index/module */
- int info;
- int ksymidx; /* for export to the kernel symtab */
- int referenced; /* actually used in the link */
-};
-
-/* Hardcode the hash table size. We shouldn't be needing so many
- symbols that we begin to degrade performance, and we get a big win
- by giving the compiler a constant divisor. */
-
-#define HASH_BUCKETS 521
-
-struct obj_file
-{
- ElfW(Ehdr) header;
- ElfW(Addr) baseaddr;
- struct obj_section **sections;
- struct obj_section *load_order;
- struct obj_section **load_order_search_start;
- struct obj_string_patch *string_patches;
- struct obj_symbol_patch *symbol_patches;
- int (*symbol_cmp)(const char *, const char *);
- unsigned long (*symbol_hash)(const char *);
- unsigned long local_symtab_size;
- struct obj_symbol **local_symtab;
- struct obj_symbol *symtab[HASH_BUCKETS];
-};
-
-enum obj_reloc
-{
- obj_reloc_ok,
- obj_reloc_overflow,
- obj_reloc_dangerous,
- obj_reloc_unhandled
-};
-
-struct obj_string_patch
-{
- struct obj_string_patch *next;
- int reloc_secidx;
- ElfW(Addr) reloc_offset;
- ElfW(Addr) string_offset;
-};
-
-struct obj_symbol_patch
-{
- struct obj_symbol_patch *next;
- int reloc_secidx;
- ElfW(Addr) reloc_offset;
- struct obj_symbol *sym;
-};
-
-
-/* Generic object manipulation routines. */
-
-static unsigned long obj_elf_hash(const char *);
-
-static unsigned long obj_elf_hash_n(const char *, unsigned long len);
-
-static struct obj_symbol *obj_find_symbol (struct obj_file *f,
- const char *name);
-
-static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
- struct obj_symbol *sym);
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-static void obj_set_symbol_compare(struct obj_file *f,
- int (*cmp)(const char *, const char *),
- unsigned long (*hash)(const char *));
-#endif
-
-static struct obj_section *obj_find_section (struct obj_file *f,
- const char *name);
-
-static void obj_insert_section_load_order (struct obj_file *f,
- struct obj_section *sec);
-
-static struct obj_section *obj_create_alloced_section (struct obj_file *f,
- const char *name,
- unsigned long align,
- unsigned long size);
-
-static struct obj_section *obj_create_alloced_section_first (struct obj_file *f,
- const char *name,
- unsigned long align,
- unsigned long size);
-
-static void *obj_extend_section (struct obj_section *sec, unsigned long more);
-
-static int obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
- const char *string);
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
- struct obj_symbol *sym);
-#endif
-
-static int obj_check_undefineds(struct obj_file *f);
-
-static void obj_allocate_commons(struct obj_file *f);
-
-static unsigned long obj_load_size (struct obj_file *f);
-
-static int obj_relocate (struct obj_file *f, ElfW(Addr) base);
-
-static struct obj_file *obj_load(FILE *f, int loadprogbits);
-
-static int obj_create_image (struct obj_file *f, char *image);
-
-/* Architecture specific manipulation routines. */
-
-static struct obj_file *arch_new_file (void);
-
-static struct obj_section *arch_new_section (void);
-
-static struct obj_symbol *arch_new_symbol (void);
-
-static enum obj_reloc arch_apply_relocation (struct obj_file *f,
- struct obj_section *targsec,
- struct obj_section *symsec,
- struct obj_symbol *sym,
- ElfW(RelM) *rel, ElfW(Addr) value);
-
-static void arch_create_got (struct obj_file *f);
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int arch_init_module (struct obj_file *f, struct new_module *);
-#endif
-
-#endif /* obj.h */
-//----------------------------------------------------------------------------
-//--------end of modutils obj.h
-//----------------------------------------------------------------------------
-
-
-
-/* SPFX is always a string, so it can be concatenated to string constants. */
-#ifdef SYMBOL_PREFIX
-#define SPFX SYMBOL_PREFIX
-#else
-#define SPFX ""
-#endif
-
-
-#define _PATH_MODULES "/lib/modules"
-static const int STRVERSIONLEN = 32;
-
-/*======================================================================*/
-
-static int flag_force_load = 0;
-static int flag_autoclean = 0;
-static int flag_verbose = 0;
-static int flag_quiet = 0;
-static int flag_export = 1;
-
-
-/*======================================================================*/
-
-#if defined(CONFIG_USE_LIST)
-
-struct arch_list_entry
-{
- struct arch_list_entry *next;
- CONFIG_LIST_ARCHTYPE addend;
- int offset;
- int inited : 1;
-};
-
-#endif
-
-#if defined(CONFIG_USE_SINGLE)
-
-struct arch_single_entry
-{
- int offset;
- int inited : 1;
- int allocated : 1;
-};
-
-#endif
-
-#if defined(__mips__)
-struct mips_hi16
-{
- struct mips_hi16 *next;
- Elf32_Addr *addr;
- Elf32_Addr value;
-};
-#endif
-
-struct arch_file {
- struct obj_file root;
-#if defined(CONFIG_USE_PLT_ENTRIES)
- struct obj_section *plt;
-#endif
-#if defined(CONFIG_USE_GOT_ENTRIES)
- struct obj_section *got;
-#endif
-#if defined(__mips__)
- struct mips_hi16 *mips_hi16_list;
-#endif
-};
-
-struct arch_symbol {
- struct obj_symbol root;
-#if defined(CONFIG_USE_PLT_ENTRIES)
-#if defined(CONFIG_USE_PLT_LIST)
- struct arch_list_entry *pltent;
-#else
- struct arch_single_entry pltent;
-#endif
-#endif
-#if defined(CONFIG_USE_GOT_ENTRIES)
- struct arch_single_entry gotent;
-#endif
-};
-
-
-struct external_module {
- const char *name;
- ElfW(Addr) addr;
- int used;
- size_t nsyms;
- struct new_module_symbol *syms;
-};
-
-static struct new_module_symbol *ksyms;
-static size_t nksyms;
-
-static struct external_module *ext_modules;
-static int n_ext_modules;
-static int n_ext_modules_used;
-extern int delete_module(const char *);
-
-static char *m_filename;
-static char *m_fullName;
-
-
-
-/*======================================================================*/
-
-
-static int check_module_name_match(const char *filename, struct stat *statbuf,
- void *userdata)
-{
- char *fullname = (char *) userdata;
-
- if (fullname[0] == '\0')
- return (FALSE);
- else {
- char *tmp, *tmp1 = bb_xstrdup(filename);
- tmp = bb_get_last_path_component(tmp1);
- if (strcmp(tmp, fullname) == 0) {
- free(tmp1);
- /* Stop searching if we find a match */
- m_filename = bb_xstrdup(filename);
- return (TRUE);
- }
- free(tmp1);
- }
- return (FALSE);
-}
-
-
-/*======================================================================*/
-
-static struct obj_file *arch_new_file(void)
-{
- struct arch_file *f;
- f = xmalloc(sizeof(*f));
-
- memset(f, 0, sizeof(*f));
-
- return &f->root;
-}
-
-static struct obj_section *arch_new_section(void)
-{
- return xmalloc(sizeof(struct obj_section));
-}
-
-static struct obj_symbol *arch_new_symbol(void)
-{
- struct arch_symbol *sym;
- sym = xmalloc(sizeof(*sym));
-
- memset(sym, 0, sizeof(*sym));
-
- return &sym->root;
-}
-
-static enum obj_reloc
-arch_apply_relocation(struct obj_file *f,
- struct obj_section *targsec,
- struct obj_section *symsec,
- struct obj_symbol *sym,
- ElfW(RelM) *rel, ElfW(Addr) v)
-{
- struct arch_file *ifile = (struct arch_file *) f;
- enum obj_reloc ret = obj_reloc_ok;
- ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
- ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
-#if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
- struct arch_symbol *isym = (struct arch_symbol *) sym;
-#endif
-#if defined(CONFIG_USE_GOT_ENTRIES)
- ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
-#endif
-#if defined(CONFIG_USE_PLT_ENTRIES)
- ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
- unsigned long *ip;
-#if defined(CONFIG_USE_PLT_LIST)
- struct arch_list_entry *pe;
-#else
- struct arch_single_entry *pe;
-#endif
-#endif
-
- switch (ELF32_R_TYPE(rel->r_info)) {
-
-
-#if defined(__arm__)
- case R_ARM_NONE:
- break;
-
- case R_ARM_ABS32:
- *loc += v;
- break;
-
- case R_ARM_GOT32:
- goto bb_use_got;
-
- case R_ARM_GOTPC:
- /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
- * (which is .got) similar to branch,
- * but is full 32 bits relative */
-
- assert(got);
- *loc += got - dot;
- break;
-
- case R_ARM_PC24:
- case R_ARM_PLT32:
- goto bb_use_plt;
-
- case R_ARM_GOTOFF: /* address relative to the got */
- assert(got);
- *loc += v - got;
- break;
-
-#elif defined(__s390__)
- case R_390_32:
- *(unsigned int *) loc += v;
- break;
- case R_390_16:
- *(unsigned short *) loc += v;
- break;
- case R_390_8:
- *(unsigned char *) loc += v;
- break;
-
- case R_390_PC32:
- *(unsigned int *) loc += v - dot;
- break;
- case R_390_PC16DBL:
- *(unsigned short *) loc += (v - dot) >> 1;
- break;
- case R_390_PC16:
- *(unsigned short *) loc += v - dot;
- break;
-
- case R_390_PLT32:
- case R_390_PLT16DBL:
- /* find the plt entry and initialize it. */
- assert(isym != NULL);
- pe = (struct arch_single_entry *) &isym->pltent;
- assert(pe->allocated);
- if (pe->inited == 0) {
- ip = (unsigned long *)(ifile->plt->contents + pe->offset);
- ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
- ip[1] = 0x100607f1;
- if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
- ip[2] = v - 2;
- else
- ip[2] = v;
- pe->inited = 1;
- }
-
- /* Insert relative distance to target. */
- v = plt + pe->offset - dot;
- if (ELF32_R_TYPE(rel->r_info) == R_390_PLT32)
- *(unsigned int *) loc = (unsigned int) v;
- else if (ELF32_R_TYPE(rel->r_info) == R_390_PLT16DBL)
- *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
- break;
-
- case R_390_GLOB_DAT:
- case R_390_JMP_SLOT:
- *loc = v;
- break;
-
- case R_390_RELATIVE:
- *loc += f->baseaddr;
- break;
-
- case R_390_GOTPC:
- assert(got != 0);
- *(unsigned long *) loc += got - dot;
- break;
-
- case R_390_GOT12:
- case R_390_GOT16:
- case R_390_GOT32:
- assert(isym != NULL);
- assert(got != 0);
- if (!isym->gotent.inited)
- {
- isym->gotent.inited = 1;
- *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
- }
- if (ELF32_R_TYPE(rel->r_info) == R_390_GOT12)
- *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
- else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT16)
- *(unsigned short *) loc += isym->gotent.offset;
- else if (ELF32_R_TYPE(rel->r_info) == R_390_GOT32)
- *(unsigned int *) loc += isym->gotent.offset;
- break;
-
- case R_390_GOTOFF:
- assert(got != 0);
- *loc += v - got;
- break;
-
-#elif defined(__i386__)
-
- case R_386_NONE:
- break;
-
- case R_386_32:
- *loc += v;
- break;
-
- case R_386_PLT32:
- case R_386_PC32:
- *loc += v - dot;
- break;
-
- case R_386_GLOB_DAT:
- case R_386_JMP_SLOT:
- *loc = v;
- break;
-
- case R_386_RELATIVE:
- *loc += f->baseaddr;
- break;
-
- case R_386_GOTPC:
- assert(got != 0);
- *loc += got - dot;
- break;
-
- case R_386_GOT32:
- goto bb_use_got;
-
- case R_386_GOTOFF:
- assert(got != 0);
- *loc += v - got;
- break;
-
-#elif defined(__mc68000__)
-
- case R_68K_NONE:
- break;
-
- case R_68K_32:
- *loc += v;
- break;
-
- case R_68K_8:
- if (v > 0xff) {
- ret = obj_reloc_overflow;
- }
- *(char *)loc = v;
- break;
-
- case R_68K_16:
- if (v > 0xffff) {
- ret = obj_reloc_overflow;
- }
- *(short *)loc = v;
- break;
-
- case R_68K_PC8:
- v -= dot;
- if ((Elf32_Sword)v > 0x7f ||
- (Elf32_Sword)v < -(Elf32_Sword)0x80) {
- ret = obj_reloc_overflow;
- }
- *(char *)loc = v;
- break;
-
- case R_68K_PC16:
- v -= dot;
- if ((Elf32_Sword)v > 0x7fff ||
- (Elf32_Sword)v < -(Elf32_Sword)0x8000) {
- ret = obj_reloc_overflow;
- }
- *(short *)loc = v;
- break;
-
- case R_68K_PC32:
- *(int *)loc = v - dot;
- break;
-
- case R_68K_GLOB_DAT:
- case R_68K_JMP_SLOT:
- *loc = v;
- break;
-
- case R_68K_RELATIVE:
- *(int *)loc += f->baseaddr;
- break;
-
- case R_68K_GOT32:
- goto bb_use_got;
-
- case R_68K_GOTOFF:
- assert(got != 0);
- *loc += v - got;
- break;
-
-#elif defined(__mips__)
-
- case R_MIPS_NONE:
- break;
-
- case R_MIPS_32:
- *loc += v;
- break;
-
- case R_MIPS_26:
- if (v % 4)
- ret = obj_reloc_dangerous;
- if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
- ret = obj_reloc_overflow;
- *loc =
- (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
- 0x03ffffff);
- break;
-
- case R_MIPS_HI16:
- {
- struct mips_hi16 *n;
-
- /* We cannot relocate this one now because we don't know the value
- of the carry we need to add. Save the information, and let LO16
- do the actual relocation. */
- n = (struct mips_hi16 *) xmalloc(sizeof *n);
- n->addr = loc;
- n->value = v;
- n->next = ifile->mips_hi16_list;
- ifile->mips_hi16_list = n;
- break;
- }
-
- case R_MIPS_LO16:
- {
- unsigned long insnlo = *loc;
- Elf32_Addr val, vallo;
-
- /* Sign extend the addend we extract from the lo insn. */
- vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
-
- if (ifile->mips_hi16_list != NULL) {
- struct mips_hi16 *l;
-
- l = ifile->mips_hi16_list;
- while (l != NULL) {
- struct mips_hi16 *next;
- unsigned long insn;
-
- /* The value for the HI16 had best be the same. */
- assert(v == l->value);
-
- /* Do the HI16 relocation. Note that we actually don't
- need to know anything about the LO16 itself, except where
- to find the low 16 bits of the addend needed by the LO16. */
- insn = *l->addr;
- val =
- ((insn & 0xffff) << 16) +
- vallo;
- val += v;
-
- /* Account for the sign extension that will happen in the
- low bits. */
- val =
- ((val >> 16) +
- ((val & 0x8000) !=
- 0)) & 0xffff;
-
- insn = (insn & ~0xffff) | val;
- *l->addr = insn;
-
- next = l->next;
- free(l);
- l = next;
- }
-
- ifile->mips_hi16_list = NULL;
- }
-
- /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
- val = v + vallo;
- insnlo = (insnlo & ~0xffff) | (val & 0xffff);
- *loc = insnlo;
- break;
- }
-
-#elif defined(__powerpc__)
-
- case R_PPC_ADDR16_HA:
- *(unsigned short *)loc = (v + 0x8000) >> 16;
- break;
-
- case R_PPC_ADDR16_HI:
- *(unsigned short *)loc = v >> 16;
- break;
-
- case R_PPC_ADDR16_LO:
- *(unsigned short *)loc = v;
- break;
-
- case R_PPC_REL24:
- goto bb_use_plt;
-
- case R_PPC_REL32:
- *loc = v - dot;
- break;
-
- case R_PPC_ADDR32:
- *loc = v;
- break;
-
-#elif defined(__sh__)
-
- case R_SH_NONE:
- break;
-
- case R_SH_DIR32:
- *loc += v;
- break;
-
- case R_SH_REL32:
- *loc += v - dot;
- break;
-
- case R_SH_PLT32:
- *loc = v - dot;
- break;
-
- case R_SH_GLOB_DAT:
- case R_SH_JMP_SLOT:
- *loc = v;
- break;
-
- case R_SH_RELATIVE:
- *loc = f->baseaddr + rel->r_addend;
- break;
-
- case R_SH_GOTPC:
- assert(got != 0);
- *loc = got - dot + rel->r_addend;
- break;
-
- case R_SH_GOT32:
- goto bb_use_got;
-
- case R_SH_GOTOFF:
- assert(got != 0);
- *loc = v - got;
- break;
-
-#endif
-
- default:
- printf("Warning: unhandled reloc %d\n",(int)ELF32_R_TYPE(rel->r_info));
- ret = obj_reloc_unhandled;
- break;
-
-#if defined (__v850e__)
- case R_V850_NONE:
- break;
-
- case R_V850_32:
- /* We write two shorts instead of a long because even
- 32-bit insns only need half-word alignment, but
- 32-bit data needs to be long-word aligned. */
- v += ((unsigned short *)loc)[0];
- v += ((unsigned short *)loc)[1] << 16;
- ((unsigned short *)loc)[0] = v & 0xffff;
- ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
- break;
-
- case R_V850_22_PCREL:
- goto bb_use_plt;
-#endif
-
-#if defined(CONFIG_USE_PLT_ENTRIES)
-
- bb_use_plt:
-
- /* find the plt entry and initialize it if necessary */
- assert(isym != NULL);
-
-#if defined(CONFIG_USE_PLT_LIST)
- for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
- pe = pe->next;
- assert(pe != NULL);
-#else
- pe = &isym->pltent;
-#endif
-
- if (! pe->inited) {
- ip = (unsigned long *) (ifile->plt->contents + pe->offset);
-
- /* generate some machine code */
-
-#if defined(__arm__)
- ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
- ip[1] = v; /* sym@ */
-#endif
-#if defined(__powerpc__)
- ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
- ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
- ip[2] = 0x7d6903a6; /* mtctr r11 */
- ip[3] = 0x4e800420; /* bctr */
-#endif
-#if defined (__v850e__)
- /* We have to trash a register, so we assume that any control
- transfer more than 21-bits away must be a function call
- (so we can use a call-clobbered register). */
- ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
- ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
-#endif
- pe->inited = 1;
- }
-
- /* relative distance to target */
- v -= dot;
- /* if the target is too far away.... */
-#if defined (__arm__) || defined (__powerpc__)
- if ((int)v < -0x02000000 || (int)v >= 0x02000000)
-#elif defined (__v850e__)
- if ((Elf32_Sword)v > 0x1fffff || (Elf32_Sword)v < (Elf32_Sword)-0x200000)
-#endif
- /* go via the plt */
- v = plt + pe->offset - dot;
-
-#if defined (__v850e__)
- if (v & 1)
-#else
- if (v & 3)
-#endif
- ret = obj_reloc_dangerous;
-
- /* merge the offset into the instruction. */
-#if defined(__arm__)
- /* Convert to words. */
- v >>= 2;
-
- *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
-#endif
-#if defined(__powerpc__)
- *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
-#endif
-#if defined (__v850e__)
- /* We write two shorts instead of a long because even 32-bit insns
- only need half-word alignment, but the 32-bit data write needs
- to be long-word aligned. */
- ((unsigned short *)loc)[0] =
- (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
- | ((v >> 16) & 0x3f); /* offs high part */
- ((unsigned short *)loc)[1] =
- (v & 0xffff); /* offs low part */
-#endif
- break;
-#endif /* CONFIG_USE_PLT_ENTRIES */
-
-#if defined(CONFIG_USE_GOT_ENTRIES)
- bb_use_got:
-
- assert(isym != NULL);
- /* needs an entry in the .got: set it, once */
- if (!isym->gotent.inited) {
- isym->gotent.inited = 1;
- *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
- }
- /* make the reloc with_respect_to_.got */
-#if defined(__sh__)
- *loc += isym->gotent.offset + rel->r_addend;
-#elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
- *loc += isym->gotent.offset;
-#endif
- break;
-
-#endif /* CONFIG_USE_GOT_ENTRIES */
- }
-
- return ret;
-}
-
-
-#if defined(CONFIG_USE_LIST)
-
-static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
- int offset, int size)
-{
- struct arch_list_entry *pe;
-
- for (pe = *list; pe != NULL; pe = pe->next) {
- if (pe->addend == rel->r_addend) {
- break;
- }
- }
-
- if (pe == NULL) {
- pe = xmalloc(sizeof(struct arch_list_entry));
- pe->next = *list;
- pe->addend = rel->r_addend;
- pe->offset = offset;
- pe->inited = 0;
- *list = pe;
- return size;
- }
- return 0;
-}
-
-#endif
-
-#if defined(CONFIG_USE_SINGLE)
-
-static int arch_single_init(ElfW(RelM) *rel, struct arch_single_entry *single,
- int offset, int size)
-{
- if (single->allocated == 0) {
- single->allocated = 1;
- single->offset = offset;
- single->inited = 0;
- return size;
- }
- return 0;
-}
-
-#endif
-
-#if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
-
-static struct obj_section *arch_xsect_init(struct obj_file *f, char *name,
- int offset, int size)
-{
- struct obj_section *myrelsec = obj_find_section(f, name);
-
- if (offset == 0) {
- offset += size;
- }
-
- if (myrelsec) {
- obj_extend_section(myrelsec, offset);
- } else {
- myrelsec = obj_create_alloced_section(f, name,
- size, offset);
- assert(myrelsec);
- }
-
- return myrelsec;
-}
-
-#endif
-
-static void arch_create_got(struct obj_file *f)
-{
-#if defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES)
- struct arch_file *ifile = (struct arch_file *) f;
- int i;
-#if defined(CONFIG_USE_GOT_ENTRIES)
- int got_offset = 0, got_needed = 0, got_allocate;
-#endif
-#if defined(CONFIG_USE_PLT_ENTRIES)
- int plt_offset = 0, plt_needed = 0, plt_allocate;
-#endif
- struct obj_section *relsec, *symsec, *strsec;
- ElfW(RelM) *rel, *relend;
- ElfW(Sym) *symtab, *extsym;
- const char *strtab, *name;
- struct arch_symbol *intsym;
-
- for (i = 0; i < f->header.e_shnum; ++i) {
- relsec = f->sections[i];
- if (relsec->header.sh_type != SHT_RELM)
- continue;
-
- symsec = f->sections[relsec->header.sh_link];
- strsec = f->sections[symsec->header.sh_link];
-
- rel = (ElfW(RelM) *) relsec->contents;
- relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
- symtab = (ElfW(Sym) *) symsec->contents;
- strtab = (const char *) strsec->contents;
-
- for (; rel < relend; ++rel) {
- extsym = &symtab[ELF32_R_SYM(rel->r_info)];
-
-#if defined(CONFIG_USE_GOT_ENTRIES)
- got_allocate = 0;
-#endif
-#if defined(CONFIG_USE_PLT_ENTRIES)
- plt_allocate = 0;
-#endif
-
- switch (ELF32_R_TYPE(rel->r_info)) {
-#if defined(__arm__)
- case R_ARM_PC24:
- case R_ARM_PLT32:
- plt_allocate = 1;
- break;
-
- case R_ARM_GOTOFF:
- case R_ARM_GOTPC:
- got_needed = 1;
- continue;
-
- case R_ARM_GOT32:
- got_allocate = 1;
- break;
-
-#elif defined(__i386__)
- case R_386_GOTPC:
- case R_386_GOTOFF:
- got_needed = 1;
- continue;
-
- case R_386_GOT32:
- got_allocate = 1;
- break;
-
-#elif defined(__powerpc__)
- case R_PPC_REL24:
- plt_allocate = 1;
- break;
-
-#elif defined(__mc68000__)
- case R_68K_GOT32:
- got_allocate = 1;
- break;
-
- case R_68K_GOTOFF:
- got_needed = 1;
- continue;
-
-#elif defined(__sh__)
- case R_SH_GOT32:
- got_allocate = 1;
- break;
-
- case R_SH_GOTPC:
- case R_SH_GOTOFF:
- got_needed = 1;
- continue;
-
-#elif defined (__v850e__)
- case R_V850_22_PCREL:
- plt_needed = 1;
- break;
-
-#endif
- default:
- continue;
- }
-
- if (extsym->st_name != 0) {
- name = strtab + extsym->st_name;
- } else {
- name = f->sections[extsym->st_shndx]->name;
- }
- intsym = (struct arch_symbol *) obj_find_symbol(f, name);
-#if defined(CONFIG_USE_GOT_ENTRIES)
- if (got_allocate) {
- got_offset += arch_single_init(
- rel, &intsym->gotent,
- got_offset, CONFIG_GOT_ENTRY_SIZE);
-
- got_needed = 1;
- }
-#endif
-#if defined(CONFIG_USE_PLT_ENTRIES)
- if (plt_allocate) {
-#if defined(CONFIG_USE_PLT_LIST)
- plt_offset += arch_list_add(
- rel, &intsym->pltent,
- plt_offset, CONFIG_PLT_ENTRY_SIZE);
-#else
- plt_offset += arch_single_init(
- rel, &intsym->pltent,
- plt_offset, CONFIG_PLT_ENTRY_SIZE);
-#endif
- plt_needed = 1;
- }
-#endif
- }
- }
-
-#if defined(CONFIG_USE_GOT_ENTRIES)
- if (got_needed) {
- ifile->got = arch_xsect_init(f, ".got", got_offset,
- CONFIG_GOT_ENTRY_SIZE);
- }
-#endif
-
-#if defined(CONFIG_USE_PLT_ENTRIES)
- if (plt_needed) {
- ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
- CONFIG_PLT_ENTRY_SIZE);
- }
-#endif
-
-#endif /* defined(CONFIG_USE_GOT_ENTRIES) || defined(CONFIG_USE_PLT_ENTRIES) */
-}
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int arch_init_module(struct obj_file *f, struct new_module *mod)
-{
- return 1;
-}
-#endif
-
-/*======================================================================*/
-
-/* Standard ELF hash function. */
-static inline unsigned long obj_elf_hash_n(const char *name, unsigned long n)
-{
- unsigned long h = 0;
- unsigned long g;
- unsigned char ch;
-
- while (n > 0) {
- ch = *name++;
- h = (h << 4) + ch;
- if ((g = (h & 0xf0000000)) != 0) {
- h ^= g >> 24;
- h &= ~g;
- }
- n--;
- }
- return h;
-}
-
-static unsigned long obj_elf_hash(const char *name)
-{
- return obj_elf_hash_n(name, strlen(name));
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-/* String comparison for non-co-versioned kernel and module. */
-
-static int ncv_strcmp(const char *a, const char *b)
-{
- size_t alen = strlen(a), blen = strlen(b);
-
- if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
- return strncmp(a, b, alen);
- else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
- return strncmp(a, b, blen);
- else
- return strcmp(a, b);
-}
-
-/* String hashing for non-co-versioned kernel and module. Here
- we are simply forced to drop the crc from the hash. */
-
-static unsigned long ncv_symbol_hash(const char *str)
-{
- size_t len = strlen(str);
- if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
- len -= 10;
- return obj_elf_hash_n(str, len);
-}
-
-static void
-obj_set_symbol_compare(struct obj_file *f,
- int (*cmp) (const char *, const char *),
- unsigned long (*hash) (const char *))
-{
- if (cmp)
- f->symbol_cmp = cmp;
- if (hash) {
- struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
- int i;
-
- f->symbol_hash = hash;
-
- memcpy(tmptab, f->symtab, sizeof(tmptab));
- memset(f->symtab, 0, sizeof(f->symtab));
-
- for (i = 0; i < HASH_BUCKETS; ++i)
- for (sym = tmptab[i]; sym; sym = next) {
- unsigned long h = hash(sym->name) % HASH_BUCKETS;
- next = sym->next;
- sym->next = f->symtab[h];
- f->symtab[h] = sym;
- }
- }
-}
-
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
-static struct obj_symbol *
-obj_add_symbol(struct obj_file *f, const char *name,
- unsigned long symidx, int info,
- int secidx, ElfW(Addr) value,
- unsigned long size)
-{
- struct obj_symbol *sym;
- unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
- int n_type = ELFW(ST_TYPE) (info);
- int n_binding = ELFW(ST_BIND) (info);
-
- for (sym = f->symtab[hash]; sym; sym = sym->next)
- if (f->symbol_cmp(sym->name, name) == 0) {
- int o_secidx = sym->secidx;
- int o_info = sym->info;
- int o_type = ELFW(ST_TYPE) (o_info);
- int o_binding = ELFW(ST_BIND) (o_info);
-
- /* A redefinition! Is it legal? */
-
- if (secidx == SHN_UNDEF)
- return sym;
- else if (o_secidx == SHN_UNDEF)
- goto found;
- else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
- /* Cope with local and global symbols of the same name
- in the same object file, as might have been created
- by ld -r. The only reason locals are now seen at this
- level at all is so that we can do semi-sensible things
- with parameters. */
-
- struct obj_symbol *nsym, **p;
-
- nsym = arch_new_symbol();
- nsym->next = sym->next;
- nsym->ksymidx = -1;
-
- /* Excise the old (local) symbol from the hash chain. */
- for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
- continue;
- *p = sym = nsym;
- goto found;
- } else if (n_binding == STB_LOCAL) {
- /* Another symbol of the same name has already been defined.
- Just add this to the local table. */
- sym = arch_new_symbol();
- sym->next = NULL;
- sym->ksymidx = -1;
- f->local_symtab[symidx] = sym;
- goto found;
- } else if (n_binding == STB_WEAK)
- return sym;
- else if (o_binding == STB_WEAK)
- goto found;
- /* Don't unify COMMON symbols with object types the programmer
- doesn't expect. */
- else if (secidx == SHN_COMMON
- && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
- return sym;
- else if (o_secidx == SHN_COMMON
- && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
- goto found;
- else {
- /* Don't report an error if the symbol is coming from
- the kernel or some external module. */
- if (secidx <= SHN_HIRESERVE)
- bb_error_msg("%s multiply defined", name);
- return sym;
- }
- }
-
- /* Completely new symbol. */
- sym = arch_new_symbol();
- sym->next = f->symtab[hash];
- f->symtab[hash] = sym;
- sym->ksymidx = -1;
-
- if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
- if (symidx >= f->local_symtab_size)
- bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
- name, (long) symidx, (long) f->local_symtab_size);
- else
- f->local_symtab[symidx] = sym;
- }
-
- found:
- sym->name = name;
- sym->value = value;
- sym->size = size;
- sym->secidx = secidx;
- sym->info = info;
-
- return sym;
-}
-
-static struct obj_symbol *
-obj_find_symbol(struct obj_file *f, const char *name)
-{
- struct obj_symbol *sym;
- unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
-
- for (sym = f->symtab[hash]; sym; sym = sym->next)
- if (f->symbol_cmp(sym->name, name) == 0)
- return sym;
-
- return NULL;
-}
-
-static ElfW(Addr)
- obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
-{
- if (sym) {
- if (sym->secidx >= SHN_LORESERVE)
- return sym->value;
-
- return sym->value + f->sections[sym->secidx]->header.sh_addr;
- } else {
- /* As a special case, a NULL sym has value zero. */
- return 0;
- }
-}
-
-static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
-{
- int i, n = f->header.e_shnum;
-
- for (i = 0; i < n; ++i)
- if (strcmp(f->sections[i]->name, name) == 0)
- return f->sections[i];
-
- return NULL;
-}
-
-static int obj_load_order_prio(struct obj_section *a)
-{
- unsigned long af, ac;
-
- af = a->header.sh_flags;
-
- ac = 0;
- if (a->name[0] != '.' || strlen(a->name) != 10 ||
- strcmp(a->name + 5, ".init"))
- ac |= 32;
- if (af & SHF_ALLOC)
- ac |= 16;
- if (!(af & SHF_WRITE))
- ac |= 8;
- if (af & SHF_EXECINSTR)
- ac |= 4;
- if (a->header.sh_type != SHT_NOBITS)
- ac |= 2;
-
- return ac;
-}
-
-static void
-obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
-{
- struct obj_section **p;
- int prio = obj_load_order_prio(sec);
- for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
- if (obj_load_order_prio(*p) < prio)
- break;
- sec->load_next = *p;
- *p = sec;
-}
-
-static struct obj_section *obj_create_alloced_section(struct obj_file *f,
- const char *name,
- unsigned long align,
- unsigned long size)
-{
- int newidx = f->header.e_shnum++;
- struct obj_section *sec;
-
- f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
- f->sections[newidx] = sec = arch_new_section();
-
- memset(sec, 0, sizeof(*sec));
- sec->header.sh_type = SHT_PROGBITS;
- sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
- sec->header.sh_size = size;
- sec->header.sh_addralign = align;
- sec->name = name;
- sec->idx = newidx;
- if (size)
- sec->contents = xmalloc(size);
-
- obj_insert_section_load_order(f, sec);
-
- return sec;
-}
-
-static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
- const char *name,
- unsigned long align,
- unsigned long size)
-{
- int newidx = f->header.e_shnum++;
- struct obj_section *sec;
-
- f->sections = xrealloc(f->sections, (newidx + 1) * sizeof(sec));
- f->sections[newidx] = sec = arch_new_section();
-
- memset(sec, 0, sizeof(*sec));
- sec->header.sh_type = SHT_PROGBITS;
- sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
- sec->header.sh_size = size;
- sec->header.sh_addralign = align;
- sec->name = name;
- sec->idx = newidx;
- if (size)
- sec->contents = xmalloc(size);
-
- sec->load_next = f->load_order;
- f->load_order = sec;
- if (f->load_order_search_start == &f->load_order)
- f->load_order_search_start = &sec->load_next;
-
- return sec;
-}
-
-static void *obj_extend_section(struct obj_section *sec, unsigned long more)
-{
- unsigned long oldsize = sec->header.sh_size;
- if (more) {
- sec->contents = xrealloc(sec->contents, sec->header.sh_size += more);
- }
- return sec->contents + oldsize;
-}
-
-
-/* Conditionally add the symbols from the given symbol set to the
- new module. */
-
-static int
-add_symbols_from(
- struct obj_file *f,
- int idx, struct new_module_symbol *syms, size_t nsyms)
-{
- struct new_module_symbol *s;
- size_t i;
- int used = 0;
-#ifdef SYMBOL_PREFIX
- char *name_buf = 0;
- size_t name_alloced_size = 0;
-#endif
-
- for (i = 0, s = syms; i < nsyms; ++i, ++s) {
- /* Only add symbols that are already marked external.
- If we override locals we may cause problems for
- argument initialization. We will also create a false
- dependency on the module. */
- struct obj_symbol *sym;
- char *name = (char *)s->name;
-
-#ifdef SYMBOL_PREFIX
- /* Prepend SYMBOL_PREFIX to the symbol's name (the
- kernel exports `C names', but module object files
- reference `linker names'). */
- size_t extra = sizeof SYMBOL_PREFIX;
- size_t name_size = strlen (name) + extra;
- if (name_size > name_alloced_size) {
- name_alloced_size = name_size * 2;
- name_buf = alloca (name_alloced_size);
- }
- strcpy (name_buf, SYMBOL_PREFIX);
- strcpy (name_buf + extra - 1, name);
- name = name_buf;
-#endif /* SYMBOL_PREFIX */
-
- sym = obj_find_symbol(f, name);
- if (sym && !(ELFW(ST_BIND) (sym->info) == STB_LOCAL)) {
-#ifdef SYMBOL_PREFIX
- /* Put NAME_BUF into more permanent storage. */
- name = xmalloc (name_size);
- strcpy (name, name_buf);
-#endif
- sym = obj_add_symbol(f, name, -1,
- ELFW(ST_INFO) (STB_GLOBAL,
- STT_NOTYPE),
- idx, s->value, 0);
- /* Did our symbol just get installed? If so, mark the
- module as "used". */
- if (sym->secidx == idx)
- used = 1;
- }
- }
-
- return used;
-}
-
-static void add_kernel_symbols(struct obj_file *f)
-{
- struct external_module *m;
- int i, nused = 0;
-
- /* Add module symbols first. */
-
- for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m)
- if (m->nsyms
- && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms,
- m->nsyms)) m->used = 1, ++nused;
-
- n_ext_modules_used = nused;
-
- /* And finally the symbols from the kernel proper. */
-
- if (nksyms)
- add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
-}
-
-static char *get_modinfo_value(struct obj_file *f, const char *key)
-{
- struct obj_section *sec;
- char *p, *v, *n, *ep;
- size_t klen = strlen(key);
-
- sec = obj_find_section(f, ".modinfo");
- if (sec == NULL)
- return NULL;
- p = sec->contents;
- ep = p + sec->header.sh_size;
- while (p < ep) {
- v = strchr(p, '=');
- n = strchr(p, '\0');
- if (v) {
- if (p + klen == v && strncmp(p, key, klen) == 0)
- return v + 1;
- } else {
- if (p + klen == n && strcmp(p, key) == 0)
- return n;
- }
- p = n + 1;
- }
-
- return NULL;
-}
-
-
-/*======================================================================*/
-/* Functions relating to module loading in pre 2.1 kernels. */
-
-static int
-old_process_module_arguments(struct obj_file *f, int argc, char **argv)
-{
- while (argc > 0) {
- char *p, *q;
- struct obj_symbol *sym;
- int *loc;
-
- p = *argv;
- if ((q = strchr(p, '=')) == NULL) {
- argc--;
- continue;
- }
- *q++ = '\0';
-
- sym = obj_find_symbol(f, p);
-
- /* Also check that the parameter was not resolved from the kernel. */
- if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
- bb_error_msg("symbol for parameter %s not found", p);
- return 0;
- }
-
- loc = (int *) (f->sections[sym->secidx]->contents + sym->value);
-
- /* Do C quoting if we begin with a ". */
- if (*q == '"') {
- char *r, *str;
-
- str = alloca(strlen(q));
- for (r = str, q++; *q != '"'; ++q, ++r) {
- if (*q == '\0') {
- bb_error_msg("improperly terminated string argument for %s", p);
- return 0;
- } else if (*q == '\\')
- switch (*++q) {
- case 'a':
- *r = '\a';
- break;
- case 'b':
- *r = '\b';
- break;
- case 'e':
- *r = '\033';
- break;
- case 'f':
- *r = '\f';
- break;
- case 'n':
- *r = '\n';
- break;
- case 'r':
- *r = '\r';
- break;
- case 't':
- *r = '\t';
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- {
- int c = *q - '0';
- if (q[1] >= '0' && q[1] <= '7') {
- c = (c * 8) + *++q - '0';
- if (q[1] >= '0' && q[1] <= '7')
- c = (c * 8) + *++q - '0';
- }
- *r = c;
- }
- break;
-
- default:
- *r = *q;
- break;
- } else
- *r = *q;
- }
- *r = '\0';
- obj_string_patch(f, sym->secidx, sym->value, str);
- } else if (*q >= '0' && *q <= '9') {
- do
- *loc++ = strtoul(q, &q, 0);
- while (*q++ == ',');
- } else {
- char *contents = f->sections[sym->secidx]->contents;
- 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, myloc - contents, q);
- myloc += sizeof(char *);
- q = r;
- }
-
- /* last part */
- obj_string_patch(f, sym->secidx, myloc - contents, q);
- }
-
- argc--, argv++;
- }
-
- return 1;
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-static int old_is_module_checksummed(struct obj_file *f)
-{
- return obj_find_symbol(f, "Using_Versions") != NULL;
-}
-/* Get the module's kernel version in the canonical integer form. */
-
-static int
-old_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
-{
- struct obj_symbol *sym;
- char *p, *q;
- int a, b, c;
-
- sym = obj_find_symbol(f, "kernel_version");
- if (sym == NULL)
- return -1;
-
- p = f->sections[sym->secidx]->contents + sym->value;
- safe_strncpy(str, p, STRVERSIONLEN);
-
- a = strtoul(p, &p, 10);
- if (*p != '.')
- return -1;
- b = strtoul(p + 1, &p, 10);
- if (*p != '.')
- return -1;
- c = strtoul(p + 1, &q, 10);
- if (p + 1 == q)
- return -1;
-
- return a << 16 | b << 8 | c;
-}
-
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
-#ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
-
-/* Fetch all the symbols and divvy them up as appropriate for the modules. */
-
-static int old_get_kernel_symbols(const char *m_name)
-{
- struct old_kernel_sym *ks, *k;
- struct new_module_symbol *s;
- struct external_module *mod;
- int nks, nms, nmod, i;
-
- nks = get_kernel_syms(NULL);
- if (nks <= 0) {
- if (nks)
- bb_perror_msg("get_kernel_syms: %s", m_name);
- else
- bb_error_msg("No kernel symbols");
- return 0;
- }
-
- ks = k = xmalloc(nks * sizeof(*ks));
-
- if (get_kernel_syms(ks) != nks) {
- perror("inconsistency with get_kernel_syms -- is someone else "
- "playing with modules?");
- free(ks);
- return 0;
- }
-
- /* Collect the module information. */
-
- mod = NULL;
- nmod = -1;
-
- while (k->name[0] == '#' && k->name[1]) {
- struct old_kernel_sym *k2;
-
- /* Find out how many symbols this module has. */
- for (k2 = k + 1; k2->name[0] != '#'; ++k2)
- continue;
- nms = k2 - k - 1;
-
- mod = xrealloc(mod, (++nmod + 1) * sizeof(*mod));
- mod[nmod].name = k->name + 1;
- mod[nmod].addr = k->value;
- mod[nmod].used = 0;
- mod[nmod].nsyms = nms;
- mod[nmod].syms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
-
- for (i = 0, ++k; i < nms; ++i, ++s, ++k) {
- s->name = (unsigned long) k->name;
- s->value = k->value;
- }
-
- k = k2;
- }
-
- ext_modules = mod;
- n_ext_modules = nmod + 1;
-
- /* Now collect the symbols for the kernel proper. */
-
- if (k->name[0] == '#')
- ++k;
-
- nksyms = nms = nks - (k - ks);
- ksyms = s = (nms ? xmalloc(nms * sizeof(*s)) : NULL);
-
- for (i = 0; i < nms; ++i, ++s, ++k) {
- s->name = (unsigned long) k->name;
- s->value = k->value;
- }
-
- return 1;
-}
-
-/* Return the kernel symbol checksum version, or zero if not used. */
-
-static int old_is_kernel_checksummed(void)
-{
- /* Using_Versions is the first symbol. */
- if (nksyms > 0
- && strcmp((char *) ksyms[0].name,
- "Using_Versions") == 0) return ksyms[0].value;
- else
- return 0;
-}
-
-
-static int old_create_mod_use_count(struct obj_file *f)
-{
- struct obj_section *sec;
-
- sec = obj_create_alloced_section_first(f, ".moduse", sizeof(long),
- sizeof(long));
-
- obj_add_symbol(f, "mod_use_count_", -1,
- ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
- sizeof(long));
-
- return 1;
-}
-
-static int
-old_init_module(const char *m_name, struct obj_file *f,
- unsigned long m_size)
-{
- char *image;
- struct old_mod_routines routines;
- struct old_symbol_table *symtab;
- int ret;
-
- /* Create the symbol table */
- {
- int nsyms = 0, strsize = 0, total;
-
- /* Size things first... */
- if (flag_export) {
- int i;
- for (i = 0; i < HASH_BUCKETS; ++i) {
- struct obj_symbol *sym;
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
- && sym->secidx <= SHN_HIRESERVE)
- {
- sym->ksymidx = nsyms++;
- strsize += strlen(sym->name) + 1;
- }
- }
- }
-
- total = (sizeof(struct old_symbol_table)
- + nsyms * sizeof(struct old_module_symbol)
- + n_ext_modules_used * sizeof(struct old_module_ref)
- + strsize);
- symtab = xmalloc(total);
- symtab->size = total;
- symtab->n_symbols = nsyms;
- symtab->n_refs = n_ext_modules_used;
-
- if (flag_export && nsyms) {
- struct old_module_symbol *ksym;
- char *str;
- int i;
-
- ksym = symtab->symbol;
- str = ((char *) ksym + nsyms * sizeof(struct old_module_symbol)
- + n_ext_modules_used * sizeof(struct old_module_ref));
-
- for (i = 0; i < HASH_BUCKETS; ++i) {
- struct obj_symbol *sym;
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (sym->ksymidx >= 0) {
- ksym->addr = obj_symbol_final_value(f, sym);
- ksym->name =
- (unsigned long) str - (unsigned long) symtab;
-
- strcpy(str, sym->name);
- str += strlen(sym->name) + 1;
- ksym++;
- }
- }
- }
-
- if (n_ext_modules_used) {
- struct old_module_ref *ref;
- int i;
-
- ref = (struct old_module_ref *)
- ((char *) symtab->symbol + nsyms * sizeof(struct old_module_symbol));
-
- for (i = 0; i < n_ext_modules; ++i)
- if (ext_modules[i].used)
- ref++->module = ext_modules[i].addr;
- }
- }
-
- /* Fill in routines. */
-
- routines.init =
- obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
- routines.cleanup =
- obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
-
- /* Whew! All of the initialization is complete. Collect the final
- module image and give it to the kernel. */
-
- image = xmalloc(m_size);
- obj_create_image(f, image);
-
- /* image holds the complete relocated module, accounting correctly for
- mod_use_count. However the old module kernel support assume that
- it is receiving something which does not contain mod_use_count. */
- ret = old_sys_init_module(m_name, image + sizeof(long),
- m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
- : 0), &routines, symtab);
- if (ret)
- bb_perror_msg("init_module: %s", m_name);
-
- free(image);
- free(symtab);
-
- return ret == 0;
-}
-
-#else
-
-#define old_create_mod_use_count(x) TRUE
-#define old_init_module(x, y, z) TRUE
-
-#endif /* CONFIG_FEATURE_OLD_MODULE_INTERFACE */
-
-
-
-/*======================================================================*/
-/* Functions relating to module loading after 2.1.18. */
-
-static int
-new_process_module_arguments(struct obj_file *f, int argc, char **argv)
-{
- while (argc > 0) {
- char *p, *q, *key, *sym_name;
- struct obj_symbol *sym;
- char *contents, *loc;
- int min, max, n;
-
- p = *argv;
- if ((q = strchr(p, '=')) == NULL) {
- argc--;
- continue;
- }
-
- key = alloca(q - p + 6);
- memcpy(key, "parm_", 5);
- memcpy(key + 5, p, q - p);
- key[q - p + 5] = 0;
-
- p = get_modinfo_value(f, key);
- key += 5;
- if (p == NULL) {
- bb_error_msg("invalid parameter %s", key);
- return 0;
- }
-
-#ifdef SYMBOL_PREFIX
- sym_name = alloca (strlen (key) + sizeof SYMBOL_PREFIX);
- strcpy (sym_name, SYMBOL_PREFIX);
- strcat (sym_name, key);
-#else
- sym_name = key;
-#endif
- sym = obj_find_symbol(f, sym_name);
-
- /* Also check that the parameter was not resolved from the kernel. */
- if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
- bb_error_msg("symbol for parameter %s not found", key);
- return 0;
- }
-
- if (isdigit(*p)) {
- min = strtoul(p, &p, 10);
- if (*p == '-')
- max = strtoul(p + 1, &p, 10);
- else
- max = min;
- } else
- min = max = 1;
-
- contents = f->sections[sym->secidx]->contents;
- loc = contents + sym->value;
- n = (*++q != '\0');
-
- while (1) {
- if ((*p == 's') || (*p == 'c')) {
- char *str;
-
- /* Do C quoting if we begin with a ", else slurp the lot. */
- if (*q == '"') {
- char *r;
-
- str = alloca(strlen(q));
- for (r = str, q++; *q != '"'; ++q, ++r) {
- if (*q == '\0') {
- bb_error_msg("improperly terminated string argument for %s",
- key);
- return 0;
- } else if (*q == '\\')
- switch (*++q) {
- case 'a':
- *r = '\a';
- break;
- case 'b':
- *r = '\b';
- break;
- case 'e':
- *r = '\033';
- break;
- case 'f':
- *r = '\f';
- break;
- case 'n':
- *r = '\n';
- break;
- case 'r':
- *r = '\r';
- break;
- case 't':
- *r = '\t';
- break;
-
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- {
- int c = *q - '0';
- if (q[1] >= '0' && q[1] <= '7') {
- c = (c * 8) + *++q - '0';
- if (q[1] >= '0' && q[1] <= '7')
- c = (c * 8) + *++q - '0';
- }
- *r = c;
- }
- break;
-
- default:
- *r = *q;
- break;
- } else
- *r = *q;
- }
- *r = '\0';
- ++q;
- } else {
- char *r;
-
- /* In this case, the string is not quoted. We will break
- it using the coma (like for ints). If the user wants to
- include comas in a string, he just has to quote it */
-
- /* Search the next coma */
- r = strchr(q, ',');
-
- /* Found ? */
- if (r != (char *) NULL) {
- /* Recopy the current field */
- str = alloca(r - q + 1);
- memcpy(str, q, r - q);
-
- /* I don't know if it is usefull, as the previous case
- doesn't null terminate the string ??? */
- str[r - q] = '\0';
-
- /* Keep next fields */
- q = r;
- } else {
- /* last string */
- str = q;
- q = "";
- }
- }
-
- if (*p == 's') {
- /* Normal string */
- obj_string_patch(f, sym->secidx, loc - contents, str);
- loc += tgt_sizeof_char_p;
- } else {
- /* Array of chars (in fact, matrix !) */
- unsigned long charssize; /* size of each member */
-
- /* Get the size of each member */
- /* Probably we should do that outside the loop ? */
- if (!isdigit(*(p + 1))) {
- bb_error_msg("parameter type 'c' for %s must be followed by"
- " the maximum size", key);
- return 0;
- }
- charssize = strtoul(p + 1, (char **) NULL, 10);
-
- /* Check length */
- if (strlen(str) >= charssize) {
- bb_error_msg("string too long for %s (max %ld)", key,
- charssize - 1);
- return 0;
- }
-
- /* Copy to location */
- strcpy((char *) loc, str);
- loc += charssize;
- }
- } else {
- long v = strtoul(q, &q, 0);
- switch (*p) {
- case 'b':
- *loc++ = v;
- break;
- case 'h':
- *(short *) loc = v;
- loc += tgt_sizeof_short;
- break;
- case 'i':
- *(int *) loc = v;
- loc += tgt_sizeof_int;
- break;
- case 'l':
- *(long *) loc = v;
- loc += tgt_sizeof_long;
- break;
-
- default:
- bb_error_msg("unknown parameter type '%c' for %s", *p, key);
- return 0;
- }
- }
-
- retry_end_of_value:
- switch (*q) {
- case '\0':
- goto end_of_arg;
-
- case ' ':
- case '\t':
- case '\n':
- case '\r':
- ++q;
- goto retry_end_of_value;
-
- case ',':
- if (++n > max) {
- bb_error_msg("too many values for %s (max %d)", key, max);
- return 0;
- }
- ++q;
- break;
-
- default:
- bb_error_msg("invalid argument syntax for %s", key);
- return 0;
- }
- }
-
- end_of_arg:
- if (n < min) {
- bb_error_msg("too few values for %s (min %d)", key, min);
- return 0;
- }
-
- argc--, argv++;
- }
-
- return 1;
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
-static int new_is_module_checksummed(struct obj_file *f)
-{
- const char *p = get_modinfo_value(f, "using_checksums");
- if (p)
- return atoi(p);
- else
- return 0;
-}
-
-/* Get the module's kernel version in the canonical integer form. */
-
-static int
-new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
-{
- char *p, *q;
- int a, b, c;
-
- p = get_modinfo_value(f, "kernel_version");
- if (p == NULL)
- return -1;
- safe_strncpy(str, p, STRVERSIONLEN);
-
- a = strtoul(p, &p, 10);
- if (*p != '.')
- return -1;
- b = strtoul(p + 1, &p, 10);
- if (*p != '.')
- return -1;
- c = strtoul(p + 1, &q, 10);
- if (p + 1 == q)
- return -1;
-
- return a << 16 | b << 8 | c;
-}
-
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-
-/* Fetch the loaded modules, and all currently exported symbols. */
-
-static int new_get_kernel_symbols(void)
-{
- char *module_names, *mn;
- struct external_module *modules, *m;
- struct new_module_symbol *syms, *s;
- size_t ret, bufsize, nmod, nsyms, i, j;
-
- /* Collect the loaded modules. */
-
- module_names = xmalloc(bufsize = 256);
- retry_modules_load:
- if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
- if (errno == ENOSPC && bufsize < ret) {
- module_names = xrealloc(module_names, bufsize = ret);
- goto retry_modules_load;
- }
- bb_perror_msg("QM_MODULES");
- return 0;
- }
-
- n_ext_modules = nmod = ret;
-
- /* Collect the modules' symbols. */
-
- if (nmod){
- ext_modules = modules = xmalloc(nmod * sizeof(*modules));
- memset(modules, 0, nmod * sizeof(*modules));
- for (i = 0, mn = module_names, m = modules;
- i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
- struct new_module_info info;
-
- if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
- if (errno == ENOENT) {
- /* The module was removed out from underneath us. */
- continue;
- }
- bb_perror_msg("query_module: QM_INFO: %s", mn);
- return 0;
- }
-
- syms = xmalloc(bufsize = 1024);
- retry_mod_sym_load:
- if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
- switch (errno) {
- case ENOSPC:
- syms = xrealloc(syms, bufsize = ret);
- goto retry_mod_sym_load;
- case ENOENT:
- /* The module was removed out from underneath us. */
- continue;
- default:
- bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
- return 0;
- }
- }
- nsyms = ret;
-
- m->name = mn;
- m->addr = info.addr;
- m->nsyms = nsyms;
- m->syms = syms;
-
- for (j = 0, s = syms; j < nsyms; ++j, ++s) {
- s->name += (unsigned long) syms;
- }
- }
- }
-
- /* Collect the kernel's symbols. */
-
- syms = xmalloc(bufsize = 16 * 1024);
- retry_kern_sym_load:
- if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
- if (errno == ENOSPC && bufsize < ret) {
- syms = xrealloc(syms, bufsize = ret);
- goto retry_kern_sym_load;
- }
- bb_perror_msg("kernel: QM_SYMBOLS");
- return 0;
- }
- nksyms = nsyms = ret;
- ksyms = syms;
-
- for (j = 0, s = syms; j < nsyms; ++j, ++s) {
- s->name += (unsigned long) syms;
- }
- return 1;
-}
-
-
-/* Return the kernel symbol checksum version, or zero if not used. */
-
-static int new_is_kernel_checksummed(void)
-{
- struct new_module_symbol *s;
- size_t i;
-
- /* Using_Versions is not the first symbol, but it should be in there. */
-
- for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
- if (strcmp((char *) s->name, "Using_Versions") == 0)
- return s->value;
-
- return 0;
-}
-
-
-static int new_create_this_module(struct obj_file *f, const char *m_name)
-{
- struct obj_section *sec;
-
- sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
- sizeof(struct new_module));
- memset(sec->contents, 0, sizeof(struct new_module));
-
- obj_add_symbol(f, SPFX "__this_module", -1,
- ELFW(ST_INFO) (STB_LOCAL, STT_OBJECT), sec->idx, 0,
- sizeof(struct new_module));
-
- obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
- m_name);
-
- return 1;
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
-/* add an entry to the __ksymtab section, creating it if necessary */
-static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
-{
- struct obj_section *sec;
- ElfW(Addr) ofs;
-
- /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
- * If __ksymtab is defined but not marked alloc, x out the first character
- * (no obj_delete routine) and create a new __ksymtab with the correct
- * characteristics.
- */
- sec = obj_find_section(f, "__ksymtab");
- if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
- *((char *)(sec->name)) = 'x'; /* override const */
- sec = NULL;
- }
- if (!sec)
- sec = obj_create_alloced_section(f, "__ksymtab",
- tgt_sizeof_void_p, 0);
- if (!sec)
- return;
- sec->header.sh_flags |= SHF_ALLOC;
- sec->header.sh_addralign = tgt_sizeof_void_p; /* Empty section might
- be byte-aligned */
- ofs = sec->header.sh_size;
- obj_symbol_patch(f, sec->idx, ofs, sym);
- obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
- obj_extend_section(sec, 2 * tgt_sizeof_char_p);
-}
-#endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
-
-static int new_create_module_ksymtab(struct obj_file *f)
-{
- struct obj_section *sec;
- int i;
-
- /* We must always add the module references. */
-
- if (n_ext_modules_used) {
- struct new_module_ref *dep;
- struct obj_symbol *tm;
-
- sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
- (sizeof(struct new_module_ref)
- * n_ext_modules_used));
- if (!sec)
- return 0;
-
- tm = obj_find_symbol(f, SPFX "__this_module");
- dep = (struct new_module_ref *) sec->contents;
- for (i = 0; i < n_ext_modules; ++i)
- if (ext_modules[i].used) {
- dep->dep = ext_modules[i].addr;
- obj_symbol_patch(f, sec->idx,
- (char *) &dep->ref - sec->contents, tm);
- dep->next_ref = 0;
- ++dep;
- }
- }
-
- if (flag_export && !obj_find_section(f, "__ksymtab")) {
- size_t nsyms;
- int *loaded;
-
- sec =
- obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p,
- 0);
-
- /* We don't want to export symbols residing in sections that
- aren't loaded. There are a number of these created so that
- we make sure certain module options don't appear twice. */
-
- loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
- while (--i >= 0)
- loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
-
- for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
- struct obj_symbol *sym;
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (ELFW(ST_BIND) (sym->info) != STB_LOCAL
- && sym->secidx <= SHN_HIRESERVE
- && (sym->secidx >= SHN_LORESERVE
- || loaded[sym->secidx])) {
- ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
-
- obj_symbol_patch(f, sec->idx, ofs, sym);
- obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
- sym->name);
-
- nsyms++;
- }
- }
-
- obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
- }
-
- return 1;
-}
-
-
-static int
-new_init_module(const char *m_name, struct obj_file *f,
- unsigned long m_size)
-{
- struct new_module *module;
- struct obj_section *sec;
- void *image;
- int ret;
- tgt_long m_addr;
-
- sec = obj_find_section(f, ".this");
- if (!sec || !sec->contents) {
- bb_perror_msg_and_die("corrupt module %s?",m_name);
- }
- module = (struct new_module *) sec->contents;
- m_addr = sec->header.sh_addr;
-
- module->size_of_struct = sizeof(*module);
- module->size = m_size;
- module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
-
- sec = obj_find_section(f, "__ksymtab");
- if (sec && sec->header.sh_size) {
- module->syms = sec->header.sh_addr;
- module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
- }
-
- if (n_ext_modules_used) {
- sec = obj_find_section(f, ".kmodtab");
- module->deps = sec->header.sh_addr;
- module->ndeps = n_ext_modules_used;
- }
-
- module->init =
- obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
- module->cleanup =
- obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
-
- sec = obj_find_section(f, "__ex_table");
- if (sec) {
- module->ex_table_start = sec->header.sh_addr;
- module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
- }
-
- sec = obj_find_section(f, ".text.init");
- if (sec) {
- module->runsize = sec->header.sh_addr - m_addr;
- }
- sec = obj_find_section(f, ".data.init");
- if (sec) {
- if (!module->runsize ||
- module->runsize > sec->header.sh_addr - m_addr)
- module->runsize = sec->header.sh_addr - m_addr;
- }
- sec = obj_find_section(f, ARCHDATA_SEC_NAME);
- if (sec && sec->header.sh_size) {
- module->archdata_start = (void*)sec->header.sh_addr;
- module->archdata_end = module->archdata_start + sec->header.sh_size;
- }
- sec = obj_find_section(f, KALLSYMS_SEC_NAME);
- if (sec && sec->header.sh_size) {
- module->kallsyms_start = (void*)sec->header.sh_addr;
- module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
- }
-
- if (!arch_init_module(f, module))
- return 0;
-
- /* Whew! All of the initialization is complete. Collect the final
- module image and give it to the kernel. */
-
- image = xmalloc(m_size);
- obj_create_image(f, image);
-
- ret = new_sys_init_module(m_name, (struct new_module *) image);
- if (ret)
- bb_perror_msg("init_module: %s", m_name);
-
- free(image);
-
- return ret == 0;
-}
-
-#else
-
-#define new_init_module(x, y, z) TRUE
-#define new_create_this_module(x, y) 0
-#define new_add_ksymtab(x, y) -1
-#define new_create_module_ksymtab(x)
-#define query_module(v, w, x, y, z) -1
-
-#endif /* CONFIG_FEATURE_NEW_MODULE_INTERFACE */
-
-
-/*======================================================================*/
-
-static int
-obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
- const char *string)
-{
- struct obj_string_patch *p;
- struct obj_section *strsec;
- size_t len = strlen(string) + 1;
- char *loc;
-
- p = xmalloc(sizeof(*p));
- p->next = f->string_patches;
- p->reloc_secidx = secidx;
- p->reloc_offset = offset;
- f->string_patches = p;
-
- strsec = obj_find_section(f, ".kstrtab");
- if (strsec == NULL) {
- strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
- p->string_offset = 0;
- loc = strsec->contents;
- } else {
- p->string_offset = strsec->header.sh_size;
- loc = obj_extend_section(strsec, len);
- }
- memcpy(loc, string, len);
-
- return 1;
-}
-
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
-static int
-obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
- struct obj_symbol *sym)
-{
- struct obj_symbol_patch *p;
-
- p = xmalloc(sizeof(*p));
- p->next = f->symbol_patches;
- p->reloc_secidx = secidx;
- p->reloc_offset = offset;
- p->sym = sym;
- f->symbol_patches = p;
-
- return 1;
-}
-#endif
-
-static int obj_check_undefineds(struct obj_file *f)
-{
- unsigned long i;
- int ret = 1;
-
- for (i = 0; i < HASH_BUCKETS; ++i) {
- struct obj_symbol *sym;
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (sym->secidx == SHN_UNDEF) {
- if (ELFW(ST_BIND) (sym->info) == STB_WEAK) {
- sym->secidx = SHN_ABS;
- sym->value = 0;
- } else {
- if (!flag_quiet) {
- bb_error_msg("unresolved symbol %s", sym->name);
- }
- ret = 0;
- }
- }
- }
-
- return ret;
-}
-
-static void obj_allocate_commons(struct obj_file *f)
-{
- struct common_entry {
- struct common_entry *next;
- struct obj_symbol *sym;
- } *common_head = NULL;
-
- unsigned long i;
-
- for (i = 0; i < HASH_BUCKETS; ++i) {
- struct obj_symbol *sym;
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (sym->secidx == SHN_COMMON) {
- /* Collect all COMMON symbols and sort them by size so as to
- minimize space wasted by alignment requirements. */
- {
- struct common_entry **p, *n;
- for (p = &common_head; *p; p = &(*p)->next)
- if (sym->size <= (*p)->sym->size)
- break;
-
- n = alloca(sizeof(*n));
- n->next = *p;
- n->sym = sym;
- *p = n;
- }
- }
- }
-
- for (i = 1; i < f->local_symtab_size; ++i) {
- struct obj_symbol *sym = f->local_symtab[i];
- if (sym && sym->secidx == SHN_COMMON) {
- struct common_entry **p, *n;
- for (p = &common_head; *p; p = &(*p)->next)
- if (sym == (*p)->sym)
- break;
- else if (sym->size < (*p)->sym->size) {
- n = alloca(sizeof(*n));
- n->next = *p;
- n->sym = sym;
- *p = n;
- break;
- }
- }
- }
-
- if (common_head) {
- /* Find the bss section. */
- for (i = 0; i < f->header.e_shnum; ++i)
- if (f->sections[i]->header.sh_type == SHT_NOBITS)
- break;
-
- /* If for some reason there hadn't been one, create one. */
- if (i == f->header.e_shnum) {
- struct obj_section *sec;
-
- f->sections = xrealloc(f->sections, (i + 1) * sizeof(sec));
- f->sections[i] = sec = arch_new_section();
- f->header.e_shnum = i + 1;
-
- memset(sec, 0, sizeof(*sec));
- sec->header.sh_type = SHT_PROGBITS;
- sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
- sec->name = ".bss";
- sec->idx = i;
- }
-
- /* Allocate the COMMONS. */
- {
- ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
- ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
- struct common_entry *c;
-
- for (c = common_head; c; c = c->next) {
- ElfW(Addr) align = c->sym->value;
-
- if (align > max_align)
- max_align = align;
- if (bss_size & (align - 1))
- bss_size = (bss_size | (align - 1)) + 1;
-
- c->sym->secidx = i;
- c->sym->value = bss_size;
-
- bss_size += c->sym->size;
- }
-
- f->sections[i]->header.sh_size = bss_size;
- f->sections[i]->header.sh_addralign = max_align;
- }
- }
-
- /* For the sake of patch relocation and parameter initialization,
- allocate zeroed data for NOBITS sections now. Note that after
- this we cannot assume NOBITS are really empty. */
- for (i = 0; i < f->header.e_shnum; ++i) {
- struct obj_section *s = f->sections[i];
- if (s->header.sh_type == SHT_NOBITS) {
- if (s->header.sh_size != 0)
- s->contents = memset(xmalloc(s->header.sh_size),
- 0, s->header.sh_size);
- else
- s->contents = NULL;
-
- s->header.sh_type = SHT_PROGBITS;
- }
- }
-}
-
-static unsigned long obj_load_size(struct obj_file *f)
-{
- unsigned long dot = 0;
- struct obj_section *sec;
-
- /* Finalize the positions of the sections relative to one another. */
-
- for (sec = f->load_order; sec; sec = sec->load_next) {
- ElfW(Addr) align;
-
- align = sec->header.sh_addralign;
- if (align && (dot & (align - 1)))
- dot = (dot | (align - 1)) + 1;
-
- sec->header.sh_addr = dot;
- dot += sec->header.sh_size;
- }
-
- return dot;
-}
-
-static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
-{
- int i, n = f->header.e_shnum;
- int ret = 1;
-
- /* Finalize the addresses of the sections. */
-
- f->baseaddr = base;
- for (i = 0; i < n; ++i)
- f->sections[i]->header.sh_addr += base;
-
- /* And iterate over all of the relocations. */
-
- for (i = 0; i < n; ++i) {
- struct obj_section *relsec, *symsec, *targsec, *strsec;
- ElfW(RelM) * rel, *relend;
- ElfW(Sym) * symtab;
- const char *strtab;
-
- relsec = f->sections[i];
- if (relsec->header.sh_type != SHT_RELM)
- continue;
-
- symsec = f->sections[relsec->header.sh_link];
- targsec = f->sections[relsec->header.sh_info];
- strsec = f->sections[symsec->header.sh_link];
-
- rel = (ElfW(RelM) *) relsec->contents;
- relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
- symtab = (ElfW(Sym) *) symsec->contents;
- strtab = (const char *) strsec->contents;
-
- for (; rel < relend; ++rel) {
- ElfW(Addr) value = 0;
- struct obj_symbol *intsym = NULL;
- unsigned long symndx;
- ElfW(Sym) * extsym = 0;
- const char *errmsg;
-
- /* Attempt to find a value to use for this relocation. */
-
- symndx = ELFW(R_SYM) (rel->r_info);
- if (symndx) {
- /* Note we've already checked for undefined symbols. */
-
- extsym = &symtab[symndx];
- if (ELFW(ST_BIND) (extsym->st_info) == STB_LOCAL) {
- /* Local symbols we look up in the local table to be sure
- we get the one that is really intended. */
- intsym = f->local_symtab[symndx];
- } else {
- /* Others we look up in the hash table. */
- const char *name;
- if (extsym->st_name)
- name = strtab + extsym->st_name;
- else
- name = f->sections[extsym->st_shndx]->name;
- intsym = obj_find_symbol(f, name);
- }
-
- value = obj_symbol_final_value(f, intsym);
- intsym->referenced = 1;
- }
-#if SHT_RELM == SHT_RELA
-#if defined(__alpha__) && defined(AXP_BROKEN_GAS)
- /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
- if (!extsym || !extsym->st_name ||
- ELFW(ST_BIND) (extsym->st_info) != STB_LOCAL)
-#endif
- value += rel->r_addend;
-#endif
-
- /* Do it! */
- switch (arch_apply_relocation
- (f, targsec, symsec, intsym, rel, value)) {
- case obj_reloc_ok:
- break;
-
- case obj_reloc_overflow:
- errmsg = "Relocation overflow";
- goto bad_reloc;
- case obj_reloc_dangerous:
- errmsg = "Dangerous relocation";
- goto bad_reloc;
- case obj_reloc_unhandled:
- errmsg = "Unhandled relocation";
- bad_reloc:
- if (extsym) {
- bb_error_msg("%s of type %ld for %s", errmsg,
- (long) ELFW(R_TYPE) (rel->r_info),
- strtab + extsym->st_name);
- } else {
- bb_error_msg("%s of type %ld", errmsg,
- (long) ELFW(R_TYPE) (rel->r_info));
- }
- ret = 0;
- break;
- }
- }
- }
-
- /* Finally, take care of the patches. */
-
- if (f->string_patches) {
- struct obj_string_patch *p;
- struct obj_section *strsec;
- ElfW(Addr) strsec_base;
- strsec = obj_find_section(f, ".kstrtab");
- strsec_base = strsec->header.sh_addr;
-
- for (p = f->string_patches; p; p = p->next) {
- struct obj_section *targsec = f->sections[p->reloc_secidx];
- *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
- = strsec_base + p->string_offset;
- }
- }
-
- if (f->symbol_patches) {
- struct obj_symbol_patch *p;
-
- for (p = f->symbol_patches; p; p = p->next) {
- struct obj_section *targsec = f->sections[p->reloc_secidx];
- *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
- = obj_symbol_final_value(f, p->sym);
- }
- }
-
- return ret;
-}
-
-static int obj_create_image(struct obj_file *f, char *image)
-{
- struct obj_section *sec;
- ElfW(Addr) base = f->baseaddr;
-
- for (sec = f->load_order; sec; sec = sec->load_next) {
- char *secimg;
-
- if (sec->contents == 0 || sec->header.sh_size == 0)
- continue;
-
- secimg = image + (sec->header.sh_addr - base);
-
- /* Note that we allocated data for NOBITS sections earlier. */
- memcpy(secimg, sec->contents, sec->header.sh_size);
- }
-
- return 1;
-}
-
-/*======================================================================*/
-
-static struct obj_file *obj_load(FILE * fp, int loadprogbits)
-{
- struct obj_file *f;
- ElfW(Shdr) * section_headers;
- int shnum, i;
- char *shstrtab;
-
- /* Read the file header. */
-
- f = arch_new_file();
- memset(f, 0, sizeof(*f));
- f->symbol_cmp = strcmp;
- f->symbol_hash = obj_elf_hash;
- f->load_order_search_start = &f->load_order;
-
- fseek(fp, 0, SEEK_SET);
- if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
- bb_perror_msg("error reading ELF header");
- return NULL;
- }
-
- if (f->header.e_ident[EI_MAG0] != ELFMAG0
- || f->header.e_ident[EI_MAG1] != ELFMAG1
- || f->header.e_ident[EI_MAG2] != ELFMAG2
- || f->header.e_ident[EI_MAG3] != ELFMAG3) {
- bb_error_msg("not an ELF file");
- return NULL;
- }
- if (f->header.e_ident[EI_CLASS] != ELFCLASSM
- || f->header.e_ident[EI_DATA] != ELFDATAM
- || f->header.e_ident[EI_VERSION] != EV_CURRENT
- || !MATCH_MACHINE(f->header.e_machine)) {
- bb_error_msg("ELF file not for this architecture");
- return NULL;
- }
- if (f->header.e_type != ET_REL) {
- bb_error_msg("ELF file not a relocatable object");
- return NULL;
- }
-
- /* Read the section headers. */
-
- if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
- bb_error_msg("section header size mismatch: %lu != %lu",
- (unsigned long) f->header.e_shentsize,
- (unsigned long) sizeof(ElfW(Shdr)));
- return NULL;
- }
-
- shnum = f->header.e_shnum;
- f->sections = xmalloc(sizeof(struct obj_section *) * shnum);
- memset(f->sections, 0, sizeof(struct obj_section *) * shnum);
-
- section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
- fseek(fp, f->header.e_shoff, SEEK_SET);
- if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
- bb_perror_msg("error reading ELF section headers");
- return NULL;
- }
-
- /* Read the section data. */
-
- for (i = 0; i < shnum; ++i) {
- struct obj_section *sec;
-
- f->sections[i] = sec = arch_new_section();
- memset(sec, 0, sizeof(*sec));
-
- sec->header = section_headers[i];
- sec->idx = i;
-
- if(sec->header.sh_size) switch (sec->header.sh_type) {
- case SHT_NULL:
- case SHT_NOTE:
- case SHT_NOBITS:
- /* ignore */
- break;
-
- case SHT_PROGBITS:
-#if LOADBITS
- if (!loadprogbits) {
- sec->contents = NULL;
- break;
- }
-#endif
- case SHT_SYMTAB:
- case SHT_STRTAB:
- case SHT_RELM:
- if (sec->header.sh_size > 0) {
- sec->contents = xmalloc(sec->header.sh_size);
- fseek(fp, sec->header.sh_offset, SEEK_SET);
- if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
- bb_perror_msg("error reading ELF section data");
- return NULL;
- }
- } else {
- sec->contents = NULL;
- }
- break;
-
-#if SHT_RELM == SHT_REL
- case SHT_RELA:
- bb_error_msg("RELA relocations not supported on this architecture");
- return NULL;
-#else
- case SHT_REL:
- bb_error_msg("REL relocations not supported on this architecture");
- return NULL;
-#endif
-
- default:
- if (sec->header.sh_type >= SHT_LOPROC) {
- /* Assume processor specific section types are debug
- info and can safely be ignored. If this is ever not
- the case (Hello MIPS?), don't put ifdefs here but
- create an arch_load_proc_section(). */
- break;
- }
-
- bb_error_msg("can't handle sections of type %ld",
- (long) sec->header.sh_type);
- return NULL;
- }
- }
-
- /* Do what sort of interpretation as needed by each section. */
-
- shstrtab = f->sections[f->header.e_shstrndx]->contents;
-
- for (i = 0; i < shnum; ++i) {
- struct obj_section *sec = f->sections[i];
- sec->name = shstrtab + sec->header.sh_name;
- }
-
- 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);
-
- switch (sec->header.sh_type) {
- case SHT_SYMTAB:
- {
- unsigned long nsym, j;
- char *strtab;
- ElfW(Sym) * sym;
-
- if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
- bb_error_msg("symbol size mismatch: %lu != %lu",
- (unsigned long) sec->header.sh_entsize,
- (unsigned long) sizeof(ElfW(Sym)));
- return NULL;
- }
-
- nsym = sec->header.sh_size / sizeof(ElfW(Sym));
- strtab = f->sections[sec->header.sh_link]->contents;
- sym = (ElfW(Sym) *) sec->contents;
-
- /* Allocate space for a table of local symbols. */
- j = f->local_symtab_size = sec->header.sh_info;
- 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 if (sym->st_shndx < shnum)
- name = f->sections[sym->st_shndx]->name;
- else
- continue;
-
- obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
- sym->st_value, sym->st_size);
- }
- }
- break;
-
- case SHT_RELM:
- if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
- bb_error_msg("relocation entry size mismatch: %lu != %lu",
- (unsigned long) sec->header.sh_entsize,
- (unsigned long) sizeof(ElfW(RelM)));
- 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(). */
- }
- }
-
- return f;
-}
-
-#ifdef CONFIG_FEATURE_INSMOD_LOADINKMEM
-/*
- * load the unloaded sections directly into the memory allocated by
- * kernel for the module
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
-{
- ElfW(Addr) base = f->baseaddr;
- struct obj_section* sec;
-
- for (sec = f->load_order; sec; sec = sec->load_next) {
+#include "libbb.h"
+#include "modutils.h"
- /* section already loaded? */
- if (sec->contents != NULL)
- continue;
-
- if (sec->header.sh_size == 0)
- continue;
-
- sec->contents = imagebase + (sec->header.sh_addr - base);
- fseek(fp, sec->header.sh_offset, SEEK_SET);
- if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
- bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
- return 0;
- }
-
- }
- return 1;
-}
-#endif
-
-static void hide_special_symbols(struct obj_file *f)
+int insmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int insmod_main(int argc UNUSED_PARAM, char **argv)
{
- static const char *const specials[] = {
- SPFX "cleanup_module",
- SPFX "init_module",
- SPFX "kernel_version",
- NULL
- };
-
- struct obj_symbol *sym;
- const char *const *p;
-
- for (p = specials; *p; ++p)
- if ((sym = obj_find_symbol(f, *p)) != NULL)
- sym->info =
- ELFW(ST_INFO) (STB_LOCAL, ELFW(ST_TYPE) (sym->info));
-}
+ char *filename;
+ int rc;
-#ifdef CONFIG_FEATURE_CHECK_TAINTED_MODULE
-static int obj_gpl_license(struct obj_file *f, const char **license)
-{
- struct obj_section *sec;
- /* This list must match *exactly* the list of allowable licenses in
- * linux/include/linux/module.h. Checking for leading "GPL" will not
- * work, somebody will use "GPL sucks, this is proprietary".
+ /* Compat note:
+ * 2.6 style insmod has no options and required filename
+ * (not module name - .ko can't be omitted).
+ * 2.4 style insmod can take module name without .o
+ * and performs module search in default directories
+ * or in $MODPATH.
*/
- static const char *gpl_licenses[] = {
- "GPL",
- "GPL v2",
- "GPL and additional rights",
- "Dual BSD/GPL",
- "Dual MPL/GPL",
- };
-
- if ((sec = obj_find_section(f, ".modinfo"))) {
- const char *value, *ptr, *endptr;
- ptr = sec->contents;
- endptr = ptr + sec->header.sh_size;
- while (ptr < endptr) {
- if ((value = strchr(ptr, '=')) && strncmp(ptr, "license", value-ptr) == 0) {
- int i;
- if (license)
- *license = value+1;
- for (i = 0; i < sizeof(gpl_licenses)/sizeof(gpl_licenses[0]); ++i) {
- if (strcmp(value+1, gpl_licenses[i]) == 0)
- return(0);
- }
- return(2);
- }
- if (strchr(ptr, '\0'))
- ptr = strchr(ptr, '\0') + 1;
- else
- ptr = endptr;
- }
- }
- return(1);
-}
-
-#define TAINT_FILENAME "/proc/sys/kernel/tainted"
-#define TAINT_PROPRIETORY_MODULE (1<<0)
-#define TAINT_FORCED_MODULE (1<<1)
-#define TAINT_UNSAFE_SMP (1<<2)
-#define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
-
-static void set_tainted(struct obj_file *f, int fd, char *m_name,
- int kernel_has_tainted, int taint, const char *text1, const char *text2)
-{
- char buf[80];
- int oldval;
- static int first = 1;
- if (fd < 0 && !kernel_has_tainted)
- return; /* New modutils on old kernel */
- printf("Warning: loading %s will taint the kernel: %s%s\n",
- m_name, text1, text2);
- if (first) {
- printf(" See %s for information about tainted modules\n", TAINT_URL);
- first = 0;
- }
- if (fd >= 0) {
- read(fd, buf, sizeof(buf)-1);
- buf[sizeof(buf)-1] = '\0';
- oldval = strtoul(buf, NULL, 10);
- sprintf(buf, "%d\n", oldval | taint);
- write(fd, buf, strlen(buf));
- }
-}
-
-/* Check if loading this module will taint the kernel. */
-static void check_tainted_module(struct obj_file *f, char *m_name)
-{
- static const char tainted_file[] = TAINT_FILENAME;
- int fd, kernel_has_tainted;
- const char *ptr;
-
- kernel_has_tainted = 1;
- if ((fd = open(tainted_file, O_RDWR)) < 0) {
- if (errno == ENOENT)
- kernel_has_tainted = 0;
- else if (errno == EACCES)
- kernel_has_tainted = 1;
- else {
- perror(tainted_file);
- kernel_has_tainted = 0;
- }
- }
-
- switch (obj_gpl_license(f, &ptr)) {
- case 0:
- break;
- case 1:
- set_tainted(f, fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
- break;
- case 2:
- /* The module has a non-GPL license so we pretend that the
- * kernel always has a taint flag to get a warning even on
- * kernels without the proc flag.
- */
- set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
- break;
- default:
- set_tainted(f, fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "Unexpected return from obj_gpl_license", "");
- break;
- }
-
- if (flag_force_load)
- set_tainted(f, fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
-
- if (fd >= 0)
- close(fd);
-}
-#else /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
-#define check_tainted_module(x, y) do { } while(0);
-#endif /* CONFIG_FEATURE_CHECK_TAINTED_MODULE */
-
-#ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
-/* add module source, timestamp, kernel version and a symbol for the
- * start of some sections. this info is used by ksymoops to do better
- * debugging.
- */
-static int
-get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
-{
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
- if (get_modinfo_value(f, "kernel_version") == NULL)
- return old_get_module_version(f, str);
- else
- return new_get_module_version(f, str);
-#else /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
- strncpy(str, "???", sizeof(str));
- return -1;
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-}
-
-/* add module source, timestamp, kernel version and a symbol for the
- * start of some sections. this info is used by ksymoops to do better
- * debugging.
- */
-static void
-add_ksymoops_symbols(struct obj_file *f, const char *filename,
- const char *m_name)
-{
- static const char symprefix[] = "__insmod_";
- struct obj_section *sec;
- struct obj_symbol *sym;
- char *name, *absolute_filename;
- char str[STRVERSIONLEN], real[PATH_MAX];
- int i, l, lm_name, lfilename, use_ksymtab, version;
- struct stat statbuf;
-
- static const char *section_names[] = {
- ".text",
- ".rodata",
- ".data",
- ".bss"
- ".sbss"
- };
-
- if (realpath(filename, real)) {
- absolute_filename = bb_xstrdup(real);
- }
- else {
- int save_errno = errno;
- bb_error_msg("cannot get realpath for %s", filename);
- errno = save_errno;
- perror("");
- absolute_filename = bb_xstrdup(filename);
- }
-
- lm_name = strlen(m_name);
- lfilename = strlen(absolute_filename);
-
- /* add to ksymtab if it already exists or there is no ksymtab and other symbols
- * are not to be exported. otherwise leave ksymtab alone for now, the
- * "export all symbols" compatibility code will export these symbols later.
- */
- use_ksymtab = obj_find_section(f, "__ksymtab") || !flag_export;
-
- if ((sec = obj_find_section(f, ".this"))) {
- /* tag the module header with the object name, last modified
- * timestamp and module version. worst case for module version
- * is 0xffffff, decimal 16777215. putting all three fields in
- * one symbol is less readable but saves kernel space.
- */
- l = sizeof(symprefix)+ /* "__insmod_" */
- lm_name+ /* module name */
- 2+ /* "_O" */
- lfilename+ /* object filename */
- 2+ /* "_M" */
- 2*sizeof(statbuf.st_mtime)+ /* mtime in hex */
- 2+ /* "_V" */
- 8+ /* version in dec */
- 1; /* nul */
- name = xmalloc(l);
- if (stat(absolute_filename, &statbuf) != 0)
- statbuf.st_mtime = 0;
- version = get_module_version(f, str); /* -1 if not found */
- snprintf(name, l, "%s%s_O%s_M%0*lX_V%d",
- symprefix, m_name, absolute_filename,
- (int)(2*sizeof(statbuf.st_mtime)), statbuf.st_mtime,
- version);
- sym = obj_add_symbol(f, name, -1,
- ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
- sec->idx, sec->header.sh_addr, 0);
- if (use_ksymtab)
- new_add_ksymtab(f, sym);
- }
- free(absolute_filename);
-#ifdef _NOT_SUPPORTED_
- /* record where the persistent data is going, same address as previous symbol */
-
- if (f->persist) {
- l = sizeof(symprefix)+ /* "__insmod_" */
- lm_name+ /* module name */
- 2+ /* "_P" */
- strlen(f->persist)+ /* data store */
- 1; /* nul */
- name = xmalloc(l);
- snprintf(name, l, "%s%s_P%s",
- symprefix, m_name, f->persist);
- sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
- sec->idx, sec->header.sh_addr, 0);
- if (use_ksymtab)
- new_add_ksymtab(f, sym);
- }
-#endif /* _NOT_SUPPORTED_ */
- /* tag the desired sections if size is non-zero */
-
- for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); ++i) {
- if ((sec = obj_find_section(f, section_names[i])) &&
- sec->header.sh_size) {
- l = sizeof(symprefix)+ /* "__insmod_" */
- lm_name+ /* module name */
- 2+ /* "_S" */
- strlen(sec->name)+ /* section name */
- 2+ /* "_L" */
- 8+ /* length in dec */
- 1; /* nul */
- name = xmalloc(l);
- snprintf(name, l, "%s%s_S%s_L%ld",
- symprefix, m_name, sec->name,
- (long)sec->header.sh_size);
- sym = obj_add_symbol(f, name, -1, ELFW(ST_INFO) (STB_GLOBAL, STT_NOTYPE),
- sec->idx, sec->header.sh_addr, 0);
- if (use_ksymtab)
- new_add_ksymtab(f, sym);
- }
- }
-}
-#endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
-
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
-static void print_load_map(struct obj_file *f)
-{
- struct obj_symbol *sym;
- struct obj_symbol **all, **p;
- struct obj_section *sec;
- int i, nsyms, *loaded;
- /* Report on the section layout. */
+ USE_FEATURE_2_4_MODULES(
+ getopt32(argv, INSMOD_OPTS INSMOD_ARGS);
+ argv += optind - 1;
+ );
- printf("Sections: Size %-*s Align\n",
- (int) (2 * sizeof(void *)), "Address");
-
- for (sec = f->load_order; sec; sec = sec->load_next) {
- int a;
- unsigned long tmp;
-
- for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
- tmp >>= 1;
- if (a == -1)
- a = 0;
-
- printf("%-15s %08lx %0*lx 2**%d\n",
- sec->name,
- (long)sec->header.sh_size,
- (int) (2 * sizeof(void *)),
- (long)sec->header.sh_addr,
- a);
- }
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP_FULL
- /* Quick reference which section indicies are loaded. */
-
- loaded = alloca(sizeof(int) * (i = f->header.e_shnum));
- while (--i >= 0)
- loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
-
- /* Collect the symbols we'll be listing. */
-
- for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (sym->secidx <= SHN_HIRESERVE
- && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
- ++nsyms;
-
- all = alloca(nsyms * sizeof(struct obj_symbol *));
-
- for (i = 0, p = all; i < HASH_BUCKETS; ++i)
- for (sym = f->symtab[i]; sym; sym = sym->next)
- if (sym->secidx <= SHN_HIRESERVE
- && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx]))
- *p++ = sym;
-
- /* And list them. */
- printf("\nSymbols:\n");
- for (p = all; p < all + nsyms; ++p) {
- char type = '?';
- unsigned long value;
-
- sym = *p;
- if (sym->secidx == SHN_ABS) {
- type = 'A';
- value = sym->value;
- } else if (sym->secidx == SHN_UNDEF) {
- type = 'U';
- value = 0;
- } else {
- sec = f->sections[sym->secidx];
-
- if (sec->header.sh_type == SHT_NOBITS)
- type = 'B';
- else if (sec->header.sh_flags & SHF_ALLOC) {
- if (sec->header.sh_flags & SHF_EXECINSTR)
- type = 'T';
- else if (sec->header.sh_flags & SHF_WRITE)
- type = 'D';
- else
- type = 'R';
- }
- value = sym->value + sec->header.sh_addr;
- }
-
- if (ELFW(ST_BIND) (sym->info) == STB_LOCAL)
- type = tolower(type);
-
- printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
- type, sym->name);
- }
-#endif
-}
-
-#endif
-
-extern int insmod_main( int argc, char **argv)
-{
- int opt;
- int k_crcs;
- int k_new_syscalls;
- int len;
- char *tmp, *tmp1;
- unsigned long m_size;
- ElfW(Addr) m_addr;
- struct obj_file *f;
- struct stat st;
- char *m_name = 0;
- int exit_status = EXIT_FAILURE;
- int m_has_modinfo;
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
- struct utsname uts_info;
- char m_strversion[STRVERSIONLEN];
- int m_version;
- int m_crcs;
-#endif
-#ifdef CONFIG_FEATURE_CLEAN_UP
- FILE *fp = 0;
-#else
- FILE *fp;
-#endif
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
- int flag_print_load_map = 0;
-#endif
-
- /* Parse any options */
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
- while ((opt = getopt(argc, argv, "fkqsvxmLo:")) > 0) {
-#else
- while ((opt = getopt(argc, argv, "fkqsvxLo:")) > 0) {
-#endif
- switch (opt) {
- case 'f': /* force loading */
- flag_force_load = 1;
- break;
- case 'k': /* module loaded by kerneld, auto-cleanable */
- flag_autoclean = 1;
- break;
- case 's': /* log to syslog */
- /* log to syslog -- not supported */
- /* but kernel needs this for request_module(), */
- /* as this calls: modprobe -k -s -- <module> */
- /* so silently ignore this flag */
- break;
- case 'v': /* verbose output */
- flag_verbose = 1;
- break;
- case 'q': /* silent */
- flag_quiet = 1;
- break;
- case 'x': /* do not export externs */
- flag_export = 0;
- break;
- case 'o': /* name the output module */
- free(m_name);
- m_name = bb_xstrdup(optarg);
- break;
- case 'L': /* Stub warning */
- /* This is needed for compatibility with modprobe.
- * In theory, this does locking, but we don't do
- * that. So be careful and plan your life around not
- * loading the same module 50 times concurrently. */
- break;
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
- case 'm': /* print module load map */
- flag_print_load_map = 1;
- break;
-#endif
- default:
- bb_show_usage();
- }
- }
-
- if (argv[optind] == NULL) {
+ filename = *++argv;
+ if (!filename)
bb_show_usage();
- }
-
- /* Grab the module name */
- tmp1 = bb_xstrdup(argv[optind]);
- tmp = basename(tmp1);
- len = strlen(tmp);
-
- if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') {
- len-=2;
- tmp[len] = '\0';
- }
-
- bb_xasprintf(&m_fullName, "%s.o", tmp);
-
- if (!m_name) {
- m_name = tmp;
- } else {
- free(tmp1);
- tmp1 = 0; /* flag for free(m_name) before exit() */
- }
-
- /* 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) {
- 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;
- char *tmdn;
- char real_module_dir[FILENAME_MAX];
-
- tmdn = concat_path_file(_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 (tmdn, real_module_dir) == NULL)
- module_dir = tmdn;
- else
- module_dir = real_module_dir;
- recursive_action(module_dir, TRUE, FALSE, FALSE,
- check_module_name_match, 0, m_fullName);
- free(tmdn);
- }
-
- /* Check if we have found anything yet */
- if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
- {
- char module_dir[FILENAME_MAX];
-
- free(m_filename);
- m_filename = 0;
- 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))
- {
- if (m_filename == 0
- || ((fp = fopen(m_filename, "r")) == NULL))
- {
- bb_error_msg("%s: no module by that name found", m_fullName);
- goto out;
- }
- } else
- bb_error_msg_and_die("%s: no module by that name found", m_fullName);
- }
- } else
- m_filename = bb_xstrdup(argv[optind]);
-
- printf("Using %s\n", m_filename);
-
- if ((f = obj_load(fp, LOADBITS)) == NULL)
- bb_perror_msg_and_die("Could not load the module");
-
- if (get_modinfo_value(f, "kernel_version") == NULL)
- m_has_modinfo = 0;
- else
- m_has_modinfo = 1;
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
- /* Version correspondence? */
- if (!flag_quiet) {
- if (uname(&uts_info) < 0)
- uts_info.release[0] = '\0';
- if (m_has_modinfo) {
- m_version = new_get_module_version(f, m_strversion);
- } else {
- m_version = old_get_module_version(f, m_strversion);
- if (m_version == -1) {
- bb_error_msg("couldn't find the kernel version the module was "
- "compiled for");
- goto out;
- }
- }
-
- if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
- if (flag_force_load) {
- bb_error_msg("Warning: kernel-module version mismatch\n"
- "\t%s was compiled for kernel version %s\n"
- "\twhile this kernel is version %s",
- m_filename, m_strversion, uts_info.release);
- } else {
- bb_error_msg("kernel-module version mismatch\n"
- "\t%s was compiled for kernel version %s\n"
- "\twhile this kernel is version %s.",
- m_filename, m_strversion, uts_info.release);
- goto out;
- }
- }
- }
- k_crcs = 0;
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
- k_new_syscalls = !query_module(NULL, 0, NULL, 0, NULL);
-
- if (k_new_syscalls) {
-#ifdef CONFIG_FEATURE_NEW_MODULE_INTERFACE
- if (!new_get_kernel_symbols())
- goto out;
- k_crcs = new_is_kernel_checksummed();
-#else
- bb_error_msg("Not configured to support new kernels");
- goto out;
-#endif
- } else {
-#ifdef CONFIG_FEATURE_OLD_MODULE_INTERFACE
- if (!old_get_kernel_symbols(m_name))
- goto out;
- k_crcs = old_is_kernel_checksummed();
-#else
- bb_error_msg("Not configured to support old kernels");
- goto out;
-#endif
- }
-
-#ifdef CONFIG_FEATURE_INSMOD_VERSION_CHECKING
- if (m_has_modinfo)
- m_crcs = new_is_module_checksummed(f);
- else
- m_crcs = old_is_module_checksummed(f);
-
- if (m_crcs != k_crcs)
- obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
-#endif /* CONFIG_FEATURE_INSMOD_VERSION_CHECKING */
-
- /* Let the module know about the kernel symbols. */
- add_kernel_symbols(f);
-
- /* Allocate common symbols, symbol tables, and string tables. */
-
- if (k_new_syscalls
- ? !new_create_this_module(f, m_name)
- : !old_create_mod_use_count(f))
- {
- goto out;
- }
-
- if (!obj_check_undefineds(f)) {
- goto out;
- }
- obj_allocate_commons(f);
- check_tainted_module(f, m_name);
-
- /* done with the module name, on to the optional var=value arguments */
- ++optind;
-
- if (optind < argc) {
- if (m_has_modinfo
- ? !new_process_module_arguments(f, argc - optind, argv + optind)
- : !old_process_module_arguments(f, argc - optind, argv + optind))
- {
- goto out;
- }
- }
-
- arch_create_got(f);
- hide_special_symbols(f);
-
-#ifdef CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
- add_ksymoops_symbols(f, m_filename, m_name);
-#endif /* CONFIG_FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
-
- if (k_new_syscalls)
- new_create_module_ksymtab(f);
-
- /* Find current size of the module */
- m_size = obj_load_size(f);
-
-
- m_addr = create_module(m_name, m_size);
- if (m_addr == -1) switch (errno) {
- case EEXIST:
- bb_error_msg("A module named %s already exists", m_name);
- goto out;
- case ENOMEM:
- bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
- m_size);
- goto out;
- default:
- bb_perror_msg("create_module: %s", m_name);
- goto out;
- }
-
-#if !LOADBITS
- /*
- * the PROGBITS section was not loaded by the obj_load
- * now we can load them directly into the kernel memory
- */
- if (!obj_load_progbits(fp, f, (char*)m_addr)) {
- delete_module(m_name);
- goto out;
- }
-#endif
-
- if (!obj_relocate(f, m_addr)) {
- delete_module(m_name);
- goto out;
- }
-
- if (k_new_syscalls
- ? !new_init_module(m_name, f, m_size)
- : !old_init_module(m_name, f, m_size))
- {
- delete_module(m_name);
- goto out;
- }
-
-#ifdef CONFIG_FEATURE_INSMOD_LOAD_MAP
- if(flag_print_load_map)
- print_load_map(f);
-#endif
- exit_status = EXIT_SUCCESS;
+ rc = bb_init_module(filename, parse_cmdline_module_options(argv));
+ if (rc)
+ bb_error_msg("can't insert '%s': %s", filename, moderror(rc));
-out:
-#ifdef CONFIG_FEATURE_CLEAN_UP
- if(fp)
- fclose(fp);
- if(tmp1) {
- free(tmp1);
- } else {
- free(m_name);
- }
- free(m_filename);
-#endif
- return(exit_status);
+ return rc;
}
diff --git a/release/src/router/busybox/modutils/lsmod.c b/release/src/router/busybox/modutils/lsmod.c
index f1eebfc9..87dd1fcb 100644
--- a/release/src/router/busybox/modutils/lsmod.c
+++ b/release/src/router/busybox/modutils/lsmod.c
@@ -2,174 +2,78 @@
/*
* Mini lsmod implementation for busybox
*
- * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
- *
- * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
- * Nicolas Ferre <nicolas.ferre@alcove.fr> to support pre 2.1 kernels
- * (which lack the query_module() interface).
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
*
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <stddef.h>
-#include <errno.h>
-#include <unistd.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <assert.h>
-#include <getopt.h>
-#include <sys/utsname.h>
-#include <sys/file.h>
-#include "busybox.h"
-
+#include "libbb.h"
-#ifndef CONFIG_FEATURE_CHECK_TAINTED_MODULE
-static inline void check_tainted(void) { printf("\n"); }
-#else
-#define TAINT_FILENAME "/proc/sys/kernel/tainted"
-#define TAINT_PROPRIETORY_MODULE (1<<0)
-#define TAINT_FORCED_MODULE (1<<1)
-#define TAINT_UNSAFE_SMP (1<<2)
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
+enum {
+ TAINT_PROPRIETORY_MODULE = (1 << 0),
+ TAINT_FORCED_MODULE = (1 << 1),
+ TAINT_UNSAFE_SMP = (1 << 2),
+};
static void check_tainted(void)
{
- int tainted;
- FILE *f;
-
- tainted = 0;
- if ((f = fopen(TAINT_FILENAME, "r"))) {
- fscanf(f, "%d", &tainted);
- fclose(f);
+ int tainted = 0;
+ char *buf = xmalloc_open_read_close("/proc/sys/kernel/tainted", NULL);
+ if (buf) {
+ tainted = atoi(buf);
+ if (ENABLE_FEATURE_CLEAN_UP)
+ free(buf);
}
- if (f && tainted) {
+
+ if (tainted) {
printf(" Tainted: %c%c%c\n",
tainted & TAINT_PROPRIETORY_MODULE ? 'P' : 'G',
tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
tainted & TAINT_UNSAFE_SMP ? 'S' : ' ');
+ } else {
+ puts(" Not tainted");
}
- else {
- printf(" Not tainted\n");
- }
- printf("\n");
}
+#else
+static void check_tainted(void) { putchar('\n'); }
#endif
-#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
-
-struct module_info
-{
- unsigned long addr;
- unsigned long size;
- unsigned long flags;
- long usecount;
-};
-
-
-int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
-
-/* Values for query_module's which. */
-static const int QM_MODULES = 1;
-static const int QM_DEPS = 2;
-static const int QM_REFS = 3;
-static const int QM_SYMBOLS = 4;
-static const int QM_INFO = 5;
-
-/* Bits of module.flags. */
-static const int NEW_MOD_RUNNING = 1;
-static const int NEW_MOD_DELETED = 2;
-static const int NEW_MOD_AUTOCLEAN = 4;
-static const int NEW_MOD_VISITED = 8;
-static const int NEW_MOD_USED_ONCE = 16;
-static const int NEW_MOD_INITIALIZING = 64;
-
-extern int lsmod_main(int argc, char **argv)
+int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
{
- struct module_info info;
- char *module_names, *mn, *deps, *dn;
- size_t bufsize, depsize, nmod, count, i, j;
-
- module_names = xmalloc(bufsize = 256);
- if (my_query_module(NULL, QM_MODULES, (void **)&module_names, &bufsize,
- &nmod)) {
- bb_perror_msg_and_die("QM_MODULES");
- }
-
- deps = xmalloc(depsize = 256);
- printf("Module Size Used by");
+#if ENABLE_FEATURE_LSMOD_PRETTY_2_6_OUTPUT
+ char *token[4];
+ parser_t *parser = config_open("/proc/modules");
+ printf("%-24sSize Used by", "Module");
check_tainted();
- for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) {
- if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) {
- if (errno == ENOENT) {
- /* The module was removed out from underneath us. */
- continue;
- }
- /* else choke */
- bb_perror_msg_and_die("module %s: QM_INFO", mn);
- }
- if (my_query_module(mn, QM_REFS, (void **)&deps, &depsize, &count)) {
- if (errno == ENOENT) {
- /* The module was removed out from underneath us. */
- continue;
- }
- bb_perror_msg_and_die("module %s: QM_REFS", mn);
- }
- printf("%-20s%8lu%4ld", mn, info.size, info.usecount);
- if (info.flags & NEW_MOD_DELETED)
- printf(" (deleted)");
- else if (info.flags & NEW_MOD_INITIALIZING)
- printf(" (initializing)");
- else if (!(info.flags & NEW_MOD_RUNNING))
- printf(" (uninitialized)");
- else {
- if (info.flags & NEW_MOD_AUTOCLEAN)
- printf(" (autoclean) ");
- if (!(info.flags & NEW_MOD_USED_ONCE))
- printf(" (unused)");
+ if (ENABLE_FEATURE_2_4_MODULES
+ && get_linux_version_code() < KERNEL_VERSION(2,6,0)
+ ) {
+ while (config_read(parser, token, 4, 3, "# \t", PARSE_NORMAL)) {
+ if (token[3] != NULL && token[3][0] == '[') {
+ token[3]++;
+ token[3][strlen(token[3])-1] = '\0';
+ } else
+ token[3] = (char *) "";
+ printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
}
- if (count) printf(" [");
- for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) {
- printf("%s%s", dn, (j==count-1)? "":" ");
+ } else {
+ while (config_read(parser, token, 4, 4, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
+ // N.B. token[3] is either '-' (module is not used by others)
+ // or comma-separated list ended by comma
+ // so trimming the trailing char is just what we need!
+ token[3][strlen(token[3])-1] = '\0';
+ printf("%-19s %8s %2s %s\n", token[0], token[1], token[2], token[3]);
}
- if (count) printf("]");
-
- printf("\n");
}
-
-#ifdef CONFIG_FEATURE_CLEAN_UP
- free(module_names);
-#endif
-
- return( 0);
-}
-
-#else /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
-
-extern int lsmod_main(int argc, char **argv)
-{
- printf("Module Size Used by");
+ if (ENABLE_FEATURE_CLEAN_UP)
+ config_close(parser);
+#else
check_tainted();
-
- if (bb_xprint_file_by_name("/proc/modules") < 0) {
- return 0;
- }
- return 1;
+ xprint_and_close_file(xfopen_for_read("/proc/modules"));
+#endif
+ return EXIT_SUCCESS;
}
-
-#endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
diff --git a/release/src/router/busybox/modutils/modprobe-small.c b/release/src/router/busybox/modutils/modprobe-small.c
new file mode 100644
index 00000000..0b3a19a2
--- /dev/null
+++ b/release/src/router/busybox/modutils/modprobe-small.c
@@ -0,0 +1,798 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * simplified modprobe
+ *
+ * Copyright (c) 2008 Vladimir Dronnikov
+ * Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code)
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+
+#include <sys/utsname.h> /* uname() */
+#include <fnmatch.h>
+
+extern int init_module(void *module, unsigned long len, const char *options);
+extern int delete_module(const char *module, unsigned flags);
+extern int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
+
+
+#define dbg1_error_msg(...) ((void)0)
+#define dbg2_error_msg(...) ((void)0)
+//#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
+//#define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
+
+#define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
+
+enum {
+ OPT_q = (1 << 0), /* be quiet */
+ OPT_r = (1 << 1), /* module removal instead of loading */
+};
+
+typedef struct module_info {
+ char *pathname;
+ char *aliases;
+ char *deps;
+} module_info;
+
+/*
+ * GLOBALS
+ */
+struct globals {
+ module_info *modinfo;
+ char *module_load_options;
+ smallint dep_bb_seen;
+ smallint wrote_dep_bb_ok;
+ int module_count;
+ int module_found_idx;
+ int stringbuf_idx;
+ char stringbuf[32 * 1024]; /* some modules have lots of stuff */
+ /* for example, drivers/media/video/saa7134/saa7134.ko */
+};
+#define G (*ptr_to_globals)
+#define modinfo (G.modinfo )
+#define dep_bb_seen (G.dep_bb_seen )
+#define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
+#define module_count (G.module_count )
+#define module_found_idx (G.module_found_idx )
+#define module_load_options (G.module_load_options)
+#define stringbuf_idx (G.stringbuf_idx )
+#define stringbuf (G.stringbuf )
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+} while (0)
+
+
+static void appendc(char c)
+{
+ if (stringbuf_idx < sizeof(stringbuf))
+ stringbuf[stringbuf_idx++] = c;
+}
+
+static void bksp(void)
+{
+ if (stringbuf_idx)
+ stringbuf_idx--;
+}
+
+static void append(const char *s)
+{
+ size_t len = strlen(s);
+ if (stringbuf_idx + len < sizeof(stringbuf)) {
+ memcpy(stringbuf + stringbuf_idx, s, len);
+ stringbuf_idx += len;
+ }
+}
+
+static void reset_stringbuf(void)
+{
+ stringbuf_idx = 0;
+}
+
+static char* copy_stringbuf(void)
+{
+ char *copy = xmalloc(stringbuf_idx);
+ return memcpy(copy, stringbuf, stringbuf_idx);
+}
+
+static char* find_keyword(char *ptr, size_t len, const char *word)
+{
+ int wlen;
+
+ if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
+ return NULL;
+
+ wlen = strlen(word);
+ len -= wlen - 1;
+ while ((ssize_t)len > 0) {
+ char *old = ptr;
+ /* search for the first char in word */
+ ptr = memchr(ptr, *word, len);
+ if (ptr == NULL) /* no occurance left, done */
+ break;
+ if (strncmp(ptr, word, wlen) == 0)
+ return ptr + wlen; /* found, return ptr past it */
+ ++ptr;
+ len -= (ptr - old);
+ }
+ return NULL;
+}
+
+static void replace(char *s, char what, char with)
+{
+ while (*s) {
+ if (what == *s)
+ *s = with;
+ ++s;
+ }
+}
+
+/* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
+static char* str_2_list(const char *str)
+{
+ int len = strlen(str) + 1;
+ char *dst = xmalloc(len + 1);
+
+ dst[len] = '\0';
+ memcpy(dst, str, len);
+//TODO: protect against 2+ spaces: "word word"
+ replace(dst, ' ', '\0');
+ return dst;
+}
+
+/* We use error numbers in a loose translation... */
+static const char *moderror(int err)
+{
+ switch (err) {
+ case ENOEXEC:
+ return "invalid module format";
+ case ENOENT:
+ return "unknown symbol in module or invalid parameter";
+ case ESRCH:
+ return "module has wrong symbol version";
+ case EINVAL: /* "invalid parameter" */
+ return "unknown symbol in module or invalid parameter"
+ + sizeof("unknown symbol in module or");
+ default:
+ return strerror(err);
+ }
+}
+
+static int load_module(const char *fname, const char *options)
+{
+#if 1
+ int r;
+ size_t len = MAXINT(ssize_t);
+ char *module_image;
+ dbg1_error_msg("load_module('%s','%s')", fname, options);
+
+ module_image = xmalloc_open_zipped_read_close(fname, &len);
+ r = (!module_image || init_module(module_image, len, options ? options : "") != 0);
+ free(module_image);
+ dbg1_error_msg("load_module:%d", r);
+ return r; /* 0 = success */
+#else
+ /* For testing */
+ dbg1_error_msg("load_module('%s','%s')", fname, options);
+ return 1;
+#endif
+}
+
+static void parse_module(module_info *info, const char *pathname)
+{
+ char *module_image;
+ char *ptr;
+ size_t len;
+ size_t pos;
+ dbg1_error_msg("parse_module('%s')", pathname);
+
+ /* Read (possibly compressed) module */
+ len = 64 * 1024 * 1024; /* 64 Mb at most */
+ module_image = xmalloc_open_zipped_read_close(pathname, &len);
+//TODO: optimize redundant module body reads
+
+ /* "alias1 symbol:sym1 alias2 symbol:sym2" */
+ reset_stringbuf();
+ pos = 0;
+ while (1) {
+ ptr = find_keyword(module_image + pos, len - pos, "alias=");
+ if (!ptr) {
+ ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
+ if (!ptr)
+ break;
+ /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
+ * in many modules. What do they mean? */
+ if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
+ goto skip;
+ dbg2_error_msg("alias:'symbol:%s'", ptr);
+ append("symbol:");
+ } else {
+ dbg2_error_msg("alias:'%s'", ptr);
+ }
+ append(ptr);
+ appendc(' ');
+ skip:
+ pos = (ptr - module_image);
+ }
+ bksp(); /* remove last ' ' */
+ appendc('\0');
+ info->aliases = copy_stringbuf();
+ replace(info->aliases, '-', '_');
+
+ /* "dependency1 depandency2" */
+ reset_stringbuf();
+ ptr = find_keyword(module_image, len, "depends=");
+ if (ptr && *ptr) {
+ replace(ptr, ',', ' ');
+ replace(ptr, '-', '_');
+ dbg2_error_msg("dep:'%s'", ptr);
+ append(ptr);
+ }
+ appendc('\0');
+ info->deps = copy_stringbuf();
+
+ free(module_image);
+}
+
+static int pathname_matches_modname(const char *pathname, const char *modname)
+{
+ const char *fname = bb_get_last_path_component_nostrip(pathname);
+ const char *suffix = strrstr(fname, ".ko");
+//TODO: can do without malloc?
+ char *name = xstrndup(fname, suffix - fname);
+ int r;
+ replace(name, '-', '_');
+ r = (strcmp(name, modname) == 0);
+ free(name);
+ return r;
+}
+
+static FAST_FUNC int fileAction(const char *pathname,
+ struct stat *sb UNUSED_PARAM,
+ void *modname_to_match,
+ int depth UNUSED_PARAM)
+{
+ int cur;
+ const char *fname;
+
+ pathname += 2; /* skip "./" */
+ fname = bb_get_last_path_component_nostrip(pathname);
+ if (!strrstr(fname, ".ko")) {
+ dbg1_error_msg("'%s' is not a module", pathname);
+ return TRUE; /* not a module, continue search */
+ }
+
+ cur = module_count++;
+ modinfo = xrealloc_vector(modinfo, 12, cur);
+ modinfo[cur].pathname = xstrdup(pathname);
+ /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
+ /*modinfo[cur+1].pathname = NULL;*/
+
+ if (!pathname_matches_modname(fname, modname_to_match)) {
+ dbg1_error_msg("'%s' module name doesn't match", pathname);
+ return TRUE; /* module name doesn't match, continue search */
+ }
+
+ dbg1_error_msg("'%s' module name matches", pathname);
+ module_found_idx = cur;
+ parse_module(&modinfo[cur], pathname);
+
+ if (!(option_mask32 & OPT_r)) {
+ if (load_module(pathname, module_load_options) == 0) {
+ /* Load was successful, there is nothing else to do.
+ * This can happen ONLY for "top-level" module load,
+ * not a dep, because deps dont do dirscan. */
+ exit(EXIT_SUCCESS);
+ }
+ }
+
+ return TRUE;
+}
+
+static int load_dep_bb(void)
+{
+ char *line;
+ FILE *fp = fopen_for_read(DEPFILE_BB);
+
+ if (!fp)
+ return 0;
+
+ dep_bb_seen = 1;
+ dbg1_error_msg("loading "DEPFILE_BB);
+
+ /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
+ * we scanned the dir and found no module by name, then we search
+ * for alias (full scan), and we decided to generate modprobe.dep.bb.
+ * But we see modprobe.dep.bb.new! Other modprobe is at work!
+ * We wait and other modprobe renames it to modprobe.dep.bb.
+ * Now we can use it.
+ * But we already have modinfo[] filled, and "module_count = 0"
+ * makes us start anew. Yes, we leak modinfo[].xxx pointers -
+ * there is not much of data there anyway. */
+ module_count = 0;
+ memset(&modinfo[0], 0, sizeof(modinfo[0]));
+
+ while ((line = xmalloc_fgetline(fp)) != NULL) {
+ char* space;
+ int cur;
+
+ if (!line[0]) {
+ free(line);
+ continue;
+ }
+ space = strchrnul(line, ' ');
+ cur = module_count++;
+ modinfo = xrealloc_vector(modinfo, 12, cur);
+ /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
+ modinfo[cur].pathname = line; /* we take ownership of malloced block here */
+ if (*space)
+ *space++ = '\0';
+ modinfo[cur].aliases = space;
+ modinfo[cur].deps = xmalloc_fgetline(fp) ? : xzalloc(1);
+ if (modinfo[cur].deps[0]) {
+ /* deps are not "", so next line must be empty */
+ line = xmalloc_fgetline(fp);
+ /* Refuse to work with damaged config file */
+ if (line && line[0])
+ bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
+ free(line);
+ }
+ }
+ return 1;
+}
+
+static int start_dep_bb_writeout(void)
+{
+ int fd;
+
+ /* depmod -n: write result to stdout */
+ if (applet_name[0] == 'd' && (option_mask32 & 1))
+ return STDOUT_FILENO;
+
+ fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
+ if (fd < 0) {
+ if (errno == EEXIST) {
+ int count = 5 * 20;
+ dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
+ while (1) {
+ usleep(1000*1000 / 20);
+ if (load_dep_bb()) {
+ dbg1_error_msg(DEPFILE_BB" appeared");
+ return -2; /* magic number */
+ }
+ if (!--count)
+ break;
+ }
+ bb_error_msg("deleting stale %s", DEPFILE_BB".new");
+ fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
+ }
+ }
+ dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
+ return fd;
+}
+
+static void write_out_dep_bb(int fd)
+{
+ int i;
+ FILE *fp;
+
+ /* We want good error reporting. fdprintf is not good enough. */
+ fp = fdopen(fd, "w");
+ if (!fp) {
+ close(fd);
+ goto err;
+ }
+ i = 0;
+ while (modinfo[i].pathname) {
+ fprintf(fp, "%s%s%s\n" "%s%s\n",
+ modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
+ modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
+ i++;
+ }
+ /* Badly formatted depfile is a no-no. Be paranoid. */
+ errno = 0;
+ if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
+ goto err;
+
+ if (fd == STDOUT_FILENO) /* it was depmod -n */
+ goto ok;
+
+ if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
+ err:
+ bb_perror_msg("can't create %s", DEPFILE_BB);
+ unlink(DEPFILE_BB".new");
+ } else {
+ ok:
+ wrote_dep_bb_ok = 1;
+ dbg1_error_msg("created "DEPFILE_BB);
+ }
+}
+
+static module_info* find_alias(const char *alias)
+{
+ int i;
+ int dep_bb_fd;
+ module_info *result;
+ dbg1_error_msg("find_alias('%s')", alias);
+
+ try_again:
+ /* First try to find by name (cheaper) */
+ i = 0;
+ while (modinfo[i].pathname) {
+ if (pathname_matches_modname(modinfo[i].pathname, alias)) {
+ dbg1_error_msg("found '%s' in module '%s'",
+ alias, modinfo[i].pathname);
+ if (!modinfo[i].aliases) {
+ parse_module(&modinfo[i], modinfo[i].pathname);
+ }
+ return &modinfo[i];
+ }
+ i++;
+ }
+
+ /* Ok, we definitely have to scan module bodies. This is a good
+ * moment to generate modprobe.dep.bb, if it does not exist yet */
+ dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
+ if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
+ goto try_again;
+
+ /* Scan all module bodies, extract modinfo (it contains aliases) */
+ i = 0;
+ result = NULL;
+ while (modinfo[i].pathname) {
+ char *desc, *s;
+ if (!modinfo[i].aliases) {
+ parse_module(&modinfo[i], modinfo[i].pathname);
+ }
+ if (result) {
+ i++;
+ continue;
+ }
+ /* "alias1 symbol:sym1 alias2 symbol:sym2" */
+ desc = str_2_list(modinfo[i].aliases);
+ /* Does matching substring exist? */
+ for (s = desc; *s; s += strlen(s) + 1) {
+ /* Aliases in module bodies can be defined with
+ * shell patterns. Example:
+ * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
+ * Plain strcmp() won't catch that */
+ if (fnmatch(s, alias, 0) == 0) {
+ dbg1_error_msg("found alias '%s' in module '%s'",
+ alias, modinfo[i].pathname);
+ result = &modinfo[i];
+ break;
+ }
+ }
+ free(desc);
+ if (result && dep_bb_fd < 0)
+ return result;
+ i++;
+ }
+
+ /* Create module.dep.bb if needed */
+ if (dep_bb_fd >= 0) {
+ write_out_dep_bb(dep_bb_fd);
+ }
+
+ dbg1_error_msg("find_alias '%s' returns %p", alias, result);
+ return result;
+}
+
+#if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
+// TODO: open only once, invent config_rewind()
+static int already_loaded(const char *name)
+{
+ int ret = 0;
+ char *s;
+ parser_t *parser = config_open2("/proc/modules", xfopen_for_read);
+ while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
+ if (strcmp(s, name) == 0) {
+ ret = 1;
+ break;
+ }
+ }
+ config_close(parser);
+ return ret;
+}
+#else
+#define already_loaded(name) is_rmmod
+#endif
+
+/*
+ * Given modules definition and module name (or alias, or symbol)
+ * load/remove the module respecting dependencies.
+ * NB: also called by depmod with bogus name "/",
+ * just in order to force modprobe.dep.bb creation.
+*/
+#if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
+#define process_module(a,b) process_module(a)
+#define cmdline_options ""
+#endif
+static void process_module(char *name, const char *cmdline_options)
+{
+ char *s, *deps, *options;
+ module_info *info;
+ int is_rmmod = (option_mask32 & OPT_r) != 0;
+ dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
+
+ replace(name, '-', '_');
+
+ dbg1_error_msg("already_loaded:%d is_rmmod:%d", already_loaded(name), is_rmmod);
+ if (already_loaded(name) != is_rmmod) {
+ dbg1_error_msg("nothing to do for '%s'", name);
+ return;
+ }
+
+ options = NULL;
+ if (!is_rmmod) {
+ char *opt_filename = xasprintf("/etc/modules/%s", name);
+ options = xmalloc_open_read_close(opt_filename, NULL);
+ if (options)
+ replace(options, '\n', ' ');
+#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
+ if (cmdline_options) {
+ /* NB: cmdline_options always have one leading ' '
+ * (see main()), we remove it here */
+ char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
+ cmdline_options + 1, options);
+ free(options);
+ options = op;
+ }
+#endif
+ free(opt_filename);
+ module_load_options = options;
+ dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
+ }
+
+ if (!module_count) {
+ /* Scan module directory. This is done only once.
+ * It will attempt module load, and will exit(EXIT_SUCCESS)
+ * on success. */
+ module_found_idx = -1;
+ recursive_action(".",
+ ACTION_RECURSE, /* flags */
+ fileAction, /* file action */
+ NULL, /* dir action */
+ name, /* user data */
+ 0); /* depth */
+ dbg1_error_msg("dirscan complete");
+ /* Module was not found, or load failed, or is_rmmod */
+ if (module_found_idx >= 0) { /* module was found */
+ info = &modinfo[module_found_idx];
+ } else { /* search for alias, not a plain module name */
+ info = find_alias(name);
+ }
+ } else {
+ info = find_alias(name);
+ }
+
+ /* rmmod? unload it by name */
+ if (is_rmmod) {
+ if (delete_module(name, O_NONBLOCK | O_EXCL) != 0
+ && !(option_mask32 & OPT_q)
+ ) {
+ bb_perror_msg("remove '%s'", name);
+ goto ret;
+ }
+ /* N.B. we do not stop here -
+ * continue to unload modules on which the module depends:
+ * "-r --remove: option causes modprobe to remove a module.
+ * If the modules it depends on are also unused, modprobe
+ * will try to remove them, too." */
+ }
+
+ if (!info) {
+ /* both dirscan and find_alias found nothing */
+ if (applet_name[0] != 'd') /* it wasn't depmod */
+ bb_error_msg("module '%s' not found", name);
+//TODO: _and_die()?
+ goto ret;
+ }
+
+ /* Iterate thru dependencies, trying to (un)load them */
+ deps = str_2_list(info->deps);
+ for (s = deps; *s; s += strlen(s) + 1) {
+ //if (strcmp(name, s) != 0) // N.B. do loops exist?
+ dbg1_error_msg("recurse on dep '%s'", s);
+ process_module(s, NULL);
+ dbg1_error_msg("recurse on dep '%s' done", s);
+ }
+ free(deps);
+
+ /* modprobe -> load it */
+ if (!is_rmmod) {
+ if (!options || strstr(options, "blacklist") == NULL) {
+ errno = 0;
+ if (load_module(info->pathname, options) != 0) {
+ if (EEXIST != errno) {
+ bb_error_msg("'%s': %s",
+ info->pathname,
+ moderror(errno));
+ } else {
+ dbg1_error_msg("'%s': %s",
+ info->pathname,
+ moderror(errno));
+ }
+ }
+ } else {
+ dbg1_error_msg("'%s': blacklisted", info->pathname);
+ }
+ }
+ ret:
+ free(options);
+//TODO: return load attempt result from process_module.
+//If dep didn't load ok, continuing makes little sense.
+}
+#undef cmdline_options
+
+
+/* For reference, module-init-tools v3.4 options:
+
+# insmod
+Usage: insmod filename [args]
+
+# rmmod --help
+Usage: rmmod [-fhswvV] modulename ...
+ -f (or --force) forces a module unload, and may crash your
+ machine. This requires the Forced Module Removal option
+ when the kernel was compiled.
+ -h (or --help) prints this help text
+ -s (or --syslog) says use syslog, not stderr
+ -v (or --verbose) enables more messages
+ -V (or --version) prints the version code
+ -w (or --wait) begins module removal even if it is used
+ and will stop new users from accessing the module (so it
+ should eventually fall to zero).
+
+# modprobe
+Usage: modprobe [-v] [-V] [-C config-file] [-n] [-i] [-q] [-b]
+ [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
+modprobe -r [-n] [-i] [-v] <modulename> ...
+modprobe -l -t <dirname> [ -a <modulename> ...]
+
+# depmod --help
+depmod 3.4 -- part of module-init-tools
+depmod -[aA] [-n -e -v -q -V -r -u]
+ [-b basedirectory] [forced_version]
+depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...
+If no arguments (except options) are given, "depmod -a" is assumed.
+depmod will output a dependency list suitable for the modprobe utility.
+Options:
+ -a, --all Probe all modules
+ -A, --quick Only does the work if there's a new module
+ -n, --show Write the dependency file on stdout only
+ -e, --errsyms Report not supplied symbols
+ -V, --version Print the release version
+ -v, --verbose Enable verbose mode
+ -h, --help Print this usage message
+The following options are useful for people managing distributions:
+ -b basedirectory
+ --basedir basedirectory
+ Use an image of a module tree
+ -F kernelsyms
+ --filesyms kernelsyms
+ Use the file instead of the current kernel symbols
+*/
+
+int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int modprobe_main(int argc UNUSED_PARAM, char **argv)
+{
+ struct utsname uts;
+ char applet0 = applet_name[0];
+ USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
+
+ /* are we lsmod? -> just dump /proc/modules */
+ if ('l' == applet0) {
+ xprint_and_close_file(xfopen_for_read("/proc/modules"));
+ return EXIT_SUCCESS;
+ }
+
+ INIT_G();
+
+ /* Prevent ugly corner cases with no modules at all */
+ modinfo = xzalloc(sizeof(modinfo[0]));
+
+ if ('i' != applet0) { /* not insmod */
+ /* Goto modules directory */
+ xchdir(CONFIG_DEFAULT_MODULES_DIR);
+ }
+ uname(&uts); /* never fails */
+
+ /* depmod? */
+ if ('d' == applet0) {
+ /* Supported:
+ * -n: print result to stdout
+ * -a: process all modules (default)
+ * optional VERSION parameter
+ * Ignored:
+ * -A: do work only if a module is newer than depfile
+ * -e: report any symbols which a module needs
+ * which are not supplied by other modules or the kernel
+ * -F FILE: System.map (symbols for -e)
+ * -q, -r, -u: noop?
+ * Not supported:
+ * -b BASEDIR: (TODO!) modules are in
+ * $BASEDIR/lib/modules/$VERSION
+ * -v: human readable deps to stdout
+ * -V: version (don't want to support it - people may depend
+ * on it as an indicator of "standard" depmod)
+ * -h: help (well duh)
+ * module1.o module2.o parameters (just ignored for now)
+ */
+ getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
+ argv += optind;
+ /* if (argv[0] && argv[1]) bb_show_usage(); */
+ /* Goto $VERSION directory */
+ xchdir(argv[0] ? argv[0] : uts.release);
+ /* Force full module scan by asking to find a bogus module.
+ * This will generate modules.dep.bb as a side effect. */
+ process_module((char*)"/", NULL);
+ return !wrote_dep_bb_ok;
+ }
+
+ /* insmod, modprobe, rmmod require at least one argument */
+ opt_complementary = "-1";
+ /* only -q (quiet) and -r (rmmod),
+ * the rest are accepted and ignored (compat) */
+ getopt32(argv, "qrfsvw");
+ argv += optind;
+
+ /* are we rmmod? -> simulate modprobe -r */
+ if ('r' == applet0) {
+ option_mask32 |= OPT_r;
+ }
+
+ if ('i' != applet0) { /* not insmod */
+ /* Goto $VERSION directory */
+ xchdir(uts.release);
+ }
+
+#if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
+ /* If not rmmod, parse possible module options given on command line.
+ * insmod/modprobe takes one module name, the rest are parameters. */
+ options = NULL;
+ if ('r' != applet0) {
+ char **arg = argv;
+ while (*++arg) {
+ /* Enclose options in quotes */
+ char *s = options;
+ options = xasprintf("%s \"%s\"", s ? s : "", *arg);
+ free(s);
+ *arg = NULL;
+ }
+ }
+#else
+ if ('r' != applet0)
+ argv[1] = NULL;
+#endif
+
+ if ('i' == applet0) { /* insmod */
+ size_t len;
+ void *map;
+
+ len = MAXINT(ssize_t);
+ map = xmalloc_xopen_read_close(*argv, &len);
+ if (init_module(map, len,
+ USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(options ? options : "")
+ SKIP_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE("")
+ ) != 0)
+ bb_error_msg_and_die("can't insert '%s': %s",
+ *argv, moderror(errno));
+ return 0;
+ }
+
+ /* Try to load modprobe.dep.bb */
+ load_dep_bb();
+
+ /* Load/remove modules.
+ * Only rmmod loops here, modprobe has only argv[0] */
+ do {
+ process_module(*argv++, options);
+ } while (*argv);
+
+ if (ENABLE_FEATURE_CLEAN_UP) {
+ USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
+ }
+ return EXIT_SUCCESS;
+}
diff --git a/release/src/router/busybox/modutils/modprobe.c b/release/src/router/busybox/modutils/modprobe.c
index b4bff510..310eebc5 100644
--- a/release/src/router/busybox/modutils/modprobe.c
+++ b/release/src/router/busybox/modutils/modprobe.c
@@ -2,565 +2,420 @@
/*
* Modprobe written from scratch for BusyBox
*
- * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
- * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
+ * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
+ * Copyright (c) 2008 Vladimir Dronnikov
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
-*/
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
+ * we expect the full dependency list to be specified in modules.dep. Older
+ * versions would only export the direct dependency list.
+ */
+
+#include "libbb.h"
+#include "modutils.h"
#include <sys/utsname.h>
-#include <getopt.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <string.h>
-#include <ctype.h>
-#include <fcntl.h>
-#include "busybox.h"
-
-
-
-struct dep_t {
- char * m_module;
- char * m_options;
-
- int m_isalias : 1;
- int m_reserved : 15;
-
- int m_depcnt : 16;
- char ** m_deparr;
-
- struct dep_t * m_next;
+#include <fnmatch.h>
+
+//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
+#define DBG(...) ((void)0)
+
+#define MODULE_FLAG_LOADED 0x0001
+#define MODULE_FLAG_NEED_DEPS 0x0002
+/* "was seen in modules.dep": */
+#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
+#define MODULE_FLAG_BLACKLISTED 0x0008
+
+struct module_entry { /* I'll call it ME. */
+ unsigned flags;
+ char *modname; /* stripped of /path/, .ext and s/-/_/g */
+ const char *probed_name; /* verbatim as seen on cmdline */
+ char *options; /* options from config files */
+ llist_t *realnames; /* strings. if this module is an alias, */
+ /* real module name is one of these. */
+//Can there really be more than one? Example from real kernel?
+ llist_t *deps; /* strings. modules we depend on */
+};
+
+#define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
+enum {
+ MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
+ MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
+ MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
+ MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
+ MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
+ MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
+ MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
+ MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
+ MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
+ MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
};
-struct mod_list_t {
- char * m_module;
- char * m_options;
-
- struct mod_list_t * m_prev;
- struct mod_list_t * m_next;
+struct globals {
+ llist_t *db; /* MEs of all modules ever seen (caching for speed) */
+ llist_t *probes; /* MEs of module(s) requested on cmdline */
+ char *cmdline_mopts; /* module options from cmdline */
+ int num_unresolved_deps;
+ /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
+ smallint need_symbols;
};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { } while (0)
-static struct dep_t *depend;
-static int autoclean, show_only, quiet, do_syslog, verbose;
+static int read_config(const char *path);
-int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
+static char *gather_options_str(char *opts, const char *append)
{
- char *tag, *value;
-
- while ( isspace ( *buffer ))
- buffer++;
- tag = value = buffer;
- while ( !isspace ( *value ))
- value++;
- *value++ = 0;
- while ( isspace ( *value ))
- value++;
-
- *ptag = tag;
- *pvalue = value;
-
- return bb_strlen( tag ) && bb_strlen( value );
+ /* Speed-optimized. We call gather_options_str many times. */
+ if (opts == NULL) {
+ opts = xstrdup(append);
+ } else {
+ int optlen = strlen(opts);
+ opts = xrealloc(opts, optlen + strlen(append) + 2);
+ sprintf(opts + optlen, " %s", append);
+ }
+ return opts;
}
-/* Jump through hoops to simulate how fgets() grabs just one line at a
- * time... Don't use any stdio since modprobe gets called from a kernel
- * thread and stdio junk can overflow the limited stack...
- */
-static char *reads ( int fd, char *buffer, size_t len )
+static struct module_entry *helper_get_module(const char *module, int create)
{
- int n = read ( fd, buffer, len );
-
- if ( n > 0 ) {
- char *p;
-
- buffer [len-1] = 0;
- p = strchr ( buffer, '\n' );
-
- if ( p ) {
- off_t offset;
-
- offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
- lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
-
- p[1] = 0;
- }
- return buffer;
+ char modname[MODULE_NAME_LEN];
+ struct module_entry *e;
+ llist_t *l;
+
+ filename2modname(module, modname);
+ for (l = G.db; l != NULL; l = l->link) {
+ e = (struct module_entry *) l->data;
+ if (strcmp(e->modname, modname) == 0)
+ return e;
}
-
- else
- return 0;
+ if (!create)
+ return NULL;
+
+ e = xzalloc(sizeof(*e));
+ e->modname = xstrdup(modname);
+ llist_add_to(&G.db, e);
+
+ return e;
+}
+static struct module_entry *get_or_add_modentry(const char *module)
+{
+ return helper_get_module(module, 1);
+}
+static struct module_entry *get_modentry(const char *module)
+{
+ return helper_get_module(module, 0);
}
-static struct dep_t *build_dep ( void )
+static void add_probe(const char *name)
{
- int fd;
- struct utsname un;
- struct dep_t *first = 0;
- struct dep_t *current = 0;
- char buffer[256];
- char *filename = buffer;
- int continuation_line = 0;
-
- if ( uname ( &un ))
- return 0;
-
- // check for buffer overflow in following code
- if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
- return 0;
+ struct module_entry *m;
+
+ m = get_or_add_modentry(name);
+ if (!(option_mask32 & MODPROBE_OPT_REMOVE)
+ && (m->flags & MODULE_FLAG_LOADED)
+ ) {
+ DBG("skipping %s, it is already loaded", name);
+ return;
}
-
- strcpy ( filename, "/lib/modules/" );
- strcat ( filename, un.release );
- strcat ( filename, "/modules.dep" );
- if (( fd = open ( filename, O_RDONLY )) < 0 ) {
+ DBG("queuing %s", name);
+ m->probed_name = name;
+ m->flags |= MODULE_FLAG_NEED_DEPS;
+ llist_add_to_end(&G.probes, m);
+ G.num_unresolved_deps++;
+ if (ENABLE_FEATURE_MODUTILS_SYMBOLS
+ && strncmp(m->modname, "symbol:", 7) == 0
+ ) {
+ G.need_symbols = 1;
+ }
+}
- /* Ok, that didn't work. Fall back to looking in /lib/modules */
- if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
- return 0;
- }
+static int FAST_FUNC config_file_action(const char *filename,
+ struct stat *statbuf UNUSED_PARAM,
+ void *userdata UNUSED_PARAM,
+ int depth UNUSED_PARAM)
+{
+ char *tokens[3];
+ parser_t *p;
+ struct module_entry *m;
+ int rc = TRUE;
+
+ if (bb_basename(filename)[0] == '.')
+ goto error;
+
+ p = config_open2(filename, fopen_for_read);
+ if (p == NULL) {
+ rc = FALSE;
+ goto error;
}
- while ( reads ( fd, buffer, sizeof( buffer ))) {
- int l = bb_strlen ( buffer );
- char *p = 0;
-
- while ( isspace ( buffer [l-1] )) {
- buffer [l-1] = 0;
- l--;
- }
-
- if ( l == 0 ) {
- continuation_line = 0;
- continue;
- }
-
- if ( !continuation_line ) {
- char *col = strchr ( buffer, ':' );
-
- if ( col ) {
- char *mods;
- char *mod;
- int ext = 0;
-
- *col = 0;
- mods = strrchr ( buffer, '/' );
-
- if ( !mods )
- mods = buffer;
- else
- mods++;
-
- if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
- ext = 2;
-
- mod = bb_xstrndup ( mods, col - mods - ext );
-
- if ( !current ) {
- first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
- }
- else {
- current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
- current = current-> m_next;
- }
- current-> m_module = mod;
- current-> m_options = 0;
- current-> m_isalias = 0;
- current-> m_depcnt = 0;
- current-> m_deparr = 0;
- current-> m_next = 0;
-
- //printf ( "%s:\n", mod );
-
- p = col + 1;
- }
- else
- p = 0;
- }
- else
- p = buffer;
-
- if ( p && *p ) {
- char *end = &buffer [l-1];
- char *deps = strrchr ( end, '/' );
- char *dep;
- int ext = 0;
-
- while ( isblank ( *end ) || ( *end == '\\' ))
- end--;
-
- deps = strrchr ( p, '/' );
-
- if ( !deps || ( deps < p )) {
- deps = p;
-
- while ( isblank ( *deps ))
- deps++;
- }
- else
- deps++;
-
- if (( *(end-1) == '.' ) && ( *end == 'o' ))
- ext = 2;
-
- /* Cope with blank lines */
- if ((end-deps-ext+1) <= 0)
+ while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
+//Use index_in_strings?
+ if (strcmp(tokens[0], "alias") == 0) {
+ /* alias <wildcard> <modulename> */
+ llist_t *l;
+ char wildcard[MODULE_NAME_LEN];
+ char *rmod;
+
+ if (tokens[2] == NULL)
continue;
-
- dep = bb_xstrndup ( deps, end - deps - ext + 1 );
-
- current-> m_depcnt++;
- current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
- current-> m_deparr [current-> m_depcnt - 1] = dep;
-
- //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
- }
-
- if ( buffer [l-1] == '\\' )
- continuation_line = 1;
- else
- continuation_line = 0;
- }
- close ( fd );
-
- // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
-
- if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
- if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
- return first;
-
- continuation_line = 0;
- while ( reads ( fd, buffer, sizeof( buffer ))) {
- int l;
- char *p;
-
- p = strchr ( buffer, '#' );
- if ( p )
- *p = 0;
-
- l = bb_strlen ( buffer );
-
- while ( l && isspace ( buffer [l-1] )) {
- buffer [l-1] = 0;
- l--;
- }
-
- if ( l == 0 ) {
- continuation_line = 0;
- continue;
- }
-
- if ( !continuation_line ) {
- if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
- char *alias, *mod;
-
- if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
- // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
-
- if ( !current ) {
- first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
- }
- else {
- current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
- current = current-> m_next;
- }
- current-> m_module = bb_xstrdup ( alias );
- current-> m_isalias = 1;
-
- if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
- current-> m_depcnt = 0;
- current-> m_deparr = 0;
- }
- else {
- current-> m_depcnt = 1;
- current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
- current-> m_deparr[0] = bb_xstrdup ( mod );
- }
- current-> m_next = 0;
+ filename2modname(tokens[1], wildcard);
+
+ for (l = G.probes; l != NULL; l = l->link) {
+ m = (struct module_entry *) l->data;
+ if (fnmatch(wildcard, m->modname, 0) != 0)
+ continue;
+ rmod = filename2modname(tokens[2], NULL);
+ llist_add_to(&m->realnames, rmod);
+
+ if (m->flags & MODULE_FLAG_NEED_DEPS) {
+ m->flags &= ~MODULE_FLAG_NEED_DEPS;
+ G.num_unresolved_deps--;
}
- }
- else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
- char *mod, *opt;
-
- if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
- struct dep_t *dt;
-
- for ( dt = first; dt; dt = dt-> m_next ) {
- if ( strcmp ( dt-> m_module, mod ) == 0 )
- break;
- }
- if ( dt ) {
- dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
- strcpy ( dt-> m_options, opt );
-
- // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
- }
+
+ m = get_or_add_modentry(rmod);
+ if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
+ m->flags |= MODULE_FLAG_NEED_DEPS;
+ G.num_unresolved_deps++;
}
}
+ } else if (strcmp(tokens[0], "options") == 0) {
+ /* options <modulename> <option...> */
+ if (tokens[2] == NULL)
+ continue;
+ m = get_or_add_modentry(tokens[1]);
+ m->options = gather_options_str(m->options, tokens[2]);
+ } else if (strcmp(tokens[0], "include") == 0) {
+ /* include <filename> */
+ read_config(tokens[1]);
+ } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
+ && strcmp(tokens[0], "blacklist") == 0
+ ) {
+ /* blacklist <modulename> */
+ get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
}
}
- close ( fd );
-
- return first;
+ config_close(p);
+error:
+ return rc;
}
-
-static int mod_process ( struct mod_list_t *list, int do_insert )
+static int read_config(const char *path)
{
- char lcmd [256];
- int rc = 1;
-
- while ( list ) {
- if ( do_insert )
- snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module, list-> m_options ? list-> m_options : "" );
- else
- snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
-
- if ( verbose )
- printf ( "%s\n", lcmd );
- if ( !show_only ) {
- int rc2 = system ( lcmd );
- if (do_insert) rc = rc2; /* only last module matters */
- else if (!rc2) rc = 0; /* success if remove any mod */
- }
-
- list = do_insert ? list-> m_prev : list-> m_next;
- }
- return (show_only) ? 0 : rc;
+ return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
+ config_file_action, NULL, NULL, 1);
}
-static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
+static int do_modprobe(struct module_entry *m)
{
- struct mod_list_t *find;
- struct dep_t *dt;
- char *opt = 0;
- int lm;
-
- // remove .o extension
- lm = bb_strlen ( mod );
- if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
- mod [lm-2] = 0;
-
- // check dependencies
- for ( dt = depend; dt; dt = dt-> m_next ) {
- if ( strcmp ( dt-> m_module, mod ) == 0 ) {
- opt = dt-> m_options;
- break;
- }
+ struct module_entry *m2 = m2; /* for compiler */
+ char *fn, *options;
+ int rc, first;
+ llist_t *l;
+
+ if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
+ DBG("skipping %s, not found in modules.dep", m->modname);
+ return -ENOENT;
}
-
- // resolve alias names
- while ( dt && dt-> m_isalias ) {
- if ( dt-> m_depcnt == 1 ) {
- struct dep_t *adt;
-
- for ( adt = depend; adt; adt = adt-> m_next ) {
- if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
- break;
- }
- if ( adt ) {
- dt = adt;
- mod = dt-> m_module;
- if ( !opt )
- opt = dt-> m_options;
+ DBG("do_modprob'ing %s", m->modname);
+
+ if (!(option_mask32 & MODPROBE_OPT_REMOVE))
+ m->deps = llist_rev(m->deps);
+
+ for (l = m->deps; l != NULL; l = l->link)
+ DBG("dep: %s", l->data);
+
+ first = 1;
+ rc = 0;
+ while (m->deps && rc == 0) {
+ fn = llist_pop(&m->deps);
+ m2 = get_or_add_modentry(fn);
+ if (option_mask32 & MODPROBE_OPT_REMOVE) {
+ if (m2->flags & MODULE_FLAG_LOADED) {
+ if (bb_delete_module(m2->modname, O_EXCL) != 0) {
+ if (first)
+ rc = errno;
+ } else {
+ m2->flags &= ~MODULE_FLAG_LOADED;
+ }
}
- else
- return;
+ /* do not error out if *deps* fail to unload */
+ first = 0;
+ } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
+ options = m2->options;
+ m2->options = NULL;
+ if (m == m2)
+ options = gather_options_str(options, G.cmdline_mopts);
+ rc = bb_init_module(fn, options);
+ DBG("loaded %s '%s', rc:%d", fn, options, rc);
+ if (rc == 0)
+ m2->flags |= MODULE_FLAG_LOADED;
+ free(options);
+ } else {
+ DBG("%s is already loaded, skipping", fn);
}
- else
- return;
- }
-
- // search for duplicates
- for ( find = *head; find; find = find-> m_next ) {
- if ( !strcmp ( mod, find-> m_module )) {
- // found -> dequeue it
-
- if ( find-> m_prev )
- find-> m_prev-> m_next = find-> m_next;
- else
- *head = find-> m_next;
-
- if ( find-> m_next )
- find-> m_next-> m_prev = find-> m_prev;
- else
- *tail = find-> m_prev;
-
- break; // there can be only one duplicate
- }
- }
- if ( !find ) { // did not find a duplicate
- find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
- find-> m_module = mod;
- find-> m_options = opt;
+ free(fn);
}
- // enqueue at tail
- if ( *tail )
- (*tail)-> m_next = find;
- find-> m_prev = *tail;
- find-> m_next = 0;
-
- if ( !*head )
- *head = find;
- *tail = find;
-
- if ( dt ) {
- int i;
-
- for ( i = 0; i < dt-> m_depcnt; i++ )
- check_dep ( dt-> m_deparr [i], head, tail );
+ if (rc && !(option_mask32 & INSMOD_OPT_SILENT)) {
+ bb_error_msg("failed to %sload module %s: %s",
+ (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
+ m2->probed_name ? m2->probed_name : m2->modname,
+ moderror(rc)
+ );
}
+
+ return rc;
}
+static void load_modules_dep(void)
+{
+ struct module_entry *m;
+ char *colon, *tokens[2];
+ parser_t *p;
+
+ /* Modprobe does not work at all without modprobe.dep,
+ * even if the full module name is given. Returning error here
+ * was making us later confuse user with this message:
+ * "module /full/path/to/existing/file/module.ko not found".
+ * It's better to die immediately, with good message.
+ * xfopen_for_read provides that. */
+ p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
+
+ while (G.num_unresolved_deps
+ && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
+ ) {
+ colon = last_char_is(tokens[0], ':');
+ if (colon == NULL)
+ continue;
+ *colon = 0;
+ m = get_modentry(tokens[0]);
+ if (m == NULL)
+ continue;
-static int mod_insert ( char *mod, int argc, char **argv )
-{
- struct mod_list_t *tail = 0;
- struct mod_list_t *head = 0;
- int rc;
-
- // get dep list for module mod
- check_dep ( mod, &head, &tail );
-
- if ( head && tail ) {
- if ( argc ) {
- int i;
- int l = 0;
-
- // append module args
- for ( i = 0; i < argc; i++ )
- l += ( bb_strlen ( argv [i] ) + 1 );
-
- head-> m_options = xrealloc ( head-> m_options, l + 1 );
- head-> m_options [0] = 0;
-
- for ( i = 0; i < argc; i++ ) {
- strcat ( head-> m_options, argv [i] );
- strcat ( head-> m_options, " " );
- }
+ /* Optimization... */
+ if ((m->flags & MODULE_FLAG_LOADED)
+ && !(option_mask32 & MODPROBE_OPT_REMOVE)
+ ) {
+ DBG("skip deps of %s, it's already loaded", tokens[0]);
+ continue;
}
-
- // process tail ---> head
- rc = mod_process ( tail, 1 );
+
+ m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
+ if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
+ G.num_unresolved_deps--;
+ llist_add_to(&m->deps, xstrdup(tokens[0]));
+ if (tokens[1])
+ string_to_llist(tokens[1], &m->deps, " ");
+ } else
+ DBG("skipping dep line");
}
- else
- rc = 1;
-
- return rc;
+ config_close(p);
}
-static int mod_remove ( char *mod )
+int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int modprobe_main(int argc UNUSED_PARAM, char **argv)
{
+ struct utsname uts;
int rc;
- static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
-
- struct mod_list_t *head = 0;
- struct mod_list_t *tail = 0;
-
- if ( mod )
- check_dep ( mod, &head, &tail );
- else // autoclean
- head = tail = &rm_a_dummy;
-
- if ( head && tail )
- rc = mod_process ( head, 0 ); // process head ---> tail
- else
- rc = 1;
- return rc;
-
-}
+ unsigned opt;
+ struct module_entry *me;
+
+ opt_complementary = "q-v:v-q";
+ opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
+ argv += optind;
+
+ if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
+ MODPROBE_OPT_SHOW_ONLY))
+ bb_error_msg_and_die("not supported");
+
+ if (!argv[0]) {
+ if (opt & MODPROBE_OPT_REMOVE) {
+ /* "modprobe -r" (w/o params).
+ * "If name is NULL, all unused modules marked
+ * autoclean will be removed".
+ */
+ if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
+ bb_perror_msg_and_die("rmmod");
+ }
+ return EXIT_SUCCESS;
+ }
+ /* Goto modules location */
+ xchdir(CONFIG_DEFAULT_MODULES_DIR);
+ uname(&uts);
+ xchdir(uts.release);
+
+ /* Retrieve module names of already loaded modules */
+ {
+ char *s;
+ parser_t *parser = config_open2("/proc/modules", fopen_for_read);
+ while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
+ get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
+ config_close(parser);
+ }
+ if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
+ /* Each argument is a module name */
+ do {
+ DBG("adding module %s", *argv);
+ add_probe(*argv++);
+ } while (*argv);
+ } else {
+ /* First argument is module name, rest are parameters */
+ DBG("probing just module %s", *argv);
+ add_probe(argv[0]);
+ G.cmdline_mopts = parse_cmdline_module_options(argv);
+ }
-extern int modprobe_main(int argc, char** argv)
-{
- int opt;
- int remove_opt = 0;
-
- autoclean = show_only = quiet = do_syslog = verbose = 0;
-
- while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
- switch(opt) {
- case 'c': // no config used
- case 'l': // no pattern matching
- return EXIT_SUCCESS;
- break;
- case 'C': // no config used
- case 't': // no pattern matching
- bb_error_msg_and_die("-t and -C not supported");
-
- case 'a': // ignore
- case 'd': // ignore
- break;
- case 'k':
- autoclean++;
- break;
- case 'n':
- show_only++;
- break;
- case 'q':
- quiet++;
- break;
- case 'r':
- remove_opt++;
- break;
- case 's':
- do_syslog++;
- break;
- case 'v':
- verbose++;
- break;
- case 'V':
- default:
- bb_show_usage();
- break;
- }
+ /* Happens if all requested modules are already loaded */
+ if (G.probes == NULL)
+ return EXIT_SUCCESS;
+
+ read_config("/etc/modprobe.conf");
+ read_config("/etc/modprobe.d");
+ if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
+ read_config("modules.symbols");
+ load_modules_dep();
+ if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
+ read_config("modules.alias");
+ load_modules_dep();
}
-
- depend = build_dep ( );
-
- if ( !depend )
- bb_error_msg_and_die ( "could not parse modules.dep\n" );
-
- if (remove_opt) {
- int rc = EXIT_SUCCESS;
- do {
- if (mod_remove ( optind < argc ?
- bb_xstrdup (argv [optind]) : NULL )) {
- bb_error_msg ("failed to remove module %s",
- argv [optind] );
- rc = EXIT_FAILURE;
+
+ while ((me = llist_pop(&G.probes)) != NULL) {
+ if (me->realnames == NULL) {
+ /* This is not an alias. Literal names are blacklisted
+ * only if '-b' is given.
+ */
+ if (!(opt & MODPROBE_OPT_BLACKLIST)
+ || !(me->flags & MODULE_FLAG_BLACKLISTED)
+ ) {
+ rc = do_modprobe(me);
+//FIXME: what if rc > 0?
+ if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
+ bb_error_msg("module %s not found",
+ me->probed_name);
}
- } while ( ++optind < argc );
-
- return rc;
+ } else {
+ /* Probe all realnames */
+ do {
+ char *realname = llist_pop(&me->realnames);
+ struct module_entry *m2;
+
+ DBG("probing %s by realname %s", me->modname, realname);
+ m2 = get_or_add_modentry(realname);
+ if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
+ do_modprobe(m2);
+//FIXME: error check?
+ free(realname);
+ } while (me->realnames != NULL);
+ }
}
- if (optind >= argc)
- bb_error_msg_and_die ( "No module or pattern provided\n" );
-
- if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
- bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
-
return EXIT_SUCCESS;
}
-
-
diff --git a/release/src/router/busybox/modutils/modutils-24.c b/release/src/router/busybox/modutils/modutils-24.c
new file mode 100644
index 00000000..a16cb1bb
--- /dev/null
+++ b/release/src/router/busybox/modutils/modutils-24.c
@@ -0,0 +1,3905 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini insmod implementation for busybox
+ *
+ * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
+ * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
+ *
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ * and Ron Alder <alder@lineo.com>
+ *
+ * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
+ * Added x86_64 support.
+ *
+ * Miles Bader <miles@gnu.org> added NEC V850E support.
+ *
+ * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
+ * and (theoretically) SH3. I have only tested SH4 in little endian mode.
+ *
+ * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
+ * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
+ * very minor changes required to also work with StrongArm and presumably
+ * all ARM based systems.
+ *
+ * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
+ * added Renesas H8/300 support.
+ *
+ * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
+ * Integrated support for sh64 (SH-5), from preliminary modutils
+ * patches from Benedict Gaster <benedict.gaster@superh.com>.
+ * Currently limited to support for 32bit ABI.
+ *
+ * Magnus Damm <damm@opensource.se> 22-May-2002.
+ * The plt and got code are now using the same structs.
+ * Added generic linked list code to fully support PowerPC.
+ * Replaced the mess in arch_apply_relocation() with architecture blocks.
+ * The arch_create_got() function got cleaned up with architecture blocks.
+ * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
+ *
+ * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
+ * PowerPC specific code stolen from modutils-2.3.16,
+ * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
+ * I've only tested the code on mpc8xx platforms in big-endian mode.
+ * Did some cleanup and added USE_xxx_ENTRIES...
+ *
+ * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
+ * based on modutils-2.4.2
+ * MIPS specific support for Elf loading and relocation.
+ * Copyright 1996, 1997 Linux International.
+ * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
+ *
+ * Based almost entirely on the Linux modutils-2.3.11 implementation.
+ * Copyright 1996, 1997 Linux International.
+ * New implementation contributed by Richard Henderson <rth@tamu.edu>
+ * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
+ * Restructured (and partly rewritten) by:
+ * Björn Ekwall <bj0rn@blox.se> February 1999
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+#include "modutils.h"
+#include <libgen.h>
+#include <sys/utsname.h>
+
+#if ENABLE_FEATURE_INSMOD_LOADINKMEM
+#define LOADBITS 0
+#else
+#define LOADBITS 1
+#endif
+
+/* Alpha */
+#if defined(__alpha__)
+#define MATCH_MACHINE(x) (x == EM_ALPHA)
+#define SHT_RELM SHT_RELA
+#define Elf64_RelM Elf64_Rela
+#define ELFCLASSM ELFCLASS64
+#endif
+
+/* ARM support */
+#if defined(__arm__)
+#define MATCH_MACHINE(x) (x == EM_ARM)
+#define SHT_RELM SHT_REL
+#define Elf32_RelM Elf32_Rel
+#define ELFCLASSM ELFCLASS32
+#define USE_PLT_ENTRIES
+#define PLT_ENTRY_SIZE 8
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 8
+#define USE_SINGLE
+#endif
+
+/* blackfin */
+#if defined(BFIN)
+#define MATCH_MACHINE(x) (x == EM_BLACKFIN)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#endif
+
+/* CRIS */
+#if defined(__cris__)
+#define MATCH_MACHINE(x) (x == EM_CRIS)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#ifndef EM_CRIS
+#define EM_CRIS 76
+#define R_CRIS_NONE 0
+#define R_CRIS_32 3
+#endif
+#endif
+
+/* H8/300 */
+#if defined(__H8300H__) || defined(__H8300S__)
+#define MATCH_MACHINE(x) (x == EM_H8_300)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_SINGLE
+#define SYMBOL_PREFIX "_"
+#endif
+
+/* PA-RISC / HP-PA */
+#if defined(__hppa__)
+#define MATCH_MACHINE(x) (x == EM_PARISC)
+#define SHT_RELM SHT_RELA
+#if defined(__LP64__)
+#define Elf64_RelM Elf64_Rela
+#define ELFCLASSM ELFCLASS64
+#else
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#endif
+#endif
+
+/* x86 */
+#if defined(__i386__)
+#ifndef EM_486
+#define MATCH_MACHINE(x) (x == EM_386)
+#else
+#define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
+#endif
+#define SHT_RELM SHT_REL
+#define Elf32_RelM Elf32_Rel
+#define ELFCLASSM ELFCLASS32
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 4
+#define USE_SINGLE
+#endif
+
+/* IA64, aka Itanium */
+#if defined(__ia64__)
+#define MATCH_MACHINE(x) (x == EM_IA_64)
+#define SHT_RELM SHT_RELA
+#define Elf64_RelM Elf64_Rela
+#define ELFCLASSM ELFCLASS64
+#endif
+
+/* m68k */
+#if defined(__mc68000__)
+#define MATCH_MACHINE(x) (x == EM_68K)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 4
+#define USE_SINGLE
+#endif
+
+/* Microblaze */
+#if defined(__microblaze__)
+#define USE_SINGLE
+#include <linux/elf-em.h>
+#define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#endif
+
+/* MIPS */
+#if defined(__mips__)
+#define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
+#define SHT_RELM SHT_REL
+#define Elf32_RelM Elf32_Rel
+#define ELFCLASSM ELFCLASS32
+/* 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 ARCHDATAM "__dbe_table"
+#endif
+
+/* Nios II */
+#if defined(__nios2__)
+#define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#endif
+
+/* PowerPC */
+#if defined(__powerpc64__)
+#define MATCH_MACHINE(x) (x == EM_PPC64)
+#define SHT_RELM SHT_RELA
+#define Elf64_RelM Elf64_Rela
+#define ELFCLASSM ELFCLASS64
+#elif defined(__powerpc__)
+#define MATCH_MACHINE(x) (x == EM_PPC)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_PLT_ENTRIES
+#define PLT_ENTRY_SIZE 16
+#define USE_PLT_LIST
+#define LIST_ARCHTYPE ElfW(Addr)
+#define USE_LIST
+#define ARCHDATAM "__ftr_fixup"
+#endif
+
+/* S390 */
+#if defined(__s390__)
+#define MATCH_MACHINE(x) (x == EM_S390)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_PLT_ENTRIES
+#define PLT_ENTRY_SIZE 8
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 8
+#define USE_SINGLE
+#endif
+
+/* SuperH */
+#if defined(__sh__)
+#define MATCH_MACHINE(x) (x == EM_SH)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 4
+#define USE_SINGLE
+/* the SH changes have only been tested in =little endian= mode */
+/* I'm not sure about big endian, so let's warn: */
+#if defined(__sh__) && BB_BIG_ENDIAN
+# error insmod.c may require changes for use on big endian SH
+#endif
+/* it may or may not work on the SH1/SH2... Error on those also */
+#if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
+#error insmod.c may require changes for SH1 or SH2 use
+#endif
+#endif
+
+/* Sparc */
+#if defined(__sparc__)
+#define MATCH_MACHINE(x) (x == EM_SPARC)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#endif
+
+/* v850e */
+#if defined(__v850e__)
+#define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
+#define SHT_RELM SHT_RELA
+#define Elf32_RelM Elf32_Rela
+#define ELFCLASSM ELFCLASS32
+#define USE_PLT_ENTRIES
+#define PLT_ENTRY_SIZE 8
+#define USE_SINGLE
+#ifndef EM_CYGNUS_V850 /* grumble */
+#define EM_CYGNUS_V850 0x9080
+#endif
+#define SYMBOL_PREFIX "_"
+#endif
+
+/* X86_64 */
+#if defined(__x86_64__)
+#define MATCH_MACHINE(x) (x == EM_X86_64)
+#define SHT_RELM SHT_RELA
+#define USE_GOT_ENTRIES
+#define GOT_ENTRY_SIZE 8
+#define USE_SINGLE
+#define Elf64_RelM Elf64_Rela
+#define ELFCLASSM ELFCLASS64
+#endif
+
+#ifndef SHT_RELM
+#error Sorry, but insmod.c does not yet support this architecture...
+#endif
+
+
+//----------------------------------------------------------------------------
+//--------modutils module.h, lines 45-242
+//----------------------------------------------------------------------------
+
+/* Definitions for the Linux module syscall interface.
+ Copyright 1996, 1997 Linux International.
+
+ Contributed by Richard Henderson <rth@tamu.edu>
+
+ This file is part of the Linux modutils.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+
+#ifndef MODUTILS_MODULE_H
+
+/*======================================================================*/
+/* For sizeof() which are related to the module platform and not to the
+ environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
+
+#define tgt_sizeof_char sizeof(char)
+#define tgt_sizeof_short sizeof(short)
+#define tgt_sizeof_int sizeof(int)
+#define tgt_sizeof_long sizeof(long)
+#define tgt_sizeof_char_p sizeof(char *)
+#define tgt_sizeof_void_p sizeof(void *)
+#define tgt_long long
+
+#if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
+#undef tgt_sizeof_long
+#undef tgt_sizeof_char_p
+#undef tgt_sizeof_void_p
+#undef tgt_long
+enum {
+ tgt_sizeof_long = 8,
+ tgt_sizeof_char_p = 8,
+ tgt_sizeof_void_p = 8
+};
+#define tgt_long long long
+#endif
+
+/*======================================================================*/
+/* The structures used in Linux 2.1. */
+
+/* Note: new_module_symbol does not use tgt_long intentionally */
+struct new_module_symbol {
+ unsigned long value;
+ unsigned long name;
+};
+
+struct new_module_persist;
+
+struct new_module_ref {
+ unsigned tgt_long dep; /* kernel addresses */
+ unsigned tgt_long ref;
+ unsigned tgt_long next_ref;
+};
+
+struct new_module {
+ unsigned tgt_long size_of_struct; /* == sizeof(module) */
+ unsigned tgt_long next;
+ unsigned tgt_long name;
+ unsigned tgt_long size;
+
+ tgt_long usecount;
+ unsigned tgt_long flags; /* AUTOCLEAN et al */
+
+ unsigned nsyms;
+ unsigned ndeps;
+
+ unsigned tgt_long syms;
+ unsigned tgt_long deps;
+ unsigned tgt_long refs;
+ unsigned tgt_long init;
+ unsigned tgt_long cleanup;
+ unsigned tgt_long ex_table_start;
+ unsigned tgt_long ex_table_end;
+#ifdef __alpha__
+ unsigned tgt_long gp;
+#endif
+ /* Everything after here is extension. */
+ unsigned tgt_long persist_start;
+ unsigned tgt_long persist_end;
+ unsigned tgt_long can_unload;
+ unsigned tgt_long runsize;
+ const char *kallsyms_start; /* All symbols for kernel debugging */
+ const char *kallsyms_end;
+ const char *archdata_start; /* arch specific data for module */
+ const char *archdata_end;
+ const char *kernel_data; /* Reserved for kernel internal use */
+};
+
+#ifdef ARCHDATAM
+#define ARCHDATA_SEC_NAME ARCHDATAM
+#else
+#define ARCHDATA_SEC_NAME "__archdata"
+#endif
+#define KALLSYMS_SEC_NAME "__kallsyms"
+
+
+struct new_module_info {
+ unsigned long addr;
+ unsigned long size;
+ unsigned long flags;
+ long usecount;
+};
+
+/* Bits of module.flags. */
+enum {
+ NEW_MOD_RUNNING = 1,
+ NEW_MOD_DELETED = 2,
+ NEW_MOD_AUTOCLEAN = 4,
+ NEW_MOD_VISITED = 8,
+ NEW_MOD_USED_ONCE = 16
+};
+
+int init_module(const char *name, const struct new_module *);
+int query_module(const char *name, int which, void *buf,
+ size_t bufsize, size_t *ret);
+
+/* Values for query_module's which. */
+enum {
+ QM_MODULES = 1,
+ QM_DEPS = 2,
+ QM_REFS = 3,
+ QM_SYMBOLS = 4,
+ QM_INFO = 5
+};
+
+/*======================================================================*/
+/* The system calls unchanged between 2.0 and 2.1. */
+
+unsigned long create_module(const char *, size_t);
+int delete_module(const char *module, unsigned int flags);
+
+
+#endif /* module.h */
+
+//----------------------------------------------------------------------------
+//--------end of modutils module.h
+//----------------------------------------------------------------------------
+
+
+
+//----------------------------------------------------------------------------
+//--------modutils obj.h, lines 253-462
+//----------------------------------------------------------------------------
+
+/* Elf object file loading and relocation routines.
+ Copyright 1996, 1997 Linux International.
+
+ Contributed by Richard Henderson <rth@tamu.edu>
+
+ This file is part of the Linux modutils.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 2 of the License, or (at your
+ option) any later version.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+
+#ifndef MODUTILS_OBJ_H
+
+/* The relocatable object is manipulated using elfin types. */
+
+#include <elf.h>
+#include <endian.h>
+
+#ifndef ElfW
+# if ELFCLASSM == ELFCLASS32
+# define ElfW(x) Elf32_ ## x
+# define ELFW(x) ELF32_ ## x
+# else
+# define ElfW(x) Elf64_ ## x
+# define ELFW(x) ELF64_ ## x
+# endif
+#endif
+
+/* For some reason this is missing from some ancient C libraries.... */
+#ifndef ELF32_ST_INFO
+# define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
+#endif
+
+#ifndef ELF64_ST_INFO
+# define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
+#endif
+
+#define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
+#define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
+#define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
+#define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
+#define ELF_R_SYM(val) ELFW(R_SYM)(val)
+
+struct obj_string_patch;
+struct obj_symbol_patch;
+
+struct obj_section
+{
+ ElfW(Shdr) header;
+ const char *name;
+ char *contents;
+ struct obj_section *load_next;
+ int idx;
+};
+
+struct obj_symbol
+{
+ struct obj_symbol *next; /* hash table link */
+ const char *name;
+ unsigned long value;
+ unsigned long size;
+ int secidx; /* the defining section index/module */
+ int info;
+ int ksymidx; /* for export to the kernel symtab */
+ int referenced; /* actually used in the link */
+};
+
+/* Hardcode the hash table size. We shouldn't be needing so many
+ symbols that we begin to degrade performance, and we get a big win
+ by giving the compiler a constant divisor. */
+
+#define HASH_BUCKETS 521
+
+struct obj_file {
+ ElfW(Ehdr) header;
+ ElfW(Addr) baseaddr;
+ struct obj_section **sections;
+ struct obj_section *load_order;
+ struct obj_section **load_order_search_start;
+ struct obj_string_patch *string_patches;
+ struct obj_symbol_patch *symbol_patches;
+ int (*symbol_cmp)(const char *, const char *);
+ unsigned long (*symbol_hash)(const char *);
+ unsigned long local_symtab_size;
+ struct obj_symbol **local_symtab;
+ struct obj_symbol *symtab[HASH_BUCKETS];
+};
+
+enum obj_reloc {
+ obj_reloc_ok,
+ obj_reloc_overflow,
+ obj_reloc_dangerous,
+ obj_reloc_unhandled
+};
+
+struct obj_string_patch {
+ struct obj_string_patch *next;
+ int reloc_secidx;
+ ElfW(Addr) reloc_offset;
+ ElfW(Addr) string_offset;
+};
+
+struct obj_symbol_patch {
+ struct obj_symbol_patch *next;
+ int reloc_secidx;
+ ElfW(Addr) reloc_offset;
+ struct obj_symbol *sym;
+};
+
+
+/* Generic object manipulation routines. */
+
+static unsigned long obj_elf_hash(const char *);
+
+static unsigned long obj_elf_hash_n(const char *, unsigned long len);
+
+static struct obj_symbol *obj_find_symbol(struct obj_file *f,
+ const char *name);
+
+static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
+ struct obj_symbol *sym);
+
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+static void obj_set_symbol_compare(struct obj_file *f,
+ int (*cmp)(const char *, const char *),
+ unsigned long (*hash)(const char *));
+#endif
+
+static struct obj_section *obj_find_section(struct obj_file *f,
+ const char *name);
+
+static void obj_insert_section_load_order(struct obj_file *f,
+ struct obj_section *sec);
+
+static struct obj_section *obj_create_alloced_section(struct obj_file *f,
+ const char *name,
+ unsigned long align,
+ unsigned long size);
+
+static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
+ const char *name,
+ unsigned long align,
+ unsigned long size);
+
+static void *obj_extend_section(struct obj_section *sec, unsigned long more);
+
+static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+ const char *string);
+
+static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+ struct obj_symbol *sym);
+
+static void obj_check_undefineds(struct obj_file *f);
+
+static void obj_allocate_commons(struct obj_file *f);
+
+static unsigned long obj_load_size(struct obj_file *f);
+
+static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
+
+#if !LOADBITS
+#define obj_load(image, image_size, loadprogbits) \
+ obj_load(image, image_size)
+#endif
+static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
+
+static int obj_create_image(struct obj_file *f, char *image);
+
+/* Architecture specific manipulation routines. */
+
+static struct obj_file *arch_new_file(void);
+
+static struct obj_section *arch_new_section(void);
+
+static struct obj_symbol *arch_new_symbol(void);
+
+static enum obj_reloc arch_apply_relocation(struct obj_file *f,
+ struct obj_section *targsec,
+ /*struct obj_section *symsec,*/
+ struct obj_symbol *sym,
+ ElfW(RelM) *rel, ElfW(Addr) value);
+
+static void arch_create_got(struct obj_file *f);
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
+static int obj_gpl_license(struct obj_file *f, const char **license);
+#endif
+#endif /* obj.h */
+//----------------------------------------------------------------------------
+//--------end of modutils obj.h
+//----------------------------------------------------------------------------
+
+
+/* SPFX is always a string, so it can be concatenated to string constants. */
+#ifdef SYMBOL_PREFIX
+#define SPFX SYMBOL_PREFIX
+#else
+#define SPFX ""
+#endif
+
+enum { STRVERSIONLEN = 64 };
+
+/*======================================================================*/
+
+#define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
+#define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
+#define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
+#define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
+#define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
+#define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
+
+/*======================================================================*/
+
+#if defined(USE_LIST)
+
+struct arch_list_entry
+{
+ struct arch_list_entry *next;
+ LIST_ARCHTYPE addend;
+ int offset;
+ int inited : 1;
+};
+
+#endif
+
+#if defined(USE_SINGLE)
+
+struct arch_single_entry
+{
+ int offset;
+ int inited : 1;
+ int allocated : 1;
+};
+
+#endif
+
+#if defined(__mips__)
+struct mips_hi16
+{
+ struct mips_hi16 *next;
+ ElfW(Addr) *addr;
+ ElfW(Addr) value;
+};
+#endif
+
+struct arch_file {
+ struct obj_file root;
+#if defined(USE_PLT_ENTRIES)
+ struct obj_section *plt;
+#endif
+#if defined(USE_GOT_ENTRIES)
+ struct obj_section *got;
+#endif
+#if defined(__mips__)
+ struct mips_hi16 *mips_hi16_list;
+#endif
+};
+
+struct arch_symbol {
+ struct obj_symbol root;
+#if defined(USE_PLT_ENTRIES)
+#if defined(USE_PLT_LIST)
+ struct arch_list_entry *pltent;
+#else
+ struct arch_single_entry pltent;
+#endif
+#endif
+#if defined(USE_GOT_ENTRIES)
+ struct arch_single_entry gotent;
+#endif
+};
+
+
+struct external_module {
+ const char *name;
+ ElfW(Addr) addr;
+ int used;
+ size_t nsyms;
+ struct new_module_symbol *syms;
+};
+
+static struct new_module_symbol *ksyms;
+static size_t nksyms;
+
+static struct external_module *ext_modules;
+static int n_ext_modules;
+static int n_ext_modules_used;
+
+/*======================================================================*/
+
+
+static struct obj_file *arch_new_file(void)
+{
+ struct arch_file *f;
+ f = xzalloc(sizeof(*f));
+ return &f->root; /* it's a first member */
+}
+
+static struct obj_section *arch_new_section(void)
+{
+ return xzalloc(sizeof(struct obj_section));
+}
+
+static struct obj_symbol *arch_new_symbol(void)
+{
+ struct arch_symbol *sym;
+ sym = xzalloc(sizeof(*sym));
+ return &sym->root;
+}
+
+static enum obj_reloc
+arch_apply_relocation(struct obj_file *f,
+ struct obj_section *targsec,
+ /*struct obj_section *symsec,*/
+ struct obj_symbol *sym,
+ ElfW(RelM) *rel, ElfW(Addr) v)
+{
+#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
+ || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
+ || defined(__powerpc__) || defined(__mips__)
+ struct arch_file *ifile = (struct arch_file *) f;
+#endif
+ enum obj_reloc ret = obj_reloc_ok;
+ ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
+#if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
+ || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
+ || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
+ || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
+ ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
+#endif
+#if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
+ struct arch_symbol *isym = (struct arch_symbol *) sym;
+#endif
+#if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
+ || defined(__sh__) || defined(__s390__)
+#if defined(USE_GOT_ENTRIES)
+ ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
+#endif
+#endif
+#if defined(USE_PLT_ENTRIES)
+ ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
+ unsigned long *ip;
+# if defined(USE_PLT_LIST)
+ struct arch_list_entry *pe;
+# else
+ struct arch_single_entry *pe;
+# endif
+#endif
+
+ switch (ELF_R_TYPE(rel->r_info)) {
+
+#if defined(__arm__)
+
+ case R_ARM_NONE:
+ break;
+
+ case R_ARM_ABS32:
+ *loc += v;
+ break;
+
+ case R_ARM_GOT32:
+ goto bb_use_got;
+
+ case R_ARM_GOTPC:
+ /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
+ * (which is .got) similar to branch,
+ * but is full 32 bits relative */
+
+ *loc += got - dot;
+ break;
+
+ case R_ARM_PC24:
+ case R_ARM_PLT32:
+ goto bb_use_plt;
+
+ case R_ARM_GOTOFF: /* address relative to the got */
+ *loc += v - got;
+ break;
+
+#elif defined(__cris__)
+
+ case R_CRIS_NONE:
+ break;
+
+ case R_CRIS_32:
+ /* CRIS keeps the relocation value in the r_addend field and
+ * should not use whats in *loc at all
+ */
+ *loc = v;
+ break;
+
+#elif defined(__H8300H__) || defined(__H8300S__)
+
+ case R_H8_DIR24R8:
+ loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
+ *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
+ break;
+ case R_H8_DIR24A8:
+ *loc += v;
+ break;
+ case R_H8_DIR32:
+ case R_H8_DIR32A16:
+ *loc += v;
+ break;
+ case R_H8_PCREL16:
+ v -= dot + 2;
+ if ((ElfW(Sword))v > 0x7fff ||
+ (ElfW(Sword))v < -(ElfW(Sword))0x8000)
+ ret = obj_reloc_overflow;
+ else
+ *(unsigned short *)loc = v;
+ break;
+ case R_H8_PCREL8:
+ v -= dot + 1;
+ if ((ElfW(Sword))v > 0x7f ||
+ (ElfW(Sword))v < -(ElfW(Sword))0x80)
+ ret = obj_reloc_overflow;
+ else
+ *(unsigned char *)loc = v;
+ break;
+
+#elif defined(__i386__)
+
+ case R_386_NONE:
+ break;
+
+ case R_386_32:
+ *loc += v;
+ break;
+
+ case R_386_PLT32:
+ case R_386_PC32:
+ case R_386_GOTOFF:
+ *loc += v - dot;
+ break;
+
+ case R_386_GLOB_DAT:
+ case R_386_JMP_SLOT:
+ *loc = v;
+ break;
+
+ case R_386_RELATIVE:
+ *loc += f->baseaddr;
+ break;
+
+ case R_386_GOTPC:
+ *loc += got - dot;
+ break;
+
+ case R_386_GOT32:
+ goto bb_use_got;
+ break;
+
+#elif defined(__microblaze__)
+ case R_MICROBLAZE_NONE:
+ case R_MICROBLAZE_64_NONE:
+ case R_MICROBLAZE_32_SYM_OP_SYM:
+ case R_MICROBLAZE_32_PCREL:
+ break;
+
+ case R_MICROBLAZE_64_PCREL: {
+ /* dot is the address of the current instruction.
+ * v is the target symbol address.
+ * So we need to extract the offset in the code,
+ * adding v, then subtrating the current address
+ * of this instruction.
+ * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
+ */
+
+ /* Get split offset stored in code */
+ unsigned int temp = (loc[0] & 0xFFFF) << 16 |
+ (loc[1] & 0xFFFF);
+
+ /* Adjust relative offset. -4 adjustment required
+ * because dot points to the IMM insn, but branch
+ * is computed relative to the branch instruction itself.
+ */
+ temp += v - dot - 4;
+
+ /* Store back into code */
+ loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
+ loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
+
+ break;
+ }
+
+ case R_MICROBLAZE_32:
+ *loc += v;
+ break;
+
+ case R_MICROBLAZE_64: {
+ /* Get split pointer stored in code */
+ unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
+ (loc[1] & 0xFFFF);
+
+ /* Add reloc offset */
+ temp1+=v;
+
+ /* Store back into code */
+ loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
+ loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
+
+ break;
+ }
+
+ case R_MICROBLAZE_32_PCREL_LO:
+ case R_MICROBLAZE_32_LO:
+ case R_MICROBLAZE_SRO32:
+ case R_MICROBLAZE_SRW32:
+ ret = obj_reloc_unhandled;
+ break;
+
+#elif defined(__mc68000__)
+
+ case R_68K_NONE:
+ break;
+
+ case R_68K_32:
+ *loc += v;
+ break;
+
+ case R_68K_8:
+ if (v > 0xff) {
+ ret = obj_reloc_overflow;
+ }
+ *(char *)loc = v;
+ break;
+
+ case R_68K_16:
+ if (v > 0xffff) {
+ ret = obj_reloc_overflow;
+ }
+ *(short *)loc = v;
+ break;
+
+ case R_68K_PC8:
+ v -= dot;
+ if ((ElfW(Sword))v > 0x7f
+ || (ElfW(Sword))v < -(ElfW(Sword))0x80
+ ) {
+ ret = obj_reloc_overflow;
+ }
+ *(char *)loc = v;
+ break;
+
+ case R_68K_PC16:
+ v -= dot;
+ if ((ElfW(Sword))v > 0x7fff
+ || (ElfW(Sword))v < -(ElfW(Sword))0x8000
+ ) {
+ ret = obj_reloc_overflow;
+ }
+ *(short *)loc = v;
+ break;
+
+ case R_68K_PC32:
+ *(int *)loc = v - dot;
+ break;
+
+ case R_68K_GLOB_DAT:
+ case R_68K_JMP_SLOT:
+ *loc = v;
+ break;
+
+ case R_68K_RELATIVE:
+ *(int *)loc += f->baseaddr;
+ break;
+
+ case R_68K_GOT32:
+ goto bb_use_got;
+
+# ifdef R_68K_GOTOFF
+ case R_68K_GOTOFF:
+ *loc += v - got;
+ break;
+# endif
+
+#elif defined(__mips__)
+
+ case R_MIPS_NONE:
+ break;
+
+ case R_MIPS_32:
+ *loc += v;
+ break;
+
+ case R_MIPS_26:
+ if (v % 4)
+ ret = obj_reloc_dangerous;
+ if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
+ ret = obj_reloc_overflow;
+ *loc =
+ (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
+ 0x03ffffff);
+ break;
+
+ case R_MIPS_HI16:
+ {
+ struct mips_hi16 *n;
+
+ /* We cannot relocate this one now because we don't know the value
+ of the carry we need to add. Save the information, and let LO16
+ do the actual relocation. */
+ n = xmalloc(sizeof *n);
+ n->addr = loc;
+ n->value = v;
+ n->next = ifile->mips_hi16_list;
+ ifile->mips_hi16_list = n;
+ break;
+ }
+
+ case R_MIPS_LO16:
+ {
+ unsigned long insnlo = *loc;
+ ElfW(Addr) val, vallo;
+
+ /* Sign extend the addend we extract from the lo insn. */
+ vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
+
+ if (ifile->mips_hi16_list != NULL) {
+ struct mips_hi16 *l;
+
+ l = ifile->mips_hi16_list;
+ while (l != NULL) {
+ struct mips_hi16 *next;
+ unsigned long insn;
+
+ /* Do the HI16 relocation. Note that we actually don't
+ need to know anything about the LO16 itself, except where
+ to find the low 16 bits of the addend needed by the LO16. */
+ insn = *l->addr;
+ val =
+ ((insn & 0xffff) << 16) +
+ vallo;
+ val += v;
+
+ /* Account for the sign extension that will happen in the
+ low bits. */
+ val =
+ ((val >> 16) +
+ ((val & 0x8000) !=
+ 0)) & 0xffff;
+
+ insn = (insn & ~0xffff) | val;
+ *l->addr = insn;
+
+ next = l->next;
+ free(l);
+ l = next;
+ }
+
+ ifile->mips_hi16_list = NULL;
+ }
+
+ /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
+ val = v + vallo;
+ insnlo = (insnlo & ~0xffff) | (val & 0xffff);
+ *loc = insnlo;
+ break;
+ }
+
+#elif defined(__nios2__)
+
+ case R_NIOS2_NONE:
+ break;
+
+ case R_NIOS2_BFD_RELOC_32:
+ *loc += v;
+ break;
+
+ case R_NIOS2_BFD_RELOC_16:
+ if (v > 0xffff) {
+ ret = obj_reloc_overflow;
+ }
+ *(short *)loc = v;
+ break;
+
+ case R_NIOS2_BFD_RELOC_8:
+ if (v > 0xff) {
+ ret = obj_reloc_overflow;
+ }
+ *(char *)loc = v;
+ break;
+
+ case R_NIOS2_S16:
+ {
+ Elf32_Addr word;
+
+ if ((Elf32_Sword)v > 0x7fff
+ || (Elf32_Sword)v < -(Elf32_Sword)0x8000
+ ) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+ (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_U16:
+ {
+ Elf32_Addr word;
+
+ if (v > 0xffff) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+ (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_PCREL16:
+ {
+ Elf32_Addr word;
+
+ v -= dot + 4;
+ if ((Elf32_Sword)v > 0x7fff
+ || (Elf32_Sword)v < -(Elf32_Sword)0x8000
+ ) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_GPREL:
+ {
+ Elf32_Addr word, gp;
+ /* get _gp */
+ gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
+ v -= gp;
+ if ((Elf32_Sword)v > 0x7fff
+ || (Elf32_Sword)v < -(Elf32_Sword)0x8000
+ ) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_CALL26:
+ if (v & 3)
+ ret = obj_reloc_dangerous;
+ if ((v >> 28) != (dot >> 28))
+ ret = obj_reloc_overflow;
+ *loc = (*loc & 0x3f) | ((v >> 2) << 6);
+ break;
+
+ case R_NIOS2_IMM5:
+ {
+ Elf32_Addr word;
+
+ if (v > 0x1f) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc & ~0x7c0;
+ *loc = word | ((v & 0x1f) << 6);
+ }
+ break;
+
+ case R_NIOS2_IMM6:
+ {
+ Elf32_Addr word;
+
+ if (v > 0x3f) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc & ~0xfc0;
+ *loc = word | ((v & 0x3f) << 6);
+ }
+ break;
+
+ case R_NIOS2_IMM8:
+ {
+ Elf32_Addr word;
+
+ if (v > 0xff) {
+ ret = obj_reloc_overflow;
+ }
+
+ word = *loc & ~0x3fc0;
+ *loc = word | ((v & 0xff) << 6);
+ }
+ break;
+
+ case R_NIOS2_HI16:
+ {
+ Elf32_Addr word;
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
+ (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_LO16:
+ {
+ Elf32_Addr word;
+
+ word = *loc;
+ *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
+ (word & 0x3f);
+ }
+ break;
+
+ case R_NIOS2_HIADJ16:
+ {
+ Elf32_Addr word1, word2;
+
+ word1 = *loc;
+ word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
+ *loc = ((((word1 >> 22) << 16) | word2) << 6) |
+ (word1 & 0x3f);
+ }
+ break;
+
+#elif defined(__powerpc64__)
+ /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
+
+#elif defined(__powerpc__)
+
+ case R_PPC_ADDR16_HA:
+ *(unsigned short *)loc = (v + 0x8000) >> 16;
+ break;
+
+ case R_PPC_ADDR16_HI:
+ *(unsigned short *)loc = v >> 16;
+ break;
+
+ case R_PPC_ADDR16_LO:
+ *(unsigned short *)loc = v;
+ break;
+
+ case R_PPC_REL24:
+ goto bb_use_plt;
+
+ case R_PPC_REL32:
+ *loc = v - dot;
+ break;
+
+ case R_PPC_ADDR32:
+ *loc = v;
+ break;
+
+#elif defined(__s390__)
+
+ case R_390_32:
+ *(unsigned int *) loc += v;
+ break;
+ case R_390_16:
+ *(unsigned short *) loc += v;
+ break;
+ case R_390_8:
+ *(unsigned char *) loc += v;
+ break;
+
+ case R_390_PC32:
+ *(unsigned int *) loc += v - dot;
+ break;
+ case R_390_PC16DBL:
+ *(unsigned short *) loc += (v - dot) >> 1;
+ break;
+ case R_390_PC16:
+ *(unsigned short *) loc += v - dot;
+ break;
+
+ case R_390_PLT32:
+ case R_390_PLT16DBL:
+ /* find the plt entry and initialize it. */
+ pe = (struct arch_single_entry *) &isym->pltent;
+ if (pe->inited == 0) {
+ ip = (unsigned long *)(ifile->plt->contents + pe->offset);
+ ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
+ ip[1] = 0x100607f1;
+ if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
+ ip[2] = v - 2;
+ else
+ ip[2] = v;
+ pe->inited = 1;
+ }
+
+ /* Insert relative distance to target. */
+ v = plt + pe->offset - dot;
+ if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
+ *(unsigned int *) loc = (unsigned int) v;
+ else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
+ *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
+ break;
+
+ case R_390_GLOB_DAT:
+ case R_390_JMP_SLOT:
+ *loc = v;
+ break;
+
+ case R_390_RELATIVE:
+ *loc += f->baseaddr;
+ break;
+
+ case R_390_GOTPC:
+ *(unsigned long *) loc += got - dot;
+ break;
+
+ case R_390_GOT12:
+ case R_390_GOT16:
+ case R_390_GOT32:
+ if (!isym->gotent.inited)
+ {
+ isym->gotent.inited = 1;
+ *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
+ }
+ if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
+ *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
+ else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
+ *(unsigned short *) loc += isym->gotent.offset;
+ else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
+ *(unsigned int *) loc += isym->gotent.offset;
+ break;
+
+# ifndef R_390_GOTOFF32
+# define R_390_GOTOFF32 R_390_GOTOFF
+# endif
+ case R_390_GOTOFF32:
+ *loc += v - got;
+ break;
+
+#elif defined(__sh__)
+
+ case R_SH_NONE:
+ break;
+
+ case R_SH_DIR32:
+ *loc += v;
+ break;
+
+ case R_SH_REL32:
+ *loc += v - dot;
+ break;
+
+ case R_SH_PLT32:
+ *loc = v - dot;
+ break;
+
+ case R_SH_GLOB_DAT:
+ case R_SH_JMP_SLOT:
+ *loc = v;
+ break;
+
+ case R_SH_RELATIVE:
+ *loc = f->baseaddr + rel->r_addend;
+ break;
+
+ case R_SH_GOTPC:
+ *loc = got - dot + rel->r_addend;
+ break;
+
+ case R_SH_GOT32:
+ goto bb_use_got;
+
+ case R_SH_GOTOFF:
+ *loc = v - got;
+ break;
+
+# if defined(__SH5__)
+ case R_SH_IMM_MEDLOW16:
+ case R_SH_IMM_LOW16:
+ {
+ ElfW(Addr) word;
+
+ if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
+ v >>= 16;
+
+ /*
+ * movi and shori have the format:
+ *
+ * | op | imm | reg | reserved |
+ * 31..26 25..10 9.. 4 3 .. 0
+ *
+ * so we simply mask and or in imm.
+ */
+ word = *loc & ~0x3fffc00;
+ word |= (v & 0xffff) << 10;
+
+ *loc = word;
+
+ break;
+ }
+
+ case R_SH_IMM_MEDLOW16_PCREL:
+ case R_SH_IMM_LOW16_PCREL:
+ {
+ ElfW(Addr) word;
+
+ word = *loc & ~0x3fffc00;
+
+ v -= dot;
+
+ if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
+ v >>= 16;
+
+ word |= (v & 0xffff) << 10;
+
+ *loc = word;
+
+ break;
+ }
+# endif /* __SH5__ */
+
+#elif defined(__v850e__)
+
+ case R_V850_NONE:
+ break;
+
+ case R_V850_32:
+ /* We write two shorts instead of a long because even
+ 32-bit insns only need half-word alignment, but
+ 32-bit data needs to be long-word aligned. */
+ v += ((unsigned short *)loc)[0];
+ v += ((unsigned short *)loc)[1] << 16;
+ ((unsigned short *)loc)[0] = v & 0xffff;
+ ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
+ break;
+
+ case R_V850_22_PCREL:
+ goto bb_use_plt;
+
+#elif defined(__x86_64__)
+
+ case R_X86_64_NONE:
+ break;
+
+ case R_X86_64_64:
+ *loc += v;
+ break;
+
+ case R_X86_64_32:
+ *(unsigned int *) loc += v;
+ if (v > 0xffffffff)
+ {
+ ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
+ /* error("Possibly is module compiled without -mcmodel=kernel!"); */
+ }
+ break;
+
+ case R_X86_64_32S:
+ *(signed int *) loc += v;
+ break;
+
+ case R_X86_64_16:
+ *(unsigned short *) loc += v;
+ break;
+
+ case R_X86_64_8:
+ *(unsigned char *) loc += v;
+ break;
+
+ case R_X86_64_PC32:
+ *(unsigned int *) loc += v - dot;
+ break;
+
+ case R_X86_64_PC16:
+ *(unsigned short *) loc += v - dot;
+ break;
+
+ case R_X86_64_PC8:
+ *(unsigned char *) loc += v - dot;
+ break;
+
+ case R_X86_64_GLOB_DAT:
+ case R_X86_64_JUMP_SLOT:
+ *loc = v;
+ break;
+
+ case R_X86_64_RELATIVE:
+ *loc += f->baseaddr;
+ break;
+
+ case R_X86_64_GOT32:
+ case R_X86_64_GOTPCREL:
+ goto bb_use_got;
+# if 0
+ if (!isym->gotent.reloc_done)
+ {
+ isym->gotent.reloc_done = 1;
+ *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
+ }
+ /* XXX are these really correct? */
+ if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
+ *(unsigned int *) loc += v + isym->gotent.offset;
+ else
+ *loc += isym->gotent.offset;
+ break;
+# endif
+
+#else
+# warning "no idea how to handle relocations on your arch"
+#endif
+
+ default:
+ printf("Warning: unhandled reloc %d\n",(int)ELF_R_TYPE(rel->r_info));
+ ret = obj_reloc_unhandled;
+ break;
+
+#if defined(USE_PLT_ENTRIES)
+
+bb_use_plt:
+
+ /* find the plt entry and initialize it if necessary */
+
+#if defined(USE_PLT_LIST)
+ for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
+ pe = pe->next;
+#else
+ pe = &isym->pltent;
+#endif
+
+ if (! pe->inited) {
+ ip = (unsigned long *) (ifile->plt->contents + pe->offset);
+
+ /* generate some machine code */
+
+#if defined(__arm__)
+ ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
+ ip[1] = v; /* sym@ */
+#endif
+#if defined(__powerpc__)
+ ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
+ ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
+ ip[2] = 0x7d6903a6; /* mtctr r11 */
+ ip[3] = 0x4e800420; /* bctr */
+#endif
+#if defined(__v850e__)
+ /* We have to trash a register, so we assume that any control
+ transfer more than 21-bits away must be a function call
+ (so we can use a call-clobbered register). */
+ ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
+ ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
+#endif
+ pe->inited = 1;
+ }
+
+ /* relative distance to target */
+ v -= dot;
+ /* if the target is too far away.... */
+#if defined(__arm__) || defined(__powerpc__)
+ if ((int)v < -0x02000000 || (int)v >= 0x02000000)
+#elif defined(__v850e__)
+ if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
+#endif
+ /* go via the plt */
+ v = plt + pe->offset - dot;
+
+#if defined(__v850e__)
+ if (v & 1)
+#else
+ if (v & 3)
+#endif
+ ret = obj_reloc_dangerous;
+
+ /* merge the offset into the instruction. */
+#if defined(__arm__)
+ /* Convert to words. */
+ v >>= 2;
+
+ *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
+#endif
+#if defined(__powerpc__)
+ *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
+#endif
+#if defined(__v850e__)
+ /* We write two shorts instead of a long because even 32-bit insns
+ only need half-word alignment, but the 32-bit data write needs
+ to be long-word aligned. */
+ ((unsigned short *)loc)[0] =
+ (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
+ | ((v >> 16) & 0x3f); /* offs high part */
+ ((unsigned short *)loc)[1] =
+ (v & 0xffff); /* offs low part */
+#endif
+ break;
+#endif /* USE_PLT_ENTRIES */
+
+#if defined(USE_GOT_ENTRIES)
+bb_use_got:
+
+ /* needs an entry in the .got: set it, once */
+ if (!isym->gotent.inited) {
+ isym->gotent.inited = 1;
+ *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
+ }
+ /* make the reloc with_respect_to_.got */
+#if defined(__sh__)
+ *loc += isym->gotent.offset + rel->r_addend;
+#elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
+ *loc += isym->gotent.offset;
+#endif
+ break;
+
+#endif /* USE_GOT_ENTRIES */
+ }
+
+ return ret;
+}
+
+
+#if defined(USE_LIST)
+
+static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
+ int offset, int size)
+{
+ struct arch_list_entry *pe;
+
+ for (pe = *list; pe != NULL; pe = pe->next) {
+ if (pe->addend == rel->r_addend) {
+ break;
+ }
+ }
+
+ if (pe == NULL) {
+ pe = xzalloc(sizeof(struct arch_list_entry));
+ pe->next = *list;
+ pe->addend = rel->r_addend;
+ pe->offset = offset;
+ /*pe->inited = 0;*/
+ *list = pe;
+ return size;
+ }
+ return 0;
+}
+
+#endif
+
+#if defined(USE_SINGLE)
+
+static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
+ int offset, int size)
+{
+ if (single->allocated == 0) {
+ single->allocated = 1;
+ single->offset = offset;
+ single->inited = 0;
+ return size;
+ }
+ return 0;
+}
+
+#endif
+
+#if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
+
+static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
+ int offset, int size)
+{
+ struct obj_section *myrelsec = obj_find_section(f, name);
+
+ if (offset == 0) {
+ offset += size;
+ }
+
+ if (myrelsec) {
+ obj_extend_section(myrelsec, offset);
+ } else {
+ myrelsec = obj_create_alloced_section(f, name,
+ size, offset);
+ }
+
+ return myrelsec;
+}
+
+#endif
+
+static void arch_create_got(struct obj_file *f)
+{
+#if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
+ struct arch_file *ifile = (struct arch_file *) f;
+ int i;
+#if defined(USE_GOT_ENTRIES)
+ int got_offset = 0, got_needed = 0, got_allocate;
+#endif
+#if defined(USE_PLT_ENTRIES)
+ int plt_offset = 0, plt_needed = 0, plt_allocate;
+#endif
+ struct obj_section *relsec, *symsec, *strsec;
+ ElfW(RelM) *rel, *relend;
+ ElfW(Sym) *symtab, *extsym;
+ const char *strtab, *name;
+ struct arch_symbol *intsym;
+
+ for (i = 0; i < f->header.e_shnum; ++i) {
+ relsec = f->sections[i];
+ if (relsec->header.sh_type != SHT_RELM)
+ continue;
+
+ symsec = f->sections[relsec->header.sh_link];
+ strsec = f->sections[symsec->header.sh_link];
+
+ rel = (ElfW(RelM) *) relsec->contents;
+ relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
+ symtab = (ElfW(Sym) *) symsec->contents;
+ strtab = (const char *) strsec->contents;
+
+ for (; rel < relend; ++rel) {
+ extsym = &symtab[ELF_R_SYM(rel->r_info)];
+
+#if defined(USE_GOT_ENTRIES)
+ got_allocate = 0;
+#endif
+#if defined(USE_PLT_ENTRIES)
+ plt_allocate = 0;
+#endif
+
+ switch (ELF_R_TYPE(rel->r_info)) {
+#if defined(__arm__)
+ case R_ARM_PC24:
+ case R_ARM_PLT32:
+ plt_allocate = 1;
+ break;
+
+ case R_ARM_GOTOFF:
+ case R_ARM_GOTPC:
+ got_needed = 1;
+ continue;
+
+ case R_ARM_GOT32:
+ got_allocate = 1;
+ break;
+
+#elif defined(__i386__)
+ case R_386_GOTPC:
+ case R_386_GOTOFF:
+ got_needed = 1;
+ continue;
+
+ case R_386_GOT32:
+ got_allocate = 1;
+ break;
+
+#elif defined(__powerpc__)
+ case R_PPC_REL24:
+ plt_allocate = 1;
+ break;
+
+#elif defined(__mc68000__)
+ case R_68K_GOT32:
+ got_allocate = 1;
+ break;
+
+#ifdef R_68K_GOTOFF
+ case R_68K_GOTOFF:
+ got_needed = 1;
+ continue;
+#endif
+
+#elif defined(__sh__)
+ case R_SH_GOT32:
+ got_allocate = 1;
+ break;
+
+ case R_SH_GOTPC:
+ case R_SH_GOTOFF:
+ got_needed = 1;
+ continue;
+
+#elif defined(__v850e__)
+ case R_V850_22_PCREL:
+ plt_needed = 1;
+ break;
+
+#endif
+ default:
+ continue;
+ }
+
+ if (extsym->st_name != 0) {
+ name = strtab + extsym->st_name;
+ } else {
+ name = f->sections[extsym->st_shndx]->name;
+ }
+ intsym = (struct arch_symbol *) obj_find_symbol(f, name);
+#if defined(USE_GOT_ENTRIES)
+ if (got_allocate) {
+ got_offset += arch_single_init(
+ /*rel,*/ &intsym->gotent,
+ got_offset, GOT_ENTRY_SIZE);
+
+ got_needed = 1;
+ }
+#endif
+#if defined(USE_PLT_ENTRIES)
+ if (plt_allocate) {
+#if defined(USE_PLT_LIST)
+ plt_offset += arch_list_add(
+ rel, &intsym->pltent,
+ plt_offset, PLT_ENTRY_SIZE);
+#else
+ plt_offset += arch_single_init(
+ /*rel,*/ &intsym->pltent,
+ plt_offset, PLT_ENTRY_SIZE);
+#endif
+ plt_needed = 1;
+ }
+#endif
+ }
+ }
+
+#if defined(USE_GOT_ENTRIES)
+ if (got_needed) {
+ ifile->got = arch_xsect_init(f, ".got", got_offset,
+ GOT_ENTRY_SIZE);
+ }
+#endif
+
+#if defined(USE_PLT_ENTRIES)
+ if (plt_needed) {
+ ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
+ PLT_ENTRY_SIZE);
+ }
+#endif
+
+#endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
+}
+
+/*======================================================================*/
+
+/* Standard ELF hash function. */
+static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
+{
+ unsigned long h = 0;
+ unsigned long g;
+ unsigned char ch;
+
+ while (n > 0) {
+ ch = *name++;
+ h = (h << 4) + ch;
+ g = (h & 0xf0000000);
+ if (g != 0) {
+ h ^= g >> 24;
+ h &= ~g;
+ }
+ n--;
+ }
+ return h;
+}
+
+static unsigned long obj_elf_hash(const char *name)
+{
+ return obj_elf_hash_n(name, strlen(name));
+}
+
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+/* String comparison for non-co-versioned kernel and module. */
+
+static int ncv_strcmp(const char *a, const char *b)
+{
+ size_t alen = strlen(a), blen = strlen(b);
+
+ if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
+ return strncmp(a, b, alen);
+ else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
+ return strncmp(a, b, blen);
+ else
+ return strcmp(a, b);
+}
+
+/* String hashing for non-co-versioned kernel and module. Here
+ we are simply forced to drop the crc from the hash. */
+
+static unsigned long ncv_symbol_hash(const char *str)
+{
+ size_t len = strlen(str);
+ if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
+ len -= 10;
+ return obj_elf_hash_n(str, len);
+}
+
+static void
+obj_set_symbol_compare(struct obj_file *f,
+ int (*cmp) (const char *, const char *),
+ unsigned long (*hash) (const char *))
+{
+ if (cmp)
+ f->symbol_cmp = cmp;
+ if (hash) {
+ struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
+ int i;
+
+ f->symbol_hash = hash;
+
+ memcpy(tmptab, f->symtab, sizeof(tmptab));
+ memset(f->symtab, 0, sizeof(f->symtab));
+
+ for (i = 0; i < HASH_BUCKETS; ++i)
+ for (sym = tmptab[i]; sym; sym = next) {
+ unsigned long h = hash(sym->name) % HASH_BUCKETS;
+ next = sym->next;
+ sym->next = f->symtab[h];
+ f->symtab[h] = sym;
+ }
+ }
+}
+
+#endif /* FEATURE_INSMOD_VERSION_CHECKING */
+
+static struct obj_symbol *
+obj_add_symbol(struct obj_file *f, const char *name,
+ unsigned long symidx, int info,
+ int secidx, ElfW(Addr) value,
+ unsigned long size)
+{
+ struct obj_symbol *sym;
+ unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
+ int n_type = ELF_ST_TYPE(info);
+ int n_binding = ELF_ST_BIND(info);
+
+ for (sym = f->symtab[hash]; sym; sym = sym->next) {
+ if (f->symbol_cmp(sym->name, name) == 0) {
+ int o_secidx = sym->secidx;
+ int o_info = sym->info;
+ int o_type = ELF_ST_TYPE(o_info);
+ int o_binding = ELF_ST_BIND(o_info);
+
+ /* A redefinition! Is it legal? */
+
+ if (secidx == SHN_UNDEF)
+ return sym;
+ else if (o_secidx == SHN_UNDEF)
+ goto found;
+ else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
+ /* Cope with local and global symbols of the same name
+ in the same object file, as might have been created
+ by ld -r. The only reason locals are now seen at this
+ level at all is so that we can do semi-sensible things
+ with parameters. */
+
+ struct obj_symbol *nsym, **p;
+
+ nsym = arch_new_symbol();
+ nsym->next = sym->next;
+ nsym->ksymidx = -1;
+
+ /* Excise the old (local) symbol from the hash chain. */
+ for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
+ continue;
+ *p = sym = nsym;
+ goto found;
+ } else if (n_binding == STB_LOCAL) {
+ /* Another symbol of the same name has already been defined.
+ Just add this to the local table. */
+ sym = arch_new_symbol();
+ sym->next = NULL;
+ sym->ksymidx = -1;
+ f->local_symtab[symidx] = sym;
+ goto found;
+ } else if (n_binding == STB_WEAK)
+ return sym;
+ else if (o_binding == STB_WEAK)
+ goto found;
+ /* Don't unify COMMON symbols with object types the programmer
+ doesn't expect. */
+ else if (secidx == SHN_COMMON
+ && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
+ return sym;
+ else if (o_secidx == SHN_COMMON
+ && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
+ goto found;
+ else {
+ /* Don't report an error if the symbol is coming from
+ the kernel or some external module. */
+ if (secidx <= SHN_HIRESERVE)
+ bb_error_msg("%s multiply defined", name);
+ return sym;
+ }
+ }
+ }
+
+ /* Completely new symbol. */
+ sym = arch_new_symbol();
+ sym->next = f->symtab[hash];
+ f->symtab[hash] = sym;
+ sym->ksymidx = -1;
+ if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
+ if (symidx >= f->local_symtab_size)
+ bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
+ name, (long) symidx, (long) f->local_symtab_size);
+ else
+ f->local_symtab[symidx] = sym;
+ }
+
+found:
+ sym->name = name;
+ sym->value = value;
+ sym->size = size;
+ sym->secidx = secidx;
+ sym->info = info;
+
+ return sym;
+}
+
+static struct obj_symbol *
+obj_find_symbol(struct obj_file *f, const char *name)
+{
+ struct obj_symbol *sym;
+ unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
+
+ for (sym = f->symtab[hash]; sym; sym = sym->next)
+ if (f->symbol_cmp(sym->name, name) == 0)
+ return sym;
+ return NULL;
+}
+
+static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
+{
+ if (sym) {
+ if (sym->secidx >= SHN_LORESERVE)
+ return sym->value;
+ return sym->value + f->sections[sym->secidx]->header.sh_addr;
+ }
+ /* As a special case, a NULL sym has value zero. */
+ return 0;
+}
+
+static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
+{
+ int i, n = f->header.e_shnum;
+
+ for (i = 0; i < n; ++i)
+ if (strcmp(f->sections[i]->name, name) == 0)
+ return f->sections[i];
+ return NULL;
+}
+
+static int obj_load_order_prio(struct obj_section *a)
+{
+ unsigned long af, ac;
+
+ af = a->header.sh_flags;
+
+ ac = 0;
+ if (a->name[0] != '.' || strlen(a->name) != 10
+ || strcmp(a->name + 5, ".init") != 0
+ ) {
+ ac |= 32;
+ }
+ if (af & SHF_ALLOC)
+ ac |= 16;
+ if (!(af & SHF_WRITE))
+ ac |= 8;
+ if (af & SHF_EXECINSTR)
+ ac |= 4;
+ if (a->header.sh_type != SHT_NOBITS)
+ ac |= 2;
+
+ return ac;
+}
+
+static void
+obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
+{
+ struct obj_section **p;
+ int prio = obj_load_order_prio(sec);
+ for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
+ if (obj_load_order_prio(*p) < prio)
+ break;
+ sec->load_next = *p;
+ *p = sec;
+}
+
+static struct obj_section *helper_create_alloced_section(struct obj_file *f,
+ const char *name,
+ unsigned long align,
+ unsigned long size)
+{
+ int newidx = f->header.e_shnum++;
+ struct obj_section *sec;
+
+ f->sections = xrealloc_vector(f->sections, 2, newidx);
+ f->sections[newidx] = sec = arch_new_section();
+
+ sec->header.sh_type = SHT_PROGBITS;
+ sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
+ sec->header.sh_size = size;
+ sec->header.sh_addralign = align;
+ sec->name = name;
+ sec->idx = newidx;
+ if (size)
+ sec->contents = xzalloc(size);
+
+ return sec;
+}
+
+static struct obj_section *obj_create_alloced_section(struct obj_file *f,
+ const char *name,
+ unsigned long align,
+ unsigned long size)
+{
+ struct obj_section *sec;
+
+ sec = helper_create_alloced_section(f, name, align, size);
+ obj_insert_section_load_order(f, sec);
+ return sec;
+}
+
+static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
+ const char *name,
+ unsigned long align,
+ unsigned long size)
+{
+ struct obj_section *sec;
+
+ sec = helper_create_alloced_section(f, name, align, size);
+ sec->load_next = f->load_order;
+ f->load_order = sec;
+ if (f->load_order_search_start == &f->load_order)
+ f->load_order_search_start = &sec->load_next;
+
+ return sec;
+}
+
+static void *obj_extend_section(struct obj_section *sec, unsigned long more)
+{
+ unsigned long oldsize = sec->header.sh_size;
+ if (more) {
+ sec->header.sh_size += more;
+ sec->contents = xrealloc(sec->contents, sec->header.sh_size);
+ }
+ return sec->contents + oldsize;
+}
+
+
+/* Conditionally add the symbols from the given symbol set to the
+ new module. */
+
+static int add_symbols_from(struct obj_file *f,
+ int idx,
+ struct new_module_symbol *syms,
+ size_t nsyms)
+{
+ struct new_module_symbol *s;
+ size_t i;
+ int used = 0;
+#ifdef SYMBOL_PREFIX
+ char *name_buf = NULL;
+ size_t name_alloced_size = 0;
+#endif
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
+ int gpl;
+
+ gpl = obj_gpl_license(f, NULL) == 0;
+#endif
+ for (i = 0, s = syms; i < nsyms; ++i, ++s) {
+ /* Only add symbols that are already marked external.
+ If we override locals we may cause problems for
+ argument initialization. We will also create a false
+ dependency on the module. */
+ struct obj_symbol *sym;
+ char *name;
+
+ /* GPL licensed modules can use symbols exported with
+ * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
+ * exported names. Non-GPL modules never see any GPLONLY_
+ * symbols so they cannot fudge it by adding the prefix on
+ * their references.
+ */
+ if (strncmp((char *)s->name, "GPLONLY_", 8) == 0) {
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
+ if (gpl)
+ s->name += 8;
+ else
+#endif
+ continue;
+ }
+ name = (char *)s->name;
+
+#ifdef SYMBOL_PREFIX
+ /* Prepend SYMBOL_PREFIX to the symbol's name (the
+ kernel exports `C names', but module object files
+ reference `linker names'). */
+ size_t extra = sizeof SYMBOL_PREFIX;
+ size_t name_size = strlen(name) + extra;
+ if (name_size > name_alloced_size) {
+ name_alloced_size = name_size * 2;
+ name_buf = alloca(name_alloced_size);
+ }
+ strcpy(name_buf, SYMBOL_PREFIX);
+ strcpy(name_buf + extra - 1, name);
+ name = name_buf;
+#endif
+
+ sym = obj_find_symbol(f, name);
+ if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
+#ifdef SYMBOL_PREFIX
+ /* Put NAME_BUF into more permanent storage. */
+ name = xmalloc(name_size);
+ strcpy(name, name_buf);
+#endif
+ sym = obj_add_symbol(f, name, -1,
+ ELF_ST_INFO(STB_GLOBAL,
+ STT_NOTYPE),
+ idx, s->value, 0);
+ /* Did our symbol just get installed? If so, mark the
+ module as "used". */
+ if (sym->secidx == idx)
+ used = 1;
+ }
+ }
+
+ return used;
+}
+
+static void add_kernel_symbols(struct obj_file *f)
+{
+ struct external_module *m;
+ int i, nused = 0;
+
+ /* Add module symbols first. */
+
+ for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
+ if (m->nsyms
+ && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
+ ) {
+ m->used = 1;
+ ++nused;
+ }
+ }
+
+ n_ext_modules_used = nused;
+
+ /* And finally the symbols from the kernel proper. */
+
+ if (nksyms)
+ add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
+}
+
+static char *get_modinfo_value(struct obj_file *f, const char *key)
+{
+ struct obj_section *sec;
+ char *p, *v, *n, *ep;
+ size_t klen = strlen(key);
+
+ sec = obj_find_section(f, ".modinfo");
+ if (sec == NULL)
+ return NULL;
+ p = sec->contents;
+ ep = p + sec->header.sh_size;
+ while (p < ep) {
+ v = strchr(p, '=');
+ n = strchr(p, '\0');
+ if (v) {
+ if (p + klen == v && strncmp(p, key, klen) == 0)
+ return v + 1;
+ } else {
+ if (p + klen == n && strcmp(p, key) == 0)
+ return n;
+ }
+ p = n + 1;
+ }
+
+ return NULL;
+}
+
+
+/*======================================================================*/
+/* Functions relating to module loading after 2.1.18. */
+
+/* From Linux-2.6 sources */
+/* You can use " around spaces, but can't escape ". */
+/* Hyphens and underscores equivalent in parameter names. */
+static char *next_arg(char *args, char **param, char **val)
+{
+ unsigned int i, equals = 0;
+ int in_quote = 0, quoted = 0;
+ char *next;
+
+ if (*args == '"') {
+ args++;
+ in_quote = 1;
+ quoted = 1;
+ }
+
+ for (i = 0; args[i]; i++) {
+ if (args[i] == ' ' && !in_quote)
+ break;
+ if (equals == 0) {
+ if (args[i] == '=')
+ equals = i;
+ }
+ if (args[i] == '"')
+ in_quote = !in_quote;
+ }
+
+ *param = args;
+ if (!equals)
+ *val = NULL;
+ else {
+ args[equals] = '\0';
+ *val = args + equals + 1;
+
+ /* Don't include quotes in value. */
+ if (**val == '"') {
+ (*val)++;
+ if (args[i-1] == '"')
+ args[i-1] = '\0';
+ }
+ if (quoted && args[i-1] == '"')
+ args[i-1] = '\0';
+ }
+
+ if (args[i]) {
+ args[i] = '\0';
+ next = args + i + 1;
+ } else
+ next = args + i;
+
+ /* Chew up trailing spaces. */
+ return skip_whitespace(next);
+}
+
+static void
+new_process_module_arguments(struct obj_file *f, const char *options)
+{
+ char *xoptions, *pos;
+ char *param, *val;
+
+ xoptions = pos = xstrdup(skip_whitespace(options));
+ while (*pos) {
+ unsigned long charssize = 0;
+ char *tmp, *contents, *loc, *pinfo, *p;
+ struct obj_symbol *sym;
+ int min, max, n, len;
+
+ pos = next_arg(pos, &param, &val);
+
+ tmp = xasprintf("parm_%s", param);
+ pinfo = get_modinfo_value(f, tmp);
+ free(tmp);
+ if (pinfo == NULL)
+ bb_error_msg_and_die("invalid parameter %s", param);
+
+#ifdef SYMBOL_PREFIX
+ tmp = xasprintf(SYMBOL_PREFIX "%s", param);
+ sym = obj_find_symbol(f, tmp);
+ free(tmp);
+#else
+ sym = obj_find_symbol(f, param);
+#endif
+
+ /* Also check that the parameter was not resolved from the kernel. */
+ if (sym == NULL || sym->secidx > SHN_HIRESERVE)
+ bb_error_msg_and_die("symbol for parameter %s not found", param);
+
+ /* Number of parameters */
+ if (isdigit(*pinfo)) {
+ min = strtoul(pinfo, &pinfo, 10);
+ if (*pinfo == '-')
+ max = strtoul(pinfo + 1, &pinfo, 10);
+ else
+ max = min;
+ } else
+ min = max = 1;
+
+ contents = f->sections[sym->secidx]->contents;
+ loc = contents + sym->value;
+
+ if (*pinfo == 'c') {
+ if (!isdigit(*(pinfo + 1))) {
+ bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
+ " the maximum size", param);
+ }
+ charssize = strtoul(pinfo + 1, (char **) NULL, 10);
+ }
+
+ if (val == NULL) {
+ if (*pinfo != 'b')
+ bb_error_msg_and_die("argument expected for parameter %s", param);
+ val = (char *) "1";
+ }
+
+ /* Parse parameter values */
+ n = 0;
+ p = val;
+ while (*p != 0) {
+ if (++n > max)
+ bb_error_msg_and_die("too many values for %s (max %d)", param, max);
+
+ switch (*pinfo) {
+ case 's':
+ len = strcspn(p, ",");
+ p[len] = 0;
+ obj_string_patch(f, sym->secidx,
+ loc - contents, p);
+ loc += tgt_sizeof_char_p;
+ p += len;
+ break;
+ case 'c':
+ len = strcspn(p, ",");
+ p[len] = 0;
+ if (len >= charssize)
+ bb_error_msg_and_die("string too long for %s (max %ld)", param,
+ charssize - 1);
+ strcpy((char *) loc, p);
+ loc += charssize;
+ p += len;
+ break;
+ case 'b':
+ *loc++ = strtoul(p, &p, 0);
+ break;
+ case 'h':
+ *(short *) loc = strtoul(p, &p, 0);
+ loc += tgt_sizeof_short;
+ break;
+ case 'i':
+ *(int *) loc = strtoul(p, &p, 0);
+ loc += tgt_sizeof_int;
+ break;
+ case 'l':
+ *(long *) loc = strtoul(p, &p, 0);
+ loc += tgt_sizeof_long;
+ break;
+ default:
+ bb_error_msg_and_die("unknown parameter type '%c' for %s",
+ *pinfo, param);
+ }
+
+ p = skip_whitespace(p);
+ if (*p != ',')
+ break;
+ p = skip_whitespace(p + 1);
+ }
+
+ if (n < min)
+ bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
+ if (*p != '\0')
+ bb_error_msg_and_die("invalid argument syntax for %s", param);
+ }
+
+ free(xoptions);
+}
+
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+static int new_is_module_checksummed(struct obj_file *f)
+{
+ const char *p = get_modinfo_value(f, "using_checksums");
+ if (p)
+ return xatoi(p);
+ return 0;
+}
+
+/* Get the module's kernel version in the canonical integer form. */
+
+static int
+new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
+{
+ char *p, *q;
+ int a, b, c;
+
+ p = get_modinfo_value(f, "kernel_version");
+ if (p == NULL)
+ return -1;
+ safe_strncpy(str, p, STRVERSIONLEN);
+
+ a = strtoul(p, &p, 10);
+ if (*p != '.')
+ return -1;
+ b = strtoul(p + 1, &p, 10);
+ if (*p != '.')
+ return -1;
+ c = strtoul(p + 1, &q, 10);
+ if (p + 1 == q)
+ return -1;
+
+ return a << 16 | b << 8 | c;
+}
+
+#endif /* FEATURE_INSMOD_VERSION_CHECKING */
+
+
+/* Fetch the loaded modules, and all currently exported symbols. */
+
+static void new_get_kernel_symbols(void)
+{
+ char *module_names, *mn;
+ struct external_module *modules, *m;
+ struct new_module_symbol *syms, *s;
+ size_t ret, bufsize, nmod, nsyms, i, j;
+
+ /* Collect the loaded modules. */
+
+ bufsize = 256;
+ module_names = xmalloc(bufsize);
+
+ retry_modules_load:
+ if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
+ if (errno == ENOSPC && bufsize < ret) {
+ bufsize = ret;
+ module_names = xrealloc(module_names, bufsize);
+ goto retry_modules_load;
+ }
+ bb_perror_msg_and_die("QM_MODULES");
+ }
+
+ n_ext_modules = nmod = ret;
+
+ /* Collect the modules' symbols. */
+
+ if (nmod) {
+ ext_modules = modules = xzalloc(nmod * sizeof(*modules));
+ for (i = 0, mn = module_names, m = modules;
+ i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
+ struct new_module_info info;
+
+ if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
+ if (errno == ENOENT) {
+ /* The module was removed out from underneath us. */
+ continue;
+ }
+ bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
+ }
+
+ bufsize = 1024;
+ syms = xmalloc(bufsize);
+ retry_mod_sym_load:
+ if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
+ switch (errno) {
+ case ENOSPC:
+ bufsize = ret;
+ syms = xrealloc(syms, bufsize);
+ goto retry_mod_sym_load;
+ case ENOENT:
+ /* The module was removed out from underneath us. */
+ continue;
+ default:
+ bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
+ }
+ }
+ nsyms = ret;
+
+ m->name = mn;
+ m->addr = info.addr;
+ m->nsyms = nsyms;
+ m->syms = syms;
+
+ for (j = 0, s = syms; j < nsyms; ++j, ++s) {
+ s->name += (unsigned long) syms;
+ }
+ }
+ }
+
+ /* Collect the kernel's symbols. */
+
+ bufsize = 16 * 1024;
+ syms = xmalloc(bufsize);
+ retry_kern_sym_load:
+ if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
+ if (errno == ENOSPC && bufsize < ret) {
+ bufsize = ret;
+ syms = xrealloc(syms, bufsize);
+ goto retry_kern_sym_load;
+ }
+ bb_perror_msg_and_die("kernel: QM_SYMBOLS");
+ }
+ nksyms = nsyms = ret;
+ ksyms = syms;
+
+ for (j = 0, s = syms; j < nsyms; ++j, ++s) {
+ s->name += (unsigned long) syms;
+ }
+}
+
+
+/* Return the kernel symbol checksum version, or zero if not used. */
+
+static int new_is_kernel_checksummed(void)
+{
+ struct new_module_symbol *s;
+ size_t i;
+
+ /* Using_Versions is not the first symbol, but it should be in there. */
+
+ for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
+ if (strcmp((char *) s->name, "Using_Versions") == 0)
+ return s->value;
+
+ return 0;
+}
+
+
+static void new_create_this_module(struct obj_file *f, const char *m_name)
+{
+ struct obj_section *sec;
+
+ sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
+ sizeof(struct new_module));
+ /* done by obj_create_alloced_section_first: */
+ /*memset(sec->contents, 0, sizeof(struct new_module));*/
+
+ obj_add_symbol(f, SPFX "__this_module", -1,
+ ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
+ sizeof(struct new_module));
+
+ obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
+ m_name);
+}
+
+#if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
+/* add an entry to the __ksymtab section, creating it if necessary */
+static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
+{
+ struct obj_section *sec;
+ ElfW(Addr) ofs;
+
+ /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
+ * If __ksymtab is defined but not marked alloc, x out the first character
+ * (no obj_delete routine) and create a new __ksymtab with the correct
+ * characteristics.
+ */
+ sec = obj_find_section(f, "__ksymtab");
+ if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
+ *((char *)(sec->name)) = 'x'; /* override const */
+ sec = NULL;
+ }
+ if (!sec)
+ sec = obj_create_alloced_section(f, "__ksymtab",
+ tgt_sizeof_void_p, 0);
+ if (!sec)
+ return;
+ sec->header.sh_flags |= SHF_ALLOC;
+ /* Empty section might be byte-aligned */
+ sec->header.sh_addralign = tgt_sizeof_void_p;
+ ofs = sec->header.sh_size;
+ obj_symbol_patch(f, sec->idx, ofs, sym);
+ obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
+ obj_extend_section(sec, 2 * tgt_sizeof_char_p);
+}
+#endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
+
+static int new_create_module_ksymtab(struct obj_file *f)
+{
+ struct obj_section *sec;
+ int i;
+
+ /* We must always add the module references. */
+
+ if (n_ext_modules_used) {
+ struct new_module_ref *dep;
+ struct obj_symbol *tm;
+
+ sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
+ (sizeof(struct new_module_ref)
+ * n_ext_modules_used));
+ if (!sec)
+ return 0;
+
+ tm = obj_find_symbol(f, SPFX "__this_module");
+ dep = (struct new_module_ref *) sec->contents;
+ for (i = 0; i < n_ext_modules; ++i)
+ if (ext_modules[i].used) {
+ dep->dep = ext_modules[i].addr;
+ obj_symbol_patch(f, sec->idx,
+ (char *) &dep->ref - sec->contents, tm);
+ dep->next_ref = 0;
+ ++dep;
+ }
+ }
+
+ if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
+ size_t nsyms;
+ int *loaded;
+
+ sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
+
+ /* We don't want to export symbols residing in sections that
+ aren't loaded. There are a number of these created so that
+ we make sure certain module options don't appear twice. */
+ i = f->header.e_shnum;
+ loaded = alloca(sizeof(int) * i);
+ while (--i >= 0)
+ loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
+
+ for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
+ struct obj_symbol *sym;
+ for (sym = f->symtab[i]; sym; sym = sym->next) {
+ if (ELF_ST_BIND(sym->info) != STB_LOCAL
+ && sym->secidx <= SHN_HIRESERVE
+ && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
+ ) {
+ ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
+
+ obj_symbol_patch(f, sec->idx, ofs, sym);
+ obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
+ sym->name);
+ nsyms++;
+ }
+ }
+ }
+
+ obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
+ }
+
+ return 1;
+}
+
+
+static int
+new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
+{
+ struct new_module *module;
+ struct obj_section *sec;
+ void *image;
+ int ret;
+ tgt_long m_addr;
+
+ sec = obj_find_section(f, ".this");
+ if (!sec || !sec->contents) {
+ bb_perror_msg_and_die("corrupt module %s?", m_name);
+ }
+ module = (struct new_module *) sec->contents;
+ m_addr = sec->header.sh_addr;
+
+ module->size_of_struct = sizeof(*module);
+ module->size = m_size;
+ module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
+
+ sec = obj_find_section(f, "__ksymtab");
+ if (sec && sec->header.sh_size) {
+ module->syms = sec->header.sh_addr;
+ module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
+ }
+
+ if (n_ext_modules_used) {
+ sec = obj_find_section(f, ".kmodtab");
+ module->deps = sec->header.sh_addr;
+ module->ndeps = n_ext_modules_used;
+ }
+
+ module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
+ module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
+
+ sec = obj_find_section(f, "__ex_table");
+ if (sec) {
+ module->ex_table_start = sec->header.sh_addr;
+ module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
+ }
+
+ sec = obj_find_section(f, ".text.init");
+ if (sec) {
+ module->runsize = sec->header.sh_addr - m_addr;
+ }
+ sec = obj_find_section(f, ".data.init");
+ if (sec) {
+ if (!module->runsize
+ || module->runsize > sec->header.sh_addr - m_addr
+ ) {
+ module->runsize = sec->header.sh_addr - m_addr;
+ }
+ }
+ sec = obj_find_section(f, ARCHDATA_SEC_NAME);
+ if (sec && sec->header.sh_size) {
+ module->archdata_start = (void*)sec->header.sh_addr;
+ module->archdata_end = module->archdata_start + sec->header.sh_size;
+ }
+ sec = obj_find_section(f, KALLSYMS_SEC_NAME);
+ if (sec && sec->header.sh_size) {
+ module->kallsyms_start = (void*)sec->header.sh_addr;
+ module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
+ }
+
+ /* Whew! All of the initialization is complete. Collect the final
+ module image and give it to the kernel. */
+
+ image = xmalloc(m_size);
+ obj_create_image(f, image);
+
+ ret = init_module(m_name, (struct new_module *) image);
+ if (ret)
+ bb_perror_msg("init_module: %s", m_name);
+
+ free(image);
+
+ return ret == 0;
+}
+
+
+/*======================================================================*/
+
+static void
+obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+ const char *string)
+{
+ struct obj_string_patch *p;
+ struct obj_section *strsec;
+ size_t len = strlen(string) + 1;
+ char *loc;
+
+ p = xzalloc(sizeof(*p));
+ p->next = f->string_patches;
+ p->reloc_secidx = secidx;
+ p->reloc_offset = offset;
+ f->string_patches = p;
+
+ strsec = obj_find_section(f, ".kstrtab");
+ if (strsec == NULL) {
+ strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
+ /*p->string_offset = 0;*/
+ loc = strsec->contents;
+ } else {
+ p->string_offset = strsec->header.sh_size;
+ loc = obj_extend_section(strsec, len);
+ }
+ memcpy(loc, string, len);
+}
+
+static void
+obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
+ struct obj_symbol *sym)
+{
+ struct obj_symbol_patch *p;
+
+ p = xmalloc(sizeof(*p));
+ p->next = f->symbol_patches;
+ p->reloc_secidx = secidx;
+ p->reloc_offset = offset;
+ p->sym = sym;
+ f->symbol_patches = p;
+}
+
+static void obj_check_undefineds(struct obj_file *f)
+{
+ unsigned i;
+
+ for (i = 0; i < HASH_BUCKETS; ++i) {
+ struct obj_symbol *sym;
+ for (sym = f->symtab[i]; sym; sym = sym->next) {
+ if (sym->secidx == SHN_UNDEF) {
+ if (ELF_ST_BIND(sym->info) == STB_WEAK) {
+ sym->secidx = SHN_ABS;
+ sym->value = 0;
+ } else {
+ if (!flag_quiet)
+ bb_error_msg_and_die("unresolved symbol %s", sym->name);
+ }
+ }
+ }
+ }
+}
+
+static void obj_allocate_commons(struct obj_file *f)
+{
+ struct common_entry {
+ struct common_entry *next;
+ struct obj_symbol *sym;
+ } *common_head = NULL;
+
+ unsigned long i;
+
+ for (i = 0; i < HASH_BUCKETS; ++i) {
+ struct obj_symbol *sym;
+ for (sym = f->symtab[i]; sym; sym = sym->next) {
+ if (sym->secidx == SHN_COMMON) {
+ /* Collect all COMMON symbols and sort them by size so as to
+ minimize space wasted by alignment requirements. */
+ struct common_entry **p, *n;
+ for (p = &common_head; *p; p = &(*p)->next)
+ if (sym->size <= (*p)->sym->size)
+ break;
+ n = alloca(sizeof(*n));
+ n->next = *p;
+ n->sym = sym;
+ *p = n;
+ }
+ }
+ }
+
+ for (i = 1; i < f->local_symtab_size; ++i) {
+ struct obj_symbol *sym = f->local_symtab[i];
+ if (sym && sym->secidx == SHN_COMMON) {
+ struct common_entry **p, *n;
+ for (p = &common_head; *p; p = &(*p)->next) {
+ if (sym == (*p)->sym)
+ break;
+ if (sym->size < (*p)->sym->size) {
+ n = alloca(sizeof(*n));
+ n->next = *p;
+ n->sym = sym;
+ *p = n;
+ break;
+ }
+ }
+ }
+ }
+
+ if (common_head) {
+ /* Find the bss section. */
+ for (i = 0; i < f->header.e_shnum; ++i)
+ if (f->sections[i]->header.sh_type == SHT_NOBITS)
+ break;
+
+ /* If for some reason there hadn't been one, create one. */
+ if (i == f->header.e_shnum) {
+ struct obj_section *sec;
+
+ f->header.e_shnum++;
+ f->sections = xrealloc_vector(f->sections, 2, i);
+ f->sections[i] = sec = arch_new_section();
+
+ sec->header.sh_type = SHT_PROGBITS;
+ sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
+ sec->name = ".bss";
+ sec->idx = i;
+ }
+
+ /* Allocate the COMMONS. */
+ {
+ ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
+ ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
+ struct common_entry *c;
+
+ for (c = common_head; c; c = c->next) {
+ ElfW(Addr) align = c->sym->value;
+
+ if (align > max_align)
+ max_align = align;
+ if (bss_size & (align - 1))
+ bss_size = (bss_size | (align - 1)) + 1;
+
+ c->sym->secidx = i;
+ c->sym->value = bss_size;
+
+ bss_size += c->sym->size;
+ }
+
+ f->sections[i]->header.sh_size = bss_size;
+ f->sections[i]->header.sh_addralign = max_align;
+ }
+ }
+
+ /* For the sake of patch relocation and parameter initialization,
+ allocate zeroed data for NOBITS sections now. Note that after
+ this we cannot assume NOBITS are really empty. */
+ for (i = 0; i < f->header.e_shnum; ++i) {
+ struct obj_section *s = f->sections[i];
+ if (s->header.sh_type == SHT_NOBITS) {
+ s->contents = NULL;
+ if (s->header.sh_size != 0)
+ s->contents = xzalloc(s->header.sh_size);
+ s->header.sh_type = SHT_PROGBITS;
+ }
+ }
+}
+
+static unsigned long obj_load_size(struct obj_file *f)
+{
+ unsigned long dot = 0;
+ struct obj_section *sec;
+
+ /* Finalize the positions of the sections relative to one another. */
+
+ for (sec = f->load_order; sec; sec = sec->load_next) {
+ ElfW(Addr) align;
+
+ align = sec->header.sh_addralign;
+ if (align && (dot & (align - 1)))
+ dot = (dot | (align - 1)) + 1;
+
+ sec->header.sh_addr = dot;
+ dot += sec->header.sh_size;
+ }
+
+ return dot;
+}
+
+static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
+{
+ int i, n = f->header.e_shnum;
+ int ret = 1;
+
+ /* Finalize the addresses of the sections. */
+
+ f->baseaddr = base;
+ for (i = 0; i < n; ++i)
+ f->sections[i]->header.sh_addr += base;
+
+ /* And iterate over all of the relocations. */
+
+ for (i = 0; i < n; ++i) {
+ struct obj_section *relsec, *symsec, *targsec, *strsec;
+ ElfW(RelM) * rel, *relend;
+ ElfW(Sym) * symtab;
+ const char *strtab;
+
+ relsec = f->sections[i];
+ if (relsec->header.sh_type != SHT_RELM)
+ continue;
+
+ symsec = f->sections[relsec->header.sh_link];
+ targsec = f->sections[relsec->header.sh_info];
+ strsec = f->sections[symsec->header.sh_link];
+
+ rel = (ElfW(RelM) *) relsec->contents;
+ relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
+ symtab = (ElfW(Sym) *) symsec->contents;
+ strtab = (const char *) strsec->contents;
+
+ for (; rel < relend; ++rel) {
+ ElfW(Addr) value = 0;
+ struct obj_symbol *intsym = NULL;
+ unsigned long symndx;
+ ElfW(Sym) *extsym = NULL;
+ const char *errmsg;
+
+ /* Attempt to find a value to use for this relocation. */
+
+ symndx = ELF_R_SYM(rel->r_info);
+ if (symndx) {
+ /* Note we've already checked for undefined symbols. */
+
+ extsym = &symtab[symndx];
+ if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
+ /* Local symbols we look up in the local table to be sure
+ we get the one that is really intended. */
+ intsym = f->local_symtab[symndx];
+ } else {
+ /* Others we look up in the hash table. */
+ const char *name;
+ if (extsym->st_name)
+ name = strtab + extsym->st_name;
+ else
+ name = f->sections[extsym->st_shndx]->name;
+ intsym = obj_find_symbol(f, name);
+ }
+
+ value = obj_symbol_final_value(f, intsym);
+ intsym->referenced = 1;
+ }
+#if SHT_RELM == SHT_RELA
+#if defined(__alpha__) && defined(AXP_BROKEN_GAS)
+ /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
+ if (!extsym || !extsym->st_name
+ || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
+#endif
+ value += rel->r_addend;
+#endif
+
+ /* Do it! */
+ switch (arch_apply_relocation
+ (f, targsec, /*symsec,*/ intsym, rel, value)
+ ) {
+ case obj_reloc_ok:
+ break;
+
+ case obj_reloc_overflow:
+ errmsg = "Relocation overflow";
+ goto bad_reloc;
+ case obj_reloc_dangerous:
+ errmsg = "Dangerous relocation";
+ goto bad_reloc;
+ case obj_reloc_unhandled:
+ errmsg = "Unhandled relocation";
+bad_reloc:
+ if (extsym) {
+ bb_error_msg("%s of type %ld for %s", errmsg,
+ (long) ELF_R_TYPE(rel->r_info),
+ strtab + extsym->st_name);
+ } else {
+ bb_error_msg("%s of type %ld", errmsg,
+ (long) ELF_R_TYPE(rel->r_info));
+ }
+ ret = 0;
+ break;
+ }
+ }
+ }
+
+ /* Finally, take care of the patches. */
+
+ if (f->string_patches) {
+ struct obj_string_patch *p;
+ struct obj_section *strsec;
+ ElfW(Addr) strsec_base;
+ strsec = obj_find_section(f, ".kstrtab");
+ strsec_base = strsec->header.sh_addr;
+
+ for (p = f->string_patches; p; p = p->next) {
+ struct obj_section *targsec = f->sections[p->reloc_secidx];
+ *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
+ = strsec_base + p->string_offset;
+ }
+ }
+
+ if (f->symbol_patches) {
+ struct obj_symbol_patch *p;
+
+ for (p = f->symbol_patches; p; p = p->next) {
+ struct obj_section *targsec = f->sections[p->reloc_secidx];
+ *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
+ = obj_symbol_final_value(f, p->sym);
+ }
+ }
+
+ return ret;
+}
+
+static int obj_create_image(struct obj_file *f, char *image)
+{
+ struct obj_section *sec;
+ ElfW(Addr) base = f->baseaddr;
+
+ for (sec = f->load_order; sec; sec = sec->load_next) {
+ char *secimg;
+
+ if (sec->contents == 0 || sec->header.sh_size == 0)
+ continue;
+
+ secimg = image + (sec->header.sh_addr - base);
+
+ /* Note that we allocated data for NOBITS sections earlier. */
+ memcpy(secimg, sec->contents, sec->header.sh_size);
+ }
+
+ return 1;
+}
+
+/*======================================================================*/
+
+static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
+{
+#if BB_LITTLE_ENDIAN
+# define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
+#else
+# define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
+#endif
+ struct obj_file *f;
+ ElfW(Shdr) * section_headers;
+ size_t shnum, i;
+ char *shstrtab;
+
+ /* Read the file header. */
+
+ f = arch_new_file();
+ f->symbol_cmp = strcmp;
+ f->symbol_hash = obj_elf_hash;
+ f->load_order_search_start = &f->load_order;
+
+ if (image_size < sizeof(f->header))
+ bb_error_msg_and_die("error while loading ELF header");
+ memcpy(&f->header, image, sizeof(f->header));
+
+ if (*(uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
+ bb_error_msg_and_die("not an ELF file");
+ }
+ if (f->header.e_ident[EI_CLASS] != ELFCLASSM
+ || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
+ || f->header.e_ident[EI_VERSION] != EV_CURRENT
+ || !MATCH_MACHINE(f->header.e_machine)
+ ) {
+ bb_error_msg_and_die("ELF file not for this architecture");
+ }
+ if (f->header.e_type != ET_REL) {
+ bb_error_msg_and_die("ELF file not a relocatable object");
+ }
+
+ /* Read the section headers. */
+
+ if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
+ bb_error_msg_and_die("section header size mismatch: %lu != %lu",
+ (unsigned long) f->header.e_shentsize,
+ (unsigned long) sizeof(ElfW(Shdr)));
+ }
+
+ shnum = f->header.e_shnum;
+ /* Growth of ->sections vector will be done by
+ * xrealloc_vector(..., 2, ...), therefore we must allocate
+ * at least 2^2 = 4 extra elements here. */
+ f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
+
+ section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
+ if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
+ bb_error_msg_and_die("error while loading section headers");
+ memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
+
+ /* Read the section data. */
+
+ for (i = 0; i < shnum; ++i) {
+ struct obj_section *sec;
+
+ f->sections[i] = sec = arch_new_section();
+
+ sec->header = section_headers[i];
+ sec->idx = i;
+
+ if (sec->header.sh_size) {
+ switch (sec->header.sh_type) {
+ case SHT_NULL:
+ case SHT_NOTE:
+ case SHT_NOBITS:
+ /* ignore */
+ break;
+ case SHT_PROGBITS:
+#if LOADBITS
+ if (!loadprogbits) {
+ sec->contents = NULL;
+ break;
+ }
+#endif
+ case SHT_SYMTAB:
+ case SHT_STRTAB:
+ case SHT_RELM:
+ sec->contents = NULL;
+ if (sec->header.sh_size > 0) {
+ sec->contents = xmalloc(sec->header.sh_size);
+ if (image_size < (sec->header.sh_offset + sec->header.sh_size))
+ bb_error_msg_and_die("error while loading section data");
+ memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
+ }
+ break;
+#if SHT_RELM == SHT_REL
+ case SHT_RELA:
+ bb_error_msg_and_die("RELA relocations not supported on this architecture");
+#else
+ case SHT_REL:
+ bb_error_msg_and_die("REL relocations not supported on this architecture");
+#endif
+ default:
+ if (sec->header.sh_type >= SHT_LOPROC) {
+ /* Assume processor specific section types are debug
+ info and can safely be ignored. If this is ever not
+ the case (Hello MIPS?), don't put ifdefs here but
+ create an arch_load_proc_section(). */
+ break;
+ }
+
+ bb_error_msg_and_die("can't handle sections of type %ld",
+ (long) sec->header.sh_type);
+ }
+ }
+ }
+
+ /* Do what sort of interpretation as needed by each section. */
+
+ shstrtab = f->sections[f->header.e_shstrndx]->contents;
+
+ for (i = 0; i < shnum; ++i) {
+ struct obj_section *sec = f->sections[i];
+ sec->name = shstrtab + sec->header.sh_name;
+ }
+
+ 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);
+
+ switch (sec->header.sh_type) {
+ case SHT_SYMTAB:
+ {
+ unsigned long nsym, j;
+ char *strtab;
+ ElfW(Sym) * sym;
+
+ if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
+ bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
+ (unsigned long) sec->header.sh_entsize,
+ (unsigned long) sizeof(ElfW(Sym)));
+ }
+
+ nsym = sec->header.sh_size / sizeof(ElfW(Sym));
+ strtab = f->sections[sec->header.sh_link]->contents;
+ sym = (ElfW(Sym) *) sec->contents;
+
+ /* Allocate space for a table of local symbols. */
+ j = f->local_symtab_size = sec->header.sh_info;
+ f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
+
+ /* Insert all symbols into the hash table. */
+ for (j = 1, ++sym; j < nsym; ++j, ++sym) {
+ ElfW(Addr) val = sym->st_value;
+ const char *name;
+ if (sym->st_name)
+ name = strtab + sym->st_name;
+ else if (sym->st_shndx < shnum)
+ name = f->sections[sym->st_shndx]->name;
+ else
+ continue;
+#if defined(__SH5__)
+ /*
+ * For sh64 it is possible that the target of a branch
+ * requires a mode switch (32 to 16 and back again).
+ *
+ * This is implied by the lsb being set in the target
+ * address for SHmedia mode and clear for SHcompact.
+ */
+ val |= sym->st_other & 4;
+#endif
+ obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
+ val, sym->st_size);
+ }
+ }
+ break;
+
+ case SHT_RELM:
+ if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
+ bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
+ (unsigned long) sec->header.sh_entsize,
+ (unsigned long) sizeof(ElfW(RelM)));
+ }
+ 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(). */
+ }
+ }
+
+ return f;
+}
+
+#if ENABLE_FEATURE_INSMOD_LOADINKMEM
+/*
+ * load the unloaded sections directly into the memory allocated by
+ * kernel for the module
+ */
+
+static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
+{
+ ElfW(Addr) base = f->baseaddr;
+ struct obj_section* sec;
+
+ for (sec = f->load_order; sec; sec = sec->load_next) {
+ /* section already loaded? */
+ if (sec->contents != NULL)
+ continue;
+ if (sec->header.sh_size == 0)
+ continue;
+ sec->contents = imagebase + (sec->header.sh_addr - base);
+ if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
+ bb_error_msg("error reading ELF section data");
+ return 0; /* need to delete half-loaded module! */
+ }
+ memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
+ }
+ return 1;
+}
+#endif
+
+static void hide_special_symbols(struct obj_file *f)
+{
+ static const char *const specials[] = {
+ SPFX "cleanup_module",
+ SPFX "init_module",
+ SPFX "kernel_version",
+ NULL
+ };
+
+ struct obj_symbol *sym;
+ const char *const *p;
+
+ for (p = specials; *p; ++p) {
+ sym = obj_find_symbol(f, *p);
+ if (sym != NULL)
+ sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
+ }
+}
+
+
+#if ENABLE_FEATURE_CHECK_TAINTED_MODULE
+static int obj_gpl_license(struct obj_file *f, const char **license)
+{
+ struct obj_section *sec;
+ /* This list must match *exactly* the list of allowable licenses in
+ * linux/include/linux/module.h. Checking for leading "GPL" will not
+ * work, somebody will use "GPL sucks, this is proprietary".
+ */
+ static const char *const gpl_licenses[] = {
+ "GPL",
+ "GPL v2",
+ "GPL and additional rights",
+ "Dual BSD/GPL",
+ "Dual MPL/GPL"
+ };
+
+ sec = obj_find_section(f, ".modinfo");
+ if (sec) {
+ const char *value, *ptr, *endptr;
+ ptr = sec->contents;
+ endptr = ptr + sec->header.sh_size;
+ while (ptr < endptr) {
+ value = strchr(ptr, '=');
+ if (value && strncmp(ptr, "license", value-ptr) == 0) {
+ unsigned i;
+ if (license)
+ *license = value+1;
+ for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
+ if (strcmp(value+1, gpl_licenses[i]) == 0)
+ return 0;
+ }
+ return 2;
+ }
+ ptr = strchr(ptr, '\0');
+ if (ptr)
+ ptr++;
+ else
+ ptr = endptr;
+ }
+ }
+ return 1;
+}
+
+#define TAINT_FILENAME "/proc/sys/kernel/tainted"
+#define TAINT_PROPRIETORY_MODULE (1 << 0)
+#define TAINT_FORCED_MODULE (1 << 1)
+#define TAINT_UNSAFE_SMP (1 << 2)
+#define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
+
+static void set_tainted(int fd, const char *m_name,
+ int kernel_has_tainted, int taint, const char *text1, const char *text2)
+{
+ static smallint printed_info;
+
+ char buf[80];
+ int oldval;
+
+ if (fd < 0 && !kernel_has_tainted)
+ return; /* New modutils on old kernel */
+ printf("Warning: loading %s will taint the kernel: %s%s\n",
+ m_name, text1, text2);
+ if (!printed_info) {
+ printf(" See %s for information about tainted modules\n", TAINT_URL);
+ printed_info = 1;
+ }
+ if (fd >= 0) {
+ read(fd, buf, sizeof(buf)-1);
+ buf[sizeof(buf)-1] = '\0';
+ oldval = strtoul(buf, NULL, 10);
+ sprintf(buf, "%d\n", oldval | taint);
+ xwrite_str(fd, buf);
+ }
+}
+
+/* Check if loading this module will taint the kernel. */
+static void check_tainted_module(struct obj_file *f, const char *m_name)
+{
+ static const char tainted_file[] ALIGN1 = TAINT_FILENAME;
+
+ int fd, kernel_has_tainted;
+ const char *ptr;
+
+ kernel_has_tainted = 1;
+ fd = open(tainted_file, O_RDWR);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ kernel_has_tainted = 0;
+ else if (errno == EACCES)
+ kernel_has_tainted = 1;
+ else {
+ perror(tainted_file);
+ kernel_has_tainted = 0;
+ }
+ }
+
+ switch (obj_gpl_license(f, &ptr)) {
+ case 0:
+ break;
+ case 1:
+ set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
+ break;
+ default: /* case 2: */
+ /* The module has a non-GPL license so we pretend that the
+ * kernel always has a taint flag to get a warning even on
+ * kernels without the proc flag.
+ */
+ set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
+ break;
+ }
+
+ if (flag_force_load)
+ set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
+
+ if (fd >= 0)
+ close(fd);
+}
+#else /* !FEATURE_CHECK_TAINTED_MODULE */
+#define check_tainted_module(x, y) do { } while (0);
+#endif
+
+#if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
+/* add module source, timestamp, kernel version and a symbol for the
+ * start of some sections. this info is used by ksymoops to do better
+ * debugging.
+ */
+#if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+#define get_module_version(f, str) get_module_version(str)
+#endif
+static int
+get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
+{
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+ return new_get_module_version(f, str);
+#else
+ strncpy(str, "???", sizeof(str));
+ return -1;
+#endif
+}
+
+/* add module source, timestamp, kernel version and a symbol for the
+ * start of some sections. this info is used by ksymoops to do better
+ * debugging.
+ */
+static void
+add_ksymoops_symbols(struct obj_file *f, const char *filename,
+ const char *m_name)
+{
+ static const char symprefix[] ALIGN1 = "__insmod_";
+ static const char section_names[][8] = {
+ ".text",
+ ".rodata",
+ ".data",
+ ".bss",
+ ".sbss"
+ };
+
+ struct obj_section *sec;
+ struct obj_symbol *sym;
+ char *name, *absolute_filename;
+ char str[STRVERSIONLEN];
+ unsigned i;
+ int lm_name, lfilename, use_ksymtab, version;
+ struct stat statbuf;
+
+ /* WARNING: was using realpath, but replaced by readlink to stop using
+ * lots of stack. But here it seems to be able to cause problems? */
+ absolute_filename = xmalloc_readlink(filename);
+ if (!absolute_filename)
+ absolute_filename = xstrdup(filename);
+
+ lm_name = strlen(m_name);
+ lfilename = strlen(absolute_filename);
+
+ /* add to ksymtab if it already exists or there is no ksymtab and other symbols
+ * are not to be exported. otherwise leave ksymtab alone for now, the
+ * "export all symbols" compatibility code will export these symbols later.
+ */
+ use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
+
+ sec = obj_find_section(f, ".this");
+ if (sec) {
+ /* tag the module header with the object name, last modified
+ * timestamp and module version. worst case for module version
+ * is 0xffffff, decimal 16777215. putting all three fields in
+ * one symbol is less readable but saves kernel space.
+ */
+ if (stat(absolute_filename, &statbuf) != 0)
+ statbuf.st_mtime = 0;
+ version = get_module_version(f, str); /* -1 if not found */
+ name = xasprintf("%s%s_O%s_M%0*lX_V%d",
+ symprefix, m_name, absolute_filename,
+ (int)(2 * sizeof(statbuf.st_mtime)),
+ (long)statbuf.st_mtime,
+ version);
+ sym = obj_add_symbol(f, name, -1,
+ ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+ sec->idx, sec->header.sh_addr, 0);
+ if (use_ksymtab)
+ new_add_ksymtab(f, sym);
+ }
+ free(absolute_filename);
+#ifdef _NOT_SUPPORTED_
+ /* record where the persistent data is going, same address as previous symbol */
+ if (f->persist) {
+ name = xasprintf("%s%s_P%s",
+ symprefix, m_name, f->persist);
+ sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+ sec->idx, sec->header.sh_addr, 0);
+ if (use_ksymtab)
+ new_add_ksymtab(f, sym);
+ }
+#endif
+ /* tag the desired sections if size is non-zero */
+ for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
+ sec = obj_find_section(f, section_names[i]);
+ if (sec && sec->header.sh_size) {
+ name = xasprintf("%s%s_S%s_L%ld",
+ symprefix, m_name, sec->name,
+ (long)sec->header.sh_size);
+ sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
+ sec->idx, sec->header.sh_addr, 0);
+ if (use_ksymtab)
+ new_add_ksymtab(f, sym);
+ }
+ }
+}
+#endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
+
+#if ENABLE_FEATURE_INSMOD_LOAD_MAP
+static void print_load_map(struct obj_file *f)
+{
+ struct obj_section *sec;
+#if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
+ struct obj_symbol **all, **p;
+ int i, nsyms;
+ char *loaded; /* array of booleans */
+ struct obj_symbol *sym;
+#endif
+ /* Report on the section layout. */
+ printf("Sections: Size %-*s Align\n",
+ (int) (2 * sizeof(void *)), "Address");
+
+ for (sec = f->load_order; sec; sec = sec->load_next) {
+ int a;
+ unsigned long tmp;
+
+ for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
+ tmp >>= 1;
+ if (a == -1)
+ a = 0;
+
+ printf("%-15s %08lx %0*lx 2**%d\n",
+ sec->name,
+ (long)sec->header.sh_size,
+ (int) (2 * sizeof(void *)),
+ (long)sec->header.sh_addr,
+ a);
+ }
+#if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
+ /* Quick reference which section indices are loaded. */
+ i = f->header.e_shnum;
+ loaded = alloca(i * sizeof(loaded[0]));
+ while (--i >= 0)
+ loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
+
+ /* Collect the symbols we'll be listing. */
+ for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
+ for (sym = f->symtab[i]; sym; sym = sym->next)
+ if (sym->secidx <= SHN_HIRESERVE
+ && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
+ ) {
+ ++nsyms;
+ }
+
+ all = alloca(nsyms * sizeof(all[0]));
+
+ for (i = 0, p = all; i < HASH_BUCKETS; ++i)
+ for (sym = f->symtab[i]; sym; sym = sym->next)
+ if (sym->secidx <= SHN_HIRESERVE
+ && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
+ ) {
+ *p++ = sym;
+ }
+
+ /* And list them. */
+ printf("\nSymbols:\n");
+ for (p = all; p < all + nsyms; ++p) {
+ char type = '?';
+ unsigned long value;
+
+ sym = *p;
+ if (sym->secidx == SHN_ABS) {
+ type = 'A';
+ value = sym->value;
+ } else if (sym->secidx == SHN_UNDEF) {
+ type = 'U';
+ value = 0;
+ } else {
+ sec = f->sections[sym->secidx];
+
+ if (sec->header.sh_type == SHT_NOBITS)
+ type = 'B';
+ else if (sec->header.sh_flags & SHF_ALLOC) {
+ if (sec->header.sh_flags & SHF_EXECINSTR)
+ type = 'T';
+ else if (sec->header.sh_flags & SHF_WRITE)
+ type = 'D';
+ else
+ type = 'R';
+ }
+ value = sym->value + sec->header.sh_addr;
+ }
+
+ if (ELF_ST_BIND(sym->info) == STB_LOCAL)
+ type |= 0x20; /* tolower. safe for '?' too */
+
+ printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
+ type, sym->name);
+ }
+#endif
+}
+#else /* !FEATURE_INSMOD_LOAD_MAP */
+static void print_load_map(struct obj_file *f UNUSED_PARAM)
+{
+}
+#endif
+
+int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
+{
+ int k_crcs;
+ unsigned long m_size;
+ ElfW(Addr) m_addr;
+ struct obj_file *f;
+ int exit_status = EXIT_FAILURE;
+ char *m_name;
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+ int m_has_modinfo;
+#endif
+ char *image;
+ size_t image_size = 64 * 1024 * 1024;
+
+ /* Load module into memory and unzip if compressed */
+ image = xmalloc_open_zipped_read_close(m_filename, &image_size);
+ if (!image)
+ return EXIT_FAILURE;
+
+ m_name = xstrdup(bb_basename(m_filename));
+ /* "module.o[.gz]" -> "module" */
+ *strchrnul(m_name, '.') = '\0';
+
+ f = obj_load(image, image_size, LOADBITS);
+
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+ /* Version correspondence? */
+ m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
+ if (!flag_quiet) {
+ char m_strversion[STRVERSIONLEN];
+ struct utsname uts;
+
+ if (m_has_modinfo) {
+ int m_version = new_get_module_version(f, m_strversion);
+ if (m_version == -1) {
+ bb_error_msg_and_die("can't find the kernel version "
+ "the module was compiled for");
+ }
+ }
+
+ uname(&uts);
+ if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
+ bb_error_msg("%skernel-module version mismatch\n"
+ "\t%s was compiled for kernel version %s\n"
+ "\twhile this kernel is version %s",
+ flag_force_load ? "warning: " : "",
+ m_name, m_strversion, uts.release);
+ if (!flag_force_load)
+ goto out;
+ }
+ }
+#endif
+
+ if (query_module(NULL, 0, NULL, 0, NULL))
+ bb_error_msg_and_die("old (unsupported) kernel");
+ new_get_kernel_symbols();
+ k_crcs = new_is_kernel_checksummed();
+
+#if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
+ {
+ int m_crcs = 0;
+ if (m_has_modinfo)
+ m_crcs = new_is_module_checksummed(f);
+ if (m_crcs != k_crcs)
+ obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
+ }
+#endif
+
+ /* Let the module know about the kernel symbols. */
+ add_kernel_symbols(f);
+
+ /* Allocate common symbols, symbol tables, and string tables. */
+ new_create_this_module(f, m_name);
+ obj_check_undefineds(f);
+ obj_allocate_commons(f);
+ check_tainted_module(f, m_name);
+
+ /* Done with the module name, on to the optional var=value arguments */
+ new_process_module_arguments(f, options);
+
+ arch_create_got(f);
+ hide_special_symbols(f);
+
+#if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
+ add_ksymoops_symbols(f, m_filename, m_name);
+#endif
+
+ new_create_module_ksymtab(f);
+
+ /* Find current size of the module */
+ m_size = obj_load_size(f);
+
+ m_addr = create_module(m_name, m_size);
+ if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
+ case EEXIST:
+ bb_error_msg_and_die("a module named %s already exists", m_name);
+ case ENOMEM:
+ bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
+ m_size);
+ default:
+ bb_perror_msg_and_die("create_module: %s", m_name);
+ }
+
+#if !LOADBITS
+ /*
+ * the PROGBITS section was not loaded by the obj_load
+ * now we can load them directly into the kernel memory
+ */
+ if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
+ delete_module(m_name, 0);
+ goto out;
+ }
+#endif
+
+ if (!obj_relocate(f, m_addr)) {
+ delete_module(m_name, 0);
+ goto out;
+ }
+
+ if (!new_init_module(m_name, f, m_size)) {
+ delete_module(m_name, 0);
+ goto out;
+ }
+
+ if (flag_print_load_map)
+ print_load_map(f);
+
+ exit_status = EXIT_SUCCESS;
+
+ out:
+ free(image);
+ free(m_name);
+
+ return exit_status;
+}
diff --git a/release/src/router/busybox/modutils/modutils.c b/release/src/router/busybox/modutils/modutils.c
new file mode 100644
index 00000000..f437a982
--- /dev/null
+++ b/release/src/router/busybox/modutils/modutils.c
@@ -0,0 +1,129 @@
+/*
+ * Common modutils related functions for busybox
+ *
+ * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+#include "modutils.h"
+
+#ifdef __UCLIBC__
+extern int init_module(void *module, unsigned long len, const char *options);
+extern int delete_module(const char *module, unsigned int flags);
+#else
+# include <sys/syscall.h>
+# define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
+# define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
+#endif
+
+void FAST_FUNC replace(char *s, char what, char with)
+{
+ while (*s) {
+ if (what == *s)
+ *s = with;
+ ++s;
+ }
+}
+
+char * FAST_FUNC replace_underscores(char *s)
+{
+ replace(s, '-', '_');
+ return s;
+}
+
+int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
+{
+ char *tok;
+ int len = 0;
+
+ while ((tok = strsep(&string, delim)) != NULL) {
+ if (tok[0] == '\0')
+ continue;
+ llist_add_to_end(llist, xstrdup(tok));
+ len += strlen(tok);
+ }
+ return len;
+}
+
+char * FAST_FUNC filename2modname(const char *filename, char *modname)
+{
+ int i;
+ char *from;
+
+ if (filename == NULL)
+ return NULL;
+ if (modname == NULL)
+ modname = xmalloc(MODULE_NAME_LEN);
+ from = bb_get_last_path_component_nostrip(filename);
+ for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
+ modname[i] = (from[i] == '-') ? '_' : from[i];
+ modname[i] = '\0';
+
+ return modname;
+}
+
+const char * FAST_FUNC moderror(int err)
+{
+ switch (err) {
+ case -1:
+ return "no such module";
+ case ENOEXEC:
+ return "invalid module format";
+ case ENOENT:
+ return "unknown symbol in module, or unknown parameter";
+ case ESRCH:
+ return "module has wrong symbol version";
+ case ENOSYS:
+ return "kernel does not support requested operation";
+ default:
+ return strerror(err);
+ }
+}
+
+char * FAST_FUNC parse_cmdline_module_options(char **argv)
+{
+ char *options;
+ int optlen;
+
+ options = xzalloc(1);
+ optlen = 0;
+ while (*++argv) {
+ options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
+ /* Spaces handled by "" pairs, but no way of escaping quotes */
+ optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
+ }
+ return options;
+}
+
+int FAST_FUNC bb_init_module(const char *filename, const char *options)
+{
+ size_t len;
+ char *image;
+ int rc;
+
+ if (!options)
+ options = "";
+
+#if ENABLE_FEATURE_2_4_MODULES
+ if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
+ return bb_init_module_24(filename, options);
+#endif
+
+ /* Use the 2.6 way */
+ len = INT_MAX - 4095;
+ rc = ENOENT;
+ image = xmalloc_open_zipped_read_close(filename, &len);
+ if (image) {
+ rc = 0;
+ if (init_module(image, len, options) != 0)
+ rc = errno;
+ free(image);
+ }
+
+ return rc;
+}
+
+int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
+{
+ return delete_module(module, flags);
+}
diff --git a/release/src/router/busybox/modutils/modutils.h b/release/src/router/busybox/modutils/modutils.h
new file mode 100644
index 00000000..5104f1b6
--- /dev/null
+++ b/release/src/router/busybox/modutils/modutils.h
@@ -0,0 +1,64 @@
+/*
+ * Common modutils related functions for busybox
+ *
+ * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#ifndef MODUTILS_H
+#define MODUTILS_H 1
+
+#include "libbb.h"
+
+PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
+
+/* linux/include/linux/module.h has 64, but this is also used
+ * internally for the maximum alias name length, which can be quite long */
+#define MODULE_NAME_LEN 256
+
+const char *moderror(int err) FAST_FUNC;
+void replace(char *s, char what, char with) FAST_FUNC;
+char *replace_underscores(char *s) FAST_FUNC;
+int string_to_llist(char *string, llist_t **llist, const char *delim) FAST_FUNC;
+char *filename2modname(const char *filename, char *modname) FAST_FUNC;
+char *parse_cmdline_module_options(char **argv) FAST_FUNC;
+
+#define INSMOD_OPTS \
+ "vq" \
+ USE_FEATURE_2_4_MODULES("sLo:fkx") \
+ USE_FEATURE_INSMOD_LOAD_MAP("m")
+
+#define INSMOD_ARGS USE_FEATURE_2_4_MODULES(, NULL)
+
+enum {
+ INSMOD_OPT_VERBOSE = 0x0001,
+ INSMOD_OPT_SILENT = 0x0002,
+ INSMOD_OPT_SYSLOG = 0x0004 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_LOCK = 0x0008 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_OUTPUTNAME = 0x0010 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_FORCE = 0x0020 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_KERNELD = 0x0040 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_NO_EXPORT = 0x0080 * ENABLE_FEATURE_2_4_MODULES,
+ INSMOD_OPT_PRINT_MAP = 0x0100 * ENABLE_FEATURE_INSMOD_LOAD_MAP,
+#if ENABLE_FEATURE_2_4_MODULES
+# if ENABLE_FEATURE_INSMOD_LOAD_MAP
+ INSMOD_OPT_UNUSED = 0x0200,
+# else
+ INSMOD_OPT_UNUSED = 0x0100,
+# endif
+#else
+ INSMOD_OPT_UNUSED = 0x0004,
+#endif
+};
+
+int FAST_FUNC bb_init_module(const char *module, const char *options);
+int FAST_FUNC bb_delete_module(const char *module, unsigned int flags);
+
+#if ENABLE_FEATURE_2_4_MODULES
+int FAST_FUNC bb_init_module_24(const char *module, const char *options);
+#endif
+
+POP_SAVED_FUNCTION_VISIBILITY
+
+#endif
diff --git a/release/src/router/busybox/modutils/rmmod.c b/release/src/router/busybox/modutils/rmmod.c
index a4ea7041..ee32dfde 100644
--- a/release/src/router/busybox/modutils/rmmod.c
+++ b/release/src/router/busybox/modutils/rmmod.c
@@ -2,76 +2,52 @@
/*
* Mini rmmod implementation for busybox
*
- * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
+ * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
*
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <getopt.h>
-#include "busybox.h"
+#include "libbb.h"
+#include "modutils.h"
-extern int delete_module(const char * name);
-
-
-extern int rmmod_main(int argc, char **argv)
+int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int rmmod_main(int argc UNUSED_PARAM, char **argv)
{
- int n, ret = EXIT_SUCCESS;
- size_t nmod = 0; /* number of modules */
- size_t pnmod = -1; /* previous number of modules */
- void *buf; /* hold the module names which we ignore but must get */
- size_t bufsize = 0;
+ int n;
+ unsigned flags = O_NONBLOCK | O_EXCL;
/* Parse command line. */
- while ((n = getopt(argc, argv, "a")) != EOF) {
- switch (n) {
- case 'a':
- /* Unload _all_ unused modules via NULL delete_module() call */
- /* until the number of modules does not change */
- buf = xmalloc(bufsize = 256);
- while (nmod != pnmod) {
- if (delete_module(NULL))
- bb_perror_msg_and_die("rmmod");
- pnmod = nmod;
- /* 1 == QM_MODULES */
- if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
- bb_perror_msg_and_die("QM_MODULES");
- }
- }
-#ifdef CONFIG_FEATURE_CLEAN_UP
- free(buf);
-#endif
- return EXIT_SUCCESS;
- default:
- bb_show_usage();
- }
+ n = getopt32(argv, "wfas"); // -s ignored
+ argv += optind;
+ if (n & 1) // --wait
+ flags &= ~O_NONBLOCK;
+ if (n & 2) // --force
+ flags |= O_TRUNC;
+ if (n & 4) {
+ /* Unload _all_ unused modules via NULL delete_module() call */
+ if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
+ bb_perror_msg_and_die("rmmod");
+ return EXIT_SUCCESS;
}
- if (optind == argc)
- bb_show_usage();
-
- for (n = optind; n < argc; n++) {
- if (delete_module(argv[n]) < 0) {
- bb_perror_msg("%s", argv[n]);
- ret = EXIT_FAILURE;
- }
+ if (!*argv)
+ bb_show_usage();
+
+ n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
+ while (*argv) {
+ char modname[MODULE_NAME_LEN];
+ const char *bname;
+
+ bname = bb_basename(*argv++);
+ if (n)
+ safe_strncpy(modname, bname, MODULE_NAME_LEN);
+ else
+ filename2modname(bname, modname);
+ if (bb_delete_module(modname, flags))
+ bb_error_msg_and_die("can't unload '%s': %s",
+ modname, moderror(errno));
}
- return(ret);
+ return EXIT_SUCCESS;
}