Dan Willemsen | 2249dc8 | 2018-10-15 00:35:59 -0700 | [diff] [blame] | 1 | // Copyright 2018 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | |
| 22 | "android/soong/symbol_inject" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | input = flag.String("i", "", "input file") |
| 27 | output = flag.String("o", "", "output file") |
| 28 | symbol = flag.String("s", "", "symbol to inject into") |
| 29 | from = flag.String("from", "", "optional existing value of the symbol for verification") |
| 30 | value = flag.String("v", "", "value to inject into symbol") |
| 31 | |
| 32 | dump = flag.Bool("dump", false, "dump the symbol table for copying into a test") |
| 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | flag.Parse() |
| 37 | |
| 38 | usageError := func(s string) { |
| 39 | fmt.Fprintln(os.Stderr, s) |
| 40 | flag.Usage() |
| 41 | os.Exit(1) |
| 42 | } |
| 43 | |
| 44 | if *input == "" { |
| 45 | usageError("-i is required") |
| 46 | } |
| 47 | |
| 48 | if !*dump { |
| 49 | if *output == "" { |
| 50 | usageError("-o is required") |
| 51 | } |
| 52 | |
| 53 | if *symbol == "" { |
| 54 | usageError("-s is required") |
| 55 | } |
| 56 | |
| 57 | if *value == "" { |
| 58 | usageError("-v is required") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | r, err := os.Open(*input) |
| 63 | if err != nil { |
| 64 | fmt.Fprintln(os.Stderr, err.Error()) |
| 65 | os.Exit(2) |
| 66 | } |
| 67 | defer r.Close() |
| 68 | |
| 69 | if *dump { |
| 70 | err := symbol_inject.DumpSymbols(r) |
| 71 | if err != nil { |
| 72 | fmt.Fprintln(os.Stderr, err.Error()) |
| 73 | os.Exit(6) |
| 74 | } |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | w, err := os.OpenFile(*output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) |
| 79 | if err != nil { |
| 80 | fmt.Fprintln(os.Stderr, err.Error()) |
| 81 | os.Exit(3) |
| 82 | } |
| 83 | defer w.Close() |
| 84 | |
| 85 | file, err := symbol_inject.OpenFile(r) |
| 86 | if err != nil { |
| 87 | fmt.Fprintln(os.Stderr, err.Error()) |
| 88 | os.Exit(4) |
| 89 | } |
| 90 | |
Dan Willemsen | 0db18c2 | 2018-10-15 17:05:35 -0700 | [diff] [blame] | 91 | err = symbol_inject.InjectStringSymbol(file, w, *symbol, *value, *from) |
Dan Willemsen | 2249dc8 | 2018-10-15 00:35:59 -0700 | [diff] [blame] | 92 | if err != nil { |
| 93 | fmt.Fprintln(os.Stderr, err.Error()) |
| 94 | os.Remove(*output) |
| 95 | os.Exit(5) |
| 96 | } |
Yabin Cui | 7f5f22b | 2022-08-31 16:23:43 -0700 | [diff] [blame] | 97 | |
| 98 | if file.IsMachoFile { |
| 99 | err = symbol_inject.CodeSignMachoFile(*output) |
| 100 | if err != nil { |
| 101 | fmt.Fprintln(os.Stderr, err.Error()) |
| 102 | os.Remove(*output) |
| 103 | os.Exit(6) |
| 104 | } |
| 105 | } |
Dan Willemsen | 2249dc8 | 2018-10-15 00:35:59 -0700 | [diff] [blame] | 106 | } |