github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/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  	"fmt"
     9  	"log"
    10  	"strings"
    11  )
    12  
    13  type Plist struct {
    14  	Firstpc *Prog
    15  }
    16  
    17  /*
    18   * start a new Prog list.
    19   */
    20  func Linknewplist(ctxt *Link) *Plist {
    21  	pl := new(Plist)
    22  	ctxt.Plists = append(ctxt.Plists, pl)
    23  	return pl
    24  }
    25  
    26  func Flushplist(ctxt *Link) {
    27  	flushplist(ctxt, ctxt.Debugasm == 0)
    28  }
    29  func FlushplistNoFree(ctxt *Link) {
    30  	flushplist(ctxt, false)
    31  }
    32  func flushplist(ctxt *Link, freeProgs bool) {
    33  	// Build list of symbols, and assign instructions to lists.
    34  	// Ignore ctxt->plist boundaries. There are no guarantees there,
    35  	// and the assemblers just use one big list.
    36  	var curtext *LSym
    37  	var etext *Prog
    38  	var text []*LSym
    39  
    40  	for _, pl := range ctxt.Plists {
    41  		var plink *Prog
    42  		for p := pl.Firstpc; p != nil; p = plink {
    43  			if ctxt.Debugasm != 0 && ctxt.Debugvlog != 0 {
    44  				fmt.Printf("obj: %v\n", p)
    45  			}
    46  			plink = p.Link
    47  			p.Link = nil
    48  
    49  			switch p.As {
    50  			case AEND:
    51  				continue
    52  
    53  			case ATEXT:
    54  				s := p.From.Sym
    55  				if s == nil {
    56  					// func _() { }
    57  					curtext = nil
    58  
    59  					continue
    60  				}
    61  
    62  				if s.Text != nil {
    63  					log.Fatalf("duplicate TEXT for %s", s.Name)
    64  				}
    65  				if s.OnList() {
    66  					log.Fatalf("symbol %s listed multiple times", s.Name)
    67  				}
    68  				s.Set(AttrOnList, true)
    69  				text = append(text, s)
    70  				flag := int(p.From3Offset())
    71  				if flag&DUPOK != 0 {
    72  					s.Set(AttrDuplicateOK, true)
    73  				}
    74  				if flag&NOSPLIT != 0 {
    75  					s.Set(AttrNoSplit, true)
    76  				}
    77  				if flag&REFLECTMETHOD != 0 {
    78  					s.Set(AttrReflectMethod, true)
    79  				}
    80  				s.Type = STEXT
    81  				s.Text = p
    82  				etext = p
    83  				curtext = s
    84  				continue
    85  
    86  			case AFUNCDATA:
    87  				// Rewrite reference to go_args_stackmap(SB) to the Go-provided declaration information.
    88  				if curtext == nil { // func _() {}
    89  					continue
    90  				}
    91  				if p.To.Sym.Name == "go_args_stackmap" {
    92  					if p.From.Type != TYPE_CONST || p.From.Offset != FUNCDATA_ArgsPointerMaps {
    93  						ctxt.Diag("FUNCDATA use of go_args_stackmap(SB) without FUNCDATA_ArgsPointerMaps")
    94  					}
    95  					p.To.Sym = Linklookup(ctxt, fmt.Sprintf("%s.args_stackmap", curtext.Name), int(curtext.Version))
    96  				}
    97  
    98  			}
    99  
   100  			if curtext == nil {
   101  				etext = nil
   102  				continue
   103  			}
   104  			etext.Link = p
   105  			etext = p
   106  		}
   107  	}
   108  
   109  	// Add reference to Go arguments for C or assembly functions without them.
   110  	for _, s := range text {
   111  		if !strings.HasPrefix(s.Name, "\"\".") {
   112  			continue
   113  		}
   114  		found := false
   115  		var p *Prog
   116  		for p = s.Text; p != nil; p = p.Link {
   117  			if p.As == AFUNCDATA && p.From.Type == TYPE_CONST && p.From.Offset == FUNCDATA_ArgsPointerMaps {
   118  				found = true
   119  				break
   120  			}
   121  		}
   122  
   123  		if !found {
   124  			p = Appendp(ctxt, s.Text)
   125  			p.As = AFUNCDATA
   126  			p.From.Type = TYPE_CONST
   127  			p.From.Offset = FUNCDATA_ArgsPointerMaps
   128  			p.To.Type = TYPE_MEM
   129  			p.To.Name = NAME_EXTERN
   130  			p.To.Sym = Linklookup(ctxt, fmt.Sprintf("%s.args_stackmap", s.Name), int(s.Version))
   131  		}
   132  	}
   133  
   134  	// Turn functions into machine code images.
   135  	for _, s := range text {
   136  		mkfwd(s)
   137  		linkpatch(ctxt, s)
   138  		ctxt.Arch.Preprocess(ctxt, s)
   139  		ctxt.Arch.Assemble(ctxt, s)
   140  		fieldtrack(ctxt, s)
   141  		linkpcln(ctxt, s)
   142  		if freeProgs {
   143  			s.Text = nil
   144  		}
   145  	}
   146  
   147  	// Add to running list in ctxt.
   148  	ctxt.Text = append(ctxt.Text, text...)
   149  	ctxt.Data = append(ctxt.Data, gendwarf(ctxt, text)...)
   150  	ctxt.Plists = nil
   151  	ctxt.Curp = nil
   152  	if freeProgs {
   153  		ctxt.freeProgs()
   154  	}
   155  }
   156  
   157  func (ctxt *Link) Globl(s *LSym, size int64, flag int) {
   158  	if s.SeenGlobl() {
   159  		fmt.Printf("duplicate %v\n", s)
   160  	}
   161  	s.Set(AttrSeenGlobl, true)
   162  	if s.OnList() {
   163  		log.Fatalf("symbol %s listed multiple times", s.Name)
   164  	}
   165  	s.Set(AttrOnList, true)
   166  	ctxt.Data = append(ctxt.Data, s)
   167  	s.Size = size
   168  	if s.Type == 0 || s.Type == SXREF {
   169  		s.Type = SBSS
   170  	}
   171  	if flag&DUPOK != 0 {
   172  		s.Set(AttrDuplicateOK, true)
   173  	}
   174  	if flag&RODATA != 0 {
   175  		s.Type = SRODATA
   176  	} else if flag&NOPTR != 0 {
   177  		s.Type = SNOPTRBSS
   178  	} else if flag&TLSBSS != 0 {
   179  		s.Type = STLSBSS
   180  	}
   181  }