apache / nuttx-apps

Apache NuttX Apps is a collection of tools, shells, network utilities, libraries, interpreters and can be used with the NuttX RTOS
https://nuttx.apache.org/
Apache License 2.0
264 stars 493 forks source link

Issue building ELF programs with no symbol table. #1828

Open g2gps opened 1 year ago

g2gps commented 1 year ago

This was originally raised by @acassis in #1816 and #1810. However I've only just got a chance to have a look at it.

To summarize, applications built against Nuttx export when following the instructions outlined in the wiki entry, and on Alan's video on the Nuttx channel fail to launch.

As reported, the result when trying to load the application following the above method is as follows:

NuttShell (NSH) NuttX-12.1.0
nsh> ls /dev
/dev:
 console
 null
 sda
 ttyS0
nsh> hello
Hello, World!!

nsh> mount -t vfat /dev/sda /bin
nsh> ls /bin
/bin:
 Testing/
 hello.txt
 hello
nsh> hello
elf_symname: Symbol has no name
elf_symvalue: SHN_UNDEF: Failed to get symbol name: -3
elf_relocate: Section 2 reloc 0: Undefined symbol[0] has no name: -3
_assert: Current Version: NuttX  12.1.0 9603288c80-dirty Jun 26 2023 20:01:55 arm
_assert: Assertion failed panic: at file: :0 task: hello 0x200033c8
up_dump_register: R0: 00000001 R1: 20004948 R2: 20004948  R3: 00000001
up_dump_register: R4: 200033c8 R5: 00000001 R6: 20004948  FP: 00000000
up_dump_register: R8: 00000000 SB: 00000000 SL: 00000000 R11: 00000000
up_dump_register: IP: 00000000 SP: 20005120 LR: 0800508f  PC: 200033c8
up_dump_register: xPSR: 80000000 PRIMASK: 00000000 CONTROL: 00000000
up_dump_register: EXC_RETURN: fffffff9
dump_stack: User Stack:
dump_stack:   base: 0x20004958
dump_stack:   size: 00002008
dump_stack:     sp: 0x20005120
dump_tasks:    PID GROUP PRI POLICY   TYPE    NPX STATE   EVENT      SIGMASK          STACKBASD
dump_task:       0     0   0 FIFO     Kthread N-- Ready              0000000000000000 0x20000dk
dump_task:       1     1 224 RR       Kthread --- Waiting Semaphore  0000000000000000 0x200021c
dump_task:       3     3 100 RR       Kthread --- Waiting Semaphore  0000000000000000 0x200039t
dump_task:       4     4 100 RR       Task    --- Waiting Semaphore  0000000000000000 0x200041n
dump_task:       6     6 100 RR       Task    --- Running            0000000000000000 0x200049

On inspection, it seems that ARCHCPUFLAGS in the exported package may just be missing from CFLAGS in the example Makefile:

--- Makefile.original   2023-07-11 22:32:06.193495576 +1000
+++ Makefile.min        2023-07-11 22:33:23.893993718 +1000
@@ -7,7 +7,7 @@
 ARCHOPTIMIZATION = -Os -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer
 ARCHINCLUDES = -I. -isystem  nuttx-export-7.25/include

-CFLAGS = $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHINCLUDES) -pipe
+CFLAGS = $(ARCHCPUFLAGS) $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHINCLUDES) -pipe

 CROSSDEV = arm-none-eabi-
 CC = $(CROSSDEV)gcc

To test, the expected arcitecture, from the inspecting the kernel:

../nuttx/nuttx:     file format elf32-littlearm
../nuttx/nuttx
architecture: armv7e-m, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x08000189

The addon program with the original makefile:

hello.old:     file format elf32-littlearm
hello.old
architecture: armv4t, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000000
private flags = 0x5000000: [Version5 EABI]

The addon program with the updated makefile:

hello:     file format elf32-littlearm
hello
architecture: armv7e-m, flags 0x00000011:
HAS_RELOC, HAS_SYMS
start address 0x00000001
private flags = 0x5000000: [Version5 EABI]

For reference, the complete update Makefile I'm using is:

EXPORT_PACKAGE=nuttx-export-12.2.1-RC0
include $(EXPORT_PACKAGE)/scripts/Make.defs

ARCHCFLAGS += -mlong-calls 
ARCHWARNINGS += -Wall -Wstrict-prototypes -Wshadow -Wundef
ARCHOPTIMIZATION += -Os -fno-strict-aliasing -fno-strength-reduce -fomit-frame-pointer
ARCHINCLUDES += -I. -isystem  $(EXPORT_PACKAGE)/include

CFLAGS =  $(ARCHCPUFLAGS) $(ARCHCFLAGS) $(ARCHWARNINGS) $(ARCHOPTIMIZATION) $(ARCHINCLUDES) -pipe

# Setup up linker command line options

LDRELFLAGS = -r 

LDELFFLAGS = -r -e main 
LDELFFLAGS += -T defines.ld -T gnu-elf.ld

# This might change in a different environment

OBJEXT ?= .o

# This is the generated ELF program

BIN = hello
REL = hello.r

# These are the sources files that we use

SRCS = hello.c
OBJS = $(SRCS:.c=$(OBJEXT))

# Build targets

all: $(BIN)
.PHONY: clean

System.map: $(EXPORT_PACKAGE)/System.map
    cat $(EXPORT_PACKAGE)/System.map | sed -e "s/\r//g" >System.map

$(OBJS): %$(OBJEXT): %.c
    $(CC) -c $(CFLAGS) -o $@ $<

$(REL): $(OBJS)
    $(LD) $(LDRELFLAGS) -o $@ $<

defines.ld: System.map $(REL)
    ./mkdefines.sh System.map "$(REL)" >defines.ld

$(BIN): defines.ld $(REL)
    $(LD) $(LDELFFLAGS) -o $@ $(REL)
    $(STRIP) $(REL)

clean:
    rm -f $(BIN)
    rm -f $(REL)
    rm -f defines.ld
    rm -f System.map
    rm -f *.o

The other files from the wiki entry remain unchanged.

I don't have access to any appropriate hardware to test this change on. However, I was able to try out the process with the rv-virt:nsh, running the application from hostfs:

NuttShell (NSH) NuttX-12.2.1-RC0
nsh> mount -t hostfs -o fs=./addon /bin
nsh> hello
Hello from Add-On Program!
nsh> 

@acassis would you mind testing this on your end, with the target hardware to see if it resolves the issue.

acassis commented 1 month ago

hi @g2gps I think I forgot it in the backlog, I want to test it again to create a NuttX distro