Turn symbol_inject into a reusable package

Bug: 31559095
Test: m blueprint_tools
Change-Id: I0f7a9f14111af26d753db547c6de313a7079658a
This commit is contained in:
Dan Willemsen 2018-10-15 00:35:59 -07:00
parent 4f644da0ad
commit 2249dc892d
14 changed files with 135 additions and 92 deletions

View File

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

View File

@ -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",
],
}

View File

@ -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)
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,25 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package main package symbol_inject
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io" "io"
"math" "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 var maxUint64 uint64 = math.MaxUint64
@ -39,71 +27,7 @@ type cantParseError struct {
error error
} }
func main() { func OpenFile(r io.ReaderAt) (*File, error) {
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) {
file, err := elfSymbolsFromFile(r) file, err := elfSymbolsFromFile(r)
if elfError, ok := err.(cantParseError); ok { if elfError, ok := err.(cantParseError); ok {
// Try as a mach-o file // Try as a mach-o file
@ -126,7 +50,7 @@ func openFile(r io.ReaderAt) (*File, error) {
return file, err 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) offset, size, err := findSymbol(file, symbol)
if err != nil { if err != nil {
return err return err
@ -239,7 +163,7 @@ type Section struct {
Size uint64 Size uint64
} }
func dumpSymbols(r io.ReaderAt) error { func DumpSymbols(r io.ReaderAt) error {
err := dumpElfSymbols(r) err := dumpElfSymbols(r)
if elfError, ok := err.(cantParseError); ok { if elfError, ok := err.(cantParseError); ok {
// Try as a mach-o file // Try as a mach-o file

View File

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