You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@nuttx.apache.org by Byron Ellacott <bj...@the-wanderers.org> on 2021/03/12 13:01:30 UTC

Symbol tables, ELF binaries, and mksymtab

Hi,

I've been working on ELF binary loading for the eZ80. I'm compiling
with CONFIG_SYSTEM_NSH=m to produce a stand-alone nsh binary.

I'm finding the symbol tables (both the one generated in libs/libc
with CONFIG_EXECFUNCS_SYSTEM_SYMTAB=y and the one generated in apps) to be
wrong when it comes to C functions having a prepended underscore - the
system symtab doesn't prepend an underscore, so all symbol lookups fail.
The apps symtab does prepend an underscore to its "extern" definitions, so
any use of that symbol table fails with undefined external references.

Is this a configuration issue on my end?

I've modified tools/mksymtab to (a) prepend an underscore to the symbol's
name, and (b) prepend an "&" to the symbol's value. The latter change
allows exporting global variables like optarg and optind - I've also added
a number of missing symbols to syscall/syscall.csv and libs/libc/libc.csv.

I can now boot a NuttX kernel with no shell and successfully execute nsh
from SD card after my changes, but before I start making PRs I'd like to
understand if the difficulties I've faced are a PEBKAC issue or something
others encounter.

(I'll also be contributing the Z80 elf loader, but it's not useful if
you're using ZDS-II.)

-- 
Byron

Re: Symbol tables, ELF binaries, and mksymtab

Posted by Byron Ellacott <bj...@the-wanderers.org>.
Hi all,

Thanks for the feedback.

I'm looking at option (2), modifying
`libs/libc/symtab/symtab_find(ordered)?byname.c` to strip a leading
underscore if a new Kconfig setting (SYMTAB_DECORATED in binfmt/Kconfig) is
set. I considered modifying the ELF loader, but the kernel module system
needs the same adjustment when making the same calls.

The NXFLAT loader 'benefits' too, but I think toolchains that prefix
underscores and toolchains that support the NXFLAT toolchain don't overlap.

-- 
bje

On Mon, Mar 15, 2021 at 12:40 AM Gregory Nutt <sp...@gmail.com> wrote:

> > Which I think is insufficient.  Probably some of the support was removed?
> >
> No.. It is just that there was no support for loadable modules for those
> older Cygwin toolchains.  The underscore has not been used with Cygwin
> since around 2015 so I think would be safe to just remove the
> CONFIG_SIM_CYGWIN_DECORATED configuration.
>
> In your case, I think you have two options:
>
> 1) Write a tool to rename all of the symbols in the library to remove
> the leading underscore (using objcopy --redefine-syms), or
>
> 2) Modify the elf loader[s] to conditionally prepend the '_' character
> before looking up the symbol by name.
>
>

Re: Symbol tables, ELF binaries, and mksymtab

Posted by Gregory Nutt <sp...@gmail.com>.
> Which I think is insufficient.  Probably some of the support was removed?
>
No.. It is just that there was no support for loadable modules for those 
older Cygwin toolchains.  The underscore has not been used with Cygwin 
since around 2015 so I think would be safe to just remove the 
CONFIG_SIM_CYGWIN_DECORATED configuration.

In your case, I think you have two options:

1) Write a tool to rename all of the symbols in the library to remove 
the leading underscore (using objcopy --redefine-syms), or

2) Modify the elf loader[s] to conditionally prepend the '_' character 
before looking up the symbol by name.


Re: Symbol tables, ELF binaries, and mksymtab

Posted by Gregory Nutt <sp...@gmail.com>.
> No, it seem that mksymtab forget to handle the case you described.

This was handled in the past, but I forget where the logic was. Let me 
look around a bit.

... after a bit ...

Googling, I find that there was some handling in the simulator for 
Cygwin.  Some older tool toolchains did prepend the underscore character 
to the symbol name.  Look at CONFIG_SIM_CYGWIN_DECORATED.

      46 config SIM_CYGWIN_DECORATED
      47         bool "Decorated Cygwin names"
      48         default n
      49         depends on WINDOWS_CYGWIN
      50         ---help---
      51                 Older versions of Cygwin tools decorated C
    symbol names by adding an
      52                 underscore to the beginning of the symbol
    name.  Newer versions of
      53                 Cygwin do not seem to do this.
      54
      55                 How do you know if you need this option? You
    could look at the generated
      56                 symbol tables to see if there are underscore
    characters at the beginning
      57                 of the symbol names.  Or, if you need this
    option, the simulation will not
      58                 run:  It will crash early, probably in some
    function due to the failure to
      59                 allocate memory.

Currently that appears only at:

  * arch/sim/Kconfig:config SIM_CYGWIN_DECORATED
  * arch/sim/src/nuttx-names.in: (defined(CONFIG_HOST_WINDOWS) &&
    defined(CONFIG_SIM_CYGWIN_DECORATED))
  * boards/sim/sim/README.txt: CONFIG_SIM_CYGWIN_DECORATED=n
  * boards/sim/sim/README.txt:    do not seem to do this.  Deselecting
    CONFIG_SIM_CYGWIN_DECORATED will

Which I think is insufficient.  Probably some of the support was removed?

I was thinking that there was a similar underscore issue with older SDCC 
toolchains, but I can't find anything now (except some references in 
arch/z80/src/Makefile.sdccw).



RE: Symbol tables, ELF binaries, and mksymtab

Posted by Xiang Xiao <xi...@gmail.com>.

> -----Original Message-----
> From: Byron Ellacott <bj...@the-wanderers.org>
> Sent: Friday, March 12, 2021 9:02 PM
> To: dev@nuttx.apache.org
> Subject: Symbol tables, ELF binaries, and mksymtab
> 
> Hi,
> 
> I've been working on ELF binary loading for the eZ80. I'm compiling with CONFIG_SYSTEM_NSH=m to produce a stand-alone nsh binary.
> 
> I'm finding the symbol tables (both the one generated in libs/libc with CONFIG_EXECFUNCS_SYSTEM_SYMTAB=y and the one generated in
> apps) to be wrong when it comes to C functions having a prepended underscore - the system symtab doesn't prepend an underscore, so all
> symbol lookups fail.
> The apps symtab does prepend an underscore to its "extern" definitions, so any use of that symbol table fails with undefined external
> references.
> 
> Is this a configuration issue on my end?
> 

No, it seem that mksymtab forget to handle the case you described.

> I've modified tools/mksymtab to (a) prepend an underscore to the symbol's name, and (b) prepend an "&" to the symbol's value. The latter
> change allows exporting global variables like optarg and optind - I've also added a number of missing symbols to syscall/syscall.csv and
> libs/libc/libc.csv.
> 
> I can now boot a NuttX kernel with no shell and successfully execute nsh from SD card after my changes, but before I start making PRs I'd
> like to understand if the difficulties I've faced are a PEBKAC issue or something others encounter.
> 

Most people try ELF loader on ARM and RISCV platform, both don't pretend an underscore to the symbol's name. So you may the first people encounter this mismatch issue☹.

> (I'll also be contributing the Z80 elf loader, but it's not useful if you're using ZDS-II.)
> 
> --
> Byron