github.com/zxy12/go_duplicate_1_12@v0.0.0-20200217043740-b1636fc0368b/src/cmd/internal/obj/plist.go (about)

     1  // Copyright 2013 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 obj
     6  
     7  import (
     8  	"cmd/internal/objabi"
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  type Plist struct {
    14  	Firstpc *Prog
    15  	Curfn   interface{} // holds a *gc.Node, if non-nil
    16  }
    17  
    18  // ProgAlloc is a function that allocates Progs.
    19  // It is used to provide access to cached/bulk-allocated Progs to the assemblers.
    20  type ProgAlloc func() *Prog
    21  
    22  func Flushplist(ctxt *Link, plist *Plist, newprog ProgAlloc, myimportpath string) {
    23  	// Build list of symbols, and assign instructions to lists.
    24  	var curtext *LSym
    25  	var etext *Prog
    26  	var text []*LSym
    27  
    28  	var plink *Prog
    29  	for p := plist.Firstpc; p != nil; p = plink {
    30  		if ctxt.Debugasm > 0 && ctxt.Debugvlog {
    31  			fmt.Printf("obj: %v\n", p)
    32  		}
    33  		plink = p.Link
    34  		p.Link = nil
    35  
    36  		switch p.As {
    37  		case AEND:
    38  			continue
    39  
    40  		case ATEXT:
    41  			s := p.From.Sym
    42  			if s == nil {
    43  				// func _() { }
    44  				curtext = nil
    45  				continue
    46  			}
    47  			text = append(text, s)
    48  			etext = p
    49  			curtext = s
    50  			continue
    51  
    52  		case AFUNCDATA:
    53  			// Rewrite reference to go_args_stackmap(SB) to the Go-provided declaration information.
    54  			if curtext == nil { // func _() {}
    55  				continue
    56  			}
    57  			if p.To.Sym.Name == "go_args_stackmap" {
    58  				if p.From.Type != TYPE_CONST || p.From.Offset != objabi.FUNCDATA_ArgsPointerMaps {
    59  					ctxt.Diag("FUNCDATA use of go_args_stackmap(SB) without FUNCDATA_ArgsPointerMaps")
    60  				}
    61  				p.To.Sym = ctxt.LookupDerived(curtext, curtext.Name+".args_stackmap")
    62  			}
    63  
    64  		}
    65  
    66  		if curtext == nil {
    67  			etext = nil
    68  			continue
    69  		}
    70  		etext.Link = p
    71  		etext = p
    72  	}
    73  
    74  	if newprog == nil {
    75  		newprog = ctxt.NewProg
    76  	}
    77  
    78  	// Add reference to Go arguments for C or assembly functions without them.
    79  	for _, s := range text {
    80  		if !strings.HasPrefix(s.Name, "\"\".") {
    81  			continue
    82  		}
    83  		found := false
    84  		for p := s.Func.Text; p != nil; p = p.Link {
    85  			if p.As == AFUNCDATA && p.From.Type == TYPE_CONST && p.From.Offset == objabi.FUNCDATA_ArgsPointerMaps {
    86  				found = true
    87  				break
    88  			}
    89  		}
    90  
    91  		if !found {
    92  			p := Appendp(s.Func.Text, newprog)
    93  			p.As = AFUNCDATA
    94  			p.From.Type = TYPE_CONST
    95  			p.From.Offset = objabi.FUNCDATA_ArgsPointerMaps
    96  			p.To.Type = TYPE_MEM
    97  			p.To.Name = NAME_EXTERN
    98  			p.To.Sym = ctxt.LookupDerived(s, s.Name+".args_stackmap")
    99  		}
   100  	}
   101  
   102  	// Turn functions into machine code images.
   103  	for _, s := range text {
   104  		mkfwd(s)
   105  		linkpatch(ctxt, s, newprog)
   106  		ctxt.Arch.Preprocess(ctxt, s, newprog)
   107  		ctxt.Arch.Assemble(ctxt, s, newprog)
   108  		linkpcln(ctxt, s)
   109  		ctxt.populateDWARF(plist.Curfn, s, myimportpath)
   110  	}
   111  }
   112  
   113  func (ctxt *Link) InitTextSym(s *LSym, flag int) {
   114  	if s == nil {
   115  		// func _() { }
   116  		return
   117  	}
   118  	if s.Func != nil {
   119  		ctxt.Diag("InitTextSym double init for %s", s.Name)
   120  	}
   121  	s.Func = new(FuncInfo)
   122  	if s.OnList() {
   123  		ctxt.Diag("symbol %s listed multiple times", s.Name)
   124  	}
   125  	s.Set(AttrOnList, true)
   126  	s.Set(AttrDuplicateOK, flag&DUPOK != 0)
   127  	s.Set(AttrNoSplit, flag&NOSPLIT != 0)
   128  	s.Set(AttrReflectMethod, flag&REFLECTMETHOD != 0)
   129  	s.Set(AttrWrapper, flag&WRAPPER != 0)
   130  	s.Set(AttrNeedCtxt, flag&NEEDCTXT != 0)
   131  	s.Set(AttrNoFrame, flag&NOFRAME != 0)
   132  	s.Type = objabi.STEXT
   133  	ctxt.Text = append(ctxt.Text, s)
   134  
   135  	// Set up DWARF entries for s.
   136  	info, loc, ranges, _, isstmt := ctxt.dwarfSym(s)
   137  	info.Type = objabi.SDWARFINFO
   138  	info.Set(AttrDuplicateOK, s.DuplicateOK())
   139  	if loc != nil {
   140  		loc.Type = objabi.SDWARFLOC
   141  		loc.Set(AttrDuplicateOK, s.DuplicateOK())
   142  		ctxt.Data = append(ctxt.Data, loc)
   143  	}
   144  	ranges.Type = objabi.SDWARFRANGE
   145  	ranges.Set(AttrDuplicateOK, s.DuplicateOK())
   146  	ctxt.Data = append(ctxt.Data, info, ranges)
   147  	isstmt.Type = objabi.SDWARFMISC
   148  	isstmt.Set(AttrDuplicateOK, s.DuplicateOK())
   149  	ctxt.Data = append(ctxt.Data, isstmt)
   150  }
   151  
   152  func (ctxt *Link) Globl(s *LSym, size int64, flag int) {
   153  	if s.SeenGlobl() {
   154  		fmt.Printf("duplicate %v\n", s)
   155  	}
   156  	s.Set(AttrSeenGlobl, true)
   157  	if s.OnList() {
   158  		ctxt.Diag("symbol %s listed multiple times", s.Name)
   159  	}
   160  	s.Set(AttrOnList, true)
   161  	ctxt.Data = append(ctxt.Data, s)
   162  	s.Size = size
   163  	if s.Type == 0 {
   164  		s.Type = objabi.SBSS
   165  	}
   166  	if flag&DUPOK != 0 {
   167  		s.Set(AttrDuplicateOK, true)
   168  	}
   169  	if flag&RODATA != 0 {
   170  		s.Type = objabi.SRODATA
   171  	} else if flag&NOPTR != 0 {
   172  		if s.Type == objabi.SDATA {
   173  			s.Type = objabi.SNOPTRDATA
   174  		} else {
   175  			s.Type = objabi.SNOPTRBSS
   176  		}
   177  	} else if flag&TLSBSS != 0 {
   178  		s.Type = objabi.STLSBSS
   179  	}
   180  }
   181  
   182  // EmitEntryLiveness generates PCDATA Progs after p to switch to the
   183  // liveness map active at the entry of function s. It returns the last
   184  // Prog generated.
   185  func (ctxt *Link) EmitEntryLiveness(s *LSym, p *Prog, newprog ProgAlloc) *Prog {
   186  	pcdata := Appendp(p, newprog)
   187  	pcdata.Pos = s.Func.Text.Pos
   188  	pcdata.As = APCDATA
   189  	pcdata.From.Type = TYPE_CONST
   190  	pcdata.From.Offset = objabi.PCDATA_StackMapIndex
   191  	pcdata.To.Type = TYPE_CONST
   192  	pcdata.To.Offset = -1 // pcdata starts at -1 at function entry
   193  
   194  	// Same, with register map.
   195  	pcdata = Appendp(pcdata, newprog)
   196  	pcdata.Pos = s.Func.Text.Pos
   197  	pcdata.As = APCDATA
   198  	pcdata.From.Type = TYPE_CONST
   199  	pcdata.From.Offset = objabi.PCDATA_RegMapIndex
   200  	pcdata.To.Type = TYPE_CONST
   201  	pcdata.To.Offset = -1
   202  
   203  	return pcdata
   204  }