nuttx icon indicating copy to clipboard operation
nuttx copied to clipboard

Add support for the loading of ET_DYN objects

Open nealef opened this issue 3 years ago • 1 comments

This is not a real PR. It follows on from my presentation at NuttX 2022 Workshop. It is my 1st attempt to take my changes to our backlevel NuttX repo and apply it to the current master. It is for information only as people expressed a desire to see what was done so I thought this was the best way to do it.

Caveats

  • Created specifically for the Meadow project which uses an arm7m - not a general solution
  • Built using a custom configuration
    • Important bits:
    CONFIG_BUILD_PROTECTED=y
    CONFIG_BUILD_2PASS=y
    CONFIG_PASS1_TARGET="all"
    CONFIG_PASS1_BUILDIR="boards/arm/stm32/olimex-stm32-p407/kernel"
    CONFIG_PASS1_OBJECT=""
    CONFIG_NUTTX_USERSPACE=0x08020000
    :
    CONFIG_LIBC_DLFCN=y 
    CONFIG_LIBC_MODLIB=y
    :
    #
    # Module library configuration
    #
    CONFIG_MODLIB_MAXDEPEND=2
    CONFIG_MODLIB_ALIGN_LOG2=2
    CONFIG_MODLIB_BUFFERSIZE=32
    CONFIG_MODLIB_BUFFERINCR=32
    CONFIG_MODLIB_RELOCATION_BUFFERCOUNT=256
    CONFIG_MODLIB_SYMBOL_CACHECOUNT=256
  • Not tried in other modes other than PROTECTED
  • Does not handle .so objects having dependencies on other .so objects
  • Only armv7-m has its arch_elf.c updated with the additional relocation types
  • It builds cleanly but I have not run to verify functionality

Change Descriptions

  • build-globals.sh

    • Build the modlib_globals.S file used to resolve symbols when dynamically loading
    • I have not looked at mksymtab.c to see if this will generate an architecture-independent of what I am attempting to create with this script
  • include/elf.h

    • Add definitions that may be found in shared objects
  • include/nuttx/lib/modlib.h

    • 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/Make.defs

    • Include new file containing library APIs used when loading shared objects
  • libs/libc/modlib/modlib.h

    • Add parameter to modlib_readsym prototype
    • Add prototypes for:
      • modlib_insertsymtab
      • modlib_findglobal
  • 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_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
  • libs/libc/modlib/modlib_load.c

    • Calculate sizes and text/data addresses based on program headers rather than section 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

    • Define library APIs that may be resolved when loading a shared object

nealef avatar Sep 27 '22 12:09 nealef

Hi @nealef thank you very much for submitting it! Now we can use it as base to polish it to integrate on NuttX! This is an amazing contribution! Thank you and WildernessLab for doing it!

acassis avatar Sep 27 '22 18:09 acassis

@xiaoxiang781216 :

====================================================================================
Configuration/Tool: c5471evm/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found
/usr/bin/bash: arm-nuttx-eabi-gcc: command not found

Why is it failing to find arm-nuttx-eabi-gcc ?

acassis avatar Jun 13 '23 14:06 acassis

Why is it failing to find arm-nuttx-eabi-gcc ?

arm-nuttx-eabi-gcc is the "buildroot" toolchain (https://bitbucket.org/nuttx/buildroot/src/master/) which you probably do not have installed.

patacongo avatar Jun 13 '23 15:06 patacongo

@acassis please ignore the warning which doesn't block the ci.

xiaoxiang781216 avatar Jun 13 '23 15:06 xiaoxiang781216

@xiaoxiang781216 I'm getting this error when compiling for sim:ostest:

LD:  nuttx
/usr/bin/ld: nuttx.rel: relocation R_X86_64_16 against `.data' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: failed to set dynamic section sizes: bad value
collect2: error: ld returned 1 exit status
make[1]: *** [Makefile:368: nuttx] Error 1
make: *** [tools/Unix.mk:527: nuttx] Error 2

Please find attached the modifications I did to fix the "undefined reference to nGlobals and globalTable " 0001-Move-build-globals.sh-to-tools-and-add-modlib_x64_gl.zip

acassis avatar Jun 15 '23 14:06 acassis

@nealef see https://github.com/apache/nuttx/actions/runs/5281758940/jobs/9555847442?pr=7202

It is failing with same error I reported above! Any idea?

acassis avatar Jun 15 '23 19:06 acassis

Yes, I forgot to add the .S file (did a git commit -a rather than a git add . for these new files). It has built again but we now fail with a relocation error.

nealef avatar Jun 15 '23 19:06 nealef

Thank you @nealef now it worked here!

acassis avatar Jul 06 '23 12:07 acassis

@acassis I see that PR CI fails. Are we sure that changes are safe to merge?

pkarashchenko avatar Jul 06 '23 12:07 pkarashchenko

Also style check fails.

2023-07-06T04:46:48.5913429Z ##[error]/home/runner/work/nuttx/nuttx/nuttx/libs/libc/dlfcn/lib_dlopen.c:251:38: error: Dangling whitespace at the end of line
2023-07-06T04:46:48.5994412Z ##[error]/home/runner/work/nuttx/nuttx/nuttx/libs/libc/machine/arm/armv7-m/arch_elf.c:499:79: error: Long line found

Are from changes in this PR

pkarashchenko avatar Jul 06 '23 12:07 pkarashchenko