2012-10-25 21:21:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_ctors.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-25 21:21:47 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-10-25 21:21:47 +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-25 21:21:47 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-11-27 09:49:31 +01:00
|
|
|
#include <inttypes.h>
|
2012-10-29 01:52:23 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2012-10-25 21:21:47 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-10-25 21:21:47 +02:00
|
|
|
#include <nuttx/binfmt/elf.h>
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
#include "libelf.h"
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
#ifdef CONFIG_BINFMT_CONSTRUCTORS
|
2012-10-25 21:21:47 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2012-10-25 21:21:47 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_loadctors
|
|
|
|
*
|
|
|
|
* Description:
|
2012-10-29 21:43:35 +01:00
|
|
|
* Load pointers to static constructors into an in-memory array.
|
2012-10-29 01:52:23 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* loadinfo - Load state information
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2012-10-25 21:21:47 +02:00
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
int elf_loadctors(FAR struct elf_loadinfo_s *loadinfo)
|
2012-10-25 21:21:47 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *shdr;
|
2012-10-29 01:52:23 +01:00
|
|
|
size_t ctorsize;
|
|
|
|
int ctoridx;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(loadinfo->ctors == NULL);
|
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
/* Allocate an I/O buffer if necessary. This buffer is used by
|
|
|
|
* elf_sectname() to accumulate the variable length symbol name.
|
2012-10-29 01:52:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = elf_allocbuffer(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("elf_allocbuffer failed: %d\n", ret);
|
2012-10-29 01:52:23 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Find the index to the section named ".ctors." NOTE: On old ABI system,
|
|
|
|
* .ctors is the name of the section containing the list of constructors;
|
2014-04-14 00:22:22 +02:00
|
|
|
* On newer systems, the similar section is called .init_array. It is
|
2020-03-26 07:54:27 +01:00
|
|
|
* expected that the linker script will force the section name to be
|
|
|
|
* ".ctors" in either case.
|
2012-10-29 20:32:05 +01:00
|
|
|
*/
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
ctoridx = elf_findsection(loadinfo, ".ctors");
|
2012-10-29 01:52:23 +01:00
|
|
|
if (ctoridx < 0)
|
|
|
|
{
|
|
|
|
/* This may not be a failure. -ENOENT indicates that the file has no
|
|
|
|
* static constructor section.
|
|
|
|
*/
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("elf_findsection .ctors section failed: %d\n", ctoridx);
|
2012-10-29 01:52:23 +01:00
|
|
|
return ret == -ENOENT ? OK : ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we can get a pointer to the .ctor section in the section header
|
|
|
|
* table.
|
2012-10-25 21:21:47 +02:00
|
|
|
*/
|
|
|
|
|
2012-10-29 01:52:23 +01:00
|
|
|
shdr = &loadinfo->shdr[ctoridx];
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Get the size of the .ctor section and the number of constructors that
|
|
|
|
* will need to be called.
|
|
|
|
*/
|
2012-10-29 01:52:23 +01:00
|
|
|
|
|
|
|
ctorsize = shdr->sh_size;
|
2012-10-29 21:43:35 +01:00
|
|
|
loadinfo->nctors = ctorsize / sizeof(binfmt_ctor_t);
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("ctoridx=%d ctorsize=%d sizeof(binfmt_ctor_t)=%d nctors=%d\n",
|
2012-10-29 21:43:35 +01:00
|
|
|
ctoridx, ctorsize, sizeof(binfmt_ctor_t), loadinfo->nctors);
|
2012-10-29 01:52:23 +01:00
|
|
|
|
|
|
|
/* Check if there are any constructors. It is not an error if there
|
|
|
|
* are none.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (loadinfo->nctors > 0)
|
|
|
|
{
|
|
|
|
/* Check an assumption that we made above */
|
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
DEBUGASSERT(shdr->sh_size == loadinfo->nctors * sizeof(binfmt_ctor_t));
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* In the old ABI, the .ctors section is not allocated. In that case,
|
|
|
|
* we need to allocate memory to hold the .ctors and then copy the
|
|
|
|
* from the file into the allocated memory.
|
|
|
|
*
|
|
|
|
* SHF_ALLOC indicates that the section requires memory during
|
|
|
|
* execution.
|
|
|
|
*/
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
if ((shdr->sh_flags & SHF_ALLOC) == 0)
|
2012-10-29 01:52:23 +01:00
|
|
|
{
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Allocate memory to hold a copy of the .ctor section */
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
loadinfo->ctoralloc = (binfmt_ctor_t *)kumm_malloc(ctorsize);
|
2012-10-29 21:43:35 +01:00
|
|
|
if (!loadinfo->ctoralloc)
|
2012-10-29 20:32:05 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to allocate memory for .ctors\n");
|
2012-10-29 20:32:05 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2012-10-29 21:43:35 +01:00
|
|
|
loadinfo->ctors = (binfmt_ctor_t *)loadinfo->ctoralloc;
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Read the section header table into memory */
|
2012-10-29 01:52:23 +01:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
ret = elf_read(loadinfo, (FAR uint8_t *)loadinfo->ctors, ctorsize,
|
2012-10-29 20:32:05 +01:00
|
|
|
shdr->sh_offset);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Failed to allocate .ctors: %d\n", ret);
|
2012-10-29 20:32:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
/* Fix up all of the .ctor addresses. Since the addresses
|
|
|
|
* do not lie in allocated memory, there will be no relocation
|
|
|
|
* section for them.
|
|
|
|
*/
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
for (i = 0; i < loadinfo->nctors; i++)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
FAR uintptr_t *ptr = (uintptr_t *)
|
|
|
|
((FAR void *)(&loadinfo->ctors)[i]);
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2020-11-27 09:49:31 +01:00
|
|
|
binfo("ctor %d: "
|
|
|
|
"%08" PRIxPTR " + %08" PRIxPTR " = %08" PRIxPTR "\n",
|
|
|
|
i, *ptr, (uintptr_t)loadinfo->textalloc,
|
|
|
|
(uintptr_t)(*ptr + loadinfo->textalloc));
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2014-09-06 19:40:35 +02:00
|
|
|
*ptr += loadinfo->textalloc;
|
2012-10-29 20:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
/* Save the address of the .ctors (actually, .init_array) where
|
|
|
|
* it was loaded into memory. Since the .ctors lie in allocated
|
|
|
|
* memory, they will be relocated via the normal mechanism.
|
2012-10-29 20:32:05 +01:00
|
|
|
*/
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2015-10-09 03:20:17 +02:00
|
|
|
loadinfo->ctors = (binfmt_ctor_t *)shdr->sh_addr;
|
2012-10-29 20:32:05 +01:00
|
|
|
}
|
2012-10-25 21:21:47 +02:00
|
|
|
}
|
2012-10-29 01:52:23 +01:00
|
|
|
|
|
|
|
return OK;
|
2012-10-25 21:21:47 +02:00
|
|
|
}
|
|
|
|
|
2012-10-29 20:32:05 +01:00
|
|
|
#endif /* CONFIG_BINFMT_CONSTRUCTORS */
|