C library: Add framework that may (or may not) eventually support shared libraries.

This commit is contained in:
Gregory Nutt 2017-01-22 09:14:18 -06:00
parent ba2b345990
commit 2a284a95b1
9 changed files with 471 additions and 3 deletions

View File

@ -41,6 +41,7 @@
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
/****************************************************************************
* Pre-processor Definitons
@ -200,6 +201,7 @@ extern "C"
* information will be available through dlerror().
*
* Reference: OpenGroup.org
*
* ****************************************************************************/
FAR void *dlopen(FAR const char *file, int mode);
@ -232,7 +234,8 @@ FAR void *dlopen(FAR const char *file, int mode);
* information will be available through dlerror().
*
* Reference: OpenGroup.org
* ****************************************************************************/
*
****************************************************************************/
FAR void *dlsym(FAR void *handle, FAR const char *name);
@ -279,7 +282,7 @@ FAR void *dlsym(FAR void *handle, FAR const char *name);
int dlclose(FAR void *handle);
/****************************************************************************
* Name:
* Name: dlerror
*
* Description:
* dlerror() returns a null-terminated character string (with no trailing
@ -296,7 +299,8 @@ int dlclose(FAR void *handle);
* Returned Value:
*
* Reference: OpenGroup.org
* ****************************************************************************/
*
****************************************************************************/
FAR char *dlerror(void);

View File

@ -32,6 +32,7 @@ config LIB_HOMEDIR
---help---
The home directory to use with operations like such as 'cd ~'
source libc/dllfcn/Kconfig
source libc/math/Kconfig
source libc/machine/Kconfig

View File

@ -64,6 +64,7 @@ VPATH := .
include aio/Make.defs
include audio/Make.defs
include dirent/Make.defs
include dllfcn/Make.defs
include fixedmath/Make.defs
include hex2bin/Make.defs
include libgen/Make.defs

14
libc/dllfcn/Kconfig Normal file
View File

@ -0,0 +1,14 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config LIBC_DLLFCN
bool "Shared library support"
default n
select LIBC_ARCH_ELF
depends on EXPERIMENTAL
---help---
Enabled support for user-space shared libraries.
A work in progress, hence, marked EXPERIMENTAL

47
libc/dllfcn/Make.defs Normal file
View File

@ -0,0 +1,47 @@
############################################################################
# libc/dllfcn/Make.defs
#
# Copyright (C) 2017 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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.
#
############################################################################
ifeq ($(CONFIG_LIBC_DLLFCN),y)
# Add the dllfcn.h files to the build
CSRCS += lib_dlopen.c lib_dlclose.c lib_dlsym.c lib_dlerror.c
# Add the dllfcn.h directory to the build
DEPPATH += --dep-path dllfcn
VPATH += :dllfcn
endif

93
libc/dllfcn/lib_dlclose.c Normal file
View File

@ -0,0 +1,93 @@
/****************************************************************************
* libc/dllfcn/lib_dlclose.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <dllfcn.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dlclose
*
* Description:
* dlclose() is used to inform the system that the object referenced by a
* handle returned from a previous dlopen() invocation is no longer needed
* by the application.
*
* The use of dlclose() reflects a statement of intent on the part of the
* process, but does not create any requirement upon the implementation,
* such as removal of the code or symbols referenced by handle. Once an
* object has been closed using dlclose() an application should assume
* that its symbols are no longer available to dlsym(). All objects loaded
* automatically as a result of invoking dlopen() on the referenced object
* are also closed.
*
* Although a dlclose() operation is not required to remove structures
* from an address space, neither is an implementation prohibited from
* doing so. The only restriction on such a removal is that no object will
* be removed to which references have been relocated, until or unless all
* such references are removed. For instance, an object that had been
* loaded with a dlopen() operation specifying the RTLD_GLOBAL flag might
* provide a target for dynamic relocations performed in the processing of
* other objects - in such environments, an application may assume that no
* relocation, once made, will be undone or remade unless the object
* requiring the relocation has itself been removed.
*
* Input Parameters:
* handle - The opaque, non-NULL value returned by a previous successful
* call to dlopen().
*
* Returned Value:
* If the referenced object was successfully closed, dlclose() returns 0.
* If the object could not be closed, or if handle does not refer to an
* open object, dlclose() returns a non-zero value. More detailed
* diagnostic information will be available through dlerror().
*
* Reference: OpenGroup.org
*
****************************************************************************/
int dlclose(FAR void *handle)
{
#warning Missing logic
return 1;
}

73
libc/dllfcn/lib_dlerror.c Normal file
View File

@ -0,0 +1,73 @@
/****************************************************************************
* libc/dllfcn/lib_dlerror.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <dllfcn.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dlerror
*
* Description:
* dlerror() returns a null-terminated character string (with no trailing
* newline) that describes the last error that occurred during dynamic
* linking processing. If no dynamic linking errors have occurred since
* the last invocation of dlerror(), dlerror() returns NULL. Thus,
* invoking dlerror() a second time, immediately following a prior
* invocation, will result in NULL being returned.
*
* Input Parameters:
* If successful, dlerror() returns a null-terminated character string.
* Otherwise, NULL is returned.
*
* Returned Value:
*
* Reference: OpenGroup.org
*
****************************************************************************/
FAR char *dlerror(void)
{
#warning Missing logic
return NULL;
}

152
libc/dllfcn/lib_dlopen.c Normal file
View File

@ -0,0 +1,152 @@
/****************************************************************************
* libc/dllfcn/lib_dlopen.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <dllfcn.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dlopen
*
* Description:
* dlopen() makes an executable object file specified by file available to
* the calling program. The class of files eligible for this operation and
* the manner of their construction are specified by the implementation,
* though typically such files are executable objects such as shared
* libraries, relocatable files or programs. Note that some implementations
* permit the construction of dependencies between such objects that are
* embedded within files. In such cases, a dlopen() operation will load
* such dependencies in addition to the object referenced by file.
* Implementations may also impose specific constraints on the construction
* of programs that can employ dlopen() and its related services.
*
* If a file is specified in multiple dlopen() invocations, mode is
* interpreted at each invocation. Note, however, that once RTLD_NOW has
* been specified all relocations will have been completed rendering
* further RTLD_NOW operations redundant and any further RTLD_LAZY
* operations irrelevant. Similarly note that once RTLD_GLOBAL has been
* specified the object will maintain the RTLD_GLOBAL status regardless
* of any previous or future specification of RTLD_LOCAL, so long as the
* object remains in the address space (see dlclose()).
*
* Symbols introduced into a program through calls to dlopen() may be
* used in relocation activities. Symbols so introduced may duplicate
* symbols already defined by the program or previous dlopen()
* operations. To resolve the ambiguities such a situation might
* present, the resolution of a symbol reference to symbol definition is
* based on a symbol resolution order. Two such resolution orders are
* defined: load or dependency ordering. Load order establishes an
* ordering among symbol definitions, such that the definition first
* loaded (including definitions from the image file and any dependent
* objects loaded with it) has priority over objects added later (via
* dlopen()). Load ordering is used in relocation processing. Dependency
* ordering uses a breadth-first order starting with a given object,
* then all of its dependencies, then any dependents of those, iterating
* until all dependencies are satisfied. With the exception of the global
* symbol object obtained via a dlopen() operation on a file of 0,
* dependency ordering is used by the dlsym() function. Load ordering is
* used in dlsym() operations upon the global symbol object.
*
* When an object is first made accessible via dlopen() it and its
* dependent objects are added in dependency order. Once all the objects
* are added, relocations are performed using load order. Note that if an
* object or its dependencies had been previously loaded, the load and
* dependency orders may yield different resolutions.
*
* The symbols introduced by dlopen() operations, and available through
* dlsym() are at a minimum those which are exported as symbols of global
* scope by the object. Typically such symbols will be those that were
* specified in (for example) C source code as having extern linkage. The
* precise manner in which an implementation constructs the set of
* exported symbols for a dlopen() object is specified by that
* implementation.
*
* Input Parameters:
* file - Used to construct a pathname to the object file. If file
* contains a slash character, the file argument is used as the
* pathname for the file. Otherwise, file is used in an
* implementation-dependent manner to yield a pathname.
*
* If the value of file is 0, dlopen() provides a handle on a
* global symbol object. This object provides access to the symbols
* from an ordered set of objects consisting of the original
* program image file, together with any objects loaded at program
* startup as specified by that process image file (for example,
* shared libraries), and the set of objects loaded using a
* dlopen() operation together with the RTLD_GLOBAL flag. As the
* latter set of objects can change during execution, the set
* identified by handle can also change dynamically.
*
* Only a single copy of an object file is brought into the address
* space, even if dlopen() is invoked multiple times in reference
* to the file, and even if different pathnames are used to
* reference the file.
* mode - Describes how dlopen() will operate upon file with respect to
* the processing of relocations and the scope of visibility of the
* symbols provided within file. When an object is brought into the
* address space of a process, it may contain references to symbols
* whose addresses are not known until the object is loaded. These
* references must be relocated before the symbols can be accessed.
* The mode parameter governs when these relocations take place.
* See definitions above for values of the mode parameter:.
*
* Returned Value:
* A successful dlopen() returns a handle which the caller may use on
* subsequent calls to dlsym() and dlclose(). The value of this handle
* should not be interpreted in any way by the caller.
*
* If file cannot be found, cannot be opened for reading, is not of an
* appropriate object format for processing by dlopen(), or if an error
* occurs during the process of loading file or relocating its symbolic
* references, dlopen() will return NULL. More detailed diagnostic
* information will be available through dlerror().
*
* Reference: OpenGroup.org
*
****************************************************************************/
FAR void *dlopen(FAR const char *file, int mode)
{
#warning Missing logic
return NULL;
}

83
libc/dllfcn/lib_dlsym.c Normal file
View File

@ -0,0 +1,83 @@
/****************************************************************************
* libc/dllfcn/lib_dlsym.c
*
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <dllfcn.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dlsym
*
* Description:
* dlsym() allows a process to obtain the address of a symbol defined
* within an object made accessible through a dlopen() call. handle is the
* value returned from a call to dlopen() (and which has not since been
* released via a call to dlclose()), name is the symbol's name as a
* character string.
*
* dlsym() will search for the named symbol in all objects loaded
* automatically as a result of loading the object referenced by handle
* (see dlopen()). Load ordering is used in dlsym() operations upon the
* global symbol object. The symbol resolution algorithm used will be
* dependency order as described in dlopen().
*
* Input Parameters:
* handle - The opaque, non-NULL value returned by a previous successful
* call to dlopen().
* name - A pointer to the symbol name string.
*
* Returned Value:
* If handle does not refer to a valid object opened by dlopen(), or if
* the named symbol cannot be found within any of the objects associated
* with handle, dlsym() will return NULL. More detailed diagnostic
* information will be available through dlerror().
*
* Reference: OpenGroup.org
*
****************************************************************************/
FAR void *dlsym(FAR void *handle, FAR const char *name)
{
#warning Missing logic
return NULL;
}