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