fba59c4
diff --git a/gold/i386.cc b/gold/i386.cc
fba59c4
index bf209fe9a86..31161ff091c 100644
fba59c4
--- a/gold/i386.cc
fba59c4
+++ b/gold/i386.cc
fba59c4
@@ -360,7 +360,11 @@ class Target_i386 : public Sized_target<32, false>
fba59c4
       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
fba59c4
       got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
fba59c4
       rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY),
fba59c4
-      got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
fba59c4
+      got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
fba59c4
+      isa_1_used_(0), isa_1_needed_(0),
fba59c4
+      feature_1_(0), feature_2_used_(0), feature_2_needed_(0),
fba59c4
+      object_isa_1_used_(0), object_feature_1_(0),
fba59c4
+      object_feature_2_used_(0), seen_first_object_(false)
fba59c4
   { }
fba59c4
 
fba59c4
   // Process the relocations to determine unreferenced sections for
fba59c4
@@ -859,6 +863,21 @@ class Target_i386 : public Sized_target<32, false>
fba59c4
 				  this->rel_dyn_section(layout));
fba59c4
   }
fba59c4
 
fba59c4
+  // Record a target-specific program property in the .note.gnu.property
fba59c4
+  // section.
fba59c4
+  void
fba59c4
+  record_gnu_property(unsigned int, unsigned int, size_t,
fba59c4
+		      const unsigned char*, const Object*);
fba59c4
+
fba59c4
+  // Merge the target-specific program properties from the current object.
fba59c4
+  void
fba59c4
+  merge_gnu_properties(const Object*);
fba59c4
+
fba59c4
+  // Finalize the target-specific program properties and add them back to
fba59c4
+  // the layout.
fba59c4
+  void
fba59c4
+  do_finalize_gnu_properties(Layout*) const;
fba59c4
+
fba59c4
   // Information about this specific target which we pass to the
fba59c4
   // general Target structure.
fba59c4
   static const Target::Target_info i386_info;
fba59c4
@@ -898,6 +917,26 @@ class Target_i386 : public Sized_target<32, false>
fba59c4
   unsigned int got_mod_index_offset_;
fba59c4
   // True if the _TLS_MODULE_BASE_ symbol has been defined.
fba59c4
   bool tls_base_symbol_defined_;
fba59c4
+
fba59c4
+  // Target-specific program properties, from .note.gnu.property section.
fba59c4
+  // Each bit represents a specific feature.
fba59c4
+  uint32_t isa_1_used_;
fba59c4
+  uint32_t isa_1_needed_;
fba59c4
+  uint32_t feature_1_;
fba59c4
+  uint32_t feature_2_used_;
fba59c4
+  uint32_t feature_2_needed_;
fba59c4
+  // Target-specific properties from the current object.
fba59c4
+  // These bits get ORed into ISA_1_USED_ after all properties for the object
fba59c4
+  // have been processed. But if either is all zeroes (as when the property
fba59c4
+  // is absent from an object), the result should be all zeroes.
fba59c4
+  // (See PR ld/23486.)
fba59c4
+  uint32_t object_isa_1_used_;
fba59c4
+  // These bits get ANDed into FEATURE_1_ after all properties for the object
fba59c4
+  // have been processed.
fba59c4
+  uint32_t object_feature_1_;
fba59c4
+  uint32_t object_feature_2_used_;
fba59c4
+  // Whether we have seen our first object, for use in initializing FEATURE_1_.
fba59c4
+  bool seen_first_object_;
fba59c4
 };
fba59c4
 
fba59c4
 const Target::Target_info Target_i386::i386_info =
fba59c4
@@ -1042,6 +1081,126 @@ Target_i386::rel_irelative_section(Layout* layout)
fba59c4
   return this->rel_irelative_;
fba59c4
 }
fba59c4
 
fba59c4
+// Record a target-specific program property from the .note.gnu.property
fba59c4
+// section.
fba59c4
+void
fba59c4
+Target_i386::record_gnu_property(
fba59c4
+    unsigned int, unsigned int pr_type,
fba59c4
+    size_t pr_datasz, const unsigned char* pr_data,
fba59c4
+    const Object* object)
fba59c4
+{
fba59c4
+  uint32_t val = 0;
fba59c4
+
fba59c4
+  switch (pr_type)
fba59c4
+    {
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
fba59c4
+      if (pr_datasz != 4)
fba59c4
+	{
fba59c4
+	  gold_warning(_("%s: corrupt .note.gnu.property section "
fba59c4
+			 "(pr_datasz for property %d is not 4)"),
fba59c4
+		       object->name().c_str(), pr_type);
fba59c4
+	  return;
fba59c4
+	}
fba59c4
+      val = elfcpp::Swap<32, false>::readval(pr_data);
fba59c4
+      break;
fba59c4
+    default:
fba59c4
+      gold_warning(_("%s: unknown program property type 0x%x "
fba59c4
+		     "in .note.gnu.property section"),
fba59c4
+		   object->name().c_str(), pr_type);
fba59c4
+      break;
fba59c4
+    }
fba59c4
+
fba59c4
+  switch (pr_type)
fba59c4
+    {
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
fba59c4
+      this->object_isa_1_used_ |= val;
fba59c4
+      break;
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
fba59c4
+      this->isa_1_needed_ |= val;
fba59c4
+      break;
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
fba59c4
+      // If we see multiple feature props in one object, OR them together.
fba59c4
+      this->object_feature_1_ |= val;
fba59c4
+      break;
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
fba59c4
+      this->object_feature_2_used_ |= val;
fba59c4
+      break;
fba59c4
+    case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
fba59c4
+      this->feature_2_needed_ |= val;
fba59c4
+      break;
fba59c4
+    }
fba59c4
+}
fba59c4
+
fba59c4
+// Merge the target-specific program properties from the current object.
fba59c4
+void
fba59c4
+Target_i386::merge_gnu_properties(const Object*)
fba59c4
+{
fba59c4
+  if (this->seen_first_object_)
fba59c4
+    {
fba59c4
+      // If any object is missing the ISA_1_USED property, we must omit
fba59c4
+      // it from the output file.
fba59c4
+      if (this->object_isa_1_used_ == 0)
fba59c4
+	this->isa_1_used_ = 0;
fba59c4
+      else if (this->isa_1_used_ != 0)
fba59c4
+	this->isa_1_used_ |= this->object_isa_1_used_;
fba59c4
+      this->feature_1_ &= this->object_feature_1_;
fba59c4
+      // If any object is missing the FEATURE_2_USED property, we must
fba59c4
+      // omit it from the output file.
fba59c4
+      if (this->object_feature_2_used_ == 0)
fba59c4
+	this->feature_2_used_ = 0;
fba59c4
+      else if (this->feature_2_used_ != 0)
fba59c4
+	this->feature_2_used_ |= this->object_feature_2_used_;
fba59c4
+    }
fba59c4
+  else
fba59c4
+    {
fba59c4
+      this->isa_1_used_ = this->object_isa_1_used_;
fba59c4
+      this->feature_1_ = this->object_feature_1_;
fba59c4
+      this->feature_2_used_ = this->object_feature_2_used_;
fba59c4
+      this->seen_first_object_ = true;
fba59c4
+    }
fba59c4
+  this->object_isa_1_used_ = 0;
fba59c4
+  this->object_feature_1_ = 0;
fba59c4
+  this->object_feature_2_used_ = 0;
fba59c4
+}
fba59c4
+
fba59c4
+static inline void
fba59c4
+add_property(Layout* layout, unsigned int pr_type, uint32_t val)
fba59c4
+{
fba59c4
+  unsigned char buf[4];
fba59c4
+  elfcpp::Swap<32, false>::writeval(buf, val);
fba59c4
+  layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
fba59c4
+}
fba59c4
+
fba59c4
+// Finalize the target-specific program properties and add them back to
fba59c4
+// the layout.
fba59c4
+void
fba59c4
+Target_i386::do_finalize_gnu_properties(Layout* layout) const
fba59c4
+{
fba59c4
+  if (this->isa_1_used_ != 0)
fba59c4
+    add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
fba59c4
+		 this->isa_1_used_);
fba59c4
+  if (this->isa_1_needed_ != 0)
fba59c4
+    add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
fba59c4
+		 this->isa_1_needed_);
fba59c4
+  if (this->feature_1_ != 0)
fba59c4
+    add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
fba59c4
+		 this->feature_1_);
fba59c4
+  if (this->feature_2_used_ != 0)
fba59c4
+    add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
fba59c4
+		 this->feature_2_used_);
fba59c4
+  if (this->feature_2_needed_ != 0)
fba59c4
+    add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
fba59c4
+		 this->feature_2_needed_);
fba59c4
+}
fba59c4
+
fba59c4
 // Write the first three reserved words of the .got.plt section.
fba59c4
 // The remainder of the section is written while writing the PLT
fba59c4
 // in Output_data_plt_i386::do_write.