Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include/err: Enforce c linkage for err and warn functions. #9147

Merged
merged 1 commit into from May 1, 2023

Conversation

g2gps
Copy link
Contributor

@g2gps g2gps commented May 1, 2023

Summary

This change ensures that xx_func names are not mangled by a c++ compiler and can be correctly resolved when loaded at runtime.

Impact

c++ applications which use err can be loaded correctly.

Testing

The issue without this addition can be reproduced by following:

  1. Use the err function from a c++ application:
diff --git a/examples/helloxx/helloxx_main.cxx b/examples/helloxx/helloxx_main.cxx
index c64891de1..93a38be58 100644
--- a/examples/helloxx/helloxx_main.cxx
+++ b/examples/helloxx/helloxx_main.cxx
@@ -26,6 +26,7 @@
 
 #include <cstdio>
 #include <debug.h>
+#include <err.h>
 
 //***************************************************************************
 // Definitions
@@ -124,6 +125,8 @@ extern "C" int main(int argc, FAR char *argv[])
   g_HelloWorld.HelloWorld();
 #endif
 
+  err(11, "This produces an undefined symbol\n");
+
   delete pHelloWorld;
   return 0;
 }
  1. Add BINFORMAT debugging and add helloxx application to rv-virt:knsh32:
diff --git a/boards/risc-v/qemu-rv/rv-virt/configs/knsh32/defconfig b/boards/risc-v/qemu-rv/rv-virt/configs/knsh32/defconfig
index 86d381bd07..f4bdd20da5 100644
--- a/boards/risc-v/qemu-rv/rv-virt/configs/knsh32/defconfig
+++ b/boards/risc-v/qemu-rv/rv-virt/configs/knsh32/defconfig
@@ -44,10 +44,18 @@ CONFIG_ARCH_USE_S_MODE=y
 CONFIG_BOARD_LOOPSPERMSEC=6366
 CONFIG_BUILD_KERNEL=y
 CONFIG_DEV_ZERO=y
+CONFIG_DEBUG_BINFMT=y
+CONFIG_DEBUG_BINFMT_ERROR=y
+CONFIG_DEBUG_ERROR=y
+CONFIG_DEBUG_FEATURES=y
+CONFIG_DEBUG_INFO=y
+CONFIG_DEBUG_WARN=y
 CONFIG_ELF=y
 CONFIG_EXAMPLES_HELLO=m
+CONFIG_EXAMPLES_HELLOXX=m
 CONFIG_FS_HOSTFS=y
 CONFIG_FS_PROCFS=y
+CONFIG_HAVE_CXX=y
 CONFIG_IDLETHREAD_STACKSIZE=3072
 CONFIG_INIT_FILEPATH="/system/bin/init"
 CONFIG_INIT_MOUNT=y

Program output when helloxx is ran from nsh:

...
[    3.347000] elf_symvalue: SHN_UNDEF: Exported symbol "_Z8err_funciPKcz" not found
[    3.348000] elf_relocateadd: Section 2 reloc 77: Failed to get value of symbol[270]: -2
[    3.350000] elf_loadbinary: Failed to bind symbols program binary: -2
[    3.351000] exec_spawn: ERROR: Failed to load program 'helloxx': -2

Using objdump riscv64-unknown-elf-objdump apps/bin/helloxx -x | c++filt | less

SYMBOL TABLE:
...
...
00000000         *UND*  00000000 err_func(int, char const*, ...)

With the additions included in the PR, the symbol is no longed undefined with the application building and launching successfully.

This change ensures that xx_func names are not mangled by a c++ compiler
and can be correctly resolved when loaded at runtime.
@xiaoxiang781216 xiaoxiang781216 merged commit a9fc76c into apache:master May 1, 2023
26 checks passed
@g2gps g2gps deleted the libc-err-c-linkage branch May 5, 2023 00:37
@jerpelea jerpelea added this to To-Add in Release Notes - 12.2.0 Jun 13, 2023
@jerpelea jerpelea moved this from To-Add to In Progress in Release Notes - 12.2.0 Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants