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


Golang elf.NewFile函数代码示例

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


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

示例1: newSectionReader

func newSectionReader(rf io.ReaderAt) (ret sectionReader, err error) {
	ret.macho, err = macho.NewFile(rf)
	if err != nil {
		ret.elf, err = elf.NewFile(rf)
	}
	return
}
开发者ID:ksurdy,项目名称:completion,代码行数:7,代码来源:info_test.go

示例2: zipExeReaderElf

// zipExeReaderElf treats the file as a ELF binary
// (linux/BSD/etc... executable) and attempts to find a zip archive.
func zipExeReaderElf(rda io.ReaderAt, size int64) (*zip.Reader, error) {
	file, err := elf.NewFile(rda)
	if err != nil {
		return nil, err
	}

	var max int64
	for _, sect := range file.Sections {
		if sect.Type == elf.SHT_NOBITS {
			continue
		}

		// Check if this section has a zip file
		if zfile, err := zip.NewReader(sect, int64(sect.Size)); err == nil {
			return zfile, nil
		}

		// Otherwise move end of file pointer
		end := int64(sect.Offset + sect.Size)
		if end > max {
			max = end
		}
	}

	// No zip file within binary, try appended to end
	section := io.NewSectionReader(rda, max, size-max)
	return zip.NewReader(section, section.Size())
}
开发者ID:Skarlso,项目名称:slick,代码行数:30,代码来源:zipexe.go

示例3: elfexec

func elfexec(b []byte) (uintptr, []KexecSegment, error) {
	f, err := elf.NewFile(bytes.NewReader(b))
	if err != nil {
		return 0, nil, err
	}
	scount := 0
	for _, v := range f.Progs {
		if v.Type.String() == "PT_LOAD" {
			scount++
		}
	}
	if scount > KEXEC_SEGMENT_MAX {
		log.Fatalf("Too many segments: got %v, max is %v", scount, KEXEC_SEGMENT_MAX)
	}
	segs := make([]KexecSegment, scount)
	for i, v := range f.Progs {
		if v.Type.String() == "PT_LOAD" {
			f := v.Open()
			b := pages(uintptr(v.Memsz))
			if _, err := f.Read(b[:v.Filesz]); err != nil {
				log.Fatalf("Reading %d bytes of program header %d: %v", v.Filesz, i, err)
			}
			segs[i] = makeseg(b, uintptr(v.Paddr))
		}
	}
	log.Printf("Using ELF image loader")
	return uintptr(f.Entry), segs, nil
	//return uintptr(0x40000), segs, nil
}
开发者ID:rhiguita,项目名称:u-root,代码行数:29,代码来源:kexec.go

示例4: TestRun

func TestRun(t *testing.T) {
	file := "samples/simple/O0/strings"
	f := ioReader(file)
	_elf, err := elf.NewFile(f)
	check(wrap(err))
	maps := loader.GetSegments(_elf)
	symbols := loader.GetSymbols(_elf)
	fmt.Println("done loading")
	emulator := MakeBlanketEmulator(maps)
	fmt.Println("done making blanket emulator")

	for rng, symb := range symbols {
		if symb.Type == ds.FUNC && strings.Contains(symb.Name, "str") {
			bbs := extract_bbs(maps, rng)
			if len(bbs) == 0 {
				continue
			}
			fmt.Printf("found function %v\n", symb.Name)
			fmt.Printf("running for %v \n", bbs)
			err := emulator.FullBlanket(bbs)
			if err != nil {
				log.WithFields(log.Fields{"error": err}).Fatal("Error running Blanket")
			}
			ev := emulator.Config.EventHandler.(*be.EventsToMinHash)
			fmt.Println("hash %v", ev.GetHash(60))
			//			fmt.Println("events %v", ev.Inspect())
		}
	}
}
开发者ID:eqv,项目名称:indika,代码行数:29,代码来源:simple_test.go

示例5: Fuzz

func Fuzz(data []byte) int {
	f, err := elf.NewFile(bytes.NewReader(data))
	if err != nil {
		if f != nil {
			panic("file is not nil on error")
		}
		return 0
	}
	defer f.Close()
	f.DynamicSymbols()
	f.ImportedLibraries()
	f.ImportedSymbols()
	f.Section(".data")
	f.SectionByType(elf.SHT_GNU_VERSYM)
	f.Symbols()
	dw, err := f.DWARF()
	if err != nil {
		if dw != nil {
			panic("dwarf is not nil on error")
		}
		return 1
	}
	dr := dw.Reader()
	for {
		e, _ := dr.Next()
		if e == nil {
			break
		}
	}
	return 2
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:31,代码来源:main.go

示例6: process

func process(file *os.File, name string) error {

	if f, err := elf.NewFile(file); err != nil {
		return err
	} else {
		if fl, err := f.ImportedLibraries(); err != nil {
			return err
		} else {
			if s := f.Section(".interp"); s == nil {
				return errors.New("No interpreter")
			} else {
				if interp, err := s.Data(); err != nil {
					return err
				} else {
					// We could just append the interp but people
					// expect to see that first.
					fl = append([]string{string(interp)}, fl...)
					for _, i := range fl {
						list[i] = true
					}
				}

			}
		}
	}
	return nil
}
开发者ID:rhiguita,项目名称:u-root,代码行数:27,代码来源:ldd.go

示例7: NewReader

// NewReader returns a reader for the export data section of an object
// (.o) or archive (.a) file read from r.
func NewReader(r io.Reader) (io.Reader, error) {
	data, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, err
	}

	// If the file is an archive, extract the first section.
	const archiveMagic = "!<arch>\n"
	if bytes.HasPrefix(data, []byte(archiveMagic)) {
		section, err := firstSection(data[len(archiveMagic):])
		if err != nil {
			return nil, err
		}
		data = section
	}

	// Data contains an ELF file with a .go_export section.
	// ELF magic number is "\x7fELF".
	ef, err := elf.NewFile(bytes.NewReader(data))
	if err != nil {
		return nil, err
	}
	sec := ef.Section(".go_export")
	if sec == nil {
		return nil, fmt.Errorf("no .go_export section")
	}
	return sec.Open(), nil
}
开发者ID:syreclabs,项目名称:go-tools,代码行数:30,代码来源:gccgoexportdata.go

示例8: Soname

func Soname(data []byte) (so string, ok bool) {
	e, err := elf.NewFile(bytes.NewReader(data))
	if err != nil {
		return so, false
	}
	section := e.Section(".dynamic")
	if section == nil {
		// not a dynamic binary.
		return so, false
	}
	// e.stringtable(section.Link)
	dynstr, _ := e.Sections[section.Link].Data()

	switch e.Class {
	case elf.ELFCLASS64:
		n := section.Size / 16 // 2*sizeof(uintptr)
		values := make([]elf.Dyn64, n)
		binary.Read(section.Open(), binary.LittleEndian, values)
		for _, v := range values {
			if elf.DynTag(v.Tag) == elf.DT_SONAME {
				return getstring(dynstr, int(v.Val)), true
			}
		}
	case elf.ELFCLASS32:
		n := section.Size / 8
		values := make([]elf.Dyn32, n)
		binary.Read(section.Open(), binary.LittleEndian, values)
		for _, v := range values {
			if elf.DynTag(v.Tag) == elf.DT_SONAME {
				return getstring(dynstr, int(v.Val)), true
			}
		}
	}
	return "", false
}
开发者ID:lyuyun,项目名称:loggregator,代码行数:35,代码来源:main.go

示例9: openElf

func openElf(r *os.File) (rawFile, error) {
	f, err := elf.NewFile(r)
	if err != nil {
		return nil, err
	}
	return &elfFile{f}, nil
}
开发者ID:sreis,项目名称:go,代码行数:7,代码来源:elf.go

示例10: openBinary

func openBinary(name string) (Binary, error) {
	f, err := os.Open(name)
	if err != nil {
		return nil, err
	}
	magic := make([]byte, 4)
	if _, err := f.ReadAt(magic[:], 0); err != nil {
		return nil, err
	}

	if bytes.HasPrefix(magic, []byte{0x7f, 'E', 'L', 'F'}) {
		e, err := elf.NewFile(f)
		if err != nil {
			return nil, err
		}
		return &elfBinary{File: e}, nil
	} else if bytes.HasPrefix(magic, []byte{'M', 'Z'}) {
		p, err := pe.Open(name)
		if err != nil {
			return nil, err
		}
		return &peBinary{File: p}, nil
	} else if bytes.HasPrefix(magic, []byte{0xcf, 0xfa, 0xed, 0xfe}) {
		m, err := macho.Open(name)
		if err != nil {
			return nil, err
		}
		return &machoBinary{File: m}, nil
	}
	return nil, fmt.Errorf("unsupported binary format")
}
开发者ID:ebfe,项目名称:gover,代码行数:31,代码来源:main.go

示例11: openDebugFile

func openDebugFile(r io.ReaderAt) (debugFile, error) {
	f, err := elf.NewFile(r)
	if err != nil {
		return nil, err
	}
	return &file{f}, nil
}
开发者ID:rainycape,项目名称:gondola,代码行数:7,代码来源:debug_elf.go

示例12: NewElfLoader

func NewElfLoader(r io.ReaderAt, arch string) (models.Loader, error) {
	file, err := elf.NewFile(r)
	if err != nil {
		return nil, err
	}
	var bits int
	switch file.Class {
	case elf.ELFCLASS32:
		bits = 32
	case elf.ELFCLASS64:
		bits = 64
	default:
		return nil, errors.New("Unknown ELF class.")
	}
	machineName, ok := machineMap[file.Machine]
	if !ok {
		return nil, fmt.Errorf("Unsupported machine: %s", file.Machine)
	}
	return &ElfLoader{
		LoaderHeader: LoaderHeader{
			arch:      machineName,
			bits:      bits,
			os:        "linux",
			entry:     file.Entry,
			byteOrder: file.ByteOrder,
		},
		file: file,
	}, nil
}
开发者ID:lunixbochs,项目名称:usercorn,代码行数:29,代码来源:elf.go

示例13: Run

func Run(file string) {
	f := ioReader(file)
	_elf, err := elf.NewFile(f)
	check(err)
	maps := GetSegments(_elf)
	_ = GetSymbols(_elf)
	fmt.Printf("%v\n", maps)
}
开发者ID:eqv,项目名称:indika,代码行数:8,代码来源:elf.go

示例14: openExportFile

// Opens the export data file at the given path. If this is an ELF file,
// searches for and opens the .go_export section. If this is an archive,
// reads the export data from the first member, which is assumed to be an ELF file.
// This is intended to replicate the logic in gofrontend.
func openExportFile(fpath string) (reader io.ReadSeeker, closer io.Closer, err error) {
	f, err := os.Open(fpath)
	if err != nil {
		return
	}
	closer = f
	defer func() {
		if err != nil && closer != nil {
			f.Close()
		}
	}()

	var magic [4]byte
	_, err = f.ReadAt(magic[:], 0)
	if err != nil {
		return
	}

	var elfreader io.ReaderAt
	switch string(magic[:]) {
	case gccgov1Magic, goimporterMagic:
		// Raw export data.
		reader = f
		return

	case archiveMagic:
		// TODO(pcc): Read the archive directly instead of using "ar".
		f.Close()
		closer = nil

		cmd := exec.Command("ar", "p", fpath)
		var out []byte
		out, err = cmd.Output()
		if err != nil {
			return
		}

		elfreader = bytes.NewReader(out)

	default:
		elfreader = f
	}

	ef, err := elf.NewFile(elfreader)
	if err != nil {
		return
	}

	sec := ef.Section(".go_export")
	if sec == nil {
		err = fmt.Errorf("%s: .go_export section not found", fpath)
		return
	}

	reader = sec.Open()
	return
}
开发者ID:4honor,项目名称:obdi,代码行数:61,代码来源:importer.go

示例15: loadTables

func loadTables(f *os.File) (textStart uint64, symtab, pclntab []byte, err error) {
	if obj, err := elf.NewFile(f); err == nil {
		if sect := obj.Section(".text"); sect != nil {
			textStart = sect.Addr
		}
		if sect := obj.Section(".gosymtab"); sect != nil {
			if symtab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		if sect := obj.Section(".gopclntab"); sect != nil {
			if pclntab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		return textStart, symtab, pclntab, nil
	}

	if obj, err := macho.NewFile(f); err == nil {
		if sect := obj.Section("__text"); sect != nil {
			textStart = sect.Addr
		}
		if sect := obj.Section("__gosymtab"); sect != nil {
			if symtab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		if sect := obj.Section("__gopclntab"); sect != nil {
			if pclntab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		return textStart, symtab, pclntab, nil
	}

	if obj, err := pe.NewFile(f); err == nil {
		if sect := obj.Section(".text"); sect != nil {
			textStart = uint64(sect.VirtualAddress)
		}
		if sect := obj.Section(".gosymtab"); sect != nil {
			if symtab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		if sect := obj.Section(".gopclntab"); sect != nil {
			if pclntab, err = sect.Data(); err != nil {
				return 0, nil, nil, err
			}
		}
		return textStart, symtab, pclntab, nil
	}

	return 0, nil, nil, fmt.Errorf("unrecognized binary format")
}
开发者ID:TomHoenderdos,项目名称:go-sunos,代码行数:54,代码来源:main.go


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