From 026b98a2a230ef378e8f802f8671f421c127e258 Mon Sep 17 00:00:00 2001 From: Than McIntosh Date: Wed, 20 Nov 2019 11:06:39 -0500 Subject: [PATCH] [dev.link] cmd/link: add Errorf method on context taking new sym 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 --- src/cmd/link/internal/ld/util.go | 37 ++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/cmd/link/internal/ld/util.go b/src/cmd/link/internal/ld/util.go index 9d236db766..9f257b8fc0 100644 --- a/src/cmd/link/internal/ld/util.go +++ b/src/cmd/link/internal/ld/util.go @@ -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 { -- 2.48.1