Commit 71274e48 authored by Dave Cheney's avatar Dave Cheney

cmd/internal/ld: delete Biobuf

Update #10652

This proposal deletes cmd/internal/ld.Biobuf and replaces all uses with
cmd/internal/obj.Biobuf. As cmd/internal/ld already imported cmd/internal/obj
there are no additional dependencies created.

Notes:

- ld.Boffset included more checks, so it was merged into obj.Boffset
- obj.Bflush was removed in 8d16253c, so replaced all calls to
  ld.Bflush, with obj.Biobuf.Flush.
- Almost all of this change was prepared with sed.

Change-Id: I814854d52f5729a5a40c523c8188e465246b88da
Reviewed-on: https://go-review.googlesource.com/9660Reviewed-by: default avatarKeith Randall <khr@golang.org>
Run-TryBot: Dave Cheney <dave@cheney.net>
parent 5ffdf53b
......@@ -579,7 +579,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Iself {
ld.Asmbelfsetup()
......@@ -597,7 +597,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segrodata.Fileoff))
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
......@@ -606,7 +606,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segdata.Fileoff))
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
......@@ -639,7 +639,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
switch ld.HEADTYPE {
default:
if ld.Iself {
......@@ -700,7 +700,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f header\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(0)
switch ld.HEADTYPE {
default:
......
......@@ -696,12 +696,12 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f codeblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Iself {
ld.Asmbelfsetup()
......@@ -719,7 +719,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segrodata.Fileoff))
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
......@@ -728,7 +728,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segdata.Fileoff))
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
......@@ -782,7 +782,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
switch ld.HEADTYPE {
default:
case obj.Hplan9,
......@@ -859,7 +859,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f headr\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(0)
switch ld.HEADTYPE {
default:
......
......@@ -319,7 +319,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Iself {
ld.Asmbelfsetup()
......@@ -337,7 +337,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segrodata.Fileoff))
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
......@@ -346,7 +346,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segdata.Fileoff))
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
......@@ -379,7 +379,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
switch ld.HEADTYPE {
default:
if ld.Iself {
......@@ -440,7 +440,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f header\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(0)
switch ld.HEADTYPE {
default:
......
......@@ -570,7 +570,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Iself {
ld.Asmbelfsetup()
......@@ -588,7 +588,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segrodata.Fileoff))
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
......@@ -597,7 +597,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segdata.Fileoff))
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
......@@ -627,7 +627,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
switch ld.HEADTYPE {
default:
if ld.Iself {
......@@ -697,7 +697,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f headr\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(0)
switch ld.HEADTYPE {
default:
......
......@@ -690,7 +690,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
if ld.Iself {
ld.Asmbelfsetup()
......@@ -708,7 +708,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segrodata.Fileoff))
ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
......@@ -717,7 +717,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(int64(ld.Segdata.Fileoff))
ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
......@@ -732,7 +732,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
switch ld.HEADTYPE {
default:
if ld.Iself {
......@@ -785,7 +785,7 @@ func asmb() {
if ld.Debug['v'] != 0 {
fmt.Fprintf(&ld.Bso, "%5.2f header\n", obj.Cputime())
}
ld.Bflush(&ld.Bso)
ld.Bso.Flush()
ld.Cseek(0)
switch ld.HEADTYPE {
default:
......
......@@ -609,7 +609,7 @@ func reloc() {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime())
}
Bflush(&Bso)
Bso.Flush()
for s := Ctxt.Textp; s != nil; s = s.Next {
relocsym(s)
......@@ -685,7 +685,7 @@ func dynreloc() {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f reloc\n", obj.Cputime())
}
Bflush(&Bso)
Bso.Flush()
for s := Ctxt.Textp; s != nil; s = s.Next {
dynrelocsym(s)
......@@ -817,7 +817,7 @@ func Codeblk(addr int64, size int64) {
}
}
Bflush(&Bso)
Bso.Flush()
}
func Datblk(addr int64, size int64) {
......@@ -1204,7 +1204,7 @@ func dodata() {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f dodata\n", obj.Cputime())
}
Bflush(&Bso)
Bso.Flush()
var last *LSym
datap = nil
......
......@@ -53,7 +53,7 @@ func lookupImport(name string) *Import {
return x
}
func ldpkg(f *Biobuf, pkg string, length int64, filename string, whence int) {
func ldpkg(f *obj.Biobuf, pkg string, length int64, filename string, whence int) {
var p0, p1 int
if Debug['g'] != 0 {
......@@ -69,7 +69,7 @@ func ldpkg(f *Biobuf, pkg string, length int64, filename string, whence int) {
}
bdata := make([]byte, length)
if int64(Bread(f, bdata)) != length {
if int64(obj.Bread(f, bdata)) != length {
fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename)
if Debug['u'] != 0 {
errorexit()
......
......@@ -265,7 +265,7 @@ type ElfSect struct {
}
type ElfObj struct {
f *Biobuf
f *obj.Biobuf
base int64 // offset in f where ELF begins
length int64 // length of ELF
is64 int
......@@ -315,13 +315,13 @@ func valuecmp(a *LSym, b *LSym) int {
return 0
}
func ldelf(f *Biobuf, pkg string, length int64, pn string) {
func ldelf(f *obj.Biobuf, pkg string, length int64, pn string) {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f ldelf %s\n", obj.Cputime(), pn)
}
Ctxt.Version++
base := int32(Boffset(f))
base := int32(obj.Boffset(f))
var add uint64
var e binary.ByteOrder
......@@ -344,7 +344,7 @@ func ldelf(f *Biobuf, pkg string, length int64, pn string) {
var sect *ElfSect
var sym ElfSym
var symbols []*LSym
if Bread(f, hdrbuf[:]) != len(hdrbuf) {
if obj.Bread(f, hdrbuf[:]) != len(hdrbuf) {
goto bad
}
hdr = new(ElfHdrBytes)
......@@ -457,7 +457,7 @@ func ldelf(f *Biobuf, pkg string, length int64, pn string) {
elfobj.nsect = uint(elfobj.shnum)
for i := 0; uint(i) < elfobj.nsect; i++ {
if Bseek(f, int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 {
if obj.Bseek(f, int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0) < 0 {
goto bad
}
sect = &elfobj.sect[i]
......@@ -826,7 +826,7 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
sect.base = make([]byte, sect.size)
err = fmt.Errorf("short read")
if Bseek(elfobj.f, int64(uint64(elfobj.base)+sect.off), 0) < 0 || Bread(elfobj.f, sect.base) != len(sect.base) {
if obj.Bseek(elfobj.f, int64(uint64(elfobj.base)+sect.off), 0) < 0 || obj.Bread(elfobj.f, sect.base) != len(sect.base) {
return err
}
......
......@@ -41,7 +41,7 @@ const (
)
type LdMachoObj struct {
f *Biobuf
f *obj.Biobuf
base int64 // off in f where Mach-O begins
length int64 // length of Mach-O
is64 bool
......@@ -297,7 +297,7 @@ func macholoadrel(m *LdMachoObj, sect *LdMachoSect) int {
rel := make([]LdMachoRel, sect.nreloc)
n := int(sect.nreloc * 8)
buf := make([]byte, n)
if Bseek(m.f, m.base+int64(sect.reloff), 0) < 0 || Bread(m.f, buf) != n {
if obj.Bseek(m.f, m.base+int64(sect.reloff), 0) < 0 || obj.Bread(m.f, buf) != n {
return -1
}
var p []byte
......@@ -343,7 +343,7 @@ func macholoaddsym(m *LdMachoObj, d *LdMachoDysymtab) int {
n := int(d.nindirectsyms)
p := make([]byte, n*4)
if Bseek(m.f, m.base+int64(d.indirectsymoff), 0) < 0 || Bread(m.f, p) != len(p) {
if obj.Bseek(m.f, m.base+int64(d.indirectsymoff), 0) < 0 || obj.Bread(m.f, p) != len(p) {
return -1
}
......@@ -360,7 +360,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
}
strbuf := make([]byte, symtab.strsize)
if Bseek(m.f, m.base+int64(symtab.stroff), 0) < 0 || Bread(m.f, strbuf) != len(strbuf) {
if obj.Bseek(m.f, m.base+int64(symtab.stroff), 0) < 0 || obj.Bread(m.f, strbuf) != len(strbuf) {
return -1
}
......@@ -370,7 +370,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
}
n := int(symtab.nsym * uint32(symsize))
symbuf := make([]byte, n)
if Bseek(m.f, m.base+int64(symtab.symoff), 0) < 0 || Bread(m.f, symbuf) != len(symbuf) {
if obj.Bseek(m.f, m.base+int64(symtab.symoff), 0) < 0 || obj.Bread(m.f, symbuf) != len(symbuf) {
return -1
}
sym := make([]LdMachoSym, symtab.nsym)
......@@ -400,7 +400,7 @@ func macholoadsym(m *LdMachoObj, symtab *LdMachoSymtab) int {
return 0
}
func ldmacho(f *Biobuf, pkg string, length int64, pn string) {
func ldmacho(f *obj.Biobuf, pkg string, length int64, pn string) {
var err error
var j int
var is64 bool
......@@ -430,8 +430,8 @@ func ldmacho(f *Biobuf, pkg string, length int64, pn string) {
var name string
Ctxt.Version++
base := Boffset(f)
if Bread(f, hdr[:]) != len(hdr) {
base := obj.Boffset(f)
if obj.Bread(f, hdr[:]) != len(hdr) {
goto bad
}
......@@ -454,7 +454,7 @@ func ldmacho(f *Biobuf, pkg string, length int64, pn string) {
if is64 {
var tmp [4]uint8
Bread(f, tmp[:4]) // skip reserved word in header
obj.Bread(f, tmp[:4]) // skip reserved word in header
}
m = new(LdMachoObj)
......@@ -492,7 +492,7 @@ func ldmacho(f *Biobuf, pkg string, length int64, pn string) {
m.cmd = make([]LdMachoCmd, ncmd)
off = uint32(len(hdr))
cmdp = make([]byte, cmdsz)
if Bread(f, cmdp) != len(cmdp) {
if obj.Bread(f, cmdp) != len(cmdp) {
err = fmt.Errorf("reading cmds: %v", err)
goto bad
}
......@@ -555,7 +555,7 @@ func ldmacho(f *Biobuf, pkg string, length int64, pn string) {
}
dat = make([]byte, c.seg.filesz)
if Bseek(f, m.base+int64(c.seg.fileoff), 0) < 0 || Bread(f, dat) != len(dat) {
if obj.Bseek(f, m.base+int64(c.seg.fileoff), 0) < 0 || obj.Bread(f, dat) != len(dat) {
err = fmt.Errorf("cannot load object data: %v", err)
goto bad
}
......
......@@ -115,7 +115,7 @@ type PeSect struct {
}
type PeObj struct {
f *Biobuf
f *obj.Biobuf
name string
base uint32
sect []PeSect
......@@ -126,14 +126,14 @@ type PeObj struct {
snames []byte
}
func ldpe(f *Biobuf, pkg string, length int64, pn string) {
func ldpe(f *obj.Biobuf, pkg string, length int64, pn string) {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "%5.2f ldpe %s\n", obj.Cputime(), pn)
}
var sect *PeSect
Ctxt.Version++
base := int32(Boffset(f))
base := int32(obj.Boffset(f))
peobj := new(PeObj)
peobj.f = f
......@@ -171,15 +171,15 @@ func ldpe(f *Biobuf, pkg string, length int64, pn string) {
// TODO return error if found .cormeta
// load string table
Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if Bread(f, symbuf[:4]) != 4 {
if obj.Bread(f, symbuf[:4]) != 4 {
goto bad
}
l = Le32(symbuf[:])
peobj.snames = make([]byte, l)
Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(peobj.fh.NumberOfSymbols), 0)
if Bread(f, peobj.snames) != len(peobj.snames) {
obj.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) {
goto bad
}
......@@ -199,10 +199,10 @@ func ldpe(f *Biobuf, pkg string, length int64, pn string) {
peobj.pesym = make([]PeSym, peobj.fh.NumberOfSymbols)
peobj.npesym = uint(peobj.fh.NumberOfSymbols)
Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable), 0)
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable), 0)
for i := 0; uint32(i) < peobj.fh.NumberOfSymbols; i += numaux + 1 {
Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0)
if Bread(f, symbuf[:]) != len(symbuf) {
obj.Bseek(f, int64(base)+int64(peobj.fh.PointerToSymbolTable)+int64(len(symbuf))*int64(i), 0)
if obj.Bread(f, symbuf[:]) != len(symbuf) {
goto bad
}
......@@ -287,10 +287,10 @@ func ldpe(f *Biobuf, pkg string, length int64, pn string) {
}
r = make([]Reloc, rsect.sh.NumberOfRelocations)
Bseek(f, int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0)
obj.Bseek(f, int64(peobj.base)+int64(rsect.sh.PointerToRelocations), 0)
for j = 0; j < int(rsect.sh.NumberOfRelocations); j++ {
rp = &r[j]
if Bread(f, symbuf[:10]) != 10 {
if obj.Bread(f, symbuf[:10]) != 10 {
goto bad
}
rva := Le32(symbuf[0:])
......@@ -463,7 +463,7 @@ func pemap(peobj *PeObj, sect *PeSect) int {
if sect.sh.PointerToRawData == 0 { // .bss doesn't have data in object file
return 0
}
if Bseek(peobj.f, int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 || Bread(peobj.f, sect.base) != len(sect.base) {
if obj.Bseek(peobj.f, int64(peobj.base)+int64(sect.sh.PointerToRawData), 0) < 0 || obj.Bread(peobj.f, sect.base) != len(sect.base) {
return -1
}
......
......@@ -230,7 +230,7 @@ const (
var (
headstring string
// buffered output
Bso Biobuf
Bso obj.Biobuf
)
var coutbuf struct {
......@@ -608,13 +608,13 @@ func loadlib() {
* look for the next file in an archive.
* adapted from libmach.
*/
func nextar(bp *Biobuf, off int64, a *ArHdr) int64 {
func nextar(bp *obj.Biobuf, off int64, a *ArHdr) int64 {
if off&1 != 0 {
off++
}
Bseek(bp, off, 0)
obj.Bseek(bp, off, 0)
buf := make([]byte, SAR_HDR)
if n := Bread(bp, buf); n < len(buf) {
if n := obj.Bread(bp, buf); n < len(buf) {
if n >= 0 {
return 0
}
......@@ -642,28 +642,28 @@ func objfile(file string, pkg string) {
if Debug['v'] > 1 {
fmt.Fprintf(&Bso, "%5.2f ldobj: %s (%s)\n", obj.Cputime(), file, pkg)
}
Bflush(&Bso)
Bso.Flush()
var err error
var f *Biobuf
f, err = Bopenr(file)
var f *obj.Biobuf
f, err = obj.Bopenr(file)
if err != nil {
Exitf("cannot open file %s: %v", file, err)
}
magbuf := make([]byte, len(ARMAG))
if Bread(f, magbuf) != len(magbuf) || !strings.HasPrefix(string(magbuf), ARMAG) {
if obj.Bread(f, magbuf) != len(magbuf) || !strings.HasPrefix(string(magbuf), ARMAG) {
/* load it as a regular file */
l := Bseek(f, 0, 2)
l := obj.Bseek(f, 0, 2)
Bseek(f, 0, 0)
obj.Bseek(f, 0, 0)
ldobj(f, pkg, l, file, file, FileObj)
Bterm(f)
obj.Bterm(f)
return
}
/* skip over optional __.GOSYMDEF and process __.PKGDEF */
off := Boffset(f)
off := obj.Boffset(f)
var arhdr ArHdr
l := nextar(f, off, &arhdr)
......@@ -722,11 +722,11 @@ func objfile(file string, pkg string) {
}
out:
Bterm(f)
obj.Bterm(f)
}
type Hostobj struct {
ld func(*Biobuf, string, int64, string)
ld func(*obj.Biobuf, string, int64, string)
pkg string
pn string
file string
......@@ -746,7 +746,7 @@ var internalpkg = []string{
"runtime/race",
}
func ldhostobj(ld func(*Biobuf, string, int64, string), f *Biobuf, pkg string, length int64, pn string, file string) {
func ldhostobj(ld func(*obj.Biobuf, string, int64, string), f *obj.Biobuf, pkg string, length int64, pn string, file string) {
isinternal := false
for i := 0; i < len(internalpkg); i++ {
if pkg == internalpkg[i] {
......@@ -777,25 +777,25 @@ func ldhostobj(ld func(*Biobuf, string, int64, string), f *Biobuf, pkg string, l
h.pkg = pkg
h.pn = pn
h.file = file
h.off = Boffset(f)
h.off = obj.Boffset(f)
h.length = length
}
func hostobjs() {
var f *Biobuf
var f *obj.Biobuf
var h *Hostobj
for i := 0; i < len(hostobj); i++ {
h = &hostobj[i]
var err error
f, err = Bopenr(h.file)
f, err = obj.Bopenr(h.file)
if f == nil {
Exitf("cannot reopen %s: %v", h.pn, err)
}
Bseek(f, h.off, 0)
obj.Bseek(f, h.off, 0)
h.ld(f, h.pkg, h.length, h.pn)
Bterm(f)
obj.Bterm(f)
}
}
......@@ -875,7 +875,7 @@ func archive() {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "archive: %s\n", strings.Join(argv, " "))
Bflush(&Bso)
Bso.Flush()
}
if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil {
......@@ -1019,7 +1019,7 @@ func hostlink() {
fmt.Fprintf(&Bso, " %q", v)
}
fmt.Fprintf(&Bso, "\n")
Bflush(&Bso)
Bso.Flush()
}
if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil {
......@@ -1027,15 +1027,15 @@ func hostlink() {
}
}
func ldobj(f *Biobuf, pkg string, length int64, pn string, file string, whence int) {
eof := Boffset(f) + length
func ldobj(f *obj.Biobuf, pkg string, length int64, pn string, file string, whence int) {
eof := obj.Boffset(f) + length
start := Boffset(f)
c1 := Bgetc(f)
c2 := Bgetc(f)
c3 := Bgetc(f)
c4 := Bgetc(f)
Bseek(f, start, 0)
start := obj.Boffset(f)
c1 := obj.Bgetc(f)
c2 := obj.Bgetc(f)
c3 := obj.Bgetc(f)
c4 := obj.Bgetc(f)
obj.Bseek(f, start, 0)
magic := uint32(c1)<<24 | uint32(c2)<<16 | uint32(c3)<<8 | uint32(c4)
if magic == 0x7f454c46 { // \x7F E L F
......@@ -1054,9 +1054,9 @@ func ldobj(f *Biobuf, pkg string, length int64, pn string, file string, whence i
}
/* check the header */
line := Brdline(f, '\n')
line := obj.Brdline(f, '\n')
if line == "" {
if Blinelen(f) > 0 {
if obj.Blinelen(f) > 0 {
Diag("%s: not an object file", pn)
return
}
......@@ -1101,28 +1101,28 @@ func ldobj(f *Biobuf, pkg string, length int64, pn string, file string, whence i
}
/* skip over exports and other info -- ends with \n!\n */
import0 := Boffset(f)
import0 := obj.Boffset(f)
c1 = '\n' // the last line ended in \n
c2 = Bgetc(f)
c3 = Bgetc(f)
c2 = obj.Bgetc(f)
c3 = obj.Bgetc(f)
for c1 != '\n' || c2 != '!' || c3 != '\n' {
c1 = c2
c2 = c3
c3 = Bgetc(f)
if c3 == Beof {
c3 = obj.Bgetc(f)
if c3 == obj.Beof {
Diag("truncated object file: %s", pn)
return
}
}
import1 := Boffset(f)
import1 := obj.Boffset(f)
Bseek(f, import0, 0)
obj.Bseek(f, import0, 0)
ldpkg(f, pkg, import1-import0-2, pn, whence) // -2 for !\n
Bseek(f, import1, 0)
obj.Bseek(f, import1, 0)
ldobjfile(Ctxt, f, pkg, eof-Boffset(f), pn)
ldobjfile(Ctxt, f, pkg, eof-obj.Boffset(f), pn)
}
func ldshlibsyms(shlib string) {
......@@ -1146,7 +1146,7 @@ func ldshlibsyms(shlib string) {
}
if Ctxt.Debugvlog > 1 && Ctxt.Bso != nil {
fmt.Fprintf(Ctxt.Bso, "%5.2f ldshlibsyms: found library with name %s at %s\n", obj.Cputime(), shlib, libpath)
Bflush(Ctxt.Bso)
Ctxt.Bso.Flush()
}
f, err := elf.Open(libpath)
......@@ -1739,7 +1739,7 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
if Debug['v'] != 0 || Debug['n'] != 0 {
fmt.Fprintf(&Bso, "%5.2f symsize = %d\n", obj.Cputime(), uint32(Symsize))
}
Bflush(&Bso)
Bso.Flush()
}
func Symaddr(s *LSym) int64 {
......
......@@ -30,7 +30,10 @@
package ld
import "encoding/binary"
import (
"cmd/internal/obj"
"encoding/binary"
)
type LSym struct {
Name string
......@@ -106,7 +109,7 @@ type Link struct {
Arch *LinkArch
Debugasm int32
Debugvlog int32
Bso *Biobuf
Bso *obj.Biobuf
Windows int32
Goroot string
Hash map[symVer]*LSym
......
......@@ -18,15 +18,15 @@ const (
endmagic = "\xff\xffgo13ld"
)
func ldobjfile(ctxt *Link, f *Biobuf, pkg string, length int64, pn string) {
start := Boffset(f)
func ldobjfile(ctxt *Link, f *obj.Biobuf, pkg string, length int64, pn string) {
start := obj.Boffset(f)
ctxt.Version++
var buf [8]uint8
Bread(f, buf[:])
obj.Bread(f, buf[:])
if string(buf[:]) != startmagic {
log.Fatalf("%s: invalid file start %x %x %x %x %x %x %x %x", pn, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7])
}
c := Bgetc(f)
c := obj.Bgetc(f)
if c != 1 {
log.Fatalf("%s: invalid file version number %d", pn, c)
}
......@@ -41,7 +41,7 @@ func ldobjfile(ctxt *Link, f *Biobuf, pkg string, length int64, pn string) {
}
for {
c, err := f.r.Peek(1)
c, err := f.Peek(1)
if err != nil {
log.Fatalf("%s: peeking: %v", pn, err)
}
......@@ -52,20 +52,20 @@ func ldobjfile(ctxt *Link, f *Biobuf, pkg string, length int64, pn string) {
}
buf = [8]uint8{}
Bread(f, buf[:])
obj.Bread(f, buf[:])
if string(buf[:]) != endmagic {
log.Fatalf("%s: invalid file end", pn)
}
if Boffset(f) != start+length {
log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(Boffset(f)), int64(start+length))
if obj.Boffset(f) != start+length {
log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(obj.Boffset(f)), int64(start+length))
}
}
var readsym_ndup int
func readsym(ctxt *Link, f *Biobuf, pkg string, pn string) {
if Bgetc(f) != 0xfe {
func readsym(ctxt *Link, f *obj.Biobuf, pkg string, pn string) {
if obj.Bgetc(f) != 0xfe {
log.Fatalf("readsym out of sync")
}
t := int(rdint(f))
......@@ -278,7 +278,7 @@ overwrite:
}
}
func rdint(f *Biobuf) int64 {
func rdint(f *obj.Biobuf) int64 {
var c int
uv := uint64(0)
......@@ -286,7 +286,7 @@ func rdint(f *Biobuf) int64 {
if shift >= 64 {
log.Fatalf("corrupt input")
}
c = Bgetc(f)
c = obj.Bgetc(f)
uv |= uint64(c&0x7F) << uint(shift)
if c&0x80 == 0 {
break
......@@ -296,23 +296,23 @@ func rdint(f *Biobuf) int64 {
return int64(uv>>1) ^ (int64(uint64(uv)<<63) >> 63)
}
func rdstring(f *Biobuf) string {
func rdstring(f *obj.Biobuf) string {
n := rdint(f)
p := make([]byte, n)
Bread(f, p)
obj.Bread(f, p)
return string(p)
}
func rddata(f *Biobuf) []byte {
func rddata(f *obj.Biobuf) []byte {
n := rdint(f)
p := make([]byte, n)
Bread(f, p)
obj.Bread(f, p)
return p
}
var symbuf []byte
func rdsym(ctxt *Link, f *Biobuf, pkg string) *LSym {
func rdsym(ctxt *Link, f *obj.Biobuf, pkg string) *LSym {
n := int(rdint(f))
if n == 0 {
rdint(f)
......@@ -322,7 +322,7 @@ func rdsym(ctxt *Link, f *Biobuf, pkg string) *LSym {
if len(symbuf) < n {
symbuf = make([]byte, n)
}
Bread(f, symbuf[:n])
obj.Bread(f, symbuf[:n])
p := string(symbuf[:n])
v := int(rdint(f))
if v != 0 {
......
......@@ -49,7 +49,7 @@ func Ldmain() {
Ctxt.Diag = Diag
Ctxt.Bso = &Bso
Bso = *Binitw(os.Stdout)
Bso = *obj.Binitw(os.Stdout)
Debug = [128]int{}
nerrors = 0
outfile = ""
......@@ -186,7 +186,7 @@ func Ldmain() {
if Debug['v'] != 0 {
fmt.Fprintf(&Bso, "HEADER = -H%d -T0x%x -D0x%x -R0x%x\n", HEADTYPE, uint64(INITTEXT), uint64(INITDAT), uint32(INITRND))
}
Bflush(&Bso)
Bso.Flush()
if Buildmode == BuildmodeShared {
for i := 0; i < flag.NArg(); i++ {
......@@ -247,7 +247,7 @@ func Ldmain() {
fmt.Fprintf(&Bso, "%d liveness data\n", liveness)
}
Bflush(&Bso)
Bso.Flush()
errorexit()
}
......@@ -5,10 +5,8 @@
package ld
import (
"bufio"
"bytes"
"encoding/binary"
"io"
"log"
"os"
"runtime"
......@@ -76,141 +74,6 @@ func cutStringAtNUL(s string) string {
return s
}
type Biobuf struct {
f *os.File
r *bufio.Reader
w *bufio.Writer
linelen int
}
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 *os.File) *Biobuf {
return &Biobuf{w: bufio.NewWriter(w), f: w}
}
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.Panicf("seeking in output [%d %d %p]: %v", offset, whence, b.f, 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 Bwrite(b *Biobuf, p []byte) (int, error) {
return b.w.Write(p)
}
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 Bgetrune(b *Biobuf) int {
r, _, err := b.r.ReadRune()
if err != nil {
return -1
}
return int(r)
}
func (b *Biobuf) Read(p []byte) (int, error) {
return b.r.Read(p)
}
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 Brdstr(b *Biobuf, delim int, cut int) string {
s, err := b.r.ReadString(byte(delim))
if err != nil {
log.Fatalf("reading input: %v", err)
}
if len(s) > 0 && cut > 0 {
s = s[:len(s)-1]
}
return s
}
func Access(name string, mode int) int {
if mode != 0 {
panic("bad access")
......@@ -222,26 +85,6 @@ func Access(name string, mode int) int {
return 0
}
func Blinelen(b *Biobuf) int {
return b.linelen
}
func Bflush(b *Biobuf) error {
return b.w.Flush()
}
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
}
// strings.Compare, introduced in Go 1.5.
func stringsCompare(a, b string) int {
if a == b {
......
......@@ -83,12 +83,17 @@ func Bseek(b *Biobuf, offset int64, whence int) int64 {
}
func Boffset(b *Biobuf) int64 {
if err := b.w.Flush(); err != nil {
log.Fatalf("writing output: %v", err)
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: %v", err)
log.Fatalf("seeking in output [0, 1]: %v", err)
}
if b.r != nil {
off -= int64(b.r.Buffered())
}
return off
}
......
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