Commit 4b7e36cd authored by Matthew Dempsky's avatar Matthew Dempsky

cmd: extract obj's Biobuf code into new bio package

API could still be made more Go-ey.

Updates #15165.

Change-Id: I514ffceffa43c293ae5d7e5f1e9193fda0098865
Reviewed-on: https://go-review.googlesource.com/21644Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent b17b9530
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
"testing" "testing"
"cmd/asm/internal/lex" "cmd/asm/internal/lex"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
) )
...@@ -33,7 +34,7 @@ func testEndToEnd(t *testing.T, goarch, file string) { ...@@ -33,7 +34,7 @@ func testEndToEnd(t *testing.T, goarch, file string) {
pList := obj.Linknewplist(ctxt) pList := obj.Linknewplist(ctxt)
var ok bool var ok bool
testOut = new(bytes.Buffer) // The assembler writes test output to this buffer. testOut = new(bytes.Buffer) // The assembler writes test output to this buffer.
ctxt.Bso = obj.Binitw(os.Stdout) ctxt.Bso = bio.BufWriter(os.Stdout)
defer ctxt.Bso.Flush() defer ctxt.Bso.Flush()
failed := false failed := false
ctxt.DiagFunc = func(format string, args ...interface{}) { ctxt.DiagFunc = func(format string, args ...interface{}) {
...@@ -271,7 +272,7 @@ func testErrors(t *testing.T, goarch, file string) { ...@@ -271,7 +272,7 @@ func testErrors(t *testing.T, goarch, file string) {
pList := obj.Linknewplist(ctxt) pList := obj.Linknewplist(ctxt)
var ok bool var ok bool
testOut = new(bytes.Buffer) // The assembler writes test output to this buffer. testOut = new(bytes.Buffer) // The assembler writes test output to this buffer.
ctxt.Bso = obj.Binitw(os.Stdout) ctxt.Bso = bio.BufWriter(os.Stdout)
defer ctxt.Bso.Flush() defer ctxt.Bso.Flush()
failed := false failed := false
var errBuf bytes.Buffer var errBuf bytes.Buffer
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"cmd/asm/internal/flags" "cmd/asm/internal/flags"
"cmd/asm/internal/lex" "cmd/asm/internal/lex"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
) )
...@@ -45,9 +46,9 @@ func main() { ...@@ -45,9 +46,9 @@ func main() {
if *flags.Shared || *flags.Dynlink { if *flags.Shared || *flags.Dynlink {
ctxt.Flag_shared = 1 ctxt.Flag_shared = 1
} }
ctxt.Bso = obj.Binitw(os.Stdout) ctxt.Bso = bio.BufWriter(os.Stdout)
defer ctxt.Bso.Flush() defer ctxt.Bso.Flush()
output := obj.Binitw(fd) output := bio.BufWriter(fd)
fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion()) fmt.Fprintf(output, "go object %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion())
fmt.Fprintf(output, "!\n") fmt.Fprintf(output, "!\n")
......
...@@ -92,7 +92,7 @@ package gc ...@@ -92,7 +92,7 @@ package gc
import ( import (
"bytes" "bytes"
"cmd/compile/internal/big" "cmd/compile/internal/big"
"cmd/internal/obj" "cmd/internal/bio"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"sort" "sort"
...@@ -124,7 +124,7 @@ const exportVersion = "v0" ...@@ -124,7 +124,7 @@ const exportVersion = "v0"
const exportInlined = true // default: true const exportInlined = true // default: true
type exporter struct { type exporter struct {
out *obj.Biobuf out *bio.Buf
pkgIndex map[*Pkg]int pkgIndex map[*Pkg]int
typIndex map[*Type]int typIndex map[*Type]int
inlined []*Func inlined []*Func
...@@ -136,7 +136,7 @@ type exporter struct { ...@@ -136,7 +136,7 @@ type exporter struct {
} }
// Export writes the exportlist for localpkg to out and returns the number of bytes written. // Export writes the exportlist for localpkg to out and returns the number of bytes written.
func Export(out *obj.Biobuf, trace bool) int { func Export(out *bio.Buf, trace bool) int {
p := exporter{ p := exporter{
out: out, out: out,
pkgIndex: make(map[*Pkg]int), pkgIndex: make(map[*Pkg]int),
...@@ -1531,10 +1531,10 @@ func (p *exporter) byte(b byte) { ...@@ -1531,10 +1531,10 @@ func (p *exporter) byte(b byte) {
fallthrough fallthrough
case '|': case '|':
// write '|' as '|' '|' // write '|' as '|' '|'
obj.Bputc(p.out, '|') p.out.WriteByte('|')
p.written++ p.written++
} }
obj.Bputc(p.out, b) p.out.WriteByte(b)
p.written++ p.written++
} }
......
...@@ -7,7 +7,7 @@ package gc ...@@ -7,7 +7,7 @@ package gc
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmd/internal/obj" "cmd/internal/bio"
"fmt" "fmt"
"sort" "sort"
"unicode" "unicode"
...@@ -384,7 +384,7 @@ func dumpexport() { ...@@ -384,7 +384,7 @@ func dumpexport() {
if debugFormat { if debugFormat {
// save a copy of the export data // save a copy of the export data
var copy bytes.Buffer var copy bytes.Buffer
bcopy := obj.Binitw(&copy) bcopy := bio.BufWriter(&copy)
size = Export(bcopy, Debug_export != 0) size = Export(bcopy, Debug_export != 0)
bcopy.Flush() // flushing to bytes.Buffer cannot fail bcopy.Flush() // flushing to bytes.Buffer cannot fail
if n, err := bout.Write(copy.Bytes()); n != size || err != nil { if n, err := bout.Write(copy.Bytes()); n != size || err != nil {
...@@ -577,7 +577,7 @@ func importtype(pt *Type, t *Type) { ...@@ -577,7 +577,7 @@ func importtype(pt *Type, t *Type) {
} }
func dumpasmhdr() { func dumpasmhdr() {
b, err := obj.Bopenw(asmhdr) b, err := bio.Create(asmhdr)
if err != nil { if err != nil {
Fatalf("%v", err) Fatalf("%v", err)
} }
...@@ -604,5 +604,5 @@ func dumpasmhdr() { ...@@ -604,5 +604,5 @@ func dumpasmhdr() {
} }
} }
obj.Bterm(b) b.Close()
} }
...@@ -6,6 +6,7 @@ package gc ...@@ -6,6 +6,7 @@ package gc
import ( import (
"cmd/compile/internal/ssa" "cmd/compile/internal/ssa"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
) )
...@@ -132,7 +133,7 @@ var infile string ...@@ -132,7 +133,7 @@ var infile string
var outfile string var outfile string
var bout *obj.Biobuf var bout *bio.Buf
var nerrors int var nerrors int
...@@ -287,7 +288,7 @@ var Ctxt *obj.Link ...@@ -287,7 +288,7 @@ var Ctxt *obj.Link
var writearchive int var writearchive int
var bstdout obj.Biobuf var bstdout *bio.Buf
var Nacl bool var Nacl bool
......
...@@ -9,6 +9,7 @@ package gc ...@@ -9,6 +9,7 @@ package gc
import ( import (
"bufio" "bufio"
"cmd/compile/internal/ssa" "cmd/compile/internal/ssa"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"flag" "flag"
...@@ -97,8 +98,8 @@ func Main() { ...@@ -97,8 +98,8 @@ func Main() {
Ctxt = obj.Linknew(Thearch.LinkArch) Ctxt = obj.Linknew(Thearch.LinkArch)
Ctxt.DiagFunc = Yyerror Ctxt.DiagFunc = Yyerror
Ctxt.Bso = &bstdout bstdout = bio.BufWriter(os.Stdout)
bstdout = *obj.Binitw(os.Stdout) Ctxt.Bso = bstdout
localpkg = mkpkg("") localpkg = mkpkg("")
localpkg.Prefix = "\"\"" localpkg.Prefix = "\"\""
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package gc package gc
import ( import (
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
...@@ -23,7 +24,7 @@ func formathdr(arhdr []byte, name string, size int64) { ...@@ -23,7 +24,7 @@ func formathdr(arhdr []byte, name string, size int64) {
func dumpobj() { func dumpobj() {
var err error var err error
bout, err = obj.Bopenw(outfile) bout, err = bio.Create(outfile)
if err != nil { if err != nil {
Flusherrors() Flusherrors()
fmt.Printf("can't create %s: %v\n", outfile, err) fmt.Printf("can't create %s: %v\n", outfile, err)
...@@ -33,10 +34,10 @@ func dumpobj() { ...@@ -33,10 +34,10 @@ func dumpobj() {
startobj := int64(0) startobj := int64(0)
var arhdr [ArhdrSize]byte var arhdr [ArhdrSize]byte
if writearchive != 0 { if writearchive != 0 {
obj.Bwritestring(bout, "!<arch>\n") bout.WriteString("!<arch>\n")
arhdr = [ArhdrSize]byte{} arhdr = [ArhdrSize]byte{}
bout.Write(arhdr[:]) bout.Write(arhdr[:])
startobj = obj.Boffset(bout) startobj = bio.Boffset(bout)
} }
fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion(), obj.Expstring()) fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion(), obj.Expstring())
...@@ -44,19 +45,19 @@ func dumpobj() { ...@@ -44,19 +45,19 @@ func dumpobj() {
if writearchive != 0 { if writearchive != 0 {
bout.Flush() bout.Flush()
size := obj.Boffset(bout) - startobj size := bio.Boffset(bout) - startobj
if size&1 != 0 { if size&1 != 0 {
obj.Bputc(bout, 0) bout.WriteByte(0)
} }
obj.Bseek(bout, startobj-ArhdrSize, 0) bio.Bseek(bout, startobj-ArhdrSize, 0)
formathdr(arhdr[:], "__.PKGDEF", size) formathdr(arhdr[:], "__.PKGDEF", size)
bout.Write(arhdr[:]) bout.Write(arhdr[:])
bout.Flush() bout.Flush()
obj.Bseek(bout, startobj+size+(size&1), 0) bio.Bseek(bout, startobj+size+(size&1), 0)
arhdr = [ArhdrSize]byte{} arhdr = [ArhdrSize]byte{}
bout.Write(arhdr[:]) bout.Write(arhdr[:])
startobj = obj.Boffset(bout) startobj = bio.Boffset(bout)
fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion(), obj.Expstring()) fmt.Fprintf(bout, "go object %s %s %s %s\n", obj.Getgoos(), obj.Getgoarch(), obj.Getgoversion(), obj.Expstring())
} }
...@@ -91,16 +92,16 @@ func dumpobj() { ...@@ -91,16 +92,16 @@ func dumpobj() {
if writearchive != 0 { if writearchive != 0 {
bout.Flush() bout.Flush()
size := obj.Boffset(bout) - startobj size := bio.Boffset(bout) - startobj
if size&1 != 0 { if size&1 != 0 {
obj.Bputc(bout, 0) bout.WriteByte(0)
} }
obj.Bseek(bout, startobj-ArhdrSize, 0) bio.Bseek(bout, startobj-ArhdrSize, 0)
formathdr(arhdr[:], "_go_.o", size) formathdr(arhdr[:], "_go_.o", size)
bout.Write(arhdr[:]) bout.Write(arhdr[:])
} }
obj.Bterm(bout) bout.Close()
} }
func dumpglobls() { func dumpglobls() {
...@@ -132,9 +133,9 @@ func dumpglobls() { ...@@ -132,9 +133,9 @@ func dumpglobls() {
funcsyms = nil funcsyms = nil
} }
func Bputname(b *obj.Biobuf, s *obj.LSym) { func Bputname(b *bio.Buf, s *obj.LSym) {
obj.Bwritestring(b, s.Name) b.WriteString(s.Name)
obj.Bputc(b, 0) b.WriteByte(0)
} }
func Linksym(s *Sym) *obj.LSym { func Linksym(s *Sym) *obj.LSym {
......
...@@ -38,6 +38,7 @@ var bootstrapDirs = []string{ ...@@ -38,6 +38,7 @@ var bootstrapDirs = []string{
"compile/internal/ppc64", "compile/internal/ppc64",
"compile/internal/ssa", "compile/internal/ssa",
"compile/internal/x86", "compile/internal/x86",
"internal/bio",
"internal/gcprog", "internal/gcprog",
"internal/obj", "internal/obj",
"internal/obj/arm", "internal/obj/arm",
......
// 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 bio implements seekable buffered I/O.
package bio
import (
"bufio"
"io"
"log"
"os"
)
const EOF = -1
// Buf implements a seekable buffered I/O abstraction.
type Buf struct {
f *os.File
r *bufio.Reader
w *bufio.Writer
}
func (b *Buf) Reader() *bufio.Reader { return b.r }
func (b *Buf) Writer() *bufio.Writer { return b.w }
func Create(name string) (*Buf, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Buf{f: f, w: bufio.NewWriter(f)}, nil
}
func Open(name string) (*Buf, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
return &Buf{f: f, r: bufio.NewReader(f)}, nil
}
func BufWriter(w io.Writer) *Buf {
return &Buf{w: bufio.NewWriter(w)}
}
func BufReader(r io.Reader) *Buf {
return &Buf{r: bufio.NewReader(r)}
}
func (b *Buf) Write(p []byte) (int, error) {
return b.w.Write(p)
}
func (b *Buf) WriteString(p string) (int, error) {
return b.w.WriteString(p)
}
func Bseek(b *Buf, offset int64, whence int) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
} else if b.r != nil {
if whence == 1 {
offset -= int64(b.r.Buffered())
}
}
off, err := b.f.Seek(offset, whence)
if err != nil {
log.Fatalf("seeking in output: %v", err)
}
if b.r != nil {
b.r.Reset(b.f)
}
return off
}
func Boffset(b *Buf) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
}
off, err := b.f.Seek(0, 1)
if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err)
}
if b.r != nil {
off -= int64(b.r.Buffered())
}
return off
}
func (b *Buf) Flush() error {
return b.w.Flush()
}
func (b *Buf) WriteByte(c byte) error {
return b.w.WriteByte(c)
}
func Bread(b *Buf, p []byte) int {
n, err := io.ReadFull(b.r, p)
if n == 0 {
if err != nil && err != io.EOF {
n = -1
}
}
return n
}
func Bgetc(b *Buf) int {
c, err := b.r.ReadByte()
if err != nil {
if err != io.EOF {
log.Fatalf("reading input: %v", err)
}
return EOF
}
return int(c)
}
func (b *Buf) Read(p []byte) (int, error) {
return b.r.Read(p)
}
func (b *Buf) Peek(n int) ([]byte, error) {
return b.r.Peek(n)
}
func Brdline(b *Buf, delim int) string {
s, err := b.r.ReadBytes(byte(delim))
if err != nil {
log.Fatalf("reading input: %v", err)
}
return string(s)
}
func (b *Buf) Close() error {
var err error
if b.w != nil {
err = b.w.Flush()
}
err1 := b.f.Close()
if err == nil {
err = err1
}
return err
}
...@@ -30,7 +30,10 @@ ...@@ -30,7 +30,10 @@
package obj package obj
import "cmd/internal/sys" import (
"cmd/internal/bio"
"cmd/internal/sys"
)
// An Addr is an argument to an instruction. // An Addr is an argument to an instruction.
// The general forms and their encodings are: // The general forms and their encodings are:
...@@ -626,7 +629,7 @@ type Link struct { ...@@ -626,7 +629,7 @@ type Link struct {
Flag_shared int32 Flag_shared int32
Flag_dynlink bool Flag_dynlink bool
Flag_optimize bool Flag_optimize bool
Bso *Biobuf Bso *bio.Buf
Pathname string Pathname string
Goroot string Goroot string
Goroot_final string Goroot_final string
......
...@@ -109,6 +109,7 @@ package obj ...@@ -109,6 +109,7 @@ package obj
import ( import (
"bufio" "bufio"
"cmd/internal/bio"
"cmd/internal/sys" "cmd/internal/sys"
"fmt" "fmt"
"log" "log"
...@@ -120,7 +121,7 @@ import ( ...@@ -120,7 +121,7 @@ import (
// The Go and C compilers, and the assembler, call writeobj to write // The Go and C compilers, and the assembler, call writeobj to write
// out a Go object file. The linker does not call this; the linker // out a Go object file. The linker does not call this; the linker
// does not write out object files. // does not write out object files.
func Writeobjdirect(ctxt *Link, b *Biobuf) { func Writeobjdirect(ctxt *Link, b *bio.Buf) {
Flushplist(ctxt) Flushplist(ctxt)
WriteObjFile(ctxt, b) WriteObjFile(ctxt, b)
} }
...@@ -373,16 +374,16 @@ func (w *objWriter) writeLengths() { ...@@ -373,16 +374,16 @@ func (w *objWriter) writeLengths() {
w.writeInt(int64(w.nFile)) w.writeInt(int64(w.nFile))
} }
func newObjWriter(ctxt *Link, b *Biobuf) *objWriter { func newObjWriter(ctxt *Link, b *bio.Buf) *objWriter {
return &objWriter{ return &objWriter{
ctxt: ctxt, ctxt: ctxt,
wr: b.w, wr: b.Writer(),
vrefIdx: make(map[string]int), vrefIdx: make(map[string]int),
refIdx: make(map[string]int), refIdx: make(map[string]int),
} }
} }
func WriteObjFile(ctxt *Link, b *Biobuf) { func WriteObjFile(ctxt *Link, b *bio.Buf) {
w := newObjWriter(ctxt, b) w := newObjWriter(ctxt, b)
// Magic header // Magic header
......
...@@ -5,10 +5,8 @@ ...@@ -5,10 +5,8 @@
package obj package obj
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"strings" "strings"
...@@ -26,144 +24,6 @@ func Cputime() float64 { ...@@ -26,144 +24,6 @@ func Cputime() float64 {
return time.Since(start).Seconds() return time.Since(start).Seconds()
} }
type Biobuf struct {
f *os.File
r *bufio.Reader
w *bufio.Writer
linelen int
}
func (b *Biobuf) Reader() *bufio.Reader { return b.r }
func Bopenw(name string) (*Biobuf, error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
return &Biobuf{f: f, w: bufio.NewWriter(f)}, nil
}
func Bopenr(name string) (*Biobuf, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
return &Biobuf{f: f, r: bufio.NewReader(f)}, nil
}
func Binitw(w io.Writer) *Biobuf {
return &Biobuf{w: bufio.NewWriter(w)}
}
func Binitr(r io.Reader) *Biobuf {
return &Biobuf{r: bufio.NewReader(r)}
}
func (b *Biobuf) Write(p []byte) (int, error) {
return b.w.Write(p)
}
func Bwritestring(b *Biobuf, p string) (int, error) {
return b.w.WriteString(p)
}
func Bseek(b *Biobuf, offset int64, whence int) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
} else if b.r != nil {
if whence == 1 {
offset -= int64(b.r.Buffered())
}
}
off, err := b.f.Seek(offset, whence)
if err != nil {
log.Fatalf("seeking in output: %v", err)
}
if b.r != nil {
b.r.Reset(b.f)
}
return off
}
func Boffset(b *Biobuf) int64 {
if b.w != nil {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
}
}
off, err := b.f.Seek(0, 1)
if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err)
}
if b.r != nil {
off -= int64(b.r.Buffered())
}
return off
}
func (b *Biobuf) Flush() error {
return b.w.Flush()
}
func Bputc(b *Biobuf, c byte) {
b.w.WriteByte(c)
}
const Beof = -1
func Bread(b *Biobuf, p []byte) int {
n, err := io.ReadFull(b.r, p)
if n == 0 {
if err != nil && err != io.EOF {
n = -1
}
}
return n
}
func Bgetc(b *Biobuf) int {
c, err := b.r.ReadByte()
if err != nil {
return -1
}
return int(c)
}
func (b *Biobuf) Read(p []byte) (int, error) {
return b.r.Read(p)
}
func (b *Biobuf) Peek(n int) ([]byte, error) {
return b.r.Peek(n)
}
func Brdline(b *Biobuf, delim int) string {
s, err := b.r.ReadBytes(byte(delim))
if err != nil {
log.Fatalf("reading input: %v", err)
}
b.linelen = len(s)
return string(s)
}
func Blinelen(b *Biobuf) int {
return b.linelen
}
func Bterm(b *Biobuf) error {
var err error
if b.w != nil {
err = b.w.Flush()
}
err1 := b.f.Close()
if err == nil {
err = err1
}
return err
}
func envOr(key, value string) string { func envOr(key, value string) string {
if x := os.Getenv(key); x != "" { if x := os.Getenv(key); x != "" {
return x return x
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
package ld package ld
import ( import (
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
...@@ -62,7 +63,7 @@ type ArHdr struct { ...@@ -62,7 +63,7 @@ type ArHdr struct {
// define them. This is used for the compiler support library // define them. This is used for the compiler support library
// libgcc.a. // libgcc.a.
func hostArchive(name string) { func hostArchive(name string) {
f, err := obj.Bopenr(name) f, err := bio.Open(name)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
// It's OK if we don't have a libgcc file at all. // It's OK if we don't have a libgcc file at all.
...@@ -73,15 +74,15 @@ func hostArchive(name string) { ...@@ -73,15 +74,15 @@ func hostArchive(name string) {
} }
Exitf("cannot open file %s: %v", name, err) Exitf("cannot open file %s: %v", name, err)
} }
defer obj.Bterm(f) defer f.Close()
magbuf := make([]byte, len(ARMAG)) magbuf := make([]byte, len(ARMAG))
if obj.Bread(f, magbuf) != len(magbuf) { if bio.Bread(f, magbuf) != len(magbuf) {
Exitf("file %s too short", name) Exitf("file %s too short", name)
} }
var arhdr ArHdr var arhdr ArHdr
l := nextar(f, obj.Boffset(f), &arhdr) l := nextar(f, bio.Boffset(f), &arhdr)
if l <= 0 { if l <= 0 {
Exitf("%s missing armap", name) Exitf("%s missing armap", name)
} }
...@@ -117,7 +118,7 @@ func hostArchive(name string) { ...@@ -117,7 +118,7 @@ func hostArchive(name string) {
l = atolwhex(arhdr.size) l = atolwhex(arhdr.size)
h := ldobj(f, "libgcc", l, pname, name, ArchiveObj) h := ldobj(f, "libgcc", l, pname, name, ArchiveObj)
obj.Bseek(f, h.off, 0) bio.Bseek(f, h.off, 0)
h.ld(f, h.pkg, h.length, h.pn) h.ld(f, h.pkg, h.length, h.pn)
} }
...@@ -130,7 +131,7 @@ func hostArchive(name string) { ...@@ -130,7 +131,7 @@ func hostArchive(name string) {
type archiveMap map[string]uint64 type archiveMap map[string]uint64
// readArmap reads the archive symbol map. // readArmap reads the archive symbol map.
func readArmap(filename string, f *obj.Biobuf, arhdr ArHdr) archiveMap { func readArmap(filename string, f *bio.Buf, arhdr ArHdr) archiveMap {
is64 := arhdr.name == "/SYM64/" is64 := arhdr.name == "/SYM64/"
wordSize := 4 wordSize := 4
if is64 { if is64 {
...@@ -139,7 +140,7 @@ func readArmap(filename string, f *obj.Biobuf, arhdr ArHdr) archiveMap { ...@@ -139,7 +140,7 @@ func readArmap(filename string, f *obj.Biobuf, arhdr ArHdr) archiveMap {
l := atolwhex(arhdr.size) l := atolwhex(arhdr.size)
contents := make([]byte, l) contents := make([]byte, l)
if obj.Bread(f, contents) != int(l) { if bio.Bread(f, contents) != int(l) {
Exitf("short read from %s", filename) Exitf("short read from %s", filename)
} }
......
...@@ -8,6 +8,7 @@ package ld ...@@ -8,6 +8,7 @@ package ld
import ( import (
"bytes" "bytes"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"fmt" "fmt"
"os" "os"
...@@ -26,7 +27,7 @@ func expandpkg(t0 string, pkg string) string { ...@@ -26,7 +27,7 @@ func expandpkg(t0 string, pkg string) string {
// once the dust settles, try to move some code to // once the dust settles, try to move some code to
// libmach, so that other linkers and ar can share. // libmach, so that other linkers and ar can share.
func ldpkg(f *obj.Biobuf, pkg string, length int64, filename string, whence int) { func ldpkg(f *bio.Buf, pkg string, length int64, filename string, whence int) {
var p0, p1 int var p0, p1 int
if Debug['g'] != 0 { if Debug['g'] != 0 {
...@@ -48,7 +49,7 @@ func ldpkg(f *obj.Biobuf, pkg string, length int64, filename string, whence int) ...@@ -48,7 +49,7 @@ func ldpkg(f *obj.Biobuf, pkg string, length int64, filename string, whence int)
} }
bdata := make([]byte, length) bdata := make([]byte, length)
if int64(obj.Bread(f, bdata)) != length { if int64(bio.Bread(f, bdata)) != length {
fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename) fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename)
if Debug['u'] != 0 { if Debug['u'] != 0 {
errorexit() errorexit()
......
...@@ -2,6 +2,7 @@ package ld ...@@ -2,6 +2,7 @@ package ld
import ( import (
"bytes" "bytes"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"encoding/binary" "encoding/binary"
...@@ -267,7 +268,7 @@ type ElfSect struct { ...@@ -267,7 +268,7 @@ type ElfSect struct {
} }
type ElfObj struct { type ElfObj struct {
f *obj.Biobuf f *bio.Buf
base int64 // offset in f where ELF begins base int64 // offset in f where ELF begins
length int64 // length of ELF length int64 // length of ELF
is64 int is64 int
...@@ -446,13 +447,13 @@ func parseArmAttributes(e binary.ByteOrder, data []byte) { ...@@ -446,13 +447,13 @@ func parseArmAttributes(e binary.ByteOrder, data []byte) {
} }
} }
func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) { func ldelf(f *bio.Buf, pkg string, length int64, pn string) {
if Debug['v'] != 0 { if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f ldelf %s\n", obj.Cputime(), pn) fmt.Fprintf(&Bso, "%5.2f ldelf %s\n", obj.Cputime(), pn)
} }
Ctxt.IncVersion() Ctxt.IncVersion()
base := int32(obj.Boffset(f)) base := int32(bio.Boffset(f))
var add uint64 var add uint64
var e binary.ByteOrder var e binary.ByteOrder
...@@ -475,7 +476,7 @@ func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -475,7 +476,7 @@ func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) {
var sect *ElfSect var sect *ElfSect
var sym ElfSym var sym ElfSym
var symbols []*LSym var symbols []*LSym
if obj.Bread(f, hdrbuf[:]) != len(hdrbuf) { if bio.Bread(f, hdrbuf[:]) != len(hdrbuf) {
goto bad goto bad
} }
hdr = new(ElfHdrBytes) hdr = new(ElfHdrBytes)
...@@ -600,7 +601,7 @@ func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -600,7 +601,7 @@ func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) {
elfobj.nsect = uint(elfobj.shnum) elfobj.nsect = uint(elfobj.shnum)
for i := 0; uint(i) < elfobj.nsect; i++ { for i := 0; uint(i) < elfobj.nsect; i++ {
if obj.Bseek(f, int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 { if bio.Bseek(f, int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 {
goto bad goto bad
} }
sect = &elfobj.sect[i] sect = &elfobj.sect[i]
...@@ -986,7 +987,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) { ...@@ -986,7 +987,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
sect.base = make([]byte, sect.size) sect.base = make([]byte, sect.size)
err = fmt.Errorf("short read") err = fmt.Errorf("short read")
if obj.Bseek(elfobj.f, int64(uint64(elfobj.base)+sect.off), 0) < 0 || obj.Bread(elfobj.f, sect.base) != len(sect.base) { if bio.Bseek(elfobj.f, int64(uint64(elfobj.base)+sect.off), 0) < 0 || bio.Bread(elfobj.f, sect.base) != len(sect.base) {
return err return err
} }
......
package ld package ld
import ( import (
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"encoding/binary" "encoding/binary"
...@@ -42,7 +43,7 @@ const ( ...@@ -42,7 +43,7 @@ const (
) )
type LdMachoObj struct { type LdMachoObj struct {
f *obj.Biobuf f *bio.Buf
base int64 // off in f where Mach-O begins base int64 // off in f where Mach-O begins
length int64 // length of Mach-O length int64 // length of Mach-O
is64 bool is64 bool
...@@ -298,7 +299,7 @@ func macholoadrel(m *LdMachoObj, sect *LdMachoSect) int { ...@@ -298,7 +299,7 @@ func macholoadrel(m *LdMachoObj, sect *LdMachoSect) int {
rel := make([]LdMachoRel, sect.nreloc) rel := make([]LdMachoRel, sect.nreloc)
n := int(sect.nreloc * 8) n := int(sect.nreloc * 8)
buf := make([]byte, n) buf := make([]byte, n)
if obj.Bseek(m.f, m.base+int64(sect.reloff), 0) < 0 || obj.Bread(m.f, buf) != n { if bio.Bseek(m.f, m.base+int64(sect.reloff), 0) < 0 || bio.Bread(m.f, buf) != n {
return -1 return -1
} }
var p []byte var p []byte
...@@ -344,7 +345,7 @@ func macholoaddsym(m *LdMachoObj, d *LdMachoDysymtab) int { ...@@ -344,7 +345,7 @@ func macholoaddsym(m *LdMachoObj, d *LdMachoDysymtab) int {
n := int(d.nindirectsyms) n := int(d.nindirectsyms)
p := make([]byte, n*4) p := make([]byte, n*4)
if obj.Bseek(m.f, m.base+int64(d.indirectsymoff), 0) < 0 || obj.Bread(m.f, p) != len(p) { if bio.Bseek(m.f, m.base+int64(d.indirectsymoff), 0) < 0 || bio.Bread(m.f, p) != len(p) {
return -1 return -1
} }
...@@ -361,7 +362,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int { ...@@ -361,7 +362,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
} }
strbuf := make([]byte, symtab.strsize) strbuf := make([]byte, symtab.strsize)
if obj.Bseek(m.f, m.base+int64(symtab.stroff), 0) < 0 || obj.Bread(m.f, strbuf) != len(strbuf) { if bio.Bseek(m.f, m.base+int64(symtab.stroff), 0) < 0 || bio.Bread(m.f, strbuf) != len(strbuf) {
return -1 return -1
} }
...@@ -371,7 +372,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int { ...@@ -371,7 +372,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
} }
n := int(symtab.nsym * uint32(symsize)) n := int(symtab.nsym * uint32(symsize))
symbuf := make([]byte, n) symbuf := make([]byte, n)
if obj.Bseek(m.f, m.base+int64(symtab.symoff), 0) < 0 || obj.Bread(m.f, symbuf) != len(symbuf) { if bio.Bseek(m.f, m.base+int64(symtab.symoff), 0) < 0 || bio.Bread(m.f, symbuf) != len(symbuf) {
return -1 return -1
} }
sym := make([]LdMachoSym, symtab.nsym) sym := make([]LdMachoSym, symtab.nsym)
...@@ -401,7 +402,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int { ...@@ -401,7 +402,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
return 0 return 0
} }
func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) { func ldmacho(f *bio.Buf, pkg string, length int64, pn string) {
var err error var err error
var j int var j int
var is64 bool var is64 bool
...@@ -431,8 +432,8 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -431,8 +432,8 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) {
var name string var name string
Ctxt.IncVersion() Ctxt.IncVersion()
base := obj.Boffset(f) base := bio.Boffset(f)
if obj.Bread(f, hdr[:]) != len(hdr) { if bio.Bread(f, hdr[:]) != len(hdr) {
goto bad goto bad
} }
...@@ -455,7 +456,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -455,7 +456,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) {
if is64 { if is64 {
var tmp [4]uint8 var tmp [4]uint8
obj.Bread(f, tmp[:4]) // skip reserved word in header bio.Bread(f, tmp[:4]) // skip reserved word in header
} }
m = new(LdMachoObj) m = new(LdMachoObj)
...@@ -493,7 +494,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -493,7 +494,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) {
m.cmd = make([]LdMachoCmd, ncmd) m.cmd = make([]LdMachoCmd, ncmd)
off = uint32(len(hdr)) off = uint32(len(hdr))
cmdp = make([]byte, cmdsz) cmdp = make([]byte, cmdsz)
if obj.Bread(f, cmdp) != len(cmdp) { if bio.Bread(f, cmdp) != len(cmdp) {
err = fmt.Errorf("reading cmds: %v", err) err = fmt.Errorf("reading cmds: %v", err)
goto bad goto bad
} }
...@@ -556,7 +557,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -556,7 +557,7 @@ func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) {
} }
dat = make([]byte, c.seg.filesz) dat = make([]byte, c.seg.filesz)
if obj.Bseek(f, m.base+int64(c.seg.fileoff), 0) < 0 || obj.Bread(f, dat) != len(dat) { if bio.Bseek(f, m.base+int64(c.seg.fileoff), 0) < 0 || bio.Bread(f, dat) != len(dat) {
err = fmt.Errorf("cannot load object data: %v", err) err = fmt.Errorf("cannot load object data: %v", err)
goto bad goto bad
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package ld package ld
import ( import (
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"encoding/binary" "encoding/binary"
...@@ -117,7 +118,7 @@ type PeSect struct { ...@@ -117,7 +118,7 @@ type PeSect struct {
} }
type PeObj struct { type PeObj struct {
f *obj.Biobuf f *bio.Buf
name string name string
base uint32 base uint32
sect []PeSect sect []PeSect
...@@ -128,14 +129,14 @@ type PeObj struct { ...@@ -128,14 +129,14 @@ type PeObj struct {
snames []byte snames []byte
} }
func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) { func ldpe(f *bio.Buf, pkg string, length int64, pn string) {
if Debug['v'] != 0 { if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f ldpe %s\n", obj.Cputime(), pn) fmt.Fprintf(&Bso, "%5.2f ldpe %s\n", obj.Cputime(), pn)
} }
var sect *PeSect var sect *PeSect
Ctxt.IncVersion() Ctxt.IncVersion()
base := int32(obj.Boffset(f)) base := int32(bio.Boffset(f))
peobj := new(PeObj) peobj := new(PeObj)
peobj.f = f peobj.f = f
...@@ -173,15 +174,15 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -173,15 +174,15 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) {
// TODO return error if found .cormeta // TODO return error if found .cormeta
// load string table // load string table
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0) bio.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if obj.Bread(f, symbuf[:4]) != 4 { if bio.Bread(f, symbuf[:4]) != 4 {
goto bad goto bad
} }
l = Le32(symbuf[:]) l = Le32(symbuf[:])
peobj.snames = make([]byte, l) peobj.snames = make([]byte, l)
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0) bio.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if obj.Bread(f, peobj.snames) != len(peobj.snames) { if bio.Bread(f, peobj.snames) != len(peobj.snames) {
goto bad goto bad
} }
...@@ -201,10 +202,10 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -201,10 +202,10 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) {
peobj.pesym = make([]PeSym, peobj.fh.NumberOfSymbols) peobj.pesym = make([]PeSym, peobj.fh.NumberOfSymbols)
peobj.npesym = uint(peobj.fh.NumberOfSymbols) peobj.npesym = uint(peobj.fh.NumberOfSymbols)
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable), 0) bio.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable), 0)
for i := 0; uint32(i) < peobj.fh.NumberOfSymbols; i += numaux + 1 { for i := 0; uint32(i) < peobj.fh.NumberOfSymbols; i += numaux + 1 {
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0) bio.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0)
if obj.Bread(f, symbuf[:]) != len(symbuf) { if bio.Bread(f, symbuf[:]) != len(symbuf) {
goto bad goto bad
} }
...@@ -289,10 +290,10 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) { ...@@ -289,10 +290,10 @@ func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) {
} }
r = make([]Reloc, rsect.sh.NumberOfRelocations) r = make([]Reloc, rsect.sh.NumberOfRelocations)
obj.Bseek(f, int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0) bio.Bseek(f, int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0)
for j = 0; j < int(rsect.sh.NumberOfRelocations); j++ { for j = 0; j < int(rsect.sh.NumberOfRelocations); j++ {
rp = &r[j] rp = &r[j]
if obj.Bread(f, symbuf[:10]) != 10 { if bio.Bread(f, symbuf[:10]) != 10 {
goto bad goto bad
} }
rva := Le32(symbuf[0:]) rva := Le32(symbuf[0:])
...@@ -465,7 +466,7 @@ func pemap(peobj *PeObj, sect *PeSect) int { ...@@ -465,7 +466,7 @@ func pemap(peobj *PeObj, sect *PeSect) int {
if sect.sh.PointerToRawData == 0 { // .bss doesn't have data in object file if sect.sh.PointerToRawData == 0 { // .bss doesn't have data in object file
return 0 return 0
} }
if obj.Bseek(peobj.f, int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 || obj.Bread(peobj.f, sect.base) != len(sect.base) { if bio.Bseek(peobj.f, int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 || bio.Bread(peobj.f, sect.base) != len(sect.base) {
return -1 return -1
} }
......
...@@ -33,6 +33,7 @@ package ld ...@@ -33,6 +33,7 @@ package ld
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"crypto/sha1" "crypto/sha1"
...@@ -240,7 +241,7 @@ const ( ...@@ -240,7 +241,7 @@ const (
var ( var (
headstring string headstring string
// buffered output // buffered output
Bso obj.Biobuf Bso bio.Buf
) )
type outBuf struct { type outBuf struct {
...@@ -738,13 +739,13 @@ func loadlib() { ...@@ -738,13 +739,13 @@ func loadlib() {
* look for the next file in an archive. * look for the next file in an archive.
* adapted from libmach. * adapted from libmach.
*/ */
func nextar(bp *obj.Biobuf, off int64, a *ArHdr) int64 { func nextar(bp *bio.Buf, off int64, a *ArHdr) int64 {
if off&1 != 0 { if off&1 != 0 {
off++ off++
} }
obj.Bseek(bp, off, 0) bio.Bseek(bp, off, 0)
buf := make([]byte, SAR_HDR) buf := make([]byte, SAR_HDR)
if n := obj.Bread(bp, buf); n < len(buf) { if n := bio.Bread(bp, buf); n < len(buf) {
if n >= 0 { if n >= 0 {
return 0 return 0
} }
...@@ -773,25 +774,25 @@ func objfile(lib *Library) { ...@@ -773,25 +774,25 @@ func objfile(lib *Library) {
fmt.Fprintf(&Bso, "%5.2f ldobj: %s (%s)\n", obj.Cputime(), lib.File, pkg) fmt.Fprintf(&Bso, "%5.2f ldobj: %s (%s)\n", obj.Cputime(), lib.File, pkg)
} }
Bso.Flush() Bso.Flush()
f, err := obj.Bopenr(lib.File) f, err := bio.Open(lib.File)
if err != nil { if err != nil {
Exitf("cannot open file %s: %v", lib.File, err) Exitf("cannot open file %s: %v", lib.File, err)
} }
magbuf := make([]byte, len(ARMAG)) magbuf := make([]byte, len(ARMAG))
if obj.Bread(f, magbuf) != len(magbuf) || !strings.HasPrefix(string(magbuf), ARMAG) { if bio.Bread(f, magbuf) != len(magbuf) || !strings.HasPrefix(string(magbuf), ARMAG) {
/* load it as a regular file */ /* load it as a regular file */
l := obj.Bseek(f, 0, 2) l := bio.Bseek(f, 0, 2)
obj.Bseek(f, 0, 0) bio.Bseek(f, 0, 0)
ldobj(f, pkg, l, lib.File, lib.File, FileObj) ldobj(f, pkg, l, lib.File, lib.File, FileObj)
obj.Bterm(f) f.Close()
return return
} }
/* process __.PKGDEF */ /* process __.PKGDEF */
off := obj.Boffset(f) off := bio.Boffset(f)
var arhdr ArHdr var arhdr ArHdr
l := nextar(f, off, &arhdr) l := nextar(f, off, &arhdr)
...@@ -807,12 +808,12 @@ func objfile(lib *Library) { ...@@ -807,12 +808,12 @@ func objfile(lib *Library) {
} }
if Buildmode == BuildmodeShared { if Buildmode == BuildmodeShared {
before := obj.Boffset(f) before := bio.Boffset(f)
pkgdefBytes := make([]byte, atolwhex(arhdr.size)) pkgdefBytes := make([]byte, atolwhex(arhdr.size))
obj.Bread(f, pkgdefBytes) bio.Bread(f, pkgdefBytes)
hash := sha1.Sum(pkgdefBytes) hash := sha1.Sum(pkgdefBytes)
lib.hash = hash[:] lib.hash = hash[:]
obj.Bseek(f, before, 0) bio.Bseek(f, before, 0)
} }
off += l off += l
...@@ -848,11 +849,11 @@ func objfile(lib *Library) { ...@@ -848,11 +849,11 @@ func objfile(lib *Library) {
} }
out: out:
obj.Bterm(f) f.Close()
} }
type Hostobj struct { type Hostobj struct {
ld func(*obj.Biobuf, string, int64, string) ld func(*bio.Buf, string, int64, string)
pkg string pkg string
pn string pn string
file string file string
...@@ -873,7 +874,7 @@ var internalpkg = []string{ ...@@ -873,7 +874,7 @@ var internalpkg = []string{
"runtime/msan", "runtime/msan",
} }
func ldhostobj(ld func(*obj.Biobuf, string, int64, string), f *obj.Biobuf, pkg string, length int64, pn string, file string) *Hostobj { func ldhostobj(ld func(*bio.Buf, string, int64, string), f *bio.Buf, pkg string, length int64, pn string, file string) *Hostobj {
isinternal := false isinternal := false
for i := 0; i < len(internalpkg); i++ { for i := 0; i < len(internalpkg); i++ {
if pkg == internalpkg[i] { if pkg == internalpkg[i] {
...@@ -904,26 +905,26 @@ func ldhostobj(ld func(*obj.Biobuf, string, int64, string), f *obj.Biobuf, pkg s ...@@ -904,26 +905,26 @@ func ldhostobj(ld func(*obj.Biobuf, string, int64, string), f *obj.Biobuf, pkg s
h.pkg = pkg h.pkg = pkg
h.pn = pn h.pn = pn
h.file = file h.file = file
h.off = obj.Boffset(f) h.off = bio.Boffset(f)
h.length = length h.length = length
return h return h
} }
func hostobjs() { func hostobjs() {
var f *obj.Biobuf var f *bio.Buf
var h *Hostobj var h *Hostobj
for i := 0; i < len(hostobj); i++ { for i := 0; i < len(hostobj); i++ {
h = &hostobj[i] h = &hostobj[i]
var err error var err error
f, err = obj.Bopenr(h.file) f, err = bio.Open(h.file)
if f == nil { if f == nil {
Exitf("cannot reopen %s: %v", h.pn, err) Exitf("cannot reopen %s: %v", h.pn, err)
} }
obj.Bseek(f, h.off, 0) bio.Bseek(f, h.off, 0)
h.ld(f, h.pkg, h.length, h.pn) h.ld(f, h.pkg, h.length, h.pn)
obj.Bterm(f) f.Close()
} }
} }
...@@ -1265,15 +1266,15 @@ func hostlinkArchArgs() []string { ...@@ -1265,15 +1266,15 @@ func hostlinkArchArgs() []string {
// ldobj loads an input object. If it is a host object (an object // ldobj loads an input object. If it is a host object (an object
// compiled by a non-Go compiler) it returns the Hostobj pointer. If // compiled by a non-Go compiler) it returns the Hostobj pointer. If
// it is a Go object, it returns nil. // it is a Go object, it returns nil.
func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, whence int) *Hostobj { func ldobj(f *bio.Buf, pkg string, length int64, pn string, file string, whence int) *Hostobj {
eof := obj.Boffset(f) + length eof := bio.Boffset(f) + length
start := obj.Boffset(f) start := bio.Boffset(f)
c1 := obj.Bgetc(f) c1 := bio.Bgetc(f)
c2 := obj.Bgetc(f) c2 := bio.Bgetc(f)
c3 := obj.Bgetc(f) c3 := bio.Bgetc(f)
c4 := obj.Bgetc(f) c4 := bio.Bgetc(f)
obj.Bseek(f, start, 0) bio.Bseek(f, start, 0)
magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4) magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4)
if magic == 0x7f454c46 { // \x7F E L F if magic == 0x7f454c46 { // \x7F E L F
...@@ -1289,12 +1290,8 @@ func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, when ...@@ -1289,12 +1290,8 @@ func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, when
} }
/* check the header */ /* check the header */
line := obj.Brdline(f, '\n') line := bio.Brdline(f, '\n')
if line == "" { if line == "" {
if obj.Blinelen(f) > 0 {
Diag("%s: not an object file", pn)
return nil
}
Diag("truncated object file: %s", pn) Diag("truncated object file: %s", pn)
return nil return nil
} }
...@@ -1337,28 +1334,28 @@ func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, when ...@@ -1337,28 +1334,28 @@ func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, when
} }
/* skip over exports and other info -- ends with \n!\n */ /* skip over exports and other info -- ends with \n!\n */
import0 := obj.Boffset(f) import0 := bio.Boffset(f)
c1 = '\n' // the last line ended in \n c1 = '\n' // the last line ended in \n
c2 = obj.Bgetc(f) c2 = bio.Bgetc(f)
c3 = obj.Bgetc(f) c3 = bio.Bgetc(f)
for c1 != '\n' || c2 != '!' || c3 != '\n' { for c1 != '\n' || c2 != '!' || c3 != '\n' {
c1 = c2 c1 = c2
c2 = c3 c2 = c3
c3 = obj.Bgetc(f) c3 = bio.Bgetc(f)
if c3 == obj.Beof { if c3 == bio.EOF {
Diag("truncated object file: %s", pn) Diag("truncated object file: %s", pn)
return nil return nil
} }
} }
import1 := obj.Boffset(f) import1 := bio.Boffset(f)
obj.Bseek(f, import0, 0) bio.Bseek(f, import0, 0)
ldpkg(f, pkg, import1-import0-2, pn, whence) // -2 for !\n ldpkg(f, pkg, import1-import0-2, pn, whence) // -2 for !\n
obj.Bseek(f, import1, 0) bio.Bseek(f, import1, 0)
LoadObjFile(Ctxt, f, pkg, eof-obj.Boffset(f), pn) LoadObjFile(Ctxt, f, pkg, eof-bio.Boffset(f), pn)
return nil return nil
} }
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
package ld package ld
import ( import (
"cmd/internal/obj" "cmd/internal/bio"
"cmd/internal/sys" "cmd/internal/sys"
"debug/elf" "debug/elf"
"fmt" "fmt"
...@@ -165,7 +165,7 @@ type Link struct { ...@@ -165,7 +165,7 @@ type Link struct {
Headtype int Headtype int
Arch *sys.Arch Arch *sys.Arch
Debugvlog int32 Debugvlog int32
Bso *obj.Biobuf Bso *bio.Buf
Windows int32 Windows int32
Goroot string Goroot string
......
...@@ -110,6 +110,7 @@ package ld ...@@ -110,6 +110,7 @@ package ld
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"io" "io"
"log" "log"
...@@ -146,8 +147,8 @@ type objReader struct { ...@@ -146,8 +147,8 @@ type objReader struct {
file []*LSym file []*LSym
} }
func LoadObjFile(ctxt *Link, f *obj.Biobuf, pkg string, length int64, pn string) { func LoadObjFile(ctxt *Link, f *bio.Buf, pkg string, length int64, pn string) {
start := obj.Boffset(f) start := bio.Boffset(f)
r := &objReader{ r := &objReader{
rd: f.Reader(), rd: f.Reader(),
pkg: pkg, pkg: pkg,
...@@ -156,8 +157,8 @@ func LoadObjFile(ctxt *Link, f *obj.Biobuf, pkg string, length int64, pn string) ...@@ -156,8 +157,8 @@ func LoadObjFile(ctxt *Link, f *obj.Biobuf, pkg string, length int64, pn string)
dupSym: &LSym{Name: ".dup"}, dupSym: &LSym{Name: ".dup"},
} }
r.loadObjFile() r.loadObjFile()
if obj.Boffset(f) != start+length { if bio.Boffset(f) != start+length {
log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(obj.Boffset(f)), int64(start+length)) log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(bio.Boffset(f)), int64(start+length))
} }
} }
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
package ld package ld
import ( import (
"cmd/internal/bio"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/sys" "cmd/internal/sys"
"flag" "flag"
...@@ -49,7 +50,7 @@ func Ldmain() { ...@@ -49,7 +50,7 @@ func Ldmain() {
Ctxt.Diag = Diag Ctxt.Diag = Diag
Ctxt.Bso = &Bso Ctxt.Bso = &Bso
Bso = *obj.Binitw(os.Stdout) Bso = *bio.BufWriter(os.Stdout)
Debug = [128]int{} Debug = [128]int{}
nerrors = 0 nerrors = 0
outfile = "" outfile = ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment