2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_read.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-24 18:46:12 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-24 18:46:12 +02: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-24 18:46:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2023-07-02 18:11:02 +02:00
|
|
|
#include <inttypes.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-10-11 20:13:41 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-24 18:46:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#undef ELF_DUMP_READDATA /* Define to dump all file data read */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_dumpreaddata
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
#ifdef ELF_DUMP_READDATA
|
|
|
|
static inline void elf_dumpreaddata(FAR char *buffer, size_t buflen)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
FAR uint32_t *buf32 = (FAR uint32_t *)buffer;
|
2023-07-02 18:11:02 +02:00
|
|
|
size_t i;
|
|
|
|
size_t j;
|
2012-10-24 18:46:12 +02:00
|
|
|
|
|
|
|
for (i = 0; i < buflen; i += 32)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
syslog(LOG_DEBUG, "%04zx:", i);
|
2012-10-24 18:46:12 +02:00
|
|
|
for (j = 0; j < 32; j += sizeof(uint32_t))
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
syslog(LOG_DEBUG, " %08" PRIx32, *buf32++);
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
2014-10-08 23:34:51 +02:00
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "\n");
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define elf_dumpreaddata(b,n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_read
|
|
|
|
*
|
|
|
|
* Description:
|
2012-12-19 18:54:26 +01:00
|
|
|
* Read 'readsize' bytes from the object file at 'offset'. The data is
|
|
|
|
* read into 'buffer.' If 'buffer' is part of the ELF address environment,
|
2020-02-22 19:31:14 +01:00
|
|
|
* then the caller is responsible for assuring that that address
|
2012-12-19 18:54:26 +01:00
|
|
|
* environment is in place before calling this function (i.e., that
|
2014-08-24 14:42:11 +02:00
|
|
|
* elf_addrenv_select() has been called if CONFIG_ARCH_ADDRENV=y).
|
2012-10-24 18:46:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 05:13:11 +02:00
|
|
|
int elf_read(FAR struct elf_loadinfo_s *loadinfo, FAR uint8_t *buffer,
|
|
|
|
size_t readsize, off_t offset)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2023-08-22 19:11:24 +02:00
|
|
|
size_t nsize = readsize; /* Bytes to read from the object file */
|
|
|
|
ssize_t nbytes; /* Number of bytes read */
|
|
|
|
off_t rpos; /* Position returned by lseek */
|
|
|
|
int ret = OK; /* Return value */
|
|
|
|
|
|
|
|
#ifdef CONFIG_ARCH_USE_COPY_SECTION
|
|
|
|
FAR uint8_t *dest = buffer; /* Destination address - `buffer` */
|
|
|
|
|
|
|
|
/* Redirect `buffer` to temporary allocated memory */
|
|
|
|
|
|
|
|
buffer = kmm_malloc(readsize);
|
|
|
|
if (buffer == NULL)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to allocate memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#endif
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
binfo("Read %zu bytes from offset %" PRIdOFF "\n", readsize, offset);
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-10-25 05:13:11 +02:00
|
|
|
/* Loop until all of the requested data has been read. */
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-10-25 05:13:11 +02:00
|
|
|
while (readsize > 0)
|
2012-10-24 18:46:12 +02:00
|
|
|
{
|
2012-10-25 05:13:11 +02:00
|
|
|
/* Seek to the next read position */
|
|
|
|
|
2021-02-15 18:15:08 +01:00
|
|
|
rpos = file_seek(&loadinfo->file, offset, SEEK_SET);
|
2012-10-24 18:46:12 +02:00
|
|
|
if (rpos != offset)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
berr("Failed to seek to position %" PRIdOFF ": %" PRIdOFF "\n",
|
|
|
|
offset, rpos);
|
2023-08-22 19:11:24 +02:00
|
|
|
ret = rpos;
|
|
|
|
goto errout;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file data at offset into the user buffer */
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
nbytes = file_read(&loadinfo->file,
|
|
|
|
buffer + nsize - readsize, readsize);
|
2017-10-11 20:13:41 +02:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* EINTR just means that we received a signal */
|
|
|
|
|
|
|
|
if (nbytes != -EINTR)
|
|
|
|
{
|
2023-07-02 18:11:02 +02:00
|
|
|
berr("Read from offset %" PRIdOFF " failed: %zd\n",
|
|
|
|
offset, nbytes);
|
2023-08-22 19:11:24 +02:00
|
|
|
ret = nbytes;
|
|
|
|
goto errout;
|
2017-10-11 20:13:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nbytes == 0)
|
|
|
|
{
|
|
|
|
berr("Unexpected end of file\n");
|
2023-08-22 19:11:24 +02:00
|
|
|
ret = -ENODATA;
|
|
|
|
goto errout;
|
2017-10-11 20:13:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
readsize -= nbytes;
|
|
|
|
offset += nbytes;
|
|
|
|
}
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 19:11:24 +02:00
|
|
|
#ifdef CONFIG_ARCH_USE_COPY_SECTION
|
|
|
|
/* Copy the requested data from temporary memory to destination */
|
|
|
|
|
|
|
|
ret = up_copy_section(dest, buffer, nsize);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Failed to copy section at offset %"PRIdOFF"\n", offset);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-07-02 18:11:02 +02:00
|
|
|
elf_dumpreaddata(buffer, nsize);
|
2023-08-22 19:11:24 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
#ifdef CONFIG_ARCH_USE_COPY_SECTION
|
|
|
|
/* Free the temporary memory */
|
|
|
|
|
|
|
|
kmm_free(buffer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|