...
Run Format

Source file src/cmd/link/internal/arm64/asm.go

Documentation: cmd/link/internal/arm64

     1  // Inferno utils/5l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/5l/asm.c
     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 arm64
    32  
    33  import (
    34  	"cmd/internal/objabi"
    35  	"cmd/internal/sys"
    36  	"cmd/link/internal/ld"
    37  	"cmd/link/internal/sym"
    38  	"debug/elf"
    39  	"encoding/binary"
    40  	"fmt"
    41  	"log"
    42  )
    43  
    44  func gentext(ctxt *ld.Link) {
    45  	if !ctxt.DynlinkingGo() {
    46  		return
    47  	}
    48  	addmoduledata := ctxt.Syms.Lookup("runtime.addmoduledata", 0)
    49  	if addmoduledata.Type == sym.STEXT && ctxt.BuildMode != ld.BuildModePlugin {
    50  		// we're linking a module containing the runtime -> no need for
    51  		// an init function
    52  		return
    53  	}
    54  	addmoduledata.Attr |= sym.AttrReachable
    55  	initfunc := ctxt.Syms.Lookup("go.link.addmoduledata", 0)
    56  	initfunc.Type = sym.STEXT
    57  	initfunc.Attr |= sym.AttrLocal
    58  	initfunc.Attr |= sym.AttrReachable
    59  	o := func(op uint32) {
    60  		initfunc.AddUint32(ctxt.Arch, op)
    61  	}
    62  	// 0000000000000000 <local.dso_init>:
    63  	// 0:	90000000 	adrp	x0, 0 <runtime.firstmoduledata>
    64  	// 	0: R_AARCH64_ADR_PREL_PG_HI21	local.moduledata
    65  	// 4:	91000000 	add	x0, x0, #0x0
    66  	// 	4: R_AARCH64_ADD_ABS_LO12_NC	local.moduledata
    67  	o(0x90000000)
    68  	o(0x91000000)
    69  	rel := initfunc.AddRel()
    70  	rel.Off = 0
    71  	rel.Siz = 8
    72  	rel.Sym = ctxt.Moduledata
    73  	rel.Type = objabi.R_ADDRARM64
    74  
    75  	// 8:	14000000 	b	0 <runtime.addmoduledata>
    76  	// 	8: R_AARCH64_CALL26	runtime.addmoduledata
    77  	o(0x14000000)
    78  	rel = initfunc.AddRel()
    79  	rel.Off = 8
    80  	rel.Siz = 4
    81  	rel.Sym = ctxt.Syms.Lookup("runtime.addmoduledata", 0)
    82  	rel.Type = objabi.R_CALLARM64 // Really should be R_AARCH64_JUMP26 but doesn't seem to make any difference
    83  
    84  	if ctxt.BuildMode == ld.BuildModePlugin {
    85  		ctxt.Textp = append(ctxt.Textp, addmoduledata)
    86  	}
    87  	ctxt.Textp = append(ctxt.Textp, initfunc)
    88  	initarray_entry := ctxt.Syms.Lookup("go.link.addmoduledatainit", 0)
    89  	initarray_entry.Attr |= sym.AttrReachable
    90  	initarray_entry.Attr |= sym.AttrLocal
    91  	initarray_entry.Type = sym.SINITARR
    92  	initarray_entry.AddAddr(ctxt.Arch, initfunc)
    93  }
    94  
    95  // adddynrel implements just enough to support external linking to
    96  // the system libc functions used by the runtime.
    97  func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
    98  	targ := r.Sym
    99  
   100  	switch r.Type {
   101  	case objabi.R_CALL,
   102  		objabi.R_PCREL,
   103  		objabi.R_CALLARM64:
   104  		if targ.Type != sym.SDYNIMPORT {
   105  			// nothing to do, the relocation will be laid out in reloc
   106  			return true
   107  		}
   108  		if ctxt.LinkMode == ld.LinkExternal {
   109  			// External linker will do this relocation.
   110  			return true
   111  		}
   112  	}
   113  	log.Fatalf("adddynrel not implemented for relocation type %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type))
   114  	return false
   115  }
   116  
   117  func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool {
   118  	ctxt.Out.Write64(uint64(sectoff))
   119  
   120  	elfsym := r.Xsym.ElfsymForReloc()
   121  	switch r.Type {
   122  	default:
   123  		return false
   124  	case objabi.R_ADDR:
   125  		switch r.Siz {
   126  		case 4:
   127  			ctxt.Out.Write64(uint64(elf.R_AARCH64_ABS32) | uint64(elfsym)<<32)
   128  		case 8:
   129  			ctxt.Out.Write64(uint64(elf.R_AARCH64_ABS64) | uint64(elfsym)<<32)
   130  		default:
   131  			return false
   132  		}
   133  	case objabi.R_ADDRARM64:
   134  		// two relocations: R_AARCH64_ADR_PREL_PG_HI21 and R_AARCH64_ADD_ABS_LO12_NC
   135  		ctxt.Out.Write64(uint64(elf.R_AARCH64_ADR_PREL_PG_HI21) | uint64(elfsym)<<32)
   136  		ctxt.Out.Write64(uint64(r.Xadd))
   137  		ctxt.Out.Write64(uint64(sectoff + 4))
   138  		ctxt.Out.Write64(uint64(elf.R_AARCH64_ADD_ABS_LO12_NC) | uint64(elfsym)<<32)
   139  	case objabi.R_ARM64_TLS_LE:
   140  		ctxt.Out.Write64(uint64(elf.R_AARCH64_TLSLE_MOVW_TPREL_G0) | uint64(elfsym)<<32)
   141  	case objabi.R_ARM64_TLS_IE:
   142  		ctxt.Out.Write64(uint64(elf.R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) | uint64(elfsym)<<32)
   143  		ctxt.Out.Write64(uint64(r.Xadd))
   144  		ctxt.Out.Write64(uint64(sectoff + 4))
   145  		ctxt.Out.Write64(uint64(elf.R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) | uint64(elfsym)<<32)
   146  	case objabi.R_ARM64_GOTPCREL:
   147  		ctxt.Out.Write64(uint64(elf.R_AARCH64_ADR_GOT_PAGE) | uint64(elfsym)<<32)
   148  		ctxt.Out.Write64(uint64(r.Xadd))
   149  		ctxt.Out.Write64(uint64(sectoff + 4))
   150  		ctxt.Out.Write64(uint64(elf.R_AARCH64_LD64_GOT_LO12_NC) | uint64(elfsym)<<32)
   151  	case objabi.R_CALLARM64:
   152  		if r.Siz != 4 {
   153  			return false
   154  		}
   155  		ctxt.Out.Write64(uint64(elf.R_AARCH64_CALL26) | uint64(elfsym)<<32)
   156  
   157  	}
   158  	ctxt.Out.Write64(uint64(r.Xadd))
   159  
   160  	return true
   161  }
   162  
   163  func elfsetupplt(ctxt *ld.Link) {
   164  	// TODO(aram)
   165  	return
   166  }
   167  
   168  func machoreloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool {
   169  	var v uint32
   170  
   171  	rs := r.Xsym
   172  
   173  	if rs.Type == sym.SHOSTOBJ || r.Type == objabi.R_CALLARM64 || r.Type == objabi.R_ADDRARM64 {
   174  		if rs.Dynid < 0 {
   175  			ld.Errorf(s, "reloc %d (%s) to non-macho symbol %s type=%d (%s)", r.Type, sym.RelocName(arch, r.Type), rs.Name, rs.Type, rs.Type)
   176  			return false
   177  		}
   178  
   179  		v = uint32(rs.Dynid)
   180  		v |= 1 << 27 // external relocation
   181  	} else {
   182  		v = uint32(rs.Sect.Extnum)
   183  		if v == 0 {
   184  			ld.Errorf(s, "reloc %d (%s) to symbol %s in non-macho section %s type=%d (%s)", r.Type, sym.RelocName(arch, r.Type), rs.Name, rs.Sect.Name, rs.Type, rs.Type)
   185  			return false
   186  		}
   187  	}
   188  
   189  	switch r.Type {
   190  	default:
   191  		return false
   192  	case objabi.R_ADDR:
   193  		v |= ld.MACHO_ARM64_RELOC_UNSIGNED << 28
   194  	case objabi.R_CALLARM64:
   195  		if r.Xadd != 0 {
   196  			ld.Errorf(s, "ld64 doesn't allow BR26 reloc with non-zero addend: %s+%d", rs.Name, r.Xadd)
   197  		}
   198  
   199  		v |= 1 << 24 // pc-relative bit
   200  		v |= ld.MACHO_ARM64_RELOC_BRANCH26 << 28
   201  	case objabi.R_ADDRARM64:
   202  		r.Siz = 4
   203  		// Two relocation entries: MACHO_ARM64_RELOC_PAGEOFF12 MACHO_ARM64_RELOC_PAGE21
   204  		// if r.Xadd is non-zero, add two MACHO_ARM64_RELOC_ADDEND.
   205  		if r.Xadd != 0 {
   206  			out.Write32(uint32(sectoff + 4))
   207  			out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(r.Xadd&0xffffff))
   208  		}
   209  		out.Write32(uint32(sectoff + 4))
   210  		out.Write32(v | (ld.MACHO_ARM64_RELOC_PAGEOFF12 << 28) | (2 << 25))
   211  		if r.Xadd != 0 {
   212  			out.Write32(uint32(sectoff))
   213  			out.Write32((ld.MACHO_ARM64_RELOC_ADDEND << 28) | (2 << 25) | uint32(r.Xadd&0xffffff))
   214  		}
   215  		v |= 1 << 24 // pc-relative bit
   216  		v |= ld.MACHO_ARM64_RELOC_PAGE21 << 28
   217  	}
   218  
   219  	switch r.Siz {
   220  	default:
   221  		return false
   222  	case 1:
   223  		v |= 0 << 25
   224  	case 2:
   225  		v |= 1 << 25
   226  	case 4:
   227  		v |= 2 << 25
   228  	case 8:
   229  		v |= 3 << 25
   230  	}
   231  
   232  	out.Write32(uint32(sectoff))
   233  	out.Write32(v)
   234  	return true
   235  }
   236  
   237  func archreloc(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val *int64) bool {
   238  	if ctxt.LinkMode == ld.LinkExternal {
   239  		switch r.Type {
   240  		default:
   241  			return false
   242  		case objabi.R_ARM64_GOTPCREL:
   243  			var o1, o2 uint32
   244  			if ctxt.Arch.ByteOrder == binary.BigEndian {
   245  				o1 = uint32(*val >> 32)
   246  				o2 = uint32(*val)
   247  			} else {
   248  				o1 = uint32(*val)
   249  				o2 = uint32(*val >> 32)
   250  			}
   251  			// Any relocation against a function symbol is redirected to
   252  			// be against a local symbol instead (see putelfsym in
   253  			// symtab.go) but unfortunately the system linker was buggy
   254  			// when confronted with a R_AARCH64_ADR_GOT_PAGE relocation
   255  			// against a local symbol until May 2015
   256  			// (https://sourceware.org/bugzilla/show_bug.cgi?id=18270). So
   257  			// we convert the adrp; ld64 + R_ARM64_GOTPCREL into adrp;
   258  			// add + R_ADDRARM64.
   259  			if !(r.Sym.Version != 0 || r.Sym.Attr.VisibilityHidden() || r.Sym.Attr.Local()) && r.Sym.Type == sym.STEXT && ctxt.DynlinkingGo() {
   260  				if o2&0xffc00000 != 0xf9400000 {
   261  					ld.Errorf(s, "R_ARM64_GOTPCREL against unexpected instruction %x", o2)
   262  				}
   263  				o2 = 0x91000000 | (o2 & 0x000003ff)
   264  				r.Type = objabi.R_ADDRARM64
   265  			}
   266  			if ctxt.Arch.ByteOrder == binary.BigEndian {
   267  				*val = int64(o1)<<32 | int64(o2)
   268  			} else {
   269  				*val = int64(o2)<<32 | int64(o1)
   270  			}
   271  			fallthrough
   272  		case objabi.R_ADDRARM64:
   273  			r.Done = false
   274  
   275  			// set up addend for eventual relocation via outer symbol.
   276  			rs := r.Sym
   277  			r.Xadd = r.Add
   278  			for rs.Outer != nil {
   279  				r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
   280  				rs = rs.Outer
   281  			}
   282  
   283  			if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
   284  				ld.Errorf(s, "missing section for %s", rs.Name)
   285  			}
   286  			r.Xsym = rs
   287  
   288  			// Note: ld64 currently has a bug that any non-zero addend for BR26 relocation
   289  			// will make the linking fail because it thinks the code is not PIC even though
   290  			// the BR26 relocation should be fully resolved at link time.
   291  			// That is the reason why the next if block is disabled. When the bug in ld64
   292  			// is fixed, we can enable this block and also enable duff's device in cmd/7g.
   293  			if false && ctxt.HeadType == objabi.Hdarwin {
   294  				var o0, o1 uint32
   295  
   296  				if ctxt.Arch.ByteOrder == binary.BigEndian {
   297  					o0 = uint32(*val >> 32)
   298  					o1 = uint32(*val)
   299  				} else {
   300  					o0 = uint32(*val)
   301  					o1 = uint32(*val >> 32)
   302  				}
   303  				// Mach-O wants the addend to be encoded in the instruction
   304  				// Note that although Mach-O supports ARM64_RELOC_ADDEND, it
   305  				// can only encode 24-bit of signed addend, but the instructions
   306  				// supports 33-bit of signed addend, so we always encode the
   307  				// addend in place.
   308  				o0 |= (uint32((r.Xadd>>12)&3) << 29) | (uint32((r.Xadd>>12>>2)&0x7ffff) << 5)
   309  				o1 |= uint32(r.Xadd&0xfff) << 10
   310  				r.Xadd = 0
   311  
   312  				// when laid out, the instruction order must always be o1, o2.
   313  				if ctxt.Arch.ByteOrder == binary.BigEndian {
   314  					*val = int64(o0)<<32 | int64(o1)
   315  				} else {
   316  					*val = int64(o1)<<32 | int64(o0)
   317  				}
   318  			}
   319  
   320  			return true
   321  		case objabi.R_CALLARM64,
   322  			objabi.R_ARM64_TLS_LE,
   323  			objabi.R_ARM64_TLS_IE:
   324  			r.Done = false
   325  			r.Xsym = r.Sym
   326  			r.Xadd = r.Add
   327  			return true
   328  		}
   329  	}
   330  
   331  	switch r.Type {
   332  	case objabi.R_CONST:
   333  		*val = r.Add
   334  		return true
   335  	case objabi.R_GOTOFF:
   336  		*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
   337  		return true
   338  	case objabi.R_ADDRARM64:
   339  		t := ld.Symaddr(r.Sym) + r.Add - ((s.Value + int64(r.Off)) &^ 0xfff)
   340  		if t >= 1<<32 || t < -1<<32 {
   341  			ld.Errorf(s, "program too large, address relocation distance = %d", t)
   342  		}
   343  
   344  		var o0, o1 uint32
   345  
   346  		if ctxt.Arch.ByteOrder == binary.BigEndian {
   347  			o0 = uint32(*val >> 32)
   348  			o1 = uint32(*val)
   349  		} else {
   350  			o0 = uint32(*val)
   351  			o1 = uint32(*val >> 32)
   352  		}
   353  
   354  		o0 |= (uint32((t>>12)&3) << 29) | (uint32((t>>12>>2)&0x7ffff) << 5)
   355  		o1 |= uint32(t&0xfff) << 10
   356  
   357  		// when laid out, the instruction order must always be o1, o2.
   358  		if ctxt.Arch.ByteOrder == binary.BigEndian {
   359  			*val = int64(o0)<<32 | int64(o1)
   360  		} else {
   361  			*val = int64(o1)<<32 | int64(o0)
   362  		}
   363  		return true
   364  	case objabi.R_ARM64_TLS_LE:
   365  		r.Done = false
   366  		if ctxt.HeadType != objabi.Hlinux {
   367  			ld.Errorf(s, "TLS reloc on unsupported OS %v", ctxt.HeadType)
   368  		}
   369  		// The TCB is two pointers. This is not documented anywhere, but is
   370  		// de facto part of the ABI.
   371  		v := r.Sym.Value + int64(2*ctxt.Arch.PtrSize)
   372  		if v < 0 || v >= 32678 {
   373  			ld.Errorf(s, "TLS offset out of range %d", v)
   374  		}
   375  		*val |= v << 5
   376  		return true
   377  	case objabi.R_CALLARM64:
   378  		t := (ld.Symaddr(r.Sym) + r.Add) - (s.Value + int64(r.Off))
   379  		if t >= 1<<27 || t < -1<<27 {
   380  			ld.Errorf(s, "program too large, call relocation distance = %d", t)
   381  		}
   382  		*val |= (t >> 2) & 0x03ffffff
   383  		return true
   384  	}
   385  
   386  	return false
   387  }
   388  
   389  func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 {
   390  	log.Fatalf("unexpected relocation variant")
   391  	return -1
   392  }
   393  
   394  func asmb(ctxt *ld.Link) {
   395  	if ctxt.Debugvlog != 0 {
   396  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   397  	}
   398  
   399  	if ctxt.IsELF {
   400  		ld.Asmbelfsetup()
   401  	}
   402  
   403  	sect := ld.Segtext.Sections[0]
   404  	ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   405  	ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   406  	for _, sect = range ld.Segtext.Sections[1:] {
   407  		ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   408  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   409  	}
   410  
   411  	if ld.Segrodata.Filelen > 0 {
   412  		if ctxt.Debugvlog != 0 {
   413  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   414  		}
   415  		ctxt.Out.SeekSet(int64(ld.Segrodata.Fileoff))
   416  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   417  	}
   418  	if ld.Segrelrodata.Filelen > 0 {
   419  		if ctxt.Debugvlog != 0 {
   420  			ctxt.Logf("%5.2f relrodatblk\n", ld.Cputime())
   421  		}
   422  		ctxt.Out.SeekSet(int64(ld.Segrelrodata.Fileoff))
   423  		ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen))
   424  	}
   425  
   426  	if ctxt.Debugvlog != 0 {
   427  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   428  	}
   429  
   430  	ctxt.Out.SeekSet(int64(ld.Segdata.Fileoff))
   431  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   432  
   433  	ctxt.Out.SeekSet(int64(ld.Segdwarf.Fileoff))
   434  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   435  
   436  	machlink := uint32(0)
   437  	if ctxt.HeadType == objabi.Hdarwin {
   438  		machlink = uint32(ld.Domacholink(ctxt))
   439  	}
   440  
   441  	/* output symbol table */
   442  	ld.Symsize = 0
   443  
   444  	ld.Lcsize = 0
   445  	symo := uint32(0)
   446  	if !*ld.FlagS {
   447  		// TODO: rationalize
   448  		if ctxt.Debugvlog != 0 {
   449  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   450  		}
   451  		switch ctxt.HeadType {
   452  		default:
   453  			if ctxt.IsELF {
   454  				symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   455  				symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
   456  			}
   457  
   458  		case objabi.Hplan9:
   459  			symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   460  
   461  		case objabi.Hdarwin:
   462  			symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(*ld.FlagRound))) + uint64(machlink))
   463  		}
   464  
   465  		ctxt.Out.SeekSet(int64(symo))
   466  		switch ctxt.HeadType {
   467  		default:
   468  			if ctxt.IsELF {
   469  				if ctxt.Debugvlog != 0 {
   470  					ctxt.Logf("%5.2f elfsym\n", ld.Cputime())
   471  				}
   472  				ld.Asmelfsym(ctxt)
   473  				ctxt.Out.Flush()
   474  				ctxt.Out.Write(ld.Elfstrdat)
   475  
   476  				if ctxt.LinkMode == ld.LinkExternal {
   477  					ld.Elfemitreloc(ctxt)
   478  				}
   479  			}
   480  
   481  		case objabi.Hplan9:
   482  			ld.Asmplan9sym(ctxt)
   483  			ctxt.Out.Flush()
   484  
   485  			sym := ctxt.Syms.Lookup("pclntab", 0)
   486  			if sym != nil {
   487  				ld.Lcsize = int32(len(sym.P))
   488  				ctxt.Out.Write(sym.P)
   489  				ctxt.Out.Flush()
   490  			}
   491  
   492  		case objabi.Hdarwin:
   493  			if ctxt.LinkMode == ld.LinkExternal {
   494  				ld.Machoemitreloc(ctxt)
   495  			}
   496  		}
   497  	}
   498  
   499  	if ctxt.Debugvlog != 0 {
   500  		ctxt.Logf("%5.2f header\n", ld.Cputime())
   501  	}
   502  	ctxt.Out.SeekSet(0)
   503  	switch ctxt.HeadType {
   504  	default:
   505  	case objabi.Hplan9: /* plan 9 */
   506  		ctxt.Out.Write32(0x647)                      /* magic */
   507  		ctxt.Out.Write32(uint32(ld.Segtext.Filelen)) /* sizes */
   508  		ctxt.Out.Write32(uint32(ld.Segdata.Filelen))
   509  		ctxt.Out.Write32(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   510  		ctxt.Out.Write32(uint32(ld.Symsize))          /* nsyms */
   511  		ctxt.Out.Write32(uint32(ld.Entryvalue(ctxt))) /* va of entry */
   512  		ctxt.Out.Write32(0)
   513  		ctxt.Out.Write32(uint32(ld.Lcsize))
   514  
   515  	case objabi.Hlinux,
   516  		objabi.Hfreebsd,
   517  		objabi.Hnetbsd,
   518  		objabi.Hopenbsd,
   519  		objabi.Hnacl:
   520  		ld.Asmbelf(ctxt, int64(symo))
   521  
   522  	case objabi.Hdarwin:
   523  		ld.Asmbmacho(ctxt)
   524  	}
   525  
   526  	ctxt.Out.Flush()
   527  	if *ld.FlagC {
   528  		fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
   529  		fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
   530  		fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
   531  		fmt.Printf("symsize=%d\n", ld.Symsize)
   532  		fmt.Printf("lcsize=%d\n", ld.Lcsize)
   533  		fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
   534  	}
   535  }
   536  

View as plain text