github.com/sbinet/go@v0.0.0-20160827155028-54d7de7dd62b/src/cmd/link/internal/x86/obj.go (about)

     1  // Inferno utils/8l/obj.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/8l/obj.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 x86
    32  
    33  import (
    34  	"cmd/internal/obj"
    35  	"cmd/internal/sys"
    36  	"cmd/link/internal/ld"
    37  	"fmt"
    38  	"log"
    39  )
    40  
    41  // Reading object files.
    42  
    43  func Main() {
    44  	linkarchinit()
    45  	ld.Main()
    46  }
    47  
    48  func linkarchinit() {
    49  	ld.SysArch = sys.Arch386
    50  
    51  	ld.Thearch.Funcalign = FuncAlign
    52  	ld.Thearch.Maxalign = MaxAlign
    53  	ld.Thearch.Minalign = MinAlign
    54  	ld.Thearch.Dwarfregsp = DWARFREGSP
    55  	ld.Thearch.Dwarfreglr = DWARFREGLR
    56  
    57  	ld.Thearch.Adddynrel = adddynrel
    58  	ld.Thearch.Archinit = archinit
    59  	ld.Thearch.Archreloc = archreloc
    60  	ld.Thearch.Archrelocvariant = archrelocvariant
    61  	ld.Thearch.Asmb = asmb
    62  	ld.Thearch.Elfreloc1 = elfreloc1
    63  	ld.Thearch.Elfsetupplt = elfsetupplt
    64  	ld.Thearch.Gentext = gentext
    65  	ld.Thearch.Machoreloc1 = machoreloc1
    66  	ld.Thearch.PEreloc1 = pereloc1
    67  	ld.Thearch.Lput = ld.Lputl
    68  	ld.Thearch.Wput = ld.Wputl
    69  	ld.Thearch.Vput = ld.Vputl
    70  	ld.Thearch.Append16 = ld.Append16l
    71  	ld.Thearch.Append32 = ld.Append32l
    72  	ld.Thearch.Append64 = ld.Append64l
    73  
    74  	ld.Thearch.Linuxdynld = "/lib/ld-linux.so.2"
    75  	ld.Thearch.Freebsddynld = "/usr/libexec/ld-elf.so.1"
    76  	ld.Thearch.Openbsddynld = "/usr/libexec/ld.so"
    77  	ld.Thearch.Netbsddynld = "/usr/libexec/ld.elf_so"
    78  	ld.Thearch.Solarisdynld = "/lib/ld.so.1"
    79  }
    80  
    81  func archinit(ctxt *ld.Link) {
    82  	// getgoextlinkenabled is based on GO_EXTLINK_ENABLED when
    83  	// Go was built; see ../../make.bash.
    84  	if ld.Linkmode == ld.LinkAuto && obj.Getgoextlinkenabled() == "0" {
    85  		ld.Linkmode = ld.LinkInternal
    86  	}
    87  
    88  	if (ld.Buildmode == ld.BuildmodeCArchive && ld.Iself) || ld.Buildmode == ld.BuildmodeCShared || ld.Buildmode == ld.BuildmodePIE || ctxt.DynlinkingGo() {
    89  		ld.Linkmode = ld.LinkExternal
    90  		got := ld.Linklookup(ctxt, "_GLOBAL_OFFSET_TABLE_", 0)
    91  		got.Type = obj.SDYNIMPORT
    92  		got.Attr |= ld.AttrReachable
    93  	}
    94  
    95  	switch ld.HEADTYPE {
    96  	default:
    97  		if ld.Linkmode == ld.LinkAuto {
    98  			ld.Linkmode = ld.LinkInternal
    99  		}
   100  		if ld.Linkmode == ld.LinkExternal && obj.Getgoextlinkenabled() != "1" {
   101  			log.Fatalf("cannot use -linkmode=external with -H %s", ld.Headstr(int(ld.HEADTYPE)))
   102  		}
   103  
   104  	case obj.Hdarwin,
   105  		obj.Hfreebsd,
   106  		obj.Hlinux,
   107  		obj.Hnetbsd,
   108  		obj.Hopenbsd,
   109  		obj.Hwindows:
   110  		break
   111  	}
   112  
   113  	switch ld.HEADTYPE {
   114  	default:
   115  		ld.Exitf("unknown -H option: %v", ld.HEADTYPE)
   116  
   117  	case obj.Hplan9: /* plan 9 */
   118  		ld.HEADR = 32
   119  
   120  		if *ld.FlagTextAddr == -1 {
   121  			*ld.FlagTextAddr = 4096 + 32
   122  		}
   123  		if *ld.FlagDataAddr == -1 {
   124  			*ld.FlagDataAddr = 0
   125  		}
   126  		if *ld.FlagRound == -1 {
   127  			*ld.FlagRound = 4096
   128  		}
   129  
   130  	case obj.Hdarwin: /* apple MACH */
   131  		ld.Machoinit()
   132  
   133  		ld.HEADR = ld.INITIAL_MACHO_HEADR
   134  		if *ld.FlagTextAddr == -1 {
   135  			*ld.FlagTextAddr = 4096 + int64(ld.HEADR)
   136  		}
   137  		if *ld.FlagDataAddr == -1 {
   138  			*ld.FlagDataAddr = 0
   139  		}
   140  		if *ld.FlagRound == -1 {
   141  			*ld.FlagRound = 4096
   142  		}
   143  
   144  	case obj.Hlinux, /* elf32 executable */
   145  		obj.Hfreebsd,
   146  		obj.Hnetbsd,
   147  		obj.Hopenbsd:
   148  		ld.Elfinit(ctxt)
   149  
   150  		ld.HEADR = ld.ELFRESERVE
   151  		if *ld.FlagTextAddr == -1 {
   152  			*ld.FlagTextAddr = 0x08048000 + int64(ld.HEADR)
   153  		}
   154  		if *ld.FlagDataAddr == -1 {
   155  			*ld.FlagDataAddr = 0
   156  		}
   157  		if *ld.FlagRound == -1 {
   158  			*ld.FlagRound = 4096
   159  		}
   160  
   161  	case obj.Hnacl:
   162  		ld.Elfinit(ctxt)
   163  		ld.HEADR = 0x10000
   164  		ld.Funcalign = 32
   165  		if *ld.FlagTextAddr == -1 {
   166  			*ld.FlagTextAddr = 0x20000
   167  		}
   168  		if *ld.FlagDataAddr == -1 {
   169  			*ld.FlagDataAddr = 0
   170  		}
   171  		if *ld.FlagRound == -1 {
   172  			*ld.FlagRound = 0x10000
   173  		}
   174  
   175  	case obj.Hwindows: /* PE executable */
   176  		ld.Peinit(ctxt)
   177  
   178  		ld.HEADR = ld.PEFILEHEADR
   179  		if *ld.FlagTextAddr == -1 {
   180  			*ld.FlagTextAddr = ld.PEBASE + int64(ld.PESECTHEADR)
   181  		}
   182  		if *ld.FlagDataAddr == -1 {
   183  			*ld.FlagDataAddr = 0
   184  		}
   185  		if *ld.FlagRound == -1 {
   186  			*ld.FlagRound = ld.PESECTALIGN
   187  		}
   188  	}
   189  
   190  	if *ld.FlagDataAddr != 0 && *ld.FlagRound != 0 {
   191  		fmt.Printf("warning: -D0x%x is ignored because of -R0x%x\n", uint64(*ld.FlagDataAddr), uint32(*ld.FlagRound))
   192  	}
   193  }