2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/modlib/modlib_sections.c
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02:00
|
|
|
* 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
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
2020-04-13 17:03:46 +02:00
|
|
|
* 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.
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
#include "libc.h"
|
|
|
|
#include "modlib/modlib.h"
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 18:17:29 +01:00
|
|
|
* Name: modlib_sectname
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the symbol name in loadinfo->iobuffer[].
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
static inline int modlib_sectname(FAR struct mod_loadinfo_s *loadinfo,
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR const Elf_Shdr *shdr)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shstr;
|
2015-12-10 16:53:31 +01:00
|
|
|
off_t offset;
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t bytesread = 0;
|
2015-12-10 16:53:31 +01:00
|
|
|
int shstrndx;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the section header table index of the entry associated with the
|
|
|
|
* section name string table. If the file has no section name string table,
|
|
|
|
* this member holds the value SH_UNDEF.
|
|
|
|
*/
|
|
|
|
|
|
|
|
shstrndx = loadinfo->ehdr.e_shstrndx;
|
|
|
|
if (shstrndx == SHN_UNDEF)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: No section header string table\n");
|
2015-12-10 16:53:31 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-07-15 16:24:43 +02:00
|
|
|
/* Allocate an I/O buffer. This buffer is used by modlib_sectname() to
|
|
|
|
* accumulate the variable length symbol name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = modlib_allocbuffer(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: modlib_allocbuffer failed: %d\n", ret);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* Get the section name string table section header */
|
|
|
|
|
|
|
|
shstr = &loadinfo->shdr[shstrndx];
|
|
|
|
|
|
|
|
/* Get the file offset to the string that is the name of the section. This
|
|
|
|
* is the sum of:
|
|
|
|
*
|
|
|
|
* shstr->sh_offset: The file offset to the first byte of the section
|
|
|
|
* header string table data.
|
|
|
|
* shdr->sh_name: The offset to the name of the section in the section
|
|
|
|
* name table
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = shstr->sh_offset + shdr->sh_name;
|
|
|
|
|
|
|
|
/* Loop until we get the entire section name into memory */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
FAR uint8_t *buffer = &loadinfo->iobuffer[bytesread];
|
|
|
|
size_t readlen = loadinfo->buflen - bytesread;
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* Get the number of bytes to read */
|
|
|
|
|
|
|
|
if (offset + readlen > loadinfo->filelen)
|
|
|
|
{
|
|
|
|
if (loadinfo->filelen <= offset)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: At end of file\n");
|
2015-12-10 16:53:31 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
readlen = loadinfo->filelen - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read that number of bytes into the array */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_read(loadinfo, buffer, readlen, offset);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: Failed to read section name: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesread += readlen;
|
|
|
|
|
|
|
|
/* Did we read the NUL terminator? */
|
|
|
|
|
|
|
|
if (memchr(buffer, '\0', readlen) != NULL)
|
|
|
|
{
|
|
|
|
/* Yes, the buffer contains a NUL terminator. */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. then we have to read more */
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
ret = modlib_reallocbuffer(loadinfo, CONFIG_MODLIB_BUFFERINCR);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: mod_reallocbuffer failed: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will not get here */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 18:17:29 +01:00
|
|
|
* Name: modlib_findsection
|
2015-12-10 16:53:31 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A section by its name.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* loadinfo - Load state information
|
|
|
|
* sectname - Name of the section to find
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the index to the section is returned; A negated errno value
|
|
|
|
* is returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 18:17:29 +01:00
|
|
|
int modlib_findsection(FAR struct mod_loadinfo_s *loadinfo,
|
|
|
|
FAR const char *sectname)
|
2015-12-10 16:53:31 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-04-13 05:54:29 +02:00
|
|
|
/* Search through the shdr[] array in loadinfo for a section named
|
|
|
|
* 'sectname'
|
|
|
|
*/
|
2015-12-10 16:53:31 +01:00
|
|
|
|
|
|
|
for (i = 0; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
FAR const Elf_Shdr *shdr = &loadinfo->shdr[i];
|
|
|
|
|
2015-12-10 16:53:31 +01:00
|
|
|
/* Get the name of this section */
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
int ret = modlib_sectname(loadinfo, shdr);
|
2015-12-10 16:53:31 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: modlib_sectname failed: %d\n", ret);
|
2015-12-10 16:53:31 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the name of this section is 'sectname' */
|
|
|
|
|
2018-06-01 18:10:17 +02:00
|
|
|
binfo("%d. Comparing \"%s\" and .\"%s\"\n",
|
2015-12-10 16:53:31 +01:00
|
|
|
i, loadinfo->iobuffer, sectname);
|
|
|
|
|
|
|
|
if (strcmp((FAR const char *)loadinfo->iobuffer, sectname) == 0)
|
|
|
|
{
|
|
|
|
/* We found it... return the index */
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We failed to find a section with this name. */
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|