aboutsummaryrefslogtreecommitdiff
path: root/libc/startup/linux
AgeCommit message (Collapse)AuthorFilesLines
2026-01-12[libc] Provide a minimal implementation of elf.h (#172766)Petr Hosek2-7/+7
Some of the baremetal users of libc use elf.h to generate coredumps and we would like to support this use case without needing Linux elf.h.
2026-01-05[libc] Delete teardown_main_tls and all its uses (#174374)Petr Hosek1-3/+0
teardown_main_tls is called right before the main process exit to unmap the TLS but that's unnecesary since the process is terminating anyway, this just introduces extra system call. This can be potentially harmful in case one of the other threads tries the access the memory that has been unmapped. Other C library implementations don't unmap TLS either so this makes LLVM libc's behavior more consistent. Fixes #173409
2025-10-09[libc] fix sysconf test for rv32 (#162685)Schrodinger ZHU Yifan3-3/+3
2025-10-08[libc] Refactor AUXV handling with new auxv.h header library (#162326)Schrodinger ZHU Yifan2-6/+10
Closes https://github.com/llvm/llvm-project/issues/153666 This patch introduces a new centralized AUXV (auxiliary vector) handling mechanism for LLVM libc on Linux, replacing the previous scattered implementation across multiple files. ## Key Changes: ### New Files: - **libc/src/__support/OSUtil/linux/auxv.h**: New header library providing a clean interface for AUXV access with: - `auxv::Entry` struct for AUXV entries (type and value) - `auxv::Vector` class with iterator support for traversing AUXV - `auxv::get()` function for retrieving specific AUXV values - Thread-safe initialization with fallback mechanisms (prctl and /proc/self/auxv) ### Modified Files: 1. **libc/src/__support/OSUtil/linux/CMakeLists.txt**: - Added `auxv` header library declaration with proper dependencies: - libc.hdr.fcntl_macros - libc.src.__support.OSUtil.osutil - libc.src.__support.common - libc.src.__support.CPP.optional - libc.src.__support.threads.callonce 2. **libc/config/linux/app.h**: - Removed `AuxEntry` struct (moved to auxv.h as `auxv::Entry`) - Removed `auxv_ptr` from `AppProperties` struct - Simplified application properties structure 3. **libc/src/sys/auxv/linux/getauxval.cpp**: - Completely refactored to use new auxv.h interface - Removed ~200 lines of complex initialization code - Simplified to just call `auxv::get()` function - Removed dependencies to external symbols (mman, prctl, fcntl, read, close, open) 4. **libc/src/sys/auxv/linux/CMakeLists.txt**: - Updated dependencies to use new auxv header library - Removed dependencies to external symbols (prctl, mman, fcntl, unistd, etc.) 5. **libc/startup/linux/do_start.cpp**: - Updated to use new `auxv::Vector` interface - Changed from pointer-based to iterator-based AUXV traversal - Updated field names (`aux_entry->id` → `aux_entry.type`, `aux_entry->value` → `aux_entry.val`) - Added call to `auxv::Vector::initialize_unsafe()` for early AUXV setup 6. **libc/startup/linux/CMakeLists.txt**: - Added dependency on `libc.src.__support.OSUtil.linux.auxv`
2025-07-23[libc][NFC] Add stdint.h proxy header to fix dependency issue with ↵lntue2-1/+2
<stdint.h> includes. (#150303) https://github.com/llvm/llvm-project/issues/149993
2024-09-11[libc] fix tls teardown while being used (#108229)Schrodinger ZHU Yifan1-5/+6
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-08-20[libc] Include startup code when installing all (#105203)Michael Jones1-1/+0
Previously the libc startup code was marked `EXCLUDE_FROM_ALL` due to build issues. This patch removes that as no longer necessary.
2024-08-11Revert "libc: Remove `extern "C"` from main declarations" (#102827)Schrodinger ZHU Yifan1-1/+1
Reverts llvm/llvm-project#102825
2024-08-11libc: Remove `extern "C"` from main declarations (#102825)David Blaikie1-1/+1
This is invalid in C++, and clang recently started warning on it as of #101853
2024-08-08[libc] Implement 'getenv' on the GPU target (#102376)Joseph Huber5-8/+8
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-05[libc][startup] fix main thread TLS unmapped with wrong value (#102009)Schrodinger ZHU Yifan1-1/+1
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-07-27revert all tid changes (#100915)Schrodinger ZHU Yifan2-6/+0
2024-07-20reland "[libc] implement cached process/thread identity (#98989)" (#99765)Schrodinger ZHU Yifan2-0/+6
2024-07-18Revert "[libc] implement cached process/thread identity" (#99559)Schrodinger ZHU Yifan2-6/+0
Reverts llvm/llvm-project#98989
2024-07-18[libc] implement cached process/thread identity (#98989)Schrodinger ZHU Yifan2-0/+6
migrated from https://github.com/llvm/llvm-project/pull/95965 due to corrupted git history
2024-07-12[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98597)Petr Hosek5-10/+15
This is a part of #97655.
2024-07-12Revert "[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace ↵Mehdi Amini5-15/+10
declaration" (#98593) Reverts llvm/llvm-project#98075 bots are broken
2024-07-11[libc] Migrate to using LIBC_NAMESPACE_DECL for namespace declaration (#98075)Petr Hosek5-10/+15
This is a part of #97655.
2024-07-07[libc] fix aarch64 GCC build (#97932)Schrodinger ZHU Yifan1-1/+12
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-06-25[libc][arm32] define argc type and stack alignment (#96367)Nick Desaulniers (paternity leave)1-2/+2
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-24[libc][startup] create header for ElfW and use in startup (#96510)Nick Desaulniers (paternity leave)2-4/+6
This is necessary for 32b platforms such as ARM and i386. Link: #94128
2024-06-21[libc][startup] check that we're cross compiling and using LLD (#96357)Nick Desaulniers (paternity leave)1-1/+5
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[libc][startup] set --target= for linker when cross compiling (#96342)Nick Desaulniers (paternity leave)1-1/+1
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-02-23[libc][NFC] Remove all trailing spaces from libc (#82831)Joseph Huber1-3/+3
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-21[libc] Fix startup utilities failing to install in full build mode (#82522)Joseph Huber1-1/+2
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-01-24[libc][NFC] remove TODO about AppProperties (#79356)Schrodinger ZHU Yifan1-1/+0
``` 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-22[libc] support PIE relocations (#78993)Schrodinger ZHU Yifan1-8/+21
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-04[libc] major refactor of startup library (#76092)Schrodinger ZHU Yifan12-671/+503
* 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) ```
2023-12-20[libc] suppress stdlib explicitly for crt1.a (#76079)Schrodinger ZHU Yifan1-1/+1
[nd: updated oneline]
2023-12-20[libc] [startup] add cmake function to merge separated crt1 objects (#75413)Schrodinger ZHU Yifan1-11/+60
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-19[libc] move __stack_chk_fail to src/ from startup/ (#75863)Nick Desaulniers1-5/+0
__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-18[libc] expose aux vector (#75806)Schrodinger ZHU Yifan3-27/+12
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-12[libc] fix issues around stack protector (#74567)Schrodinger ZHU Yifan4-18/+25
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-04[libc][NFC] unify startup library's code style with the rest (#74041)Schrodinger ZHU Yifan3-44/+45
This PR unifies the startup library's code style with the rest of libc.
2023-10-04[libc] Add x86-64 stack protector support.tnv012-1/+21
2023-09-26[libc] Start to refactor riscv platform abstraction to support both 32 and ↵Mikhail R. Gadelha2-0/+0
64 bits versions This patch enables the compilation of libc for rv32 by unifying the current rv64 and rv32 implementation into a single rv implementation. We updated the cmake file to match the new riscv32 arch and force LIBC_TARGET_ARCHITECTURE to be "riscv" whenever we find "riscv32" or "riscv64". This is required as LIBC_TARGET_ARCHITECTURE is used in the path for several platform specific implementations. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D148797
2023-09-26[libc] Mass replace enclosing namespace (#67032)Guillaume Chatelet3-75/+75
This is step 4 of https://discourse.llvm.org/t/rfc-customizable-namespace-to-allow-testing-the-libc-when-the-system-libc-is-also-llvms-libc/73079
2023-09-14[libc] Fix start up crash on 32 bit systems (#66210)Mikhail R. Gadelha1-7/+17
This patch changes the default types of argc/argv so it's no longer a uint64_t in all systems, instead, it's now a uintptr_t, which fixes crashes in 32-bit systems that expect 32-bit types. This patch also adds two uintptr_t types (EnvironType and AuxEntryType) for the same reason. The patch also adds a PgrHdrTableType type behind an ifdef that's Elf64_Phdr in 64-bit systems and Elf32_Phdr in 32-bit systems.
2023-08-30[libc] Fix set_thread_ptr call in rv32 start up codeMikhail R. Gadelha1-1/+1
This patch changes the instruction in set_thread_ptr from ld to mv, as rv32 doesn't have the ld instruction, and mv is supported by both rv32 and rv64. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D159110
2023-08-07[libc][cleanup] Fix most conversion warningsMichael Jones3-25/+31
This patch is large, but is almost entirely just adding casts to calls to syscall_impl. Much of the work was done programatically, with human checking when the syntax or types got confusing. Reviewed By: mcgrathr Differential Revision: https://reviews.llvm.org/D156950
2023-07-19[libc][NFC] Rename filesGuillaume Chatelet6-6/+6
This patch mostly renames files so it better reflects the function they declare. Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D155607
2023-07-05[libc] Initiliaze the global pointer in riscv startup code.Siva Chandra1-0/+4
Reviewed By: mikhail.ramalho Differential Revision: https://reviews.llvm.org/D151539
2023-06-23[libc][NFC] Simplify return value logic in set_thread_ptr()Jun Zhang1-3/+1
Signed-off-by: Jun Zhang <jun@junz.org> Differential Revision: https://reviews.llvm.org/D153572
2023-04-17[libc][NFC] Standardize missing syscalls error messages.Mikhail R. Gadelha3-3/+3
This patch standardizes the error messages when a syscall is not available to be in the format: "ABC and DEF syscalls are not available." Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D148373
2023-03-16[libc] Add missing dependencies to RISC-V startup implementationJoseph Huber1-0/+2
Summary: Just like the last patch, the threads and envrion dependencies were missing. This lead to linker failures when building the tests.
2023-03-16[libc] Add missing environ dependency to AArch64 startup implementationJoseph Huber1-0/+1
Summary: Just like the last patch, this dependency was missing. This lead to linker failures when building the tests.
2023-03-16[libc] Add missing threads dependency to AArch64 startup implementationJoseph Huber1-0/+1
Summary: The changes in D146184 made the integration tests use the inhereted dependencies from the startup code like a normal target. For the AArch64 target this resulted in the threads depenency not being pulled in because it was not present in the original code.
2023-03-13[libc] Declare __dso_handle in the integration test instead of startup.Siva Chandra Reddy3-9/+0
Fixes #61355. The __dso_handle decl was introduced incorrectly into the startup objects during the integration test cleanup which moved the integration tests away from using an artificial sysroot to using -nostdlib. Having it in the startup creates the duplicate symbol error when one does not use -nostdlib. Since this is an integration test only problem, it is meaningful to keep it in the integration test anyway. Differential Revision: https://reviews.llvm.org/D145898
2023-03-09[libc] Extend the linux thread implementation for riscv64.Siva Chandra2-0/+229
Also, added riscv64 startup code for static linking which is used by the integration tests. Functions from the C standard threads library have been enabled. Reviewed By: mikhail.ramalho Differential Revision: https://reviews.llvm.org/D145670
2023-03-06[libc] Simplify integration tests by eliminating the artificial sysroot.Siva Chandra Reddy2-0/+6
The test binaries are built like any other executable but with two additional linker options -static and -nostdlib. Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D145298