fix binary extraction for *nix

This commit is contained in:
Loic Gelle 2017-06-28 10:05:19 -07:00
parent f11a1405aa
commit 5f00dcaf2f

View File

@ -23,7 +23,7 @@ type ExtractingArgs struct {
ArchiverName string ArchiverName string
ArArgs []string ArArgs []string
ObjectTypeInArchive int // Type of file that can be put into an archive ObjectTypeInArchive int // Type of file that can be put into an archive
Extractor func(string) string Extractor func(string) []string
IsVerbose bool IsVerbose bool
IsWriteManifest bool IsWriteManifest bool
IsBuildBitcodeArchive bool IsBuildBitcodeArchive bool
@ -151,13 +151,16 @@ func parseExtractingArgs(args []string) ExtractingArgs {
} }
func handleExecutable(ea ExtractingArgs) { func handleExecutable(ea ExtractingArgs) {
artifactPath := ea.Extractor(ea.InputFile) artifactPaths := ea.Extractor(ea.InputFile)
filesToLink := []string{resolveBitcodePath(artifactPath)} filesToLink := make([]string, len(artifactPaths))
for i, artPath := range artifactPaths {
filesToLink[i] = resolveBitcodePath(artPath)
}
extractTimeLinkFiles(ea, filesToLink) extractTimeLinkFiles(ea, filesToLink)
// Write manifest // Write manifest
if ea.IsWriteManifest { if ea.IsWriteManifest {
writeManifest(ea, filesToLink, []string{artifactPath}) writeManifest(ea, filesToLink, artifactPaths)
} }
} }
@ -186,10 +189,12 @@ func handleArchive(ea ExtractingArgs) {
if err == nil && !info.IsDir() { if err == nil && !info.IsDir() {
ft := getFileType(path) ft := getFileType(path)
if ft == ea.ObjectTypeInArchive { if ft == ea.ObjectTypeInArchive {
artifactPath := ea.Extractor(path) artifactPaths := ea.Extractor(path)
bcPath := resolveBitcodePath(artifactPath) for _, artPath := range artifactPaths {
bcFiles = append(bcFiles, bcPath) bcPath := resolveBitcodePath(artPath)
artifactFiles = append(artifactFiles, artifactPath) bcFiles = append(bcFiles, bcPath)
}
artifactFiles = append(artifactFiles, artifactPaths...)
} }
} }
return nil return nil
@ -247,7 +252,7 @@ func extractTimeLinkFiles(ea ExtractingArgs, filesToLink []string) {
fmt.Println("Bitcode file extracted to", ea.OutputFile) fmt.Println("Bitcode file extracted to", ea.OutputFile)
} }
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) cmd := exec.Command("otool", "-X", "-s", DARWIN_SEGMENT_NAME, DARWIN_SECTION_NAME, inputFile)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
@ -269,17 +274,17 @@ func extractSectionDarwin(inputFile string) (contents string) {
octets = append(octets, dst...) octets = append(octets, dst...)
} }
} }
contents = strings.TrimSuffix(string(octets), "\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) cmd := exec.Command("objcopy", "--dump-section", ELF_SECTION_NAME + "=/dev/stdout", inputFile)
out, err := cmd.Output() 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("There was an error reading the contents of ", inputFile, ". Make sure that the 'objcopy' command is installed.")
} }
contents = strings.TrimSuffix(string(out), "\n") contents = strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")
return return
} }