[gold patch] Add warning for executable stacks

Cary Coutant ccoutant@google.com
Thu Dec 9 21:33:00 GMT 2010


This patch adds a --warn-execstack option, to have the linker print a
warning for any object that will cause the output to have an
executable stack.

I tested by hand with the existing memory_test test case, but I'm not
sure if it's reasonable to bake into a test case the assumption that
the assembler by default will leave out the .note.GNU-stack section,
or that the --execstack and --noexecstack options to gas are portable.

OK?

-cary


        * layout.cc (Layout::layout_gnu_stack): Add warnings for executable
        stack.
        * layout.h (Layout::layout_gnu_stack): Add pointer to Object
        parameter; change all callers.
        * object.cc (Sized_relobj::do_layout): Adjust call to layout_gnu_stack.
        * options.h (warn_execstack): New option.
-------------- next part --------------
Index: layout.cc
===================================================================
RCS file: /cvs/src/src/gold/layout.cc,v
retrieving revision 1.188
diff -u -p -r1.188 layout.cc
--- layout.cc	16 Nov 2010 19:18:31 -0000	1.188
+++ layout.cc	9 Dec 2010 21:22:44 -0000
@@ -1344,15 +1344,28 @@ Layout::expected_segment_count() const
 // object.  On some targets that will force an executable stack.
 
 void
-Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
+Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
+			 const Object* obj)
 {
   if (!seen_gnu_stack)
-    this->input_without_gnu_stack_note_ = true;
+    {
+      this->input_without_gnu_stack_note_ = true;
+      if (parameters->options().warn_execstack()
+	  && parameters->target().is_default_stack_executable())
+	gold_warning(_("%s: missing .note.GNU-stack section"),
+		     obj->name().c_str());
+    }
   else
     {
       this->input_with_gnu_stack_note_ = true;
       if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
-	this->input_requires_executable_stack_ = true;
+	{
+	  this->input_requires_executable_stack_ = true;
+	  if (parameters->options().warn_execstack()
+	      || parameters->options().is_stack_executable())
+	    gold_warning(_("%s: requires executable stack"),
+			 obj->name().c_str());
+	}
     }
 }
 
Index: layout.h
===================================================================
RCS file: /cvs/src/src/gold/layout.h,v
retrieving revision 1.86
diff -u -p -r1.86 layout.h
--- layout.h	25 Aug 2010 08:36:54 -0000	1.86
+++ layout.h	9 Dec 2010 21:22:44 -0000
@@ -470,7 +470,8 @@ class Layout
   // .note.GNU-stack section.  GNU_STACK_FLAGS is the section flags
   // from that section if there was one.
   void
-  layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags);
+  layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
+		   const Object*);
 
   // Add an Output_section_data to the layout.  This is used for
   // special sections like the GOT section.  ORDER is where the
Index: object.cc
===================================================================
RCS file: /cvs/src/src/gold/object.cc,v
retrieving revision 1.132
diff -u -p -r1.132 object.cc
--- object.cc	9 Sep 2010 19:57:06 -0000	1.132
+++ object.cc	9 Dec 2010 21:22:44 -0000
@@ -1429,7 +1429,7 @@ Sized_relobj<size, big_endian>::do_layou
     }
 
   if (!is_gc_pass_two)
-    layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
+    layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this);
 
   // When doing a relocatable link handle the reloc sections at the
   // end.  Garbage collection  and Identical Code Folding is not 
Index: options.h
===================================================================
RCS file: /cvs/src/src/gold/options.h,v
retrieving revision 1.152
diff -u -p -r1.152 options.h
--- options.h	14 Oct 2010 22:10:22 -0000	1.152
+++ options.h	9 Dec 2010 21:22:45 -0000
@@ -1035,6 +1035,10 @@ class General_options
   DEFINE_bool(warn_constructors, options::TWO_DASHES, '\0', false,
 	      N_("Ignored"), N_("Ignored"));
 
+  DEFINE_bool(warn_execstack, options::TWO_DASHES, '\0', false,
+	      N_("Warn if the stack is executable"),
+	      N_("Do not warn if the stack is executable (default)"));
+
   DEFINE_bool(warn_mismatch, options::TWO_DASHES, '\0', true,
 	      NULL, N_("Don't warn about mismatched input files"));
 


More information about the Binutils mailing list