github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/link/internal/ld/typelink.go (about)

     1  // Copyright 2016 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  package ld
     6  
     7  import (
     8  	"github.com/gagliardetto/golang-go/cmd/internal/objabi"
     9  	"github.com/gagliardetto/golang-go/cmd/link/internal/sym"
    10  	"sort"
    11  )
    12  
    13  type byTypeStr []typelinkSortKey
    14  
    15  type typelinkSortKey struct {
    16  	TypeStr string
    17  	Type    *sym.Symbol
    18  }
    19  
    20  func (s byTypeStr) Less(i, j int) bool { return s[i].TypeStr < s[j].TypeStr }
    21  func (s byTypeStr) Len() int           { return len(s) }
    22  func (s byTypeStr) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    23  
    24  // typelink generates the typelink table which is used by reflect.typelinks().
    25  // Types that should be added to the typelinks table are marked with the
    26  // MakeTypelink attribute by the compiler.
    27  func (ctxt *Link) typelink() {
    28  	typelinks := byTypeStr{}
    29  	for _, s := range ctxt.Syms.Allsym {
    30  		if s.Attr.Reachable() && s.Attr.MakeTypelink() {
    31  			typelinks = append(typelinks, typelinkSortKey{decodetypeStr(ctxt.Arch, s), s})
    32  		}
    33  	}
    34  	sort.Sort(typelinks)
    35  
    36  	tl := ctxt.Syms.Lookup("runtime.typelink", 0)
    37  	tl.Type = sym.STYPELINK
    38  	tl.Attr |= sym.AttrReachable | sym.AttrLocal
    39  	tl.Size = int64(4 * len(typelinks))
    40  	tl.P = make([]byte, tl.Size)
    41  	tl.R = make([]sym.Reloc, len(typelinks))
    42  	for i, s := range typelinks {
    43  		r := &tl.R[i]
    44  		r.Sym = s.Type
    45  		r.Off = int32(i * 4)
    46  		r.Siz = 4
    47  		r.Type = objabi.R_ADDROFF
    48  	}
    49  }