blob: 4a3ecc7416003ebe39a2649657cf2ef1ebc979f0 [file] [log] [blame]
Colin Cross5498f852018-01-03 23:39:54 -08001// 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
15package main
16
17import (
18 "debug/macho"
19 "fmt"
20 "io"
Colin Crossc4a18e02018-02-23 22:43:24 -080021 "sort"
Colin Cross5498f852018-01-03 23:39:54 -080022)
23
24func findMachoSymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) {
25 machoFile, err := macho.NewFile(r)
26 if err != nil {
27 return maxUint64, maxUint64, cantParseError{err}
28 }
29
Colin Cross64c6d4b2018-02-23 17:10:34 -080030 // symbols in macho files seem to be prefixed with an underscore
Colin Cross5498f852018-01-03 23:39:54 -080031 symbolName = "_" + symbolName
32
Colin Crossc4a18e02018-02-23 22:43:24 -080033 symbols := machoFile.Symtab.Syms
34 sort.Slice(symbols, func(i, j int) bool {
35 if symbols[i].Sect != symbols[j].Sect {
36 return symbols[i].Sect < symbols[j].Sect
37 }
38 return symbols[i].Value < symbols[j].Value
39 })
40
41 for i, symbol := range symbols {
Colin Cross5498f852018-01-03 23:39:54 -080042 if symbol.Sect == 0 {
43 continue
44 }
45 if symbol.Name == symbolName {
46 var nextSymbol *macho.Symbol
Colin Crossc4a18e02018-02-23 22:43:24 -080047 if i+1 < len(symbols) {
48 nextSymbol = &symbols[i+1]
Colin Cross5498f852018-01-03 23:39:54 -080049 }
50 return calculateMachoSymbolOffset(machoFile, symbol, nextSymbol)
51 }
52 }
53
54 return maxUint64, maxUint64, fmt.Errorf("symbol not found")
55}
56
57func calculateMachoSymbolOffset(file *macho.File, symbol macho.Symbol, nextSymbol *macho.Symbol) (uint64, uint64, error) {
58 section := file.Sections[symbol.Sect-1]
59
60 var end uint64
61 if nextSymbol != nil && nextSymbol.Sect != symbol.Sect {
62 nextSymbol = nil
63 }
64 if nextSymbol != nil {
65 end = nextSymbol.Value
66 } else {
67 end = section.Addr + section.Size
68 }
69
70 size := end - symbol.Value - 1
71 offset := uint64(section.Offset) + (symbol.Value - section.Addr)
72
73 return offset, size, nil
74}