[libc-commits] [libc] [libc] lift app's definition out of startup (PR #75717)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Dec 16 13:49:04 PST 2023


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/75717

>From 0b0ec99e56ad396a44a112a5c049550c1f092d30 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Sat, 16 Dec 2023 16:39:24 -0500
Subject: [PATCH] [libc] lift app's definition out of startup

AppProperties are not target specific hence this patch lifts app from a header library to an object library.
In overlay mode, this also provides a way to check if libc is loaded by its own CRT.
---
 libc/config/linux/CMakeLists.txt              |  6 ++++--
 libc/config/linux/app.cpp                     | 13 ++++++++++++
 libc/config/linux/app.h                       | 20 +++++++++++--------
 .../__support/threads/linux/CMakeLists.txt    |  2 +-
 libc/src/stdlib/CMakeLists.txt                |  2 +-
 libc/startup/linux/aarch64/CMakeLists.txt     |  2 +-
 libc/startup/linux/aarch64/start.cpp          |  2 --
 libc/startup/linux/riscv/CMakeLists.txt       |  2 +-
 libc/startup/linux/riscv/start.cpp            |  2 --
 libc/startup/linux/x86_64/CMakeLists.txt      |  2 +-
 libc/startup/linux/x86_64/start.cpp           |  2 --
 11 files changed, 34 insertions(+), 21 deletions(-)
 create mode 100644 libc/config/linux/app.cpp

diff --git a/libc/config/linux/CMakeLists.txt b/libc/config/linux/CMakeLists.txt
index cf38ae3eed7267..ad2445697d3dbb 100644
--- a/libc/config/linux/CMakeLists.txt
+++ b/libc/config/linux/CMakeLists.txt
@@ -1,5 +1,7 @@
-add_header_library(
-  app_h
+add_object_library(
+  app
+  SRCS
+    app.cpp
   HDRS
     app.h
   DEPENDS
diff --git a/libc/config/linux/app.cpp b/libc/config/linux/app.cpp
new file mode 100644
index 00000000000000..b4ce253bc6361b
--- /dev/null
+++ b/libc/config/linux/app.cpp
@@ -0,0 +1,13 @@
+//===-- Implementation file for linux applications classes  -----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "config/linux/app.h"
+
+namespace LIBC_NAMESPACE {
+AppProperties app;
+}
diff --git a/libc/config/linux/app.h b/libc/config/linux/app.h
index 0d2f9475c10db6..f8e091eb5266d7 100644
--- a/libc/config/linux/app.h
+++ b/libc/config/linux/app.h
@@ -18,21 +18,23 @@ namespace LIBC_NAMESPACE {
 // Data structure to capture properties of the linux/ELF TLS image.
 struct TLSImage {
   // The load address of the TLS.
-  uintptr_t address;
+  uintptr_t address = 0;
 
   // The byte size of the TLS image consisting of both initialized and
   // uninitialized memory. In ELF executables, it is size of .tdata + size of
   // .tbss. Put in another way, it is the memsz field of the PT_TLS header.
-  uintptr_t size;
+  uintptr_t size = 0;
 
   // The byte size of initialized memory in the TLS image. In ELF exectubles,
   // this is the size of .tdata. Put in another way, it is the filesz of the
   // PT_TLS header.
-  uintptr_t init_size;
+  uintptr_t init_size = 0;
 
   // The alignment of the TLS layout. It assumed that the alignment
   // value is a power of 2.
-  uintptr_t align;
+  uintptr_t align = 0;
+
+  constexpr TLSImage() = default;
 };
 
 #if defined(LIBC_TARGET_ARCH_IS_X86_64) ||                                     \
@@ -69,15 +71,17 @@ struct Args {
 // Data structure which captures properties of a linux application.
 struct AppProperties {
   // Page size used for the application.
-  uintptr_t page_size;
+  uintptr_t page_size = 0;
 
-  Args *args;
+  Args *args = nullptr;
 
   // The properties of an application's TLS image.
-  TLSImage tls;
+  TLSImage tls{};
 
   // Environment data.
-  EnvironType *env_ptr;
+  EnvironType *env_ptr = nullptr;
+
+  constexpr AppProperties() = default;
 };
 
 extern AppProperties app;
diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt
index 642eead7277262..d62d2c26c53d70 100644
--- a/libc/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/src/__support/threads/linux/CMakeLists.txt
@@ -26,7 +26,7 @@ add_object_library(
     thread.cpp
   DEPENDS
     .futex_word_type
-    libc.config.linux.app_h
+    libc.config.linux.app
     libc.include.sys_syscall
     libc.src.errno.errno
     libc.src.__support.CPP.atomic
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index a4d51fb9a11eef..3c8b63af85b7ac 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -49,7 +49,7 @@ add_entrypoint_object(
   HDRS
     getenv.h
   DEPENDS
-    libc.config.linux.app_h
+    libc.config.linux.app
 )
 
 add_entrypoint_object(
diff --git a/libc/startup/linux/aarch64/CMakeLists.txt b/libc/startup/linux/aarch64/CMakeLists.txt
index b47db8eb5d23f3..b39698f622566b 100644
--- a/libc/startup/linux/aarch64/CMakeLists.txt
+++ b/libc/startup/linux/aarch64/CMakeLists.txt
@@ -3,7 +3,7 @@ add_startup_object(
   SRC
     start.cpp
   DEPENDS
-    libc.config.linux.app_h
+    libc.config.linux.app
     libc.include.sys_mman
     libc.include.sys_syscall
     libc.src.__support.threads.thread
diff --git a/libc/startup/linux/aarch64/start.cpp b/libc/startup/linux/aarch64/start.cpp
index c3e20eb09e4b42..552916e06c1bbc 100644
--- a/libc/startup/linux/aarch64/start.cpp
+++ b/libc/startup/linux/aarch64/start.cpp
@@ -37,8 +37,6 @@ static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
 #error "mmap and mmap2 syscalls not available."
 #endif
 
-AppProperties app;
-
 static ThreadAttributes main_thread_attrib;
 
 void init_tls(TLSDescriptor &tls_descriptor) {
diff --git a/libc/startup/linux/riscv/CMakeLists.txt b/libc/startup/linux/riscv/CMakeLists.txt
index b47db8eb5d23f3..b39698f622566b 100644
--- a/libc/startup/linux/riscv/CMakeLists.txt
+++ b/libc/startup/linux/riscv/CMakeLists.txt
@@ -3,7 +3,7 @@ add_startup_object(
   SRC
     start.cpp
   DEPENDS
-    libc.config.linux.app_h
+    libc.config.linux.app
     libc.include.sys_mman
     libc.include.sys_syscall
     libc.src.__support.threads.thread
diff --git a/libc/startup/linux/riscv/start.cpp b/libc/startup/linux/riscv/start.cpp
index 4d37662ccea13c..b3a5a90d138d23 100644
--- a/libc/startup/linux/riscv/start.cpp
+++ b/libc/startup/linux/riscv/start.cpp
@@ -32,8 +32,6 @@ static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
 #error "mmap and mmap2 syscalls not available."
 #endif
 
-AppProperties app;
-
 static ThreadAttributes main_thread_attrib;
 
 void init_tls(TLSDescriptor &tls_descriptor) {
diff --git a/libc/startup/linux/x86_64/CMakeLists.txt b/libc/startup/linux/x86_64/CMakeLists.txt
index aac5a0626a176a..59932b72700cb0 100644
--- a/libc/startup/linux/x86_64/CMakeLists.txt
+++ b/libc/startup/linux/x86_64/CMakeLists.txt
@@ -3,7 +3,7 @@ add_startup_object(
   SRC
     start.cpp
   DEPENDS
-    libc.config.linux.app_h
+    libc.config.linux.app
     libc.include.sys_mman
     libc.include.sys_syscall
     libc.include.unistd
diff --git a/libc/startup/linux/x86_64/start.cpp b/libc/startup/linux/x86_64/start.cpp
index 496105dfd0b43a..d30fd4f02d9e7c 100644
--- a/libc/startup/linux/x86_64/start.cpp
+++ b/libc/startup/linux/x86_64/start.cpp
@@ -40,8 +40,6 @@ static constexpr long MMAP_SYSCALL_NUMBER = SYS_mmap;
 #error "mmap and mmap2 syscalls not available."
 #endif
 
-AppProperties app;
-
 static ThreadAttributes main_thread_attrib;
 
 // TODO: The function is x86_64 specific. Move it to config/linux/app.h



More information about the libc-commits mailing list