]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/archive: skip sentinel archive entries created by Go cmd
authorThan McIntosh <thanm@google.com>
Tue, 15 Aug 2023 14:24:41 +0000 (10:24 -0400)
committerThan McIntosh <thanm@google.com>
Tue, 15 Aug 2023 15:39:57 +0000 (15:39 +0000)
When reading an archive, check for the presence of sentinel entries
created by the Go command. These zero-sized marker entries don't contain
any useful symbols, but rather are there to communicate info to the
linker; ignore them during symbol dumping.

Fixes #62036.

Change-Id: Ied017b0c5b92a3cf6fd13bb9c9f3a9664e4f20f8
Reviewed-on: https://go-review.googlesource.com/c/go/+/519635
Run-TryBot: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/go/testdata/script/cgo_suspect_flag_force_external.txt
src/cmd/internal/archive/archive.go
src/cmd/internal/objfile/goobj.go

index e5bcdc6cfa4ead276d1fecfc5225812a33855474..d555278865baf2b3f8c4904e17dd7b604b23614f 100644 (file)
@@ -95,6 +95,9 @@ go build -ldflags=-tmpdir=tmp4 -o $devnull ./usesExplicitCgo &
 [cgolinkext] go list ./usesInternalCgo
 [!cgolinkext] go build '-ldflags=-tmpdir=tmp5 -linkmode=internal' -o $devnull ./usesInternalCgo &
 
+# Sixth build: explicit CGO use in a non-main package.
+go build -o p.a ./nonMainPackageUsesExplicitCgo &
+
 wait
 
 # Check first build: no external linking expected
@@ -113,6 +116,10 @@ exists tmp4/go.o
 # Fifth build: explicit CGO, -linkmode=internal.
 ! exists tmp5/go.o
 
+# Sixth build: make sure that "go tool nm" doesn't get confused
+# by the presence of the "preferlinkext" sentinel.
+go tool nm p.a
+
 -- go.mod --
 
 module cgo.example
@@ -153,3 +160,16 @@ import "C"
 func main() {
      println(C.meaningOfLife())
 }
+
+-- nonMainPackageUsesExplicitCgo/main.go --
+
+package p
+
+/*
+int meaningOfLife() { return 42; }
+*/
+import "C"
+
+func PrintIt() {
+     println(C.meaningOfLife())
+}
index 8ac50e202f7a8da2bafbab170d294ab9269acb6e..393034d7769f2d662de1cc3db1abf2dec48316ec 100644 (file)
@@ -70,6 +70,7 @@ const (
        EntryPkgDef EntryType = iota
        EntryGoObj
        EntryNativeObj
+       EntrySentinelNonObj
 )
 
 func (e *Entry) String() string {
@@ -357,6 +358,23 @@ func (r *objReader) parseArchive(verbose bool) error {
                                Data:  Data{r.offset, size},
                        })
                        r.skip(size)
+               case "preferlinkext", "dynimportfail":
+                       if size == 0 {
+                               // These are not actual objects, but rather sentinel
+                               // entries put into the archive by the Go command to
+                               // be read by the linker. See #62036.
+                               r.a.Entries = append(r.a.Entries, Entry{
+                                       Name:  name,
+                                       Type:  EntrySentinelNonObj,
+                                       Mtime: mtime,
+                                       Uid:   uid,
+                                       Gid:   gid,
+                                       Mode:  mode,
+                                       Data:  Data{r.offset, size},
+                               })
+                               break
+                       }
+                       fallthrough
                default:
                        var typ EntryType
                        var o *GoObj
index 24d2d0bb5c7b76c9ab91f9123d298f04b1e1c295..a0a2a1799b39e7193354934c9830ee345135b5ae 100644 (file)
@@ -35,7 +35,7 @@ func openGoFile(f *os.File) (*File, error) {
 L:
        for _, e := range a.Entries {
                switch e.Type {
-               case archive.EntryPkgDef:
+               case archive.EntryPkgDef, archive.EntrySentinelNonObj:
                        continue
                case archive.EntryGoObj:
                        o := e.Obj