]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: add Errorf method on context taking new sym
authorThan McIntosh <thanm@google.com>
Wed, 20 Nov 2019 16:06:39 +0000 (11:06 -0500)
committerThan McIntosh <thanm@google.com>
Fri, 27 Dec 2019 15:05:58 +0000 (15:05 +0000)
Add a Link method that takes a loader sym index instead
of a sym.Symbol.

Change-Id: If59e246cb1adc9066080f019be135387fc1b2fcd
Reviewed-on: https://go-review.googlesource.com/c/go/+/208228
Reviewed-by: Cherry Zhang <cherryyz@google.com>
src/cmd/link/internal/ld/util.go

index 9d236db76688d6e6db5cc55ff086c830793f73cc..9f257b8fc0ab03e99610dca575ac865a3f83d61a 100644 (file)
@@ -5,6 +5,7 @@
 package ld
 
 import (
+       "cmd/link/internal/loader"
        "cmd/link/internal/sym"
        "encoding/binary"
        "fmt"
@@ -38,6 +39,18 @@ func Exitf(format string, a ...interface{}) {
        Exit(2)
 }
 
+// afterErrorAction updates 'nerrors' on error and invokes exit or
+// panics in the proper circumstances.
+func afterErrorAction() {
+       nerrors++
+       if *flagH {
+               panic("error")
+       }
+       if nerrors > 20 {
+               Exitf("too many errors")
+       }
+}
+
 // Errorf logs an error message.
 //
 // If more than 20 errors have been printed, exit with an error.
@@ -50,13 +63,25 @@ func Errorf(s *sym.Symbol, format string, args ...interface{}) {
        }
        format += "\n"
        fmt.Fprintf(os.Stderr, format, args...)
-       nerrors++
-       if *flagH {
-               panic("error")
-       }
-       if nerrors > 20 {
-               Exitf("too many errors")
+       afterErrorAction()
+}
+
+// Errorf method logs an error message.
+//
+// If more than 20 errors have been printed, exit with an error.
+//
+// Logging an error means that on exit cmd/link will delete any
+// output file and return a non-zero error code.
+func (ctxt *Link) Errorf(s loader.Sym, format string, args ...interface{}) {
+       if s != 0 && ctxt.loader != nil {
+               sn := ctxt.loader.SymName(s)
+               format = sn + ": " + format
+       } else {
+               format = fmt.Sprintf("sym %d: %s", s, format)
        }
+       format += "\n"
+       fmt.Fprintf(os.Stderr, format, args...)
+       afterErrorAction()
 }
 
 func artrim(x []byte) string {