[libc-commits] [PATCH] D152983: [libc] Move the definitions of the standard IO streams to the platform

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Jun 14 17:03:31 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: lntue, michaelrj, sivachandra.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
jhuber6 requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152983

Files:
  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


Index: libc/src/stdio/stdout.cpp
===================================================================
--- libc/src/stdio/stdout.cpp
+++ 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;
Index: libc/src/stdio/stdin.cpp
===================================================================
--- libc/src/stdio/stdin.cpp
+++ 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;
Index: libc/src/stdio/stderr.cpp
===================================================================
--- libc/src/stdio/stderr.cpp
+++ 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;
Index: libc/src/__support/File/linux/file.cpp
===================================================================
--- libc/src/__support/File/linux/file.cpp
+++ libc/src/__support/File/linux/file.cpp
@@ -183,3 +183,8 @@
 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);
+extern "C" FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
+extern "C" FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);
Index: libc/src/__support/File/gpu/file.cpp
===================================================================
--- libc/src/__support/File/gpu/file.cpp
+++ libc/src/__support/File/gpu/file.cpp
@@ -97,3 +97,8 @@
 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);
+extern "C" FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc::StdErr);
+extern "C" FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc::StdOut);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152983.531575.patch
Type: text/x-patch
Size: 2045 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230615/4fc2cf0b/attachment.bin>


More information about the libc-commits mailing list