Home | History | Annotate | Download | only in ld
      1 // Do not edit. Bootstrap copy of /Volumes/Android/buildbot/src/android/build-tools/out/obj/go/src/cmd/link/internal/ld/util.go
      2 
      3 //line /Volumes/Android/buildbot/src/android/build-tools/out/obj/go/src/cmd/link/internal/ld/util.go:1
      4 // Copyright 2015 The Go Authors.  All rights reserved.
      5 // Use of this source code is governed by a BSD-style
      6 // license that can be found in the LICENSE file.
      7 
      8 package ld
      9 
     10 import (
     11 	"bytes"
     12 	"encoding/binary"
     13 	"log"
     14 	"os"
     15 	"runtime"
     16 	"runtime/pprof"
     17 	"strings"
     18 	"time"
     19 )
     20 
     21 func cstring(x []byte) string {
     22 	i := bytes.IndexByte(x, '\x00')
     23 	if i >= 0 {
     24 		x = x[:i]
     25 	}
     26 	return string(x)
     27 }
     28 
     29 func tokenize(s string) []string {
     30 	var f []string
     31 	for {
     32 		s = strings.TrimLeft(s, " \t\r\n")
     33 		if s == "" {
     34 			break
     35 		}
     36 		quote := false
     37 		i := 0
     38 		for ; i < len(s); i++ {
     39 			if s[i] == '\'' {
     40 				if quote && i+1 < len(s) && s[i+1] == '\'' {
     41 					i++
     42 					continue
     43 				}
     44 				quote = !quote
     45 			}
     46 			if !quote && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n') {
     47 				break
     48 			}
     49 		}
     50 		next := s[:i]
     51 		s = s[i:]
     52 		if strings.Contains(next, "'") {
     53 			var buf []byte
     54 			quote := false
     55 			for i := 0; i < len(next); i++ {
     56 				if next[i] == '\'' {
     57 					if quote && i+1 < len(next) && next[i+1] == '\'' {
     58 						i++
     59 						buf = append(buf, '\'')
     60 					}
     61 					quote = !quote
     62 					continue
     63 				}
     64 				buf = append(buf, next[i])
     65 			}
     66 			next = string(buf)
     67 		}
     68 		f = append(f, next)
     69 	}
     70 	return f
     71 }
     72 
     73 func cutStringAtNUL(s string) string {
     74 	if i := strings.Index(s, "\x00"); i >= 0 {
     75 		s = s[:i]
     76 	}
     77 	return s
     78 }
     79 
     80 func Access(name string, mode int) int {
     81 	if mode != 0 {
     82 		panic("bad access")
     83 	}
     84 	_, err := os.Stat(name)
     85 	if err != nil {
     86 		return -1
     87 	}
     88 	return 0
     89 }
     90 
     91 // strings.Compare, introduced in Go 1.5.
     92 func stringsCompare(a, b string) int {
     93 	if a == b {
     94 		return 0
     95 	}
     96 	if a < b {
     97 		return -1
     98 	}
     99 	return +1
    100 }
    101 
    102 var atExitFuncs []func()
    103 
    104 func AtExit(f func()) {
    105 	atExitFuncs = append(atExitFuncs, f)
    106 }
    107 
    108 func Exit(code int) {
    109 	for i := len(atExitFuncs) - 1; i >= 0; i-- {
    110 		f := atExitFuncs[i]
    111 		atExitFuncs = atExitFuncs[:i]
    112 		f()
    113 	}
    114 	os.Exit(code)
    115 }
    116 
    117 var (
    118 	cpuprofile     string
    119 	memprofile     string
    120 	memprofilerate int64
    121 )
    122 
    123 func startProfile() {
    124 	if cpuprofile != "" {
    125 		f, err := os.Create(cpuprofile)
    126 		if err != nil {
    127 			log.Fatalf("%v", err)
    128 		}
    129 		if err := pprof.StartCPUProfile(f); err != nil {
    130 			log.Fatalf("%v", err)
    131 		}
    132 		AtExit(pprof.StopCPUProfile)
    133 	}
    134 	if memprofile != "" {
    135 		if memprofilerate != 0 {
    136 			runtime.MemProfileRate = int(memprofilerate)
    137 		}
    138 		f, err := os.Create(memprofile)
    139 		if err != nil {
    140 			log.Fatalf("%v", err)
    141 		}
    142 		AtExit(func() {
    143 			runtime.GC() // profile all outstanding allocations
    144 			if err := pprof.WriteHeapProfile(f); err != nil {
    145 				log.Fatalf("%v", err)
    146 			}
    147 		})
    148 	}
    149 }
    150 
    151 func artrim(x []byte) string {
    152 	i := 0
    153 	j := len(x)
    154 	for i < len(x) && x[i] == ' ' {
    155 		i++
    156 	}
    157 	for j > i && x[j-1] == ' ' {
    158 		j--
    159 	}
    160 	return string(x[i:j])
    161 }
    162 
    163 func stringtouint32(x []uint32, s string) {
    164 	for i := 0; len(s) > 0; i++ {
    165 		var buf [4]byte
    166 		s = s[copy(buf[:], s):]
    167 		x[i] = binary.LittleEndian.Uint32(buf[:])
    168 	}
    169 }
    170 
    171 var start = time.Now()
    172 
    173 func elapsed() float64 {
    174 	return time.Since(start).Seconds()
    175 }
    176