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 (
|
2018-05-10 23:30:22 +02:00
|
|
|
"bytes"
|
2017-06-29 21:38:04 +02:00
|
|
|
"debug/elf"
|
|
|
|
"debug/macho"
|
2017-07-05 20:00:07 +02:00
|
|
|
"flag"
|
2019-10-26 02:37:23 +02:00
|
|
|
"fmt"
|
2017-06-29 21:38:04 +02:00
|
|
|
"io/ioutil"
|
2017-06-28 23:13:56 +02:00
|
|
|
"os"
|
2018-05-10 23:30:22 +02:00
|
|
|
"os/exec"
|
2017-06-28 23:13:56 +02:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-06-29 21:38:04 +02:00
|
|
|
"runtime"
|
2018-04-28 18:23:55 +02:00
|
|
|
"sort"
|
2018-05-02 22:05:24 +02:00
|
|
|
"strconv"
|
2017-06-28 23:13:56 +02:00
|
|
|
"strings"
|
2017-06-23 23:08:46 +02:00
|
|
|
)
|
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
//for distilling the desired commandline options
|
2019-10-28 15:38:49 +01:00
|
|
|
type ExtractionArgs struct {
|
2019-10-26 18:47:47 +02:00
|
|
|
Failure bool // indicates failure in parsing the cmd line args
|
2018-05-11 16:13:13 +02:00
|
|
|
Verbose bool // inform the user of what is going on
|
|
|
|
WriteManifest bool // write a manifest of bitcode files used
|
|
|
|
SortBitcodeFiles bool // sort the arguments to linking and archiving (debugging too)
|
|
|
|
BuildBitcodeModule bool // buld an archive rather than a module
|
|
|
|
KeepTemp bool // keep temporary linking folder
|
|
|
|
LinkArgSize int // maximum size of a llvm-link command line
|
2017-07-05 20:00:07 +02:00
|
|
|
InputType int
|
2018-05-11 16:13:13 +02:00
|
|
|
ObjectTypeInArchive int // Type of file that can be put into an archive
|
2018-05-11 01:25:44 +02:00
|
|
|
InputFile string
|
2017-07-05 20:00:07 +02:00
|
|
|
OutputFile string
|
2019-10-25 18:27:32 +02:00
|
|
|
LlvmLinkerName string
|
2019-10-22 19:47:34 +02:00
|
|
|
LlvmArchiverName string
|
2017-07-05 20:00:07 +02:00
|
|
|
ArchiverName string
|
|
|
|
ArArgs []string
|
|
|
|
Extractor func(string) []string
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
//for printing out the parsed arguments, some have been skipped.
|
2019-10-28 15:38:49 +01:00
|
|
|
func (ea ExtractionArgs) String() string {
|
2019-10-26 02:37:23 +02:00
|
|
|
format :=
|
|
|
|
`
|
|
|
|
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)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func ParseSwitches(args []string) (ea ExtractionArgs) {
|
2019-10-28 21:47:17 +01:00
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
var flagSet *flag.FlagSet = flag.NewFlagSet(args[0], flag.ContinueOnError)
|
2019-10-28 21:47:17 +01:00
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
flagSet.BoolVar(&ea.Verbose, "v", false, "verbose mode")
|
|
|
|
flagSet.BoolVar(&ea.WriteManifest, "m", false, "write the manifest")
|
|
|
|
flagSet.BoolVar(&ea.SortBitcodeFiles, "s", false, "sort the bitcode files")
|
|
|
|
flagSet.BoolVar(&ea.BuildBitcodeModule, "b", false, "build a bitcode module")
|
|
|
|
flagSet.StringVar(&ea.OutputFile, "o", "", "the output file")
|
|
|
|
flagSet.StringVar(&ea.LlvmArchiverName, "a", "llvm-ar", "the llvm archiver (i.e. llvm-ar)")
|
|
|
|
flagSet.StringVar(&ea.ArchiverName, "r", "ar", "the system archiver (i.e. ar)")
|
|
|
|
flagSet.StringVar(&ea.LlvmLinkerName, "l", "llvm-link", "the llvm linker (i.e. llvm-link)")
|
|
|
|
flagSet.IntVar(&ea.LinkArgSize, "n", 0, "maximum llvm-link command line size (in bytes)")
|
|
|
|
flagSet.BoolVar(&ea.KeepTemp, "t", false, "keep temporary linking folder")
|
|
|
|
|
|
|
|
err := flagSet.Parse(args[1:])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ea.Failure = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ea.LlvmArchiverName = resolveTool("llvm-ar", LLVMARName, ea.LlvmArchiverName)
|
|
|
|
ea.LlvmLinkerName = resolveTool("llvm-link", LLVMLINKName, ea.LlvmLinkerName)
|
|
|
|
inputFiles := flagSet.Args()
|
|
|
|
if len(inputFiles) != 1 {
|
|
|
|
LogError("Can currently only deal with exactly one input file, sorry. You gave me %v input files.\n", len(inputFiles))
|
|
|
|
ea.Failure = true
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2019-10-26 18:47:47 +02:00
|
|
|
}
|
|
|
|
ea.InputFile = inputFiles[0]
|
|
|
|
if _, err := os.Stat(ea.InputFile); os.IsNotExist(err) {
|
|
|
|
LogError("The input file %s does not exist.", ea.InputFile)
|
|
|
|
ea.Failure = true
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2019-10-26 18:47:47 +02:00
|
|
|
}
|
|
|
|
realPath, err := filepath.EvalSymlinks(ea.InputFile)
|
|
|
|
if err != nil {
|
|
|
|
LogError("There was an error getting the real path of %s.", ea.InputFile)
|
|
|
|
ea.Failure = true
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2019-10-26 18:47:47 +02:00
|
|
|
}
|
|
|
|
ea.InputFile = realPath
|
|
|
|
ea.InputType = getFileType(realPath)
|
|
|
|
|
|
|
|
LogInfo("%v", ea)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//Extract extracts the LLVM bitcode according to the arguments it is passed.
|
|
|
|
func Extract(args []string) (exitCode int) {
|
2019-10-28 21:47:17 +01:00
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
exitCode = 1
|
2019-10-28 21:47:17 +01:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
ea := ParseSwitches(args)
|
2019-10-26 18:47:47 +02:00
|
|
|
|
|
|
|
if ea.Failure {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set arguments according to runtime OS
|
|
|
|
success := setPlatform(&ea)
|
|
|
|
if !success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create output filename if not given
|
|
|
|
setOutputFile(&ea)
|
|
|
|
|
|
|
|
switch ea.InputType {
|
|
|
|
case fileTypeELFEXECUTABLE,
|
|
|
|
fileTypeELFSHARED,
|
|
|
|
fileTypeELFOBJECT,
|
|
|
|
fileTypeMACHEXECUTABLE,
|
|
|
|
fileTypeMACHSHARED,
|
|
|
|
fileTypeMACHOBJECT:
|
|
|
|
success = handleExecutable(ea)
|
|
|
|
case fileTypeARCHIVE:
|
|
|
|
success = handleArchive(ea)
|
|
|
|
case fileTypeTHINARCHIVE:
|
|
|
|
success = handleThinArchive(ea)
|
|
|
|
case fileTypeERROR:
|
|
|
|
default:
|
|
|
|
LogError("Incorrect input file type %v.", ea.InputType)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if success {
|
|
|
|
exitCode = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:37:23 +02:00
|
|
|
// Set arguments according to runtime OS
|
2019-10-28 15:38:49 +01:00
|
|
|
func setPlatform(ea *ExtractionArgs) (success bool) {
|
2017-06-28 23:13:56 +02:00
|
|
|
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
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
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
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
2017-06-28 23:13:56 +02:00
|
|
|
default:
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("Unsupported platform: %s.", platform)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2019-10-26 02:37:23 +02:00
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
|
2019-10-26 02:37:23 +02:00
|
|
|
// Create output filename if not given
|
2019-10-28 15:38:49 +01:00
|
|
|
func setOutputFile(ea *ExtractionArgs) {
|
2017-06-28 23:13:56 +02:00
|
|
|
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
|
2018-05-11 16:13:13 +02:00
|
|
|
if ea.BuildBitcodeModule {
|
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"
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 02:37:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-09 23:57:10 +02:00
|
|
|
func resolveTool(defaultPath string, envPath string, usrPath string) (path string) {
|
2019-10-25 18:27:32 +02:00
|
|
|
if usrPath != defaultPath {
|
2018-05-09 23:57:10 +02:00
|
|
|
path = usrPath
|
2018-05-09 23:21:17 +02:00
|
|
|
} else {
|
|
|
|
if LLVMToolChainBinDir != "" {
|
|
|
|
if envPath != "" {
|
|
|
|
path = filepath.Join(LLVMToolChainBinDir, envPath)
|
|
|
|
} else {
|
|
|
|
path = filepath.Join(LLVMToolChainBinDir, defaultPath)
|
|
|
|
}
|
2018-05-09 23:57:10 +02:00
|
|
|
} else {
|
|
|
|
if envPath != "" {
|
|
|
|
path = envPath
|
|
|
|
} else {
|
|
|
|
path = defaultPath
|
|
|
|
}
|
2018-05-09 23:21:17 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-09 23:57:10 +02:00
|
|
|
LogDebug("defaultPath = %s", defaultPath)
|
|
|
|
LogDebug("envPath = %s", envPath)
|
|
|
|
LogDebug("usrPath = %s", usrPath)
|
|
|
|
LogDebug("path = %s", path)
|
2018-05-09 23:21:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func handleExecutable(ea ExtractionArgs) (success bool) {
|
2019-10-26 18:47:47 +02:00
|
|
|
// get the list of bitcode paths
|
2017-06-28 23:13:56 +02:00
|
|
|
artifactPaths := ea.Extractor(ea.InputFile)
|
2018-04-17 20:46:22 +02:00
|
|
|
|
2018-05-11 16:13:13 +02:00
|
|
|
if len(artifactPaths) < 20 {
|
|
|
|
// naert: to avoid saturating the log when dealing with big file lists
|
2018-05-10 22:44:59 +02:00
|
|
|
LogInfo("handleExecutable: artifactPaths = %v\n", artifactPaths)
|
|
|
|
}
|
2018-04-17 20:46:22 +02:00
|
|
|
|
2018-02-06 00:23:04 +01:00
|
|
|
if len(artifactPaths) == 0 {
|
2019-10-26 18:47:47 +02: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)
|
|
|
|
}
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
// Sort the bitcode files
|
|
|
|
if ea.SortBitcodeFiles {
|
|
|
|
LogWarning("Sorting bitcode files.")
|
|
|
|
sort.Strings(filesToLink)
|
|
|
|
sort.Strings(artifactPaths)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
|
|
|
|
// Write manifest
|
2017-07-05 20:00:07 +02:00
|
|
|
if ea.WriteManifest {
|
2019-10-26 18:47:47 +02:00
|
|
|
if !writeManifest(ea, filesToLink, artifactPaths) {
|
|
|
|
return
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2017-06-26 21:42:47 +02:00
|
|
|
|
2019-10-26 18:47:47 +02:00
|
|
|
success = linkBitcodeFiles(ea, filesToLink)
|
2019-10-26 02:37:23 +02:00
|
|
|
return
|
2018-04-28 03:31:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func handleThinArchive(ea ExtractionArgs) (success bool) {
|
2018-04-28 03:31:38 +02:00
|
|
|
// List bitcode files to link
|
|
|
|
var artifactFiles []string
|
|
|
|
|
|
|
|
var objectFiles []string
|
|
|
|
var bcFiles []string
|
|
|
|
|
2019-10-22 19:47:34 +02:00
|
|
|
objectFiles = listArchiveFiles(ea, ea.InputFile)
|
2018-04-28 03:31:38 +02:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
LogInfo("handleThinArchive: ExtractionArgs = %v\nobjectFiles = %v\n", ea, objectFiles)
|
2018-04-28 03:31:38 +02:00
|
|
|
|
|
|
|
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 {
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
// Sort the bitcode files
|
|
|
|
if ea.SortBitcodeFiles {
|
|
|
|
LogWarning("Sorting bitcode files.")
|
|
|
|
sort.Strings(bcFiles)
|
|
|
|
sort.Strings(artifactFiles)
|
|
|
|
}
|
|
|
|
|
2018-04-28 03:31:38 +02:00
|
|
|
// Build archive
|
2018-05-11 16:13:13 +02:00
|
|
|
if ea.BuildBitcodeModule {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = linkBitcodeFiles(ea, bcFiles)
|
2018-04-28 03:31:38 +02:00
|
|
|
} else {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = archiveBcFiles(ea, bcFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2018-04-28 03:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write manifest
|
|
|
|
if ea.WriteManifest {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = writeManifest(ea, bcFiles, artifactFiles)
|
2018-04-28 03:31:38 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogError("No bitcode files found\n")
|
2019-10-26 18:47:47 +02:00
|
|
|
success = false
|
2018-04-28 03:31:38 +02:00
|
|
|
}
|
2019-10-26 02:37:23 +02:00
|
|
|
return
|
2018-04-28 03:31:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func listArchiveFiles(ea ExtractionArgs, inputFile string) (contents []string) {
|
2018-05-02 22:05:24 +02:00
|
|
|
var arArgs []string
|
|
|
|
arArgs = append(arArgs, "-t")
|
|
|
|
arArgs = append(arArgs, inputFile)
|
2019-10-22 15:41:44 +02:00
|
|
|
output, err := runCmd(ea.ArchiverName, arArgs)
|
2018-05-02 22:05:24 +02:00
|
|
|
if err != nil {
|
2019-10-22 15:41:44 +02:00
|
|
|
LogWarning("ar command: %v %v", ea.ArchiverName, arArgs)
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("Failed to extract contents from archive %s because: %v.\n", inputFile, err)
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
|
|
|
contents = strings.Split(output, "\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func extractFile(ea ExtractionArgs, archive string, filename string, instance int) (success bool) {
|
2018-05-02 22:05:24 +02:00
|
|
|
var arArgs []string
|
2018-05-25 02:41:49 +02:00
|
|
|
if runtime.GOOS != osDARWIN {
|
2018-05-23 19:18:48 +02:00
|
|
|
arArgs = append(arArgs, "xN")
|
|
|
|
arArgs = append(arArgs, strconv.Itoa(instance))
|
|
|
|
} else {
|
2018-05-25 23:59:56 +02:00
|
|
|
if instance > 1 {
|
|
|
|
LogWarning("Cannot extract instance %v of %v from archive %s for instance > 1.\n", instance, filename, archive)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-25 23:59:56 +02:00
|
|
|
}
|
2018-05-23 19:18:48 +02:00
|
|
|
arArgs = append(arArgs, "x")
|
|
|
|
}
|
2018-05-02 22:05:24 +02:00
|
|
|
arArgs = append(arArgs, archive)
|
|
|
|
arArgs = append(arArgs, filename)
|
2019-10-22 15:41:44 +02:00
|
|
|
_, err := runCmd(ea.ArchiverName, arArgs)
|
2018-05-02 22:05:24 +02:00
|
|
|
if err != nil {
|
2019-10-22 15:41:44 +02:00
|
|
|
LogWarning("The archiver %v failed to extract instance %v of %v from archive %s because: %v.\n", ea.ArchiverName, instance, filename, archive, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func fetchTOC(ea ExtractionArgs, inputFile string) map[string]int {
|
2018-05-02 22:05:24 +02:00
|
|
|
toc := make(map[string]int)
|
|
|
|
|
2019-10-22 19:47:34 +02:00
|
|
|
contents := listArchiveFiles(ea, inputFile)
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
for _, item := range contents {
|
2018-05-23 19:18:48 +02:00
|
|
|
//iam: this is a hack to make get-bc work on libcurl.a
|
|
|
|
if item != "" && !strings.HasPrefix(item, "__.SYMDEF") {
|
2018-05-02 22:05:24 +02:00
|
|
|
toc[item]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return toc
|
|
|
|
}
|
|
|
|
|
2019-10-25 22:22:52 +02:00
|
|
|
//handleArchive processes an archive, and creates either a bitcode archive, or a module, depending on the flags used.
|
2018-05-02 22:05:24 +02:00
|
|
|
//
|
|
|
|
// Archives are strange beasts. handleArchive processes the archive by:
|
|
|
|
//
|
|
|
|
// 1. first creating a table of contents of the archive, which maps file names (in the archive) to the number of
|
|
|
|
// times a file with that name is stored in the archive.
|
|
|
|
//
|
2018-05-02 23:16:03 +02:00
|
|
|
// 2. for each OCCURRENCE of a file (name and count) it extracts the section from the object file, and adds the
|
2018-05-02 22:05:24 +02:00
|
|
|
// bitcode paths to the bitcode list.
|
|
|
|
//
|
|
|
|
// 3. it then either links all these bitcode files together using llvm-link, or else is creates a bitcode
|
|
|
|
// archive using llvm-ar
|
|
|
|
//
|
|
|
|
//iam: 5/1/2018
|
2019-10-28 15:38:49 +01:00
|
|
|
func handleArchive(ea ExtractionArgs) (success bool) {
|
2017-06-28 23:13:56 +02:00
|
|
|
// List bitcode files to link
|
|
|
|
var bcFiles []string
|
|
|
|
var artifactFiles []string
|
|
|
|
|
2018-05-02 22:05:24 +02:00
|
|
|
inputFile, _ := filepath.Abs(ea.InputFile)
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
LogInfo("handleArchive: ExtractionArgs = %v\n", ea)
|
2018-04-17 20:46:22 +02:00
|
|
|
|
2017-06-28 23:13:56 +02:00
|
|
|
// Create tmp dir
|
|
|
|
tmpDirName, err := ioutil.TempDir("", "gllvm")
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("The temporary directory in which to extract object files could not be created.")
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2019-10-26 18:47:47 +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
|
|
|
|
2018-05-02 22:05:24 +02:00
|
|
|
homeDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("Could not ascertain our whereabouts: %v", err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
2018-04-17 20:46:22 +02:00
|
|
|
|
2018-05-02 22:05:24 +02:00
|
|
|
err = os.Chdir(tmpDirName)
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("Could not cd to %v because: %v", tmpDirName, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:05:24 +02:00
|
|
|
//1. fetch the Table of Contents
|
2019-10-22 19:47:34 +02:00
|
|
|
toc := fetchTOC(ea, inputFile)
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
LogDebug("Table of Contents of %v:\n%v\n", inputFile, toc)
|
|
|
|
|
|
|
|
for obj, instance := range toc {
|
|
|
|
for i := 1; i <= instance; i++ {
|
|
|
|
|
2019-10-22 19:47:34 +02:00
|
|
|
if obj != "" && extractFile(ea, inputFile, obj, i) {
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
artifacts := ea.Extractor(obj)
|
|
|
|
LogInfo("\t%v\n", artifacts)
|
|
|
|
artifactFiles = append(artifactFiles, artifacts...)
|
|
|
|
for _, bc := range artifacts {
|
|
|
|
bcPath := resolveBitcodePath(bc)
|
2018-04-17 20:46:22 +02:00
|
|
|
if bcPath != "" {
|
|
|
|
bcFiles = append(bcFiles, bcPath)
|
|
|
|
}
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 22:05:24 +02:00
|
|
|
err = os.Chdir(homeDir)
|
2018-04-18 15:58:47 +02:00
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("Could not cd to %v because: %v", homeDir, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-04-18 15:58:47 +02:00
|
|
|
}
|
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 {
|
2018-05-02 22:05:24 +02:00
|
|
|
|
|
|
|
// Sort the bitcode files
|
|
|
|
if ea.SortBitcodeFiles {
|
|
|
|
LogWarning("Sorting bitcode files.")
|
|
|
|
sort.Strings(bcFiles)
|
|
|
|
sort.Strings(artifactFiles)
|
|
|
|
}
|
|
|
|
|
2018-04-17 20:46:22 +02:00
|
|
|
// Build archive
|
2018-05-11 16:13:13 +02:00
|
|
|
if ea.BuildBitcodeModule {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = linkBitcodeFiles(ea, bcFiles)
|
2018-04-17 20:46:22 +02:00
|
|
|
} else {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = archiveBcFiles(ea, bcFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !success {
|
|
|
|
//hopefully the failure has alreadu been reported...
|
2019-10-28 21:47:17 +01:00
|
|
|
return
|
2018-04-17 20:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write manifest
|
|
|
|
if ea.WriteManifest {
|
2019-10-26 18:47:47 +02:00
|
|
|
success = writeManifest(ea, bcFiles, artifactFiles)
|
2018-04-17 20:46:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LogError("No bitcode files found\n")
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2019-10-26 02:37:23 +02:00
|
|
|
return
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|
2017-06-23 23:08:46 +02:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func archiveBcFiles(ea ExtractionArgs, bcFiles []string) (success bool) {
|
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
|
2019-10-26 18:47:47 +02:00
|
|
|
var err error
|
2017-06-28 23:13:56 +02:00
|
|
|
args = append(args, "rs", absOutputFile)
|
|
|
|
args = append(args, bcFilesInDir...)
|
2019-10-26 18:47:47 +02:00
|
|
|
success, err = execCmd(ea.LlvmArchiverName, args, dir)
|
2019-10-22 19:47:34 +02:00
|
|
|
LogInfo("ea.LlvmArchiverName = %s, args = %v, dir = %s\n", ea.LlvmArchiverName, args, dir)
|
2017-06-30 19:00:25 +02:00
|
|
|
if !success {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("There was an error creating the bitcode archive: %v.\n", err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 19:18:48 +02:00
|
|
|
informUser("Built bitcode archive: %s.\n", ea.OutputFile)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
2019-10-26 03:53:45 +02:00
|
|
|
return
|
2017-06-23 23:08:46 +02:00
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:59 +02:00
|
|
|
func getsize(stringslice []string) (totalLength int) {
|
|
|
|
totalLength = 0
|
|
|
|
for _, s := range stringslice {
|
|
|
|
totalLength += len(s)
|
|
|
|
}
|
|
|
|
return totalLength
|
|
|
|
}
|
2018-05-11 16:13:13 +02:00
|
|
|
|
2018-05-10 23:30:22 +02:00
|
|
|
func formatStdOut(stdout bytes.Buffer, usefulIndex int) string {
|
|
|
|
infoArr := strings.Split(stdout.String(), "\n")[usefulIndex]
|
|
|
|
ret := strings.Fields(infoArr)
|
|
|
|
return ret[0]
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func fetchArgMax(ea ExtractionArgs) (argMax int) {
|
2018-05-11 00:55:46 +02:00
|
|
|
if ea.LinkArgSize == 0 {
|
|
|
|
getArgMax := exec.Command("getconf", "ARG_MAX")
|
|
|
|
var argMaxStr bytes.Buffer
|
|
|
|
getArgMax.Stdout = &argMaxStr
|
|
|
|
err := getArgMax.Run()
|
|
|
|
if err != nil {
|
|
|
|
LogError("getconf ARG_MAX failed with %s\n", err)
|
|
|
|
}
|
|
|
|
argMax, err = strconv.Atoi(formatStdOut(argMaxStr, 0))
|
|
|
|
if err != nil {
|
|
|
|
LogError("string conversion for argMax failed with %s\n", err)
|
|
|
|
}
|
|
|
|
argMax = int(0.9 * float32(argMax)) // keeping a comfort margin
|
|
|
|
} else {
|
|
|
|
argMax = ea.LinkArgSize
|
2018-05-10 22:44:59 +02:00
|
|
|
}
|
2018-05-11 16:13:13 +02:00
|
|
|
LogInfo("argMax = %v\n", argMax)
|
|
|
|
return
|
|
|
|
}
|
2018-05-11 00:55:46 +02:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func linkBitcodeFilesIncrementally(ea ExtractionArgs, filesToLink []string, argMax int, linkArgs []string) (success bool) {
|
2018-05-11 16:13:13 +02:00
|
|
|
var tmpFileList []string
|
|
|
|
// Create tmp dir
|
|
|
|
tmpDirName, err := ioutil.TempDir(".", "glinking")
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("The temporary directory in which to put temporary linking files could not be created.")
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
2018-05-11 16:30:19 +02:00
|
|
|
if !ea.KeepTemp { // delete temporary folder after used unless told otherwise
|
2018-05-11 16:13:13 +02:00
|
|
|
LogInfo("Temporary folder will be deleted")
|
|
|
|
defer CheckDefer(func() error { return os.RemoveAll(tmpDirName) })
|
|
|
|
} else {
|
|
|
|
LogInfo("Keeping the temporary folder")
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2018-05-10 22:44:59 +02:00
|
|
|
|
2018-05-11 16:13:13 +02:00
|
|
|
tmpFile, err := ioutil.TempFile(tmpDirName, "tmp")
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("The temporary linking file could not be created.")
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
|
|
|
tmpFileList = append(tmpFileList, tmpFile.Name())
|
|
|
|
linkArgs = append(linkArgs, "-o", tmpFile.Name())
|
|
|
|
|
|
|
|
LogInfo("llvm-link argument size : %d", getsize(filesToLink))
|
|
|
|
for _, file := range filesToLink {
|
|
|
|
linkArgs = append(linkArgs, file)
|
|
|
|
if getsize(linkArgs) > argMax {
|
|
|
|
LogInfo("Linking command size exceeding system capacity : splitting the command")
|
2019-10-25 18:27:32 +02:00
|
|
|
success, err = execCmd(ea.LlvmLinkerName, linkArgs, "")
|
2018-05-11 16:13:13 +02:00
|
|
|
if !success || err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("There was an error linking input files into %s because %v, on file %s.\n", ea.OutputFile, err, file)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = false
|
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
|
|
|
linkArgs = nil
|
2018-05-11 00:55:46 +02:00
|
|
|
|
2018-05-11 16:13:13 +02:00
|
|
|
if ea.Verbose {
|
|
|
|
linkArgs = append(linkArgs, "-v")
|
|
|
|
}
|
|
|
|
tmpFile, err = ioutil.TempFile(tmpDirName, "tmp")
|
|
|
|
if err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("Could not generate a temp file in %s because %v.\n", tmpDirName, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = false
|
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
|
|
|
tmpFileList = append(tmpFileList, tmpFile.Name())
|
|
|
|
linkArgs = append(linkArgs, "-o", tmpFile.Name())
|
2018-05-10 23:30:22 +02:00
|
|
|
}
|
2018-05-11 16:30:19 +02:00
|
|
|
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
2019-10-26 18:47:47 +02:00
|
|
|
success, err = execCmd(ea.LlvmLinkerName, linkArgs, "")
|
|
|
|
if !success || err != nil {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("There was an error linking input files into %s because %v.\n", tmpFile.Name(), err)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = false
|
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
|
|
|
linkArgs = nil
|
|
|
|
if ea.Verbose {
|
|
|
|
linkArgs = append(linkArgs, "-v")
|
|
|
|
}
|
|
|
|
linkArgs = append(linkArgs, tmpFileList...)
|
2018-05-10 23:30:22 +02:00
|
|
|
|
2018-05-11 16:13:13 +02:00
|
|
|
linkArgs = append(linkArgs, "-o", ea.OutputFile)
|
2018-05-10 23:30:22 +02:00
|
|
|
|
2019-10-25 18:27:32 +02:00
|
|
|
success, err = execCmd(ea.LlvmLinkerName, linkArgs, "")
|
2018-05-11 16:13:13 +02:00
|
|
|
if !success {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("There was an error linking input files into %s because %v.\n", ea.OutputFile, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
2018-05-23 19:18:48 +02:00
|
|
|
LogInfo("Bitcode file extracted to: %s, from files %v \n", ea.OutputFile, tmpFileList)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
2019-10-26 03:53:45 +02:00
|
|
|
return
|
2018-05-11 16:13:13 +02:00
|
|
|
}
|
2018-05-10 23:30:22 +02:00
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func linkBitcodeFiles(ea ExtractionArgs, filesToLink []string) (success bool) {
|
2018-05-11 16:13:13 +02:00
|
|
|
var linkArgs []string
|
|
|
|
// Extracting the command line max size from the environment if it is not specified
|
|
|
|
argMax := fetchArgMax(ea)
|
|
|
|
if ea.Verbose {
|
|
|
|
linkArgs = append(linkArgs, "-v")
|
|
|
|
}
|
|
|
|
if getsize(filesToLink) > argMax { //command line size too large for the OS (necessitated by chromium)
|
2019-10-26 03:53:45 +02:00
|
|
|
return linkBitcodeFilesIncrementally(ea, filesToLink, argMax, linkArgs)
|
2018-05-10 23:30:22 +02:00
|
|
|
} else {
|
2019-10-26 18:47:47 +02:00
|
|
|
var err error
|
2018-05-10 23:30:22 +02:00
|
|
|
linkArgs = append(linkArgs, "-o", ea.OutputFile)
|
|
|
|
linkArgs = append(linkArgs, filesToLink...)
|
2019-10-26 18:47:47 +02:00
|
|
|
success, err = execCmd(ea.LlvmLinkerName, linkArgs, "")
|
2018-05-10 23:30:22 +02:00
|
|
|
if !success {
|
2019-10-26 03:53:45 +02:00
|
|
|
LogError("There was an error linking input files into %s because %v.\n", ea.OutputFile, err)
|
2019-10-26 18:47:47 +02:00
|
|
|
return
|
2018-05-10 23:30:22 +02:00
|
|
|
}
|
2018-05-23 19:18:48 +02:00
|
|
|
informUser("Bitcode file extracted to: %s.\n", ea.OutputFile)
|
2017-06-28 23:13:56 +02:00
|
|
|
}
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
2019-10-26 03:53:45 +02:00
|
|
|
return
|
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 {
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("Mach-O file %s could not be read.", inputFile)
|
|
|
|
return
|
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 {
|
2018-08-06 18:43:38 +02:00
|
|
|
LogWarning("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile)
|
2019-10-26 18:47:47 +02:00
|
|
|
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-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 {
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("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
|
|
|
}
|
|
|
|
|
2019-10-28 15:38:49 +01:00
|
|
|
func writeManifest(ea ExtractionArgs, bcFiles []string, artifactFiles []string) (success bool) {
|
2018-04-28 18:23:55 +02:00
|
|
|
manifestFilename := ea.OutputFile + ".llvm.manifest"
|
2018-05-02 22:05:24 +02:00
|
|
|
//only go into the gory details if we have a store around.
|
|
|
|
if LLVMBitcodeStorePath != "" {
|
|
|
|
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 {
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("There was an error while writing the manifest file: ", err)
|
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contents := []byte("\n" + strings.Join(bcFiles, "\n") + "\n")
|
|
|
|
if err := ioutil.WriteFile(manifestFilename, contents, 0644); err != nil {
|
2019-10-26 18:47:47 +02:00
|
|
|
LogError("There was an error while writing the manifest file: ", err)
|
|
|
|
return
|
2018-05-02 22:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 19:18:48 +02:00
|
|
|
informUser("Manifest file written to %s.\n", manifestFilename)
|
2019-10-26 18:47:47 +02:00
|
|
|
success = true
|
|
|
|
return
|
2017-06-26 21:42:47 +02:00
|
|
|
}
|