1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package macho implements access to Mach-O object files, as defined by
6 // http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html.
9 // High level access to low level data structures.
20 // A File represents an open Mach-O file.
23 ByteOrder binary.ByteOrder
33 // A Load represents any Mach-O load command.
38 // A LoadBytes is the uninterpreted bytes of a Mach-O load command.
41 func (b LoadBytes) Raw() []byte { return b }
43 // A SegmentHeader is the header for a Mach-O 32-bit or 64-bit load segment command.
44 type SegmentHeader struct {
58 // A Segment represents a Mach-O 32-bit or 64-bit load segment command.
63 // Embed ReaderAt for ReadAt method.
64 // Do not embed SectionReader directly
65 // to avoid having Read and Seek.
66 // If a client wants Read and Seek it must use
67 // Open() to avoid fighting over the seek offset
68 // with other clients.
73 // Data reads and returns the contents of the segment.
74 func (s *Segment) Data() ([]byte, os.Error) {
75 dat := make([]byte, s.sr.Size())
76 n, err := s.sr.ReadAt(dat, 0)
80 // Open returns a new ReadSeeker reading the segment.
81 func (s *Segment) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) }
83 type SectionHeader struct {
98 // Embed ReaderAt for ReadAt method.
99 // Do not embed SectionReader directly
100 // to avoid having Read and Seek.
101 // If a client wants Read and Seek it must use
102 // Open() to avoid fighting over the seek offset
103 // with other clients.
108 // Data reads and returns the contents of the Mach-O section.
109 func (s *Section) Data() ([]byte, os.Error) {
110 dat := make([]byte, s.sr.Size())
111 n, err := s.sr.ReadAt(dat, 0)
115 // Open returns a new ReadSeeker reading the Mach-O section.
116 func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) }
118 // A Dylib represents a Mach-O load dynamic library command.
123 CurrentVersion uint32
127 // A Symtab represents a Mach-O symbol table command.
134 // A Dysymtab represents a Mach-O dynamic symbol table command.
135 type Dysymtab struct {
138 IndirectSyms []uint32 // indices into Symtab.Syms
145 type FormatError struct {
151 func (e *FormatError) String() string {
154 msg += fmt.Sprintf(" '%v'", e.val)
156 msg += fmt.Sprintf(" in record at byte %#x", e.off)
160 // Open opens the named file using os.Open and prepares it for use as a Mach-O binary.
161 func Open(name string) (*File, os.Error) {
162 f, err := os.Open(name)
166 ff, err := NewFile(f)
175 // Close closes the File.
176 // If the File was created using NewFile directly instead of Open,
177 // Close has no effect.
178 func (f *File) Close() os.Error {
181 err = f.closer.Close()
187 // NewFile creates a new File for accessing a Mach-O binary in an underlying reader.
188 // The Mach-O binary is expected to start at position 0 in the ReaderAt.
189 func NewFile(r io.ReaderAt) (*File, os.Error) {
191 sr := io.NewSectionReader(r, 0, 1<<63-1)
193 // Read and decode Mach magic to determine byte order, size.
194 // Magic32 and Magic64 differ only in the bottom bit.
196 if _, err := r.ReadAt(ident[0:], 0); err != nil {
199 be := binary.BigEndian.Uint32(ident[0:])
200 le := binary.LittleEndian.Uint32(ident[0:])
201 switch Magic32 &^ 1 {
203 f.ByteOrder = binary.BigEndian
206 f.ByteOrder = binary.LittleEndian
209 return nil, &FormatError{0, "invalid magic number", nil}
212 // Read entire file header.
213 if err := binary.Read(sr, f.ByteOrder, &f.FileHeader); err != nil {
217 // Then load commands.
218 offset := int64(fileHeaderSize32)
219 if f.Magic == Magic64 {
220 offset = fileHeaderSize64
222 dat := make([]byte, f.Cmdsz)
223 if _, err := r.ReadAt(dat, offset); err != nil {
226 f.Loads = make([]Load, f.Ncmd)
228 for i := range f.Loads {
229 // Each load command begins with uint32 command and length.
231 return nil, &FormatError{offset, "command block too small", nil}
233 cmd, siz := LoadCmd(bo.Uint32(dat[0:4])), bo.Uint32(dat[4:8])
234 if siz < 8 || siz > uint32(len(dat)) {
235 return nil, &FormatError{offset, "invalid command block size", nil}
238 cmddat, dat = dat[0:siz], dat[siz:]
243 f.Loads[i] = LoadBytes(cmddat)
247 b := bytes.NewBuffer(cmddat)
248 if err := binary.Read(b, bo, &hdr); err != nil {
252 if hdr.Name >= uint32(len(cmddat)) {
253 return nil, &FormatError{offset, "invalid name in dynamic library command", hdr.Name}
255 l.Name = cstring(cmddat[hdr.Name:])
257 l.CurrentVersion = hdr.CurrentVersion
258 l.CompatVersion = hdr.CompatVersion
259 l.LoadBytes = LoadBytes(cmddat)
264 b := bytes.NewBuffer(cmddat)
265 if err := binary.Read(b, bo, &hdr); err != nil {
268 strtab := make([]byte, hdr.Strsize)
269 if _, err := r.ReadAt(strtab, int64(hdr.Stroff)); err != nil {
273 if f.Magic == Magic64 {
278 symdat := make([]byte, int(hdr.Nsyms)*symsz)
279 if _, err := r.ReadAt(symdat, int64(hdr.Symoff)); err != nil {
282 st, err := f.parseSymtab(symdat, strtab, cmddat, &hdr, offset)
289 case LoadCmdDysymtab:
291 b := bytes.NewBuffer(cmddat)
292 if err := binary.Read(b, bo, &hdr); err != nil {
295 dat := make([]byte, hdr.Nindirectsyms*4)
296 if _, err := r.ReadAt(dat, int64(hdr.Indirectsymoff)); err != nil {
299 x := make([]uint32, hdr.Nindirectsyms)
300 if err := binary.Read(bytes.NewBuffer(dat), bo, x); err != nil {
304 st.LoadBytes = LoadBytes(cmddat)
312 b := bytes.NewBuffer(cmddat)
313 if err := binary.Read(b, bo, &seg32); err != nil {
320 s.Name = cstring(seg32.Name[0:])
321 s.Addr = uint64(seg32.Addr)
322 s.Memsz = uint64(seg32.Memsz)
323 s.Offset = uint64(seg32.Offset)
324 s.Filesz = uint64(seg32.Filesz)
325 s.Maxprot = seg32.Maxprot
327 s.Nsect = seg32.Nsect
330 for i := 0; i < int(s.Nsect); i++ {
332 if err := binary.Read(b, bo, &sh32); err != nil {
336 sh.Name = cstring(sh32.Name[0:])
337 sh.Seg = cstring(sh32.Seg[0:])
338 sh.Addr = uint64(sh32.Addr)
339 sh.Size = uint64(sh32.Size)
340 sh.Offset = sh32.Offset
341 sh.Align = sh32.Align
342 sh.Reloff = sh32.Reloff
343 sh.Nreloc = sh32.Nreloc
344 sh.Flags = sh32.Flags
348 case LoadCmdSegment64:
350 b := bytes.NewBuffer(cmddat)
351 if err := binary.Read(b, bo, &seg64); err != nil {
358 s.Name = cstring(seg64.Name[0:])
360 s.Memsz = seg64.Memsz
361 s.Offset = seg64.Offset
362 s.Filesz = seg64.Filesz
363 s.Maxprot = seg64.Maxprot
365 s.Nsect = seg64.Nsect
368 for i := 0; i < int(s.Nsect); i++ {
370 if err := binary.Read(b, bo, &sh64); err != nil {
374 sh.Name = cstring(sh64.Name[0:])
375 sh.Seg = cstring(sh64.Seg[0:])
378 sh.Offset = sh64.Offset
379 sh.Align = sh64.Align
380 sh.Reloff = sh64.Reloff
381 sh.Nreloc = sh64.Nreloc
382 sh.Flags = sh64.Flags
387 s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.Filesz))
394 func (f *File) parseSymtab(symdat, strtab, cmddat []byte, hdr *SymtabCmd, offset int64) (*Symtab, os.Error) {
396 symtab := make([]Symbol, hdr.Nsyms)
397 b := bytes.NewBuffer(symdat)
398 for i := range symtab {
400 if f.Magic == Magic64 {
401 if err := binary.Read(b, bo, &n); err != nil {
406 if err := binary.Read(b, bo, &n32); err != nil {
413 n.Value = uint64(n32.Value)
416 if n.Name >= uint32(len(strtab)) {
417 return nil, &FormatError{offset, "invalid name in symbol table", n.Name}
419 sym.Name = cstring(strtab[n.Name:])
426 st.LoadBytes = LoadBytes(cmddat)
431 func (f *File) pushSection(sh *Section, r io.ReaderAt) {
432 f.Sections = append(f.Sections, sh)
433 sh.sr = io.NewSectionReader(r, int64(sh.Offset), int64(sh.Size))
437 func cstring(b []byte) string {
439 for i = 0; i < len(b) && b[i] != 0; i++ {
441 return string(b[0:i])
444 // Segment returns the first Segment with the given name, or nil if no such segment exists.
445 func (f *File) Segment(name string) *Segment {
446 for _, l := range f.Loads {
447 if s, ok := l.(*Segment); ok && s.Name == name {
454 // Section returns the first section with the given name, or nil if no such
456 func (f *File) Section(name string) *Section {
457 for _, s := range f.Sections {
465 // DWARF returns the DWARF debug information for the Mach-O file.
466 func (f *File) DWARF() (*dwarf.Data, os.Error) {
467 // There are many other DWARF sections, but these
468 // are the required ones, and the debug/dwarf package
469 // does not use the others, so don't bother loading them.
470 var names = [...]string{"abbrev", "info", "str"}
471 var dat [len(names)][]byte
472 for i, name := range names {
473 name = "__debug_" + name
476 return nil, os.NewError("missing Mach-O section " + name)
479 if err != nil && uint64(len(b)) < s.Size {
485 abbrev, info, str := dat[0], dat[1], dat[2]
486 return dwarf.New(abbrev, nil, nil, info, nil, nil, nil, str)
489 // ImportedSymbols returns the names of all symbols
490 // referred to by the binary f that are expected to be
491 // satisfied by other libraries at dynamic load time.
492 func (f *File) ImportedSymbols() ([]string, os.Error) {
493 if f.Dysymtab == nil || f.Symtab == nil {
494 return nil, &FormatError{0, "missing symbol table", nil}
500 for _, s := range st.Syms[dt.Iundefsym : dt.Iundefsym+dt.Nundefsym] {
501 all = append(all, s.Name)
506 // ImportedLibraries returns the paths of all libraries
507 // referred to by the binary f that are expected to be
508 // linked with the binary at dynamic link time.
509 func (f *File) ImportedLibraries() ([]string, os.Error) {
511 for _, l := range f.Loads {
512 if lib, ok := l.(*Dylib); ok {
513 all = append(all, lib.Name)