Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ if(LLVM_TOOLCHAIN_C_LIBRARY STREQUAL llvmlibc)
COMPONENT llvm-toolchain-llvmlibc-configs
)

install(
DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/llvmlibc-samples/src
DESTINATION samples
COMPONENT llvm-toolchain-llvmlibc-configs
)
# We aren't yet able to build C++ libraries to go with llvm-libc
set(CXX_LIBS OFF)

Expand Down
9 changes: 9 additions & 0 deletions docs/llvmlibc.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ following command line options, in addition to `--target`, `-march` or
`__llvm_libc_heap_limit` in addition to whatever other memory layout
you want.

* LLVM libc does not define errno. If you are using a function that
sets errno then you must implement the function `int *__llvm_libc_errno()`
that returns the address of your definition of errno.

For example:

```
clang --config=llvmlibc.cfg --target=arm-none-eabi -march=armv7m -o hello hello.c -lsemihost -lcrt0 -Wl,--defsym=__stack=0x200000
```

## Samples

The overlay package installs a llvmlibc directory in the samples/src
directory containing sample programs that use LLVM libc.

## Limitations of LLVM libc in LLVM Embedded Toolchain for Arm

At present, this toolchain does not build any C++ libraries to go with
Expand Down
37 changes: 37 additions & 0 deletions llvmlibc-samples/src/llvmlibc/baremetal-semihosting/Makefile
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
41 changes: 41 additions & 0 deletions llvmlibc-samples/src/llvmlibc/baremetal-semihosting/crt0llvmlibc.c
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));
}
46 changes: 46 additions & 0 deletions llvmlibc-samples/src/llvmlibc/baremetal-semihosting/hello.c
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;
Copy link
Collaborator

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 name errno will surely become a macro, making this function into nonsense. Any other name is fine.

Copy link
Contributor Author

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.

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)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you wrote lround rather than lroundf, the float returned from atanf is being converted into a double, so this code is testing a mix of single and double precision operations.

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;
}
Loading
Loading