github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/go/internal/gcimporter/exportdata.go (about)

     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.
     4  
     5  // This file implements FindExportData.
     6  
     7  package gcimporter
     8  
     9  import (
    10  	"bufio"
    11  	"errors"
    12  	"fmt"
    13  	"io"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  func readGopackHeader(r *bufio.Reader) (name string, size int, err error) {
    19  	// See $GOROOT/include/ar.h.
    20  	hdr := make([]byte, 16+12+6+6+8+10+2)
    21  	_, err = io.ReadFull(r, hdr)
    22  	if err != nil {
    23  		return
    24  	}
    25  	// leave for debugging
    26  	if false {
    27  		fmt.Printf("header: %s", hdr)
    28  	}
    29  	s := strings.TrimSpace(string(hdr[16+12+6+6+8:][:10]))
    30  	size, err = strconv.Atoi(s)
    31  	if err != nil || hdr[len(hdr)-2] != '`' || hdr[len(hdr)-1] != '\n' {
    32  		err = errors.New("invalid archive header")
    33  		return
    34  	}
    35  	name = strings.TrimSpace(string(hdr[:16]))
    36  	return
    37  }
    38  
    39  // FindExportData positions the reader r at the beginning of the
    40  // export data section of an underlying GC-created object/archive
    41  // file by reading from it. The reader must be positioned at the
    42  // start of the file before calling this function.
    43  //
    44  func FindExportData(r *bufio.Reader) (err error) {
    45  	// Read first line to make sure this is an object file.
    46  	line, err := r.ReadSlice('\n')
    47  	if err != nil {
    48  		return
    49  	}
    50  	if string(line) == "!<arch>\n" {
    51  		// Archive file. Scan to __.PKGDEF.
    52  		var name string
    53  		var size int
    54  		if name, size, err = readGopackHeader(r); err != nil {
    55  			return
    56  		}
    57  
    58  		// Optional leading __.GOSYMDEF or __.SYMDEF.
    59  		// Read and discard.
    60  		if name == "__.SYMDEF" || name == "__.GOSYMDEF" {
    61  			const block = 4096
    62  			tmp := make([]byte, block)
    63  			for size > 0 {
    64  				n := size
    65  				if n > block {
    66  					n = block
    67  				}
    68  				if _, err = io.ReadFull(r, tmp[:n]); err != nil {
    69  					return
    70  				}
    71  				size -= n
    72  			}
    73  
    74  			if name, _, err = readGopackHeader(r); err != nil {
    75  				return
    76  			}
    77  		}
    78  
    79  		// First real entry should be __.PKGDEF.
    80  		if name != "__.PKGDEF" {
    81  			err = errors.New("go archive is missing __.PKGDEF")
    82  			return
    83  		}
    84  
    85  		// Read first line of __.PKGDEF data, so that line
    86  		// is once again the first line of the input.
    87  		if line, err = r.ReadSlice('\n'); err != nil {
    88  			return
    89  		}
    90  	}
    91  
    92  	// Now at __.PKGDEF in archive or still at beginning of file.
    93  	// Either way, line should begin with "go object ".
    94  	if !strings.HasPrefix(string(line), "go object ") {
    95  		err = errors.New("not a go object file")
    96  		return
    97  	}
    98  
    99  	// Skip over object header to export data.
   100  	// Begins after first line with $$.
   101  	for line[0] != '$' {
   102  		if line, err = r.ReadSlice('\n'); err != nil {
   103  			return
   104  		}
   105  	}
   106  
   107  	return
   108  }