aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Waller <[email protected]>2013-02-17 14:35:39 +0000
committerPeter Waller <[email protected]>2013-02-18 09:43:11 +0000
commit11c6569b9911f4d41df9d90751458f74ca56350a (patch)
tree9456c9f04ec292d9a9752e2c757700c33530c38a
parentMerge pull request #2 from toqueteos/master (diff)
downloadgoupx-11c6569b9911f4d41df9d90751458f74ca56350a.tar.xz
goupx-11c6569b9911f4d41df9d90751458f74ca56350a.zip
Extract logic to separate package, move command to cmd/goupx
-rw-r--r--README.md4
-rw-r--r--hemfix/hemfix.go (renamed from goupx.go)73
-rw-r--r--main.go67
3 files changed, 76 insertions, 68 deletions
diff --git a/README.md b/README.md
index cce99c5..fb93c66 100644
--- a/README.md
+++ b/README.md
@@ -1,9 +1,9 @@
goupx - Fix golang executables to work with upx
-------------------------------------------
-Installation: `go get github.com/pwaller/goupx`
+Installation: `go get github.com/pwaller/goupx/`
-(or if you don't want to do it with root, `GOPATH=${PWD}/env go get github.com/pwaller/goupx` will install it to `${PWD}/env/bin/goupx`)
+(or if you don't want to do it with root, `GOPATH=${PWD}/env go get github.com/pwaller/goupx/` will install it to `${PWD}/env/bin/goupx`)
Usage: `goupx [filename]`
diff --git a/goupx.go b/hemfix/hemfix.go
index c4c0f6e..2425df1 100644
--- a/goupx.go
+++ b/hemfix/hemfix.go
@@ -1,8 +1,8 @@
-package main
+package hemfix
-/*
+/*
-goupx: Fix compiled go binaries so that they can be packed by
+goupx: Fix compiled go binaries so that they can be packed by
the universal packer for executables (upx)
Copyright (c) 2012 Peter Waller <[email protected]>
@@ -16,12 +16,12 @@ Based on hemfix.c Copyright (C) 2012 John Reiser, BitWagon Software LLC
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
-
+
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-
+
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -31,21 +31,11 @@ import (
ELF "debug/elf"
"encoding/binary"
"errors"
- "flag"
"io"
"log"
"os"
- "os/exec"
)
-const usageText = "usage: goupx [args...] path\n"
-
-// usage prints some nice output instead of panic stacktrace when an user calls goupx without arguments
-func usage() {
- os.Stderr.WriteString(usageText)
- flag.PrintDefaults()
-}
-
// The functions gethdr and writephdr are heavily influenced by code found at
// http://golang.org/src/pkg/debug/elf/file.go
@@ -112,7 +102,7 @@ func writephdr(f *ELF.File, dst int64, sw io.WriteSeeker, p *ELF.Prog) error {
func fixelf(elf *ELF.File, fd io.ReadWriteSeeker) error {
- // Determine where to write header (need
+ // Determine where to write header (need
off, sz, err := gethdr(elf, fd)
if err != nil {
return err
@@ -145,7 +135,7 @@ func fixelf(elf *ELF.File, fd io.ReadWriteSeeker) error {
return nil
}
-func fixfile(filename string) error {
+func FixFile(filename string) error {
fd, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return err
@@ -167,52 +157,3 @@ func fixfile(filename string) error {
}
return nil
}
-
-func main() {
-
- run_strip := flag.Bool("s", false, "run strip")
- run_upx := flag.Bool("u", true, "run upx")
-
- flag.Parse()
-
- if flag.NArg() != 1 {
- usage()
- return
- }
-
- defer func() {
- if err := recover(); err != nil {
- log.Print("Panicked. Giving up.")
- panic(err)
- return
- }
- }()
-
- input_file := flag.Arg(0)
- err := fixfile(input_file)
- if err != nil {
- log.Panicf("Failed to fix '%s': %v", input_file, err)
- }
- log.Print("File fixed!")
-
- if *run_strip {
- cmd := exec.Command("strip", "-s", input_file)
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- err = cmd.Run()
- if err != nil {
- log.Panic("strip failed: ", err)
- }
- }
-
- if *run_upx {
- cmd := exec.Command("upx", input_file)
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- err = cmd.Run()
- if err != nil {
- log.Panic("upx failed: ", err)
- }
- }
-
-}
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..0461b68
--- /dev/null
+++ b/main.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "flag"
+ "log"
+ "os"
+ "os/exec"
+
+ "github.com/pwaller/goupx/hemfix"
+)
+
+const usageText = "usage: goupx [args...] path\n"
+
+// usage prints some nice output instead of panic stacktrace when an user calls goupx without arguments
+func usage() {
+ os.Stderr.WriteString(usageText)
+ flag.PrintDefaults()
+}
+
+func main() {
+
+ run_strip := flag.Bool("s", false, "run strip")
+ run_upx := flag.Bool("u", true, "run upx")
+
+ flag.Parse()
+
+ if flag.NArg() != 1 {
+ usage()
+ return
+ }
+
+ defer func() {
+ if err := recover(); err != nil {
+ log.Print("Panicked. Giving up.")
+ panic(err)
+ return
+ }
+ }()
+
+ input_file := flag.Arg(0)
+ err := hemfix.FixFile(input_file)
+ if err != nil {
+ log.Panicf("Failed to fix '%s': %v", input_file, err)
+ }
+ log.Print("File fixed!")
+
+ if *run_strip {
+ cmd := exec.Command("strip", "-s", input_file)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ err = cmd.Run()
+ if err != nil {
+ log.Panic("strip failed: ", err)
+ }
+ }
+
+ if *run_upx {
+ cmd := exec.Command("upx", input_file)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ err = cmd.Run()
+ if err != nil {
+ log.Panic("upx failed: ", err)
+ }
+ }
+
+}