File size: 11,434 Bytes
d7a5f2f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// go-specific code shared across loaders (5l, 6l, 8l).
package ld
import (
"cmd/internal/bio"
"cmd/internal/obj"
"cmd/internal/objabi"
"cmd/internal/sys"
"cmd/link/internal/loader"
"cmd/link/internal/sym"
"debug/elf"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
)
// go-specific code shared across loaders (5l, 6l, 8l).
// TODO:
// generate debugging section in binary.
// once the dust settles, try to move some code to
// libmach, so that other linkers and ar can share.
func ldpkg(ctxt *Link, f *bio.Reader, lib *sym.Library, length int64, filename string) {
if *flagG {
return
}
if int64(int(length)) != length {
fmt.Fprintf(os.Stderr, "%s: too much pkg data in %s\n", os.Args[0], filename)
return
}
bdata := make([]byte, length)
if _, err := io.ReadFull(f, bdata); err != nil {
fmt.Fprintf(os.Stderr, "%s: short pkg read %s\n", os.Args[0], filename)
return
}
data := string(bdata)
// process header lines
for data != "" {
var line string
line, data, _ = strings.Cut(data, "\n")
if line == "main" {
lib.Main = true
}
if line == "" {
break
}
}
// look for cgo section
p0 := strings.Index(data, "\n$$ // cgo")
var p1 int
if p0 >= 0 {
p0 += p1
i := strings.IndexByte(data[p0+1:], '\n')
if i < 0 {
fmt.Fprintf(os.Stderr, "%s: found $$ // cgo but no newline in %s\n", os.Args[0], filename)
return
}
p0 += 1 + i
p1 = strings.Index(data[p0:], "\n$$")
if p1 < 0 {
p1 = strings.Index(data[p0:], "\n!\n")
}
if p1 < 0 {
fmt.Fprintf(os.Stderr, "%s: cannot find end of // cgo section in %s\n", os.Args[0], filename)
return
}
p1 += p0
loadcgo(ctxt, filename, objabi.PathToPrefix(lib.Pkg), data[p0:p1])
}
}
func loadcgo(ctxt *Link, file string, pkg string, p string) {
var directives [][]string
if err := json.NewDecoder(strings.NewReader(p)).Decode(&directives); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s: failed decoding cgo directives: %v\n", os.Args[0], file, err)
nerrors++
return
}
// Record the directives. We'll process them later after Symbols are created.
ctxt.cgodata = append(ctxt.cgodata, cgodata{file, pkg, directives})
}
// Set symbol attributes or flags based on cgo directives.
// Any newly discovered HOSTOBJ syms are added to 'hostObjSyms'.
func setCgoAttr(ctxt *Link, file string, pkg string, directives [][]string, hostObjSyms map[loader.Sym]struct{}) {
l := ctxt.loader
for _, f := range directives {
switch f[0] {
case "cgo_import_dynamic":
if len(f) < 2 || len(f) > 4 {
break
}
local := f[1]
remote := local
if len(f) > 2 {
remote = f[2]
}
lib := ""
if len(f) > 3 {
lib = f[3]
}
if *FlagD {
fmt.Fprintf(os.Stderr, "%s: %s: cannot use dynamic imports with -d flag\n", os.Args[0], file)
nerrors++
return
}
if local == "_" && remote == "_" {
// allow #pragma dynimport _ _ "foo.so"
// to force a link of foo.so.
havedynamic = 1
if ctxt.HeadType == objabi.Hdarwin {
machoadddynlib(lib, ctxt.LinkMode)
} else {
dynlib = append(dynlib, lib)
}
continue
}
q := ""
if before, after, found := strings.Cut(remote, "#"); found {
remote, q = before, after
}
s := l.LookupOrCreateSym(local, 0)
st := l.SymType(s)
if st == 0 || st == sym.SXREF || st == sym.SBSS || st == sym.SNOPTRBSS || st == sym.SHOSTOBJ {
l.SetSymDynimplib(s, lib)
l.SetSymExtname(s, remote)
l.SetSymDynimpvers(s, q)
if st != sym.SHOSTOBJ {
su := l.MakeSymbolUpdater(s)
su.SetType(sym.SDYNIMPORT)
} else {
hostObjSyms[s] = struct{}{}
}
havedynamic = 1
if lib != "" && ctxt.IsDarwin() {
machoadddynlib(lib, ctxt.LinkMode)
}
}
continue
case "cgo_import_static":
if len(f) != 2 {
break
}
local := f[1]
s := l.LookupOrCreateSym(local, 0)
su := l.MakeSymbolUpdater(s)
su.SetType(sym.SHOSTOBJ)
su.SetSize(0)
hostObjSyms[s] = struct{}{}
continue
case "cgo_export_static", "cgo_export_dynamic":
if len(f) < 2 || len(f) > 4 {
break
}
local := f[1]
remote := local
if len(f) > 2 {
remote = f[2]
}
// The compiler adds a fourth argument giving
// the definition ABI of function symbols.
abi := obj.ABI0
if len(f) > 3 {
var ok bool
abi, ok = obj.ParseABI(f[3])
if !ok {
fmt.Fprintf(os.Stderr, "%s: bad ABI in cgo_export directive %s\n", os.Args[0], f)
nerrors++
return
}
}
s := l.LookupOrCreateSym(local, sym.ABIToVersion(abi))
if l.SymType(s) == sym.SHOSTOBJ {
hostObjSyms[s] = struct{}{}
}
switch ctxt.BuildMode {
case BuildModeCShared, BuildModeCArchive, BuildModePlugin:
if s == l.Lookup("main", 0) {
continue
}
}
// export overrides import, for openbsd/cgo.
// see issue 4878.
if l.SymDynimplib(s) != "" {
l.SetSymDynimplib(s, "")
l.SetSymDynimpvers(s, "")
l.SetSymExtname(s, "")
var su *loader.SymbolBuilder
su = l.MakeSymbolUpdater(s)
su.SetType(0)
}
if !(l.AttrCgoExportStatic(s) || l.AttrCgoExportDynamic(s)) {
l.SetSymExtname(s, remote)
} else if l.SymExtname(s) != remote {
fmt.Fprintf(os.Stderr, "%s: conflicting cgo_export directives: %s as %s and %s\n", os.Args[0], l.SymName(s), l.SymExtname(s), remote)
nerrors++
return
}
// Mark exported symbols and also add them to
// the lists used for roots in the deadcode pass.
if f[0] == "cgo_export_static" {
if ctxt.LinkMode == LinkExternal && !l.AttrCgoExportStatic(s) {
// Static cgo exports appear
// in the exported symbol table.
ctxt.dynexp = append(ctxt.dynexp, s)
}
if ctxt.LinkMode == LinkInternal {
// For internal linking, we're
// responsible for resolving
// relocations from host objects.
// Record the right Go symbol
// version to use.
l.AddCgoExport(s)
}
l.SetAttrCgoExportStatic(s, true)
} else {
if ctxt.LinkMode == LinkInternal && !l.AttrCgoExportDynamic(s) {
// Dynamic cgo exports appear
// in the exported symbol table.
ctxt.dynexp = append(ctxt.dynexp, s)
}
l.SetAttrCgoExportDynamic(s, true)
}
continue
case "cgo_dynamic_linker":
if len(f) != 2 {
break
}
if *flagInterpreter == "" {
if interpreter != "" && interpreter != f[1] {
fmt.Fprintf(os.Stderr, "%s: conflict dynlinker: %s and %s\n", os.Args[0], interpreter, f[1])
nerrors++
return
}
interpreter = f[1]
}
continue
case "cgo_ldflag":
if len(f) != 2 {
break
}
ldflag = append(ldflag, f[1])
continue
}
fmt.Fprintf(os.Stderr, "%s: %s: invalid cgo directive: %q\n", os.Args[0], file, f)
nerrors++
}
return
}
// openbsdTrimLibVersion indicates whether a shared library is
// versioned and if it is, returns the unversioned name. The
// OpenBSD library naming scheme is lib<name>.so.<major>.<minor>
func openbsdTrimLibVersion(lib string) (string, bool) {
parts := strings.Split(lib, ".")
if len(parts) != 4 {
return "", false
}
if parts[1] != "so" {
return "", false
}
if _, err := strconv.Atoi(parts[2]); err != nil {
return "", false
}
if _, err := strconv.Atoi(parts[3]); err != nil {
return "", false
}
return fmt.Sprintf("%s.%s", parts[0], parts[1]), true
}
// dedupLibrariesOpenBSD dedups a list of shared libraries, treating versioned
// and unversioned libraries as equivalents. Versioned libraries are preferred
// and retained over unversioned libraries. This avoids the situation where
// the use of cgo results in a DT_NEEDED for a versioned library (for example,
// libc.so.96.1), while a dynamic import specifies an unversioned library (for
// example, libc.so) - this would otherwise result in two DT_NEEDED entries
// for the same library, resulting in a failure when ld.so attempts to load
// the Go binary.
func dedupLibrariesOpenBSD(ctxt *Link, libs []string) []string {
libraries := make(map[string]string)
for _, lib := range libs {
if name, ok := openbsdTrimLibVersion(lib); ok {
// Record unversioned name as seen.
seenlib[name] = true
libraries[name] = lib
} else if _, ok := libraries[lib]; !ok {
libraries[lib] = lib
}
}
libs = nil
for _, lib := range libraries {
libs = append(libs, lib)
}
sort.Strings(libs)
return libs
}
func dedupLibraries(ctxt *Link, libs []string) []string {
if ctxt.Target.IsOpenbsd() {
return dedupLibrariesOpenBSD(ctxt, libs)
}
return libs
}
var seenlib = make(map[string]bool)
func adddynlib(ctxt *Link, lib string) {
if seenlib[lib] || ctxt.LinkMode == LinkExternal {
return
}
seenlib[lib] = true
if ctxt.IsELF {
dsu := ctxt.loader.MakeSymbolUpdater(ctxt.DynStr)
if dsu.Size() == 0 {
dsu.Addstring("")
}
du := ctxt.loader.MakeSymbolUpdater(ctxt.Dynamic)
Elfwritedynent(ctxt.Arch, du, elf.DT_NEEDED, uint64(dsu.Addstring(lib)))
} else {
Errorf("adddynlib: unsupported binary format")
}
}
func Adddynsym(ldr *loader.Loader, target *Target, syms *ArchSyms, s loader.Sym) {
if ldr.SymDynid(s) >= 0 || target.LinkMode == LinkExternal {
return
}
if target.IsELF {
elfadddynsym(ldr, target, syms, s)
} else if target.HeadType == objabi.Hdarwin {
ldr.Errorf(s, "adddynsym: missed symbol (Extname=%s)", ldr.SymExtname(s))
} else if target.HeadType == objabi.Hwindows {
// already taken care of
} else {
ldr.Errorf(s, "adddynsym: unsupported binary format")
}
}
func fieldtrack(arch *sys.Arch, l *loader.Loader) {
var buf strings.Builder
for i := loader.Sym(1); i < loader.Sym(l.NSym()); i++ {
if name := l.SymName(i); strings.HasPrefix(name, "go:track.") {
if l.AttrReachable(i) {
l.SetAttrSpecial(i, true)
l.SetAttrNotInSymbolTable(i, true)
buf.WriteString(name[9:])
for p := l.Reachparent[i]; p != 0; p = l.Reachparent[p] {
buf.WriteString("\t")
buf.WriteString(l.SymName(p))
}
buf.WriteString("\n")
}
}
}
l.Reachparent = nil // we are done with it
if *flagFieldTrack == "" {
return
}
s := l.Lookup(*flagFieldTrack, 0)
if s == 0 || !l.AttrReachable(s) {
return
}
bld := l.MakeSymbolUpdater(s)
bld.SetType(sym.SDATA)
addstrdata(arch, l, *flagFieldTrack, buf.String())
}
func (ctxt *Link) addexport() {
// Track undefined external symbols during external link.
if ctxt.LinkMode == LinkExternal {
for _, s := range ctxt.Textp {
if ctxt.loader.AttrSpecial(s) || ctxt.loader.AttrSubSymbol(s) {
continue
}
relocs := ctxt.loader.Relocs(s)
for i := 0; i < relocs.Count(); i++ {
if rs := relocs.At(i).Sym(); rs != 0 {
if ctxt.loader.SymType(rs) == sym.Sxxx && !ctxt.loader.AttrLocal(rs) {
// sanity check
if len(ctxt.loader.Data(rs)) != 0 {
panic("expected no data on undef symbol")
}
su := ctxt.loader.MakeSymbolUpdater(rs)
su.SetType(sym.SUNDEFEXT)
}
}
}
}
}
// TODO(aix)
if ctxt.HeadType == objabi.Hdarwin || ctxt.HeadType == objabi.Haix {
return
}
// Add dynamic symbols.
for _, s := range ctxt.dynexp {
// Consistency check.
if !ctxt.loader.AttrReachable(s) {
panic("dynexp entry not reachable")
}
Adddynsym(ctxt.loader, &ctxt.Target, &ctxt.ArchSyms, s)
}
for _, lib := range dedupLibraries(ctxt, dynlib) {
adddynlib(ctxt, lib)
}
}
|