Blame SOURCES/binutils-gold-i386-gnu-property-notes.patch

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