Browse Source

Turn symbol_inject into a reusable package

Bug: 31559095
Test: m blueprint_tools
Change-Id: I0f7a9f14111af26d753db547c6de313a7079658a
Dan Willemsen 5 years ago
parent
commit
2249dc892d

+ 3 - 2
cmd/symbol_inject/Android.bp → symbol_inject/Android.bp

@@ -12,8 +12,9 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-blueprint_go_binary {
-    name: "symbol_inject",
+bootstrap_go_package {
+    name: "soong-symbol_inject",
+    pkgPath: "android/soong/symbol_inject",
     srcs: [
         "symbol_inject.go",
         "elf.go",

+ 21 - 0
symbol_inject/cmd/Android.bp

@@ -0,0 +1,21 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+blueprint_go_binary {
+    name: "symbol_inject",
+    deps: ["soong-symbol_inject"],
+    srcs: [
+        "symbol_inject.go",
+    ],
+}

+ 97 - 0
symbol_inject/cmd/symbol_inject.go

@@ -0,0 +1,97 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package main
+
+import (
+	"flag"
+	"fmt"
+	"os"
+
+	"android/soong/symbol_inject"
+)
+
+var (
+	input  = flag.String("i", "", "input file")
+	output = flag.String("o", "", "output file")
+	symbol = flag.String("s", "", "symbol to inject into")
+	from   = flag.String("from", "", "optional existing value of the symbol for verification")
+	value  = flag.String("v", "", "value to inject into symbol")
+
+	dump = flag.Bool("dump", false, "dump the symbol table for copying into a test")
+)
+
+func main() {
+	flag.Parse()
+
+	usageError := func(s string) {
+		fmt.Fprintln(os.Stderr, s)
+		flag.Usage()
+		os.Exit(1)
+	}
+
+	if *input == "" {
+		usageError("-i is required")
+	}
+
+	if !*dump {
+		if *output == "" {
+			usageError("-o is required")
+		}
+
+		if *symbol == "" {
+			usageError("-s is required")
+		}
+
+		if *value == "" {
+			usageError("-v is required")
+		}
+	}
+
+	r, err := os.Open(*input)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err.Error())
+		os.Exit(2)
+	}
+	defer r.Close()
+
+	if *dump {
+		err := symbol_inject.DumpSymbols(r)
+		if err != nil {
+			fmt.Fprintln(os.Stderr, err.Error())
+			os.Exit(6)
+		}
+		return
+	}
+
+	w, err := os.OpenFile(*output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err.Error())
+		os.Exit(3)
+	}
+	defer w.Close()
+
+	file, err := symbol_inject.OpenFile(r)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err.Error())
+		os.Exit(4)
+	}
+
+	err = symbol_inject.InjectSymbol(file, w, *symbol, *value, *from)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err.Error())
+		os.Remove(*output)
+		os.Exit(5)
+	}
+}

+ 1 - 1
cmd/symbol_inject/elf.go → symbol_inject/elf.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/elf"

+ 1 - 1
cmd/symbol_inject/elf_symboldata_test.go → symbol_inject/elf_symboldata_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import "debug/elf"
 

+ 1 - 1
cmd/symbol_inject/elf_test.go → symbol_inject/elf_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"strconv"

+ 1 - 1
cmd/symbol_inject/macho.go → symbol_inject/macho.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/macho"

+ 1 - 1
cmd/symbol_inject/macho_symboldata_test.go → symbol_inject/macho_symboldata_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/macho"

+ 1 - 1
cmd/symbol_inject/macho_test.go → symbol_inject/macho_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/macho"

+ 1 - 1
cmd/symbol_inject/pe.go → symbol_inject/pe.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/pe"

+ 1 - 1
cmd/symbol_inject/pe_symboldata_test.go → symbol_inject/pe_symboldata_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/pe"

+ 1 - 1
cmd/symbol_inject/pe_test.go → symbol_inject/pe_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"debug/pe"

+ 4 - 80
cmd/symbol_inject/symbol_inject.go → symbol_inject/symbol_inject.go

@@ -12,25 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"bytes"
-	"flag"
 	"fmt"
 	"io"
 	"math"
-	"os"
-)
-
-var (
-	input  = flag.String("i", "", "input file")
-	output = flag.String("o", "", "output file")
-	symbol = flag.String("s", "", "symbol to inject into")
-	from   = flag.String("from", "", "optional existing value of the symbol for verification")
-	value  = flag.String("v", "", "value to inject into symbol")
-
-	dump = flag.Bool("dump", false, "dump the symbol table for copying into a test")
 )
 
 var maxUint64 uint64 = math.MaxUint64
@@ -39,71 +27,7 @@ type cantParseError struct {
 	error
 }
 
-func main() {
-	flag.Parse()
-
-	usageError := func(s string) {
-		fmt.Fprintln(os.Stderr, s)
-		flag.Usage()
-		os.Exit(1)
-	}
-
-	if *input == "" {
-		usageError("-i is required")
-	}
-
-	if !*dump {
-		if *output == "" {
-			usageError("-o is required")
-		}
-
-		if *symbol == "" {
-			usageError("-s is required")
-		}
-
-		if *value == "" {
-			usageError("-v is required")
-		}
-	}
-
-	r, err := os.Open(*input)
-	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(2)
-	}
-	defer r.Close()
-
-	if *dump {
-		err := dumpSymbols(r)
-		if err != nil {
-			fmt.Fprintln(os.Stderr, err.Error())
-			os.Exit(6)
-		}
-		return
-	}
-
-	w, err := os.OpenFile(*output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
-	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(3)
-	}
-	defer w.Close()
-
-	file, err := openFile(r)
-	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
-		os.Exit(4)
-	}
-
-	err = injectSymbol(file, w, *symbol, *value, *from)
-	if err != nil {
-		fmt.Fprintln(os.Stderr, err.Error())
-		os.Remove(*output)
-		os.Exit(5)
-	}
-}
-
-func openFile(r io.ReaderAt) (*File, error) {
+func OpenFile(r io.ReaderAt) (*File, error) {
 	file, err := elfSymbolsFromFile(r)
 	if elfError, ok := err.(cantParseError); ok {
 		// Try as a mach-o file
@@ -126,7 +50,7 @@ func openFile(r io.ReaderAt) (*File, error) {
 	return file, err
 }
 
-func injectSymbol(file *File, w io.Writer, symbol, value, from string) error {
+func InjectSymbol(file *File, w io.Writer, symbol, value, from string) error {
 	offset, size, err := findSymbol(file, symbol)
 	if err != nil {
 		return err
@@ -239,7 +163,7 @@ type Section struct {
 	Size   uint64
 }
 
-func dumpSymbols(r io.ReaderAt) error {
+func DumpSymbols(r io.ReaderAt) error {
 	err := dumpElfSymbols(r)
 	if elfError, ok := err.(cantParseError); ok {
 		// Try as a mach-o file

+ 1 - 1
cmd/symbol_inject/symbol_inject_test.go → symbol_inject/symbol_inject_test.go

@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package main
+package symbol_inject
 
 import (
 	"bytes"