當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。