Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [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 | |
Dan Willemsen | 2249dc8 | 2018-10-15 00:35:59 -0700 | [diff] [blame] | 15 | package symbol_inject |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "debug/macho" |
| 19 | "fmt" |
| 20 | "io" |
Yabin Cui | 7f5f22b | 2022-08-31 16:23:43 -0700 | [diff] [blame^] | 21 | "os/exec" |
Colin Cross | c4a18e0 | 2018-02-23 22:43:24 -0800 | [diff] [blame] | 22 | "sort" |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 23 | "strings" |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 26 | func machoSymbolsFromFile(r io.ReaderAt) (*File, error) { |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 27 | machoFile, err := macho.NewFile(r) |
| 28 | if err != nil { |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 29 | return nil, cantParseError{err} |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 30 | } |
| 31 | |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 32 | return extractMachoSymbols(machoFile) |
| 33 | } |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 34 | |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 35 | func extractMachoSymbols(machoFile *macho.File) (*File, error) { |
Colin Cross | c4a18e0 | 2018-02-23 22:43:24 -0800 | [diff] [blame] | 36 | symbols := machoFile.Symtab.Syms |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 37 | sort.SliceStable(symbols, func(i, j int) bool { |
Colin Cross | c4a18e0 | 2018-02-23 22:43:24 -0800 | [diff] [blame] | 38 | if symbols[i].Sect != symbols[j].Sect { |
| 39 | return symbols[i].Sect < symbols[j].Sect |
| 40 | } |
| 41 | return symbols[i].Value < symbols[j].Value |
| 42 | }) |
| 43 | |
Yabin Cui | 7f5f22b | 2022-08-31 16:23:43 -0700 | [diff] [blame^] | 44 | file := &File{IsMachoFile: true} |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 45 | |
| 46 | for _, section := range machoFile.Sections { |
| 47 | file.Sections = append(file.Sections, &Section{ |
| 48 | Name: section.Name, |
| 49 | Addr: section.Addr, |
| 50 | Offset: uint64(section.Offset), |
| 51 | Size: section.Size, |
| 52 | }) |
| 53 | } |
| 54 | |
Colin Cross | dfce764 | 2018-02-28 13:05:39 -0800 | [diff] [blame] | 55 | for _, symbol := range symbols { |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 56 | if symbol.Sect > 0 { |
| 57 | section := file.Sections[symbol.Sect-1] |
| 58 | file.Symbols = append(file.Symbols, &Symbol{ |
| 59 | // symbols in macho files seem to be prefixed with an underscore |
| 60 | Name: strings.TrimPrefix(symbol.Name, "_"), |
| 61 | // MachO symbol value is virtual address of the symbol, convert it to offset into the section. |
| 62 | Addr: symbol.Value - section.Addr, |
| 63 | // MachO symbols don't have size information. |
| 64 | Size: 0, |
| 65 | Section: section, |
Colin Cross | dfce764 | 2018-02-28 13:05:39 -0800 | [diff] [blame] | 66 | }) |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Colin Cross | 8673b5b | 2018-03-01 11:20:25 -0800 | [diff] [blame] | 70 | return file, nil |
| 71 | } |
| 72 | |
| 73 | func dumpMachoSymbols(r io.ReaderAt) error { |
| 74 | machoFile, err := macho.NewFile(r) |
| 75 | if err != nil { |
| 76 | return cantParseError{err} |
| 77 | } |
| 78 | |
| 79 | fmt.Println("&macho.File{") |
| 80 | |
| 81 | fmt.Println("\tSections: []*macho.Section{") |
| 82 | for _, section := range machoFile.Sections { |
| 83 | fmt.Printf("\t\t&macho.Section{SectionHeader: %#v},\n", section.SectionHeader) |
| 84 | } |
| 85 | fmt.Println("\t},") |
| 86 | |
| 87 | fmt.Println("\tSymtab: &macho.Symtab{") |
| 88 | fmt.Println("\t\tSyms: []macho.Symbol{") |
| 89 | for _, symbol := range machoFile.Symtab.Syms { |
| 90 | fmt.Printf("\t\t\t%#v,\n", symbol) |
| 91 | } |
| 92 | fmt.Println("\t\t},") |
| 93 | fmt.Println("\t},") |
| 94 | |
| 95 | fmt.Println("}") |
| 96 | |
| 97 | return nil |
Colin Cross | 5498f85 | 2018-01-03 23:39:54 -0800 | [diff] [blame] | 98 | } |
Yabin Cui | 7f5f22b | 2022-08-31 16:23:43 -0700 | [diff] [blame^] | 99 | |
| 100 | func CodeSignMachoFile(path string) error { |
| 101 | cmd := exec.Command("/usr/bin/codesign", "--force", "-s", "-", path) |
| 102 | return cmd.Run() |
| 103 | } |