...
Run Format

Source file src/cmd/link/internal/ld/ar.go

Documentation: cmd/link/internal/ld

     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  	"cmd/link/internal/sym"
    37  	"encoding/binary"
    38  	"fmt"
    39  	"io"
    40  	"os"
    41  )
    42  
    43  const (
    44  	SARMAG  = 8
    45  	SAR_HDR = 16 + 44
    46  )
    47  
    48  const (
    49  	ARMAG = "!<arch>\n"
    50  )
    51  
    52  type ArHdr struct {
    53  	name string
    54  	date string
    55  	uid  string
    56  	gid  string
    57  	mode string
    58  	size string
    59  	fmag string
    60  }
    61  
    62  // hostArchive reads an archive file holding host objects and links in
    63  // required objects. The general format is the same as a Go archive
    64  // file, but it has an armap listing symbols and the objects that
    65  // define them. This is used for the compiler support library
    66  // libgcc.a.
    67  func hostArchive(ctxt *Link, name string) {
    68  	f, err := bio.Open(name)
    69  	if err != nil {
    70  		if os.IsNotExist(err) {
    71  			// It's OK if we don't have a libgcc file at all.
    72  			if ctxt.Debugvlog != 0 {
    73  				ctxt.Logf("skipping libgcc file: %v\n", err)
    74  			}
    75  			return
    76  		}
    77  		Exitf("cannot open file %s: %v", name, err)
    78  	}
    79  	defer f.Close()
    80  
    81  	var magbuf [len(ARMAG)]byte
    82  	if _, err := io.ReadFull(f, magbuf[:]); err != nil {
    83  		Exitf("file %s too short", name)
    84  	}
    85  
    86  	if string(magbuf[:]) != ARMAG {
    87  		Exitf("%s is not an archive file", name)
    88  	}
    89  
    90  	var arhdr ArHdr
    91  	l := nextar(f, f.Offset(), &arhdr)
    92  	if l <= 0 {
    93  		Exitf("%s missing armap", name)
    94  	}
    95  
    96  	var armap archiveMap
    97  	if arhdr.name == "/" || arhdr.name == "/SYM64/" {
    98  		armap = readArmap(name, f, arhdr)
    99  	} else {
   100  		Exitf("%s missing armap", name)
   101  	}
   102  
   103  	loaded := make(map[uint64]bool)
   104  	any := true
   105  	for any {
   106  		var load []uint64
   107  		for _, s := range ctxt.Syms.Allsym {
   108  			for _, r := range s.R {
   109  				if r.Sym != nil && r.Sym.Type == sym.SXREF {
   110  					if off := armap[r.Sym.Name]; off != 0 && !loaded[off] {
   111  						load = append(load, off)
   112  						loaded[off] = true
   113  					}
   114  				}
   115  			}
   116  		}
   117  
   118  		for _, off := range load {
   119  			l := nextar(f, int64(off), &arhdr)
   120  			if l <= 0 {
   121  				Exitf("%s missing archive entry at offset %d", name, off)
   122  			}
   123  			pname := fmt.Sprintf("%s(%s)", name, arhdr.name)
   124  			l = atolwhex(arhdr.size)
   125  
   126  			libgcc := sym.Library{Pkg: "libgcc"}
   127  			h := ldobj(ctxt, f, &libgcc, l, pname, name, ArchiveObj)
   128  			f.Seek(h.off, 0)
   129  			h.ld(ctxt, f, h.pkg, h.length, h.pn)
   130  		}
   131  
   132  		any = len(load) > 0
   133  	}
   134  }
   135  
   136  // archiveMap is an archive symbol map: a mapping from symbol name to
   137  // offset within the archive file.
   138  type archiveMap map[string]uint64
   139  
   140  // readArmap reads the archive symbol map.
   141  func readArmap(filename string, f *bio.Reader, arhdr ArHdr) archiveMap {
   142  	is64 := arhdr.name == "/SYM64/"
   143  	wordSize := 4
   144  	if is64 {
   145  		wordSize = 8
   146  	}
   147  
   148  	contents := make([]byte, atolwhex(arhdr.size))
   149  	if _, err := io.ReadFull(f, contents); err != nil {
   150  		Exitf("short read from %s", filename)
   151  	}
   152  
   153  	var c uint64
   154  	if is64 {
   155  		c = binary.BigEndian.Uint64(contents)
   156  	} else {
   157  		c = uint64(binary.BigEndian.Uint32(contents))
   158  	}
   159  	contents = contents[wordSize:]
   160  
   161  	ret := make(archiveMap)
   162  
   163  	names := contents[c*uint64(wordSize):]
   164  	for i := uint64(0); i < c; i++ {
   165  		n := 0
   166  		for names[n] != 0 {
   167  			n++
   168  		}
   169  		name := string(names[:n])
   170  		names = names[n+1:]
   171  
   172  		// For Mach-O and PE/386 files we strip a leading
   173  		// underscore from the symbol name.
   174  		if objabi.GOOS == "darwin" || (objabi.GOOS == "windows" && objabi.GOARCH == "386") {
   175  			if name[0] == '_' && len(name) > 1 {
   176  				name = name[1:]
   177  			}
   178  		}
   179  
   180  		var off uint64
   181  		if is64 {
   182  			off = binary.BigEndian.Uint64(contents)
   183  		} else {
   184  			off = uint64(binary.BigEndian.Uint32(contents))
   185  		}
   186  		contents = contents[wordSize:]
   187  
   188  		ret[name] = off
   189  	}
   190  
   191  	return ret
   192  }
   193  

View as plain text