2017-08-26 01:34:03 +02:00
|
|
|
//
|
|
|
|
// OCCAM
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017, SRI International
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
// list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * Neither the name of SRI International nor the names of its contributors may
|
|
|
|
// be used to endorse or promote products derived from this software without
|
|
|
|
// specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
|
|
|
|
2017-07-05 17:14:11 +02:00
|
|
|
package shared
|
2017-06-23 23:08:46 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-29 21:38:04 +02:00
|
|
|
"debug/elf"
|
|
|
|
"debug/macho"
|
2017-07-05 20:00:07 +02:00
|
|
|
"flag"
|
2017-06-29 21:38:04 +02:00
|
|
|
"io/ioutil"
|
2017-06-28 23:13:56 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-06-29 21:38:04 +02:00
|
|
|
"runtime"
|
2018-04-28 18:23:55 +02:00
|
|
|
"sort"
|
2017-06-28 23:13:56 +02:00
|
|
|
"strings"
|
2017-06-23 23:08:46 +02:00
|
|
|
)
|
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
type extractionArgs struct {
|
2017-07-05 20:00:07 +02:00
|
|
|
InputFile string
|
|
|
|
InputType int
|
|
|
|
OutputFile string
|
|
|
|
LinkerName string
|
|
|
|
ArchiverName string
|
|
|
|
ArArgs []string
|
|
|
|
ObjectTypeInArchive int // Type of file that can be put into an archive
|
|
|
|
Extractor func(string) []string
|
|
|
|
Verbose bool
|
|
|
|
WriteManifest bool
|
2018-04-28 18:23:55 +02:00
|
|
|
SortManifest bool
|
2017-07-05 20:00:07 +02:00
|
|
|
BuildBitcodeArchive bool
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2018-02-06 00:23:04 +01:00
|
|
|
//Extract extracts the LLVM bitcode according to the arguments it is passed.
|
2017-07-05 17:14:11 +02:00
|
|
|
func Extract(args []string) {
|
2017-07-05 20:00:07 +02:00
|
|
|
ea := parseSwitches()
|
2017-06-28 23:13:56 +02:00
|
|
|
|
|
|
|
// Set arguments according to runtime OS
|
|
|
|
switch platform := runtime.GOOS; platform {
|
2018-04-18 15:58:47 +02:00
|
|
|
case osFREEBSD, osLINUX:
|
2017-06-28 23:13:56 +02:00
|
|
|
ea.Extractor = extractSectionUnix
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.Verbose {
|
2017-06-28 23:13:56 +02:00
|
|
|
ea.ArArgs = append(ea.ArArgs, "xv")
|
|
|
|
} else {
|
|
|
|
ea.ArArgs = append(ea.ArArgs, "x")
|
|
|
|
}
|
2017-06-30 20:54:59 +02:00
|
|
|
ea.ObjectTypeInArchive = fileTypeELFOBJECT
|
2018-04-18 15:58:47 +02:00
|
|
|
case osDARWIN:
|
2017-06-28 23:13:56 +02:00
|
|
|
ea.Extractor = extractSectionDarwin
|
|
|
|
ea.ArArgs = append(ea.ArArgs, "-x")
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.Verbose {
|
2017-06-28 23:13:56 +02:00
|
|
|
ea.ArArgs = append(ea.ArArgs, "-v")
|
|
|
|
}
|
2017-06-30 20:54:59 +02:00
|
|
|
ea.ObjectTypeInArchive = fileTypeMACHOBJECT
|
2017-06-28 23:13:56 +02:00
|
|
|
default:
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("Unsupported platform: %s.", platform)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create output filename if not given
|
|
|
|
if ea.OutputFile == "" {
|
2018-04-28 03:31:38 +02:00
|
|
|
if ea.InputType == fileTypeARCHIVE || ea.InputType == fileTypeTHINARCHIVE {
|
2017-06-28 23:13:56 +02:00
|
|
|
var ext string
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.BuildBitcodeArchive {
|
2017-06-28 23:13:56 +02:00
|
|
|
ext = ".a.bc"
|
|
|
|
} else {
|
|
|
|
ext = ".bca"
|
|
|
|
}
|
|
|
|
ea.OutputFile = strings.TrimSuffix(ea.InputFile, ".a") + ext
|
|
|
|
} else {
|
|
|
|
ea.OutputFile = ea.InputFile + ".bc"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 20:00:07 +02:00
|
|
|
switch ea.InputType {
|
|
|
|
case fileTypeELFEXECUTABLE,
|
|
|
|
fileTypeELFSHARED,
|
|
|
|
fileTypeELFOBJECT,
|
|
|
|
fileTypeMACHEXECUTABLE,
|
|
|
|
fileTypeMACHSHARED,
|
|
|
|
fileTypeMACHOBJECT:
|
|
|
|
handleExecutable(ea)
|
|
|
|
case fileTypeARCHIVE:
|
|
|
|
handleArchive(ea)
|
2018-04-28 03:31:38 +02:00
|
|
|
case fileTypeTHINARCHIVE:
|
|
|
|
handleThinArchive(ea)
|
2017-07-05 20:00:07 +02:00
|
|
|
default:
|
|
|
|
LogFatal("Incorrect input file type %v.", ea.InputType)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSwitches() (ea extractionArgs) {
|
|
|
|
ea = extractionArgs{
|
|
|
|
LinkerName: "llvm-link",
|
|
|
|
ArchiverName: "llvm-ar",
|
|
|
|
}
|
|
|
|
|
|
|
|
verbosePtr := flag.Bool("v", false, "verbose mode")
|
|
|
|
|
|
|
|
writeManifestPtr := flag.Bool("m", false, "write the manifest")
|
|
|
|
|
2018-04-28 18:23:55 +02:00
|
|
|
sortManifestPtr := flag.Bool("s", false, "sort the manifest")
|
|
|
|
|
2017-07-05 20:00:07 +02:00
|
|
|
buildBitcodeArchive := flag.Bool("b", false, "build a bitcode module(FIXME? should this be archive)")
|
|
|
|
|
|
|
|
outputFilePtr := flag.String("o", "", "the output file")
|
|
|
|
|
|
|
|
archiverNamePtr := flag.String("a", "", "the llvm archiver")
|
|
|
|
|
|
|
|
linkerNamePtr := flag.String("l", "", "the llvm linker")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
ea.Verbose = *verbosePtr
|
|
|
|
ea.WriteManifest = *writeManifestPtr
|
2018-04-28 18:23:55 +02:00
|
|
|
ea.SortManifest = *sortManifestPtr
|
2017-07-05 20:00:07 +02:00
|
|
|
ea.BuildBitcodeArchive = *buildBitcodeArchive
|
|
|
|
|
|
|
|
if *archiverNamePtr != "" {
|
|
|
|
ea.ArchiverName = *archiverNamePtr
|
2017-07-05 20:10:32 +02:00
|
|
|
} else {
|
|
|
|
if LLVMARName != "" {
|
|
|
|
ea.ArchiverName = filepath.Join(LLVMToolChainBinDir, LLVMARName)
|
|
|
|
}
|
2017-07-05 20:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if *linkerNamePtr != "" {
|
|
|
|
ea.LinkerName = *linkerNamePtr
|
2017-07-05 20:10:32 +02:00
|
|
|
} else {
|
|
|
|
if LLVMLINKName != "" {
|
|
|
|
ea.LinkerName = filepath.Join(LLVMToolChainBinDir, LLVMLINKName)
|
|
|
|
}
|
2017-07-05 20:00:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ea.OutputFile = *outputFilePtr
|
|
|
|
|
|
|
|
inputFiles := flag.Args()
|
|
|
|
|
|
|
|
LogInfo("ea.Verbose: %v\n", ea.Verbose)
|
|
|
|
LogInfo("ea.WriteManifest: %v\n", ea.WriteManifest)
|
|
|
|
LogInfo("ea.BuildBitcodeArchive: %v\n", ea.BuildBitcodeArchive)
|
|
|
|
LogInfo("ea.ArchiverName: %v\n", ea.ArchiverName)
|
|
|
|
LogInfo("ea.LinkerName: %v\n", ea.LinkerName)
|
|
|
|
LogInfo("ea.OutputFile: %v\n", ea.OutputFile)
|
|
|
|
|
|
|
|
if len(inputFiles) != 1 {
|
|
|
|
LogFatal("Can currently only deal with exactly one input file, sorry. You gave me %v\n.", len(inputFiles))
|
|
|
|
}
|
|
|
|
|
|
|
|
ea.InputFile = inputFiles[0]
|
|
|
|
|
|
|
|
LogInfo("ea.InputFile: %v\n", ea.InputFile)
|
|
|
|
|
|
|
|
if _, err := os.Stat(ea.InputFile); os.IsNotExist(err) {
|
|
|
|
LogFatal("The input file %s does not exist.", ea.InputFile)
|
|
|
|
}
|
|
|
|
realPath, err := filepath.EvalSymlinks(ea.InputFile)
|
|
|
|
if err != nil {
|
|
|
|
LogFatal("There was an error getting the real path of %s.", ea.InputFile)
|
|
|
|
}
|
|
|
|
ea.InputFile = realPath
|
|
|
|
ea.InputType = getFileType(realPath)
|
|
|
|
|
|
|
|
LogInfo("ea.InputFile real path: %v\n", ea.InputFile)
|
|
|
|
|
|
|
|
return
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
func handleExecutable(ea extractionArgs) {
|
2017-06-28 23:13:56 +02:00
|
|
|
artifactPaths := ea.Extractor(ea.InputFile)
|
2018-04-17 20:46:22 +02:00
|
|
|
|
|
|
|
LogInfo("handleExecutable: artifactPaths = %v\n", artifactPaths)
|
|
|
|
|
2018-02-06 00:23:04 +01:00
|
|
|
if len(artifactPaths) == 0 {
|
2018-02-06 00:27:50 +01:00
|
|
|
return
|
2018-02-06 00:23:04 +01:00
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
filesToLink := make([]string, len(artifactPaths))
|
|
|
|
for i, artPath := range artifactPaths {
|
|
|
|
filesToLink[i] = resolveBitcodePath(artPath)
|
|
|
|
}
|
|
|
|
extractTimeLinkFiles(ea, filesToLink)
|
|
|
|
|
|
|
|
// Write manifest
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.WriteManifest {
|
2017-06-28 23:13:56 +02:00
|
|
|
writeManifest(ea, filesToLink, artifactPaths)
|
|
|
|
}
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2018-04-28 03:31:38 +02:00
|
|
|
func extractFromThinArchive(inputFile string) (contents []string) {
|
|
|
|
var arArgs []string
|
|
|
|
arArgs = append(arArgs, "-t")
|
|
|
|
arArgs = append(arArgs, inputFile)
|
|
|
|
output, err := runCmd("ar", arArgs)
|
|
|
|
if err != nil {
|
|
|
|
LogFatal("Failed to extract from thin archive %s because: %v.\n", inputFile, err)
|
|
|
|
}
|
|
|
|
contents = strings.Split(output, "\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleThinArchive(ea extractionArgs) {
|
|
|
|
// List bitcode files to link
|
|
|
|
var artifactFiles []string
|
|
|
|
|
|
|
|
var objectFiles []string
|
|
|
|
var bcFiles []string
|
|
|
|
|
|
|
|
objectFiles = extractFromThinArchive(ea.InputFile)
|
|
|
|
|
|
|
|
LogInfo("handleThinArchive: extractionArgs = %v\nobjectFiles = %v\n", ea, objectFiles)
|
|
|
|
|
|
|
|
for index, obj := range objectFiles {
|
|
|
|
LogInfo("obj = '%v'\n", obj)
|
|
|
|
if len(obj) > 0 {
|
|
|
|
artifacts := ea.Extractor(obj)
|
|
|
|
LogInfo("\t%v\n", artifacts)
|
|
|
|
artifactFiles = append(artifactFiles, artifacts...)
|
|
|
|
for _, bc := range artifacts {
|
|
|
|
bcPath := resolveBitcodePath(bc)
|
|
|
|
if bcPath != "" {
|
|
|
|
bcFiles = append(bcFiles, bcPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogDebug("\tskipping empty entry at index %v\n", index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogInfo("bcFiles: %v\n", bcFiles)
|
|
|
|
LogInfo("len(bcFiles) = %v\n", len(bcFiles))
|
|
|
|
|
|
|
|
if len(bcFiles) > 0 {
|
|
|
|
// Build archive
|
|
|
|
if ea.BuildBitcodeArchive {
|
|
|
|
extractTimeLinkFiles(ea, bcFiles)
|
|
|
|
} else {
|
|
|
|
archiveBcFiles(ea, bcFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write manifest
|
|
|
|
if ea.WriteManifest {
|
|
|
|
writeManifest(ea, bcFiles, artifactFiles)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogError("No bitcode files found\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
func handleArchive(ea extractionArgs) {
|
2017-06-28 23:13:56 +02:00
|
|
|
// List bitcode files to link
|
|
|
|
var bcFiles []string
|
|
|
|
var artifactFiles []string
|
|
|
|
|
2018-04-17 20:46:22 +02:00
|
|
|
LogInfo("handleArchive: extractionArgs = %v\n", ea)
|
|
|
|
|
2017-06-28 23:13:56 +02:00
|
|
|
// Create tmp dir
|
|
|
|
tmpDirName, err := ioutil.TempDir("", "gllvm")
|
|
|
|
if err != nil {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("The temporary directory in which to extract object files could not be created.")
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2018-04-18 15:58:47 +02:00
|
|
|
defer CheckDefer(func() error { return os.RemoveAll(tmpDirName) })
|
2017-06-28 23:13:56 +02:00
|
|
|
|
|
|
|
// Extract objects to tmpDir
|
|
|
|
arArgs := ea.ArArgs
|
|
|
|
inputAbsPath, _ := filepath.Abs(ea.InputFile)
|
|
|
|
arArgs = append(arArgs, inputAbsPath)
|
2018-04-17 20:46:22 +02:00
|
|
|
|
|
|
|
LogInfo("handleArchive: executing ar %v %v\n", arArgs, tmpDirName)
|
|
|
|
|
2017-06-30 19:00:25 +02:00
|
|
|
success, err := execCmd("ar", arArgs, tmpDirName)
|
|
|
|
if !success {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("Failed to extract object files from %s to %s because: %v.\n", ea.InputFile, tmpDirName, err)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define object file handling closure
|
|
|
|
var walkHandlingFunc = func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err == nil && !info.IsDir() {
|
2017-06-30 20:54:59 +02:00
|
|
|
fileType := getFileType(path)
|
|
|
|
if fileType == ea.ObjectTypeInArchive {
|
2017-06-28 23:13:56 +02:00
|
|
|
artifactPaths := ea.Extractor(path)
|
|
|
|
for _, artPath := range artifactPaths {
|
|
|
|
bcPath := resolveBitcodePath(artPath)
|
2018-04-17 20:46:22 +02:00
|
|
|
if bcPath != "" {
|
|
|
|
bcFiles = append(bcFiles, bcPath)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
artifactFiles = append(artifactFiles, artifactPaths...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle object files
|
2018-04-18 15:58:47 +02:00
|
|
|
err = filepath.Walk(tmpDirName, walkHandlingFunc)
|
|
|
|
if err != nil {
|
|
|
|
LogFatal("handleArchive: walking %v failed with %v\n", tmpDirName, err)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
|
2018-04-17 20:46:22 +02:00
|
|
|
LogDebug("handleArchive: walked %v\nartifactFiles:\n%v\nbcFiles:\n%v\n", tmpDirName, artifactFiles, bcFiles)
|
2017-06-28 23:13:56 +02:00
|
|
|
|
2018-04-17 20:46:22 +02:00
|
|
|
if len(bcFiles) > 0 {
|
|
|
|
// Build archive
|
|
|
|
if ea.BuildBitcodeArchive {
|
|
|
|
extractTimeLinkFiles(ea, bcFiles)
|
|
|
|
} else {
|
|
|
|
archiveBcFiles(ea, bcFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write manifest
|
|
|
|
if ea.WriteManifest {
|
|
|
|
writeManifest(ea, bcFiles, artifactFiles)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogError("No bitcode files found\n")
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|
2017-06-23 23:08:46 +02:00
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
func archiveBcFiles(ea extractionArgs, bcFiles []string) {
|
2017-06-28 23:13:56 +02:00
|
|
|
// We do not want full paths in the archive, so we need to chdir into each
|
|
|
|
// bitcode's folder. Handle this by calling llvm-ar once for all bitcode
|
|
|
|
// files in the same directory
|
|
|
|
dirToBcMap := make(map[string][]string)
|
|
|
|
for _, bcFile := range bcFiles {
|
|
|
|
dirName, baseName := path.Split(bcFile)
|
|
|
|
dirToBcMap[dirName] = append(dirToBcMap[dirName], baseName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call llvm-ar from each directory
|
|
|
|
absOutputFile, _ := filepath.Abs(ea.OutputFile)
|
|
|
|
for dir, bcFilesInDir := range dirToBcMap {
|
|
|
|
var args []string
|
|
|
|
args = append(args, "rs", absOutputFile)
|
|
|
|
args = append(args, bcFilesInDir...)
|
2017-06-30 21:17:27 +02:00
|
|
|
success, err := execCmd(ea.ArchiverName, args, dir)
|
2017-07-05 20:00:07 +02:00
|
|
|
LogInfo("ea.ArchiverName = %s, args = %v, dir = %s\n", ea.ArchiverName, args, dir)
|
2017-06-30 19:00:25 +02:00
|
|
|
if !success {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("There was an error creating the bitcode archive: %v.\n", err)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-05 17:14:11 +02:00
|
|
|
LogInfo("Built bitcode archive: %s.", ea.OutputFile)
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
func extractTimeLinkFiles(ea extractionArgs, filesToLink []string) {
|
2017-06-28 23:13:56 +02:00
|
|
|
var linkArgs []string
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.Verbose {
|
2017-06-28 23:13:56 +02:00
|
|
|
linkArgs = append(linkArgs, "-v")
|
|
|
|
}
|
|
|
|
linkArgs = append(linkArgs, "-o", ea.OutputFile)
|
|
|
|
linkArgs = append(linkArgs, filesToLink...)
|
2017-06-30 19:00:25 +02:00
|
|
|
success, err := execCmd(ea.LinkerName, linkArgs, "")
|
|
|
|
if !success {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("There was an error linking input files into %s because %v.\n", ea.OutputFile, err)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-07-05 17:14:11 +02:00
|
|
|
LogInfo("Bitcode file extracted to: %s.", ea.OutputFile)
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 19:05:19 +02:00
|
|
|
func extractSectionDarwin(inputFile string) (contents []string) {
|
2017-06-28 23:13:56 +02:00
|
|
|
machoFile, err := macho.Open(inputFile)
|
|
|
|
if err != nil {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("Mach-O file %s could not be read.", inputFile)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-06-30 20:54:59 +02:00
|
|
|
section := machoFile.Section(DarwinSectionName)
|
2018-02-06 00:27:50 +01:00
|
|
|
if section == nil {
|
|
|
|
LogWarning("The %s section of %s is missing!\n", DarwinSectionName, inputFile)
|
|
|
|
return
|
2018-02-06 00:23:04 +01:00
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
sectionContents, errContents := section.Data()
|
|
|
|
if errContents != nil {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
|
|
|
|
return
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 19:05:19 +02:00
|
|
|
func extractSectionUnix(inputFile string) (contents []string) {
|
2017-06-28 23:13:56 +02:00
|
|
|
elfFile, err := elf.Open(inputFile)
|
|
|
|
if err != nil {
|
2018-04-17 00:45:06 +02:00
|
|
|
LogFatal("ELF file %s could not be read.", inputFile)
|
2018-04-17 00:23:59 +02:00
|
|
|
return
|
2018-04-17 00:45:06 +02:00
|
|
|
}
|
2017-06-30 20:54:59 +02:00
|
|
|
section := elfFile.Section(ELFSectionName)
|
2018-04-17 00:21:30 +02:00
|
|
|
if section == nil {
|
2018-04-17 00:23:59 +02:00
|
|
|
LogWarning("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
|
|
|
|
return
|
2018-04-17 00:21:30 +02:00
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
sectionContents, errContents := section.Data()
|
|
|
|
if errContents != nil {
|
2018-04-17 00:23:59 +02:00
|
|
|
LogWarning("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile)
|
|
|
|
return
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n")
|
|
|
|
return
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 21:42:47 +02:00
|
|
|
// Return the actual path to the bitcode file, or an empty string if it does not exist
|
|
|
|
func resolveBitcodePath(bcPath string) string {
|
2017-06-28 23:13:56 +02:00
|
|
|
if _, err := os.Stat(bcPath); os.IsNotExist(err) {
|
|
|
|
// If the bitcode file does not exist, try to find it in the store
|
2017-07-11 19:52:05 +02:00
|
|
|
if LLVMBitcodeStorePath != "" {
|
2017-06-28 23:13:56 +02:00
|
|
|
// Compute absolute path hash
|
|
|
|
absBcPath, _ := filepath.Abs(bcPath)
|
2017-07-11 19:52:05 +02:00
|
|
|
storeBcPath := path.Join(LLVMBitcodeStorePath, getHashedPath(absBcPath))
|
2017-06-28 23:13:56 +02:00
|
|
|
if _, err := os.Stat(storeBcPath); os.IsNotExist(err) {
|
|
|
|
return ""
|
|
|
|
}
|
2017-06-29 23:19:12 +02:00
|
|
|
return storeBcPath
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2018-04-17 20:46:22 +02:00
|
|
|
LogWarning("Failed to find the file %v\n", bcPath)
|
2017-06-29 23:25:55 +02:00
|
|
|
return ""
|
2017-06-29 23:44:39 +02:00
|
|
|
}
|
2017-06-29 23:25:55 +02:00
|
|
|
return bcPath
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:46:30 +02:00
|
|
|
func writeManifest(ea extractionArgs, bcFiles []string, artifactFiles []string) {
|
2018-04-28 18:23:55 +02:00
|
|
|
manifestFilename := ea.OutputFile + ".llvm.manifest"
|
|
|
|
if ea.SortManifest {
|
|
|
|
LogWarning("Manifest sorting %s.", manifestFilename)
|
|
|
|
sort.Strings(bcFiles)
|
|
|
|
sort.Strings(artifactFiles)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
section1 := "Physical location of extracted files:\n" + strings.Join(bcFiles, "\n") + "\n\n"
|
|
|
|
section2 := "Build-time location of extracted files:\n" + strings.Join(artifactFiles, "\n")
|
|
|
|
contents := []byte(section1 + section2)
|
|
|
|
if err := ioutil.WriteFile(manifestFilename, contents, 0644); err != nil {
|
2017-07-05 17:14:11 +02:00
|
|
|
LogFatal("There was an error while writing the manifest file: ", err)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-07-05 17:14:11 +02:00
|
|
|
LogInfo("Manifest file written to %s.", manifestFilename)
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|