$OpenBSD$

Add a dummy -nopie flag to not accidentally trigger -n
Properly check for -nopie
Force -pie by default
Add -z wxneeded option (and -z nowxneeded option)
.openbsd.randomdata and .openbsd.wxneeded support

Index: gold/options.h
--- gold/options.h.orig
+++ gold/options.h
@@ -1061,6 +1061,8 @@ class General_options
 
   // n
 
+  DEFINE_bool(nopie, options::ONE_DASH, '\0', false,
+	      N_("Do not create a position independent executable"), NULL);
   DEFINE_bool(nmagic, options::TWO_DASHES, 'n', false,
 	      N_("Do not page align data"), NULL);
   DEFINE_bool(omagic, options::EXACTLY_TWO_DASHES, 'N', false,
@@ -1102,7 +1104,7 @@ class General_options
   DEFINE_bool(p, options::ONE_DASH, 'p', false,
 	      N_("Ignored for ARM compatibility"), NULL);
 
-  DEFINE_bool(pie, options::ONE_DASH, '\0', false,
+  DEFINE_bool(pie, options::ONE_DASH, '\0', true,
 	      N_("Create a position independent executable"),
 	      N_("Do not create a position independent executable"));
   DEFINE_bool_alias(pic_executable, pie, options::TWO_DASHES, '\0',
@@ -1514,6 +1516,8 @@ class General_options
   DEFINE_bool(now, options::DASH_Z, '\0', false,
 	      N_("Mark object for immediate function binding"),
 	      NULL);
+  DEFINE_bool(nowxneeded, options::DASH_Z, '\0', false,
+	      N_("Mark output as not requiring wxneeded"), NULL);
   DEFINE_bool(origin, options::DASH_Z, '\0', false,
 	      N_("Mark DSO to indicate that needs immediate $ORIGIN "
 		 "processing at runtime"), NULL);
@@ -1541,6 +1545,8 @@ class General_options
 	      N_("Keep .text.hot, .text.startup, .text.exit and .text.unlikely "
 		 "as separate sections in the final binary."),
 	      N_("Merge all .text.* prefix sections."));
+  DEFINE_bool(wxneeded, options::DASH_Z, '\0', false,
+	      N_("Mark output as requiring wxneeded"), NULL);
 
 
  public:
@@ -1570,7 +1576,7 @@ class General_options
   // the output is position-independent or not.
   bool
   output_is_position_independent() const
-  { return this->shared() || this->pie(); }
+  { return this->pie() && !this->nopie(); }
 
   // Return true if the output is something that can be exec()ed, such
   // as a static executable, or a position-dependent or
@@ -1629,6 +1635,10 @@ class General_options
   { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
 
   bool
+  is_wxneeded_set() const
+  { return this->wxneeded_status_ == WXNEEDED_YES; }
+
+  bool
   is_stack_executable() const
   { return this->execstack_status_ == EXECSTACK_YES; }
 
@@ -1839,6 +1849,17 @@ class General_options
     EXECSTACK_NO
   };
 
+  // Whether to mark the binary as wxneeded.
+  enum Wxneeded
+  {
+    // Not set on command line.
+    WXNEEDED_FROM_INPUT,
+    // Mark the binary as wxneeded (-z wxneeded).
+    WXNEEDED_YES,
+    // Mark the binary as not wxneeded (-z nowxneeded).
+    WXNEEDED_NO
+  };
+
   enum Icf_status
   {
     // Do not fold any functions (Default or --icf=none).
@@ -1858,6 +1879,10 @@ class General_options
   { this->execstack_status_ = value; }
 
   void
+  set_wxneeded_status(Wxneeded value)
+  { this->wxneeded_status_ = value; }
+
+  void
   set_do_demangle(bool value)
   { this->do_demangle_ = value; }
 
@@ -1903,6 +1928,8 @@ class General_options
   bool printed_version_;
   // Whether to mark the stack as executable.
   Execstack execstack_status_;
+  // Whether to mark the binary as wxneeded.
+  Wxneeded wxneeded_status_;
   // Whether to do code folding.
   Icf_status icf_status_;
   // Whether to do a static link.
