1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:14:39 -07:00

cmd/internal/ld: use a simpler cout writer

Removes the unused *bufio.Reader from the object controlling the
linker's primary output.

Change-Id: If91d9f60752f3dc4b280f35d6eb441f3c47574b2
Reviewed-on: https://go-review.googlesource.com/9362
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
David Crawshaw 2015-04-29 10:59:22 -04:00
parent e2b6cebcd6
commit ea426dcae1
3 changed files with 40 additions and 20 deletions

View File

@ -1034,7 +1034,7 @@ func elfinterp(sh *ElfShdr, startva uint64, resoff uint64, p string) int {
func elfwriteinterp() int { func elfwriteinterp() int {
sh := elfshname(".interp") sh := elfshname(".interp")
Cseek(int64(sh.off)) Cseek(int64(sh.off))
coutbuf.w.WriteString(interp) coutbuf.WriteString(interp)
Cput(0) Cput(0)
return int(sh.size) return int(sh.size)
} }

View File

@ -31,6 +31,7 @@
package ld package ld
import ( import (
"bufio"
"bytes" "bytes"
"cmd/internal/obj" "cmd/internal/obj"
"debug/elf" "debug/elf"
@ -230,9 +231,13 @@ var (
headstring string headstring string
// buffered output // buffered output
Bso Biobuf Bso Biobuf
coutbuf Biobuf
) )
var coutbuf struct {
*bufio.Writer
f *os.File
}
const ( const (
// Whether to assume that the external linker is "gold" // Whether to assume that the external linker is "gold"
// (http://sourceware.org/ml/binutils/2008-03/msg00162.html). // (http://sourceware.org/ml/binutils/2008-03/msg00162.html).
@ -245,7 +250,6 @@ const (
) )
var ( var (
cout *os.File
// Set if we see an object compiled by the host compiler that is not // Set if we see an object compiled by the host compiler that is not
// from a package that is known to support internal linking mode. // from a package that is known to support internal linking mode.
externalobj = false externalobj = false
@ -359,8 +363,8 @@ func libinit() {
Exitf("cannot create %s: %v", outfile, err) Exitf("cannot create %s: %v", outfile, err)
} }
cout = f coutbuf.Writer = bufio.NewWriter(f)
coutbuf = *Binitw(f) coutbuf.f = f
if INITENTRY == "" { if INITENTRY == "" {
switch Buildmode { switch Buildmode {
@ -382,21 +386,26 @@ func libinit() {
func Exitf(format string, a ...interface{}) { func Exitf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...) fmt.Fprintf(os.Stderr, os.Args[0]+": "+format+"\n", a...)
if cout != nil { if coutbuf.f != nil {
cout.Close() coutbuf.f.Close()
mayberemoveoutfile() mayberemoveoutfile()
} }
Exit(2) Exit(2)
} }
func errorexit() { func errorexit() {
if cout != nil { if coutbuf.f != nil {
if nerrors != 0 {
Cflush()
}
// For rmtemp run at atexit time on Windows. // For rmtemp run at atexit time on Windows.
cout.Close() if err := coutbuf.f.Close(); err != nil {
Exitf("close: %v", err)
}
} }
if nerrors != 0 { if nerrors != 0 {
if cout != nil { if coutbuf.f != nil {
mayberemoveoutfile() mayberemoveoutfile()
} }
Exit(2) Exit(2)
@ -803,16 +812,17 @@ func hostlinksetup() {
} }
// change our output to temporary object file // change our output to temporary object file
cout.Close() coutbuf.f.Close()
p := fmt.Sprintf("%s/go.o", tmpdir) p := fmt.Sprintf("%s/go.o", tmpdir)
var err error var err error
cout, err = os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0775) f, err := os.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0775)
if err != nil { if err != nil {
Exitf("cannot create %s: %v", p, err) Exitf("cannot create %s: %v", p, err)
} }
coutbuf = *Binitw(cout) coutbuf.Writer = bufio.NewWriter(f)
coutbuf.f = f
} }
// hostobjCopy creates a copy of the object files in hostobj in a // hostobjCopy creates a copy of the object files in hostobj in a
@ -1555,23 +1565,33 @@ func Yconv(s *LSym) string {
} }
func Cflush() { func Cflush() {
Bflush(&coutbuf) if err := coutbuf.Writer.Flush(); err != nil {
Exitf("flushing %s: %v", coutbuf.f.Name(), err)
}
} }
func Cpos() int64 { func Cpos() int64 {
return Boffset(&coutbuf) Cflush()
off, err := coutbuf.f.Seek(0, 1)
if err != nil {
Exitf("seeking in output [0, 1]: %v", err)
}
return off
} }
func Cseek(p int64) { func Cseek(p int64) {
Bseek(&coutbuf, p, 0) Cflush()
if _, err := coutbuf.f.Seek(p, 0); err != nil {
Exitf("seeking in output [0, 1]: %v", err)
}
} }
func Cwrite(p []byte) { func Cwrite(p []byte) {
Bwrite(&coutbuf, p) coutbuf.Write(p)
} }
func Cput(c uint8) { func Cput(c uint8) {
Bputc(&coutbuf, c) coutbuf.WriteByte(c)
} }
func usage() { func usage() {

View File

@ -474,7 +474,7 @@ func pewrite() {
} }
func strput(s string) { func strput(s string) {
coutbuf.w.WriteString(s) coutbuf.WriteString(s)
Cput(0) Cput(0)
// string must be padded to even size // string must be padded to even size
if (len(s)+1)%2 != 0 { if (len(s)+1)%2 != 0 {