2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libelf/libelf_bind.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>
|
|
|
|
|
2020-11-22 02:56:05 +01:00
|
|
|
#include <inttypes.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2019-01-26 18:18:45 +01:00
|
|
|
#include <nuttx/elf.h>
|
2019-03-19 15:57:13 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/elf.h>
|
2012-10-24 18:46:12 +02:00
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
#include "libelf.h"
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-26 07:54:27 +01:00
|
|
|
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT have to
|
|
|
|
* be defined or CONFIG_ELF_DUMPBUFFER does nothing.
|
2012-10-24 18:46:12 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-11 19:50:18 +02:00
|
|
|
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_DEBUG_BINFMT)
|
2012-10-24 18:46:12 +02:00
|
|
|
# undef CONFIG_ELF_DUMPBUFFER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_ELF_DUMPBUFFER
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
|
2012-10-24 18:46:12 +02:00
|
|
|
#else
|
2023-05-20 00:32:34 +02:00
|
|
|
# define elf_dumpbuffer(m,b,n)
|
2012-10-24 18:46:12 +02:00
|
|
|
#endif
|
|
|
|
|
riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:
label:
auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).
However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.
The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-05 11:30:46 +01:00
|
|
|
#ifdef ARCH_ELFDATA
|
|
|
|
# define ARCH_ELFDATA_DEF arch_elfdata_t arch_data; \
|
|
|
|
memset(&arch_data, 0, sizeof(arch_elfdata_t))
|
|
|
|
# define ARCH_ELFDATA_PARM &arch_data
|
|
|
|
#else
|
|
|
|
# define ARCH_ELFDATA_DEF
|
|
|
|
# define ARCH_ELFDATA_PARM NULL
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
struct elf_symcache_s
|
2019-03-19 16:13:50 +01:00
|
|
|
{
|
2019-03-20 16:34:08 +01:00
|
|
|
dq_entry_t entry;
|
2020-02-08 00:10:23 +01:00
|
|
|
Elf_Sym sym;
|
2019-03-20 16:34:08 +01:00
|
|
|
int idx;
|
|
|
|
};
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
typedef struct elf_symcache_s elf_symcache_t;
|
2019-03-19 16:13:50 +01:00
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
/****************************************************************************
|
2019-03-19 15:57:13 +01:00
|
|
|
* Name: elf_readrels
|
2012-10-25 18:18:20 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-02-08 00:10:23 +01:00
|
|
|
* Read the (ELF_Rel structure * buffer count) into memory.
|
2012-10-25 18:18:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
static inline int elf_readrels(FAR struct elf_loadinfo_s *loadinfo,
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR const Elf_Shdr *relsec,
|
|
|
|
int index, FAR Elf_Rel *rels,
|
2019-03-19 16:13:50 +01:00
|
|
|
int count)
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
|
|
|
off_t offset;
|
2019-03-19 15:57:13 +01:00
|
|
|
int size;
|
2012-10-25 18:18:20 +02:00
|
|
|
|
|
|
|
/* Verify that the symbol table index lies within symbol table */
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
if (index < 0 || index > (relsec->sh_size / sizeof(Elf_Rel)))
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("Bad relocation symbol index: %d\n", index);
|
2012-10-25 18:18:20 +02:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the file offset to the symbol table entry */
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
offset = sizeof(Elf_Rel) * index;
|
|
|
|
size = sizeof(Elf_Rel) * count;
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
if (offset + size > relsec->sh_size)
|
|
|
|
{
|
|
|
|
size = relsec->sh_size - offset;
|
|
|
|
}
|
2012-10-25 18:18:20 +02:00
|
|
|
|
|
|
|
/* And, finally, read the symbol table entry into memory */
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
return elf_read(loadinfo, (FAR uint8_t *)rels, size,
|
|
|
|
relsec->sh_offset + offset);
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_readrelas
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the (ELF_Rela structure * buffer count) into memory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int elf_readrelas(FAR struct elf_loadinfo_s *loadinfo,
|
|
|
|
FAR const Elf_Shdr *relsec,
|
|
|
|
int index, FAR Elf_Rela *relas,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
off_t offset;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
/* Verify that the symbol table index lies within symbol table */
|
|
|
|
|
|
|
|
if (index < 0 || index > (relsec->sh_size / sizeof(Elf_Rela)))
|
|
|
|
{
|
|
|
|
berr("Bad relocation symbol index: %d\n", index);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the file offset to the symbol table entry */
|
|
|
|
|
|
|
|
offset = sizeof(Elf_Rela) * index;
|
|
|
|
size = sizeof(Elf_Rela) * count;
|
|
|
|
|
|
|
|
if (offset + size > relsec->sh_size)
|
|
|
|
{
|
|
|
|
size = relsec->sh_size - offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And, finally, read the symbol table entry into memory */
|
|
|
|
|
|
|
|
return elf_read(loadinfo, (FAR uint8_t *)relas, size,
|
|
|
|
relsec->sh_offset + offset);
|
|
|
|
}
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_relocate and elf_relocateadd
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform all relocations associated with a section.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-25 21:21:47 +02:00
|
|
|
static int elf_relocate(FAR struct elf_loadinfo_s *loadinfo, int relidx,
|
|
|
|
FAR const struct symtab_s *exports, int nexports)
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *relsec = &loadinfo->shdr[relidx];
|
|
|
|
FAR Elf_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
|
|
|
|
FAR Elf_Rel *rels;
|
|
|
|
FAR Elf_Rel *rel;
|
|
|
|
FAR elf_symcache_t *cache;
|
|
|
|
FAR Elf_Sym *sym;
|
2019-03-20 16:34:08 +01:00
|
|
|
FAR dq_entry_t *e;
|
|
|
|
dq_queue_t q;
|
|
|
|
uintptr_t addr;
|
|
|
|
int symidx;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
int j;
|
2012-10-25 18:18:20 +02:00
|
|
|
|
riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:
label:
auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).
However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.
The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-05 11:30:46 +01:00
|
|
|
/* Define potential architecture specific elf data container */
|
|
|
|
|
|
|
|
ARCH_ELFDATA_DEF;
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
rels = kmm_malloc(CONFIG_ELF_RELOCATION_BUFFERCOUNT * sizeof(Elf_Rel));
|
2019-03-19 15:57:13 +01:00
|
|
|
if (rels == NULL)
|
|
|
|
{
|
2019-03-19 16:13:50 +01:00
|
|
|
berr("Failed to allocate memory for elf relocation\n");
|
2019-03-19 15:57:13 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:13:50 +01:00
|
|
|
dq_init(&q);
|
|
|
|
|
2012-10-27 02:04:47 +02:00
|
|
|
/* Examine each relocation in the section. 'relsec' is the section
|
|
|
|
* containing the relations. 'dstsec' is the section containing the data
|
|
|
|
* to be relocated.
|
|
|
|
*/
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
ret = OK;
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
for (i = j = 0; i < relsec->sh_size / sizeof(Elf_Rel); i++)
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
|
|
|
/* Read the relocation entry into memory */
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
rel = &rels[i % CONFIG_ELF_RELOCATION_BUFFERCOUNT];
|
|
|
|
|
|
|
|
if (!(i % CONFIG_ELF_RELOCATION_BUFFERCOUNT))
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2019-03-19 16:13:50 +01:00
|
|
|
ret = elf_readrels(loadinfo, relsec, i, rels,
|
|
|
|
CONFIG_ELF_RELOCATION_BUFFERCOUNT);
|
2019-03-19 15:57:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
berr("Section %d reloc %d: "
|
|
|
|
"Failed to read relocation entry: %d\n",
|
|
|
|
relidx, i, ret);
|
2019-03-19 15:57:13 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the symbol table index for the relocation. This is contained
|
|
|
|
* in a bit-field within the r_info element.
|
|
|
|
*/
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
symidx = ELF_R_SYM(rel->r_info);
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2019-03-19 16:13:50 +01:00
|
|
|
/* First try the cache */
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2019-03-19 16:13:50 +01:00
|
|
|
sym = NULL;
|
|
|
|
for (e = dq_peek(&q); e; e = dq_next(e))
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
cache = (FAR elf_symcache_t *)e;
|
2019-03-19 16:13:50 +01:00
|
|
|
if (cache->idx == symidx)
|
|
|
|
{
|
|
|
|
dq_rem(&cache->entry, &q);
|
|
|
|
dq_addfirst(&cache->entry, &q);
|
|
|
|
sym = &cache->sym;
|
|
|
|
break;
|
|
|
|
}
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 16:13:50 +01:00
|
|
|
/* If the symbol was not found in the cache, we will need to read the
|
|
|
|
* symbol from the file.
|
|
|
|
*/
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2019-03-19 16:13:50 +01:00
|
|
|
if (sym == NULL)
|
2012-10-25 21:21:47 +02:00
|
|
|
{
|
2019-03-19 16:13:50 +01:00
|
|
|
if (j < CONFIG_ELF_SYMBOL_CACHECOUNT)
|
2014-09-10 00:52:51 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
cache = kmm_malloc(sizeof(elf_symcache_t));
|
2019-03-19 16:13:50 +01:00
|
|
|
if (!cache)
|
|
|
|
{
|
|
|
|
berr("Failed to allocate memory for elf symbols\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
j++;
|
2014-09-10 00:52:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
cache = (FAR elf_symcache_t *)dq_remlast(&q);
|
2019-03-19 16:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sym = &cache->sym;
|
|
|
|
|
|
|
|
/* Read the symbol table entry into memory */
|
|
|
|
|
|
|
|
ret = elf_readsym(loadinfo, symidx, sym);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("Section %d reloc %d: Failed to read symbol[%d]: %d\n",
|
|
|
|
relidx, i, symidx, ret);
|
|
|
|
kmm_free(cache);
|
2019-03-19 15:57:13 +01:00
|
|
|
break;
|
2014-09-10 00:52:51 +02:00
|
|
|
}
|
2019-03-19 16:13:50 +01:00
|
|
|
|
|
|
|
/* Get the value of the symbol (in sym.st_value) */
|
|
|
|
|
|
|
|
ret = elf_symvalue(loadinfo, sym, exports, nexports);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
/* The special error -ESRCH is returned only in one condition:
|
|
|
|
* The symbol has no name.
|
2019-03-19 16:13:50 +01:00
|
|
|
*
|
|
|
|
* There are a few relocations for a few architectures that do
|
|
|
|
* no depend upon a named symbol. We don't know if that is the
|
|
|
|
* case here, but we will use a NULL symbol pointer to indicate
|
|
|
|
* that case to up_relocate(). That function can then do what
|
|
|
|
* is best.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ESRCH)
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
berr("Section %d reloc %d: "
|
|
|
|
"Undefined symbol[%d] has no name: %d\n",
|
|
|
|
relidx, i, symidx, ret);
|
2019-03-19 16:13:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
berr("Section %d reloc %d: "
|
|
|
|
"Failed to get value of symbol[%d]: %d\n",
|
2019-03-19 16:13:50 +01:00
|
|
|
relidx, i, symidx, ret);
|
|
|
|
kmm_free(cache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->idx = symidx;
|
|
|
|
dq_addfirst(&cache->entry, &q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym->st_shndx == SHN_UNDEF && sym->st_name == 0)
|
|
|
|
{
|
|
|
|
sym = NULL;
|
2012-10-25 21:21:47 +02:00
|
|
|
}
|
|
|
|
|
2012-12-19 18:54:26 +01:00
|
|
|
/* Calculate the relocation address. */
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2020-01-11 17:10:49 +01:00
|
|
|
if (rel->r_offset < 0 ||
|
|
|
|
rel->r_offset > dstsec->sh_size - sizeof(uint32_t))
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2020-01-11 17:10:49 +01:00
|
|
|
berr("Section %d reloc %d: Relocation address out of range, "
|
2020-11-22 02:56:05 +01:00
|
|
|
"offset %" PRIdPTR " size %jd\n",
|
|
|
|
relidx, i, (uintptr_t)rel->r_offset,
|
|
|
|
(uintmax_t)dstsec->sh_size);
|
2019-03-19 15:57:13 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
addr = dstsec->sh_addr + rel->r_offset;
|
2012-10-25 18:18:20 +02:00
|
|
|
|
|
|
|
/* Now perform the architecture-specific relocation */
|
|
|
|
|
riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:
label:
auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).
However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.
The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-05 11:30:46 +01:00
|
|
|
ret = up_relocate(rel, sym, addr, ARCH_ELFDATA_PARM);
|
2012-10-25 18:18:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
berr("ERROR: Section %d reloc %d: Relocation failed: %d\n",
|
|
|
|
relidx, i, ret);
|
2019-03-19 15:57:13 +01:00
|
|
|
break;
|
2012-12-19 18:54:26 +01:00
|
|
|
}
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2019-03-19 15:57:13 +01:00
|
|
|
kmm_free(rels);
|
2019-03-19 16:13:50 +01:00
|
|
|
while ((e = dq_peek(&q)))
|
|
|
|
{
|
|
|
|
dq_rem(e, &q);
|
|
|
|
kmm_free(e);
|
|
|
|
}
|
2019-03-19 15:57:13 +01:00
|
|
|
|
|
|
|
return ret;
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2012-10-25 21:21:47 +02:00
|
|
|
static int elf_relocateadd(FAR struct elf_loadinfo_s *loadinfo, int relidx,
|
|
|
|
FAR const struct symtab_s *exports, int nexports)
|
2012-10-25 18:18:20 +02:00
|
|
|
{
|
2020-02-08 00:10:23 +01:00
|
|
|
FAR Elf_Shdr *relsec = &loadinfo->shdr[relidx];
|
|
|
|
FAR Elf_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
|
|
|
|
FAR Elf_Rela *relas;
|
|
|
|
FAR Elf_Rela *rela;
|
|
|
|
FAR elf_symcache_t *cache;
|
|
|
|
FAR Elf_Sym *sym;
|
|
|
|
FAR dq_entry_t *e;
|
|
|
|
dq_queue_t q;
|
|
|
|
uintptr_t addr;
|
|
|
|
int symidx;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:
label:
auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).
However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.
The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-05 11:30:46 +01:00
|
|
|
/* Define potential architecture specific elf data container */
|
|
|
|
|
|
|
|
ARCH_ELFDATA_DEF;
|
|
|
|
|
2020-02-08 00:10:23 +01:00
|
|
|
relas = kmm_malloc(CONFIG_ELF_RELOCATION_BUFFERCOUNT * sizeof(Elf_Rela));
|
|
|
|
if (relas == NULL)
|
|
|
|
{
|
|
|
|
berr("Failed to allocate memory for elf relocation\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
dq_init(&q);
|
|
|
|
|
|
|
|
/* Examine each relocation in the section. 'relsec' is the section
|
|
|
|
* containing the relations. 'dstsec' is the section containing the data
|
|
|
|
* to be relocated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
for (i = j = 0; i < relsec->sh_size / sizeof(Elf_Rela); i++)
|
|
|
|
{
|
|
|
|
/* Read the relocation entry into memory */
|
|
|
|
|
|
|
|
rela = &relas[i % CONFIG_ELF_RELOCATION_BUFFERCOUNT];
|
|
|
|
|
|
|
|
if (!(i % CONFIG_ELF_RELOCATION_BUFFERCOUNT))
|
|
|
|
{
|
|
|
|
ret = elf_readrelas(loadinfo, relsec, i, relas,
|
|
|
|
CONFIG_ELF_RELOCATION_BUFFERCOUNT);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
berr("Section %d reloc %d: "
|
|
|
|
"Failed to read relocation entry: %d\n",
|
|
|
|
relidx, i, ret);
|
2020-02-08 00:10:23 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the symbol table index for the relocation. This is contained
|
|
|
|
* in a bit-field within the r_info element.
|
|
|
|
*/
|
|
|
|
|
|
|
|
symidx = ELF_R_SYM(rela->r_info);
|
|
|
|
|
|
|
|
/* First try the cache */
|
|
|
|
|
|
|
|
sym = NULL;
|
|
|
|
for (e = dq_peek(&q); e; e = dq_next(e))
|
|
|
|
{
|
|
|
|
cache = (FAR elf_symcache_t *)e;
|
|
|
|
if (cache->idx == symidx)
|
|
|
|
{
|
|
|
|
dq_rem(&cache->entry, &q);
|
|
|
|
dq_addfirst(&cache->entry, &q);
|
|
|
|
sym = &cache->sym;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the symbol was not found in the cache, we will need to read the
|
|
|
|
* symbol from the file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sym == NULL)
|
|
|
|
{
|
|
|
|
if (j < CONFIG_ELF_SYMBOL_CACHECOUNT)
|
|
|
|
{
|
|
|
|
cache = kmm_malloc(sizeof(elf_symcache_t));
|
|
|
|
if (!cache)
|
|
|
|
{
|
|
|
|
berr("Failed to allocate memory for elf symbols\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cache = (FAR elf_symcache_t *)dq_remlast(&q);
|
|
|
|
}
|
|
|
|
|
|
|
|
sym = &cache->sym;
|
|
|
|
|
|
|
|
/* Read the symbol table entry into memory */
|
|
|
|
|
|
|
|
ret = elf_readsym(loadinfo, symidx, sym);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("Section %d reloc %d: Failed to read symbol[%d]: %d\n",
|
|
|
|
relidx, i, symidx, ret);
|
|
|
|
kmm_free(cache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the value of the symbol (in sym.st_value) */
|
|
|
|
|
|
|
|
ret = elf_symvalue(loadinfo, sym, exports, nexports);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-26 07:54:27 +01:00
|
|
|
/* The special error -ESRCH is returned only in one condition:
|
|
|
|
* The symbol has no name.
|
2020-02-08 00:10:23 +01:00
|
|
|
*
|
|
|
|
* There are a few relocations for a few architectures that do
|
|
|
|
* no depend upon a named symbol. We don't know if that is the
|
|
|
|
* case here, but we will use a NULL symbol pointer to indicate
|
|
|
|
* that case to up_relocate(). That function can then do what
|
|
|
|
* is best.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret == -ESRCH)
|
|
|
|
{
|
2023-12-29 07:35:25 +01:00
|
|
|
bwarn("Section %d reloc %d: "
|
2020-02-08 00:10:23 +01:00
|
|
|
"Undefined symbol[%d] has no name: %d\n",
|
|
|
|
relidx, i, symidx, ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
berr("Section %d reloc %d: "
|
|
|
|
"Failed to get value of symbol[%d]: %d\n",
|
|
|
|
relidx, i, symidx, ret);
|
|
|
|
kmm_free(cache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cache->idx = symidx;
|
|
|
|
dq_addfirst(&cache->entry, &q);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym->st_shndx == SHN_UNDEF && sym->st_name == 0)
|
|
|
|
{
|
|
|
|
sym = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the relocation address. */
|
|
|
|
|
|
|
|
if (rela->r_offset < 0 ||
|
|
|
|
rela->r_offset > dstsec->sh_size)
|
|
|
|
{
|
|
|
|
berr("Section %d reloc %d: Relocation address out of range, "
|
2020-11-22 02:56:05 +01:00
|
|
|
"offset %" PRIdPTR " size %jd\n",
|
|
|
|
relidx, i, (uintptr_t)rela->r_offset,
|
|
|
|
(uintmax_t)dstsec->sh_size);
|
2020-02-08 00:10:23 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = dstsec->sh_addr + rela->r_offset;
|
|
|
|
|
|
|
|
/* Now perform the architecture-specific relocation */
|
|
|
|
|
riscv/arch_elf.c: Handle PCREL_HI20/LO12_I/S relocations correctly
There is a problem with the current elf loader for risc-v: when a pair of
PCREL_HI20 / LO12 relocations are encountered, it is assumed that these
will follow each other immediately, as follows:
label:
auipc a0, %pcrel_hi(symbol) // R_RISCV_PCREL_HI20
load/store a0, %pcrel_lo(label)(a0) // R_RISCV_PCREL_LO12_I/S
With this assumption, the hi/lo relocations are both done when a hi20
relocation entry is encountered, first to the current instruction (addr)
and to the next instruction (addr + 4).
However, this assumption is wrong. There is nothing in the elf relocation
specification[1] that mandates this. Thus, the hi/lo relocation always
needs to first fixup the hi-part, and when the lo-part is encountered, it
needs to find the corresponding hi relocation entry, via the given "label".
This necessitates (re-)visiting the relocation entries for the current
section as well as looking for "label" in the symbol table.
The NuttX elf loader does not allow such operations to be done in the
machine specific part, so this patch fixes the relocation issue by
introducing an architecture specific cache for the hi20 relocation and
symbol table entries. When a lo12 relocation is encountered, the cache
can be consulted to find the hi20 part.
[1] https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc
2023-12-05 11:30:46 +01:00
|
|
|
ret = up_relocateadd(rela, sym, addr, ARCH_ELFDATA_PARM);
|
2020-02-08 00:10:23 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
berr("ERROR: Section %d reloc %d: Relocation failed: %d\n",
|
|
|
|
relidx, i, ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kmm_free(relas);
|
|
|
|
while ((e = dq_peek(&q)))
|
|
|
|
{
|
|
|
|
dq_rem(e, &q);
|
|
|
|
kmm_free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
2012-10-24 18:46:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: elf_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the imported symbol names in the loaded module described by
|
|
|
|
* 'loadinfo' using the exported symbol values provided by 'symtab'.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int elf_bind(FAR struct elf_loadinfo_s *loadinfo,
|
|
|
|
FAR const struct symtab_s *exports, int nexports)
|
|
|
|
{
|
2014-08-26 15:57:30 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
int status;
|
|
|
|
#endif
|
2012-10-25 18:18:20 +02:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Find the symbol and string tables */
|
|
|
|
|
|
|
|
ret = elf_findsymtab(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
#ifdef CONFIG_ARCH_ADDRENV
|
|
|
|
/* If CONFIG_ARCH_ADDRENV=y, then the loaded ELF lies in a virtual address
|
|
|
|
* space that may not be in place now. elf_addrenv_select() will
|
|
|
|
* temporarily instantiate that address space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = elf_addrenv_select(loadinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_addrenv_select() failed: %d\n", ret);
|
2014-08-26 15:57:30 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
/* Process relocations in every allocated section */
|
|
|
|
|
|
|
|
for (i = 1; i < loadinfo->ehdr.e_shnum; i++)
|
|
|
|
{
|
|
|
|
/* Get the index to the relocation section */
|
2014-04-14 00:22:22 +02:00
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
int infosec = loadinfo->shdr[i].sh_info;
|
|
|
|
if (infosec >= loadinfo->ehdr.e_shnum)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the section is allocated. We can't relocated
|
|
|
|
* sections that were not loaded into memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the relocations by type */
|
|
|
|
|
|
|
|
if (loadinfo->shdr[i].sh_type == SHT_REL)
|
|
|
|
{
|
2012-10-25 21:21:47 +02:00
|
|
|
ret = elf_relocate(loadinfo, i, exports, nexports);
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
else if (loadinfo->shdr[i].sh_type == SHT_RELA)
|
|
|
|
{
|
2012-10-25 21:21:47 +02:00
|
|
|
ret = elf_relocateadd(loadinfo, i, exports, nexports);
|
2012-10-25 18:18:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:53:19 +02:00
|
|
|
#if defined(CONFIG_ARCH_ADDRENV)
|
2014-08-24 22:12:45 +02:00
|
|
|
/* Ensure that the I and D caches are coherent before starting the newly
|
|
|
|
* loaded module by cleaning the D cache (i.e., flushing the D cache
|
2014-08-26 14:33:26 +02:00
|
|
|
* contents to memory and invalidating the I cache).
|
2014-08-24 22:12:45 +02:00
|
|
|
*/
|
2012-10-25 18:18:20 +02:00
|
|
|
|
2014-08-26 22:53:19 +02:00
|
|
|
#if 0 /* REVISIT... has some problems */
|
2023-01-27 12:45:03 +01:00
|
|
|
up_addrenv_coherent(&loadinfo->addrenv.addrenv);
|
2014-08-26 22:53:19 +02:00
|
|
|
#else
|
2014-08-25 14:47:14 +02:00
|
|
|
up_coherent_dcache(loadinfo->textalloc, loadinfo->textsize);
|
2014-08-26 22:53:19 +02:00
|
|
|
up_coherent_dcache(loadinfo->dataalloc, loadinfo->datasize);
|
2012-10-25 18:37:31 +02:00
|
|
|
#endif
|
2012-10-25 21:21:47 +02:00
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
/* Restore the original address environment */
|
|
|
|
|
|
|
|
status = elf_addrenv_restore(loadinfo);
|
|
|
|
if (status < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
berr("ERROR: elf_addrenv_restore() failed: %d\n", status);
|
2014-08-26 15:57:30 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = status;
|
|
|
|
}
|
|
|
|
}
|
2014-08-26 22:53:19 +02:00
|
|
|
|
2019-03-19 17:37:13 +01:00
|
|
|
#else
|
2014-08-26 22:53:19 +02:00
|
|
|
/* Ensure that the I and D caches are coherent before starting the newly
|
|
|
|
* loaded module by cleaning the D cache (i.e., flushing the D cache
|
|
|
|
* contents to memory and invalidating the I cache).
|
|
|
|
*/
|
|
|
|
|
|
|
|
up_coherent_dcache(loadinfo->textalloc, loadinfo->textsize);
|
|
|
|
up_coherent_dcache(loadinfo->dataalloc, loadinfo->datasize);
|
|
|
|
|
2014-08-26 15:57:30 +02:00
|
|
|
#endif
|
|
|
|
|
2012-10-25 18:18:20 +02:00
|
|
|
return ret;
|
2012-10-24 18:46:12 +02:00
|
|
|
}
|