1// Inferno utils/6l/obj.c
2// https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/obj.c
3//
4//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
5//	Portions Copyright © 1995-1997 C H Forsyth ([email protected])
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 ([email protected])
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
31package ld
32
33import (
34	"bufio"
35	"cmd/internal/goobj"
36	"cmd/internal/objabi"
37	"cmd/internal/quoted"
38	"cmd/internal/sys"
39	"cmd/internal/telemetry/counter"
40	"cmd/link/internal/benchmark"
41	"flag"
42	"internal/buildcfg"
43	"log"
44	"os"
45	"runtime"
46	"runtime/pprof"
47	"strconv"
48	"strings"
49)
50
51var (
52	pkglistfornote []byte
53	windowsgui     bool // writes a "GUI binary" instead of a "console binary"
54	ownTmpDir      bool // set to true if tmp dir created by linker (e.g. no -tmpdir)
55)
56
57func init() {
58	flag.Var(&rpath, "r", "set the ELF dynamic linker search `path` to dir1:dir2:...")
59	flag.Var(&flagExtld, "extld", "use `linker` when linking in external mode")
60	flag.Var(&flagExtldflags, "extldflags", "pass `flags` to external linker")
61	flag.Var(&flagW, "w", "disable DWARF generation")
62}
63
64// Flags used by the linker. The exported flags are used by the architecture-specific packages.
65var (
66	flagBuildid = flag.String("buildid", "", "record `id` as Go toolchain build id")
67	flagBindNow = flag.Bool("bindnow", false, "mark a dynamically linked ELF object for immediate function binding")
68
69	flagOutfile    = flag.String("o", "", "write output to `file`")
70	flagPluginPath = flag.String("pluginpath", "", "full path name for plugin")
71
72	flagInstallSuffix = flag.String("installsuffix", "", "set package directory `suffix`")
73	flagDumpDep       = flag.Bool("dumpdep", false, "dump symbol dependency graph")
74	flagRace          = flag.Bool("race", false, "enable race detector")
75	flagMsan          = flag.Bool("msan", false, "enable MSan interface")
76	flagAsan          = flag.Bool("asan", false, "enable ASan interface")
77	flagAslr          = flag.Bool("aslr", true, "enable ASLR for buildmode=c-shared on windows")
78
79	flagFieldTrack = flag.String("k", "", "set field tracking `symbol`")
80	flagLibGCC     = flag.String("libgcc", "", "compiler support lib for internal linking; use \"none\" to disable")
81	flagTmpdir     = flag.String("tmpdir", "", "use `directory` for temporary files")
82
83	flagExtld      quoted.Flag
84	flagExtldflags quoted.Flag
85	flagExtar      = flag.String("extar", "", "archive program for buildmode=c-archive")
86
87	flagCaptureHostObjs = flag.String("capturehostobjs", "", "capture host object files loaded during internal linking to specified dir")
88
89	flagA             = flag.Bool("a", false, "no-op (deprecated)")
90	FlagC             = flag.Bool("c", false, "dump call graph")
91	FlagD             = flag.Bool("d", false, "disable dynamic executable")
92	flagF             = flag.Bool("f", false, "ignore version mismatch")
93	flagG             = flag.Bool("g", false, "disable go package data checks")
94	flagH             = flag.Bool("h", false, "halt on error")
95	flagN             = flag.Bool("n", false, "no-op (deprecated)")
96	FlagS             = flag.Bool("s", false, "disable symbol table")
97	flag8             bool // use 64-bit addresses in symbol table
98	flagInterpreter   = flag.String("I", "", "use `linker` as ELF dynamic linker")
99	flagCheckLinkname = flag.Bool("checklinkname", true, "check linkname symbol references")
100	FlagDebugTramp    = flag.Int("debugtramp", 0, "debug trampolines")
101	FlagDebugTextSize = flag.Int("debugtextsize", 0, "debug text section max size")
102	flagDebugNosplit  = flag.Bool("debugnosplit", false, "dump nosplit call graph")
103	FlagStrictDups    = flag.Int("strictdups", 0, "sanity check duplicate symbol contents during object file reading (1=warn 2=err).")
104	FlagRound         = flag.Int64("R", -1, "set address rounding `quantum`")
105	FlagTextAddr      = flag.Int64("T", -1, "set the start address of text symbols")
106	flagEntrySymbol   = flag.String("E", "", "set `entry` symbol name")
107	flagPruneWeakMap  = flag.Bool("pruneweakmap", true, "prune weak mapinit refs")
108	flagRandLayout    = flag.Int64("randlayout", 0, "randomize function layout")
109	cpuprofile        = flag.String("cpuprofile", "", "write cpu profile to `file`")
110	memprofile        = flag.String("memprofile", "", "write memory profile to `file`")
111	memprofilerate    = flag.Int64("memprofilerate", 0, "set runtime.MemProfileRate to `rate`")
112	benchmarkFlag     = flag.String("benchmark", "", "set to 'mem' or 'cpu' to enable phase benchmarking")
113	benchmarkFileFlag = flag.String("benchmarkprofile", "", "emit phase profiles to `base`_phase.{cpu,mem}prof")
114
115	flagW ternaryFlag
116	FlagW = new(bool) // the -w flag, computed in main from flagW
117)
118
119// ternaryFlag is like a boolean flag, but has a default value that is
120// neither true nor false, allowing it to be set from context (e.g. from another
121// flag).
122// *ternaryFlag implements flag.Value.
123type ternaryFlag int
124
125const (
126	ternaryFlagUnset ternaryFlag = iota
127	ternaryFlagFalse
128	ternaryFlagTrue
129)
130
131func (t *ternaryFlag) Set(s string) error {
132	v, err := strconv.ParseBool(s)
133	if err != nil {
134		return err
135	}
136	if v {
137		*t = ternaryFlagTrue
138	} else {
139		*t = ternaryFlagFalse
140	}
141	return nil
142}
143
144func (t *ternaryFlag) String() string {
145	switch *t {
146	case ternaryFlagFalse:
147		return "false"
148	case ternaryFlagTrue:
149		return "true"
150	}
151	return "unset"
152}
153
154func (t *ternaryFlag) IsBoolFlag() bool { return true } // parse like a boolean flag
155
156// Main is the main entry point for the linker code.
157func Main(arch *sys.Arch, theArch Arch) {
158	log.SetPrefix("link: ")
159	log.SetFlags(0)
160	counter.Open()
161	counter.Inc("link/invocations")
162
163	thearch = theArch
164	ctxt := linknew(arch)
165	ctxt.Bso = bufio.NewWriter(os.Stdout)
166
167	// For testing behavior of go command when tools crash silently.
168	// Undocumented, not in standard flag parser to avoid
169	// exposing in usage message.
170	for _, arg := range os.Args {
171		if arg == "-crash_for_testing" {
172			os.Exit(2)
173		}
174	}
175
176	if buildcfg.GOROOT == "" {
177		// cmd/go clears the GOROOT variable when -trimpath is set,
178		// so omit it from the binary even if cmd/link itself has an
179		// embedded GOROOT value reported by runtime.GOROOT.
180	} else {
181		addstrdata1(ctxt, "runtime.defaultGOROOT="+buildcfg.GOROOT)
182	}
183
184	buildVersion := buildcfg.Version
185	if goexperiment := buildcfg.Experiment.String(); goexperiment != "" {
186		buildVersion += " X:" + goexperiment
187	}
188	addstrdata1(ctxt, "runtime.buildVersion="+buildVersion)
189
190	// TODO(matloob): define these above and then check flag values here
191	if ctxt.Arch.Family == sys.AMD64 && buildcfg.GOOS == "plan9" {
192		flag.BoolVar(&flag8, "8", false, "use 64-bit addresses in symbol table")
193	}
194	flagHeadType := flag.String("H", "", "set header `type`")
195	flag.BoolVar(&ctxt.linkShared, "linkshared", false, "link against installed Go shared libraries")
196	flag.Var(&ctxt.LinkMode, "linkmode", "set link `mode`")
197	flag.Var(&ctxt.BuildMode, "buildmode", "set build `mode`")
198	flag.BoolVar(&ctxt.compressDWARF, "compressdwarf", true, "compress DWARF if possible")
199	objabi.Flagfn1("B", "add an ELF NT_GNU_BUILD_ID `note` when using ELF; use \"gobuildid\" to generate it from the Go build ID", addbuildinfo)
200	objabi.Flagfn1("L", "add specified `directory` to library path", func(a string) { Lflag(ctxt, a) })
201	objabi.AddVersionFlag() // -V
202	objabi.Flagfn1("X", "add string value `definition` of the form importpath.name=value", func(s string) { addstrdata1(ctxt, s) })
203	objabi.Flagcount("v", "print link trace", &ctxt.Debugvlog)
204	objabi.Flagfn1("importcfg", "read import configuration from `file`", ctxt.readImportCfg)
205
206	objabi.Flagparse(usage)
207	counter.CountFlags("link/flag:", *flag.CommandLine)
208
209	if ctxt.Debugvlog > 0 {
210		// dump symbol info on crash
211		defer func() { ctxt.loader.Dump() }()
212	}
213	if ctxt.Debugvlog > 1 {
214		// dump symbol info on error
215		AtExit(func() {
216			if nerrors > 0 {
217				ctxt.loader.Dump()
218			}
219		})
220	}
221
222	switch *flagHeadType {
223	case "":
224	case "windowsgui":
225		ctxt.HeadType = objabi.Hwindows
226		windowsgui = true
227	default:
228		if err := ctxt.HeadType.Set(*flagHeadType); err != nil {
229			Errorf(nil, "%v", err)
230			usage()
231		}
232	}
233	if ctxt.HeadType == objabi.Hunknown {
234		ctxt.HeadType.Set(buildcfg.GOOS)
235	}
236
237	if !*flagAslr && ctxt.BuildMode != BuildModeCShared {
238		Errorf(nil, "-aslr=false is only allowed for -buildmode=c-shared")
239		usage()
240	}
241
242	if *FlagD && ctxt.UsesLibc() {
243		Exitf("dynamic linking required on %s; -d flag cannot be used", buildcfg.GOOS)
244	}
245
246	isPowerOfTwo := func(n int64) bool {
247		return n > 0 && n&(n-1) == 0
248	}
249	if *FlagRound != -1 && (*FlagRound < 4096 || !isPowerOfTwo(*FlagRound)) {
250		Exitf("invalid -R value 0x%x", *FlagRound)
251	}
252
253	checkStrictDups = *FlagStrictDups
254
255	switch flagW {
256	case ternaryFlagFalse:
257		*FlagW = false
258	case ternaryFlagTrue:
259		*FlagW = true
260	case ternaryFlagUnset:
261		*FlagW = *FlagS // -s implies -w if not explicitly set
262		if ctxt.IsDarwin() && ctxt.BuildMode == BuildModeCShared {
263			*FlagW = true // default to -w in c-shared mode on darwin, see #61229
264		}
265	}
266
267	if !buildcfg.Experiment.RegabiWrappers {
268		abiInternalVer = 0
269	}
270
271	startProfile()
272	if ctxt.BuildMode == BuildModeUnset {
273		ctxt.BuildMode.Set("exe")
274	}
275
276	if ctxt.BuildMode != BuildModeShared && flag.NArg() != 1 {
277		usage()
278	}
279
280	if *flagOutfile == "" {
281		*flagOutfile = "a.out"
282		if ctxt.HeadType == objabi.Hwindows {
283			*flagOutfile += ".exe"
284		}
285	}
286
287	interpreter = *flagInterpreter
288
289	if *flagBuildid == "" && ctxt.Target.IsOpenbsd() {
290		// TODO(jsing): Remove once direct syscalls are no longer in use.
291		// OpenBSD 6.7 onwards will not permit direct syscalls from a
292		// dynamically linked binary unless it identifies the binary
293		// contains a .note.go.buildid ELF note. See issue #36435.
294		*flagBuildid = "go-openbsd"
295	}
296
297	// enable benchmarking
298	var bench *benchmark.Metrics
299	if len(*benchmarkFlag) != 0 {
300		if *benchmarkFlag == "mem" {
301			bench = benchmark.New(benchmark.GC, *benchmarkFileFlag)
302		} else if *benchmarkFlag == "cpu" {
303			bench = benchmark.New(benchmark.NoGC, *benchmarkFileFlag)
304		} else {
305			Errorf(nil, "unknown benchmark flag: %q", *benchmarkFlag)
306			usage()
307		}
308	}
309
310	bench.Start("libinit")
311	libinit(ctxt) // creates outfile
312	bench.Start("computeTLSOffset")
313	ctxt.computeTLSOffset()
314	bench.Start("Archinit")
315	thearch.Archinit(ctxt)
316
317	if ctxt.linkShared && !ctxt.IsELF {
318		Exitf("-linkshared can only be used on elf systems")
319	}
320
321	if ctxt.Debugvlog != 0 {
322		onOff := func(b bool) string {
323			if b {
324				return "on"
325			}
326			return "off"
327		}
328		ctxt.Logf("build mode: %s, symbol table: %s, DWARF: %s\n", ctxt.BuildMode, onOff(!*FlagS), onOff(dwarfEnabled(ctxt)))
329		ctxt.Logf("HEADER = -H%d -T0x%x -R0x%x\n", ctxt.HeadType, uint64(*FlagTextAddr), uint32(*FlagRound))
330	}
331
332	zerofp := goobj.FingerprintType{}
333	switch ctxt.BuildMode {
334	case BuildModeShared:
335		for i := 0; i < flag.NArg(); i++ {
336			arg := flag.Arg(i)
337			parts := strings.SplitN(arg, "=", 2)
338			var pkgpath, file string
339			if len(parts) == 1 {
340				pkgpath, file = "main", arg
341			} else {
342				pkgpath, file = parts[0], parts[1]
343			}
344			pkglistfornote = append(pkglistfornote, pkgpath...)
345			pkglistfornote = append(pkglistfornote, '\n')
346			addlibpath(ctxt, "command line", "command line", file, pkgpath, "", zerofp)
347		}
348	case BuildModePlugin:
349		addlibpath(ctxt, "command line", "command line", flag.Arg(0), *flagPluginPath, "", zerofp)
350	default:
351		addlibpath(ctxt, "command line", "command line", flag.Arg(0), "main", "", zerofp)
352	}
353	bench.Start("loadlib")
354	ctxt.loadlib()
355
356	bench.Start("inittasks")
357	ctxt.inittasks()
358
359	bench.Start("deadcode")
360	deadcode(ctxt)
361
362	bench.Start("linksetup")
363	ctxt.linksetup()
364
365	bench.Start("dostrdata")
366	ctxt.dostrdata()
367	if buildcfg.Experiment.FieldTrack {
368		bench.Start("fieldtrack")
369		fieldtrack(ctxt.Arch, ctxt.loader)
370	}
371
372	bench.Start("dwarfGenerateDebugInfo")
373	dwarfGenerateDebugInfo(ctxt)
374
375	bench.Start("callgraph")
376	ctxt.callgraph()
377
378	bench.Start("doStackCheck")
379	ctxt.doStackCheck()
380
381	bench.Start("mangleTypeSym")
382	ctxt.mangleTypeSym()
383
384	if ctxt.IsELF {
385		bench.Start("doelf")
386		ctxt.doelf()
387	}
388	if ctxt.IsDarwin() {
389		bench.Start("domacho")
390		ctxt.domacho()
391	}
392	if ctxt.IsWindows() {
393		bench.Start("dope")
394		ctxt.dope()
395		bench.Start("windynrelocsyms")
396		ctxt.windynrelocsyms()
397	}
398	if ctxt.IsAIX() {
399		bench.Start("doxcoff")
400		ctxt.doxcoff()
401	}
402
403	bench.Start("textbuildid")
404	ctxt.textbuildid()
405	bench.Start("addexport")
406	ctxt.setArchSyms()
407	ctxt.addexport()
408	bench.Start("Gentext")
409	thearch.Gentext(ctxt, ctxt.loader) // trampolines, call stubs, etc.
410
411	bench.Start("textaddress")
412	ctxt.textaddress()
413	bench.Start("typelink")
414	ctxt.typelink()
415	bench.Start("buildinfo")
416	ctxt.buildinfo()
417	bench.Start("pclntab")
418	containers := ctxt.findContainerSyms()
419	pclnState := ctxt.pclntab(containers)
420	bench.Start("findfunctab")
421	ctxt.findfunctab(pclnState, containers)
422	bench.Start("dwarfGenerateDebugSyms")
423	dwarfGenerateDebugSyms(ctxt)
424	bench.Start("symtab")
425	symGroupType := ctxt.symtab(pclnState)
426	bench.Start("dodata")
427	ctxt.dodata(symGroupType)
428	bench.Start("address")
429	order := ctxt.address()
430	bench.Start("dwarfcompress")
431	dwarfcompress(ctxt)
432	bench.Start("layout")
433	filesize := ctxt.layout(order)
434
435	// Write out the output file.
436	// It is split into two parts (Asmb and Asmb2). The first
437	// part writes most of the content (sections and segments),
438	// for which we have computed the size and offset, in a
439	// mmap'd region. The second part writes more content, for
440	// which we don't know the size.
441	if ctxt.Arch.Family != sys.Wasm {
442		// Don't mmap if we're building for Wasm. Wasm file
443		// layout is very different so filesize is meaningless.
444		if err := ctxt.Out.Mmap(filesize); err != nil {
445			Exitf("mapping output file failed: %v", err)
446		}
447	}
448	// asmb will redirect symbols to the output file mmap, and relocations
449	// will be applied directly there.
450	bench.Start("Asmb")
451	asmb(ctxt)
452
453	exitIfErrors()
454
455	// Generate additional symbols for the native symbol table just prior
456	// to code generation.
457	bench.Start("GenSymsLate")
458	if thearch.GenSymsLate != nil {
459		thearch.GenSymsLate(ctxt, ctxt.loader)
460	}
461
462	bench.Start("Asmb2")
463	asmb2(ctxt)
464
465	bench.Start("Munmap")
466	ctxt.Out.Close() // Close handles Munmapping if necessary.
467
468	bench.Start("hostlink")
469	ctxt.hostlink()
470	if ctxt.Debugvlog != 0 {
471		ctxt.Logf("%s", ctxt.loader.Stat())
472		ctxt.Logf("%d liveness data\n", liveness)
473	}
474	bench.Start("Flush")
475	ctxt.Bso.Flush()
476	bench.Start("archive")
477	ctxt.archive()
478	bench.Report(os.Stdout)
479
480	errorexit()
481}
482
483type Rpath struct {
484	set bool
485	val string
486}
487
488func (r *Rpath) Set(val string) error {
489	r.set = true
490	r.val = val
491	return nil
492}
493
494func (r *Rpath) String() string {
495	return r.val
496}
497
498func startProfile() {
499	if *cpuprofile != "" {
500		f, err := os.Create(*cpuprofile)
501		if err != nil {
502			log.Fatalf("%v", err)
503		}
504		if err := pprof.StartCPUProfile(f); err != nil {
505			log.Fatalf("%v", err)
506		}
507		AtExit(func() {
508			pprof.StopCPUProfile()
509			if err = f.Close(); err != nil {
510				log.Fatalf("error closing cpu profile: %v", err)
511			}
512		})
513	}
514	if *memprofile != "" {
515		if *memprofilerate != 0 {
516			runtime.MemProfileRate = int(*memprofilerate)
517		}
518		f, err := os.Create(*memprofile)
519		if err != nil {
520			log.Fatalf("%v", err)
521		}
522		AtExit(func() {
523			// Profile all outstanding allocations.
524			runtime.GC()
525			// compilebench parses the memory profile to extract memstats,
526			// which are only written in the legacy pprof format.
527			// See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap.
528			const writeLegacyFormat = 1
529			if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
530				log.Fatalf("%v", err)
531			}
532			// Close the file after writing the profile.
533			if err := f.Close(); err != nil {
534				log.Fatalf("could not close %v: %v", *memprofile, err)
535			}
536		})
537	}
538}
539