aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/oldlink/internal/ld/outbuf.go
blob: 596d2395c8f5715cf3b54cb7405ddd27d4389c6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2017 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 (
	"bufio"
	"cmd/internal/sys"
	"cmd/oldlink/internal/sym"
	"encoding/binary"
	"log"
	"os"
)

// OutBuf is a buffered file writer.
//
// It is simlar to the Writer in cmd/internal/bio with a few small differences.
//
// First, it tracks the output architecture and uses it to provide
// endian helpers.
//
// Second, it provides a very cheap offset counter that doesn't require
// any system calls to read the value.
//
// It also mmaps the output file (if available). The intended usage is:
// - Mmap the output file
// - Write the content
// - possibly apply any edits in the output buffer
// - Munmap the output file
// - possibly write more content to the file, which will not be edited later.
type OutBuf struct {
	arch   *sys.Arch
	off    int64
	w      *bufio.Writer
	buf    []byte // backing store of mmap'd output file
	f      *os.File
	encbuf [8]byte // temp buffer used by WriteN methods
}

func (out *OutBuf) SeekSet(p int64) {
	if p == out.off {
		return
	}
	if out.buf == nil {
		out.Flush()
		if _, err := out.f.Seek(p, 0); err != nil {
			Exitf("seeking to %d in %s: %v", p, out.f.Name(), err)
		}
	}
	out.off = p
}

func (out *OutBuf) Offset() int64 {
	return out.off
}

// Write writes the contents of v to the buffer.
//
// As Write is backed by a bufio.Writer, callers do not have
// to explicitly handle the returned error as long as Flush is
// eventually called.
func (out *OutBuf) Write(v []byte) (int, error) {
	if out.buf != nil {
		n := copy(out.buf[out.off:], v)
		out.off += int64(n)
		return n, nil
	}
	n, err := out.w.Write(v)
	out.off += int64(n)
	return n, err
}

func (out *OutBuf) Write8(v uint8) {
	if out.buf != nil {
		out.buf[out.off] = v
		out.off++
		return
	}
	if err := out.w.WriteByte(v); err == nil {
		out.off++
	}
}

// WriteByte is an alias for Write8 to fulfill the io.ByteWriter interface.
func (out *OutBuf) WriteByte(v byte) error {
	out.Write8(v)
	return nil
}

func (out *OutBuf) Write16(v uint16) {
	out.arch.ByteOrder.PutUint16(out.encbuf[:], v)
	out.Write(out.encbuf[:2])
}

func (out *OutBuf) Write32(v uint32) {
	out.arch.ByteOrder.PutUint32(out.encbuf[:], v)
	out.Write(out.encbuf[:4])
}

func (out *OutBuf) Write32b(v uint32) {
	binary.BigEndian.PutUint32(out.encbuf[:], v)
	out.Write(out.encbuf[:4])
}

func (out *OutBuf) Write64(v uint64) {
	out.arch.ByteOrder.PutUint64(out.encbuf[:], v)
	out.Write(out.encbuf[:8])
}

func (out *OutBuf) Write64b(v uint64) {
	binary.BigEndian.PutUint64(out.encbuf[:], v)
	out.Write(out.encbuf[:8])
}

func (out *OutBuf) WriteString(s string) {
	if out.buf != nil {
		n := copy(out.buf[out.off:], s)
		if n != len(s) {
			log.Fatalf("WriteString truncated. buffer size: %d, offset: %d, len(s)=%d", len(out.buf), out.off, len(s))
		}
		out.off += int64(n)
		return
	}
	n, _ := out.w.WriteString(s)
	out.off += int64(n)
}

// WriteStringN writes the first n bytes of s.
// If n is larger than len(s) then it is padded with zero bytes.
func (out *OutBuf) WriteStringN(s string, n int) {
	out.WriteStringPad(s, n, zeros[:])
}

// WriteStringPad writes the first n bytes of s.
// If n is larger than len(s) then it is padded with the bytes in pad (repeated as needed).
func (out *OutBuf) WriteStringPad(s string, n int, pad []byte) {
	if len(s) >= n {
		out.WriteString(s[:n])
	} else {
		out.WriteString(s)
		n -= len(s)
		for n > len(pad) {
			out.Write(pad)
			n -= len(pad)

		}
		out.Write(pad[:n])
	}
}

// WriteSym writes the content of a Symbol, then changes the Symbol's content
// to point to the output buffer that we just wrote, so we can apply further
// edit to the symbol content.
// If the output file is not Mmap'd, just writes the content.
func (out *OutBuf) WriteSym(s *sym.Symbol) {
	if out.buf != nil {
		start := out.off
		out.Write(s.P)
		s.P = out.buf[start:out.off]
		s.Attr.Set(sym.AttrReadOnly, false)
	} else {
		out.Write(s.P)
	}
}

func (out *OutBuf) Flush() {
	var err error
	if out.buf != nil {
		err = out.Msync()
	} else {
		err = out.w.Flush()
	}
	if err != nil {
		Exitf("flushing %s: %v", out.f.Name(), err)
	}
}