1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file is a copy of $GOROOT/src/go/internal/gcimporter/exportdata.go.
7 // This file implements FindExportData.
20 func readGopackHeader(r io.Reader) (name string, size int, err error) {
21 // See $GOROOT/include/ar.h.
22 hdr := make([]byte, 16+12+6+6+8+10+2)
23 _, err = io.ReadFull(r, hdr)
27 // leave for debugging
29 fmt.Printf("header: %s", hdr)
31 s := strings.TrimSpace(string(hdr[16+12+6+6+8:][:10]))
32 size, err = strconv.Atoi(s)
33 if err != nil || hdr[len(hdr)-2] != '`' || hdr[len(hdr)-1] != '\n' {
34 err = fmt.Errorf("invalid archive header")
37 name = strings.TrimSpace(string(hdr[:16]))
41 // findExportData positions the reader r at the beginning of the
42 // export data section of an underlying GC-created object/archive
43 // file by reading from it. The reader must be positioned at the
44 // start of the file before calling this function. The hdr result
45 // is the string before the export data, either "$$" or "$$B".
47 func findExportData(r *bufio.Reader) (hdr string, length int, err error) {
48 // Read first line to make sure this is an object file.
49 line, err := r.ReadSlice('\n')
51 err = fmt.Errorf("can't find export data (%v)", err)
55 if string(line) == "!<arch>\n" {
56 // Archive file. Scan to __.PKGDEF.
58 if name, length, err = readGopackHeader(r); err != nil {
62 // First entry should be __.PKGDEF.
63 if name != "__.PKGDEF" {
64 err = fmt.Errorf("go archive is missing __.PKGDEF")
68 // Read first line of __.PKGDEF data, so that line
69 // is once again the first line of the input.
70 if line, err = r.ReadSlice('\n'); err != nil {
71 err = fmt.Errorf("can't find export data (%v)", err)
77 // Now at __.PKGDEF in archive or still at beginning of file.
78 // Either way, line should begin with "go object ".
79 if !strings.HasPrefix(string(line), "go object ") {
80 err = fmt.Errorf("not a Go object file")
84 // Skip over object header to export data.
85 // Begins after first line starting with $$.
87 if line, err = r.ReadSlice('\n'); err != nil {
88 err = fmt.Errorf("can't find export data (%v)", err)
98 func GetExportData(r io.ReadSeeker, b []byte) ([]byte, error) {
99 br := bufio.NewReader(r)
100 _, length, err := findExportData(br)
104 if _, err := r.Seek(-int64(br.Buffered()), io.SeekCurrent); err != nil {
108 if cap(b) >= length {
111 b = make([]byte, length)
113 _, err := io.ReadFull(r, b)
116 return ioutil.ReadAll(r)