[20/21] Workaround for line info in compressed MIPS func

Message ID 20250402121759.1962001-21-jovan.dmitrovic@htecgroup.com
State New
Headers
Series Integrate MIPS-Specific Support |

Commit Message

Jovan Dmitrovic April 2, 2025, 12:18 p.m. UTC
  From: Matthew Fortune <matthew.fortune@mips.com>

This fix allows the first address of a MIPS16/microMIPS function to
be used to get the filename/line and function that it belongs to
whereas it has previously needed the ISA bit to be set in the address
for the first instruction to match up correctly.

Note: we should really be checking the output of addr2line, but the
binutils test-framework doesn't have that capability yet.
Expected addr2line output for both new tests is:

$ addr2line -a 0x400000 0x400004 0x40000c -p -e <elf>
0x00400000: .*-lineno.c:1
0x00400004: .*-lineno.c:2
0x0040000c: .*-lineno.c:3

Cherry-picked 2279503
from https://github.com/MIPS/binutils-gdb

Signed-off-by: Faraz Shahbazker <fshahbazker@wavecomp.com>
Signed-off-by: Milica Matic <milica.matic@htecgroup.com>

Changelog:

bfd/
	* elfxx-mips.c (_bfd_mips_elf_find_nearest_line): If no matching
	line is found for an address, retry with the ISA bit set in case
	searching at the beginning of a MIPS16/microMIPS section.

ld/testsuite/
	* ld-mips-elf/mips16-lineno.s: New test source.
	* ld-mips-elf/micromips-lineno.s: New test source.
	* ld-mips-elf/mips16-lineno.d: New test.
	* ld-mips-elf/micromips-lineno.d: New test.
	* ld-mips-elf/mips-elf.exp: Run the new tests
---
 bfd/elfxx-mips.c                            | 19 +++++++--
 ld/testsuite/ld-mips-elf/micromips-lineno.d | 33 +++++++++++++++
 ld/testsuite/ld-mips-elf/micromips-lineno.s | 44 ++++++++++++++++++++
 ld/testsuite/ld-mips-elf/mips16-lineno.d    | 32 ++++++++++++++
 ld/testsuite/ld-mips-elf/mips16-lineno.s    | 46 +++++++++++++++++++++
 5 files changed, 171 insertions(+), 3 deletions(-)
 create mode 100644 ld/testsuite/ld-mips-elf/micromips-lineno.d
 create mode 100644 ld/testsuite/ld-mips-elf/micromips-lineno.s
 create mode 100644 ld/testsuite/ld-mips-elf/mips16-lineno.d
 create mode 100644 ld/testsuite/ld-mips-elf/mips16-lineno.s
  

Patch

diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 68b439c456a..f0c5e7ae6cd 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -13423,6 +13423,7 @@  _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
 				 unsigned int *discriminator_ptr)
 {
   asection *msec;
+  bool found = false;
 
   if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
 				     filename_ptr, functionname_ptr,
@@ -13513,9 +13514,21 @@  _bfd_mips_elf_find_nearest_line (bfd *abfd, asymbol **symbols,
 
   /* Fall back on the generic ELF find_nearest_line routine.  */
 
-  return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
-				     filename_ptr, functionname_ptr,
-				     line_ptr, discriminator_ptr);
+  found = _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
+				       filename_ptr, functionname_ptr,
+				       line_ptr, discriminator_ptr);
+
+  /* The bfd_find_nearest_line_discriminator may not be able to find filename
+     and line if pc points to start of a MIPS compressed function.  This is
+     because ISA bit is set in the line number entries.  For example, if start
+     of a function is 0x80200750 then line number entries start from 0x80200751
+     (31st bit is set).  Set ISA bit of the pc and search again.  
+  if (found && *filename_ptr == NULL && *line_ptr == 0 && (offset & 1) == 0)
+    found = _bfd_elf_find_nearest_line (abfd, symbols, section,
+					offset | 1,
+					filename_ptr, functionname_ptr,
+					line_ptr, discriminator_ptr);  */
+  return found;
 }
 
 bool
diff --git a/ld/testsuite/ld-mips-elf/micromips-lineno.d b/ld/testsuite/ld-mips-elf/micromips-lineno.d
new file mode 100644
index 00000000000..09d8008580d
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/micromips-lineno.d
@@ -0,0 +1,33 @@ 
+#name: MICROMIPS address mapping to debug line information
+#ld: -Ttext 0x400020
+#objdump: -dlr
+#source: micromips-lineno.s
+
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+
+00400020 <__start>:
+__start\(\):
+  400020:	00000000 	nop
+
+00400024 <foo>:
+foo\(\):
+  400024:	0c00      	nop
+.*micromips-lineno.c:1
+  400026:	47e3      	jrc	ra
+
+00400028 <bar>:
+.*micromips-lineno.c:2
+  400028:	03e00009 	jr	ra
+bar\(\):
+.*micromips-lineno.c:2
+  40002c:	00000000 	nop
+
+00400030 <baz>:
+baz\(\):
+  400030:	0c00      	nop
+.*micromips-lineno.c:3
+  400032:	47e3      	jrc	ra
+
+#pass
diff --git a/ld/testsuite/ld-mips-elf/micromips-lineno.s b/ld/testsuite/ld-mips-elf/micromips-lineno.s
new file mode 100644
index 00000000000..0dced268360
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/micromips-lineno.s
@@ -0,0 +1,44 @@ 
+	# Dummy file name
+	.file 1 "micromips-lineno.c"
+	.section .text.foo,"ax",@progbits
+	.globl foo
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	# Dummy line number 1
+	.loc 1 1 0
+	.set	micromips
+	.set	nomips16
+	.ent	foo
+foo:
+	nop
+	jr	$ra
+	.end	foo
+	.section .text.bar,"ax",@progbits
+	.globl	bar
+	# Dummy line number 2
+	.loc 1 2 0
+	.set	nomicromips
+	.ent	bar
+bar:
+	nop
+	jr	$ra
+	.end	bar
+	.section .text.baz,"ax",@progbits
+	.globl	baz
+	# Dummy line number 3
+	.loc 1 3 0
+	.set	micromips
+	.ent	baz
+baz:
+	nop
+	jr	$ra
+	.end	baz
+
+	.text
+main:
+start:
+_start:
+__start:
+	.byte 0
diff --git a/ld/testsuite/ld-mips-elf/mips16-lineno.d b/ld/testsuite/ld-mips-elf/mips16-lineno.d
new file mode 100644
index 00000000000..05dde9348a4
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/mips16-lineno.d
@@ -0,0 +1,32 @@ 
+#name: MIPS16 address mapping to debug line information
+#as: -march=mips2 -mabi=32
+#ld: -Ttext 0x400020
+#objdump: -dlr
+#source: mips16-lineno.s
+
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+
+00400020 <\_\_start>:
+\_\_start\(\):
+  400020:	00000000 	nop
+
+00400024 <foo>:
+foo\(\):
+  400024:	e820      	jr	ra
+.*mips16-lineno.c:1
+  400026:	6500      	nop
+
+00400028 <bar>:
+.*mips16-lineno.c:2
+  400028:	03e00008 	jr	ra
+bar\(\):
+.*mips16-lineno.c:2
+  40002c:	00000000 	nop
+
+00400030 <baz>:
+baz\(\):
+  400030:	e820      	jr	ra
+.*mips16-lineno.c:3
+  400032:	6500      	nop
diff --git a/ld/testsuite/ld-mips-elf/mips16-lineno.s b/ld/testsuite/ld-mips-elf/mips16-lineno.s
new file mode 100644
index 00000000000..48c2baa4948
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/mips16-lineno.s
@@ -0,0 +1,46 @@ 
+	# Dummy file name
+	.file 1 "mips16-lineno.c"
+	.section .text.foo,"ax",@progbits
+	.globl foo
+	.globl main
+	.globl start
+	.globl _start
+	.globl __start
+	# Dummy line number 1
+	.loc 1 1 0
+	.set	mips16
+	.set	nomicromips
+	.ent	foo
+foo:
+	nop
+	jr	$ra
+	.end	foo
+	.section .text.bar,"ax",@progbits
+	.globl	bar
+	# Dummy line number 2
+	.loc 1 2 0
+	.set	nomips16
+	.ent	bar
+bar:
+	nop
+	jr	$ra
+	.end	bar
+	.section .text.baz,"ax",@progbits
+	.globl	baz
+	# Dummy line number 3
+	.loc 1 3 0
+	.set	mips16
+	.ent	baz
+baz:
+	nop
+	jr	$ra
+	.end	baz
+
+
+	.text
+main:
+start:
+_start:
+__start:
+	.byte 0
+