2020-04-13 16:50:14 +02:00
|
|
|
/****************************************************************************
|
2022-09-26 08:22:03 +02:00
|
|
|
* libs/libc/modlib/modlib_loadhdrs.c
|
2020-04-13 16:50:14 +02: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
|
2020-04-13 16:50:14 +02:00
|
|
|
*
|
2020-04-13 17:03:46 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2020-04-13 16:50:14 +02: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.
|
2020-04-13 16:50:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/lib/modlib.h>
|
|
|
|
|
|
|
|
#include "libc.h"
|
|
|
|
#include "modlib/modlib.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2022-09-26 08:22:03 +02:00
|
|
|
* Name: modlib_loadhdrs
|
2020-04-13 16:50:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2022-09-26 08:22:03 +02:00
|
|
|
* Loads program and section headers into memory.
|
2020-04-13 16:50:14 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-09-26 08:22:03 +02:00
|
|
|
int modlib_loadhdrs(FAR struct mod_loadinfo_s *loadinfo)
|
2020-04-13 16:50:14 +02:00
|
|
|
{
|
|
|
|
size_t shdrsize;
|
2022-09-26 08:22:03 +02:00
|
|
|
size_t phdrsize;
|
2020-04-13 16:50:14 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo->shdr == NULL);
|
2022-09-26 08:22:03 +02:00
|
|
|
DEBUGASSERT(loadinfo->phdr == NULL);
|
2020-04-13 16:50:14 +02:00
|
|
|
|
|
|
|
/* Verify that there are sections */
|
|
|
|
|
|
|
|
if (loadinfo->ehdr.e_shnum < 1)
|
|
|
|
{
|
|
|
|
berr("ERROR: No sections(?)\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total size of the section header table */
|
|
|
|
|
|
|
|
shdrsize = (size_t)loadinfo->ehdr.e_shentsize *
|
|
|
|
(size_t)loadinfo->ehdr.e_shnum;
|
|
|
|
if (loadinfo->ehdr.e_shoff + shdrsize > loadinfo->filelen)
|
|
|
|
{
|
2022-07-07 18:01:22 +02:00
|
|
|
berr("ERROR: Insufficient space in file for section header table\n");
|
2020-04-13 16:50:14 +02:00
|
|
|
return -ESPIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory to hold a working copy of the sector header table */
|
|
|
|
|
2023-07-07 01:15:59 +02:00
|
|
|
loadinfo->shdr = (FAR Elf_Shdr *)lib_malloc(shdrsize);
|
2020-04-13 16:50:14 +02:00
|
|
|
if (!loadinfo->shdr)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
berr("ERROR: Failed to allocate the section header table. Size: %zu\n",
|
|
|
|
shdrsize);
|
2020-04-13 16:50:14 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the section header table into memory */
|
|
|
|
|
|
|
|
ret = modlib_read(loadinfo, (FAR uint8_t *)loadinfo->shdr, shdrsize,
|
|
|
|
loadinfo->ehdr.e_shoff);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read section header table: %d\n", ret);
|
|
|
|
}
|
|
|
|
|
2023-07-07 01:15:59 +02:00
|
|
|
if (loadinfo->ehdr.e_phnum > 0)
|
2022-09-26 08:22:03 +02:00
|
|
|
{
|
2023-07-07 01:15:59 +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("ERROR: Insufficent space for program header table\n");
|
|
|
|
return -ESPIPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory to hold a working copy of the program header table */
|
|
|
|
|
|
|
|
loadinfo->phdr = (FAR Elf_Phdr *)lib_malloc(phdrsize);
|
|
|
|
if (!loadinfo->phdr)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
lib_free(loadinfo->shdr);
|
2023-07-07 01:15:59 +02:00
|
|
|
berr("ERROR: Failed to allocate the program header table."
|
2023-07-02 18:11:02 +02:00
|
|
|
"Size: %zu\n", phdrsize);
|
2023-07-07 01:15:59 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the program header table into memory */
|
|
|
|
|
|
|
|
ret = modlib_read(loadinfo, (FAR uint8_t *)loadinfo->phdr, phdrsize,
|
|
|
|
loadinfo->ehdr.e_phoff);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to read program header table: %d\n", ret);
|
2023-07-02 18:11:02 +02:00
|
|
|
lib_free(loadinfo->phdr);
|
|
|
|
lib_free(loadinfo->shdr);
|
2023-07-07 01:15:59 +02:00
|
|
|
}
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
2023-07-07 01:15:59 +02:00
|
|
|
else
|
2022-09-26 08:22:03 +02:00
|
|
|
{
|
2023-07-07 01:15:59 +02:00
|
|
|
loadinfo->phdr = NULL;
|
2022-09-26 08:22:03 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:50:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|