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