mirror of
https://github.com/golang/go.git
synced 2025-05-21 07:13:27 +00:00
Begin passing coutbuf by as a parameter. To make the initial plumbing pass easier, it is also a field in the standard ctxt parameter. Consolidate the byte writing functions into the OutBuf object. The result is less architecture-dependent initialization. To avoid plumbing out everywhere we want to report an error, move handling of out file deletion to an AtExit function. For #22095 Change-Id: I0863695241562e0662ae3669666c7922b8c846f9 Reviewed-on: https://go-review.googlesource.com/67318 Run-TryBot: David Crawshaw <crawshaw@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
145 lines
2.6 KiB
Go
145 lines
2.6 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ld
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var startTime time.Time
|
|
|
|
// TODO(josharian): delete. See issue 19865.
|
|
func Cputime() float64 {
|
|
if startTime.IsZero() {
|
|
startTime = time.Now()
|
|
}
|
|
return time.Since(startTime).Seconds()
|
|
}
|
|
|
|
func cstring(x []byte) string {
|
|
i := bytes.IndexByte(x, '\x00')
|
|
if i >= 0 {
|
|
x = x[:i]
|
|
}
|
|
return string(x)
|
|
}
|
|
|
|
func tokenize(s string) []string {
|
|
var f []string
|
|
for {
|
|
s = strings.TrimLeft(s, " \t\r\n")
|
|
if s == "" {
|
|
break
|
|
}
|
|
quote := false
|
|
i := 0
|
|
for ; i < len(s); i++ {
|
|
if s[i] == '\'' {
|
|
if quote && i+1 < len(s) && s[i+1] == '\'' {
|
|
i++
|
|
continue
|
|
}
|
|
quote = !quote
|
|
}
|
|
if !quote && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n') {
|
|
break
|
|
}
|
|
}
|
|
next := s[:i]
|
|
s = s[i:]
|
|
if strings.Contains(next, "'") {
|
|
var buf []byte
|
|
quote := false
|
|
for i := 0; i < len(next); i++ {
|
|
if next[i] == '\'' {
|
|
if quote && i+1 < len(next) && next[i+1] == '\'' {
|
|
i++
|
|
buf = append(buf, '\'')
|
|
}
|
|
quote = !quote
|
|
continue
|
|
}
|
|
buf = append(buf, next[i])
|
|
}
|
|
next = string(buf)
|
|
}
|
|
f = append(f, next)
|
|
}
|
|
return f
|
|
}
|
|
|
|
var atExitFuncs []func()
|
|
|
|
func AtExit(f func()) {
|
|
atExitFuncs = append(atExitFuncs, f)
|
|
}
|
|
|
|
// Exit exits with code after executing all atExitFuncs.
|
|
func Exit(code int) {
|
|
for i := len(atExitFuncs) - 1; i >= 0; i-- {
|
|
atExitFuncs[i]()
|
|
}
|
|
os.Exit(code)
|
|
}
|
|
|
|
// Exitf logs an error message then calls Exit(2).
|
|
func Exitf(format string, a ...interface{}) {
|
|
fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...)
|
|
nerrors++
|
|
Exit(2)
|
|
}
|
|
|
|
// Errorf 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 Errorf(s *Symbol, format string, args ...interface{}) {
|
|
if s != nil {
|
|
format = s.Name + ": " + format
|
|
}
|
|
format += "\n"
|
|
fmt.Fprintf(os.Stderr, format, args...)
|
|
nerrors++
|
|
if *flagH {
|
|
panic("error")
|
|
}
|
|
if nerrors > 20 {
|
|
Exitf("too many errors")
|
|
}
|
|
}
|
|
|
|
func artrim(x []byte) string {
|
|
i := 0
|
|
j := len(x)
|
|
for i < len(x) && x[i] == ' ' {
|
|
i++
|
|
}
|
|
for j > i && x[j-1] == ' ' {
|
|
j--
|
|
}
|
|
return string(x[i:j])
|
|
}
|
|
|
|
func stringtouint32(x []uint32, s string) {
|
|
for i := 0; len(s) > 0; i++ {
|
|
var buf [4]byte
|
|
s = s[copy(buf[:], s):]
|
|
x[i] = binary.LittleEndian.Uint32(buf[:])
|
|
}
|
|
}
|
|
|
|
var start = time.Now()
|
|
|
|
func elapsed() float64 {
|
|
return time.Since(start).Seconds()
|
|
}
|