Stop Gas from generating line info or address ranges for sections that do not contain code or are not loaded

Alan Modra amodra@gmail.com
Tue Nov 17 03:49:32 GMT 2020


Do you like this?  In particular I'm asking about always emitting a
warning when dwarf line info is dropped - you may know a reason why
this shouldn't be done..  Testing still in progress.

	* doc/as.texi (.nop): Document optional size arg.
	* dwarf2dbg.c (dwarf2_gen_line_info_1): Only check SEC_ALLOC
	when ELF.  Warn whenever dwarf line number information is ignored.
	* read.c (s_nop): Extend to support optional size arg.
	* testsuite/gas/elf/dwarf2-20.d: Expect warnings, and exact range.
	* testsuite/gas/elf/dwarf2-20.s: Emit 16 bytes worth of nops.

diff --git a/gas/doc/as.texi b/gas/doc/as.texi
index 4d5294552a..f518651145 100644
--- a/gas/doc/as.texi
+++ b/gas/doc/as.texi
@@ -6167,14 +6167,17 @@ counter, and @code{.nolist} decrements it.  Assembly listings are
 generated whenever the counter is greater than zero.
 
 @node Nop
-@section @code{.nop}
+@section @code{.nop [@var{size}]}
 
 @cindex @code{nop} directive
 @cindex filling memory with no-op instructions
-This directive emits a single no-op instruction.  It is provided on all
-architectures, allowing the creation of architecture neutral tests involving
-actual code.  The size of the generated instruction is target specific.  The
-instruction does affect the generation of DWARF debug line information.
+This directive emits no-op instructions.  It is provided on all architectures,
+allowing the creation of architecture neutral tests involving actual code.  The
+size of the generated instruction is target specific, but if the optional
+@var{size} argument is given and resolves to an absolute positive value at that
+point in assembly (no forward expressions allowed) then the fewest no-op
+instructions are emitted that equal or exceed a total @var{size} in bytes.
+@code{.nop} does affect the generation of DWARF debug line information.
 
 @node Nops
 @section @code{.nops @var{size}[, @var{control}]}
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 25ebc94a8d..e927c5059b 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -504,16 +504,18 @@ dwarf2_gen_line_info_1 (symbolS *label, struct dwarf2_line_info *loc)
 {
   struct line_subseg *lss;
   struct line_entry *e;
-  flagword need_flags = SEC_ALLOC | SEC_LOAD | SEC_CODE;
+  flagword need_flags = SEC_LOAD | SEC_CODE;
 
-  /* PR 26850: Do not record LOCs in non-executable, non-allocated,
-     or non-loaded sections.  */
+  /* PR 26850: Do not record LOCs in non-executable or non-loaded
+     sections.  SEC_ALLOC isn't tested for non-ELF because obj-coff.c
+     obj_coff_section is careless in setting SEC_ALLOC.  */
+  if (IS_ELF)
+    need_flags |= SEC_ALLOC;
   if ((now_seg->flags & need_flags) != need_flags)
     {
-      if (! SEG_NORMAL (now_seg))
-	/* FIXME: Add code to suppress multiple warnings ?  */
-	as_warn ("dwarf line number information for %s ignored",
-		 segment_name (now_seg));
+      /* FIXME: Add code to suppress multiple warnings ?  */
+      as_warn ("dwarf line number information for %s ignored",
+	       segment_name (now_seg));
       return;
     }
 
diff --git a/gas/read.c b/gas/read.c
index 7fd9af05fa..1f30c6b2e8 100644
--- a/gas/read.c
+++ b/gas/read.c
@@ -3506,6 +3506,11 @@ s_space (int mult)
 void
 s_nop (int ignore ATTRIBUTE_UNUSED)
 {
+  expressionS exp;
+  fragS *start;
+  addressT start_off;
+  offsetT frag_off;
+
 #ifdef md_flush_pending_output
   md_flush_pending_output ();
 #endif
@@ -3515,29 +3520,42 @@ s_nop (int ignore ATTRIBUTE_UNUSED)
 #endif
 
   SKIP_WHITESPACE ();
+  expression (&exp);
   demand_empty_rest_of_line ();
 
+  start = frag_now;
+  start_off = frag_now_fix ();
+  do
+    {
 #ifdef md_emit_single_noop
-  md_emit_single_noop;
+      md_emit_single_noop;
 #else
-  char * nop;
+      char *nop;
 
 #ifndef md_single_noop_insn
 #define md_single_noop_insn "nop"
 #endif
-  /* md_assemble might modify its argument, so
-     we must pass it a string that is writeable.  */
-  if (asprintf (&nop, "%s", md_single_noop_insn) < 0)
-    as_fatal ("%s", xstrerror (errno));
-
-  /* Some targets assume that they can update input_line_pointer inside
-     md_assemble, and, worse, that they can leave it assigned to the string
-     pointer that was provided as an argument.  So preserve ilp here.  */
-  char * saved_ilp = input_line_pointer;
-  md_assemble (nop);
-  input_line_pointer = saved_ilp;
-  free (nop);
+      /* md_assemble might modify its argument, so
+	 we must pass it a string that is writable.  */
+      if (asprintf (&nop, "%s", md_single_noop_insn) < 0)
+	as_fatal ("%s", xstrerror (errno));
+
+      /* Some targets assume that they can update input_line_pointer
+	 inside md_assemble, and, worse, that they can leave it
+	 assigned to the string pointer that was provided as an
+	 argument.  So preserve ilp here.  */
+      char *saved_ilp = input_line_pointer;
+      md_assemble (nop);
+      input_line_pointer = saved_ilp;
+      free (nop);
+#ifdef md_flush_pending_output
+      md_flush_pending_output ();
+#endif
 #endif
+    } while (exp.X_op == O_constant
+	     && exp.X_add_number > 0
+	     && frag_offset_fixed_p (start, frag_now, &frag_off)
+	     && frag_off + frag_now_fix () < start_off + exp.X_add_number);
 }
 
 void
diff --git a/gas/testsuite/gas/elf/dwarf2-20.d b/gas/testsuite/gas/elf/dwarf2-20.d
index 363510a9b0..c60b3a649e 100644
--- a/gas/testsuite/gas/elf/dwarf2-20.d
+++ b/gas/testsuite/gas/elf/dwarf2-20.d
@@ -3,6 +3,10 @@
 #name: DWARF2_20: debug ranges ignore non-code sections
 # The mn10200 target has a pointer size of 3, but it does not use segment selectors.  This confuses DWARF and readelf will complain.
 #xfail: mn102*-*
+#warning: .* \.alloc0 ignored
+#warning: .* \.alloc1 ignored
+#warning: .* \.nonalloc ignored
+#warning: .* \.nonallocexec ignored
 
 Contents of the .debug_aranges section:
 
@@ -13,6 +17,6 @@ Contents of the .debug_aranges section:
 [ 	]+Segment Size:[ 	]+0
 
 [ 	]+Address[ 	]+Length
-[ 	]+0+000 0+00. 
+[ 	]+0+000 0+010 
 [ 	]+0+000 0+000 
 #pass
diff --git a/gas/testsuite/gas/elf/dwarf2-20.s b/gas/testsuite/gas/elf/dwarf2-20.s
index 0ad0cc3ff0..00b631448b 100644
--- a/gas/testsuite/gas/elf/dwarf2-20.s
+++ b/gas/testsuite/gas/elf/dwarf2-20.s
@@ -1,10 +1,10 @@
 	.section .alloc0,"a"
-	.nop
+	.nop 16
 	.section .alloc1,"a"
-	.nop
+	.nop 16
 	.section .nonalloc,""
-	.nop
+	.nop 16
 	.section .allocexec,"ax"
-	.nop
+	.nop 16
 	.section .nonallocexec,"x"
-	.nop
+	.nop 16

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list