[libc-commits] [libc] a09bec6 - [libc] Move the definitions of the standard IO streams to the platform
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Thu Jun 15 05:06:55 PDT 2023
Author: Joseph Huber
Date: 2023-06-15T07:06:43-05:00
New Revision: a09bec6459331e7f949c2ed3df6102de52d25f5d
URL: https://github.com/llvm/llvm-project/commit/a09bec6459331e7f949c2ed3df6102de52d25f5d
DIFF: https://github.com/llvm/llvm-project/commit/a09bec6459331e7f949c2ed3df6102de52d25f5d.diff
LOG: [libc] Move the definitions of the standard IO streams to the platform
This patch moves the definitions of the standard IO streams to the
platform file definition. This is necessary because previously we had a
level of indirection where the stream's `FILE *` was initialized based
on the pointer to the internal `__llvm_libc` version. This cannot be
resolved ahead of time by the linker because the address will not be
known until runtime. This caused the previous implementation to emit a
global constructor to initialize the pointer to the actual `FILE *`. By
moving these definitions so that we can bind their address to the
original file type we can avoid this global constructor.
This file keeps the entrypoints, but makes them empty files only
containing an external reference. This is so they still appear as
entrypoints and get emitted as declarations in the generated headers.
Reviewed By: lntue, sivachandra
Differential Revision: https://reviews.llvm.org/D152983
Added:
Modified:
libc/src/__support/File/gpu/file.cpp
libc/src/__support/File/linux/file.cpp
libc/src/stdio/stderr.cpp
libc/src/stdio/stdin.cpp
libc/src/stdio/stdout.cpp
Removed:
################################################################################
diff --git a/libc/src/__support/File/gpu/file.cpp b/libc/src/__support/File/gpu/file.cpp
index 9985a3c76df97..2db56d7ce5e5f 100644
--- a/libc/src/__support/File/gpu/file.cpp
+++ b/libc/src/__support/File/gpu/file.cpp
@@ -97,3 +97,10 @@ static GPUFile StdErr(0UL, File::ModeFlags(File::OpenMode::APPEND));
File *stderr = &StdErr;
} // namespace __llvm_libc
+
+// Provide the external defintitions of the standard IO streams.
+extern "C" {
+FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc::StdIn);
+FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
+FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
+}
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 02746eeedc0bf..7fb7cb587c86f 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -183,3 +183,10 @@ static LinuxFile StdErr(2, nullptr, STDERR_BUFFER_SIZE, _IONBF, false,
File *stderr = &StdErr;
} // namespace __llvm_libc
+
+// Provide the external defintitions of the standard IO streams.
+extern "C" {
+FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc::StdIn);
+FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
+FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
+}
diff --git a/libc/src/stdio/stderr.cpp b/libc/src/stdio/stderr.cpp
index 56ec9b78a67f0..31b618f6928a0 100644
--- a/libc/src/stdio/stderr.cpp
+++ b/libc/src/stdio/stderr.cpp
@@ -10,6 +10,4 @@
#include <stdio.h>
-extern "C" {
-FILE *stderr = reinterpret_cast<FILE *>(__llvm_libc::stderr);
-}
+extern "C" FILE *stderr;
diff --git a/libc/src/stdio/stdin.cpp b/libc/src/stdio/stdin.cpp
index b6ae95d32ff40..f99ea898dfcd1 100644
--- a/libc/src/stdio/stdin.cpp
+++ b/libc/src/stdio/stdin.cpp
@@ -10,6 +10,4 @@
#include <stdio.h>
-extern "C" {
-FILE *stdin = reinterpret_cast<FILE *>(__llvm_libc::stdin);
-}
+extern "C" FILE *stdin;
diff --git a/libc/src/stdio/stdout.cpp b/libc/src/stdio/stdout.cpp
index 7eb9e1bf8e735..2d5aaf5667260 100644
--- a/libc/src/stdio/stdout.cpp
+++ b/libc/src/stdio/stdout.cpp
@@ -10,6 +10,4 @@
#include <stdio.h>
-extern "C" {
-FILE *stdout = reinterpret_cast<FILE *>(__llvm_libc::stdout);
-}
+extern "C" FILE *stdout;
More information about the libc-commits
mailing list