symtabs: improve handling of symbol lookups

When CONFIG_SYMTAB_ORDEREDBYNAME is selected most code will use
the ordered search function. When it is not selected no code will
use the ordered search function. This change merges the two
functions and varies its behaviour based on the config setting,
such that all callers can simply call the one search function
and get the best behaviour.

An additional configuration option allows leading underscores to
be stripped from symbols being relocated in loaded objects. This
allows toolchains which prefix C symbol with underscores to make
loadable ELF objects.
This commit is contained in:
Byron Ellacott
2021-03-15 19:35:47 +10:00
committed by Xiang Xiao
parent c80cdf06c2
commit c9db653c8d
11 changed files with 94 additions and 178 deletions
-11
View File
@@ -174,15 +174,9 @@ static int modlib_symcallback(FAR struct module_s *modp, FAR void *arg)
/* Check if this module exports a symbol of that name */
#ifdef CONFIG_SYMTAB_ORDEREDBYNAME
exportinfo->symbol = symtab_findorderedbyname(modp->modinfo.exports,
exportinfo->name,
modp->modinfo.nexports);
#else
exportinfo->symbol = symtab_findbyname(modp->modinfo.exports,
exportinfo->name,
modp->modinfo.nexports);
#endif
if (exportinfo->symbol != NULL)
{
@@ -381,13 +375,8 @@ int modlib_symvalue(FAR struct module_s *modp,
if (symbol == NULL)
{
modlib_getsymtab(&symbol, &nsymbols);
#ifdef CONFIG_SYMTAB_ORDEREDBYNAME
symbol = symtab_findorderedbyname(symbol, exportinfo.name,
nsymbols);
#else
symbol = symtab_findbyname(symbol, exportinfo.name,
nsymbols);
#endif
}
/* Was the symbol found from any exporter? */
+1 -2
View File
@@ -20,8 +20,7 @@
# Symbol table source files
CSRCS += symtab_findbyname.c symtab_findbyvalue.c
CSRCS += symtab_findorderedbyname.c symtab_sortbyname.c
CSRCS += symtab_findbyname.c symtab_findbyvalue.c symtab_sortbyname.c
# Add the symtab directory to the build
+59
View File
@@ -53,7 +53,65 @@ FAR const struct symtab_s *
symtab_findbyname(FAR const struct symtab_s *symtab,
FAR const char *name, int nsyms)
{
#ifdef CONFIG_SYMTAB_ORDEREDBYNAME
int low = 0;
int high = nsyms - 1;
int mid;
int cmp;
#endif
#ifdef CONFIG_SYMTAB_DECORATED
if (name[0] == '_')
{
name++;
}
#endif
DEBUGASSERT(symtab != NULL && name != NULL);
#ifdef CONFIG_SYMTAB_ORDEREDBYNAME
while (low < high)
{
/* Compare the name to the one in the middle. (or just below
* the middle in the case where one is even and one is odd).
*/
mid = (low + high) >> 1;
cmp = strcmp(name, symtab[mid].sym_name);
if (cmp < 0)
{
/* name < symtab[mid].sym_name
*
* NOTE: Because of truncation in the calculation of 'mid'.
* 'mid' could be equal to 'low'
*/
high = mid > low ? mid - 1 : low;
}
else if (cmp > 0)
{
/* name > symtab[mid].sym_name */
low = mid + 1;
}
else
{
/* symtab[mid].sym_name == name */
return &symtab[mid];
}
}
/* low == high... One final check. We might not have actually tested
* the final symtab[] name.
*
* Example: Only the last pass through loop, suppose low = 1, high = 2,
* mid = 1, and symtab[high].sym_name == name. Then we would get here
* with low = 2, high = 2, but symtab[2].sym_name was never tested.
*/
return strcmp(name, symtab[low].sym_name) == 0 ? &symtab[low] : NULL;
#else
for (; nsyms > 0; symtab++, nsyms--)
{
if (strcmp(name, symtab->sym_name) == 0)
@@ -63,4 +121,5 @@ symtab_findbyname(FAR const struct symtab_s *symtab,
}
return NULL;
#endif
}
+2 -2
View File
@@ -41,12 +41,12 @@
* 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
* ordered with respect to symbol value 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.
* value is found; NULL is returned if the entry is not found.
*
****************************************************************************/
-110
View File
@@ -1,110 +0,0 @@
/****************************************************************************
* libs/libc/symtab/symtab_findorderedbyname.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <string.h>
#include <debug.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/symtab.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* 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.
*
* This function is a lot larger than symbtab_findbyname(). This function
* not be used, unless the symbol table is large and the performance
* benefit is worth the increased size.
*
* 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)
{
int low = 0;
int high = nsyms - 1;
int mid;
int cmp;
/* Loop until the range has been isolated to a single symbol table
* entry that may or may not match the search name.
*/
DEBUGASSERT(symtab != NULL && name != NULL);
while (low < high)
{
/* Compare the name to the one in the middle. (or just below
* the middle in the case where one is even and one is odd).
*/
mid = (low + high) >> 1;
cmp = strcmp(name, symtab[mid].sym_name);
if (cmp < 0)
{
/* name < symtab[mid].sym_name
*
* NOTE: Because of truncation in the calculation of 'mid'.
* 'mid' could be equal to 'low'
*/
high = mid > low ? mid - 1 : low;
}
else if (cmp > 0)
{
/* name > symtab[mid].sym_name */
low = mid + 1;
}
else
{
/* symtab[mid].sym_name == name */
return &symtab[mid];
}
}
/* low == high... One final check. We might not have actually tested
* the final symtab[] name.
*
* Example: Only the last pass through loop, suppose low = 1, high = 2,
* mid = 1, and symtab[high].sym_name == name. Then we would get here
* with low = 2, high = 2, but symtab[2].sym_name was never tested.
*/
return strcmp(name, symtab[low].sym_name) == 0 ? &symtab[low] : NULL;
}
+17
View File
@@ -94,6 +94,23 @@ config EXECFUNCS_SYSTEM_SYMTAB
endif # EXECFUNCS_HAVE_SYMTAB
endif # LIBC_EXECFUNCS
config SYMTAB_ORDEREDBYNAME
bool "Symbol Tables Ordered by Name"
default n
---help---
Select if the symbol table is ordered by symbol name. In this case,
the logic can perform faster lookups using a binary search.
Otherwise, the symbol table is assumed to be un-ordered and only
slow, linear searches are supported.
config SYMTAB_DECORATED
bool "Symbols are decorated with leading underscores"
default n
---help---
Select if symbols in relocatable objects are decorated with a leading
underscore. This option will remove the underscore from symbol names
when relocating a loadable object.
config POSIX_SPAWN_PROXY_STACKSIZE
int "Spawn Stack Size"
default 1024