Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/compile/internal/importer/exportdata.go

Documentation: cmd/compile/internal/importer

     1  // UNREVIEWED
     2  // Copyright 2011 The Go Authors. All rights reserved.
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // This file implements FindExportData.
     7  
     8  package importer
     9  
    10  import (
    11  	"bufio"
    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 = fmt.Errorf("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. The hdr result
    43  // is the string before the export data, either "$$" or "$$B".
    44  //
    45  func FindExportData(r *bufio.Reader) (hdr string, err error) {
    46  	// Read first line to make sure this is an object file.
    47  	line, err := r.ReadSlice('\n')
    48  	if err != nil {
    49  		err = fmt.Errorf("can't find export data (%v)", err)
    50  		return
    51  	}
    52  
    53  	if string(line) == "!<arch>\n" {
    54  		// Archive file. Scan to __.PKGDEF.
    55  		var name string
    56  		if name, _, err = readGopackHeader(r); err != nil {
    57  			return
    58  		}
    59  
    60  		// First entry should be __.PKGDEF.
    61  		if name != "__.PKGDEF" {
    62  			err = fmt.Errorf("go archive is missing __.PKGDEF")
    63  			return
    64  		}
    65  
    66  		// Read first line of __.PKGDEF data, so that line
    67  		// is once again the first line of the input.
    68  		if line, err = r.ReadSlice('\n'); err != nil {
    69  			err = fmt.Errorf("can't find export data (%v)", err)
    70  			return
    71  		}
    72  	}
    73  
    74  	// Now at __.PKGDEF in archive or still at beginning of file.
    75  	// Either way, line should begin with "go object ".
    76  	if !strings.HasPrefix(string(line), "go object ") {
    77  		err = fmt.Errorf("not a Go object file")
    78  		return
    79  	}
    80  
    81  	// Skip over object header to export data.
    82  	// Begins after first line starting with $$.
    83  	for line[0] != '$' {
    84  		if line, err = r.ReadSlice('\n'); err != nil {
    85  			err = fmt.Errorf("can't find export data (%v)", err)
    86  			return
    87  		}
    88  	}
    89  	hdr = string(line)
    90  
    91  	return
    92  }
    93  

View as plain text