mirror of
https://github.com/danog/gllvm.git
synced 2025-01-23 04:21:16 +01:00
remove dependency to objcopy/otool for extraction
This commit is contained in:
parent
5f00dcaf2f
commit
071418d0b5
43
extractor.go
43
extractor.go
@ -8,11 +8,10 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"regexp"
|
|
||||||
"encoding/hex"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"debug/macho"
|
||||||
|
"debug/elf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtractingArgs struct {
|
type ExtractingArgs struct {
|
||||||
@ -253,38 +252,30 @@ func extractTimeLinkFiles(ea ExtractingArgs, filesToLink []string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func extractSectionDarwin(inputFile string) (contents []string) {
|
func extractSectionDarwin(inputFile string) (contents []string) {
|
||||||
cmd := exec.Command("otool", "-X", "-s", DARWIN_SEGMENT_NAME, DARWIN_SECTION_NAME, inputFile)
|
machoFile, err := macho.Open(inputFile)
|
||||||
out, err := cmd.Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("There was an error extracting the gllvm section from ", inputFile, ". Make sure that the 'otool' command is installed.")
|
log.Fatal("Mach-O file ", inputFile, " could not be read.")
|
||||||
}
|
}
|
||||||
//FIXME: ok this looks all wrong. need to deal with out properly and the go way, whatever that may be.
|
section := machoFile.Section(DARWIN_SECTION_NAME)
|
||||||
logDebug("extractSectionDarwin: T(out) = %v \n", reflect.TypeOf(out))
|
sectionContents, errContents := section.Data()
|
||||||
logDebug("extractSectionDarwin: out = %v \n", out)
|
if errContents != nil {
|
||||||
sectionLines := strings.Split(string(out), "\n")
|
log.Fatal("Error reading the ", DARWIN_SECTION_NAME, " section of Mach-O file ", inputFile, ".")
|
||||||
logDebug("extractSectionDarwin: sectionLines = %v\n", out)
|
|
||||||
regExp := regexp.MustCompile(`^(?:[0-9a-f]{8,16}\t)?([0-9a-f\s]+)$`)
|
|
||||||
var octets []byte
|
|
||||||
|
|
||||||
for _, line := range sectionLines {
|
|
||||||
if matches := regExp.FindStringSubmatch(line); matches != nil {
|
|
||||||
hexline := []byte(strings.Join(strings.Split(matches[1], " "), ""))
|
|
||||||
dst := make([]byte, hex.DecodedLen(len(hexline)))
|
|
||||||
hex.Decode(dst, hexline)
|
|
||||||
octets = append(octets, dst...)
|
|
||||||
}
|
}
|
||||||
}
|
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
|
||||||
contents = strings.Split(strings.TrimSuffix(string(octets), "\n"), "\n")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractSectionUnix(inputFile string) (contents []string) {
|
func extractSectionUnix(inputFile string) (contents []string) {
|
||||||
cmd := exec.Command("objcopy", "--dump-section", ELF_SECTION_NAME + "=/dev/stdout", inputFile)
|
elfFile, err := elf.Open(inputFile)
|
||||||
out, err := cmd.Output()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("There was an error reading the contents of ", inputFile, ". Make sure that the 'objcopy' command is installed.")
|
log.Fatal("ELF file ", inputFile, " could not be read.")
|
||||||
}
|
}
|
||||||
contents = strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")
|
section := elfFile.Section(ELF_SECTION_NAME)
|
||||||
|
sectionContents, errContents := section.Data()
|
||||||
|
if errContents != nil {
|
||||||
|
log.Fatal("Error reading the ", ELF_SECTION_NAME, " section of ELF file ", inputFile, ".")
|
||||||
|
}
|
||||||
|
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user