-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "Add a tool to inject data into an elf, macho, or PE symbol"
- Loading branch information
Showing
5 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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", | ||
srcs: [ | ||
"symbol_inject.go", | ||
"elf.go", | ||
"macho.go", | ||
"pe.go", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 ( | ||
"debug/elf" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func findElfSymbol(r io.ReaderAt, symbol string) (uint64, uint64, error) { | ||
elfFile, err := elf.NewFile(r) | ||
if err != nil { | ||
return maxUint64, maxUint64, cantParseError{err} | ||
} | ||
|
||
symbols, err := elfFile.Symbols() | ||
if err != nil { | ||
return maxUint64, maxUint64, err | ||
} | ||
|
||
for _, s := range symbols { | ||
if elf.ST_TYPE(s.Info) != elf.STT_OBJECT { | ||
continue | ||
} | ||
if s.Name == symbol { | ||
offset, err := calculateElfSymbolOffset(elfFile, s) | ||
if err != nil { | ||
return maxUint64, maxUint64, err | ||
} | ||
return offset, s.Size, nil | ||
} | ||
} | ||
|
||
return maxUint64, maxUint64, fmt.Errorf("symbol not found") | ||
} | ||
|
||
func calculateElfSymbolOffset(file *elf.File, symbol elf.Symbol) (uint64, error) { | ||
if symbol.Section == elf.SHN_UNDEF || int(symbol.Section) >= len(file.Sections) { | ||
return maxUint64, fmt.Errorf("invalid section index %d", symbol.Section) | ||
} | ||
section := file.Sections[symbol.Section] | ||
switch file.Type { | ||
case elf.ET_REL: | ||
// "In relocatable files, st_value holds a section offset for a defined symbol. | ||
// That is, st_value is an offset from the beginning of the section that st_shndx identifies." | ||
return file.Sections[symbol.Section].Addr + symbol.Value, nil | ||
case elf.ET_EXEC, elf.ET_DYN: | ||
// "In executable and shared object files, st_value holds a virtual address. To make these | ||
// files’ symbols more useful for the dynamic linker, the section offset (file interpretation) | ||
// gives way to a virtual address (memory interpretation) for which the section number is | ||
// irrelevant." | ||
if symbol.Value < section.Addr { | ||
return maxUint64, fmt.Errorf("symbol starts before the start of its section") | ||
} | ||
section_offset := symbol.Value - section.Addr | ||
if section_offset+symbol.Size > section.Size { | ||
return maxUint64, fmt.Errorf("symbol extends past the end of its section") | ||
} | ||
return section.Offset + section_offset, nil | ||
default: | ||
return maxUint64, fmt.Errorf("unsupported elf file type %d", file.Type) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 ( | ||
"debug/macho" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func findMachoSymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) { | ||
machoFile, err := macho.NewFile(r) | ||
if err != nil { | ||
return maxUint64, maxUint64, cantParseError{err} | ||
} | ||
|
||
// TODO(ccross): why? | ||
symbolName = "_" + symbolName | ||
|
||
for i, symbol := range machoFile.Symtab.Syms { | ||
if symbol.Sect == 0 { | ||
continue | ||
} | ||
if symbol.Name == symbolName { | ||
var nextSymbol *macho.Symbol | ||
if i+1 < len(machoFile.Symtab.Syms) { | ||
nextSymbol = &machoFile.Symtab.Syms[i+1] | ||
} | ||
return calculateMachoSymbolOffset(machoFile, symbol, nextSymbol) | ||
} | ||
} | ||
|
||
return maxUint64, maxUint64, fmt.Errorf("symbol not found") | ||
} | ||
|
||
func calculateMachoSymbolOffset(file *macho.File, symbol macho.Symbol, nextSymbol *macho.Symbol) (uint64, uint64, error) { | ||
section := file.Sections[symbol.Sect-1] | ||
|
||
var end uint64 | ||
if nextSymbol != nil && nextSymbol.Sect != symbol.Sect { | ||
nextSymbol = nil | ||
} | ||
if nextSymbol != nil { | ||
end = nextSymbol.Value | ||
} else { | ||
end = section.Addr + section.Size | ||
} | ||
|
||
size := end - symbol.Value - 1 | ||
offset := uint64(section.Offset) + (symbol.Value - section.Addr) | ||
|
||
return offset, size, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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 ( | ||
"debug/pe" | ||
"fmt" | ||
"io" | ||
"sort" | ||
) | ||
|
||
func findPESymbol(r io.ReaderAt, symbolName string) (uint64, uint64, error) { | ||
peFile, err := pe.NewFile(r) | ||
if err != nil { | ||
return maxUint64, maxUint64, cantParseError{err} | ||
} | ||
|
||
sort.Slice(peFile.Symbols, func(i, j int) bool { | ||
if peFile.Symbols[i].SectionNumber != peFile.Symbols[j].SectionNumber { | ||
return peFile.Symbols[i].SectionNumber < peFile.Symbols[j].SectionNumber | ||
} | ||
return peFile.Symbols[i].Value < peFile.Symbols[j].Value | ||
}) | ||
|
||
for i, symbol := range peFile.Symbols { | ||
if symbol.Name == symbolName { | ||
var nextSymbol *pe.Symbol | ||
if i+1 < len(peFile.Symbols) { | ||
nextSymbol = peFile.Symbols[i+1] | ||
} | ||
return calculatePESymbolOffset(peFile, symbol, nextSymbol) | ||
} | ||
} | ||
|
||
return maxUint64, maxUint64, fmt.Errorf("symbol not found") | ||
} | ||
|
||
func calculatePESymbolOffset(file *pe.File, symbol *pe.Symbol, nextSymbol *pe.Symbol) (uint64, uint64, error) { | ||
section := file.Sections[symbol.SectionNumber-1] | ||
|
||
var end uint32 | ||
if nextSymbol != nil && nextSymbol.SectionNumber != symbol.SectionNumber { | ||
nextSymbol = nil | ||
} | ||
if nextSymbol != nil { | ||
end = nextSymbol.Value | ||
} else { | ||
end = section.Size | ||
} | ||
|
||
size := end - symbol.Value - 1 | ||
offset := section.Offset + symbol.Value | ||
|
||
return uint64(offset), uint64(size), nil | ||
} |
Oops, something went wrong.