[libcxx-commits] [PATCH] D31413: [libc++] Use __attribute__((init_priority(101))) to ensure streams get initialized early

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 16 09:30:29 PDT 2020


ldionne updated this revision to Diff 292254.
ldionne added a comment.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Added a test. I can't reproduce the issue, so I don't know whether the test is useful or not. Please help with that!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D31413/new/

https://reviews.llvm.org/D31413

Files:
  libcxx/src/iostream.cpp
  libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp


Index: libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/input.output/iostream.objects/narrow.stream.objects/cout.init_priority.sh.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// XFAIL: libcpp-has-no-stdout
+
+// Make sure that the iostreams are initialized before everything else.
+// This has been an issue when statically linking libc++ in some contexts.
+// See https://llvm.org/PR28954 for details.
+
+// RUN: %{build}
+// RUN: %{run} > %t.out
+// RUN: grep -e 'SHOULD BE PRINTED' %t.out
+
+#include <iostream>
+#include "test_macros.h"
+
+
+struct Foo {
+    Foo() {
+        std::cout << "SHOULD BE PRINTED" << std::endl;
+    }
+};
+
+Foo foo;
+
+int main(int, char**) {
+    return 0;
+}
Index: libcxx/src/iostream.cpp
===================================================================
--- libcxx/src/iostream.cpp
+++ libcxx/src/iostream.cpp
@@ -77,7 +77,7 @@
 #endif
 ;
 
-_LIBCPP_HIDDEN ios_base::Init __start_std_streams;
+_LIBCPP_HIDDEN ios_base::Init __start_std_streams __attribute__((init_priority(101)));
 
 // On Windows the TLS storage for locales needs to be initialized before we create
 // the standard streams, otherwise it may not be alive during program termination


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31413.292254.patch
Type: text/x-patch
Size: 1693 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200916/452c5626/attachment.bin>


More information about the libcxx-commits mailing list