From 2249dc892d8fb95ad2b0ce0eb4278110534f31e4 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Mon, 15 Oct 2018 00:35:59 -0700 Subject: [PATCH] Turn symbol_inject into a reusable package Bug: 31559095 Test: m blueprint_tools Change-Id: I0f7a9f14111af26d753db547c6de313a7079658a --- .../Android.bp | 5 +- symbol_inject/cmd/Android.bp | 21 ++++ symbol_inject/cmd/symbol_inject.go | 97 +++++++++++++++++++ {cmd/symbol_inject => symbol_inject}/elf.go | 2 +- .../elf_symboldata_test.go | 2 +- .../elf_test.go | 2 +- {cmd/symbol_inject => symbol_inject}/macho.go | 2 +- .../macho_symboldata_test.go | 2 +- .../macho_test.go | 2 +- {cmd/symbol_inject => symbol_inject}/pe.go | 2 +- .../pe_symboldata_test.go | 2 +- .../pe_test.go | 2 +- .../symbol_inject.go | 84 +--------------- .../symbol_inject_test.go | 2 +- 14 files changed, 135 insertions(+), 92 deletions(-) rename {cmd/symbol_inject => symbol_inject}/Android.bp (90%) create mode 100644 symbol_inject/cmd/Android.bp create mode 100644 symbol_inject/cmd/symbol_inject.go rename {cmd/symbol_inject => symbol_inject}/elf.go (99%) rename {cmd/symbol_inject => symbol_inject}/elf_symboldata_test.go (99%) rename {cmd/symbol_inject => symbol_inject}/elf_test.go (98%) rename {cmd/symbol_inject => symbol_inject}/macho.go (99%) rename {cmd/symbol_inject => symbol_inject}/macho_symboldata_test.go (99%) rename {cmd/symbol_inject => symbol_inject}/macho_test.go (98%) rename {cmd/symbol_inject => symbol_inject}/pe.go (99%) rename {cmd/symbol_inject => symbol_inject}/pe_symboldata_test.go (99%) rename {cmd/symbol_inject => symbol_inject}/pe_test.go (99%) rename {cmd/symbol_inject => symbol_inject}/symbol_inject.go (72%) rename {cmd/symbol_inject => symbol_inject}/symbol_inject_test.go (98%) diff --git a/cmd/symbol_inject/Android.bp b/symbol_inject/Android.bp similarity index 90% rename from cmd/symbol_inject/Android.bp rename to symbol_inject/Android.bp index a2ea12bfe..8308043cd 100644 --- a/cmd/symbol_inject/Android.bp +++ b/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", diff --git a/symbol_inject/cmd/Android.bp b/symbol_inject/cmd/Android.bp new file mode 100644 index 000000000..ee2f25932 --- /dev/null +++ b/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", + ], +} diff --git a/symbol_inject/cmd/symbol_inject.go b/symbol_inject/cmd/symbol_inject.go new file mode 100644 index 000000000..09f444564 --- /dev/null +++ b/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) + } +} diff --git a/cmd/symbol_inject/elf.go b/symbol_inject/elf.go similarity index 99% rename from cmd/symbol_inject/elf.go rename to symbol_inject/elf.go index d94877d47..8742cbd1a 100644 --- a/cmd/symbol_inject/elf.go +++ b/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" diff --git a/cmd/symbol_inject/elf_symboldata_test.go b/symbol_inject/elf_symboldata_test.go similarity index 99% rename from cmd/symbol_inject/elf_symboldata_test.go rename to symbol_inject/elf_symboldata_test.go index 9ba7153fd..b2f11482c 100644 --- a/cmd/symbol_inject/elf_symboldata_test.go +++ b/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" diff --git a/cmd/symbol_inject/elf_test.go b/symbol_inject/elf_test.go similarity index 98% rename from cmd/symbol_inject/elf_test.go rename to symbol_inject/elf_test.go index 30b46a5e2..aceee4418 100644 --- a/cmd/symbol_inject/elf_test.go +++ b/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" diff --git a/cmd/symbol_inject/macho.go b/symbol_inject/macho.go similarity index 99% rename from cmd/symbol_inject/macho.go rename to symbol_inject/macho.go index be49f8b29..6ee3f4fbd 100644 --- a/cmd/symbol_inject/macho.go +++ b/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" diff --git a/cmd/symbol_inject/macho_symboldata_test.go b/symbol_inject/macho_symboldata_test.go similarity index 99% rename from cmd/symbol_inject/macho_symboldata_test.go rename to symbol_inject/macho_symboldata_test.go index 3100a8108..7336a27d0 100644 --- a/cmd/symbol_inject/macho_symboldata_test.go +++ b/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" diff --git a/cmd/symbol_inject/macho_test.go b/symbol_inject/macho_test.go similarity index 98% rename from cmd/symbol_inject/macho_test.go rename to symbol_inject/macho_test.go index 7acab23d6..50df1315d 100644 --- a/cmd/symbol_inject/macho_test.go +++ b/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" diff --git a/cmd/symbol_inject/pe.go b/symbol_inject/pe.go similarity index 99% rename from cmd/symbol_inject/pe.go rename to symbol_inject/pe.go index 12f35ee04..58cf91a5a 100644 --- a/cmd/symbol_inject/pe.go +++ b/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" diff --git a/cmd/symbol_inject/pe_symboldata_test.go b/symbol_inject/pe_symboldata_test.go similarity index 99% rename from cmd/symbol_inject/pe_symboldata_test.go rename to symbol_inject/pe_symboldata_test.go index edc1c976c..5c0fd70c4 100644 --- a/cmd/symbol_inject/pe_symboldata_test.go +++ b/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" diff --git a/cmd/symbol_inject/pe_test.go b/symbol_inject/pe_test.go similarity index 99% rename from cmd/symbol_inject/pe_test.go rename to symbol_inject/pe_test.go index 21a0bc45c..df7bac3cb 100644 --- a/cmd/symbol_inject/pe_test.go +++ b/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" diff --git a/cmd/symbol_inject/symbol_inject.go b/symbol_inject/symbol_inject.go similarity index 72% rename from cmd/symbol_inject/symbol_inject.go rename to symbol_inject/symbol_inject.go index d0f01c536..ebf05c871 100644 --- a/cmd/symbol_inject/symbol_inject.go +++ b/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 diff --git a/cmd/symbol_inject/symbol_inject_test.go b/symbol_inject/symbol_inject_test.go similarity index 98% rename from cmd/symbol_inject/symbol_inject_test.go rename to symbol_inject/symbol_inject_test.go index dbee39a06..77ec7d942 100644 --- a/cmd/symbol_inject/symbol_inject_test.go +++ b/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"