github.com/kdevb0x/go@v0.0.0-20180115030120-39687051e9e7/src/cmd/link/internal/arm64/asm.go (about)

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