Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/link/internal/ld/util.go

Documentation: cmd/link/internal/ld

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"cmd/link/internal/loader"
     9  	"encoding/binary"
    10  	"fmt"
    11  	"os"
    12  )
    13  
    14  var atExitFuncs []func()
    15  
    16  func AtExit(f func()) {
    17  	atExitFuncs = append(atExitFuncs, f)
    18  }
    19  
    20  // runAtExitFuncs runs the queued set of AtExit functions.
    21  func runAtExitFuncs() {
    22  	for i := len(atExitFuncs) - 1; i >= 0; i-- {
    23  		atExitFuncs[i]()
    24  	}
    25  	atExitFuncs = nil
    26  }
    27  
    28  // Exit exits with code after executing all atExitFuncs.
    29  func Exit(code int) {
    30  	runAtExitFuncs()
    31  	os.Exit(code)
    32  }
    33  
    34  // Exitf logs an error message then calls Exit(2).
    35  func Exitf(format string, a ...interface{}) {
    36  	fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...)
    37  	nerrors++
    38  	Exit(2)
    39  }
    40  
    41  // afterErrorAction updates 'nerrors' on error and invokes exit or
    42  // panics in the proper circumstances.
    43  func afterErrorAction() {
    44  	nerrors++
    45  	if *flagH {
    46  		panic("error")
    47  	}
    48  	if nerrors > 20 {
    49  		Exitf("too many errors")
    50  	}
    51  }
    52  
    53  // Errorf logs an error message.
    54  //
    55  // If more than 20 errors have been printed, exit with an error.
    56  //
    57  // Logging an error means that on exit cmd/link will delete any
    58  // output file and return a non-zero error code.
    59  //
    60  // TODO: remove. Use ctxt.Errorf instead.
    61  // All remaining calls use nil as first arg.
    62  func Errorf(dummy *int, format string, args ...interface{}) {
    63  	format += "\n"
    64  	fmt.Fprintf(os.Stderr, format, args...)
    65  	afterErrorAction()
    66  }
    67  
    68  // Errorf method logs an error message.
    69  //
    70  // If more than 20 errors have been printed, exit with an error.
    71  //
    72  // Logging an error means that on exit cmd/link will delete any
    73  // output file and return a non-zero error code.
    74  func (ctxt *Link) Errorf(s loader.Sym, format string, args ...interface{}) {
    75  	if ctxt.loader != nil {
    76  		ctxt.loader.Errorf(s, format, args...)
    77  		return
    78  	}
    79  	// Note: this is not expected to happen very often.
    80  	format = fmt.Sprintf("sym %d: %s", s, format)
    81  	format += "\n"
    82  	fmt.Fprintf(os.Stderr, format, args...)
    83  	afterErrorAction()
    84  }
    85  
    86  func artrim(x []byte) string {
    87  	i := 0
    88  	j := len(x)
    89  	for i < len(x) && x[i] == ' ' {
    90  		i++
    91  	}
    92  	for j > i && x[j-1] == ' ' {
    93  		j--
    94  	}
    95  	return string(x[i:j])
    96  }
    97  
    98  func stringtouint32(x []uint32, s string) {
    99  	for i := 0; len(s) > 0; i++ {
   100  		var buf [4]byte
   101  		s = s[copy(buf[:], s):]
   102  		x[i] = binary.LittleEndian.Uint32(buf[:])
   103  	}
   104  }
   105  
   106  // contains reports whether v is in s.
   107  func contains(s []string, v string) bool {
   108  	for _, x := range s {
   109  		if x == v {
   110  			return true
   111  		}
   112  	}
   113  	return false
   114  }
   115  

View as plain text