aboutsummaryrefslogtreecommitdiffhomepage
path: root/builder/ar.go
blob: c3aad171ebd75692d52ead034f9bccebad3a0c2c (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package builder

import (
	"bytes"
	"debug/elf"
	"debug/pe"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"time"

	"github.com/blakesmith/ar"
)

// makeArchive creates an arcive for static linking from a list of object files
// given as a parameter. It is equivalent to the following command:
//
//	ar -rcs <archivePath> <objs...>
func makeArchive(arfile *os.File, objs []string) error {
	// Open the archive file.
	arwriter := ar.NewWriter(arfile)
	err := arwriter.WriteGlobalHeader()
	if err != nil {
		return &os.PathError{Op: "write ar header", Path: arfile.Name(), Err: err}
	}

	// Open all object files and read the symbols for the symbol table.
	symbolTable := []struct {
		name      string // symbol name
		fileIndex int    // index into objfiles
	}{}
	archiveOffsets := make([]int32, len(objs))
	for i, objpath := range objs {
		objfile, err := os.Open(objpath)
		if err != nil {
			return err
		}

		// Read the symbols and add them to the symbol table.
		if dbg, err := elf.NewFile(objfile); err == nil {
			symbols, err := dbg.Symbols()
			if err != nil {
				return err
			}
			for _, symbol := range symbols {
				bind := elf.ST_BIND(symbol.Info)
				if bind != elf.STB_GLOBAL && bind != elf.STB_WEAK {
					// Don't include local symbols (STB_LOCAL).
					continue
				}
				if elf.ST_TYPE(symbol.Info) != elf.STT_FUNC && elf.ST_TYPE(symbol.Info) != elf.STT_OBJECT {
					// Not a function.
					continue
				}
				// Include in archive.
				symbolTable = append(symbolTable, struct {
					name      string
					fileIndex int
				}{symbol.Name, i})
			}
		} else if dbg, err := pe.NewFile(objfile); err == nil {
			for _, symbol := range dbg.Symbols {
				if symbol.StorageClass != 2 {
					continue
				}
				if symbol.SectionNumber == 0 {
					continue
				}
				symbolTable = append(symbolTable, struct {
					name      string
					fileIndex int
				}{symbol.Name, i})
			}
		} else {
			return fmt.Errorf("failed to open file %s as ELF or PE/COFF: %w", objpath, err)
		}

		// Close file, to avoid issues with too many open files (especially on
		// MacOS X).
		objfile.Close()
	}

	// Create the symbol table buffer.
	// For some (sparse) details on the file format:
	// https://en.wikipedia.org/wiki/Ar_(Unix)#System_V_(or_GNU)_variant
	buf := &bytes.Buffer{}
	binary.Write(buf, binary.BigEndian, int32(len(symbolTable)))
	for range symbolTable {
		// This is a placeholder index, it will be updated after all files have
		// been written to the archive (see the end of this function).
		err = binary.Write(buf, binary.BigEndian, int32(0))
		if err != nil {
			return err
		}
	}
	for _, sym := range symbolTable {
		_, err := buf.Write([]byte(sym.name + "\x00"))
		if err != nil {
			return err
		}
	}
	for buf.Len()%2 != 0 {
		// The symbol table must be aligned.
		// This appears to be required by lld.
		buf.WriteByte(0)
	}

	// Write the symbol table.
	err = arwriter.WriteHeader(&ar.Header{
		Name:    "/",
		ModTime: time.Unix(0, 0),
		Uid:     0,
		Gid:     0,
		Mode:    0,
		Size:    int64(buf.Len()),
	})
	if err != nil {
		return err
	}

	// Keep track of the start of the symbol table.
	symbolTableStart, err := arfile.Seek(0, os.SEEK_CUR)
	if err != nil {
		return err
	}

	// Write symbol table contents.
	_, err = arfile.Write(buf.Bytes())
	if err != nil {
		return err
	}

	// Add all object files to the archive.
	var copyBuf bytes.Buffer
	for i, objpath := range objs {
		objfile, err := os.Open(objpath)
		if err != nil {
			return err
		}
		defer objfile.Close()

		// Store the start index, for when we'll update the symbol table with
		// the correct file start indices.
		offset, err := arfile.Seek(0, os.SEEK_CUR)
		if err != nil {
			return err
		}
		if int64(int32(offset)) != offset {
			return errors.New("large archives (4GB+) not supported: " + arfile.Name())
		}
		archiveOffsets[i] = int32(offset)

		// Write the file header.
		st, err := objfile.Stat()
		if err != nil {
			return err
		}
		err = arwriter.WriteHeader(&ar.Header{
			Name:    filepath.Base(objfile.Name()),
			ModTime: time.Unix(0, 0),
			Uid:     0,
			Gid:     0,
			Mode:    0644,
			Size:    st.Size(),
		})
		if err != nil {
			return err
		}

		// Copy the file contents into the archive.
		// First load all contents into a buffer, then write it all in one go to
		// the archive file. This is a bit complicated, but is necessary because
		// io.Copy can't deal with files that are of an odd size.
		copyBuf.Reset()
		n, err := io.Copy(&copyBuf, objfile)
		if err != nil {
			return fmt.Errorf("could not copy object file into ar file: %w", err)
		}
		if n != st.Size() {
			return errors.New("file modified during ar creation: " + arfile.Name())
		}
		_, err = arwriter.Write(copyBuf.Bytes())
		if err != nil {
			return fmt.Errorf("could not copy object file into ar file: %w", err)
		}

		// File is not needed anymore.
		objfile.Close()
	}

	// Create symbol indices.
	indicesBuf := &bytes.Buffer{}
	for _, sym := range symbolTable {
		err = binary.Write(indicesBuf, binary.BigEndian, archiveOffsets[sym.fileIndex])
		if err != nil {
			return err
		}
	}

	// Overwrite placeholder indices.
	_, err = arfile.WriteAt(indicesBuf.Bytes(), symbolTableStart+4)
	return err
}