Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEBUGGER_REGNO #31

Closed
vinriviere opened this issue Oct 28, 2023 · 4 comments
Closed

DEBUGGER_REGNO #31

vinriviere opened this issue Oct 28, 2023 · 4 comments

Comments

@vinriviere
Copy link
Member

As mentioned by @th-otto here: #23 (comment)
There is a question about how to configure DEBUGGER_REGNO.

Official GCC documentation:
https://gcc.gnu.org/onlinedocs/gccint/All-Debuggers.html#index-DEBUGGER_005fREGNO

This is a mapping between internal GCC register numbers and gdb register numbers. As I understand, this matters for DWARF-2 debug information and maybe DWARF-2 exceptions.

Default GCC implementation:

/* How to renumber registers for gdb. If not defined, assume
no renumbering is necessary. */
#ifndef DEBUGGER_REGNO
#define DEBUGGER_REGNO(REGNO) (REGNO)
#endif

Default m68k implementation (Sun-3):

/* On the Sun-3, the floating point registers have numbers
18 to 25, not 16 to 23 as they do in the compiler. */
#define DEBUGGER_REGNO(REGNO) ((REGNO) < 16 ? (REGNO) : (REGNO) + 2)

m68k-linux implementation:

#undef DEBUGGER_REGNO
#define DEBUGGER_REGNO(REGNO) (REGNO)

m68k-elf implementation (SVR4):

/* Define how the m68k registers should be numbered for Dwarf output.
The numbering provided here should be compatible with the native
SVR4 debugger in the m68k/SVR4 reference port, where d0-d7
are 0-7, a0-a8 are 8-15, and fp0-fp7 are 16-23. */
#undef DEBUGGER_REGNO
#define DEBUGGER_REGNO(REGNO) (REGNO)

So for this parameter, the default m68k value is hacked for Sun-3 compatibility. This affects the numbering of FPU registers. While m68k-elf reverts it to the default GCC value.

So what to do for the m68k-atari-mintelf target?

As the default m68k value seems to be hacked specifically for Sun-3 compatibility, I don't think we need that. So I think we should revert DEBUGGER_REGNO back to normal, just like m68k-elf.

@th-otto
Copy link
Contributor

th-otto commented Oct 28, 2023

As the default m68k value seems to be hacked specifically for Sun-3 compatibility, I don't think we need that. So I think we should revert DEBUGGER_REGNO back to normal, just like m68k-elf.

Yes, i agree. It will make things much simpler. We don't have to care about debuggers for sun-3. I've done that in my branch already. One of the few settings that should be copied from m68kelf.h to mint.h when you get rid of m68kelf.h.

I think that is even required, because most of the code in gdb was copied/modelled after bsd sources, and i don't think the register numbers are mapped back there.

@vinriviere
Copy link
Member Author

I'm trying to check that DEBUGGER_REGNO setting with a testcase. Not simple stuff, but I think I got it.

regno.c:

float f(float p)
{
	register float v = p*3;
	return v;
}
$ m68k-atari-mintelf-gcc -m68020-60 -O0 -fomit-frame-pointer -S regno.c -o -
#NO_APP
	.file	"regno.c"
	.text
	.align	2
	.globl	f
	.type	f, @function
f:
	fmovem #4,-(%sp)
	fmove.s 16(%sp),%fp2
	fsglmul.s #0x40400000,%fp2
	fmove.s %fp2,%d0
	fmovem (%sp)+,#32
	rts
	.size	f, .-f
	.ident	"GCC: (GCC MiNT ELF 20231027) 13.2.0"

We can see that the only FPU register used is fp2.

$ m68k-atari-mintelf-gcc -m68020-60 -O0 -fomit-frame-pointer -c regno.c -g
$ m68k-atari-mintelf-readelf -w regno.o
Contents of the .debug_frame section:


00000000 00000010 ffffffff CIE
  Version:               3
  Augmentation:          ""
  Code alignment factor: 1
  Data alignment factor: -2
  Return address column: 24

  DW_CFA_def_cfa: r15 ofs 4
  DW_CFA_offset: r24 at cfa-4
  DW_CFA_nop
  DW_CFA_nop

00000014 00000018 00000000 FDE cie=00000000 pc=00000000..0000001c
  DW_CFA_advance_loc4: 4 to 00000004
  DW_CFA_def_cfa_offset: 16
  DW_CFA_offset: r18 at cfa-16
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop

Contents of the .debug_info section:

  Compilation Unit @ offset 0:
   Length:        0x5c (32-bit)
   Version:       5
   Unit Type:     DW_UT_compile (1)
   Abbrev Offset: 0
   Pointer Size:  4
 <0><c>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <d>   DW_AT_producer    : (indirect string, offset: 0x6): GNU C17 13.2.0 -m68020-60 -g -O0 -fomit-frame-pointer
    <11>   DW_AT_language    : 29	(C11)
    <12>   DW_AT_name        : (indirect string, offset: 0x3c): regno.c
    <16>   DW_AT_comp_dir    : (indirect string, offset: 0x44): /home/vincent/tests_gcc
    <1a>   DW_AT_low_pc      : 0
    <1e>   DW_AT_high_pc     : 0x1c
    <22>   DW_AT_stmt_list   : 0
 <1><26>: Abbrev Number: 2 (DW_TAG_subprogram)
    <27>   DW_AT_external    : 1
    <27>   DW_AT_name        : f
    <29>   DW_AT_decl_file   : 1
    <2a>   DW_AT_decl_line   : 1
    <2b>   DW_AT_decl_column : 7
    <2c>   DW_AT_prototyped  : 1
    <2c>   DW_AT_type        : <0x58>
    <30>   DW_AT_low_pc      : 0
    <34>   DW_AT_high_pc     : 0x1c
    <38>   DW_AT_frame_base  : 1 byte block: 9c 	(DW_OP_call_frame_cfa)
    <3a>   DW_AT_call_all_calls: 1
    <3a>   DW_AT_sibling     : <0x58>
 <2><3e>: Abbrev Number: 3 (DW_TAG_formal_parameter)
    <3f>   DW_AT_name        : p
    <41>   DW_AT_decl_file   : 1
    <42>   DW_AT_decl_line   : 1
    <43>   DW_AT_decl_column : 15
    <44>   DW_AT_type        : <0x58>
    <48>   DW_AT_location    : 2 byte block: 91 0 	(DW_OP_fbreg: 0)
 <2><4b>: Abbrev Number: 4 (DW_TAG_variable)
    <4c>   DW_AT_name        : v
    <4e>   DW_AT_decl_file   : 1
    <4f>   DW_AT_decl_line   : 3
    <50>   DW_AT_decl_column : 17
    <51>   DW_AT_type        : <0x58>
    <55>   DW_AT_location    : 1 byte block: 62 	(DW_OP_reg18 (r18))
 <2><57>: Abbrev Number: 0
 <1><58>: Abbrev Number: 5 (DW_TAG_base_type)
    <59>   DW_AT_byte_size   : 4
    <5a>   DW_AT_encoding    : 4	(float)
    <5b>   DW_AT_name        : (indirect string, offset: 0): float
 <1><5f>: Abbrev Number: 0

Contents of the .debug_abbrev section:

  Number TAG (0)
   1      DW_TAG_compile_unit    [has children]
    DW_AT_producer     DW_FORM_strp
    DW_AT_language     DW_FORM_data1
    DW_AT_name         DW_FORM_strp
    DW_AT_comp_dir     DW_FORM_strp
    DW_AT_low_pc       DW_FORM_addr
    DW_AT_high_pc      DW_FORM_data4
    DW_AT_stmt_list    DW_FORM_sec_offset
    DW_AT value: 0     DW_FORM value: 0
   2      DW_TAG_subprogram    [has children]
    DW_AT_external     DW_FORM_flag_present
    DW_AT_name         DW_FORM_string
    DW_AT_decl_file    DW_FORM_data1
    DW_AT_decl_line    DW_FORM_data1
    DW_AT_decl_column  DW_FORM_data1
    DW_AT_prototyped   DW_FORM_flag_present
    DW_AT_type         DW_FORM_ref4
    DW_AT_low_pc       DW_FORM_addr
    DW_AT_high_pc      DW_FORM_data4
    DW_AT_frame_base   DW_FORM_exprloc
    DW_AT_call_all_calls DW_FORM_flag_present
    DW_AT_sibling      DW_FORM_ref4
    DW_AT value: 0     DW_FORM value: 0
   3      DW_TAG_formal_parameter    [no children]
    DW_AT_name         DW_FORM_string
    DW_AT_decl_file    DW_FORM_data1
    DW_AT_decl_line    DW_FORM_data1
    DW_AT_decl_column  DW_FORM_data1
    DW_AT_type         DW_FORM_ref4
    DW_AT_location     DW_FORM_exprloc
    DW_AT value: 0     DW_FORM value: 0
   4      DW_TAG_variable    [no children]
    DW_AT_name         DW_FORM_string
    DW_AT_decl_file    DW_FORM_data1
    DW_AT_decl_line    DW_FORM_data1
    DW_AT_decl_column  DW_FORM_data1
    DW_AT_type         DW_FORM_ref4
    DW_AT_location     DW_FORM_exprloc
    DW_AT value: 0     DW_FORM value: 0
   5      DW_TAG_base_type    [no children]
    DW_AT_byte_size    DW_FORM_data1
    DW_AT_encoding     DW_FORM_data1
    DW_AT_name         DW_FORM_strp
    DW_AT value: 0     DW_FORM value: 0

Contents of the .debug_aranges section:

  Length:                   28
  Version:                  2
  Offset into .debug_info:  0
  Pointer Size:             4
  Segment Size:             0

    Address    Length
    00000000 0000001c
    00000000 00000000

Raw dump of debug contents of section .debug_line:

  Offset:                      0
  Length:                      60
  DWARF Version:               3
  Prologue Length:             30
  Minimum Instruction Length:  2
  Initial value of 'is_stmt':  1
  Line Base:                   -5
  Line Range:                  14
  Opcode Base:                 13

 Opcodes:
  Opcode 1 has 0 args
  Opcode 2 has 1 arg
  Opcode 3 has 1 arg
  Opcode 4 has 1 arg
  Opcode 5 has 1 arg
  Opcode 6 has 0 args
  Opcode 7 has 0 args
  Opcode 8 has 0 args
  Opcode 9 has 1 arg
  Opcode 10 has 0 args
  Opcode 11 has 0 args
  Opcode 12 has 1 arg

 The Directory Table is empty.

 The File Name Table (offset 0x1c):
  Entry	Dir	Time	Size	Name
  1	0	0	0	regno.c

 Line Number Statements:
  [0x00000028]  Set column to 1
  [0x0000002a]  Extended opcode 2: set Address to 0
  [0x00000031]  Special opcode 6: advance Address by 0 to 0 and Line by 1 to 2
  [0x00000032]  Set column to 17
  [0x00000034]  Special opcode 34: advance Address by 4 to 0x4 and Line by 1 to 3
  [0x00000035]  Set column to 9
  [0x00000037]  Special opcode 104: advance Address by 14 to 0x12 and Line by 1 to 4
  [0x00000038]  Set column to 1
  [0x0000003a]  Special opcode 34: advance Address by 4 to 0x16 and Line by 1 to 5
  [0x0000003b]  Advance PC by 6 to 0x1c
  [0x0000003d]  Extended opcode 1: End of Sequence


Contents of the .debug_str section:

  0x00000000 666c6f61 7400474e 55204331 37203133 float.GNU C17 13
  0x00000010 2e322e30 202d6d36 38303230 2d363020 .2.0 -m68020-60 
  0x00000020 2d67202d 4f30202d 666f6d69 742d6672 -g -O0 -fomit-fr
  0x00000030 616d652d 706f696e 74657200 7265676e ame-pointer.regn
  0x00000040 6f2e6300 2f686f6d 652f7669 6e63656e o.c./home/vincen
  0x00000050 742f7465 7374735f 67636300          t/tests_gcc.

I guess that the relevant code is:

  DW_CFA_offset: r18 at cfa-16

where r18 means fp2. So fp0 would be 16, as expected.

If I'm brave enough, I will try to recompile gcc with the other setting (bad m68k default value) to see if that r18 is replaced by r20.

@vinriviere
Copy link
Member Author

I guess that the relevant code is:

Ah, I guessed wrong.

$ diff -u 1 2
--- 1	2023-10-31 11:25:42.052665014 +0100
+++ 2	2023-10-31 11:25:48.404591062 +0100
@@ -63,7 +63,7 @@
     <4f>   DW_AT_decl_line   : 3
     <50>   DW_AT_decl_column : 17
     <51>   DW_AT_type        : <0x58>
-    <55>   DW_AT_location    : 1 byte block: 62 	(DW_OP_reg18 (r18))
+    <55>   DW_AT_location    : 1 byte block: 64 	(DW_OP_reg20 (r20))
  <2><57>: Abbrev Number: 0
  <1><58>: Abbrev Number: 5 (DW_TAG_base_type)
     <59>   DW_AT_byte_size   : 4

So the relevant keyword is DW_AT_location. We can see that (for fp2 register) it contains r18 with the right DEBUGGER_REGNO setting, and r20 with the wrong. As expected.

@vinriviere
Copy link
Member Author

To write it explicitly: the right setting for m68k-atari-mint* targets is:

#define DEBUGGER_REGNO(REGNO) (REGNO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants