本文整理汇总了Golang中debug/macho.NewFile函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFile函数的具体用法?Golang NewFile怎么用?Golang NewFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: openMacho
func openMacho(r *os.File) (rawFile, error) {
f, err := macho.NewFile(r)
if err != nil {
return nil, err
}
return &machoFile{f}, nil
}
示例2: openDebugFile
func openDebugFile(r io.ReaderAt) (debugFile, error) {
f, err := macho.NewFile(r)
if err != nil {
return nil, err
}
return &file{f}, nil
}
示例3: zipExeReaderMacho
// zipExeReaderMacho treats the file as a Mach-O binary
// (Mac OS X / Darwin executable) and attempts to find a zip archive.
func zipExeReaderMacho(rda io.ReaderAt, size int64) (*zip.Reader, error) {
file, err := macho.NewFile(rda)
if err != nil {
return nil, err
}
var max int64
for _, load := range file.Loads {
seg, ok := load.(*macho.Segment)
if ok {
// Check if the segment contains a zip file
if zfile, err := zip.NewReader(seg, int64(seg.Filesz)); err == nil {
return zfile, nil
}
// Otherwise move end of file pointer
end := int64(seg.Offset + seg.Filesz)
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())
}
示例4: 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
}
示例5: readMachoGoBuildID
// The Go build ID is stored at the beginning of the Mach-O __text segment.
// The caller has already opened filename, to get f, and read a few kB out, in data.
// Sadly, that's not guaranteed to hold the note, because there is an arbitrary amount
// of other junk placed in the file ahead of the main text.
func readMachoGoBuildID(filename string, f *os.File, data []byte) (buildid string, err error) {
// If the data we want has already been read, don't worry about Mach-O parsing.
// This is both an optimization and a hedge against the Mach-O parsing failing
// in the future due to, for example, the name of the __text section changing.
if b, err := readRawGoBuildID(filename, data); b != "" && err == nil {
return b, err
}
mf, err := macho.NewFile(f)
if err != nil {
return "", &os.PathError{Path: filename, Op: "parse", Err: err}
}
sect := mf.Section("__text")
if sect == nil {
// Every binary has a __text section. Something is wrong.
return "", &os.PathError{Path: filename, Op: "parse", Err: fmt.Errorf("cannot find __text section")}
}
// It should be in the first few bytes, but read a lot just in case,
// especially given our past problems on OS X with the build ID moving.
// There shouldn't be much difference between reading 4kB and 32kB:
// the hard part is getting to the data, not transferring it.
n := sect.Size
if n > uint64(BuildIDReadSize) {
n = uint64(BuildIDReadSize)
}
buf := make([]byte, n)
if _, err := f.ReadAt(buf, int64(sect.Offset)); err != nil {
return "", err
}
return readRawGoBuildID(filename, buf)
}
示例6: NewMachOLoader
func NewMachOLoader(r io.ReaderAt) (models.Loader, error) {
file, err := macho.NewFile(r)
if err != nil {
return nil, err
}
var bits int
switch file.Magic {
case macho.Magic32:
bits = 32
case macho.Magic64:
bits = 64
default:
return nil, errors.New("Unknown ELF class.")
}
machineName, ok := machoCpuMap[file.Cpu]
if !ok {
return nil, fmt.Errorf("Unsupported CPU: %s", file.Cpu)
}
entry, err := findEntry(file, bits)
if err != nil {
return nil, err
}
return &MachOLoader{
LoaderHeader: LoaderHeader{
arch: machineName,
bits: bits,
os: "darwin",
entry: entry,
},
file: file,
}, nil
}
示例7: machoSymbols
func machoSymbols(f *os.File) (syms []Sym, goarch string) {
p, err := macho.NewFile(f)
if err != nil {
errorf("parsing %s: %v", f.Name(), err)
return
}
if p.Symtab == nil {
errorf("%s: no symbol table", f.Name())
return
}
switch p.Cpu {
case macho.Cpu386:
goarch = "386"
case macho.CpuAmd64:
goarch = "amd64"
case macho.CpuArm:
goarch = "arm"
}
// Build sorted list of addresses of all symbols.
// We infer the size of a symbol by looking at where the next symbol begins.
var addrs []uint64
for _, s := range p.Symtab.Syms {
addrs = append(addrs, s.Value)
}
sort.Sort(uint64s(addrs))
for _, s := range p.Symtab.Syms {
sym := Sym{Name: s.Name, Addr: s.Value, Code: '?'}
i := sort.Search(len(addrs), func(x int) bool { return addrs[x] > s.Value })
if i < len(addrs) {
sym.Size = int64(addrs[i] - s.Value)
}
if s.Sect == 0 {
sym.Code = 'U'
} else if int(s.Sect) <= len(p.Sections) {
sect := p.Sections[s.Sect-1]
switch sect.Seg {
case "__TEXT":
sym.Code = 'R'
case "__DATA":
sym.Code = 'D'
}
switch sect.Seg + " " + sect.Name {
case "__TEXT __text":
sym.Code = 'T'
case "__DATA __bss", "__DATA __noptrbss":
sym.Code = 'B'
}
}
syms = append(syms, sym)
}
return
}
示例8: NewMachOLoader
func NewMachOLoader(r io.ReaderAt, archHint string) (models.Loader, error) {
var (
file *macho.File
fatFile *macho.FatFile
err error
)
magic := getMagic(r)
if bytes.Equal(magic, fatMagic) {
fatFile, err = macho.NewFatFile(r)
if fatFile != nil {
for _, arch := range fatFile.Arches {
if machineName, ok := machoCpuMap[arch.Cpu]; ok {
if machineName == archHint || archHint == "any" {
file = arch.File
break
}
}
}
if file == nil {
return nil, fmt.Errorf("Could not find Mach-O fat binary entry for arch '%s'.", archHint)
}
}
} else {
file, err = macho.NewFile(r)
}
if err != nil {
return nil, err
}
var bits int
switch file.Magic {
case macho.Magic32:
bits = 32
case macho.Magic64:
bits = 64
default:
return nil, errors.New("Unknown ELF class.")
}
machineName, ok := machoCpuMap[file.Cpu]
if !ok {
return nil, fmt.Errorf("Unsupported CPU: %s", file.Cpu)
}
entry, err := findEntry(file, bits)
if err != nil {
return nil, err
}
return &MachOLoader{
LoaderHeader: LoaderHeader{
arch: machineName,
bits: bits,
os: "darwin",
entry: entry,
},
file: file,
}, nil
}
示例9: 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")
}
示例10: NewFile
func NewFile(r io.ReaderAt) (binaryx.File, error) {
elfBinary, err := elf.NewFile(r)
if err == nil {
return newFile(&elfx.File{elfBinary})
}
machoBinary, err := macho.NewFile(r)
if err == nil {
return newFile(&machox.File{machoBinary})
}
peBinary, err := pe.NewFile(r)
if err == nil {
return newFile(&pex.File{peBinary})
}
return nil, err
}
示例11: grabDebugLineSection
func grabDebugLineSection(p string, t *testing.T) []byte {
f, err := os.Open(p)
if err != nil {
t.Fatal(err)
}
defer f.Close()
ef, err := elf.NewFile(f)
if err == nil {
data, _ := ef.Section(".debug_line").Data()
return data
}
mf, _ := macho.NewFile(f)
data, _ := mf.Section("__debug_line").Data()
return data
}
示例12: NewDWARFHelper
func NewDWARFHelper(id string, r io.ReaderAt) (*DWARFHelper, error) {
// TODO: detect file format..
if f, err := macho.NewFile(r); err != nil {
return nil, err
} else {
defer f.Close()
if df, err := f.DWARF(); err != nil {
for i := range f.Sections {
log4go.Debug(f.Sections[i])
}
return nil, err
} else {
var ret DWARFHelper
ret.df = df
ret.id = id
return &ret, nil
}
}
}
示例13: worker
func (fq *findQueue) worker() {
for fp := range fq.queue {
blacklisted := false
for _, re := range blacklistRegexps {
blacklisted = blacklisted || re.MatchString(fp)
}
if blacklisted {
continue
}
f, err := os.Open(fp)
if err != nil {
log.Printf("%s: %v", fp, err)
continue
}
fatFile, err := macho.NewFatFile(f)
if err == nil {
// The file is fat, so dump its architectures.
for _, fatArch := range fatFile.Arches {
fq.dumpMachOFile(fp, fatArch.File)
}
fatFile.Close()
} else if err == macho.ErrNotFat {
// The file isn't fat but may still be MachO.
thinFile, err := macho.NewFile(f)
if err != nil {
log.Printf("%s: %v", fp, err)
continue
}
fq.dumpMachOFile(fp, thinFile)
thinFile.Close()
} else {
f.Close()
}
}
}
示例14: machoCombineDwarf
// machoCombineDwarf merges dwarf info generated by dsymutil into a macho executable.
// With internal linking, DWARF is embedded into the executable, this lets us do the
// same for external linking.
// inexe is the path to the executable with no DWARF. It must have enough room in the macho
// header to add the DWARF sections. (Use ld's -headerpad option)
// dsym is the path to the macho file containing DWARF from dsymutil.
// outexe is the path where the combined executable should be saved.
func machoCombineDwarf(inexe, dsym, outexe string) error {
exef, err := os.Open(inexe)
if err != nil {
return err
}
dwarff, err := os.Open(dsym)
if err != nil {
return err
}
outf, err := os.Create(outexe)
if err != nil {
return err
}
outf.Chmod(0755)
exem, err := macho.NewFile(exef)
if err != nil {
return err
}
dwarfm, err := macho.NewFile(dwarff)
if err != nil {
return err
}
// The string table needs to be the last thing in the file
// for code signing to work. So we'll need to move the
// linkedit section, but all the others can be copied directly.
linkseg = exem.Segment("__LINKEDIT")
if linkseg == nil {
return fmt.Errorf("missing __LINKEDIT segment")
}
if _, err = exef.Seek(0, 0); err != nil {
return err
}
if _, err := io.CopyN(outf, exef, int64(linkseg.Offset)); err != nil {
return err
}
realdwarf = dwarfm.Segment("__DWARF")
if realdwarf == nil {
return fmt.Errorf("missing __DWARF segment")
}
// Now copy the dwarf data into the output.
maxalign := uint32(dwarfMinAlign) //
for _, sect := range dwarfm.Sections {
if sect.Align > maxalign {
maxalign = sect.Align
}
}
dwarfstart = machoCalcStart(realdwarf.Offset, linkseg.Offset, maxalign)
if _, err = outf.Seek(dwarfstart, 0); err != nil {
return err
}
if _, err = dwarff.Seek(int64(realdwarf.Offset), 0); err != nil {
return err
}
if _, err := io.CopyN(outf, dwarff, int64(realdwarf.Filesz)); err != nil {
return err
}
// And finally the linkedit section.
if _, err = exef.Seek(int64(linkseg.Offset), 0); err != nil {
return err
}
linkstart = machoCalcStart(linkseg.Offset, uint64(dwarfstart)+realdwarf.Filesz, pageAlign)
linkoffset = uint32(linkstart - int64(linkseg.Offset))
if _, err = outf.Seek(linkstart, 0); err != nil {
return err
}
if _, err := io.Copy(outf, exef); err != nil {
return err
}
// Now we need to update the headers.
cmdOffset := unsafe.Sizeof(exem.FileHeader)
is64bit := exem.Magic == macho.Magic64
if is64bit {
// mach_header_64 has one extra uint32.
cmdOffset += unsafe.Sizeof(exem.Magic)
}
textsect := exem.Section("__text")
if linkseg == nil {
return fmt.Errorf("missing __text section")
}
dwarfCmdOffset := int64(cmdOffset) + int64(exem.FileHeader.Cmdsz)
availablePadding := int64(textsect.Offset) - dwarfCmdOffset
if availablePadding < int64(realdwarf.Len) {
return fmt.Errorf("No room to add dwarf info. Need at least %d padding bytes, found %d", realdwarf.Len, availablePadding)
//.........这里部分代码省略.........
示例15: machoRead
// machoRead reads the mach-o file in data and returns a corresponding prog.
func machoRead(arch machoArch, data []byte) (*Prog, error) {
f, err := macho.NewFile(bytes.NewReader(data))
if err != nil {
return nil, err
}
var errors []string
errorf := func(format string, args ...interface{}) {
errors = append(errors, fmt.Sprintf(format, args...))
}
magic := uint32(0xFEEDFACE)
if arch.CPU&macho64Bit != 0 {
magic |= 1
}
if f.Magic != magic {
errorf("header: Magic = %#x, want %#x", f.Magic, magic)
}
if f.Cpu != macho.CpuAmd64 {
errorf("header: CPU = %#x, want %#x", f.Cpu, macho.CpuAmd64)
}
if f.SubCpu != 3 {
errorf("header: SubCPU = %#x, want %#x", f.SubCpu, 3)
}
if f.Type != 2 {
errorf("header: FileType = %d, want %d", f.Type, 2)
}
if f.Flags != 1 {
errorf("header: Flags = %d, want %d", f.Flags, 1)
}
msects := f.Sections
var limit uint64
prog := new(Prog)
for _, load := range f.Loads {
switch load := load.(type) {
default:
errorf("unexpected macho load %T %x", load, load.Raw())
case macho.LoadBytes:
if len(load) < 8 || len(load)%4 != 0 {
errorf("unexpected load length %d", len(load))
continue
}
cmd := f.ByteOrder.Uint32(load)
switch macho.LoadCmd(cmd) {
default:
errorf("unexpected macho load cmd %s", macho.LoadCmd(cmd))
case macho.LoadCmdUnixThread:
data := make([]uint32, len(load[8:])/4)
binary.Read(bytes.NewReader(load[8:]), f.ByteOrder, data)
if len(data) != 44 {
errorf("macho thread len(data) = %d, want 42", len(data))
continue
}
if data[0] != 4 {
errorf("macho thread type = %d, want 4", data[0])
}
if data[1] != uint32(len(data))-2 {
errorf("macho thread desc len = %d, want %d", data[1], uint32(len(data))-2)
continue
}
for i, val := range data[2:] {
switch i {
default:
if val != 0 {
errorf("macho thread data[%d] = %#x, want 0", i, val)
}
case 32:
prog.Entry = Addr(val)
case 33:
prog.Entry |= Addr(val) << 32
}
}
}
case *macho.Segment:
if load.Addr < limit {
errorf("segments out of order: %q at %#x after %#x", load.Name, load.Addr, limit)
}
limit = load.Addr + load.Memsz
if load.Name == "__PAGEZERO" || load.Addr == 0 && load.Filesz == 0 {
if load.Name != "__PAGEZERO" {
errorf("segment with Addr=0, Filesz=0 is named %q, want %q", load.Name, "__PAGEZERO")
} else if load.Addr != 0 || load.Filesz != 0 {
errorf("segment %q has Addr=%#x, Filesz=%d, want Addr=%#x, Filesz=%d", load.Name, load.Addr, load.Filesz, 0, 0)
}
prog.UnmappedSize = Addr(load.Memsz)
continue
}
if !strings.HasPrefix(load.Name, "__") {
errorf("segment name %q does not begin with %q", load.Name, "__")
}
if strings.ToUpper(load.Name) != load.Name {
errorf("segment name %q is not all upper case", load.Name)
}
seg := &Segment{
//.........这里部分代码省略.........