Commit Graph

88 Commits

Author SHA1 Message Date
anjiahao a613bcb7cb modlib:add new dump api to modlib
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-05 15:22:02 +08:00
anjiahao a9cb28cd23 modlib:Standardized module loading method
1. use '__attribute__((constructor))' mark initialize function
2. use '__attribute__((destructor))' mark uninitialize function
3. compile module with -fvisibility=hidden. use `__attribute__((visibility("default")))`
mark is need export symbol.so not need module_initialize to initialize export symbol.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-10-05 15:22:02 +08:00
Alin Jerpelea 6b5dddd5d7 libs/libc: migrate to SPDX identifier
Most tools used for compliance and SBOM generation use SPDX identifiers
This change brings us a step closer to an easy SBOM generation.

Signed-off-by: Alin Jerpelea <alin.jerpelea@sony.com>
2024-10-01 12:25:52 +08:00
anjiahao 29e4e71167 modlib/dlfcn:unify same code
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2024-09-29 15:06:54 +08:00
dongjiuzhu1 fc1aefbeb3 libc/modlib: free memory resource when rmmod elf
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-09-29 15:06:54 +08:00
dongjiuzhu1 1f1d90de1c binfmt/modlib: support loading each sections to different memory for Relocate object
The feature depends on ARCH_USE_SEPARATED_SECTION
the different memory area has different access speed and cache
capability, so the arch can custom allocate them based on
section names to achieve performance optimization

test:
sim:elf
sim:sotest

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2024-09-29 15:06:54 +08:00
guoshichao c9223469d4 moblib_bind: fix the variable uninit misra issue
Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-09-27 00:20:53 +08:00
wangzhi16 129b738e6b [Bug-Fix] Resource leaks
In file "nuttx/libs/libc/modlib/modlib_bind.c", line656 not check return value of lib_malloc(), and line661 not free dyn.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
2024-09-15 10:26:21 +08:00
Petro Karashchenko d499ac9d58 nuttx: fix multiple 'FAR', 'CODE' and style issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2024-08-25 19:22:15 +08:00
guoshichao cc79201c59 greenhills: fix the pointless compare warning
CC:  modlib/modlib_depend.c "modlib/modlib_bind.c", line 741: warning #186-D: pointless comparison of
          unsigned integer with zero
            if (rel->r_offset < 0)
                              ^

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-08-24 20:43:53 +08:00
guoshichao 04f5aaebfa greenhills: fix macro undefined warning
AS:  modlib/modlib_globals.S "modlib/modlib_globals.S", line 38: warning #193-D: zero used for undefined
          preprocessing identifier "__SIZEOF_POINTER__"
  #if __SIZEOF_POINTER__ == 8
      ^

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-08-24 20:43:53 +08:00
guoshichao d76218e817 greenhills: fix the moblib asm compile error
CC:  assert/lib_assert.c [asarm] (error #2179) modlib/modlib_globals.S 61: unexpected token type (char) encountered; expected type (identifier)
  .size \ globalNames , . - \ globalNames
--------^

[asarm] (error #2179) modlib/modlib_globals.S 67: unexpected token type (char) encountered; expected type (identifier)
  .size \ nglobals , . - \ nglobals
--------^

[asarm] (error #2179) modlib/modlib_globals.S 72: unexpected token type (char) encountered; expected type (identifier)
  .size \ global_table , . - \ global_table
--------^

Signed-off-by: guoshichao <guoshichao@xiaomi.com>
2024-08-11 14:28:12 -03:00
Tiago Medicci Serrano 84d39a8d9a binfmt/libelf: Enable ELF loader if text heap read is word-aligned
The ELF loader needs to load the app into the memory before
executing it from the same location. As expected, this memory space
should be able to execute code. For architectures containing data
and instruction buses, the instruction bus may not be able to be
accessed in a non-aligned way, which is usually required when
copying data to that location. Eventually, this same memory space
can be accessed through the data bus, using different address
ranges. This commit enables accessing the memory through the data
bus to copy the app's data before executing it when
`CONFIG_ARCH_HAVE_TEXT_HEAP_WORD_ALIGNED_READ` is enabled.
2024-07-31 02:33:12 +08:00
Ville Juven 996625ec58 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-12 17:32:36 -08:00
dongjiuzhu1 a7e448c57e modlib/modlib_load: fix compile warning
CC:  icmp/icmp_input.c modlib/modlib_load.c: In function 'modlib_elfsize':
modlib/modlib_load.c:87:30: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
   87 |                   textaddr = (FAR void *)phdr->p_vaddr;
      |                              ^
cc1: all warnings being treated as errors

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-26 01:53:38 +08:00
dongjiuzhu1 db13ff2dd5 libs/libc: fix compile warning about modlib_depend when CONFIG_MODLIB_MAXDEPEND = 0
modlib/modlib_symbols.c: In function ‘modlib_symcallback’:
modlib/modlib_symbols.c:215:13: warning: implicit declaration of function ‘modlib_depend’; did you mean ‘modlib_read’? [-Wimplicit-function-declaration]
  215 |       ret = modlib_depend(exportinfo->modp, modp);
      |             ^~~~~~~~~~~~~
      |             modlib_read

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-26 01:53:38 +08:00
dongjiuzhu1 e88a36fa92 libs/modlib: Adding architecture-specific memory allocator for dynamic data loading
Arch can specific the memory allocator for data to optimize access speed.

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-26 01:53:38 +08:00
dongjiuzhu1 81717a59ea libs/modlib: remove dupliate define about modlib_dumpbuffer
CP:  nuttx/include/nuttx/config.h
modlib/modlib_init.c:53: warning: "modlib_dumpbuffer" redefined
   53 | #  define modlib_dumpbuffer(m,b,n) binfodumpbuffer(m,b,n)
      |
In file included from modlib/modlib_init.c:36:
nuttx/include/nuttx/lib/modlib.h:64: note: this is the location of the previous definition
   64 | #  define modlib_dumpbuffer(m,b,n) sinfodumpbuffer(m,b,n)
      |

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-10-26 01:53:38 +08:00
Neale Ferguson 5d7f2fdf16 Fix loading of ET_DYN type of shared objects
* build-globals.sh
  - Only look in the nuttx for external symbols used when loading
    dynamic shared objects

* include/elf64.h
  - Correct the type of fields in the Elf64_Phdr structure

* libs/libc/dlfcn/lib_dlclose.c
  - Distinguish between ET_DYN and other objects as the former
    has both text and data in a single allocation to reserve
    GOT offsets

* libs/libc/dlfcn/lib_dlopen.c
  - Code formatting

* libs/libc/modlib/modlib_bind.c
  - Distinguish between relocation entry sizes by section type
  - Handle RELA style relocations

* libs/libc/modlib/modlib_globals.S
  - Formatting fixes
  - Symbols should not be weak - they exist or they don't

* include/nuttx/lib/modlib.h
  - Add an inidcator to module_s to distinguish between ET_DYN and other

* libs/libc/modlib/modlib_load.c
  - ET_DYN objects need to keep the relative displacement between the text
    and data sections due to GOT references from the former to the latter.
    This also implies that linking may require modification from the default
    for the shared objects being produced. For example, default alignment may
    mean nearly 64K of wasted space.

* libs/libc/modlib/modlib_unload.c
  sched/module/mod_rmmod.c
  - Distingusih between freeing of ET_DYN storage and other as the former
    is a single allocation.

* libs/libc/modlib/mod_insmod.c
  - Cater for ET_DYN objects having init and preinit sections
2023-09-20 09:35:28 -04:00
dongjiuzhu1 3884087396 libc/mod: fix minor issue about description
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-09-20 17:02:29 +08:00
dongjiuzhu1 e8c9541263 libs/modlib: avoid double free when call modlib_unload with erro
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-09-20 17:02:29 +08:00
dongjiuzhu1 310ba09ed7 libs/modlib: avoid seeking in each reading
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-09-20 17:02:29 +08:00
dongjiuzhu1 13bdaaec99 libs/modlib: close fd when error happen
Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
2023-09-20 17:02:29 +08:00
Petro Karashchenko 8a72eaf557 libs/libc/modlib: fix nxstyle issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-09-16 14:17:47 +08:00
Xiang Xiao cb8df39207 binfmt/elf: Fix the minor style issue
and remove the unused macros and unnecessary cast

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-08-27 18:46:40 -03:00
Petro Karashchenko d113722eb2 style: fix indentation issues
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-08-08 11:58:29 -03:00
Tiago Medicci Serrano a5c92582a4 Rename CamelCase symbols to lowercase 2023-07-25 08:51:17 -07:00
Alan Carvalho de Assis 35ec548033 Remove empty lines with spaces found in the source code 2023-07-25 08:51:17 -07:00
Neale Ferguson ed4f651bbd Fix modlib to get binary loading working again
This patch fixes the issue reported here:
https://github.com/apache/nuttx/issues/9845
2023-07-25 08:51:17 -07:00
Xiang Xiao 120c4a1660 modlib: Move modlib_allocbuffer to modlib_sectname and modlib_symname
it's better to allocate the buffer just before really use it.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-16 14:17:07 +03:00
Neale Ferguson 878384fef0 Fix and improve dynamic loader
This patch fixes some issues found by Mark Stevens and
improve the dynamic loader.
2023-07-13 10:11:58 +08:00
Xiang Xiao 9dc59e9109 Fix error: Mixed case identifier found
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +03:00
Xiang Xiao ebcb03dce9 libc/symtab: Don't include symtab.h in the header files
to unify the inclusion of symtab.h only from the source files

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +03:00
chao an de7132c697 cmake/sched: fix build break based on mainline
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-10 13:26:21 +08:00
chao an 6ee9ec7656 build: add initial cmake build system
1. Update all CMakeLists.txt to adapt to new layout
2. Fix cmake build break
3. Update all new file license
4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

------------------

How to test

From within nuttx/. Configure:

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

(or full path in custom board) :
cmake -B build -DBOARD_CONFIG=$PWD/boards/sim/sim/sim/configs/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

--------------------------

2. cmake/build: reformat the cmake style by cmake-format

https://github.com/cheshirekow/cmake_format

$ pip install cmakelang

$ for i in `find -name CMakeLists.txt`;do cmake-format $i -o $i;done
$ for i in `find -name *\.cmake`;do cmake-format $i -o $i;done

Co-authored-by: Matias N <matias@protobits.dev>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-08 13:50:48 +08:00
Neale Ferguson b5cd7c2a82 libs/libc/modlib: fix dynamic loader issues
* build-globals.sh
    - Macros for defining symbols etc. based on assembler in use
    - Use the System.map to get all the nuttx symbols

  * libs/libc/modlib/modlib_globals.S
    - Provide an empty skeleton. If the dynamic loading functions
      are required then run build-global.sh after a clean build
      using the skeleton. This will fill out the skeleton with the
      symbols to be available to dynamically loaded modules.

  * libs/libc/modlib/modlib_loadhdrs.c
    - Fix case where there are no program headers are avaiable
2023-07-07 13:43:20 -04:00
Alan Carvalho de Assis bc62699c1d Fix coding style issues from PR #7202
Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-07-06 14:08:08 -03:00
Xiang Xiao 8f38fa6062 symtab: Remove the unnessary inclusion of nuttx/symtab.h
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:24:02 -03:00
Xiang Xiao 935f43185f libc/modlib: Replace nx_stat with file_stat
to avoid searching the path twice

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:20:24 -03:00
Xiang Xiao 7bb97f7e22 elf: Replace {0x7f, 'E', 'L', 'F'} to EI_MAGIC
to avoid the duplication of the magic number.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:15:50 -03:00
Neale Ferguson fec789047a Add support for the loading of ET_DYN objects
* libs/libc/machine/arm/armv7-m/arch_elf.c
      - Fix cast of error message parameters

    * build-globals.sh
      - Build the modlib_globals.S file used to resolve symbols when dynamically loading

    * libs/libc/modlib/Make.defs
      - Build modlib_globals.S for all targets

    * libs/libc/modlib/modlib_load.c
      - Calculate sizes and text/data addresses based on program headers rather than section headers
      - Handle objects with no program headers

    * libs/libc/modlib/modlib_bind.c
      - Call modlib_readsym with pointer to symbol table
      - Add modlib_relocatedyn to manage relocation of symbols with shared object (ET_DYN)
      - Differentiate between ET_REL and ET_DYN objects
      - Use arch independent symbol ELF_R_SYM
      - Cast sizes to avoid warnings

    * libs/libc/modlib/modlib_load.c
      - Cast sizes to avoid warnings

    * include/elf.h
      - Add definitions that may be found in shared objects

    * include/nuttx/lib/modlib.h
      - Add parameter to modlib_readsym prototype
      - Add prototypes for:
        - modlib_insertsymtab
        - modlib_findglobal
      - Define Elf_Dyn and Elf_Off according to the elf architecture
      - Add fields to mod_loadifno_s:
        - Program headers
        - Exported symbols
        - Data section address
        - Padding requirement
        - Section index for dynamic symbol table
        - Number of symbols exported
      - Add prottotype for modlib_freesymtab

    * libs/libc/dlfcn/lib_dlclose.c
      - Free the symbol table when the dll is closed

    * libs/libc/dlfcn/lib_dlopen.c
      - Add dump of program headers to debug routine
      - Differentiate between ET_REL and ET_DYN objects

    * libs/libc/machine/arm/armv7-m/arch_elf.c
      - Add handling of R_ARM_RELATIVE and R_ARM_JUMP slot relocation types

    * libs/libc/modlib/modlib_loadshdrs.c
      - Rename modlib_loadshdrs.c to modlib_loadhdrs.c
      - Rename modlib_loadshdrs to modlib_loadhdrs
      - Add code to load program headers

    * libs/libc/modlib/modlib_symbols.c
      - Define entry point structure
      - Add offset parameter to modlib_symname() and use to find symbol names
      - Add symtab section header parameter to modlib_readsym()
      - Add offset parameter to modlib_symvalue() to locate symbol names
      - Add modlib_insertsyntab() to create a symbol table for exporting and resolution
      - Add findEP() to resolve a symbol in the modlib_global table
      - Add modlib_findglobal() to find symbol in the modlib_global table
      - Add modlib_freesymtab() to free the symbol table

    * libs/libc/modlib/modlib_uninit.c
      - Free header and sections from a module_loadinfo_s control block

    * libs/libc/modlib/modlib_verify.c
      - Handle ET_DYN shared objects

    * libs/libc/modlib/modlib_globals.S
      - Multi-target global table
      - Define library APIs that may be resolved when loading a shared object
2023-07-06 09:13:38 -03:00
Xiang Xiao 7990f90915 Indent the define statement by two spaces
follow the code style convention

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-21 09:52:08 -03:00
Petro Karashchenko 4309c6693c libc/modlib: fix indentation issue
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-03-26 09:23:50 -06:00
Gustavo Henrique Nihei e6b204f438 nuttx: Use MIN/MAX definitions from "sys/param.h"
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2023-02-01 23:47:44 +08:00
Nathan Hartman 849f760b77 Fix various typos 2022-07-08 02:15:54 +08:00
anjiahao b88a8cf39f use rmutex inside of all repeated implementation
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2022-05-30 19:43:48 +08:00
YAMAMOTO Takashi 75b1358d8e modlib_bind.c: Restore "Relocation address out of range" checks
Restore the checks which got completely broken by the following change.
```
commit 15142a8b10
Author: anjiahao <anjiahao@xiaomi.com>
Date:   Fri Apr 1 16:20:03 2022 +0800

    modlib/modlib_bind:fix unsigned_compare with zero

    Signed-off-by: anjiahao <anjiahao@xiaomi.com>
```
2022-04-04 09:12:37 +03:00
anjiahao 5b214888aa Update libs/libc/modlib/modlib_bind.c
Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2022-04-02 01:05:22 +08:00
anjiahao c7badac601 Update libs/libc/modlib/modlib_bind.c
u are right

Co-authored-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2022-04-02 01:05:22 +08:00
anjiahao 15142a8b10 modlib/modlib_bind:fix unsigned_compare with zero
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
2022-04-02 01:05:22 +08:00