]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: remove the use of debug/elf package
authorShenghou Ma <minux@golang.org>
Thu, 7 Apr 2016 20:03:53 +0000 (16:03 -0400)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 4 May 2016 21:23:20 +0000 (21:23 +0000)
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>

src/cmd/dist/util.go

index 34f7372de8b093b3769a753d0ecdc94d056cb797..bbf3b75b5b24fb04fbf5b9d5d04f84e8f90736cc 100644 (file)
@@ -6,9 +6,8 @@ package main
 
 import (
        "bytes"
-       "debug/elf"
-       "encoding/binary"
        "fmt"
+       "io"
        "io/ioutil"
        "os"
        "os/exec"
@@ -442,13 +441,8 @@ func main() {
                case strings.Contains(out, "ppc64"):
                        gohostarch = "ppc64"
                case strings.Contains(out, "mips64"):
-                       file, err := elf.Open(os.Args[0])
-                       if err != nil {
-                               fatal("failed to open %s to determine endianness: %v", os.Args[0], err)
-                       }
-                       if file.FileHeader.ByteOrder == binary.BigEndian {
-                               gohostarch = "mips64"
-                       } else {
+                       gohostarch = "mips64"
+                       if elfIsLittleEndian(os.Args[0]) {
                                gohostarch = "mips64le"
                        }
                case strings.Contains(out, "s390x"):
@@ -556,3 +550,28 @@ func min(a, b int) int {
        }
        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")
+}