Skip to content

Latest commit

 

History

History
134 lines (99 loc) · 4.84 KB

libc.md

File metadata and controls

134 lines (99 loc) · 4.84 KB

libc Usage in sDDF Components

sDDF supports two modes for accessing standard C library functionality, depending on whether an external libc is available in the build environment.

Vendored libc (Internal sDDF Implementation)

Motivation

While sDDF components do not need a full libc like you would expect on an OS like Linux, there are certain basic functions such as memcpy or strlen that are expected.

It is trivial to write our own implementations of these functions, however for high-performance use-cases such as networking we need optimised implementations which is why we vendor them.

Overview

sDDF includes a minimal subset of the C standard library, vendored from newlib at commit 4613a9a9799792a53444576de61bd1569f346ffa for aarch64 and riscv64 architectures, suitable for use in environments without a full libc.

The vendored subset includes the following library functions:

The following are provided (optimised for the aarch64, x86_64, riscv64 architectures):

  • memcmp
  • memcpy
  • memmove
  • memset
  • strcmp
  • strcpy
  • strlen
  • strncmp

Note that compiler builtins are preferred when available; the sDDF library headers already map standard functions to __builtin_* equivalents using feature detection.

Internal implementations of atoi and strcat are also included.

Usage

If you wish to use the vendored libc in your sDDF system, the sDDF utility library includes the provided libc functions. All the existing sDDF example systems already use this library to provide functionality like printing in either its serial (libsddf_util.a) or debug (libsddf_util_debug.a) form.

To simplify this, we have created the SDDF_CUSTOM_LIBC configuration variable.

Makefile

First, you must set the custom libc configuration variable to 1 in your system's Makefile:

SDDF_CUSTOM_LIBC := 1

You must also ensure the sDDF utility library make snippet is included, and that one or more utility libraries are a target of your Makefile. You will also need to ensure that the library is declared as a pre-requisite for any components depending on it:

include ${SDDF}/util/util.mk

${IMAGES}: libsddf_util_debug.a

The utility library make snippet will then build and archive the libc functions, and add the required include path to your system's CFLAGS.

Finally, you will need to ensure that all components requiring the libc functions are linked with the desired utility library (this is typically done by adding libsddf_util_debug to your linker LIBS).

External libc (OS-Provided)

When building sDDF components as part of an operating system that provides its own libc (e.g. musl), the vendored implementations should be disabled. This is done by default when SDDF_CUSTOM_LIBC is not defined.

To allow libc dependencies to still be tracked when an external libc is to be used, we have created a distinct SDDF_LIBC_INCLUDE path/configuration variable that typically contains the path of the external libc headers. This works best when the path is a Makefile target in your external system's Makefile. You can read more about how this is used in LionsOS here.

Usage

To use an external libc:

Makefile

Ensure that SDDF_CUSTOM_LIBC is not set.

Set SDDF_LIBC_INCLUDE to the external libc header path, and ensure this definition is visible to all sDDF component Makefiles. If you are building this path and headers, ensure there is a matching target in your Makefile. sDDF components treat this as an order-only prerequisite to ensure headers are available before compilation (avoiding unnecessary rebuilds).

Add the external libc header path (typically SDDF_LIBC_INCLUDE) to the CFLAGS of the sDDF components. Also ensure that sDDF components are linked with the external libc when required.

Developing

When developing an sDDF example system or component, you typically use the vendored libc in the utility library. Since components depend on this library for functionality outside of just libc, most example systems list the utility library as a dependency for all components, thus per-component libc dependency can easily be ignored.

However, when an external libc is used, it is important to keep track of per-component libc dependency to avoid future build dependency issues. When creating a new make snippet for a component or system, you must ensure that any components requiring libc functionalities are marked as having this pre-requisite using SDDF_LIBC_INCLUDE:

network/imx/ethernet.o: ... | $(SDDF_LIBC_INCLUDE)
    ...