Commit 804a4024 authored by Elias Naur's avatar Elias Naur

cmd/link/internal/ld: don't leave files open in a loop

Noticed by Ingo Oeser in his review of CL 168321.

Change-Id: I2f39db675a7c22b395062a11903657a9d0d1956d
Reviewed-on: https://go-review.googlesource.com/c/go/+/168560
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 4a522675
...@@ -694,14 +694,20 @@ func Asmbmacho(ctxt *Link) { ...@@ -694,14 +694,20 @@ func Asmbmacho(ctxt *Link) {
} }
} }
} }
load, err := hostobjMachoPlatform(hostobj) foundLoad := false
for _, h := range hostobj {
load, err := hostobjMachoPlatform(&h)
if err != nil { if err != nil {
Exitf("%v", err) Exitf("%v", err)
} }
if load != nil { if load != nil {
ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data))) ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data)))
copy(ml.data, load.cmd.data) copy(ml.data, load.cmd.data)
} else if ctxt.LinkMode == LinkInternal { foundLoad = true
break
}
}
if !foundLoad && ctxt.LinkMode == LinkInternal {
// For lldb, must say LC_VERSION_MIN_MACOSX or else // For lldb, must say LC_VERSION_MIN_MACOSX or else
// it won't know that this Mach-O binary is from OS X // it won't know that this Mach-O binary is from OS X
// (could be iOS or WatchOS instead). // (could be iOS or WatchOS instead).
...@@ -1027,9 +1033,8 @@ func Machoemitreloc(ctxt *Link) { ...@@ -1027,9 +1033,8 @@ func Machoemitreloc(ctxt *Link) {
} }
// hostobjMachoPlatform returns the first platform load command found // hostobjMachoPlatform returns the first platform load command found
// in the host objects, if any. // in the host object, if any.
func hostobjMachoPlatform(hostobj []Hostobj) (*MachoPlatformLoad, error) { func hostobjMachoPlatform(h *Hostobj) (*MachoPlatformLoad, error) {
for _, h := range hostobj {
f, err := os.Open(h.file) f, err := os.Open(h.file)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: failed to open host object: %v\n", h.file, err) return nil, fmt.Errorf("%s: failed to open host object: %v\n", h.file, err)
...@@ -1041,15 +1046,7 @@ func hostobjMachoPlatform(hostobj []Hostobj) (*MachoPlatformLoad, error) { ...@@ -1041,15 +1046,7 @@ func hostobjMachoPlatform(hostobj []Hostobj) (*MachoPlatformLoad, error) {
// Not a valid Mach-O file. // Not a valid Mach-O file.
return nil, nil return nil, nil
} }
load, err := peekMachoPlatform(m) return peekMachoPlatform(m)
if err != nil {
return nil, err
}
if load != nil {
return load, nil
}
}
return nil, nil
} }
// peekMachoPlatform returns the first LC_VERSION_MIN_* or LC_BUILD_VERSION // peekMachoPlatform returns the first LC_VERSION_MIN_* or LC_BUILD_VERSION
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment