github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/nogo/objdump/objdump.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package objdump is a wrapper around relevant objdump flags.
    16  package objdump
    17  
    18  import (
    19  	"flag"
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"os/exec"
    24  )
    25  
    26  var (
    27  	// Binary is the binary under analysis.
    28  	//
    29  	// See Reader, below.
    30  	binary = flag.String("binary", "", "binary under analysis")
    31  
    32  	// Reader is the input stream.
    33  	//
    34  	// This may be set instead of Binary.
    35  	Reader io.Reader
    36  
    37  	// objdumpTool is the tool used to dump a binary.
    38  	objdumpTool = flag.String("objdump_tool", "", "tool used to dump a binary")
    39  )
    40  
    41  // LoadRaw reads the raw object output.
    42  func LoadRaw(fn func(r io.Reader) error) error {
    43  	var r io.Reader
    44  	if *binary != "" {
    45  		f, err := os.Open(*binary)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		defer f.Close()
    50  		r = f
    51  	} else if Reader != nil {
    52  		r = Reader
    53  	} else {
    54  		// We have no input stream.
    55  		return fmt.Errorf("no binary or reader provided")
    56  	}
    57  	return fn(r)
    58  }
    59  
    60  // Load reads the objdump output.
    61  func Load(fn func(r io.Reader) error) error {
    62  	var (
    63  		args  []string
    64  		stdin io.Reader
    65  	)
    66  	if *binary != "" {
    67  		args = append(args, *binary)
    68  	} else if Reader != nil {
    69  		stdin = Reader
    70  	} else {
    71  		// We have no input stream or binary.
    72  		return fmt.Errorf("no binary or reader provided")
    73  	}
    74  
    75  	// Construct our command.
    76  	cmd := exec.Command(*objdumpTool, args...)
    77  	cmd.Stdin = stdin
    78  	cmd.Stderr = os.Stderr
    79  	out, err := cmd.StdoutPipe()
    80  	if err != nil {
    81  		return err
    82  	}
    83  	if err := cmd.Start(); err != nil {
    84  		return err
    85  	}
    86  
    87  	// Call the user hook.
    88  	userErr := fn(out)
    89  
    90  	// Wait for the dump to finish.
    91  	if err := cmd.Wait(); userErr == nil && err != nil {
    92  		return err
    93  	}
    94  
    95  	return userErr
    96  }