github.com/eun/go@v0.0.0-20170811110501-92cfd07a6cfd/src/cmd/link/internal/ld/ar.go (about)

     1  // Inferno utils/include/ar.h
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/include/ar.h
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package ld
    32  
    33  import (
    34  	"cmd/internal/bio"
    35  	"cmd/internal/objabi"
    36  	"encoding/binary"
    37  	"fmt"
    38  	"io"
    39  	"os"
    40  )
    41  
    42  const (
    43  	SARMAG  = 8
    44  	SAR_HDR = 16 + 44
    45  )
    46  
    47  const (
    48  	ARMAG = "!<arch>\n"
    49  )
    50  
    51  type ArHdr struct {
    52  	name string
    53  	date string
    54  	uid  string
    55  	gid  string
    56  	mode string
    57  	size string
    58  	fmag string
    59  }
    60  
    61  // hostArchive reads an archive file holding host objects and links in
    62  // required objects. The general format is the same as a Go archive
    63  // file, but it has an armap listing symbols and the objects that
    64  // define them. This is used for the compiler support library
    65  // libgcc.a.
    66  func hostArchive(ctxt *Link, name string) {
    67  	f, err := bio.Open(name)
    68  	if err != nil {
    69  		if os.IsNotExist(err) {
    70  			// It's OK if we don't have a libgcc file at all.
    71  			if ctxt.Debugvlog != 0 {
    72  				ctxt.Logf("skipping libgcc file: %v\n", err)
    73  			}
    74  			return
    75  		}
    76  		Exitf("cannot open file %s: %v", name, err)
    77  	}
    78  	defer f.Close()
    79  
    80  	var magbuf [len(ARMAG)]byte
    81  	if _, err := io.ReadFull(f, magbuf[:]); err != nil {
    82  		Exitf("file %s too short", name)
    83  	}
    84  
    85  	if string(magbuf[:]) != ARMAG {
    86  		Exitf("%s is not an archive file", name)
    87  	}
    88  
    89  	var arhdr ArHdr
    90  	l := nextar(f, f.Offset(), &arhdr)
    91  	if l <= 0 {
    92  		Exitf("%s missing armap", name)
    93  	}
    94  
    95  	var armap archiveMap
    96  	if arhdr.name == "/" || arhdr.name == "/SYM64/" {
    97  		armap = readArmap(name, f, arhdr)
    98  	} else {
    99  		Exitf("%s missing armap", name)
   100  	}
   101  
   102  	loaded := make(map[uint64]bool)
   103  	any := true
   104  	for any {
   105  		var load []uint64
   106  		for _, s := range ctxt.Syms.Allsym {
   107  			for _, r := range s.R {
   108  				if r.Sym != nil && r.Sym.Type&SMASK == SXREF {
   109  					if off := armap[r.Sym.Name]; off != 0 && !loaded[off] {
   110  						load = append(load, off)
   111  						loaded[off] = true
   112  					}
   113  				}
   114  			}
   115  		}
   116  
   117  		for _, off := range load {
   118  			l := nextar(f, int64(off), &arhdr)
   119  			if l <= 0 {
   120  				Exitf("%s missing archive entry at offset %d", name, off)
   121  			}
   122  			pname := fmt.Sprintf("%s(%s)", name, arhdr.name)
   123  			l = atolwhex(arhdr.size)
   124  
   125  			libgcc := Library{Pkg: "libgcc"}
   126  			h := ldobj(ctxt, f, &libgcc, l, pname, name, ArchiveObj)
   127  			f.Seek(h.off, 0)
   128  			h.ld(ctxt, f, h.pkg, h.length, h.pn)
   129  		}
   130  
   131  		any = len(load) > 0
   132  	}
   133  }
   134  
   135  // archiveMap is an archive symbol map: a mapping from symbol name to
   136  // offset within the archive file.
   137  type archiveMap map[string]uint64
   138  
   139  // readArmap reads the archive symbol map.
   140  func readArmap(filename string, f *bio.Reader, arhdr ArHdr) archiveMap {
   141  	is64 := arhdr.name == "/SYM64/"
   142  	wordSize := 4
   143  	if is64 {
   144  		wordSize = 8
   145  	}
   146  
   147  	contents := make([]byte, atolwhex(arhdr.size))
   148  	if _, err := io.ReadFull(f, contents); err != nil {
   149  		Exitf("short read from %s", filename)
   150  	}
   151  
   152  	var c uint64
   153  	if is64 {
   154  		c = binary.BigEndian.Uint64(contents)
   155  	} else {
   156  		c = uint64(binary.BigEndian.Uint32(contents))
   157  	}
   158  	contents = contents[wordSize:]
   159  
   160  	ret := make(archiveMap)
   161  
   162  	names := contents[c*uint64(wordSize):]
   163  	for i := uint64(0); i < c; i++ {
   164  		n := 0
   165  		for names[n] != 0 {
   166  			n++
   167  		}
   168  		name := string(names[:n])
   169  		names = names[n+1:]
   170  
   171  		// For Mach-O and PE/386 files we strip a leading
   172  		// underscore from the symbol name.
   173  		if objabi.GOOS == "darwin" || (objabi.GOOS == "windows" && objabi.GOARCH == "386") {
   174  			if name[0] == '_' && len(name) > 1 {
   175  				name = name[1:]
   176  			}
   177  		}
   178  
   179  		var off uint64
   180  		if is64 {
   181  			off = binary.BigEndian.Uint64(contents)
   182  		} else {
   183  			off = uint64(binary.BigEndian.Uint32(contents))
   184  		}
   185  		contents = contents[wordSize:]
   186  
   187  		ret[name] = off
   188  	}
   189  
   190  	return ret
   191  }