• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang elf.Open函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: TestNoTextrel

// The shared library does not have relocations against the text segment.
func TestNoTextrel(t *testing.T) {
	sopath := filepath.Join(gorootInstallDir, soname)
	f, err := elf.Open(sopath)
	if err != nil {
		t.Fatal("elf.Open failed: ", err)
	}
	defer f.Close()
	if hasDynTag(f, elf.DT_TEXTREL) {
		t.Errorf("%s has DT_TEXTREL set", soname)
	}
}
开发者ID:danny8002,项目名称:go,代码行数:12,代码来源:shared_test.go


示例17: elfData

func elfData(t *testing.T, name string) *Data {
	f, err := elf.Open(name)
	if err != nil {
		t.Fatal(err)
	}

	d, err := f.DWARF()
	if err != nil {
		t.Fatal(err)
	}
	return d
}
开发者ID:8l,项目名称:go-learn,代码行数:12,代码来源:type_test.go


示例18: ExtractSymbolTable

// ExtractSymbolTable attempts to parse the DWARF section of a binary and return
// a symbol table. This currently just supports file names, and function
// definitions
func ExtractSymbolTable(binary string, offset uint64) (*SymbolTable, error) {
	files := make(SymbolTable)

	f, err := elf.Open(binary)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dwarfs, err := f.DWARF()
	if err != nil {
		return nil, err
	}

	dwarfReader := dwarfs.Reader()
	for {
		entry, _ := dwarfReader.Next()
		if entry == nil {
			break
		}

		/* TODO: This is all by value, make this references */
		// For now, all we need are files and functions
		switch entry.Tag {

		case dwarf.TagCompileUnit:
			file := extractFile(entry)
			// file.Lowpc += offset
			// file.Highpc += offset
			name := file.Filename

			if name == "" {
				continue
			}

			files[name] = file

		/* TODO: Stupid inefficient */
		case dwarf.TagSubprogram:
			fun := extractFunction(entry)
			// fun.Highpc += offset
			// fun.Lowpc += offset
			for _, v := range files {
				if contains(v, fun) {
					v.Functions[fun.Name] = fun
				}
			}
		}
	}

	return &files, nil

}
开发者ID:yan,项目名称:grace,代码行数:56,代码来源:symbols.go


示例19: dynStrings

func dynStrings(t *testing.T, path string, flag elf.DynTag) []string {
	f, err := elf.Open(path)
	defer f.Close()
	if err != nil {
		t.Fatalf("elf.Open(%q) failed: %v", path, err)
	}
	dynstrings, err := f.DynString(flag)
	if err != nil {
		t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err)
	}
	return dynstrings
}
开发者ID:hurkgu,项目名称:go,代码行数:12,代码来源:shared_test.go


示例20: ELFAnal

func ELFAnal(input string, symbolsDump bool) ([][]int, error) {
	// An array of arrays for storing the section offsets
	var sectionData [][]int

	fmt.Printf("[+] Analyzing binary: %s\n", input)

	// Check for executable type
	elfFmt, err := elf.Open(input)
	if err != nil {
		return sectionData, err
	}
	defer elfFmt.Close()

	sections := elfFmt.Sections
	sectionCount := len(sections)

	fmt.Printf("[+] Number of sections: %d\n", sectionCount)

	for k := range sections {
		sec := sections[k]
		secName := sec.Name
		secSize := sec.Size
		secOffset := sec.Offset + 1
		secEnd := int(secOffset) + int(secSize) - 1

		fmt.Printf("\t Name: %s\n", secName)
		fmt.Printf("\t Size: %d\n", secSize)
		fmt.Printf("\t Offset: %d\n", secOffset)
		fmt.Printf("\t Section end: %d\n", secEnd)
		fmt.Println("")

		sectionData = append(sectionData, []int{int(secOffset), int(secEnd)})
	}

	symbols, err := elfFmt.ImportedSymbols()
	if err != nil {
		return sectionData, err
	}

	numberOfSymbols := len(symbols)

	fmt.Printf("[+] Found %d symbols\n", numberOfSymbols)
	if numberOfSymbols > 0 && symbolsDump {
		for k := range symbols {
			symName := symbols[k]

			fmt.Printf("\t Name: %s\n", symName)
		}
		fmt.Println("")
	}

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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang elf.File类代码示例发布时间:2022-05-24
下一篇:
Golang elf.NewFile函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap