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

cmd/objdump: use symbolic names for globals #20650

Closed
randall77 opened this issue Jun 12, 2017 · 5 comments
Closed

cmd/objdump: use symbolic names for globals #20650

randall77 opened this issue Jun 12, 2017 · 5 comments

Comments

@randall77
Copy link
Contributor

randall77 commented Jun 12, 2017

package main

var g int

func main() {
	println(g)
}
> go build tmp.go
> go tool objdump -s main.main tmp
TEXT main.main(SB) /usr/local/google/home/khr/go/tmp.go
	tmp.go:5	0x44e590	64488b0c25f8ffffff	FS MOVQ FS:0xfffffff8, CX		
	tmp.go:5	0x44e599	483b6110		CMPQ 0x10(CX), SP			
	tmp.go:5	0x44e59d	7637			JBE 0x44e5d6				
	tmp.go:5	0x44e59f	4883ec10		SUBQ $0x10, SP				
	tmp.go:5	0x44e5a3	48896c2408		MOVQ BP, 0x8(SP)			
	tmp.go:5	0x44e5a8	488d6c2408		LEAQ 0x8(SP), BP			
	tmp.go:6	0x44e5ad	e86e55fdff		CALL runtime.printlock(SB)		
	tmp.go:6	0x44e5b2	488b056f9a0600		MOVQ 0x69a6f(IP), AX			
	tmp.go:6	0x44e5b9	48890424		MOVQ AX, 0(SP)				
	tmp.go:6	0x44e5bd	e81e5dfdff		CALL runtime.printint(SB)		
	tmp.go:6	0x44e5c2	e84958fdff		CALL runtime.printnl(SB)		
	tmp.go:6	0x44e5c7	e8e455fdff		CALL runtime.printunlock(SB)		
	tmp.go:7	0x44e5cc	488b6c2408		MOVQ 0x8(SP), BP			
	tmp.go:7	0x44e5d1	4883c410		ADDQ $0x10, SP				
	tmp.go:7	0x44e5d5	c3			RET					
	tmp.go:5	0x44e5d6	e81584ffff		CALL runtime.morestack_noctxt(SB)	
	tmp.go:5	0x44e5db	ebb3			JMP main.main(SB)			

Note the MOVQ 0x69a6f(IP), AX instruction. It would be nice if this instead read MOVQ main.g(IP), AX or something.
We do the address->symbol mapping for CALLs, so I suspect we just need to enable that for IP-relative addressing also.

@robbert229
Copy link

I can take a look at this. @randall77

So far from my understanding of objdump the changes for this feature would need to occur in each of the assembly packages used by disasm.go in the cmd/internal/objfile package

"golang.org/x/arch/arm/armasm"
"golang.org/x/arch/ppc64/ppc64asm"
"golang.org/x/arch/x86/x86asm"

I think the changes would be to the decode function in each of the previously stated packages, is this correct?

@randall77
Copy link
Contributor Author

Those are the right packages. I think the code might need to be added to plan9x.go:plan9Arg (at least on x86).

@ccbrown
Copy link

ccbrown commented Sep 11, 2017

So this is pretty easy to do. In fact, I have a patch that I believe does the job. And I've tested it and it works just fine as far as I can tell:

package main

var g = "foo"

func main() {
	println(g)
}
chris@mbp:~/repos/golang/go% ./bin/go tool objdump -s main.main tmp
TEXT main.main(SB) /Users/chris/repos/golang/go/tmp.go
  tmp.go:5		0x104f2c0		65488b0c25a0080000	MOVQ GS:0x8a0, CX			
  tmp.go:5		0x104f2c9		483b6110		CMPQ 0x10(CX), SP			
  tmp.go:5		0x104f2cd		7643			JBE 0x104f312				
  tmp.go:5		0x104f2cf		4883ec18		SUBQ $0x18, SP				
  tmp.go:5		0x104f2d3		48896c2410		MOVQ BP, 0x10(SP)			
  tmp.go:5		0x104f2d8		488d6c2410		LEAQ 0x10(SP), BP			
  tmp.go:6		0x104f2dd		e87e3ffdff		CALL runtime.printlock(SB)		
  tmp.go:6		0x104f2e2		488b0547770500		MOVQ main.g(SB), AX			
  tmp.go:6		0x104f2e9		488b0d48770500		MOVQ main.g+8(SB), CX			
  tmp.go:6		0x104f2f0		48890424		MOVQ AX, 0(SP)				
  tmp.go:6		0x104f2f4		48894c2408		MOVQ CX, 0x8(SP)			
  tmp.go:6		0x104f2f9		e80249fdff		CALL runtime.printstring(SB)		
  tmp.go:6		0x104f2fe		e80d42fdff		CALL runtime.printnl(SB)		
  tmp.go:6		0x104f303		e8e83ffdff		CALL runtime.printunlock(SB)		
  tmp.go:7		0x104f308		488b6c2410		MOVQ 0x10(SP), BP			
  tmp.go:7		0x104f30d		4883c418		ADDQ $0x18, SP				
  tmp.go:7		0x104f311		c3			RET
chris@mbp:~/repos/golang/go% ./bin/go tool objdump -s main.main tmparm
TEXT main.main(SB) /Users/chris/repos/golang/go/tmp.go
  tmp.go:5		0x5ba00			e59a1008		MOVW 0x8(R10), R1		
  tmp.go:5		0x5ba04			e15d0001		CMP R1, R13			
  tmp.go:5		0x5ba08			9a00000b		B.LS 0x5ba3c			
  tmp.go:5		0x5ba0c			e52de00c		MOVW.W R14, -0xc(R13)		
  tmp.go:6		0x5ba10			ebff6154		BL runtime.printlock(SB)	
  tmp.go:6		0x5ba14			e59fb030		MOVW $main.g(SB), R11		
  tmp.go:6		0x5ba18			e59b0000		MOVW (R11), R0			
  tmp.go:6		0x5ba1c			e59fb02c		MOVW $main.g+4(SB), R11		
  tmp.go:6		0x5ba20			e59b1000		MOVW (R11), R1			
  tmp.go:6		0x5ba24			e58d0004		MOVW R0, 0x4(R13)		
  tmp.go:6		0x5ba28			e58d1008		MOVW R1, 0x8(R13)		
  tmp.go:6		0x5ba2c			ebff63a6		BL runtime.printstring(SB)	
  tmp.go:6		0x5ba30			ebff61ce		BL runtime.printnl(SB)		
  tmp.go:6		0x5ba34			ebff6168		BL runtime.printunlock(SB)	
  tmp.go:7		0x5ba38			e49df00c		RET #12	

But what I just can't figure out is how the x/arch tests are supposed to be run. A lot of them are skipped because my environment isn't what's expected (e.g. "/Users/rsc/bin/objdump2" doesn't exist), and a lot of them fail with errors such as this:

ext_test.go:171: decode(0511223344556677885f5f5f5f5f5f5f) = "ADDL $0x44332211, AX", 5, want "ADDL $0x44332211,AX", 5

Of course, this is with a clean working directory. No source changes or anything.

Does anyone (@rsc?) know if there's an easy way I can actually get all of these running and passing?
I haven't been able to find any useful documentation here and I'd love to actually get my changes submitted. Thanks!

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 22, 2017
@gopherbot
Copy link

Change https://golang.org/cl/80843 mentions this issue: cmd/vendor/golang.org/x/arch: revendor at commit dda8112

gopherbot pushed a commit that referenced this issue Nov 29, 2017
Updates #20650 (CL 45099 introduced the feature to x86).

Change-Id: If40cc9d87417a05281d8633f05cd91f6f434b136
Reviewed-on: https://go-review.googlesource.com/80843
Run-TryBot: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@ianlancetaylor ianlancetaylor modified the milestones: Go1.11, Unplanned Jun 14, 2018
@randall77
Copy link
Contributor Author

This looks like it has been fixed.

@golang golang locked and limited conversation to collaborators Dec 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants