-
Notifications
You must be signed in to change notification settings - Fork 101
Add LLVM libc sample #538
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
Add LLVM libc sample #538
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # | ||
| # Copyright (c) 2020-2024, Arm Limited and affiliates. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| include ../../../Makefile.conf | ||
|
|
||
| build: hello.elf | ||
|
|
||
| hello.elf: *.c | ||
| ../$(BIN_PATH)/clang --config=llvmlibc.cfg $(MICROBIT_TARGET) -nostartfiles -lsemihost -g -fno-exceptions -fno-rtti -T microbit-llvmlibc.ld -o hello.elf $^ | ||
|
|
||
| %.hex: %.elf | ||
| ../$(BIN_PATH)/llvm-objcopy -O ihex $< $@ | ||
|
|
||
| run: hello.hex | ||
| qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$< | ||
|
|
||
| debug: hello.hex | ||
| qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$< -s -S | ||
|
|
||
| clean: | ||
| rm -f *.elf *.hex | ||
|
|
||
| .PHONY: clean run debug |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) 2024, Arm Limited and affiliates. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
|
|
||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <stddef.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdint.h> | ||
|
|
||
| extern int main(int argc, char** argv); | ||
|
|
||
| extern void _platform_init(); | ||
|
|
||
| extern char __data_source[]; | ||
| extern char __data_start[]; | ||
| extern char __data_end[]; | ||
| extern char __data_size[]; | ||
| extern char __bss_start[]; | ||
| extern char __bss_end[]; | ||
| extern char __bss_size[]; | ||
| extern char __tls_base[]; | ||
| extern char __tdata_end[]; | ||
| extern char __tls_end[]; | ||
|
|
||
| void _start(void) { | ||
| memcpy(__data_start, __data_source, (size_t) __data_size); | ||
| memset(__bss_start, 0, (size_t) __bss_size); | ||
| _platform_init(); | ||
| _Exit(main(0, NULL)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) 2024, Arm Limited and affiliates. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
|
|
||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <assert.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <math.h> | ||
|
|
||
| // Implementation of errno | ||
| int *__llvm_libc_errno() { | ||
| static int errno; | ||
| return &errno; | ||
| } | ||
|
|
||
| // Example that uses heap, string and math library. | ||
|
|
||
| int main(void) { | ||
| const char *hello_s = "hello "; | ||
| const char *world_s = "world"; | ||
| const size_t hello_s_len = strlen(hello_s); | ||
| const size_t world_s_len = strlen(world_s); | ||
| const size_t out_s_len = hello_s_len + world_s_len + 1; | ||
| char *out_s = (char*) malloc(out_s_len); | ||
| assert(out_s_len >= hello_s_len + 1); | ||
| strncpy(out_s, hello_s, hello_s_len + 1); | ||
| assert(out_s_len >= strlen(out_s) + world_s_len + 1); | ||
| strncat(out_s, world_s, world_s_len + 1); | ||
| // 2024-10-17 Embedded printf implementation does not currently | ||
| // support printing floating point numbers. | ||
| printf("%s %li\n", out_s, lround(400000 * atanf(1.0f))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because you wrote I think that's actually a good thing – it makes it a better test! I point it out because you probably didn't do it on purpose, but I quite like it, and perhaps it's better not to "fix" it :-) |
||
| free(out_s); | ||
| return 0; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to do another pedantic one, but I recommend calling that internal variable something other than
errno. If a user starts from this code and adds#include <errno.h>, then the nameerrnowill surely become a macro, making this function into nonsense. Any other name is fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting. I've used a name that doesn't contain errno.