testOperandParser(t, parser, s390xOperandTests)
}
+func TestFuncAddress(t *testing.T) {
+ type subtest struct {
+ arch string
+ tests []operandTest
+ }
+ for _, sub := range []subtest{
+ {"amd64", amd64OperandTests},
+ {"386", x86OperandTests},
+ {"arm", armOperandTests},
+ {"arm64", arm64OperandTests},
+ {"ppc64", ppc64OperandTests},
+ {"mips", mipsOperandTests},
+ {"mips64", mips64OperandTests},
+ {"s390x", s390xOperandTests},
+ } {
+ t.Run(sub.arch, func(t *testing.T) {
+ parser := newParser(sub.arch)
+ for _, test := range sub.tests {
+ parser.start(lex.Tokenize(test.input))
+ name, ok := parser.funcAddress()
+
+ isFuncSym := strings.HasSuffix(test.input, "(SB)") &&
+ // Ignore static symbols.
+ !strings.Contains(test.input, "<>") &&
+ // Ignore symbols with offsets.
+ !strings.Contains(test.input, "+")
+
+ wantName := ""
+ if isFuncSym {
+ // Strip $|* and (SB).
+ wantName = test.output[:len(test.output)-4]
+ if strings.HasPrefix(wantName, "$") || strings.HasPrefix(wantName, "*") {
+ wantName = wantName[1:]
+ }
+ }
+ if ok != isFuncSym || name != wantName {
+ t.Errorf("fail at %s as function address: got %s, %v; expected %s, %v", test.input, name, ok, wantName, isFuncSym)
+ }
+ }
+ })
+ }
+}
+
type operandTest struct {
input, output string
}
return p.firstProg, true
}
+// ParseSymABIs parses p's assembly code to find text symbol
+// definitions and references and writes a symabis file to w.
+func (p *Parser) ParseSymABIs(w io.Writer) bool {
+ operands := make([][]lex.Token, 0, 3)
+ for {
+ word, _, operands1, ok := p.line(operands)
+ if !ok {
+ break
+ }
+ operands = operands1
+
+ p.symDefRef(w, word, operands)
+ }
+ return p.errorCount == 0
+}
+
// line consumes a single assembly line from p.lex of the form
//
// {label:} WORD[.cond] [ arg {, arg} ] (';' | '\n')
return true
}
+// symDefRef scans a line for potential text symbol definitions and
+// references and writes symabis information to w.
+//
+// The symabis format is documented at
+// cmd/compile/internal/gc.readSymABIs.
+func (p *Parser) symDefRef(w io.Writer, word string, operands [][]lex.Token) {
+ switch word {
+ case "TEXT":
+ // Defines text symbol in operands[0].
+ if len(operands) > 0 {
+ p.start(operands[0])
+ if name, ok := p.funcAddress(); ok {
+ fmt.Fprintf(w, "def %s ABI0\n", name)
+ }
+ }
+ return
+ case "GLOBL", "PCDATA":
+ // No text definitions or symbol references.
+ case "DATA", "FUNCDATA":
+ // For DATA, operands[0] is defined symbol.
+ // For FUNCDATA, operands[0] is an immediate constant.
+ // Remaining operands may have references.
+ if len(operands) < 2 {
+ return
+ }
+ operands = operands[1:]
+ }
+ // Search for symbol references.
+ for _, op := range operands {
+ p.start(op)
+ if name, ok := p.funcAddress(); ok {
+ fmt.Fprintf(w, "ref %s ABI0\n", name)
+ }
+ }
+}
+
func (p *Parser) start(operand []lex.Token) {
p.input = operand
p.inputPos = 0
}
}
+// funcAddress parses an external function address. This is a
+// constrained form of the operand syntax that's always SB-based,
+// non-static, and has no additional offsets:
+//
+// [$|*]sym(SB)
+func (p *Parser) funcAddress() (string, bool) {
+ switch p.peek() {
+ case '$', '*':
+ // Skip prefix.
+ p.next()
+ }
+
+ tok := p.next()
+ name := tok.String()
+ if tok.ScanToken != scanner.Ident || p.atStartOfRegister(name) {
+ return "", false
+ }
+ if p.next().ScanToken != '(' {
+ return "", false
+ }
+ if reg := p.next(); reg.ScanToken != scanner.Ident || reg.String() != "SB" {
+ return "", false
+ }
+ if p.next().ScanToken != ')' || p.peek() != scanner.EOF {
+ return "", false
+ }
+ return name, true
+}
+
// registerIndirect parses the general form of a register indirection.
// It is can be (R1), (R2*scale), (R1)(R2*scale), (R1)(R2.SXTX<<3) or (R1)(R2<<3)
// where R1 may be a simple register or register pair R:R or (R, R) or (R+R).
Shared = flag.Bool("shared", false, "generate code that can be linked into a shared library")
Dynlink = flag.Bool("dynlink", false, "support references to Go symbols defined in other shared libraries")
AllErrors = flag.Bool("e", false, "no limit on number of errors reported")
+ SymABIs = flag.Bool("symabis", false, "write symbol ABI information to output file, don't assemble")
)
var (
defer bio.MustClose(out)
buf := bufio.NewWriter(bio.MustWriter(out))
- fmt.Fprintf(buf, "go object %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version)
- fmt.Fprintf(buf, "!\n")
+ if !*flags.SymABIs {
+ fmt.Fprintf(buf, "go object %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version)
+ fmt.Fprintf(buf, "!\n")
+ }
var ok, diag bool
var failedFile string
diag = true
log.Printf(format, args...)
}
- pList := new(obj.Plist)
- pList.Firstpc, ok = parser.Parse()
+ if *flags.SymABIs {
+ ok = parser.ParseSymABIs(buf)
+ } else {
+ pList := new(obj.Plist)
+ pList.Firstpc, ok = parser.Parse()
+ // reports errors to parser.Errorf
+ if ok {
+ obj.Flushplist(ctxt, pList, nil, "")
+ }
+ }
if !ok {
failedFile = f
break
}
- // reports errors to parser.Errorf
- obj.Flushplist(ctxt, pList, nil, "")
}
- if ok {
+ if ok && !*flags.SymABIs {
obj.WriteObjFile(ctxt, buf)
}
if !ok || diag {