103 Commits

Author SHA1 Message Date
William Huynh
1b9e9e29e2
[libc] Add boot code for AArch64 (#154789)
This is required in hermetic testing downstream. It is not complete, and
will not work on hardware, however it runs on QEMU, and can report a
pass/fail on our tests.
2025-08-21 18:47:34 +00:00
William Huynh
6b16a276ef
[libc] Add startup code for ARM v7-A, ARM v7-R variants (#153576)
These variants require a different exception table that requires a bit
of initialisation.

This allows us to enable testing for these variants downstream.
2025-08-15 09:17:50 +00:00
William Huynh
372d86dcf1
[libc] Cleanup startup/baremetal/arm/start.cpp (#151532)
Post-commit review changes as suggested by @petrhosek in #146863
2025-08-11 15:11:36 +01:00
William Huynh
8c9863eb1e
[libc] Basic implementation of crt0 (#146863)
In order to run hermetic tests downstream (#145349), we need startup
code. This is based on the crt0 that is present in Arm Toolchain:
5446a3cbf4/arm-software/embedded/llvmlibc-support/crt0.
Note that some tests do fail due to lack of hardware setup, which will
be implemented at a later time.

Most of the CMake/structure is a copy from the existing code in both
`libc/startup/linux` and `libc/startup/gpu`. It is required to build an
object file to be linked with.

Also add header files for init/fini such that crt0 can use the provided
symbols.

---------

Co-authored-by: Michael Jones <michaelrj@google.com>
2025-07-31 12:17:17 +01:00
lntue
66603dd1f1
[libc][NFC] Add stdint.h proxy header to fix dependency issue with <stdint.h> includes. (#150303)
https://github.com/llvm/llvm-project/issues/149993
2025-07-23 20:19:52 -04:00
Tristan Ross
865fb9c1a5
[libc][uefi] add crt1 (#132150)
Adds `crt1.o` for the UEFI platform in the LLVM C library. This makes
things start to become useful.
2025-05-09 09:02:33 -07:00
Joseph Huber
d0aa1f9c43
[Clang] Make --lto-partitions only default for HIP (#133164)
Summary:
The default behavior for LTO on other targets does not specify the
number of LTO partitions. Recent changes made this default to 8 on
AMDGPU which had some issues with the `libc` project. The option to
disable this is HIP only so I think for now we should restrict this just
to HIP.

I'm definitely on board with getting some more parallelism here, but I
think it should probably be restricted to just offloading languages. The
new driver goes through the `--target=amdgcn-amd-amdhsa` for its output,
which means we'd need to forward the default somehow.
2025-03-27 07:34:57 -05:00
Joseph Huber
0106e5ad91 [libc] Temporarily disable LTO flags for AMDGPU crt1.o
Summary:
Some recent changes seem to have introduced a bug that breaks this. Turn
it off while I triage it.
2025-03-26 12:55:43 -05:00
Joseph Huber
77363f7518
[libc] Allow building the GPU targets that don't have CRT (#129945)
Summary:
If there's no subdirectory we can't make an alias. This allows it to at
least continue.
2025-03-05 16:39:35 -06:00
Joseph Huber
666a3f4ed4
[libc] Stub TLS functions on the GPU temporarily (#108267)
Summary:
There's an extern weak symbol for this, we should just factor these into
a more common interface. Stub them temporarily to make the bots happy.
PTXAS does not handle extern weak.
2024-09-11 11:36:07 -07:00
Schrodinger ZHU Yifan
779a444009
[libc] fix tls teardown while being used (#108229)
The call chain to `Mutex:lock` can be polluted by stack protector. For
completely safe, let's postpone the main TLS tearing down to a separate
phase.

fix #108030
2024-09-11 12:22:35 -04:00
Michael Jones
2353f484a5
[libc] Include startup code when installing all (#105203)
Previously the libc startup code was marked `EXCLUDE_FROM_ALL` due to
build issues. This patch removes that as no longer necessary.
2024-08-20 13:54:09 -07:00
Joseph Huber
5c13f9aea2
[libc] Add single threaded kernel attributes to AMDGPU startup utility (#104651)
Summary:
I fixed the errors here recently so I can actually use these. This
shouldn't impact much, just should hopefully make the code generated
slightly better.
2024-08-18 12:50:15 -05:00
Schrodinger ZHU Yifan
b7c7dbd473
Revert "libc: Remove extern "C" from main declarations" (#102827)
Reverts llvm/llvm-project#102825
2024-08-11 13:40:50 -07:00
David Blaikie
1b71c471c7
libc: Remove extern "C" from main declarations (#102825)
This is invalid in C++, and clang recently started warning on it as of
#101853
2024-08-11 13:17:27 -07:00
Joseph Huber
1a92cc5a0a
[libc] Implement 'getenv' on the GPU target (#102376)
Summary:
This patch implements 'getenv'. I was torn on how to implement this,
since realistically we only have access to this environment pointer in
the "loader" interface. An alternative would be to use an RPC call every
time, but I think that's overkill for what this will be used for. A
better solution is just to emit a common `DataEnvironment` that contains
all of the host visible resources to initialize. Right now this is the
`env_ptr`, `clock_freq`, and `rpc_client`.

I did this by making the `app.h` interface that Linux uses more general,
could possibly move that into a separate patch, but I figured it's
easier to see with the usage.
2024-08-08 06:45:42 -05:00
Schrodinger ZHU Yifan
72c9e24ed9
[libc][startup] fix main thread TLS unmapped with wrong value (#102009)
We have been unmapping a wrong address all the time.
```c
munmap(0x704cffebb000, 41048)           = 0
munmap(0x704cffec6000, 69632)           = 0
munmap(0x704cffe9f000, 41048)           = 0
munmap(0x704cffeaa000, 69632)           = 0
munmap(0x704cffe83000, 41048)           = 0
munmap(0x704cffe8e000, 69632)           = 0
munmap(0x704cffe67000, 41048)           = 0
munmap(0x704cffe72000, 69632)           = 0
munmap(0x704cffe4b000, 41048)           = 0
munmap(0x704cffe56000, 69632)           = 0
munmap(0x704cffe2f000, 41048)           = 0
munmap(0x704cffe3a000, 69632)           = 0
munmap(0x704cfff51028, 41048)           = -1 EINVAL (Invalid argument)
```
2024-08-05 09:25:14 -07:00
Joseph Huber
2bf58f5d27
[Clang] Suppress missing architecture error when doing LTO (#100652)
Summary:
The `nvlink-wrapper` can do LTO now, which means we can still create
some LLVM-IR without needing an architecture. In the case that we try to
invoke `nvlink` internally, that will still fail. This patch simply
defers the error until later so we can use `--lto-emit-llvm` to get the
IR without specifying an architecture.
2024-07-31 14:41:55 -05:00
Schrodinger ZHU Yifan
3db5c1eeb0
revert all tid changes (#100915) 2024-07-27 22:29:21 -07:00
Joseph Huber
d3ecb16e85 [libc] Fix relocatable link attempting to pull in standard libs 2024-07-26 09:10:06 -05:00
Joseph Huber
0fa53d5183
[libc] Make crt1.o available to clang for GPU builds (#100599)
Summary:
We want to provide the `crt1.o` object inside of the current build so
that other projects can use it for tests. Because it's currently an
object library (as is also required for the dependencies for the
hermetic tests) this cannot be renamed and is not emitted by default.
Here we simply add an executable that emits bitcode so that CMake will
give it a name.
2024-07-26 08:51:25 -05:00
Schrodinger ZHU Yifan
29be889c2c
reland "[libc] implement cached process/thread identity (#98989)" (#99765) 2024-07-20 10:25:40 -07:00
Schrodinger ZHU Yifan
415ca24f8e
Revert "[libc] implement cached process/thread identity" (#99559)
Reverts llvm/llvm-project#98989
2024-07-18 13:31:04 -07:00
Schrodinger ZHU Yifan
5c9fc3cdd7
[libc] implement cached process/thread identity (#98989)
migrated from https://github.com/llvm/llvm-project/pull/95965 due to
corrupted git history
2024-07-18 13:27:50 -07:00
Petr Hosek
5ff3ff33ff
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)
This is a part of #97655.
2024-07-12 09:28:41 -07:00
Mehdi Amini
ce9035f5bd
Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration" (#98593)
Reverts llvm/llvm-project#98075

bots are broken
2024-07-12 09:12:13 +02:00
Petr Hosek
3f30effe1b
[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)
This is a part of #97655.
2024-07-11 12:35:22 -07:00
Petr Hosek
be9b1de1ab
[libc] Move the internal extern "C" symbols inside the namespace (#98232)
This ensures that these symbols inherit the namespace visibility.
2024-07-09 16:22:19 -07:00
Schrodinger ZHU Yifan
1b26bb0d12
[libc] fix aarch64 GCC build (#97932)
This PR fix several build errors on aarch64 targets when building with
gcc:
- uninitialized values leading to `Werrors`
- undefined builtin functions
- glibc header pollution
2024-07-07 12:12:47 -07:00
Nick Desaulniers (paternity leave)
dca49d739d
[libc][arm32] define argc type and stack alignment (#96367)
https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#6212stack-constraints-at-a-public-interface
mentions that the stack on ARM32 is double word aligned.

Remove confused comments around ArgcType. argc is always an int, passed on the
stack, so we need to store a pointer to it (regardless of ILP32 or LP64).
2024-06-25 09:04:19 -07:00
Nick Desaulniers (paternity leave)
bea7f3d873
[libc][startup] create header for ElfW and use in startup (#96510)
This is necessary for 32b platforms such as ARM and i386.

Link: #94128
2024-06-24 14:37:50 -07:00
Nick Desaulniers (paternity leave)
781d5cf32a
[libc][startup] check that we're cross compiling and using LLD (#96357)
We only need to set `--target=` for LLD when cross compiling. This should fix
the host build using BFD or targeting the host.

Fixes: #96342
2024-06-21 14:33:27 -07:00
Nick Desaulniers (paternity leave)
32d05db6e9
[libc][startup] set --target= for linker when cross compiling (#96342)
Otherwise the startup objects will fail to link since they were cross compiled,
but the linker is not informed of the intent to cross compile, which results in
linker errors when the host architecture does not match the target
architecture.
2024-06-21 13:30:19 -07:00
Petr Hosek
b3e71ec9f7
[libc] Provide __libc_{init,fini}_array for baremetal (#90828)
These are provided by newlib and many baremetal projects assume they're
available rather than providing their own implementation.
2024-05-21 15:46:04 -07:00
Joseph Huber
69c0b2febe
[libc][NFC] Remove all trailing spaces from libc (#82831)
Summary:
There are a lot of random training spaces on various lines. This patch
just got rid of all of them with `sed 's/\ \+$//g'.
2024-02-23 16:34:00 -06:00
Joseph Huber
0352d5eee0
[libc][NFC] Remove redundant external clock symbol for AMDGPU (#82794)
Summary:
The AMDGPU target needs an external clock symbol so the driver can set
the frequency with the correct value. This was left over from the
previous implementation and I forgot to remove it when actually
implementing the timing utilities.
2024-02-23 11:59:46 -06:00
Joseph Huber
47b7c91abe
[libc] Rework the GPU build to be a regular target (#81921)
Summary:
This is a massive patch because it reworks the entire build and
everything that depends on it. This is not split up because various bots
would fail otherwise. I will attempt to describe the necessary changes
here.

This patch completely reworks how the GPU build is built and targeted.
Previously, we used a standard runtimes build and handled both NVPTX and
AMDGPU in a single build via multi-targeting. This added a lot of
divergence in the build system and prevented us from doing various
things like building for the CPU / GPU at the same time, or exporting
the startup libraries or running tests without a full rebuild.

The new appraoch is to handle the GPU builds as strict cross-compiling
runtimes. The first step required
https://github.com/llvm/llvm-project/pull/81557 to allow the `LIBC`
target to build for the GPU without touching the other targets. This
means that the GPU uses all the same handling as the other builds in
`libc`.

The new expected way to build the GPU libc is with
`LLVM_LIBC_RUNTIME_TARGETS=amdgcn-amd-amdhsa;nvptx64-nvidia-cuda`.

The second step was reworking how we generated the embedded GPU library
by moving it into the library install step. Where we previously had one
`libcgpu.a` we now have `libcgpu-amdgpu.a` and `libcgpu-nvptx.a`. This
patch includes the necessary clang / OpenMP changes to make that not
break the bots when this lands.

We unfortunately still require that the NVPTX target has an `internal`
target for tests. This is because the NVPTX target needs to do LTO for
the provided version (The offloading toolchain can handle it) but cannot
use it for the native toolchain which is used for making tests.

This approach is vastly superior in every way, allowing us to treat the
GPU as a standard cross-compiling target. We can now install the GPU
utilities to do things like use the offload tests and other fun things.

Some certain utilities need to be built with 
`--target=${LLVM_HOST_TRIPLE}` as well. I think this is a fine
workaround as we
will always assume that the GPU `libc` is a cross-build with a
functioning host.

Depends on https://github.com/llvm/llvm-project/pull/81557
2024-02-22 15:29:29 -06:00
Joseph Huber
049e142bad
[libc] Fix startup utilities failing to install in full build mode (#82522)
Summary:
Currently, doing `ninja install` will fail in fullbuild mode due to the
startup utilities not being built by default. This was hidden previously
by the fact that if tests were run, it would build the startup utilities
and thus they would be present.

This patch solves this issue by making the `libc-startup` target a
dependncy on the final library. Furthermore we simply factor out the
library install directory into the base CMake directory next to the
include directory handling. This change makes the `crt` files get
installed in `lib/x86_64-unknown-linu-gnu` instead of just `lib`.

This fixes an error I had where doing a runtimes failed to install its
libraries because the install step always errored.
2024-02-21 17:04:31 -06:00
lntue
0881d0f009
[libc] Refactor _build_gpu_objects cmake function. (#80631) 2024-02-05 10:44:19 -05:00
Schrodinger ZHU Yifan
4a9b5aa116
[libc][NFC] remove TODO about AppProperties (#79356)
```
AppProperties app;
```
is marked as a weak symbol in header now. One can just use `&app !=
nullptr` to check if `app` is defined. There is no need to define it for
overlay mode.
2024-01-24 15:18:11 -05:00
Schrodinger ZHU Yifan
0fe20aa45e
[libc] support PIE relocations (#78993)
For some reasons, we are using `-fpie`
(libc/cmake/modules/LLVMLibCObjectRules.cmake:31) without supporting it.
According to @lntue, some of the hermetic tests are broken without
proper PIE support. This patch implements basic relocations support for
PIE.
2024-01-22 12:45:35 -05:00
Schrodinger ZHU Yifan
2bc994456c
[libc] major refactor of startup library (#76092)
* separate initialization routines into _start and do_start for all
architectures.
* lift do_start as a separate object library to avoid code duplication.
* (addtionally) address the problem of building hermetic libc with
-fstack-pointer-*

The `crt1.o` is now a merged result of three components:

```
___
  |___ x86_64
  |      |_______ start.cpp.o    <- _start (loads process initial stack and aligns stack pointer)
  |      |_______ tls.cpp.o      <- init_tls, cleanup_tls, set_thread_pointer (TLS related routines) 
  |___ do_start.cpp.o            <- do_start (sets up global variables and invokes the main function) 
```
2024-01-04 12:51:14 -08:00
Schrodinger ZHU Yifan
7a87ff64e1
[libc] suppress stdlib explicitly for crt1.a (#76079)
[nd: updated oneline]
2023-12-20 09:42:35 -08:00
Schrodinger ZHU Yifan
8bbeed05c4
[libc] [startup] add cmake function to merge separated crt1 objects (#75413)
As part of startup refactoring, this patch adds a function to merge
multiple objects into a single relocatable object:
                     cc -r obj1.o obj2.o -o obj.o

A relocatable object is an object file that is not fully linked into an
executable or a shared library. It is an intermediate file format that
can be passed into the linker.

A crt object can have arch-specific code and arch-agnostic code. To
reduce code cohesion, the implementation is splitted into multiple
units. As a result, we need to merge them into a single relocatable
object.
2023-12-20 08:18:51 -08:00
Nick Desaulniers
315a5cce89
[libc] move __stack_chk_fail to src/ from startup/ (#75863)
__stack_chk_fail should be provided by libc.a, not startup files.

Add __stack_chk_fail to existing linux and arm entrypoints. On Windows
(when
not targeting MinGW), it seems that the corresponding function
identifier is
__security_check_cookie, so no entrypoint is added for Windows.
Baremetal
targets also ought to be compileable with `-fstack-protector*`

There is no common header for this prototype, since calls to
__stack_chk_fail
are meant to be inserted by the compiler upon function return when
compiled
`-fstack-protector*`.
2023-12-19 11:05:12 -08:00
Schrodinger ZHU Yifan
6c1f56fdb5
[libc] expose aux vector (#75806)
This patch lifts aux vector related definitions to app.h. Because
startup's refactoring is in progress, this patch still contains
duplicated changes. This problem will be addressed very soon in an
incoming patch.
2023-12-18 12:27:30 -08:00
Schrodinger ZHU Yifan
3568521e3d
[libc] fix issues around stack protector (#74567)
If a function is declared with stack-protector, the compiler may try to
load the TLS. However, inside certain runtime functions, TLS may not be
available. This patch disables stack protectors for such routines to fix
the problem.

Closes #74487.
2023-12-12 12:31:51 -08:00
Schrodinger ZHU Yifan
a0eda10947
[libc][NFC] unify startup library's code style with the rest (#74041)
This PR unifies the startup library's code style with the rest of libc.
2023-12-04 10:31:18 -08:00
Joseph Huber
abd85cd473
[libc] Remove the optional arguments for NVPTX constructors (#69536)
Summary:
We call the global constructors by function pointer. For whatever reason
the NVPTX architecture relies very specifically on the arguments to the
function pointer invocation matching what the function is implemented
as. This is problematic as most of these constructors are generated
with no arguments. This patch removes the extended arguments that GNU
and LLVM use for the constructors optionally so that it can support the
common case.
2023-11-20 17:10:15 -06:00
Joseph Huber
dc30fa6aca [libc][fix] Call GPU destructors in the correct order
Summary:
I was mistakenly iterating the list backwards. Regular semantics puts
both arrays in priority order but the destructors are called backwards.
2023-11-09 09:22:41 -06:00