...
Run Format

Source file src/cmd/internal/obj/plist.go

Documentation: cmd/internal/obj

     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 && 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.Func.Text != nil {
   123  		ctxt.Diag("duplicate TEXT for %s", s.Name)
   124  	}
   125  	if s.OnList() {
   126  		ctxt.Diag("symbol %s listed multiple times", s.Name)
   127  	}
   128  	s.Set(AttrOnList, true)
   129  	s.Set(AttrDuplicateOK, flag&DUPOK != 0)
   130  	s.Set(AttrNoSplit, flag&NOSPLIT != 0)
   131  	s.Set(AttrReflectMethod, flag&REFLECTMETHOD != 0)
   132  	s.Set(AttrWrapper, flag&WRAPPER != 0)
   133  	s.Set(AttrNeedCtxt, flag&NEEDCTXT != 0)
   134  	s.Set(AttrNoFrame, flag&NOFRAME != 0)
   135  	s.Type = objabi.STEXT
   136  	ctxt.Text = append(ctxt.Text, s)
   137  
   138  	// Set up DWARF entries for s.
   139  	info, loc, ranges, _, isstmt := ctxt.dwarfSym(s)
   140  	info.Type = objabi.SDWARFINFO
   141  	info.Set(AttrDuplicateOK, s.DuplicateOK())
   142  	if loc != nil {
   143  		loc.Type = objabi.SDWARFLOC
   144  		loc.Set(AttrDuplicateOK, s.DuplicateOK())
   145  		ctxt.Data = append(ctxt.Data, loc)
   146  	}
   147  	ranges.Type = objabi.SDWARFRANGE
   148  	ranges.Set(AttrDuplicateOK, s.DuplicateOK())
   149  	ctxt.Data = append(ctxt.Data, info, ranges)
   150  	isstmt.Type = objabi.SDWARFMISC
   151  	isstmt.Set(AttrDuplicateOK, s.DuplicateOK())
   152  	ctxt.Data = append(ctxt.Data, isstmt)
   153  
   154  	// Set up the function's gcargs and gclocals.
   155  	// They will be filled in later if needed.
   156  	gcargs := &s.Func.GCArgs
   157  	gcargs.Set(AttrDuplicateOK, true)
   158  	gcargs.Type = objabi.SRODATA
   159  	gclocals := &s.Func.GCLocals
   160  	gclocals.Set(AttrDuplicateOK, true)
   161  	gclocals.Type = objabi.SRODATA
   162  	gcregs := &s.Func.GCRegs
   163  	gcregs.Set(AttrDuplicateOK, true)
   164  	gcregs.Type = objabi.SRODATA
   165  }
   166  
   167  func (ctxt *Link) Globl(s *LSym, size int64, flag int) {
   168  	if s.SeenGlobl() {
   169  		fmt.Printf("duplicate %v\n", s)
   170  	}
   171  	s.Set(AttrSeenGlobl, true)
   172  	if s.OnList() {
   173  		ctxt.Diag("symbol %s listed multiple times", s.Name)
   174  	}
   175  	s.Set(AttrOnList, true)
   176  	ctxt.Data = append(ctxt.Data, s)
   177  	s.Size = size
   178  	if s.Type == 0 {
   179  		s.Type = objabi.SBSS
   180  	}
   181  	if flag&DUPOK != 0 {
   182  		s.Set(AttrDuplicateOK, true)
   183  	}
   184  	if flag&RODATA != 0 {
   185  		s.Type = objabi.SRODATA
   186  	} else if flag&NOPTR != 0 {
   187  		if s.Type == objabi.SDATA {
   188  			s.Type = objabi.SNOPTRDATA
   189  		} else {
   190  			s.Type = objabi.SNOPTRBSS
   191  		}
   192  	} else if flag&TLSBSS != 0 {
   193  		s.Type = objabi.STLSBSS
   194  	}
   195  }
   196  
   197  // EmitEntryLiveness generates PCDATA Progs after p to switch to the
   198  // liveness map active at the entry of function s. It returns the last
   199  // Prog generated.
   200  func (ctxt *Link) EmitEntryLiveness(s *LSym, p *Prog, newprog ProgAlloc) *Prog {
   201  	pcdata := Appendp(p, newprog)
   202  	pcdata.Pos = s.Func.Text.Pos
   203  	pcdata.As = APCDATA
   204  	pcdata.From.Type = TYPE_CONST
   205  	pcdata.From.Offset = objabi.PCDATA_StackMapIndex
   206  	pcdata.To.Type = TYPE_CONST
   207  	pcdata.To.Offset = -1 // pcdata starts at -1 at function entry
   208  
   209  	// Same, with register map.
   210  	pcdata = Appendp(pcdata, newprog)
   211  	pcdata.Pos = s.Func.Text.Pos
   212  	pcdata.As = APCDATA
   213  	pcdata.From.Type = TYPE_CONST
   214  	pcdata.From.Offset = objabi.PCDATA_RegMapIndex
   215  	pcdata.To.Type = TYPE_CONST
   216  	pcdata.To.Offset = -1
   217  
   218  	return pcdata
   219  }
   220  

View as plain text