zephyr: Cannot run a unit test on Mac OSX with M1 Chip
When I attempt to build the rb_tree unit tests on my Mac Laptop, with an M1 Chip, I get the following build error:
zephyr/tests/unit/rbtree/main.c:280:1: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a comma
ZTEST_SUITE(rbtree_api, NULL, NULL, NULL, NULL, NULL);
^
zephyr/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h:220:15: note: expanded from macro 'ZTEST_SUITE'
static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \
^
zephyr/include/zephyr/toolchain/common.h:215:2: note: expanded from macro 'STRUCT_SECTION_ITERABLE'
__in_section(_##struct_type, static, name) __used __noasan
^
zephyr/include/zephyr/toolchain/gcc.h:178:31: note: expanded from macro '__in_section'
#define __in_section(a, b, c) ___in_section(a, b, c)
^
zephyr/include/zephyr/toolchain/gcc.h:175:25: note: expanded from macro '___in_section'
__attribute__((section("." Z_STRINGIFY(a)
Additional Information
- I am using commit
a337bfc3b319825788b8e3c4a4764f8b8098d22e - Apple M1 (ARM-based) Chip
To Reproduce
- Clone the repository
- Run
west init - Run
west update - Run
source zephyr-env.sh - Run
./scripts/twister -W -v -k --testsuite-root tests/unit/rbtree/
Expected behavior I would expect the tests to build and run, especially unit tests.
Impact
I wanted to test and experiment with the rb_tree data structure locally, prior to using on a microcontroller. I wanted to do so through unit testing, I am not able to do this.
Logs and console output
zephyr/tests/unit/rbtree/main.c:280:1: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a comma
ZTEST_SUITE(rbtree_api, NULL, NULL, NULL, NULL, NULL);
^
zephyr/subsys/testsuite/ztest/include/zephyr/ztest_test_new.h:220:15: note: expanded from macro 'ZTEST_SUITE'
static const STRUCT_SECTION_ITERABLE(ztest_suite_node, \
^
zephyr/include/zephyr/toolchain/common.h:215:2: note: expanded from macro 'STRUCT_SECTION_ITERABLE'
__in_section(_##struct_type, static, name) __used __noasan
^
zephyr/include/zephyr/toolchain/gcc.h:178:31: note: expanded from macro '__in_section'
#define __in_section(a, b, c) ___in_section(a, b, c)
^
zephyr/include/zephyr/toolchain/gcc.h:175:25: note: expanded from macro '___in_section'
__attribute__((section("." Z_STRINGIFY(a)
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (15 by maintainers)
Unfortunately, it’s not nearly that simple.
Mach-O segment and section names have a 16-char limit. In order to even get zephyr to compile cleanly, LLVM needs to be modified to generate hashes for section names instead of using the actual section name. Either that or every section name in Zephyr needs to be modified to fit inside 16 chars.
Note: instead of speculating, I would strongly recommend going over the slides here
https://bit.ly/3LnXj8S
Pay careful attention to the links, as they do point to existing branches where work has already been done on e.g. Zephyr, LLVM, etc.
Note: a large portion of ELF and Linux-isms could be removed from Zephyr if we switched to using link-time optimization (LTO). With that, binary format would be irrelevant and Zephyr would be portable, ISO standard C.
hey all, I haven’t forgotten about this; I have been a bit distracted with personal matters these last few months. I apologize.
I have time to work on this this upcoming week, so I will. @cfriedt I will have a PR or a suggested approach(s) this week. Thanks for your patience everyone.
First start off with an #ifdef in the correct header file. If you are able to craft a new macro, you might run into the 16-char limit in Mach-O section / segment names.
This was mentioned in my ZDS talk.
Good luck!
@cfriedt but the thing is that Unit Tests are native macOS applications that do not use things like custom linker scripts or any other features of the Zephyr build system. They are just regular CMake portable applications that can be built with any compiler on any operating system.
EDIT: Actually ok, I see that ztest uses section names! OK, then you indeed have a point here, let’s see what @tejlmand comes up with. I think the simplest would be to simplify ztest so it doesn’t use any fancy section functionality and so we can use it for Unit Tests anywhere, regardless of compiler capabilities.