[PATCH] as: Add a .dwarf_level directive to set the DWARF level

H.J. Lu hjl.tools@gmail.com
Sun Jan 17 04:48:19 GMT 2021


Currently

$ as -o x.o x.s

fails when x.s contains DWARF5 info.  Add a .dwarf_level directive to
set the DWARF level and GCC can emit ".dwarf_level 5" when generating
DWARF5 info so that

$ gcc -S -g -c x.c
$ gcc -c x.s

works.

	PR gas/27195
	* NEWS: Mention .dwarf_level.
	* read.c (s_dwarf_level): New.
	(potable): Add dwarf_level.
	* doc/as.texi: Document .dwarf_level.
	* testsuite/gas/elf/elf.exp: Run PR gas/27195 tests.
	* testsuite/gas/elf/dwarf-5-directive-1.d: New file.
	* testsuite/gas/elf/dwarf-5-directive-1.s: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-2.d: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-2.l: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-2.s: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-3.d: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-3.l: Likewise.
	* testsuite/gas/elf/dwarf-5-directive-3.s: Likewise.
---
 gas/NEWS                                    |  2 ++
 gas/doc/as.texi                             |  7 +++++++
 gas/read.c                                  | 12 ++++++++++++
 gas/testsuite/gas/elf/dwarf-5-directive-1.d | 12 ++++++++++++
 gas/testsuite/gas/elf/dwarf-5-directive-1.s | 15 +++++++++++++++
 gas/testsuite/gas/elf/dwarf-5-directive-2.d |  3 +++
 gas/testsuite/gas/elf/dwarf-5-directive-2.l |  2 ++
 gas/testsuite/gas/elf/dwarf-5-directive-2.s | 15 +++++++++++++++
 gas/testsuite/gas/elf/dwarf-5-directive-3.d |  3 +++
 gas/testsuite/gas/elf/dwarf-5-directive-3.l |  2 ++
 gas/testsuite/gas/elf/dwarf-5-directive-3.s | 15 +++++++++++++++
 gas/testsuite/gas/elf/elf.exp               |  3 +++
 12 files changed, 91 insertions(+)
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-1.d
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-1.s
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-2.d
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-2.l
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-2.s
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-3.d
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-3.l
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-directive-3.s

diff --git a/gas/NEWS b/gas/NEWS
index 33dc91b48b4..67d7bfaae40 100644
--- a/gas/NEWS
+++ b/gas/NEWS
@@ -2,6 +2,8 @@
 
 Changes in 2.36:
 
+* Added a .dwarf_level directive to set the DWARF level.
+
 * Add support for Intel AVX VNNI instructions.
 
 * Add support for Intel HRESET instruction.
diff --git a/gas/doc/as.texi b/gas/doc/as.texi
index e0559cf0f1d..2f751bc16a0 100644
--- a/gas/doc/as.texi
+++ b/gas/doc/as.texi
@@ -4384,6 +4384,7 @@ Some machine configurations provide additional directives.
 @end ifset
 
 * Double::                      @code{.double @var{flonums}}
+* Dwarf_level::                 @code{.dwarf_level @var{level}}
 * Eject::                       @code{.eject}
 * Else::                        @code{.else}
 * Elseif::                      @code{.elseif}
@@ -5188,6 +5189,12 @@ in @sc{ieee} format.
 @end ifset
 @end ifclear
 
+@node Dwarf_level
+@section @code{.dwarf_level @var{level}}
+
+@cindex @code{dwarf_level} directive
+Set the DWARF level to @var{level}.  Must be between 2 and 5.
+
 @node Eject
 @section @code{.eject}
 
diff --git a/gas/read.c b/gas/read.c
index 06ca7fbb95f..0f0fb0887c8 100644
--- a/gas/read.c
+++ b/gas/read.c
@@ -343,6 +343,17 @@ po_entry_find (htab_t table, const char *poc_name)
 
 static struct htab *po_hash;
 
+static void
+s_dwarf_level (int arg ATTRIBUTE_UNUSED)
+{
+  dwarf_level = get_absolute_expression ();
+  if (dwarf_level < 2 || dwarf_level > 5)
+    as_fatal (_("invalid DWARF level %d (must be between 2 and 5)"),
+	      dwarf_level);
+  SKIP_WHITESPACE ();
+  demand_empty_rest_of_line ();
+}
+
 static const pseudo_typeS potable[] = {
   {"abort", s_abort, 0},
   {"align", s_align_ptwo, 0},
@@ -394,6 +405,7 @@ static const pseudo_typeS potable[] = {
 #endif
 /* dim  */
   {"double", float_cons, 'd'},
+  {"dwarf_level", s_dwarf_level, 0},
 /* dsect  */
   {"eject", listing_eject, 0},	/* Formfeed listing.  */
   {"else", s_else, 0},
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-1.d b/gas/testsuite/gas/elf/dwarf-5-directive-1.d
new file mode 100644
index 00000000000..74247c492df
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-1.d
@@ -0,0 +1,12 @@
+#as:
+#name: DWARF5 directive 1
+#readelf: -wi
+
+#...
+  Compilation Unit @ offset 0x0:
+   Length:        0x.*
+   Version:       5
+   Unit Type:     DW_UT_compile \(1\)
+   Abbrev Offset: 0x0
+   Pointer Size:  .
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-1.s b/gas/testsuite/gas/elf/dwarf-5-directive-1.s
new file mode 100644
index 00000000000..598ec9e65eb
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-1.s
@@ -0,0 +1,15 @@
+	.dwarf_level 5
+	.text
+	.file 1 "foo/bar.s"
+	.loc_mark_labels 1
+	.globl foobar
+	.type   foobar, %function
+foobar:
+	.quad   0x1
+	.size foobar, .-foobar
+
+	.globl baz
+	.type  baz, %function
+baz:
+	.quad 0x1
+	.size baz, .-baz
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-2.d b/gas/testsuite/gas/elf/dwarf-5-directive-2.d
new file mode 100644
index 00000000000..5fd8991338c
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-2.d
@@ -0,0 +1,3 @@
+#as:
+#name: DWARF5 directive 2
+#error_output: dwarf-5-directive-2.l
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-2.l b/gas/testsuite/gas/elf/dwarf-5-directive-2.l
new file mode 100644
index 00000000000..fab172218db
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-2.l
@@ -0,0 +1,2 @@
+[^:]*: Assembler messages:
+[^:]*.* Fatal error: invalid DWARF level 1 \(must be between 2 and 5\)
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-2.s b/gas/testsuite/gas/elf/dwarf-5-directive-2.s
new file mode 100644
index 00000000000..3d34e34b215
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-2.s
@@ -0,0 +1,15 @@
+	.dwarf_level 1
+	.text
+	.file 1 "foo/bar.s"
+	.loc_mark_labels 1
+	.globl foobar
+	.type   foobar, %function
+foobar:
+	.quad   0x1
+	.size foobar, .-foobar
+
+	.globl baz
+	.type  baz, %function
+baz:
+	.quad 0x1
+	.size baz, .-baz
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-3.d b/gas/testsuite/gas/elf/dwarf-5-directive-3.d
new file mode 100644
index 00000000000..f3a1625efaf
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-3.d
@@ -0,0 +1,3 @@
+#as:
+#name: DWARF5 directive 3
+#error_output: dwarf-5-directive-3.l
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-3.l b/gas/testsuite/gas/elf/dwarf-5-directive-3.l
new file mode 100644
index 00000000000..6d8b748a438
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-3.l
@@ -0,0 +1,2 @@
+[^:]*: Assembler messages:
+[^:]*.* Fatal error: invalid DWARF level 6 \(must be between 2 and 5\)
diff --git a/gas/testsuite/gas/elf/dwarf-5-directive-3.s b/gas/testsuite/gas/elf/dwarf-5-directive-3.s
new file mode 100644
index 00000000000..0cec4c9a94c
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-directive-3.s
@@ -0,0 +1,15 @@
+	.dwarf_level 6
+	.text
+	.file 1 "foo/bar.s"
+	.loc_mark_labels 1
+	.globl foobar
+	.type   foobar, %function
+foobar:
+	.quad   0x1
+	.size foobar, .-foobar
+
+	.globl baz
+	.type  baz, %function
+baz:
+	.quad 0x1
+	.size baz, .-baz
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index a0f98eddd67..881cca0b9e1 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -297,6 +297,9 @@ if { [is_elf_format] } then {
     run_dump_test "dwarf-5-file0" $dump_opts
     run_dump_test "dwarf-4-cu" $dump_opts
     run_dump_test "dwarf-5-cu" $dump_opts
+    run_dump_test "dwarf-5-directive-1" $dump_opts
+    run_dump_test "dwarf-5-directive-2" $dump_opts
+    run_dump_test "dwarf-5-directive-3" $dump_opts
     run_dump_test "dwarf-5-nop-for-line-table" $dump_opts
     run_dump_test "pr25917"
     run_dump_test "bss"
-- 
2.29.2



More information about the Binutils mailing list