Path: blob/main/system/lib/llvm-libc/config/linux/app.h
6170 views
//===-- Classes to capture properites of linux applications -----*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_CONFIG_LINUX_APP_H9#define LLVM_LIBC_CONFIG_LINUX_APP_H1011#include "src/__support/macros/config.h"12#include "src/__support/macros/properties/architectures.h"1314#include <stdint.h>1516namespace LIBC_NAMESPACE_DECL {1718// Data structure to capture properties of the linux/ELF TLS image.19struct TLSImage {20// The load address of the TLS.21uintptr_t address;2223// The byte size of the TLS image consisting of both initialized and24// uninitialized memory. In ELF executables, it is size of .tdata + size of25// .tbss. Put in another way, it is the memsz field of the PT_TLS header.26uintptr_t size;2728// The byte size of initialized memory in the TLS image. In ELF exectubles,29// this is the size of .tdata. Put in another way, it is the filesz of the30// PT_TLS header.31uintptr_t init_size;3233// The alignment of the TLS layout. It assumed that the alignment34// value is a power of 2.35uintptr_t align;36};3738// Linux manpage on `proc(5)` says that the aux vector is an array of39// unsigned long pairs.40// (see: https://man7.org/linux/man-pages/man5/proc.5.html)41using AuxEntryType = unsigned long;42// Using the naming convention from `proc(5)`.43// TODO: Would be nice to use the aux entry structure from elf.h when available.44struct AuxEntry {45AuxEntryType id;46AuxEntryType value;47};4849struct Args {50uintptr_t argc;5152// A flexible length array would be more suitable here, but C++ doesn't have53// flexible arrays: P1039 proposes to fix this. So, for now we just fake it.54// Even if argc is zero, "argv[argc] shall be a null pointer"55// (ISO C 5.1.2.2.1) so one is fine. Also, length of 1 is not really wrong as56// |argc| is guaranteed to be atleast 1, and there is an 8-byte null entry at57// the end of the argv array.58uintptr_t argv[1];59};6061// Data structure which captures properties of a linux application.62struct AppProperties {63// Page size used for the application.64uintptr_t page_size;6566Args *args;6768// The properties of an application's TLS image.69TLSImage tls;7071// Environment data.72uintptr_t *env_ptr;7374// Auxiliary vector data.75AuxEntry *auxv_ptr;76};7778[[gnu::weak]] extern AppProperties app;7980// The descriptor of a thread's TLS area.81struct TLSDescriptor {82// The size of the TLS area.83uintptr_t size = 0;8485// The address of the TLS area. This address can be passed to cleanup86// functions like munmap.87uintptr_t addr = 0;8889// The value the thread pointer register should be initialized to.90// Note that, dependending the target architecture ABI, it can be the91// same as |addr| or something else.92uintptr_t tp = 0;9394constexpr TLSDescriptor() = default;95};9697// Create and initialize the TLS area for the current thread. Should not98// be called before app.tls has been initialized.99void init_tls(TLSDescriptor &tls);100101// Cleanup the TLS area as described in |tls_descriptor|.102void cleanup_tls(uintptr_t tls_addr, uintptr_t tls_size);103104// Set the thread pointer for the current thread.105bool set_thread_ptr(uintptr_t val);106107} // namespace LIBC_NAMESPACE_DECL108109#endif // LLVM_LIBC_CONFIG_LINUX_APP_H110111112