[libc] remove #include <fcntl.h> and add proxy or type (#113836)

GitOrigin-RevId: abc49cc19463970d5523d7d3332e4c1f83bc2ef7
diff --git a/hdr/CMakeLists.txt b/hdr/CMakeLists.txt
index 13dc892..80545ee 100644
--- a/hdr/CMakeLists.txt
+++ b/hdr/CMakeLists.txt
@@ -51,10 +51,13 @@
     libc.include.llvm-libc-macros.generic_error_number_macros
 )
 
+add_header_library(fcntl_overlay HDRS fcntl_overlay.h)
 add_proxy_header_library(
   fcntl_macros
   HDRS
     fcntl_macros.h
+  DEPENDS
+    .fcntl_overlay
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-macros.fcntl_macros
     libc.include.fcntl
diff --git a/hdr/fcntl_macros.h b/hdr/fcntl_macros.h
index 828cb98..3a1ddeb 100644
--- a/hdr/fcntl_macros.h
+++ b/hdr/fcntl_macros.h
@@ -15,7 +15,7 @@
 
 #else // Overlay mode
 
-#include <fcntl.h>
+#include "hdr/fcntl_overlay.h"
 
 #endif // LLVM_LIBC_FULL_BUILD
 
diff --git a/hdr/fcntl_overlay.h b/hdr/fcntl_overlay.h
new file mode 100644
index 0000000..c1cc98b
--- /dev/null
+++ b/hdr/fcntl_overlay.h
@@ -0,0 +1,37 @@
+//===-- Including fcntl.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_FCNTL_OVERLAY_H
+#define LLVM_LIBC_HDR_FCNTL_OVERLAY_H
+
+#ifdef LIBC_FULL_BUILD
+#error "This header should only be included in overlay mode"
+#endif
+
+// Overlay mode
+
+// glibc <fcntl.h> header might provide extern inline definitions for few
+// functions, causing external alias errors.  They are guarded by
+// `__USE_FORTIFY_LEVEL`, which will be temporarily disabled
+// with `_FORTIFY_SOURCE`.
+
+#ifdef __USE_FORTIFY_LEVEL
+#define LIBC_OLD_USE_FORTIFY_LEVEL __USE_FORTIFY_LEVEL
+#undef __USE_FORTIFY_LEVEL
+#define __USE_FORTIFY_LEVEL 0
+#endif
+
+#include <fcntl.h>
+
+#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
+
+#endif // LLVM_LIBC_HDR_FCNTL_OVERLAY_H
diff --git a/hdr/types/CMakeLists.txt b/hdr/types/CMakeLists.txt
index fab5245..e459798 100644
--- a/hdr/types/CMakeLists.txt
+++ b/hdr/types/CMakeLists.txt
@@ -47,6 +47,17 @@
 )
 
 add_proxy_header_library(
+  mode_t
+  HDRS
+    mode_t.h
+  DEPENDS
+    ../fcntl_overlay
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.mode_t
+    libc.include.fcntl
+)
+
+add_proxy_header_library(
   fenv_t
   HDRS
     fenv_t.h
diff --git a/hdr/types/mode_t.h b/hdr/types/mode_t.h
new file mode 100644
index 0000000..abbbdb0
--- /dev/null
+++ b/hdr/types/mode_t.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from mode_t.h --------------------------------===//
+//
+// 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_MODE_T_H
+#define LLVM_LIBC_HDR_MODE_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/mode_t.h"
+
+#else // Overlay mode
+
+#include "hdr/fcntl_overlay.h"
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_MODE_T_H
diff --git a/src/__support/File/linux/CMakeLists.txt b/src/__support/File/linux/CMakeLists.txt
index 5abbf11..84e3d56 100644
--- a/src/__support/File/linux/CMakeLists.txt
+++ b/src/__support/File/linux/CMakeLists.txt
@@ -7,7 +7,7 @@
     file.h
     lseekImpl.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_syscall
     libc.include.sys_stat
     libc.src.__support.CPP.new
@@ -55,7 +55,7 @@
   SRCS
     dir.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.__support.error_or
diff --git a/src/__support/File/linux/dir.cpp b/src/__support/File/linux/dir.cpp
index fc90ff0..5fe44fa 100644
--- a/src/__support/File/linux/dir.cpp
+++ b/src/__support/File/linux/dir.cpp
@@ -12,7 +12,7 @@
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
 
-#include <fcntl.h>       // For open flags
+#include "hdr/fcntl_macros.h" // For open flags
 #include <sys/syscall.h> // For syscall numbers
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/__support/File/linux/file.cpp b/src/__support/File/linux/file.cpp
index 2229233..824c1f2 100644
--- a/src/__support/File/linux/file.cpp
+++ b/src/__support/File/linux/file.cpp
@@ -18,7 +18,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h" // For error macros
 
-#include <fcntl.h>       // For mode_t and other flags to the open syscall
+#include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
 #include <sys/stat.h>    // For S_IS*, S_IF*, and S_IR* flags.
 #include <sys/syscall.h> // For syscall numbers
 
diff --git a/src/__support/threads/linux/CMakeLists.txt b/src/__support/threads/linux/CMakeLists.txt
index b6796f4..fa11458 100644
--- a/src/__support/threads/linux/CMakeLists.txt
+++ b/src/__support/threads/linux/CMakeLists.txt
@@ -79,7 +79,7 @@
     .futex_utils
     libc.config.app_h
     libc.include.sys_syscall
-    libc.include.fcntl
+    libc.hdr.fcntl_macros  
     libc.src.errno.errno
     libc.src.__support.CPP.atomic
     libc.src.__support.CPP.stringstream
diff --git a/src/__support/threads/linux/thread.cpp b/src/__support/threads/linux/thread.cpp
index ee3f63f..c531d74 100644
--- a/src/__support/threads/linux/thread.cpp
+++ b/src/__support/threads/linux/thread.cpp
@@ -22,7 +22,7 @@
 #include <arm_acle.h>
 #endif
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <linux/param.h> // For EXEC_PAGESIZE.
 #include <linux/prctl.h> // For PR_SET_NAME
 #include <linux/sched.h> // For CLONE_* flags.
diff --git a/src/fcntl/creat.h b/src/fcntl/creat.h
index e180e17..3e00427 100644
--- a/src/fcntl/creat.h
+++ b/src/fcntl/creat.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_FCNTL_CREAT_H
 #define LLVM_LIBC_SRC_FCNTL_CREAT_H
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
-#include <fcntl.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/src/fcntl/linux/CMakeLists.txt b/src/fcntl/linux/CMakeLists.txt
index ee8ae63..ecfb2cd 100644
--- a/src/fcntl/linux/CMakeLists.txt
+++ b/src/fcntl/linux/CMakeLists.txt
@@ -5,7 +5,7 @@
   HDRS
     ../creat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
@@ -17,7 +17,7 @@
   HDRS
     ../fcntl.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.src.__support.OSUtil.osutil
 )
 
@@ -28,7 +28,7 @@
   HDRS
     ../open.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.types.mode_t
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
@@ -40,7 +40,7 @@
   HDRS
     ../openat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.types.mode_t
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
diff --git a/src/fcntl/linux/creat.cpp b/src/fcntl/linux/creat.cpp
index 2c5b5d7..23abae2 100644
--- a/src/fcntl/linux/creat.cpp
+++ b/src/fcntl/linux/creat.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/fcntl/linux/open.cpp b/src/fcntl/linux/open.cpp
index 79b7b2b..9f3d035 100644
--- a/src/fcntl/linux/open.cpp
+++ b/src/fcntl/linux/open.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/types/mode_t.h"
 #include <stdarg.h>
 #include <sys/syscall.h> // For syscall numbers.
 
diff --git a/src/fcntl/linux/openat.cpp b/src/fcntl/linux/openat.cpp
index 0862082..6063d9c 100644
--- a/src/fcntl/linux/openat.cpp
+++ b/src/fcntl/linux/openat.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/types/mode_t.h"
 #include <stdarg.h>
 #include <sys/syscall.h> // For syscall numbers.
 
diff --git a/src/fcntl/open.h b/src/fcntl/open.h
index 19bb53c..11f0ae5 100644
--- a/src/fcntl/open.h
+++ b/src/fcntl/open.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_FCNTL_OPEN_H
 #define LLVM_LIBC_SRC_FCNTL_OPEN_H
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
-#include <fcntl.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/src/fcntl/openat.h b/src/fcntl/openat.h
index d09791a..051c8a2 100644
--- a/src/fcntl/openat.h
+++ b/src/fcntl/openat.h
@@ -9,8 +9,8 @@
 #ifndef LLVM_LIBC_SRC_FCNTL_OPENAT_H
 #define LLVM_LIBC_SRC_FCNTL_OPENAT_H
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
-#include <fcntl.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
diff --git a/src/spawn/linux/CMakeLists.txt b/src/spawn/linux/CMakeLists.txt
index 9ef3a9d..5f7ab4c 100644
--- a/src/spawn/linux/CMakeLists.txt
+++ b/src/spawn/linux/CMakeLists.txt
@@ -5,7 +5,7 @@
   HDRS
     ../posix_spawn.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.types.mode_t
     libc.include.spawn
     libc.include.sys_syscall
     libc.include.signal
diff --git a/src/spawn/linux/posix_spawn.cpp b/src/spawn/linux/posix_spawn.cpp
index 4c0469b..d6caf8b 100644
--- a/src/spawn/linux/posix_spawn.cpp
+++ b/src/spawn/linux/posix_spawn.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/macros/config.h"
 #include "src/spawn/file_actions.h"
 
-#include <fcntl.h>
+#include "hdr/types/mode_t.h"
 #include <signal.h> // For SIGCHLD
 #include <spawn.h>
 #include <sys/syscall.h> // For syscall numbers.
diff --git a/src/stdio/linux/CMakeLists.txt b/src/stdio/linux/CMakeLists.txt
index d6241e1..e81642d 100644
--- a/src/stdio/linux/CMakeLists.txt
+++ b/src/stdio/linux/CMakeLists.txt
@@ -5,7 +5,7 @@
   HDRS
     ../remove.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
diff --git a/src/stdio/linux/remove.cpp b/src/stdio/linux/remove.cpp
index 9e299aa..dbb4491 100644
--- a/src/stdio/linux/remove.cpp
+++ b/src/stdio/linux/remove.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h" // For AT_* macros.
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>       // For AT_* macros.
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/sys/stat/linux/CMakeLists.txt b/src/sys/stat/linux/CMakeLists.txt
index 415d2fa..7c9496b 100644
--- a/src/sys/stat/linux/CMakeLists.txt
+++ b/src/sys/stat/linux/CMakeLists.txt
@@ -5,7 +5,7 @@
   HDRS
     ../chmod.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.types.mode_t
     libc.include.sys_stat
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -19,6 +19,7 @@
   HDRS
     ../fchmod.h
   DEPENDS
+    libc.hdr.types.mode_t
     libc.include.sys_stat
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -45,7 +46,7 @@
   HDRS
     ../mkdir.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.types.mode_t
     libc.include.sys_stat
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -84,7 +85,7 @@
     ../stat.h
   DEPENDS
     .kernel_statx
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
 )
@@ -97,7 +98,7 @@
     ../lstat.h
   DEPENDS
     .kernel_statx
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
 )
@@ -110,7 +111,7 @@
     ../fstat.h
   DEPENDS
     .kernel_statx
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
 )
diff --git a/src/sys/stat/linux/chmod.cpp b/src/sys/stat/linux/chmod.cpp
index c91cabb..9d28603 100644
--- a/src/sys/stat/linux/chmod.cpp
+++ b/src/sys/stat/linux/chmod.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/types/mode_t.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/syscall.h> // For syscall numbers.
 
diff --git a/src/sys/stat/linux/fchmod.cpp b/src/sys/stat/linux/fchmod.cpp
index 7b6c7b7..0d6fd35 100644
--- a/src/sys/stat/linux/fchmod.cpp
+++ b/src/sys/stat/linux/fchmod.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/types/mode_t.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/syscall.h> // For syscall numbers.
 
diff --git a/src/sys/stat/linux/fstat.cpp b/src/sys/stat/linux/fstat.cpp
index 411aa47..35cf8f0 100644
--- a/src/sys/stat/linux/fstat.cpp
+++ b/src/sys/stat/linux/fstat.cpp
@@ -13,7 +13,7 @@
 
 #include "src/__support/common.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/sys/stat/linux/lstat.cpp b/src/sys/stat/linux/lstat.cpp
index 5a6eff0..354c5b6 100644
--- a/src/sys/stat/linux/lstat.cpp
+++ b/src/sys/stat/linux/lstat.cpp
@@ -14,7 +14,7 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/sys/stat/linux/mkdir.cpp b/src/sys/stat/linux/mkdir.cpp
index 527c3d2..bd6efef 100644
--- a/src/sys/stat/linux/mkdir.cpp
+++ b/src/sys/stat/linux/mkdir.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/types/mode_t.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/syscall.h> // For syscall numbers.
 
diff --git a/src/sys/stat/linux/stat.cpp b/src/sys/stat/linux/stat.cpp
index c5149e6..de9cdb1 100644
--- a/src/sys/stat/linux/stat.cpp
+++ b/src/sys/stat/linux/stat.cpp
@@ -13,7 +13,7 @@
 
 #include "src/__support/common.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/CMakeLists.txt b/src/unistd/linux/CMakeLists.txt
index 9b0d752..472438c 100644
--- a/src/unistd/linux/CMakeLists.txt
+++ b/src/unistd/linux/CMakeLists.txt
@@ -5,6 +5,7 @@
   HDRS
     ../access.h
   DEPENDS
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -57,7 +58,7 @@
   HDRS
     ../dup2.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -254,7 +255,7 @@
   HDRS
     ../link.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -268,7 +269,7 @@
   HDRS
     ../linkat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -377,7 +378,7 @@
   HDRS
     ../rmdir.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -391,7 +392,7 @@
   HDRS
     ../readlink.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -405,7 +406,7 @@
   HDRS
     ../readlinkat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -419,7 +420,7 @@
   HDRS
     ../symlink.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -433,7 +434,7 @@
   HDRS
     ../symlinkat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -485,7 +486,7 @@
   HDRS
     ../unlink.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
@@ -499,7 +500,7 @@
   HDRS
     ../unlinkat.h
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
diff --git a/src/unistd/linux/access.cpp b/src/unistd/linux/access.cpp
index e9ad749..2f7ebbc 100644
--- a/src/unistd/linux/access.cpp
+++ b/src/unistd/linux/access.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/dup2.cpp b/src/unistd/linux/dup2.cpp
index 51a19a7..c7c7c1a 100644
--- a/src/unistd/linux/dup2.cpp
+++ b/src/unistd/linux/dup2.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/link.cpp b/src/unistd/linux/link.cpp
index 37ca58e..477806a 100644
--- a/src/unistd/linux/link.cpp
+++ b/src/unistd/linux/link.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/linkat.cpp b/src/unistd/linux/linkat.cpp
index fcd6a5f..40f68cc 100644
--- a/src/unistd/linux/linkat.cpp
+++ b/src/unistd/linux/linkat.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/readlink.cpp b/src/unistd/linux/readlink.cpp
index 7b15245..2055e6b 100644
--- a/src/unistd/linux/readlink.cpp
+++ b/src/unistd/linux/readlink.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/readlinkat.cpp b/src/unistd/linux/readlinkat.cpp
index 19a9ff9..e5e4d0d 100644
--- a/src/unistd/linux/readlinkat.cpp
+++ b/src/unistd/linux/readlinkat.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/rmdir.cpp b/src/unistd/linux/rmdir.cpp
index 8974468..075af12 100644
--- a/src/unistd/linux/rmdir.cpp
+++ b/src/unistd/linux/rmdir.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/symlink.cpp b/src/unistd/linux/symlink.cpp
index 5efd4df..9e1b288 100644
--- a/src/unistd/linux/symlink.cpp
+++ b/src/unistd/linux/symlink.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/symlinkat.cpp b/src/unistd/linux/symlinkat.cpp
index 63d2e6d..bcf2d0f 100644
--- a/src/unistd/linux/symlinkat.cpp
+++ b/src/unistd/linux/symlinkat.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/unlink.cpp b/src/unistd/linux/unlink.cpp
index de7cae8..72d8e23 100644
--- a/src/unistd/linux/unlink.cpp
+++ b/src/unistd/linux/unlink.cpp
@@ -11,9 +11,9 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "hdr/fcntl_macros.h"
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
-#include <fcntl.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/src/unistd/linux/unlinkat.cpp b/src/unistd/linux/unlinkat.cpp
index e794f24..4ed20f5 100644
--- a/src/unistd/linux/unlinkat.cpp
+++ b/src/unistd/linux/unlinkat.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/config.h"
 #include "src/errno/libc_errno.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
diff --git a/test/src/fcntl/CMakeLists.txt b/test/src/fcntl/CMakeLists.txt
index 48048b7..b522fef 100644
--- a/test/src/fcntl/CMakeLists.txt
+++ b/test/src/fcntl/CMakeLists.txt
@@ -42,7 +42,7 @@
   SRCS
     openat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.src.errno.errno
     libc.src.fcntl.open
     libc.src.fcntl.openat
diff --git a/test/src/fcntl/openat_test.cpp b/test/src/fcntl/openat_test.cpp
index 9dafd12..547359e 100644
--- a/test/src/fcntl/openat_test.cpp
+++ b/test/src/fcntl/openat_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 TEST(LlvmLibcUniStd, OpenAndReadTest) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
diff --git a/test/src/sys/sendfile/CMakeLists.txt b/test/src/sys/sendfile/CMakeLists.txt
index 82efaa1..ceaa4ac 100644
--- a/test/src/sys/sendfile/CMakeLists.txt
+++ b/test/src/sys/sendfile/CMakeLists.txt
@@ -9,7 +9,7 @@
   SRCS
     sendfile_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno 
     libc.src.fcntl.open
diff --git a/test/src/sys/sendfile/sendfile_test.cpp b/test/src/sys/sendfile/sendfile_test.cpp
index 5902543..a658212 100644
--- a/test/src/sys/sendfile/sendfile_test.cpp
+++ b/test/src/sys/sendfile/sendfile_test.cpp
@@ -17,7 +17,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 namespace cpp = LIBC_NAMESPACE::cpp;
diff --git a/test/src/sys/stat/CMakeLists.txt b/test/src/sys/stat/CMakeLists.txt
index 877a129..dd3d093 100644
--- a/test/src/sys/stat/CMakeLists.txt
+++ b/test/src/sys/stat/CMakeLists.txt
@@ -9,7 +9,7 @@
   SRCS
     chmod_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.fcntl.open
@@ -25,7 +25,7 @@
   SRCS
     fchmodat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.fcntl.open
@@ -41,7 +41,7 @@
   SRCS
     fchmod_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.fcntl.open
@@ -57,7 +57,7 @@
   SRCS
     mkdirat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.sys.stat.mkdirat
@@ -71,7 +71,7 @@
   SRCS
     stat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.sys.stat.stat
@@ -87,7 +87,7 @@
   SRCS
     lstat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.sys.stat.lstat
@@ -103,7 +103,7 @@
   SRCS
     fstat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_stat
     libc.src.errno.errno
     libc.src.sys.stat.fstat
diff --git a/test/src/sys/stat/chmod_test.cpp b/test/src/sys/stat/chmod_test.cpp
index c688996..83ab0f4 100644
--- a/test/src/sys/stat/chmod_test.cpp
+++ b/test/src/sys/stat/chmod_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcChmodTest, ChangeAndOpen) {
diff --git a/test/src/sys/stat/fchmod_test.cpp b/test/src/sys/stat/fchmod_test.cpp
index 91c0f68..03eb79d 100644
--- a/test/src/sys/stat/fchmod_test.cpp
+++ b/test/src/sys/stat/fchmod_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcChmodTest, ChangeAndOpen) {
diff --git a/test/src/sys/stat/fchmodat_test.cpp b/test/src/sys/stat/fchmodat_test.cpp
index c43ef8a..09970b6 100644
--- a/test/src/sys/stat/fchmodat_test.cpp
+++ b/test/src/sys/stat/fchmodat_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcFchmodatTest, ChangeAndOpen) {
diff --git a/test/src/sys/stat/fstat_test.cpp b/test/src/sys/stat/fstat_test.cpp
index 1379eae..34c675d 100644
--- a/test/src/sys/stat/fstat_test.cpp
+++ b/test/src/sys/stat/fstat_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcFStatTest, CreatAndReadMode) {
diff --git a/test/src/sys/stat/lstat_test.cpp b/test/src/sys/stat/lstat_test.cpp
index b44b3d1..a723d5a 100644
--- a/test/src/sys/stat/lstat_test.cpp
+++ b/test/src/sys/stat/lstat_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcLStatTest, CreatAndReadMode) {
diff --git a/test/src/sys/stat/mkdirat_test.cpp b/test/src/sys/stat/mkdirat_test.cpp
index cbacc16..85e013d 100644
--- a/test/src/sys/stat/mkdirat_test.cpp
+++ b/test/src/sys/stat/mkdirat_test.cpp
@@ -11,7 +11,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 TEST(LlvmLibcMkdiratTest, CreateAndRemove) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
diff --git a/test/src/sys/stat/stat_test.cpp b/test/src/sys/stat/stat_test.cpp
index baf3633..0ddd8ba 100644
--- a/test/src/sys/stat/stat_test.cpp
+++ b/test/src/sys/stat/stat_test.cpp
@@ -14,7 +14,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>
 
 TEST(LlvmLibcStatTest, CreatAndReadMode) {
diff --git a/test/src/unistd/CMakeLists.txt b/test/src/unistd/CMakeLists.txt
index e03e56b..ce936ce 100644
--- a/test/src/unistd/CMakeLists.txt
+++ b/test/src/unistd/CMakeLists.txt
@@ -24,11 +24,12 @@
   SRCS
     chdir_test.cpp
   DEPENDS
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.src.errno.errno
-    libc.src.fcntl.open
     libc.src.unistd.chdir
     libc.src.unistd.close
+    libc.src.fcntl.open 
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
@@ -223,7 +224,7 @@
   SRCS
     rmdir_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.src.errno.errno
     libc.src.sys.stat.mkdir
     libc.src.unistd.rmdir
@@ -262,7 +263,7 @@
   SRCS
     readlinkat_test.cpp
   DEPENDS
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.unistd
     libc.src.errno.errno
     libc.src.unistd.readlinkat
@@ -410,7 +411,7 @@
     syscall_test.cpp
   DEPENDS
     libc.include.unistd
-    libc.include.fcntl
+    libc.hdr.fcntl_macros
     libc.include.sys_syscall
     libc.src.errno.errno 
     libc.src.unistd.__llvm_libc_syscall
diff --git a/test/src/unistd/chdir_test.cpp b/test/src/unistd/chdir_test.cpp
index 51dc7bb..e1bdcd7 100644
--- a/test/src/unistd/chdir_test.cpp
+++ b/test/src/unistd/chdir_test.cpp
@@ -13,7 +13,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 TEST(LlvmLibcChdirTest, ChangeAndOpen) {
   // The idea of this test is that we will first open an existing test file
diff --git a/test/src/unistd/fchdir_test.cpp b/test/src/unistd/fchdir_test.cpp
index ae88e1f..0e39fde 100644
--- a/test/src/unistd/fchdir_test.cpp
+++ b/test/src/unistd/fchdir_test.cpp
@@ -13,7 +13,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 TEST(LlvmLibcChdirTest, ChangeAndOpen) {
   // The idea of this test is that we will first open an existing test file
diff --git a/test/src/unistd/readlinkat_test.cpp b/test/src/unistd/readlinkat_test.cpp
index 1fa683b..9e4bb9a 100644
--- a/test/src/unistd/readlinkat_test.cpp
+++ b/test/src/unistd/readlinkat_test.cpp
@@ -15,7 +15,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 namespace cpp = LIBC_NAMESPACE::cpp;
 
diff --git a/test/src/unistd/rmdir_test.cpp b/test/src/unistd/rmdir_test.cpp
index 93cb0f3..4f4cd94 100644
--- a/test/src/unistd/rmdir_test.cpp
+++ b/test/src/unistd/rmdir_test.cpp
@@ -12,7 +12,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 
 TEST(LlvmLibcRmdirTest, CreateAndRemove) {
   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
diff --git a/test/src/unistd/syscall_test.cpp b/test/src/unistd/syscall_test.cpp
index cee29bd..f6cc3ea 100644
--- a/test/src/unistd/syscall_test.cpp
+++ b/test/src/unistd/syscall_test.cpp
@@ -11,7 +11,7 @@
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
-#include <fcntl.h>
+#include "hdr/fcntl_macros.h"
 #include <sys/stat.h>    // For S_* flags.
 #include <sys/syscall.h> // For syscall numbers.
 #include <unistd.h>