[libc-commits] [libc] [libc] Add unistd overlay (PR #118882)

Tristan Ross via libc-commits libc-commits at lists.llvm.org
Thu Dec 5 14:02:43 PST 2024


https://github.com/RossComputerGuy updated https://github.com/llvm/llvm-project/pull/118882

>From 50ef7fbcdcd7d595120c232f31dea00148685827 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 5 Dec 2024 13:52:05 -0800
Subject: [PATCH] [libc] Add unistd overlay

---
 libc/hdr/CMakeLists.txt      |  3 ++
 libc/hdr/unistd_macros.h     |  2 +-
 libc/hdr/unistd_overlay.h    | 70 ++++++++++++++++++++++++++++++++++++
 libc/src/unistd/getcwd.h     |  2 +-
 libc/src/unistd/pread.h      |  2 +-
 libc/src/unistd/read.h       |  2 +-
 libc/src/unistd/readlink.h   |  2 +-
 libc/src/unistd/readlinkat.h |  2 +-
 8 files changed, 79 insertions(+), 6 deletions(-)
 create mode 100644 libc/hdr/unistd_overlay.h

diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 5eb311f4bb2298..7f523c50e86943 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -126,10 +126,13 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.sys_stat_macros
 )
 
+add_header_library(unistd_overlay HDRS unistd_overlay.h)
 add_proxy_header_library(
   unistd_macros
   HDRS
     unistd_macros.h
+  DEPENDS
+    .unistd_overlay
   FULL_BUILD_DEPENDS
     libc.include.unistd
     libc.include.llvm-libc-macros.unistd_macros
diff --git a/libc/hdr/unistd_macros.h b/libc/hdr/unistd_macros.h
index 132e123280139f..5c2b24354dd3ee 100644
--- a/libc/hdr/unistd_macros.h
+++ b/libc/hdr/unistd_macros.h
@@ -15,7 +15,7 @@
 
 #else // Overlay mode
 
-#include <unistd.h>
+#include "unistd_overlay.h"
 
 #endif // LLVM_LIBC_FULL_BUILD
 
diff --git a/libc/hdr/unistd_overlay.h b/libc/hdr/unistd_overlay.h
new file mode 100644
index 00000000000000..1b837ca531417a
--- /dev/null
+++ b/libc/hdr/unistd_overlay.h
@@ -0,0 +1,70 @@
+//===-- Including unistd.h in overlay mode
+//---------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_UNISTD_OVERLAY_H
+#define LLVM_LIBC_HDR_UNISTD_OVERLAY_H
+
+#ifdef LIBC_FULL_BUILD
+#error "This header should only be included in overlay mode"
+#endif
+
+// Overlay mode
+
+// glibc <unistd.h> header might provide extern inline definitions for few
+// functions, causing external alias errors.  They are guarded by
+// `__USE_EXTERN_INLINES` macro.  We temporarily disable `__USE_EXTERN_INLINES`
+// macro by defining `__NO_INLINE__` before including <stdio.h>.
+// And the same with `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef _FORTIFY_SOURCE
+#define LIBC_OLD_FORTIFY_SOURCE _FORTIFY_SOURCE
+#undef _FORTIFY_SOURCE
+#endif
+
+#ifdef __USE_EXTERN_INLINES
+#define LIBC_OLD_USE_EXTERN_INLINES
+#undef __USE_EXTERN_INLINES
+#endif
+
+#ifdef __USE_FORTIFY_LEVEL
+#define LIBC_OLD_USE_FORTIFY_LEVEL __USE_FORTIFY_LEVEL
+#undef __USE_FORTIFY_LEVEL
+#define __USE_FORTIFY_LEVEL 0
+#endif
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#endif
+
+#include <unistd.h>
+
+#ifdef LIBC_OLD_FORTIFY_SOURCE
+#define _FORTIFY_SOURCE LIBC_OLD_FORTIFY_SOURCE
+#undef LIBC_OLD_FORTIFY_SOURCE
+#endif
+
+#ifdef LIBC_SET_NO_INLINE
+#undef __NO_INLINE__
+#undef LIBC_SET_NO_INLINE
+#endif
+
+#ifdef LIBC_OLD_USE_FORTIFY_LEVEL
+#undef __USE_FORTIFY_LEVEL
+#define __USE_FORTIFY_LEVEL LIBC_OLD_USE_FORTIFY_LEVEL
+#undef LIBC_OLD_USE_FORTIFY_LEVEL
+#endif
+
+#ifdef LIBC_OLD_USE_EXTERN_INLINES
+#define __USE_EXTERN_INLINES
+#undef LIBC_OLD_USE_EXTERN_INLINES
+#endif
+
+#endif // LLVM_LIBC_HDR_UNISTD_OVERLAY_H
diff --git a/libc/src/unistd/getcwd.h b/libc/src/unistd/getcwd.h
index 8b63a91c26b5c7..36ca48141b93fb 100644
--- a/libc/src/unistd/getcwd.h
+++ b/libc/src/unistd/getcwd.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_UNISTD_GETCWD_H
 #define LLVM_LIBC_SRC_UNISTD_GETCWD_H
 
+#include "hdr/unistd_macros.h"
 #include "src/__support/macros/config.h"
-#include <unistd.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/unistd/pread.h b/libc/src/unistd/pread.h
index 4723675e82a20a..e2cfce6b53ed5f 100644
--- a/libc/src/unistd/pread.h
+++ b/libc/src/unistd/pread.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_UNISTD_PREAD_H
 #define LLVM_LIBC_SRC_UNISTD_PREAD_H
 
+#include "hdr/unistd_macros.h"
 #include "src/__support/macros/config.h"
-#include <unistd.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/unistd/read.h b/libc/src/unistd/read.h
index 01231cb82e35e5..a12c49e4254eca 100644
--- a/libc/src/unistd/read.h
+++ b/libc/src/unistd/read.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_UNISTD_READ_H
 #define LLVM_LIBC_SRC_UNISTD_READ_H
 
+#include "hdr/unistd_macros.h"
 #include "src/__support/macros/config.h"
-#include <unistd.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/unistd/readlink.h b/libc/src/unistd/readlink.h
index a73e9740c74637..9047097f46b24a 100644
--- a/libc/src/unistd/readlink.h
+++ b/libc/src/unistd/readlink.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_UNISTD_READLINK_H
 #define LLVM_LIBC_SRC_UNISTD_READLINK_H
 
+#include "hdr/unistd_macros.h"
 #include "src/__support/macros/config.h"
-#include <unistd.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/libc/src/unistd/readlinkat.h b/libc/src/unistd/readlinkat.h
index 6bdd48b537fc8c..10fd8bb6a14b8d 100644
--- a/libc/src/unistd/readlinkat.h
+++ b/libc/src/unistd/readlinkat.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_UNISTD_READLINKAT_H
 #define LLVM_LIBC_SRC_UNISTD_READLINKAT_H
 
+#include "hdr/unistd_macros.h"
 #include "src/__support/macros/config.h"
-#include <unistd.h>
 
 namespace LIBC_NAMESPACE_DECL {
 



More information about the libc-commits mailing list