当前位置: 首页>>代码示例>>Golang>>正文


Golang elf.Open函数代码示例

本文整理汇总了Golang中debug/elf.Open函数的典型用法代码示例。如果您正苦于以下问题:Golang Open函数的具体用法?Golang Open怎么用?Golang Open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: sanity

func sanity() {
	goBinGo := path.Join(config.Gosrcroot, "go/bin/go")
	log.Printf("check %v as the go binary", goBinGo)
	_, err := os.Stat(goBinGo)
	if err == nil {
		config.Go = "go/bin/go"
	}
	// but does the one in go/bin/OS_ARCH exist too?
	archgo := fmt.Sprintf("bin/%s_%s/go", config.Goos, config.Arch)
	linuxBinGo := path.Join(config.Gosrcroot, archgo)
	log.Printf("check %v as the go binary", linuxBinGo)
	_, err = os.Stat(linuxBinGo)
	if err == nil {
		config.Go = archgo
		goBinGo = linuxBinGo
	}
	log.Printf("Using %v as the go command", goBinGo)
	if config.Go == "" {
		log.Fatalf("Can't find a go binary! Is GOROOT set correctly?")
	}
	f, err := elf.Open(goBinGo)
	if err != nil {
		log.Fatalf("%v is not an ELF file; don't know what to do", goBinGo)
	}
	ds := f.SectionByType(elf.SHT_DYNAMIC)
	if ds != nil {
		log.Printf("U-root requires a staticically built go tree at present. %v is dynamic.", goBinGo)
		log.Fatalf("To fix this:\ncd %v/src\nexport CGO_ENABLED=0\nGOARCH=%v ./make.bash", config.Goroot, config.Arch)
	}
}
开发者ID:rhiguita,项目名称:u-root,代码行数:30,代码来源:ramfs.go

示例2: IsELF

func IsELF(fPath string) error {
	if _, err := elf.Open(fPath); err != nil {
		return err
	}

	return nil
}
开发者ID:hcninja,项目名称:malpic,代码行数:7,代码来源:anal.go

示例3: cmdMode

func cmdMode(cmd string, args []string) error {
	f, err := elf.Open(cmd)
	if err != nil {
		return err
	}
	defer f.Close()

	// Set mem limits
	for _, p := range f.Progs {
		if p.Type != elf.PT_LOAD {
			continue
		}
		if p.Vaddr < startAddr {
			startAddr = p.Vaddr
		}
		if addr := p.Vaddr + p.Memsz; addr > endAddr {
			endAddr = addr
		}
	}

	pt, err := ForkExec(cmd, args)
	if err != nil {
		return err
	}
	if err := pt.Wait(printPC); err != nil {
		return err
	}
	return nil
}
开发者ID:jroimartin,项目名称:diff_tracer,代码行数:29,代码来源:main.go

示例4: getDwarf

func getDwarf(execname string) *dwarf.Data {
	e, err := elf.Open(execname)
	if err == nil {
		defer e.Close()
		d, err := e.DWARF()
		if err == nil {
			return d
		}
	}
	m, err := macho.Open(execname)
	if err == nil {
		defer m.Close()
		d, err := m.DWARF()
		if err == nil {
			return d
		}
	}
	p, err := pe.Open(execname)
	if err == nil {
		defer p.Close()
		d, err := p.DWARF()
		if err == nil {
			return d
		}
	}
	log.Fatal("can't get dwarf info from executable", err)
	return nil
}
开发者ID:lamproae,项目名称:hprof,代码行数:28,代码来源:parser.go

示例5: FindFunc

func FindFunc(fileName string, pc uint64) (s elf.Symbol, err error) {
	file, err := elf.Open(fileName)
	if err != nil {
		return elf.Symbol{}, err
	}

	symbols, err := file.Symbols()
	if err != nil {
		return elf.Symbol{}, err
	}

	funcSymbols := make(Symbols, 0)
	for _, symbol := range symbols {
		if elf.ST_TYPE(symbol.Info) == elf.STT_FUNC {
			funcSymbols = append(funcSymbols, symbol)
		}
	}
	n := len(funcSymbols)
	if n == 0 {
		return elf.Symbol{}, &ErrNoFunctions{}
	}

	sort.Sort(funcSymbols)
	i := sort.Search(n, func(i int) bool { return funcSymbols[i].Value >= pc })
	if i != 0 || funcSymbols[i].Value <= pc {
		if i == n || (funcSymbols[i].Value != pc && i != 0) {
			i--
		}
		return funcSymbols[i], nil
	}

	return elf.Symbol{}, &ErrFunctionNotFound{}
}
开发者ID:postfix,项目名称:findfunc,代码行数:33,代码来源:findfunc.go

示例6: testBuildID

func testBuildID(t *testing.T) {
	f, err := elf.Open("/proc/self/exe")
	if err != nil {
		if os.IsNotExist(err) {
			t.Skip("no /proc/self/exe")
		}
		t.Fatal("opening /proc/self/exe: ", err)
	}
	defer f.Close()

	c := 0
	for i, s := range f.Sections {
		if s.Type != elf.SHT_NOTE {
			continue
		}

		d, err := s.Data()
		if err != nil {
			t.Logf("reading data of note section %d: %v", i, err)
			continue
		}

		for len(d) > 0 {

			// ELF standards differ as to the sizes in
			// note sections.  Both the GNU linker and
			// gold always generate 32-bit sizes, so that
			// is what we assume here.

			if len(d) < 12 {
				t.Logf("note section %d too short (%d < 12)", i, len(d))
				continue
			}

			namesz := f.ByteOrder.Uint32(d)
			descsz := f.ByteOrder.Uint32(d[4:])
			typ := f.ByteOrder.Uint32(d[8:])

			an := (namesz + 3) &^ 3
			ad := (descsz + 3) &^ 3

			if int(12+an+ad) > len(d) {
				t.Logf("note section %d too short for header (%d < 12 + align(%d,4) + align(%d,4))", i, len(d), namesz, descsz)
				continue
			}

			// 3 == NT_GNU_BUILD_ID
			if typ == 3 && namesz == 4 && bytes.Equal(d[12:16], []byte("GNU\000")) {
				c++
			}

			d = d[12+an+ad:]
		}
	}

	if c > 1 {
		t.Errorf("found %d build ID notes", c)
	}
}
开发者ID:danny8002,项目名称:go,代码行数:59,代码来源:buildid_linux.go

示例7: main

func main() {
	file, err := elf.Open("hello")
	if err != nil {
		log.Fatalf("failed opening file: %s", err)
	}
	defer file.Close()
	printDwarfInformation(file)
}
开发者ID:rrudduck,项目名称:golang-stuff,代码行数:8,代码来源:dwarf.go

示例8: main

func main() {
	file, err := elf.Open("bash.elf")
	if err != nil {
		log.Fatalf("failed opening file: %s", err)
	}
	defer file.Close()
	printFileInformation(file)
}
开发者ID:rrudduck,项目名称:golang-stuff,代码行数:8,代码来源:elf.go

示例9: crack

func crack(file string, t *testing.T) (*elf.File, *Table) {
	// Open self
	f, err := elf.Open(file)
	if err != nil {
		t.Fatal(err)
	}
	return parse(file, f, t)
}
开发者ID:kostyll,项目名称:gccpy,代码行数:8,代码来源:pclntab_test.go

示例10: main

func main() {
	file, err := elf.Open("doozerd")
	if err != nil {
		log.Fatalf("failed opening file: %s", err)
	}
	defer file.Close()
	processGoInformation(file)
}
开发者ID:rrudduck,项目名称:golang-stuff,代码行数:8,代码来源:gosym.go

示例11: dynimport

func dynimport(obj string) {
	if f, err := elf.Open(obj); err == nil {
		sym, err := f.ImportedSymbols()
		if err != nil {
			fatalf("cannot load imported symbols from ELF file %s: %v", obj, err)
		}
		for _, s := range sym {
			targ := s.Name
			if s.Version != "" {
				targ += "@" + s.Version
			}
			fmt.Printf("#pragma dynimport %s %s %q\n", s.Name, targ, s.Library)
		}
		lib, err := f.ImportedLibraries()
		if err != nil {
			fatalf("cannot load imported libraries from ELF file %s: %v", obj, err)
		}
		for _, l := range lib {
			fmt.Printf("#pragma dynimport _ _ %q\n", l)
		}
		return
	}

	if f, err := macho.Open(obj); err == nil {
		sym, err := f.ImportedSymbols()
		if err != nil {
			fatalf("cannot load imported symbols from Mach-O file %s: %v", obj, err)
		}
		for _, s := range sym {
			if len(s) > 0 && s[0] == '_' {
				s = s[1:]
			}
			fmt.Printf("#pragma dynimport %s %s %q\n", s, s, "")
		}
		lib, err := f.ImportedLibraries()
		if err != nil {
			fatalf("cannot load imported libraries from Mach-O file %s: %v", obj, err)
		}
		for _, l := range lib {
			fmt.Printf("#pragma dynimport _ _ %q\n", l)
		}
		return
	}

	if f, err := pe.Open(obj); err == nil {
		sym, err := f.ImportedSymbols()
		if err != nil {
			fatalf("cannot load imported symbols from PE file %s: %v", obj, err)
		}
		for _, s := range sym {
			ss := strings.Split(s, ":", -1)
			fmt.Printf("#pragma dynimport %s %s %q\n", ss[0], ss[0], strings.ToLower(ss[1]))
		}
		return
	}

	fatalf("cannot parse %s as ELF, Mach-O or PE", obj)
}
开发者ID:jnwhiteh,项目名称:go,代码行数:58,代码来源:out.go

示例12: data2c

func data2c(name string, path string) (string, error) {
	var out []byte
	var in []byte

	if elf, err := elf.Open(path); err == nil {
		elf.Close()
		cwd, err := os.Getwd()
		tmpf, err := ioutil.TempFile(cwd, name)
		if err != nil {
			log.Fatalf("%v\n", err)
		}
		args := []string{"-o", tmpf.Name(), path}
		cmd := exec.Command(toolprefix+"strip", args...)
		cmd.Env = nil
		cmd.Stdin = os.Stdin
		cmd.Stderr = os.Stderr
		cmd.Stdout = os.Stdout
		log.Printf("%v", cmd.Args)
		err = cmd.Run()
		if err != nil {
			log.Fatalf("%v\n", err)
		}

		in, err = ioutil.ReadAll(tmpf)
		if err != nil {
			log.Fatalf("%v\n", err)
		}
		tmpf.Close()
		os.Remove(tmpf.Name())
	} else {
		var file *os.File
		var err error
		if file, err = os.Open(path); err != nil {
			log.Fatalf("%v", err)
		}
		in, err = ioutil.ReadAll(file)
		if err != nil {
			log.Fatalf("%v\n", err)
		}
		file.Close()
	}

	total := len(in)

	out = []byte(fmt.Sprintf("static unsigned char ramfs_%s_code[] = {\n", name))
	for len(in) > 0 {
		for j := 0; j < 16 && len(in) > 0; j++ {
			out = append(out, []byte(fmt.Sprintf("0x%02x, ", in[0]))...)
			in = in[1:]
		}
		out = append(out, '\n')
	}

	out = append(out, []byte(fmt.Sprintf("0,\n};\nint ramfs_%s_len = %v;\n", name, total))...)

	return string(out), nil
}
开发者ID:npe9,项目名称:harvey,代码行数:57,代码来源:build.go

示例13: HasSection

func HasSection(path string) bool {
	file, err := elf.Open(path)
	if err != nil {
		return false
	}
	defer file.Close()

	return file.Section("goblet") != nil
}
开发者ID:darkhelmet,项目名称:goblet,代码行数:9,代码来源:elf.go

示例14: main

func main() {
	f, err := elf.Open(os.Args[0])
	if err != nil {
		fmt.Println("  ", err)
		return
	}
	fmt.Println(f.FileHeader.ByteOrder)
	f.Close()
}
开发者ID:travis1230,项目名称:RosettaCodeData,代码行数:9,代码来源:host-introspection-2.go

示例15: dump_elf

func dump_elf(filename string) int {
	file, err := elf.Open(filename)
	if err != nil {
		fmt.Printf("Couldn’t open file : \"%s\" as an ELF.\n")
		return 2
	}
	dump_dynstr(file)
	dump_symbols(file)
	return 0
}
开发者ID:n4ss,项目名称:GoPlay,代码行数:10,代码来源:main.go


注:本文中的debug/elf.Open函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。