2012-10-29 20:32:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_sections.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01: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
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01: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.
|
2012-10-29 20:32:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/binfmt/elf.h>
|
|
|
|
|
|
|
|
#include "libelf.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-29 20:32:05 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_sectname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the symbol name in loadinfo->iobuffer[].
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int elf_sectname(FAR struct elf_loadinfo_s *loadinfo,
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR const Elf_Shdr *shdr)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shstr;
|
2012-10-29 20:32:05 +01:00
|
|
|
off_t offset;
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t bytesread = 0;
|
2012-10-29 20:32:05 +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)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("No section header string table\n");
|
2012-10-29 20:32:05 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-07-15 16:11:06 +02:00
|
|
|
/* Allocate an I/O buffer if necessary. This buffer is used by
|
|
|
|
* elf_sectname() to accumulate the variable length symbol name.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = elf_allocbuffer(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("elf_allocbuffer failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-29 20:32:05 +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 */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
for (; ; )
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
FAR uint8_t *buffer = &loadinfo->iobuffer[bytesread];
|
|
|
|
size_t readlen = loadinfo->buflen - bytesread;
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Get the number of bytes to read */
|
|
|
|
|
|
|
|
if (offset + readlen > loadinfo->filelen)
|
|
|
|
{
|
2014-02-11 01:08:49 +01:00
|
|
|
if (loadinfo->filelen <= offset)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("At end of file\n");
|
2012-10-29 20:32:05 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-02-11 01:08:49 +01:00
|
|
|
|
|
|
|
readlen = loadinfo->filelen - offset;
|
2012-10-29 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read that number of bytes into the array */
|
|
|
|
|
2015-05-08 07:00:59 +02:00
|
|
|
ret = elf_read(loadinfo, buffer, readlen, offset + bytesread);
|
2012-10-29 20:32:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to read section name\n");
|
2012-10-29 20:32:05 +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 */
|
|
|
|
|
|
|
|
ret = elf_reallocbuffer(loadinfo, CONFIG_ELF_BUFFERINCR);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("elf_reallocbuffer failed: %d\n", ret);
|
2012-10-29 20:32:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We will not get here */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-08-21 14:28:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadphdrs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads program headers into memory.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_loadphdrs(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
size_t phdrsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo->phdr == NULL);
|
|
|
|
|
|
|
|
/* Verify that there are programs */
|
|
|
|
|
|
|
|
if (loadinfo->ehdr.e_phnum < 1)
|
|
|
|
{
|
2023-09-04 10:55:35 +02:00
|
|
|
binfo("No programs(?)\n");
|
|
|
|
return 0;
|
2023-08-21 14:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total size of the program header table */
|
|
|
|
|
|
|
|
phdrsize = (size_t)loadinfo->ehdr.e_phentsize *
|
|
|
|
(size_t)loadinfo->ehdr.e_phnum;
|
|
|
|
if (loadinfo->ehdr.e_phoff + phdrsize > loadinfo->filelen)
|
|
|
|
{
|
|
|
|
berr("Insufficient space in file for program header table\n");
|
|
|
|
return -ESPIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory to hold a working copy of the program header table */
|
|
|
|
|
|
|
|
loadinfo->phdr = (FAR FAR Elf_Phdr *)kmm_malloc(phdrsize);
|
|
|
|
if (!loadinfo->phdr)
|
|
|
|
{
|
|
|
|
berr("Failed to allocate the program header table. Size: %ld\n",
|
|
|
|
(long)phdrsize);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the program header table into memory */
|
|
|
|
|
|
|
|
ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->phdr, phdrsize,
|
|
|
|
loadinfo->ehdr.e_phoff);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("Failed to read program header table: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadshdrs
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Loads section headers into memory.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_loadshdrs(FAR struct elf_loadinfo_s *loadinfo)
|
|
|
|
{
|
|
|
|
size_t shdrsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo->shdr == NULL);
|
|
|
|
|
|
|
|
/* Verify that there are sections */
|
|
|
|
|
|
|
|
if (loadinfo->ehdr.e_shnum < 1)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("No sections(?)\n");
|
2012-10-29 20:32:05 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total size of the section header table */
|
|
|
|
|
2020-03-26 07:54:27 +01:00
|
|
|
shdrsize = (size_t)loadinfo->ehdr.e_shentsize *
|
|
|
|
(size_t)loadinfo->ehdr.e_shnum;
|
2014-04-12 20:53:19 +02:00
|
|
|
if (loadinfo->ehdr.e_shoff + shdrsize > loadinfo->filelen)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
berr("Insufficient space in file for section header table\n");
|
2012-10-29 20:32:05 +01:00
|
|
|
return -ESPIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory to hold a working copy of the sector header table */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
loadinfo->shdr = kmm_malloc(shdrsize);
|
2012-10-29 20:32:05 +01:00
|
|
|
if (!loadinfo->shdr)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
berr("Failed to allocate the section header table. Size: %zu\n",
|
|
|
|
shdrsize);
|
2012-10-29 20:32:05 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the section header table into memory */
|
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->shdr, shdrsize,
|
|
|
|
loadinfo->ehdr.e_shoff);
|
2012-10-29 20:32:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to read section header table: %d\n", ret);
|
2012-10-29 20:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_findsection
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_findsection(FAR struct elf_loadinfo_s *loadinfo,
|
|
|
|
FAR const char *sectname)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-04-22 10:24:15 +02:00
|
|
|
/* Search through the shdr[] array in loadinfo for a section named
|
|
|
|
* 'sectname'
|
|
|
|
*/
|
2012-10-29 20:32:05 +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];
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Get the name of this section */
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
int ret = elf_sectname(loadinfo, shdr);
|
2012-10-29 20:32:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("elf_sectname failed: %d\n", ret);
|
2012-10-29 20:32:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Check if the name of this section is 'sectname' */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("%d. Comparing \"%s\" and .\"%s\"\n",
|
2012-10-29 20:32:05 +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;
|
|
|
|
}
|