From e8d0f85c8bfe415a093c571ff0bbd01a2769fa8f Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 13 Dec 2015 08:10:01 -0600 Subject: [PATCH] Modules: Add mod_setsymtab to set global symtol table once. Now we can remove symbol table parameters from the insmod call. This will make implementing an NSH insmod command much easier --- binfmt/Makefile | 7 +- include/nuttx/binfmt/symtab.h | 113 +----------- include/nuttx/module.h | 49 +++++- include/nuttx/symtab.h | 164 ++++++++++++++++++ include/sys/boardctl.h | 33 ++-- libc/Makefile | 1 + libc/symtab/Make.defs | 44 +++++ {binfmt => libc/symtab}/symtab_findbyname.c | 23 +-- {binfmt => libc/symtab}/symtab_findbyvalue.c | 23 +-- .../symtab}/symtab_findorderedbyname.c | 22 +-- .../symtab}/symtab_findorderedbyvalue.c | 23 +-- sched/module/Make.defs | 4 +- sched/module/mod_bind.c | 17 +- sched/module/mod_insmod.c | 14 +- sched/module/mod_symbols.c | 11 +- sched/module/mod_symtab.c | 110 ++++++++++++ sched/module/module.h | 18 +- 17 files changed, 437 insertions(+), 239 deletions(-) create mode 100644 include/nuttx/symtab.h create mode 100644 libc/symtab/Make.defs rename {binfmt => libc/symtab}/symtab_findbyname.c (78%) rename {binfmt => libc/symtab}/symtab_findbyvalue.c (81%) rename {binfmt => libc/symtab}/symtab_findorderedbyname.c (83%) rename {binfmt => libc/symtab}/symtab_findorderedbyvalue.c (82%) create mode 100644 sched/module/mod_symtab.c diff --git a/binfmt/Makefile b/binfmt/Makefile index 9da55ea973a..5c5244d89bf 100644 --- a/binfmt/Makefile +++ b/binfmt/Makefile @@ -1,7 +1,7 @@ ############################################################################ # nxflat/Makefile # -# Copyright (C) 2007-2009, 2012-2014 Gregory Nutt. All rights reserved. +# Copyright (C) 2007-2009, 2012-2015 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -56,11 +56,6 @@ ifeq ($(CONFIG_SCHED_HAVE_PARENT),y) BINFMT_CSRCS += binfmt_schedunload.c endif -# Symbol table source files - -BINFMT_CSRCS += symtab_findbyname.c symtab_findbyvalue.c -BINFMT_CSRCS += symtab_findorderedbyname.c symtab_findorderedbyvalue.c - ifeq ($(CONFIG_LIBC_EXECFUNCS),y) BINFMT_CSRCS += binfmt_execsymtab.c endif diff --git a/include/nuttx/binfmt/symtab.h b/include/nuttx/binfmt/symtab.h index c6cdeffe444..9e51ca35398 100644 --- a/include/nuttx/binfmt/symtab.h +++ b/include/nuttx/binfmt/symtab.h @@ -1,7 +1,7 @@ /**************************************************************************** * include/nuttx/binfmt/symtab.h * - * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -41,34 +41,7 @@ ****************************************************************************/ #include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* struct symbtab_s describes one entry in the symbol table. A symbol table - * is a fixed size array of struct symtab_s. The information is intentionally - * minimal and supports only: - * - * 1. Function pointers as sym_values. Of other kinds of values need to be - * supported, then typing information would also need to be included in - * the structure. - * - * 2. Fixed size arrays. There is no explicit provisional for dyanamically - * adding or removing entries from the symbol table (realloc might be - * used for that purpose if needed). The intention is to support only - * fixed size arrays completely defined at compilation or link time. - */ - -struct symtab_s -{ - FAR const char *sym_name; /* A pointer to the symbol name string */ - FAR const void *sym_value; /* The value associated witht the string */ -}; +#include /**************************************************************************** * Public Functions @@ -87,7 +60,7 @@ extern "C" * Name: exec_getsymtab * * Description: - * Get the current symbol table selection as an atomic operation. + * Get the current application symbol table selection as an atomic operation. * * Input Parameters: * symtab - The location to store the symbol table. @@ -104,7 +77,7 @@ void exec_getsymtab(FAR const struct symtab_s **symtab, FAR int *nsymbols); * Name: exec_setsymtab * * Description: - * Select a new symbol table selection as an atomic operation. + * Select a new application symbol table selection as an atomic operation. * * Input Parameters: * symtab - The new symbol table. @@ -117,82 +90,4 @@ void exec_getsymtab(FAR const struct symtab_s **symtab, FAR int *nsymbols); void exec_setsymtab(FAR const struct symtab_s *symtab, int nsymbols); -/**************************************************************************** - * Name: symtab_findbyname - * - * Description: - * Find the symbol in the symbol table with the matching name. - * This version assumes that table is not ordered with respect to symbol - * name and, hence, access time will be linear with respect to nsyms. - * - * Returned Value: - * A reference to the symbol table entry if an entry with the matching - * name is found; NULL is returned if the entry is not found. - * - ****************************************************************************/ - -FAR const struct symtab_s * -symtab_findbyname(FAR const struct symtab_s *symtab, - FAR const char *name, int nsyms); - -/**************************************************************************** - * Name: symtab_findorderedbyname - * - * Description: - * Find the symbol in the symbol table with the matching name. - * This version assumes that table ordered with respect to symbol name. - * - * Returned Value: - * A reference to the symbol table entry if an entry with the matching - * name is found; NULL is returned if the entry is not found. - * - ****************************************************************************/ - -FAR const struct symtab_s * -symtab_findorderedbyname(FAR const struct symtab_s *symtab, - FAR const char *name, int nsyms); - -/**************************************************************************** - * Name: symtab_findbyvalue - * - * Description: - * Find the symbol in the symbol table whose value closest (but not greater - * than), the provided value. This version assumes that table is not ordered - * with respect to symbol name and, hence, access time will be linear with - * respect to nsyms. - * - * Returned Value: - * A reference to the symbol table entry if an entry with the matching - * name is found; NULL is returned if the entry is not found. - * - ****************************************************************************/ - -FAR const struct symtab_s * -symtab_findbyvalue(FAR const struct symtab_s *symtab, - FAR void *value, int nsyms); - -/**************************************************************************** - * Name: symtab_findorderedbyvalue - * - * Description: - * Find the symbol in the symbol table whose value closest (but not greater - * than), the provided value. This version assumes that table is ordered - * with respect to symbol name. - * - * Returned Value: - * A reference to the symbol table entry if an entry with the matching - * name is found; NULL is returned if the entry is not found. - * - ****************************************************************************/ - -FAR const struct symtab_s * -symtab_findorderedbyvalue(FAR const struct symtab_s *symtab, - FAR void *value, int nsyms); - -#undef EXTERN -#if defined(__cplusplus) -} -#endif - #endif /* __INCLUDE_NUTTX_BINFMT_SYMTAB_H */ - diff --git a/include/nuttx/module.h b/include/nuttx/module.h index 8b9c2cfc8e1..7ce8b804472 100644 --- a/include/nuttx/module.h +++ b/include/nuttx/module.h @@ -49,6 +49,7 @@ #include #include +#include #include /**************************************************************************** @@ -132,6 +133,44 @@ extern "C" #define EXTERN extern #endif +/**************************************************************************** + * Name: mod_getsymtab + * + * Description: + * Get the current kernel symbol table selection as an atomic operation. + * + * Input Parameters: + * symtab - The location to store the symbol table. + * nsymbols - The location to store the number of symbols in the symbol table. + * + * Returned Value: + * None + * + ****************************************************************************/ + +#ifdef __KERNEL__ +void mod_getsymtab(FAR const struct symtab_s **symtab, FAR int *nsymbols); +#endif + +/**************************************************************************** + * Name: mod_setsymtab + * + * Description: + * Select a new kernel symbol table selection as an atomic operation. + * + * Input Parameters: + * symtab - The new symbol table. + * nsymbols - The number of symbols in the symbol table. + * + * Returned Value: + * None + * + ****************************************************************************/ + +#ifdef __KERNEL__ +void mod_setsymtab(FAR const struct symtab_s *symtab, int nsymbols); +#endif + /**************************************************************************** * Name: insmod * @@ -139,13 +178,16 @@ extern "C" * Verify that the file is an ELF module binary and, if so, load the * module into kernel memory and initialize it for use. * + * NOTE: mod_setsymtab had to have been called in board-specific OS logic + * prior to calling this function from application logic (perhaps via + * boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to + * resolve symbols in the OS module. + * * Input Parameters: * * filename - Full path to the module binary to be loaded * modulename - The name that can be used to refer to the module after * it has been loaded. - * exports - Table of exported symbols - * nexports - The number of symbols in exports[] * * Returned Value: * Zero (OK) on success. On any failure, -1 (ERROR) is returned the @@ -153,8 +195,7 @@ extern "C" * ****************************************************************************/ -int insmod(FAR const char *filename, FAR const char *modulename, - FAR const struct symtab_s *exports, int nexports); +int insmod(FAR const char *filename, FAR const char *modulename); /**************************************************************************** * Name: rmmod diff --git a/include/nuttx/symtab.h b/include/nuttx/symtab.h new file mode 100644 index 00000000000..d835a17b0db --- /dev/null +++ b/include/nuttx/symtab.h @@ -0,0 +1,164 @@ +/**************************************************************************** + * include/nuttx/binfmt/symtab.h + * + * Copyright (C) 2009, 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SYMTAB_H +#define __INCLUDE_NUTTX_SYMTAB_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* struct symbtab_s describes one entry in the symbol table. A symbol table + * is a fixed size array of struct symtab_s. The information is intentionally + * minimal and supports only: + * + * 1. Function pointers as sym_values. Of other kinds of values need to be + * supported, then typing information would also need to be included in + * the structure. + * + * 2. Fixed size arrays. There is no explicit provisional for dyanamically + * adding or removing entries from the symbol table (realloc might be + * used for that purpose if needed). The intention is to support only + * fixed size arrays completely defined at compilation or link time. + */ + +struct symtab_s +{ + FAR const char *sym_name; /* A pointer to the symbol name string */ + FAR const void *sym_value; /* The value associated witht the string */ +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: symtab_findbyname + * + * Description: + * Find the symbol in the symbol table with the matching name. + * This version assumes that table is not ordered with respect to symbol + * name and, hence, access time will be linear with respect to nsyms. + * + * Returned Value: + * A reference to the symbol table entry if an entry with the matching + * name is found; NULL is returned if the entry is not found. + * + ****************************************************************************/ + +FAR const struct symtab_s * +symtab_findbyname(FAR const struct symtab_s *symtab, + FAR const char *name, int nsyms); + +/**************************************************************************** + * Name: symtab_findorderedbyname + * + * Description: + * Find the symbol in the symbol table with the matching name. + * This version assumes that table ordered with respect to symbol name. + * + * Returned Value: + * A reference to the symbol table entry if an entry with the matching + * name is found; NULL is returned if the entry is not found. + * + ****************************************************************************/ + +FAR const struct symtab_s * +symtab_findorderedbyname(FAR const struct symtab_s *symtab, + FAR const char *name, int nsyms); + +/**************************************************************************** + * Name: symtab_findbyvalue + * + * Description: + * Find the symbol in the symbol table whose value closest (but not greater + * than), the provided value. This version assumes that table is not ordered + * with respect to symbol name and, hence, access time will be linear with + * respect to nsyms. + * + * Returned Value: + * A reference to the symbol table entry if an entry with the matching + * name is found; NULL is returned if the entry is not found. + * + ****************************************************************************/ + +FAR const struct symtab_s * +symtab_findbyvalue(FAR const struct symtab_s *symtab, + FAR void *value, int nsyms); + +/**************************************************************************** + * Name: symtab_findorderedbyvalue + * + * Description: + * Find the symbol in the symbol table whose value closest (but not greater + * than), the provided value. This version assumes that table is ordered + * with respect to symbol name. + * + * Returned Value: + * A reference to the symbol table entry if an entry with the matching + * name is found; NULL is returned if the entry is not found. + * + ****************************************************************************/ + +FAR const struct symtab_s * +symtab_findorderedbyvalue(FAR const struct symtab_s *symtab, + FAR void *value, int nsyms); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __INCLUDE_NUTTX_SYMTAB_H */ + diff --git a/include/sys/boardctl.h b/include/sys/boardctl.h index 81b511bb5b5..ac7989b9220 100644 --- a/include/sys/boardctl.h +++ b/include/sys/boardctl.h @@ -77,10 +77,20 @@ * which to receive the board unique ID. * DEPENDENCIES: Board logic must provide the board_uniqueid() interface. * - * CMD: BOARDIOC_SYMTAB - * DESCRIPTION: Select a symbol table + * CMD: BOARDIOC_APP_SYMTAB + * DESCRIPTION: Select the application symbol table. This symbol table + * provides the symbol definitions exported to application + * code from application space. * ARG: A pointer to an instance of struct boardioc_symtab_s - * CONFIGURATION: CONFIG_BOARDCTL_SYMTAB + * CONFIGURATION: CONFIG_BOARDCTL_APP_SYMTAB + * DEPENDENCIES: None + * + * CMD: BOARDIOC_OS_SYMTAB + * DESCRIPTION: Select the OS symbol table. This symbol table provides + * the symbol definitions exported by the OS to kernel + * modules. + * ARG: A pointer to an instance of struct boardioc_symtab_s + * CONFIGURATION: CONFIG_BOARDCTL_OS_SYMTAB * DEPENDENCIES: None * * CMD: BOARDIOC_TSCTEST_SETUP @@ -125,13 +135,14 @@ #define BOARDIOC_POWEROFF _BOARDIOC(0x0002) #define BOARDIOC_RESET _BOARDIOC(0x0003) #define BOARDIOC_UNIQUEID _BOARDIOC(0x0004) -#define BOARDIOC_SYMTAB _BOARDIOC(0x0005) -#define BOARDIOC_TSCTEST_SETUP _BOARDIOC(0x0006) -#define BOARDIOC_TSCTEST_TEARDOWN _BOARDIOC(0x0007) -#define BOARDIOC_ADCTEST_SETUP _BOARDIOC(0x0008) -#define BOARDIOC_PWMTEST_SETUP _BOARDIOC(0x0009) -#define BOARDIOC_CAN_INITIALIZE _BOARDIOC(0x000a) -#define BOARDIOC_GRAPHICS_SETUP _BOARDIOC(0x000b) +#define BOARDIOC_APP_SYMTAB _BOARDIOC(0x0005) +#define BOARDIOC_OS_SYMTAB _BOARDIOC(0x0006) +#define BOARDIOC_TSCTEST_SETUP _BOARDIOC(0x0007) +#define BOARDIOC_TSCTEST_TEARDOWN _BOARDIOC(0x0008) +#define BOARDIOC_ADCTEST_SETUP _BOARDIOC(0x0009) +#define BOARDIOC_PWMTEST_SETUP _BOARDIOC(0x000a) +#define BOARDIOC_CAN_INITIALIZE _BOARDIOC(0x000b) +#define BOARDIOC_GRAPHICS_SETUP _BOARDIOC(0x000c) /* If CONFIG_BOARDCTL_IOCTL=y, then boad-specific commands will be support. * In this case, all commands not recognized by boardctl() will be forwarded @@ -140,7 +151,7 @@ * User defined board commands may begin with this value: */ -#define BOARDIOC_USER _BOARDIOC(0x000c) +#define BOARDIOC_USER _BOARDIOC(0x000d) /**************************************************************************** * Public Type Definitions diff --git a/libc/Makefile b/libc/Makefile index 76d3e473aa7..d83957a95c9 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -81,6 +81,7 @@ include dirent/Make.defs include termios/Make.defs include spawn/Make.defs include queue/Make.defs +include symtab/Make.defs include wqueue/Make.defs include misc/Make.defs include audio/Make.defs diff --git a/libc/symtab/Make.defs b/libc/symtab/Make.defs new file mode 100644 index 00000000000..84f2ed23f8b --- /dev/null +++ b/libc/symtab/Make.defs @@ -0,0 +1,44 @@ +############################################################################ +# libc/symtab/Make.defs +# +# Copyright (C) 2015 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +# Symbol table source files + +CSRCS += symtab_findbyname.c symtab_findbyvalue.c +CSRCS += symtab_findorderedbyname.c symtab_findorderedbyvalue.c + +# Add the symtab directory to the build + +DEPPATH += --dep-path symtab +VPATH += :symtab diff --git a/binfmt/symtab_findbyname.c b/libc/symtab/symtab_findbyname.c similarity index 78% rename from binfmt/symtab_findbyname.c rename to libc/symtab/symtab_findbyname.c index ecbd1bd7ee5..e84e5eb5271 100644 --- a/binfmt/symtab_findbyname.c +++ b/libc/symtab/symtab_findbyname.c @@ -1,7 +1,7 @@ /**************************************************************************** - * binfmt/symtab_findbyname.c + * libc/symtab/symtab_findbyname.c * - * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,23 +44,7 @@ #include #include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#include /**************************************************************************** * Public Functions @@ -94,4 +78,3 @@ symtab_findbyname(FAR const struct symtab_s *symtab, } return NULL; } - diff --git a/binfmt/symtab_findbyvalue.c b/libc/symtab/symtab_findbyvalue.c similarity index 81% rename from binfmt/symtab_findbyvalue.c rename to libc/symtab/symtab_findbyvalue.c index 600614a1e3c..d3a12105d1a 100644 --- a/binfmt/symtab_findbyvalue.c +++ b/libc/symtab/symtab_findbyvalue.c @@ -1,7 +1,7 @@ /**************************************************************************** - * binfmt/symtab_findbyvalue.c + * libc/symtab/symtab_findbyvalue.c * - * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,23 +44,7 @@ #include #include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#include /**************************************************************************** * Public Functions @@ -118,4 +102,3 @@ symtab_findbyvalue(FAR const struct symtab_s *symtab, return retval; } - diff --git a/binfmt/symtab_findorderedbyname.c b/libc/symtab/symtab_findorderedbyname.c similarity index 83% rename from binfmt/symtab_findorderedbyname.c rename to libc/symtab/symtab_findorderedbyname.c index 27714001941..40d6e883a2c 100644 --- a/binfmt/symtab_findorderedbyname.c +++ b/libc/symtab/symtab_findorderedbyname.c @@ -1,7 +1,7 @@ /**************************************************************************** - * binfmt/symtab_findorderedbyname.c + * libc/symtab/symtab_findorderedbyname.c * - * Copyright (C) 2009, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2014-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,23 +44,7 @@ #include #include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#include /**************************************************************************** * Public Functions diff --git a/binfmt/symtab_findorderedbyvalue.c b/libc/symtab/symtab_findorderedbyvalue.c similarity index 82% rename from binfmt/symtab_findorderedbyvalue.c rename to libc/symtab/symtab_findorderedbyvalue.c index ba731be965a..253ec2cc5fb 100644 --- a/binfmt/symtab_findorderedbyvalue.c +++ b/libc/symtab/symtab_findorderedbyvalue.c @@ -1,7 +1,7 @@ /**************************************************************************** - * binfmt/symtab_findorderedbyvalue.c + * libc/symtab/symtab_findorderedbyvalue.c * - * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -44,23 +44,7 @@ #include #include -#include - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ +#include /**************************************************************************** * Public Functions @@ -124,4 +108,3 @@ symtab_findorderedbyvalue(FAR const struct symtab_s *symtab, return value == symtab[low].sym_value ? &symtab[low] : NULL; } - diff --git a/sched/module/Make.defs b/sched/module/Make.defs index ec60760c8a9..00f84675606 100644 --- a/sched/module/Make.defs +++ b/sched/module/Make.defs @@ -42,8 +42,8 @@ CSRCS += mod_insmod.c mod_rmmod.c # loadable module library CSRCS += mod_bind.c mod_init.c mod_iobuffer.c mod_load.c mod_read.c -CSRCS += mod_registry.c mod_sections.c mod_symbols.c mod_uninit.c -CSRCS += mod_unload.c mod_verify.c +CSRCS += mod_registry.c mod_sections.c mod_symbols.c mod_symtab.c +CSRCS += mod_uninit.c mod_unload.c mod_verify.c # procfs support diff --git a/sched/module/mod_bind.c b/sched/module/mod_bind.c index a2979664b7a..75d8e1b56a4 100644 --- a/sched/module/mod_bind.c +++ b/sched/module/mod_bind.c @@ -98,8 +98,7 @@ static inline int mod_readrel(FAR struct mod_loadinfo_s *loadinfo, * ****************************************************************************/ -static int mod_relocate(FAR struct mod_loadinfo_s *loadinfo, int relidx, - FAR const struct symtab_s *exports, int nexports) +static int mod_relocate(FAR struct mod_loadinfo_s *loadinfo, int relidx) { FAR Elf32_Shdr *relsec = &loadinfo->shdr[relidx]; @@ -149,7 +148,7 @@ static int mod_relocate(FAR struct mod_loadinfo_s *loadinfo, int relidx, /* Get the value of the symbol (in sym.st_value) */ - ret = mod_symvalue(loadinfo, &sym, exports, nexports); + ret = mod_symvalue(loadinfo, &sym); if (ret < 0) { /* The special error -ESRCH is returned only in one condition: The @@ -200,8 +199,7 @@ static int mod_relocate(FAR struct mod_loadinfo_s *loadinfo, int relidx, return OK; } -static int mod_relocateadd(FAR struct mod_loadinfo_s *loadinfo, int relidx, - FAR const struct symtab_s *exports, int nexports) +static int mod_relocateadd(FAR struct mod_loadinfo_s *loadinfo, int relidx) { sdbg("Not implemented\n"); return -ENOSYS; @@ -216,7 +214,7 @@ static int mod_relocateadd(FAR struct mod_loadinfo_s *loadinfo, int relidx, * * Description: * Bind the imported symbol names in the loaded module described by - * 'loadinfo' using the exported symbol values provided by 'symtab'. + * 'loadinfo' using the exported symbol values provided by mod_setsymtab(). * * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on @@ -224,8 +222,7 @@ static int mod_relocateadd(FAR struct mod_loadinfo_s *loadinfo, int relidx, * ****************************************************************************/ -int mod_bind(FAR struct mod_loadinfo_s *loadinfo, - FAR const struct symtab_s *exports, int nexports) +int mod_bind(FAR struct mod_loadinfo_s *loadinfo) { int ret; int i; @@ -274,11 +271,11 @@ int mod_bind(FAR struct mod_loadinfo_s *loadinfo, if (loadinfo->shdr[i].sh_type == SHT_REL) { - ret = mod_relocate(loadinfo, i, exports, nexports); + ret = mod_relocate(loadinfo, i); } else if (loadinfo->shdr[i].sh_type == SHT_RELA) { - ret = mod_relocateadd(loadinfo, i, exports, nexports); + ret = mod_relocateadd(loadinfo, i); } if (ret < 0) diff --git a/sched/module/mod_insmod.c b/sched/module/mod_insmod.c index f0a96dabdb1..9242c5419ef 100644 --- a/sched/module/mod_insmod.c +++ b/sched/module/mod_insmod.c @@ -166,13 +166,16 @@ static void mod_dumpinitializer(mod_initializer_t initializer, * Verify that the file is an ELF module binary and, if so, load the * module into kernel memory and initialize it for use. * + * NOTE: mod_setsymtab had to have been called in board-specific OS logic + * prior to calling this function from application logic (perhaps via + * boardctl(BOARDIOC_OS_SYMTAB). Otherwise, insmod will be unable to + * resolve symbols in the OS module. + * * Input Parameters: * * filename - Full path to the module binary to be loaded * modulename - The name that can be used to refer to the module after * it has been loaded. - * exports - Table of exported symbols - * nexports - The number of symbols in exports[] * * Returned Value: * Zero (OK) on success. On any failure, -1 (ERROR) is returned the @@ -180,8 +183,7 @@ static void mod_dumpinitializer(mod_initializer_t initializer, * ****************************************************************************/ -int insmod(FAR const char *filename, FAR const char *modulename, - FAR const struct symtab_s *exports, int nexports) +int insmod(FAR const char *filename, FAR const char *modulename) { struct mod_loadinfo_s loadinfo; FAR struct module_s *modp; @@ -237,9 +239,9 @@ int insmod(FAR const char *filename, FAR const char *modulename, goto errout_with_registry_entry; } - /* Bind the program to the exported symbol table */ + /* Bind the program to the kernel symbol table */ - ret = mod_bind(&loadinfo, exports, nexports); + ret = mod_bind(&loadinfo); if (ret != 0) { sdbg("Failed to bind symbols program binary: %d\n", ret); diff --git a/sched/module/mod_symbols.c b/sched/module/mod_symbols.c index 0c60bbf981e..bc91a426e3e 100644 --- a/sched/module/mod_symbols.c +++ b/sched/module/mod_symbols.c @@ -261,8 +261,7 @@ int mod_readsym(FAR struct mod_loadinfo_s *loadinfo, int index, * ****************************************************************************/ -int mod_symvalue(FAR struct mod_loadinfo_s *loadinfo, FAR Elf32_Sym *sym, - FAR const struct symtab_s *exports, int nexports) +int mod_symvalue(FAR struct mod_loadinfo_s *loadinfo, FAR Elf32_Sym *sym) { FAR const struct symtab_s *symbol; uintptr_t secbase; @@ -306,9 +305,13 @@ int mod_symvalue(FAR struct mod_loadinfo_s *loadinfo, FAR Elf32_Sym *sym, /* Check if the base code exports a symbol of this name */ #ifdef CONFIG_SYMTAB_ORDEREDBYNAME - symbol = symtab_findorderedbyname(exports, (FAR char *)loadinfo->iobuffer, nexports); + symbol = symtab_findorderedbyname(g_mod_symtab, + (FAR char *)loadinfo->iobuffer, + g_mod_nsymbols); #else - symbol = symtab_findbyname(exports, (FAR char *)loadinfo->iobuffer, nexports); + symbol = symtab_findbyname(g_mod_symtab, + (FAR char *)loadinfo->iobuffer, + g_mod_nsymbols); #endif if (!symbol) { diff --git a/sched/module/mod_symtab.c b/sched/module/mod_symtab.c new file mode 100644 index 00000000000..0c0648f2944 --- /dev/null +++ b/sched/module/mod_symtab.c @@ -0,0 +1,110 @@ +/**************************************************************************** + * sched/module/mod_symtab.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include +#include + +#include "module.h" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +FAR const struct symtab_s *g_mod_symtab; +FAR int g_mod_nsymbols; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mod_getsymtab + * + * Description: + * Get the current kernel symbol table selection as an atomic operation. + * + * Input Parameters: + * symtab - The location to store the symbol table. + * nsymbols - The location to store the number of symbols in the symbol table. + * + * Returned Value: + * None + * + ****************************************************************************/ + +void mod_getsymtab(FAR const struct symtab_s **symtab, FAR int *nsymbols) +{ + DEBUGASSERT(symtab != NULL && nsymbols != NULL); + + /* Borrow the registry lock to assure atomic access */ + + mod_registry_lock(); + *symtab = g_mod_symtab; + *nsymbols = g_mod_nsymbols; + mod_registry_lock(); +} + +/**************************************************************************** + * Name: mod_setsymtab + * + * Description: + * Select a new kernel symbol table selection as an atomic operation. + * + * Input Parameters: + * symtab - The new symbol table. + * nsymbols - The number of symbols in the symbol table. + * + * Returned Value: + * None + * + ****************************************************************************/ + +void mod_setsymtab(FAR const struct symtab_s *symtab, int nsymbols) +{ + /* Borrow the registry lock to assure atomic access */ + + mod_registry_lock(); + g_mod_symtab = symtab; + g_mod_nsymbols = nsymbols; + mod_registry_lock(); +} diff --git a/sched/module/module.h b/sched/module/module.h index 6c9b6813b0e..9bccc0c6e1d 100644 --- a/sched/module/module.h +++ b/sched/module/module.h @@ -99,6 +99,13 @@ struct mod_loadinfo_s int filfd; /* Descriptor for the file being loaded */ }; +/**************************************************************************** + * Public Data + ****************************************************************************/ + +FAR const struct symtab_s *g_mod_symtab; +FAR int g_mod_nsymbols; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -154,7 +161,7 @@ int mod_load(FAR struct mod_loadinfo_s *loadinfo); * * Description: * Bind the imported symbol names in the loaded module described by - * 'loadinfo' using the exported symbol values provided by 'symtab'. + * 'loadinfo' using the exported symbol values provided by mod_setsymtab(). * * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on @@ -162,9 +169,7 @@ int mod_load(FAR struct mod_loadinfo_s *loadinfo); * ****************************************************************************/ -struct symtab_s; -int mod_bind(FAR struct mod_loadinfo_s *loadinfo, - FAR const struct symtab_s *exports, int nexports); +int mod_bind(FAR struct mod_loadinfo_s *loadinfo); /**************************************************************************** * Name: mod_unload @@ -290,8 +295,6 @@ int mod_readsym(FAR struct mod_loadinfo_s *loadinfo, int index, * Input Parameters: * loadinfo - Load state information * sym - Symbol table entry (value might be undefined) - * exports - The symbol table to use for resolving undefined symbols. - * nexports - Number of symbols in the symbol table. * * Returned Value: * 0 (OK) is returned on success and a negated errno is returned on @@ -305,8 +308,7 @@ int mod_readsym(FAR struct mod_loadinfo_s *loadinfo, int index, * ****************************************************************************/ -int mod_symvalue(FAR struct mod_loadinfo_s *loadinfo, FAR Elf32_Sym *sym, - FAR const struct symtab_s *exports, int nexports); +int mod_symvalue(FAR struct mod_loadinfo_s *loadinfo, FAR Elf32_Sym *sym); /**************************************************************************** * Name: mod_freebuffers