[libc-commits] [libc] [llvm] [libc][hdr] Add poll and socket overlay headers for GCC overlay builds. (PR #227961)
via libc-commits
libc-commits at lists.llvm.org
Wed Sep 30 22:49:10 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: lntue
<details>
<summary>Changes</summary>
When building LLVM libc in overlay mode with gcc/g++, alias and macro redirections in GNU libc headers (such as ppoll and socket functions) cause conflict with libc header declarations.
Fixes in this PR:
- Add `poll_overlay.h` and `sys_socket_overlay.h` to wrap system headers before libc macros and type declarations in overlay mode.
- Update proxy headers in `libc/hdr/` and `libc/hdr/types/`.
- Update CMake and Bazel build files accordingly.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/227961.diff
17 Files Affected:
- (modified) libc/hdr/CMakeLists.txt (+6)
- (modified) libc/hdr/poll_macros.h (+1-1)
- (added) libc/hdr/poll_overlay.h (+69)
- (modified) libc/hdr/sys_socket_macros.h (+1-1)
- (added) libc/hdr/sys_socket_overlay.h (+69)
- (modified) libc/hdr/types/CMakeLists.txt (+20)
- (modified) libc/hdr/types/nfds_t.h (+1-1)
- (modified) libc/hdr/types/sa_family_t.h (+1-1)
- (modified) libc/hdr/types/socklen_t.h (+1-1)
- (modified) libc/hdr/types/struct_cmsghdr.h (+1-1)
- (modified) libc/hdr/types/struct_linger.h (+1-1)
- (modified) libc/hdr/types/struct_mmsghdr.h (+1-1)
- (modified) libc/hdr/types/struct_msghdr.h (+1-1)
- (modified) libc/hdr/types/struct_pollfd.h (+1-1)
- (modified) libc/hdr/types/struct_sockaddr.h (+1-1)
- (modified) libc/hdr/types/struct_sockaddr_storage.h (+1-1)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+46-5)
``````````diff
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 6aeb342a63c94..63077607bb4fb 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -120,10 +120,13 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.netinet_in_macros
)
+add_header_library(poll_overlay HDRS poll_overlay.h)
add_proxy_header_library(
poll_macros
HDRS
poll_macros.h
+ DEPENDS
+ .poll_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.poll-macros
)
@@ -234,10 +237,13 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_sem_macros
)
+add_header_library(sys_socket_overlay HDRS sys_socket_overlay.h)
add_proxy_header_library(
sys_socket_macros
HDRS
sys_socket_macros.h
+ DEPENDS
+ .sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.sys_socket_macros
)
diff --git a/libc/hdr/poll_macros.h b/libc/hdr/poll_macros.h
index 1567b6d8b2483..0a3fbb18a723e 100644
--- a/libc/hdr/poll_macros.h
+++ b/libc/hdr/poll_macros.h
@@ -20,7 +20,7 @@
#else // Overlay mode
-#include <poll.h>
+#include "hdr/poll_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/poll_overlay.h b/libc/hdr/poll_overlay.h
new file mode 100644
index 0000000000000..6ffa3e1d89a28
--- /dev/null
+++ b/libc/hdr/poll_overlay.h
@@ -0,0 +1,69 @@
+//===-- Including poll.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_POLL_OVERLAY_H
+#define LLVM_LIBC_HDR_POLL_OVERLAY_H
+
+#ifdef LIBC_FULL_BUILD
+#error "This header should only be included in overlay mode"
+#endif
+
+// Overlay mode
+
+// glibc <poll.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 <poll.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
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#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
+
+#include <poll.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_POLL_OVERLAY_H
diff --git a/libc/hdr/sys_socket_macros.h b/libc/hdr/sys_socket_macros.h
index 48fe13186449f..1517c7134fa64 100644
--- a/libc/hdr/sys_socket_macros.h
+++ b/libc/hdr/sys_socket_macros.h
@@ -15,7 +15,7 @@
#else // Overlay mode
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/sys_socket_overlay.h b/libc/hdr/sys_socket_overlay.h
new file mode 100644
index 0000000000000..122bc0cef46f2
--- /dev/null
+++ b/libc/hdr/sys_socket_overlay.h
@@ -0,0 +1,69 @@
+//===-- Including sys/socket.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_SYS_SOCKET_OVERLAY_H
+#define LLVM_LIBC_HDR_SYS_SOCKET_OVERLAY_H
+
+#ifdef LIBC_FULL_BUILD
+#error "This header should only be included in overlay mode"
+#endif
+
+// Overlay mode
+
+// glibc <sys/socket.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 <sys/socket.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
+
+#ifndef __NO_INLINE__
+#define __NO_INLINE__ 1
+#define LIBC_SET_NO_INLINE
+#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
+
+#include <sys/socket.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_SYS_SOCKET_OVERLAY_H
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 505c2d9fa6579..92a2985b74be7 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -558,6 +558,8 @@ add_proxy_header_library(
struct_linger
HDRS
struct_linger.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_linger
libc.include.sys_socket
@@ -567,6 +569,8 @@ add_proxy_header_library(
struct_cmsghdr
HDRS
struct_cmsghdr.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_cmsghdr
libc.include.sys_socket
@@ -576,6 +580,8 @@ add_proxy_header_library(
struct_mmsghdr
HDRS
struct_mmsghdr.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_mmsghdr
libc.include.sys_socket
@@ -585,6 +591,8 @@ add_proxy_header_library(
struct_msghdr
HDRS
struct_msghdr.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_msghdr
libc.include.sys_socket
@@ -711,6 +719,8 @@ add_proxy_header_library(
struct_sockaddr
HDRS
struct_sockaddr.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_sockaddr
libc.include.sys_socket
@@ -728,6 +738,8 @@ add_proxy_header_library(
struct_sockaddr_storage
HDRS
struct_sockaddr_storage.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_sockaddr_storage
libc.include.sys_socket
@@ -791,6 +803,8 @@ add_proxy_header_library(
sa_family_t
HDRS
sa_family_t.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.sa_family_t
libc.include.sys_socket
@@ -800,6 +814,8 @@ add_proxy_header_library(
socklen_t
HDRS
socklen_t.h
+ DEPENDS
+ libc.hdr.sys_socket_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.socklen_t
libc.include.sys_socket
@@ -856,6 +872,8 @@ add_proxy_header_library(
nfds_t
HDRS
nfds_t.h
+ DEPENDS
+ libc.hdr.poll_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.nfds_t
)
@@ -864,6 +882,8 @@ add_proxy_header_library(
struct_pollfd
HDRS
struct_pollfd.h
+ DEPENDS
+ libc.hdr.poll_overlay
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_pollfd
)
diff --git a/libc/hdr/types/nfds_t.h b/libc/hdr/types/nfds_t.h
index f390bbe603779..22db1ffdf4e15 100644
--- a/libc/hdr/types/nfds_t.h
+++ b/libc/hdr/types/nfds_t.h
@@ -16,7 +16,7 @@
#else // overlay mode
-#include <poll.h>
+#include "hdr/poll_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/sa_family_t.h b/libc/hdr/types/sa_family_t.h
index af26df226be81..cec1d639901a4 100644
--- a/libc/hdr/types/sa_family_t.h
+++ b/libc/hdr/types/sa_family_t.h
@@ -20,7 +20,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/socklen_t.h b/libc/hdr/types/socklen_t.h
index 547a6a6a0cbea..cf75fccafc5e3 100644
--- a/libc/hdr/types/socklen_t.h
+++ b/libc/hdr/types/socklen_t.h
@@ -14,7 +14,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_cmsghdr.h b/libc/hdr/types/struct_cmsghdr.h
index b84a1a2ede798..a494f7217ecc8 100644
--- a/libc/hdr/types/struct_cmsghdr.h
+++ b/libc/hdr/types/struct_cmsghdr.h
@@ -14,7 +14,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_linger.h b/libc/hdr/types/struct_linger.h
index 6343e7dda2bdc..c0e48c9129136 100644
--- a/libc/hdr/types/struct_linger.h
+++ b/libc/hdr/types/struct_linger.h
@@ -15,7 +15,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_mmsghdr.h b/libc/hdr/types/struct_mmsghdr.h
index 84473bbd46685..5a28e43ef8267 100644
--- a/libc/hdr/types/struct_mmsghdr.h
+++ b/libc/hdr/types/struct_mmsghdr.h
@@ -19,7 +19,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_msghdr.h b/libc/hdr/types/struct_msghdr.h
index 6a36af791e214..29a4fb6bccb79 100644
--- a/libc/hdr/types/struct_msghdr.h
+++ b/libc/hdr/types/struct_msghdr.h
@@ -14,7 +14,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_pollfd.h b/libc/hdr/types/struct_pollfd.h
index ec56d841aced3..1d9d30a67122c 100644
--- a/libc/hdr/types/struct_pollfd.h
+++ b/libc/hdr/types/struct_pollfd.h
@@ -16,7 +16,7 @@
#else // overlay mode
-#include <poll.h>
+#include "hdr/poll_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_sockaddr.h b/libc/hdr/types/struct_sockaddr.h
index 0fc31d5374854..b006233692ca8 100644
--- a/libc/hdr/types/struct_sockaddr.h
+++ b/libc/hdr/types/struct_sockaddr.h
@@ -14,7 +14,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/libc/hdr/types/struct_sockaddr_storage.h b/libc/hdr/types/struct_sockaddr_storage.h
index c8559f2f84fb5..f1790d09bfcb9 100644
--- a/libc/hdr/types/struct_sockaddr_storage.h
+++ b/libc/hdr/types/struct_sockaddr_storage.h
@@ -14,7 +14,7 @@
#else
-#include <sys/socket.h>
+#include "hdr/sys_socket_overlay.h"
#endif // LIBC_FULL_BUILD
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 2319d757be655..bccee3c081af4 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -775,6 +775,24 @@ libc_support_library(
}),
)
+libc_support_library(
+ name = "hdr_poll_overlay",
+ hdrs = ["hdr/poll_overlay.h"],
+ target_compatible_with = select({
+ ":full_build": ["@platforms//:incompatible"],
+ "//conditions:default": [],
+ }),
+)
+
+libc_support_library(
+ name = "hdr_sys_socket_overlay",
+ hdrs = ["hdr/sys_socket_overlay.h"],
+ target_compatible_with = select({
+ ":full_build": ["@platforms//:incompatible"],
+ "//conditions:default": [],
+ }),
+)
+
libc_support_library(
name = "hdr_netinet_in_macros",
hdrs = ["hdr/netinet_in_macros.h"],
@@ -842,9 +860,12 @@ libc_support_library(
libc_support_library(
name = "hdr_sys_socket_macros",
hdrs = ["hdr/sys_socket_macros.h"],
- deps = [
- ":llvm_libc_macros_sys_socket_macros",
- ],
+ deps = select({
+ ":full_build": [
+ ":llvm_libc_macros_sys_socket_macros",
+ ],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
@@ -1228,7 +1249,7 @@ libc_support_library(
hdrs = ["hdr/types/nfds_t.h"],
deps = select({
":full_build": [":llvm_libc_types_nfds_t"],
- "//conditions:default": [],
+ "//conditions:default": [":hdr_poll_overlay"],
}),
)
@@ -1237,7 +1258,7 @@ libc_support_library(
hdrs = ["hdr/types/struct_pollfd.h"],
deps = select({
":full_build": [":llvm_libc_types_struct_pollfd"],
- "//conditions:default": [],
+ "//conditions:default": [":hdr_poll_overlay"],
}),
)
@@ -1383,11 +1404,19 @@ libc_support_library(
libc_support_library(
name = "types_sa_family_t",
hdrs = ["hdr/types/sa_family_t.h"],
+ deps = select({
+ ":full_build": [],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
name = "types_socklen_t",
hdrs = ["hdr/types/socklen_t.h"],
+ deps = select({
+ ":full_build": [],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
@@ -1408,6 +1437,10 @@ libc_support_library(
libc_support_library(
name = "types_struct_sockaddr",
hdrs = ["hdr/types/struct_sockaddr.h"],
+ deps = select({
+ ":full_build": [],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
@@ -1423,6 +1456,10 @@ libc_support_library(
libc_support_library(
name = "types_struct_sockaddr_storage",
hdrs = ["hdr/types/struct_sockaddr_storage.h"],
+ deps = select({
+ ":full_build": [],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
@@ -1433,6 +1470,10 @@ libc_support_library(
libc_support_library(
name = "types_struct_linger",
hdrs = ["hdr/types/struct_linger.h"],
+ deps = select({
+ ":full_build": [],
+ "//conditions:default": [":hdr_sys_socket_overlay"],
+ }),
)
libc_support_library(
``````````
</details>
https://github.com/llvm/llvm-project/pull/227961
More information about the libc-commits
mailing list