mirror of
https://github.com/golang/go
synced 2024-11-12 03:40:21 -07:00
cmd/dist: remove the use of debug/elf package
debug/elf is only needed to determine the endianness of the host machine, which is easy to do without debug/elf. Fixes #15180. Change-Id: I21035ed3884871270765a1ca3b812a5d4890a7ee Reviewed-on: https://go-review.googlesource.com/21662 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Minux Ma <minux@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
24744f6561
commit
2e2481ed34
37
src/cmd/dist/util.go
vendored
37
src/cmd/dist/util.go
vendored
@ -6,9 +6,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"debug/elf"
|
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -442,13 +441,8 @@ func main() {
|
|||||||
case strings.Contains(out, "ppc64"):
|
case strings.Contains(out, "ppc64"):
|
||||||
gohostarch = "ppc64"
|
gohostarch = "ppc64"
|
||||||
case strings.Contains(out, "mips64"):
|
case strings.Contains(out, "mips64"):
|
||||||
file, err := elf.Open(os.Args[0])
|
gohostarch = "mips64"
|
||||||
if err != nil {
|
if elfIsLittleEndian(os.Args[0]) {
|
||||||
fatal("failed to open %s to determine endianness: %v", os.Args[0], err)
|
|
||||||
}
|
|
||||||
if file.FileHeader.ByteOrder == binary.BigEndian {
|
|
||||||
gohostarch = "mips64"
|
|
||||||
} else {
|
|
||||||
gohostarch = "mips64le"
|
gohostarch = "mips64le"
|
||||||
}
|
}
|
||||||
case strings.Contains(out, "s390x"):
|
case strings.Contains(out, "s390x"):
|
||||||
@ -556,3 +550,28 @@ func min(a, b int) int {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// elfIsLittleEndian detects if the ELF file is little endian.
|
||||||
|
func elfIsLittleEndian(fn string) bool {
|
||||||
|
// read the ELF file header to determine the endianness without using the
|
||||||
|
// debug/elf package.
|
||||||
|
file, err := os.Open(fn)
|
||||||
|
if err != nil {
|
||||||
|
fatal("failed to open file to determine endianness: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
var hdr [16]byte
|
||||||
|
if _, err := io.ReadFull(file, hdr[:]); err != nil {
|
||||||
|
fatal("failed to read ELF header to determine endianness: %v", err)
|
||||||
|
}
|
||||||
|
// hdr[5] is EI_DATA byte, 1 is ELFDATA2LSB and 2 is ELFDATA2MSB
|
||||||
|
switch hdr[5] {
|
||||||
|
default:
|
||||||
|
fatal("unknown ELF endianness of %s: EI_DATA = %d", fn, hdr[5])
|
||||||
|
case 1:
|
||||||
|
return true
|
||||||
|
case 2:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
panic("unreachable")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user