Skip to content

cmd/asm: doesn't handle register offset correctly when GOARCH=arm #36253

Open

Description

@ClarkGuan

What version of Go are you using (go version)?

$ go version
go version go1.13.5 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/dd/Library/Caches/go-build"
GOENV="/Users/dd/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/dd/dev/source/golang/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n5/26_m50tn4_j2n3gmvt19300h0000gn/T/go-build651869539=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

main.go:

package main

import (
	"fmt"
)

func b()

func print32(i uint32) {
	fmt.Printf("0x%x\n", i)
}

func main() {
	b()
}

asm_arm.s:

#include "textflag.h"

TEXT ·b(SB), NOSPLIT, $4
    MOVW    $1, R0
    MOVW    $4, R1
    MOVW    R0, (R13)(R1)
    CALL    ·print32(SB)
    RET

then run,

objdump -d -df 'main.b' ./demo

and

go tool objdump -S -s 'main\.b' ./demo

./demo is the target executable file.

What did you expect to see?

main.b:
   b8e18:       08 e0 2d e5     str     lr, [sp, #-8]!
   b8e1c:       01 00 a0 e3     mov     r0, #1
   b8e20:       04 10 a0 e3     mov     r1, #4
   b8e24:       01 00 8d e7     str     r0, [sp, r1]
   b8e28:       c5 ff ff eb     bl      #-236 <main.print32>
   b8e2c:       08 f0 9d e4     ldr     pc, [sp], #8687 

and

TEXT main.b(SB) 
  0xb8e18               e52de008                MOVW.W R14, -0x8(R13)   
  0xb8e1c               e3a00001                MOVW $1, R0             
  0xb8e20               e3a01004                MOVW $4, R1             
  0xb8e24               e78d0001                MOVW R0, (R13)(R1)      
  0xb8e28               ebffffc5                BL main.print32(SB)     
  0xb8e2c               e49df008                RET #8 

What did you see instead?

main.b:
   b8e18:       08 e0 2d e5     str     lr, [sp, #-8]!
   b8e1c:       01 00 a0 e3     mov     r0, #1
   b8e20:       04 10 a0 e3     mov     r1, #4
   b8e24:       00 00 8d e5     str     r0, [sp]
   b8e28:       c5 ff ff eb     bl      #-236 <main.print32>
   b8e2c:       08 f0 9d e4     ldr     pc, [sp], #8

and

TEXT main.b(SB) 
  0xb8e18               e52de008                MOVW.W R14, -0x8(R13)   
  0xb8e1c               e3a00001                MOVW $1, R0             
  0xb8e20               e3a01004                MOVW $4, R1             
  0xb8e24               e58d0000                MOVW R0, (R13)          
  0xb8e28               ebffffc5                BL main.print32(SB)     
  0xb8e2c               e49df008                RET #8 

don't know why the code of register offset was lost?

MOVW    R0, (R13)(R1)

change into

MOVW    R0, (R13)

Activity

  1. cherrymui commented on Dec 23, 2019

    @cherrymui
    Member

    Try MOVW R0, R1<<0(R13).

    I'm sorry the syntax is different. We should handle (R1)(R13) syntax as well.

  2. added this to the Go1.15 milestone on Dec 23, 2019
  3. ClarkGuan commented on Dec 23, 2019

    @ClarkGuan
    Author

    Glad to hear from you!Yes. I've found that it can generate the same machine code.

    But I also found some other problems, like:

    WORD    $0b1110_0111_1011_0001_0000_0000_0000_0010
    WORD    $0b1110_0111_1011_0001_0000_0000_1110_0010
    

    using go tool objdump can output:

    MOVW.W (R1)(R2), R0
    MOVW.W (R1)(R2@>1), R0
    

    If I write this code in my source file, I will get some errors, like

    register extension not supported on this architecture
    

    I don't know how many more problems like this...

  4. cherrymui commented on Dec 23, 2019

    @cherrymui
    Member

    Try

    MOVW.W	R2<<0(R1), R0
    MOVW.W	R2@>1(R1), R0
    

    It is unfortunate that the objdump output syntax doesn't match the input. We could fix objdump.

  5. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Dec 23, 2019
  6. ClarkGuan commented on Dec 24, 2019

    @ClarkGuan
    Author

    It is unfortunate that the objdump output syntax doesn't match the input. We could fix objdump.

    There is no very specific document to describe the corresponding relationship between go assembly and arm assembly, so objdump maybe the only way to understand the above differences.

    If the output of objdump is not standard and correct, it will be a problem indeed.

  7. modified the milestones: Go1.15, Backlog on Jun 16, 2020
  8. ianlancetaylor commented on Jun 16, 2020

    @ianlancetaylor
    Contributor

    Moving to backlog milestone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.help wanted

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions