Still futzing with the Extract method...

This commit is contained in:
Ian A. Mason 2019-10-26 00:37:23 +00:00
parent bd88b14e25
commit ef77e255a8
3 changed files with 84 additions and 61 deletions

View File

@ -38,6 +38,7 @@ import (
"debug/elf" "debug/elf"
"debug/macho" "debug/macho"
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -67,11 +68,28 @@ type extractionArgs struct {
Extractor func(string) []string Extractor func(string) []string
} }
//Extract extracts the LLVM bitcode according to the arguments it is passed. func (ea extractionArgs) String() string {
func Extract(args []string)(exitCode int) { format :=
ea := parseSwitches(args) `
ea.Verbose: %v
ea.WriteManifest: %v
ea.SortBitcodeFiles: %v
ea.BuildBitcodeModule: %v
ea.KeepTemp: %v
ea.LinkArgSize: %v
ea.InputFile: %v
ea.OutputFile: %v
ea.LlvmArchiverName: %v
ea.LlvmLinkerName: %v
ea.ArchiverName: %v
`
return fmt.Sprintf(format, ea.Verbose, ea.WriteManifest, ea.SortBitcodeFiles, ea.BuildBitcodeModule,
ea.KeepTemp, ea.LinkArgSize, ea.InputFile, ea.OutputFile, ea.LlvmArchiverName,
ea.LlvmLinkerName, ea.ArchiverName)
}
// Set arguments according to runtime OS // Set arguments according to runtime OS
func setPlatform(ea *extractionArgs) {
switch platform := runtime.GOOS; platform { switch platform := runtime.GOOS; platform {
case osFREEBSD, osLINUX: case osFREEBSD, osLINUX:
ea.Extractor = extractSectionUnix ea.Extractor = extractSectionUnix
@ -91,8 +109,10 @@ func Extract(args []string)(exitCode int) {
default: default:
LogFatal("Unsupported platform: %s.", platform) LogFatal("Unsupported platform: %s.", platform)
} }
}
// Create output filename if not given // Create output filename if not given
func setOutputFile(ea *extractionArgs) {
if ea.OutputFile == "" { if ea.OutputFile == "" {
if ea.InputType == fileTypeARCHIVE || ea.InputType == fileTypeTHINARCHIVE { if ea.InputType == fileTypeARCHIVE || ea.InputType == fileTypeTHINARCHIVE {
var ext string var ext string
@ -106,6 +126,18 @@ func Extract(args []string)(exitCode int) {
ea.OutputFile = ea.InputFile + ".bc" ea.OutputFile = ea.InputFile + ".bc"
} }
} }
}
//Extract extracts the LLVM bitcode according to the arguments it is passed.
func Extract(args []string) (exitCode int) {
ea := parseSwitches(args)
// Set arguments according to runtime OS
setPlatform(&ea)
// Create output filename if not given
setOutputFile(&ea)
switch ea.InputType { switch ea.InputType {
case fileTypeELFEXECUTABLE, case fileTypeELFEXECUTABLE,
@ -124,7 +156,7 @@ func Extract(args []string)(exitCode int) {
} }
//need to actually get an exitCode eventually. //need to actually get an exitCode eventually.
return return
} }
func resolveTool(defaultPath string, envPath string, usrPath string) (path string) { func resolveTool(defaultPath string, envPath string, usrPath string) (path string) {
@ -181,23 +213,13 @@ func parseSwitches(args []string) (ea extractionArgs) {
} }
ea.InputFile = realPath ea.InputFile = realPath
ea.InputType = getFileType(realPath) ea.InputType = getFileType(realPath)
//<this should be a method of extractionArgs>
LogInfo("ea.Verbose: %v\n", ea.Verbose) LogInfo("%v", ea)
LogInfo("ea.WriteManifest: %v\n", ea.WriteManifest)
LogInfo("ea.BuildBitcodeModule: %v\n", ea.BuildBitcodeModule)
LogInfo("ea.LlvmArchiverName: %v\n", ea.LlvmArchiverName)
LogInfo("ea.LlvmLinkerName: %v\n", ea.LlvmLinkerName)
LogInfo("ea.ArchiverName: %v\n", ea.ArchiverName)
LogInfo("ea.OutputFile: %v\n", ea.OutputFile)
LogInfo("ea.InputFile: %v\n", ea.InputFile)
LogInfo("ea.InputFile real path: %v\n", ea.InputFile)
LogInfo("ea.LinkArgSize %d", ea.LinkArgSize)
LogInfo("ea.KeepTemp %v", ea.KeepTemp)
//</this should be a method of extractionArgs>
return return
} }
func handleExecutable(ea extractionArgs) { func handleExecutable(ea extractionArgs) (exitCode int) {
artifactPaths := ea.Extractor(ea.InputFile) artifactPaths := ea.Extractor(ea.InputFile)
if len(artifactPaths) < 20 { if len(artifactPaths) < 20 {
@ -226,9 +248,10 @@ func handleExecutable(ea extractionArgs) {
} }
linkBitcodeFiles(ea, filesToLink) linkBitcodeFiles(ea, filesToLink)
return
} }
func handleThinArchive(ea extractionArgs) { func handleThinArchive(ea extractionArgs) (exitCode int) {
// List bitcode files to link // List bitcode files to link
var artifactFiles []string var artifactFiles []string
@ -282,7 +305,7 @@ func handleThinArchive(ea extractionArgs) {
} else { } else {
LogError("No bitcode files found\n") LogError("No bitcode files found\n")
} }
return
} }
func listArchiveFiles(ea extractionArgs, inputFile string) (contents []string) { func listArchiveFiles(ea extractionArgs, inputFile string) (contents []string) {
@ -348,7 +371,7 @@ func fetchTOC(ea extractionArgs, inputFile string) map[string]int {
// archive using llvm-ar // archive using llvm-ar
// //
//iam: 5/1/2018 //iam: 5/1/2018
func handleArchive(ea extractionArgs) { func handleArchive(ea extractionArgs) (exitCode int) {
// List bitcode files to link // List bitcode files to link
var bcFiles []string var bcFiles []string
var artifactFiles []string var artifactFiles []string
@ -427,6 +450,7 @@ func handleArchive(ea extractionArgs) {
} else { } else {
LogError("No bitcode files found\n") LogError("No bitcode files found\n")
} }
return
} }
func archiveBcFiles(ea extractionArgs, bcFiles []string) { func archiveBcFiles(ea extractionArgs, bcFiles []string) {

View File

@ -133,42 +133,42 @@ func parse(argList []string) parserResult {
"-nostdlibinc": {0, pr.compileUnaryCallback}, "-nostdlibinc": {0, pr.compileUnaryCallback},
"-mno-omit-leaf-frame-pointer": {0, pr.compileUnaryCallback}, "-mno-omit-leaf-frame-pointer": {0, pr.compileUnaryCallback},
"-maes": {0, pr.compileUnaryCallback}, "-maes": {0, pr.compileUnaryCallback},
"-mno-aes": {0, pr.compileUnaryCallback}, "-mno-aes": {0, pr.compileUnaryCallback},
"-mavx": {0, pr.compileUnaryCallback}, "-mavx": {0, pr.compileUnaryCallback},
"-mno-avx": {0, pr.compileUnaryCallback}, "-mno-avx": {0, pr.compileUnaryCallback},
"-mavx2": {0, pr.compileUnaryCallback}, "-mavx2": {0, pr.compileUnaryCallback},
"-mno-avx2": {0, pr.compileUnaryCallback}, "-mno-avx2": {0, pr.compileUnaryCallback},
"-mno-red-zone": {0, pr.compileUnaryCallback}, "-mno-red-zone": {0, pr.compileUnaryCallback},
"-mmmx": {0, pr.compileUnaryCallback}, "-mmmx": {0, pr.compileUnaryCallback},
"-mno-mmx": {0, pr.compileUnaryCallback}, "-mno-mmx": {0, pr.compileUnaryCallback},
"-mno-global-merge": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mno-global-merge": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-mno-80387": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mno-80387": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-msse": {0, pr.compileUnaryCallback}, "-msse": {0, pr.compileUnaryCallback},
"-mno-sse": {0, pr.compileUnaryCallback}, "-mno-sse": {0, pr.compileUnaryCallback},
"-msse2": {0, pr.compileUnaryCallback}, "-msse2": {0, pr.compileUnaryCallback},
"-mno-sse2": {0, pr.compileUnaryCallback}, "-mno-sse2": {0, pr.compileUnaryCallback},
"-msse3": {0, pr.compileUnaryCallback}, "-msse3": {0, pr.compileUnaryCallback},
"-mno-sse3": {0, pr.compileUnaryCallback}, "-mno-sse3": {0, pr.compileUnaryCallback},
"-mssse3": {0, pr.compileUnaryCallback}, "-mssse3": {0, pr.compileUnaryCallback},
"-mno-ssse3": {0, pr.compileUnaryCallback}, "-mno-ssse3": {0, pr.compileUnaryCallback},
"-msse4": {0, pr.compileUnaryCallback}, "-msse4": {0, pr.compileUnaryCallback},
"-mno-sse4": {0, pr.compileUnaryCallback}, "-mno-sse4": {0, pr.compileUnaryCallback},
"-msse4.1": {0, pr.compileUnaryCallback}, "-msse4.1": {0, pr.compileUnaryCallback},
"-mno-sse4.1": {0, pr.compileUnaryCallback}, "-mno-sse4.1": {0, pr.compileUnaryCallback},
"-msse4.2": {0, pr.compileUnaryCallback}, "-msse4.2": {0, pr.compileUnaryCallback},
"-mno-sse4.2": {0, pr.compileUnaryCallback}, "-mno-sse4.2": {0, pr.compileUnaryCallback},
"-msoft-float": {0, pr.compileUnaryCallback}, "-msoft-float": {0, pr.compileUnaryCallback},
"-m3dnow": {0, pr.compileUnaryCallback}, "-m3dnow": {0, pr.compileUnaryCallback},
"-mno-3dnow": {0, pr.compileUnaryCallback}, "-mno-3dnow": {0, pr.compileUnaryCallback},
"-m16": {0, pr.compileLinkUnaryCallback}, //iam: linux kernel stuff "-m16": {0, pr.compileLinkUnaryCallback}, //iam: linux kernel stuff
"-m32": {0, pr.compileLinkUnaryCallback}, "-m32": {0, pr.compileLinkUnaryCallback},
"-m64": {0, pr.compileLinkUnaryCallback}, "-m64": {0, pr.compileLinkUnaryCallback},
"-mstackrealign": {0, pr.compileUnaryCallback}, "-mstackrealign": {0, pr.compileUnaryCallback},
"-mretpoline-external-thunk": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mretpoline-external-thunk": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-mno-fp-ret-in-387": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mno-fp-ret-in-387": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-mskip-rax-setup": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mskip-rax-setup": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-mindirect-branch-register": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff "-mindirect-branch-register": {0, pr.compileUnaryCallback}, //iam: linux kernel stuff
"-A": {1, pr.compileBinaryCallback}, "-A": {1, pr.compileBinaryCallback},
"-D": {1, pr.compileBinaryCallback}, "-D": {1, pr.compileBinaryCallback},

View File

@ -26,6 +26,5 @@ func Test_basic(t *testing.T) {
} else { } else {
fmt.Println("Extraction OK") fmt.Println("Extraction OK")
} }
} }