Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/link/internal/ld/macho_combine_dwarf.go

Documentation: cmd/link/internal/ld

     1  // Copyright 2015 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.
     4  
     5  package ld
     6  
     7  import (
     8  	"bytes"
     9  	"compress/zlib"
    10  	"debug/macho"
    11  	"encoding/binary"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  	"reflect"
    16  	"unsafe"
    17  )
    18  
    19  type loadCmd struct {
    20  	Cmd macho.LoadCmd
    21  	Len uint32
    22  }
    23  
    24  type dyldInfoCmd struct {
    25  	Cmd                      macho.LoadCmd
    26  	Len                      uint32
    27  	RebaseOff, RebaseLen     uint32
    28  	BindOff, BindLen         uint32
    29  	WeakBindOff, WeakBindLen uint32
    30  	LazyBindOff, LazyBindLen uint32
    31  	ExportOff, ExportLen     uint32
    32  }
    33  
    34  type linkEditDataCmd struct {
    35  	Cmd              macho.LoadCmd
    36  	Len              uint32
    37  	DataOff, DataLen uint32
    38  }
    39  
    40  type encryptionInfoCmd struct {
    41  	Cmd                macho.LoadCmd
    42  	Len                uint32
    43  	CryptOff, CryptLen uint32
    44  	CryptId            uint32
    45  }
    46  
    47  type loadCmdReader struct {
    48  	offset, next int64
    49  	f            *os.File
    50  	order        binary.ByteOrder
    51  }
    52  
    53  func (r *loadCmdReader) Next() (loadCmd, error) {
    54  	var cmd loadCmd
    55  
    56  	r.offset = r.next
    57  	if _, err := r.f.Seek(r.offset, 0); err != nil {
    58  		return cmd, err
    59  	}
    60  	if err := binary.Read(r.f, r.order, &cmd); err != nil {
    61  		return cmd, err
    62  	}
    63  	r.next = r.offset + int64(cmd.Len)
    64  	return cmd, nil
    65  }
    66  
    67  func (r loadCmdReader) ReadAt(offset int64, data interface{}) error {
    68  	if _, err := r.f.Seek(r.offset+offset, 0); err != nil {
    69  		return err
    70  	}
    71  	return binary.Read(r.f, r.order, data)
    72  }
    73  
    74  func (r loadCmdReader) WriteAt(offset int64, data interface{}) error {
    75  	if _, err := r.f.Seek(r.offset+offset, 0); err != nil {
    76  		return err
    77  	}
    78  	return binary.Write(r.f, r.order, data)
    79  }
    80  
    81  // machoCombineDwarf merges dwarf info generated by dsymutil into a macho executable.
    82  //
    83  // With internal linking, DWARF is embedded into the executable, this lets us do the
    84  // same for external linking.
    85  // exef is the file of the executable with no DWARF. It must have enough room in the macho
    86  // header to add the DWARF sections. (Use ld's -headerpad option)
    87  // exem is the macho representation of exef.
    88  // dsym is the path to the macho file containing DWARF from dsymutil.
    89  // outexe is the path where the combined executable should be saved.
    90  func machoCombineDwarf(ctxt *Link, exef *os.File, exem *macho.File, dsym, outexe string) error {
    91  	dwarff, err := os.Open(dsym)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	defer dwarff.Close()
    96  	outf, err := os.OpenFile(outexe, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
    97  	if err != nil {
    98  		return err
    99  	}
   100  	defer outf.Close()
   101  	dwarfm, err := macho.NewFile(dwarff)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	defer dwarfm.Close()
   106  
   107  	// The string table needs to be the last thing in the file
   108  	// for code signing to work. So we'll need to move the
   109  	// linkedit section, but all the others can be copied directly.
   110  	linkseg := exem.Segment("__LINKEDIT")
   111  	if linkseg == nil {
   112  		return fmt.Errorf("missing __LINKEDIT segment")
   113  	}
   114  
   115  	if _, err := exef.Seek(0, 0); err != nil {
   116  		return err
   117  	}
   118  	if _, err := io.CopyN(outf, exef, int64(linkseg.Offset)); err != nil {
   119  		return err
   120  	}
   121  
   122  	realdwarf := dwarfm.Segment("__DWARF")
   123  	if realdwarf == nil {
   124  		return fmt.Errorf("missing __DWARF segment")
   125  	}
   126  
   127  	// Try to compress the DWARF sections. This includes some Apple
   128  	// proprietary sections like __apple_types.
   129  	compressedSects, compressedBytes, err := machoCompressSections(ctxt, dwarfm)
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	// Now copy the dwarf data into the output.
   135  	// Kernel requires all loaded segments to be page-aligned in the file,
   136  	// even though we mark this one as being 0 bytes of virtual address space.
   137  	dwarfstart := Rnd(int64(linkseg.Offset), int64(*FlagRound))
   138  	if _, err := outf.Seek(dwarfstart, 0); err != nil {
   139  		return err
   140  	}
   141  
   142  	if _, err := dwarff.Seek(int64(realdwarf.Offset), 0); err != nil {
   143  		return err
   144  	}
   145  
   146  	// Write out the compressed sections, or the originals if we gave up
   147  	// on compressing them.
   148  	var dwarfsize uint64
   149  	if compressedBytes != nil {
   150  		dwarfsize = uint64(len(compressedBytes))
   151  		if _, err := outf.Write(compressedBytes); err != nil {
   152  			return err
   153  		}
   154  	} else {
   155  		if _, err := io.CopyN(outf, dwarff, int64(realdwarf.Filesz)); err != nil {
   156  			return err
   157  		}
   158  		dwarfsize = realdwarf.Filesz
   159  	}
   160  
   161  	// And finally the linkedit section.
   162  	if _, err := exef.Seek(int64(linkseg.Offset), 0); err != nil {
   163  		return err
   164  	}
   165  	linkstart := Rnd(dwarfstart+int64(dwarfsize), int64(*FlagRound))
   166  	if _, err := outf.Seek(linkstart, 0); err != nil {
   167  		return err
   168  	}
   169  	if _, err := io.Copy(outf, exef); err != nil {
   170  		return err
   171  	}
   172  
   173  	// Now we need to update the headers.
   174  	textsect := exem.Section("__text")
   175  	if textsect == nil {
   176  		return fmt.Errorf("missing __text section")
   177  	}
   178  
   179  	cmdOffset := unsafe.Sizeof(exem.FileHeader)
   180  	if is64bit := exem.Magic == macho.Magic64; is64bit {
   181  		// mach_header_64 has one extra uint32.
   182  		cmdOffset += unsafe.Sizeof(exem.Magic)
   183  	}
   184  	dwarfCmdOffset := uint32(cmdOffset) + exem.FileHeader.Cmdsz
   185  	availablePadding := textsect.Offset - dwarfCmdOffset
   186  	if availablePadding < realdwarf.Len {
   187  		return fmt.Errorf("no room to add dwarf info. Need at least %d padding bytes, found %d", realdwarf.Len, availablePadding)
   188  	}
   189  	// First, copy the dwarf load command into the header. It will be
   190  	// updated later with new offsets and lengths as necessary.
   191  	if _, err := outf.Seek(int64(dwarfCmdOffset), 0); err != nil {
   192  		return err
   193  	}
   194  	if _, err := io.CopyN(outf, bytes.NewReader(realdwarf.Raw()), int64(realdwarf.Len)); err != nil {
   195  		return err
   196  	}
   197  	if _, err := outf.Seek(int64(unsafe.Offsetof(exem.FileHeader.Ncmd)), 0); err != nil {
   198  		return err
   199  	}
   200  	if err := binary.Write(outf, exem.ByteOrder, exem.Ncmd+1); err != nil {
   201  		return err
   202  	}
   203  	if err := binary.Write(outf, exem.ByteOrder, exem.Cmdsz+realdwarf.Len); err != nil {
   204  		return err
   205  	}
   206  
   207  	reader := loadCmdReader{next: int64(cmdOffset), f: outf, order: exem.ByteOrder}
   208  	for i := uint32(0); i < exem.Ncmd; i++ {
   209  		cmd, err := reader.Next()
   210  		if err != nil {
   211  			return err
   212  		}
   213  		linkoffset := uint64(linkstart) - linkseg.Offset
   214  		switch cmd.Cmd {
   215  		case macho.LoadCmdSegment64:
   216  			err = machoUpdateSegment(reader, linkseg, linkoffset)
   217  		case macho.LoadCmdSegment:
   218  			panic("unexpected 32-bit segment")
   219  		case LC_DYLD_INFO, LC_DYLD_INFO_ONLY:
   220  			err = machoUpdateLoadCommand(reader, linkseg, linkoffset, &dyldInfoCmd{}, "RebaseOff", "BindOff", "WeakBindOff", "LazyBindOff", "ExportOff")
   221  		case macho.LoadCmdSymtab:
   222  			err = machoUpdateLoadCommand(reader, linkseg, linkoffset, &macho.SymtabCmd{}, "Symoff", "Stroff")
   223  		case macho.LoadCmdDysymtab:
   224  			err = machoUpdateLoadCommand(reader, linkseg, linkoffset, &macho.DysymtabCmd{}, "Tocoffset", "Modtaboff", "Extrefsymoff", "Indirectsymoff", "Extreloff", "Locreloff")
   225  		case LC_CODE_SIGNATURE, LC_SEGMENT_SPLIT_INFO, LC_FUNCTION_STARTS, LC_DATA_IN_CODE, LC_DYLIB_CODE_SIGN_DRS:
   226  			err = machoUpdateLoadCommand(reader, linkseg, linkoffset, &linkEditDataCmd{}, "DataOff")
   227  		case LC_ENCRYPTION_INFO, LC_ENCRYPTION_INFO_64:
   228  			err = machoUpdateLoadCommand(reader, linkseg, linkoffset, &encryptionInfoCmd{}, "CryptOff")
   229  		case macho.LoadCmdDylib, macho.LoadCmdThread, macho.LoadCmdUnixThread, LC_PREBOUND_DYLIB, LC_UUID, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_SOURCE_VERSION, LC_MAIN, LC_LOAD_DYLINKER, LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB, LC_RPATH, LC_ID_DYLIB, LC_SYMSEG, LC_LOADFVMLIB, LC_IDFVMLIB, LC_IDENT, LC_FVMFILE, LC_PREPAGE, LC_ID_DYLINKER, LC_ROUTINES, LC_SUB_FRAMEWORK, LC_SUB_UMBRELLA, LC_SUB_CLIENT, LC_SUB_LIBRARY, LC_TWOLEVEL_HINTS, LC_PREBIND_CKSUM, LC_ROUTINES_64, LC_LAZY_LOAD_DYLIB, LC_LOAD_UPWARD_DYLIB, LC_DYLD_ENVIRONMENT, LC_LINKER_OPTION, LC_LINKER_OPTIMIZATION_HINT, LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS, LC_VERSION_NOTE, LC_BUILD_VERSION:
   230  			// Nothing to update
   231  		default:
   232  			err = fmt.Errorf("unknown load command 0x%x (%s)", int(cmd.Cmd), cmd.Cmd)
   233  		}
   234  		if err != nil {
   235  			return err
   236  		}
   237  	}
   238  	// Do the final update of the DWARF segment's load command.
   239  	return machoUpdateDwarfHeader(&reader, compressedSects, dwarfsize, dwarfstart, realdwarf)
   240  }
   241  
   242  // machoCompressSections tries to compress the DWARF segments in dwarfm,
   243  // returning the updated sections and segment contents, nils if the sections
   244  // weren't compressed, or an error if there was a problem reading dwarfm.
   245  func machoCompressSections(ctxt *Link, dwarfm *macho.File) ([]*macho.Section, []byte, error) {
   246  	if !ctxt.compressDWARF {
   247  		return nil, nil, nil
   248  	}
   249  
   250  	dwarfseg := dwarfm.Segment("__DWARF")
   251  	var sects []*macho.Section
   252  	var buf bytes.Buffer
   253  
   254  	for _, sect := range dwarfm.Sections {
   255  		if sect.Seg != "__DWARF" {
   256  			continue
   257  		}
   258  
   259  		// As of writing, there are no relocations in dsymutil's output
   260  		// so there's no point in worrying about them. Bail out if that
   261  		// changes.
   262  		if sect.Nreloc != 0 {
   263  			return nil, nil, nil
   264  		}
   265  
   266  		data, err := sect.Data()
   267  		if err != nil {
   268  			return nil, nil, err
   269  		}
   270  
   271  		compressed, contents, err := machoCompressSection(data)
   272  		if err != nil {
   273  			return nil, nil, err
   274  		}
   275  
   276  		newSec := *sect
   277  		newSec.Offset = uint32(dwarfseg.Offset) + uint32(buf.Len())
   278  		newSec.Addr = dwarfseg.Addr + uint64(buf.Len())
   279  		if compressed {
   280  			newSec.Name = "__z" + sect.Name[2:]
   281  			newSec.Size = uint64(len(contents))
   282  		}
   283  		sects = append(sects, &newSec)
   284  		buf.Write(contents)
   285  	}
   286  	return sects, buf.Bytes(), nil
   287  }
   288  
   289  // machoCompressSection compresses secBytes if it results in less data.
   290  func machoCompressSection(sectBytes []byte) (compressed bool, contents []byte, err error) {
   291  	var buf bytes.Buffer
   292  	buf.WriteString("ZLIB")
   293  	var sizeBytes [8]byte
   294  	binary.BigEndian.PutUint64(sizeBytes[:], uint64(len(sectBytes)))
   295  	buf.Write(sizeBytes[:])
   296  
   297  	z := zlib.NewWriter(&buf)
   298  	if _, err := z.Write(sectBytes); err != nil {
   299  		return false, nil, err
   300  	}
   301  	if err := z.Close(); err != nil {
   302  		return false, nil, err
   303  	}
   304  	if buf.Len() >= len(sectBytes) {
   305  		return false, sectBytes, nil
   306  	}
   307  	return true, buf.Bytes(), nil
   308  }
   309  
   310  // machoUpdateSegment updates the load command for a moved segment.
   311  // Only the linkedit segment should move, and it should have 0 sections.
   312  func machoUpdateSegment(r loadCmdReader, linkseg *macho.Segment, linkoffset uint64) error {
   313  	var seg macho.Segment64
   314  	if err := r.ReadAt(0, &seg); err != nil {
   315  		return err
   316  	}
   317  
   318  	// Only the linkedit segment moved, anything before that is fine.
   319  	if seg.Offset < linkseg.Offset {
   320  		return nil
   321  	}
   322  	seg.Offset += linkoffset
   323  	if err := r.WriteAt(0, &seg); err != nil {
   324  		return err
   325  	}
   326  	// There shouldn't be any sections, but just to make sure...
   327  	return machoUpdateSections(r, &seg, linkoffset, nil)
   328  }
   329  
   330  func machoUpdateSections(r loadCmdReader, seg *macho.Segment64, deltaOffset uint64, compressedSects []*macho.Section) error {
   331  	nsect := seg.Nsect
   332  	if nsect == 0 {
   333  		return nil
   334  	}
   335  	sectOffset := int64(unsafe.Sizeof(*seg))
   336  
   337  	var sect macho.Section64
   338  	sectSize := int64(unsafe.Sizeof(sect))
   339  	for i := uint32(0); i < nsect; i++ {
   340  		if err := r.ReadAt(sectOffset, &sect); err != nil {
   341  			return err
   342  		}
   343  		if compressedSects != nil {
   344  			cSect := compressedSects[i]
   345  			copy(sect.Name[:], cSect.Name)
   346  			sect.Size = cSect.Size
   347  			if cSect.Offset != 0 {
   348  				sect.Offset = cSect.Offset + uint32(deltaOffset)
   349  			}
   350  			if cSect.Addr != 0 {
   351  				sect.Addr = cSect.Addr
   352  			}
   353  		} else {
   354  			if sect.Offset != 0 {
   355  				sect.Offset += uint32(deltaOffset)
   356  			}
   357  			if sect.Reloff != 0 {
   358  				sect.Reloff += uint32(deltaOffset)
   359  			}
   360  		}
   361  		if err := r.WriteAt(sectOffset, &sect); err != nil {
   362  			return err
   363  		}
   364  		sectOffset += sectSize
   365  	}
   366  	return nil
   367  }
   368  
   369  // machoUpdateDwarfHeader updates the DWARF segment load command.
   370  func machoUpdateDwarfHeader(r *loadCmdReader, compressedSects []*macho.Section, dwarfsize uint64, dwarfstart int64, realdwarf *macho.Segment) error {
   371  	cmd, err := r.Next()
   372  	if err != nil {
   373  		return err
   374  	}
   375  	if cmd.Cmd != macho.LoadCmdSegment64 {
   376  		panic("not a Segment64")
   377  	}
   378  	var seg macho.Segment64
   379  	if err := r.ReadAt(0, &seg); err != nil {
   380  		return err
   381  	}
   382  	seg.Offset = uint64(dwarfstart)
   383  
   384  	if compressedSects != nil {
   385  		var segSize uint64
   386  		for _, newSect := range compressedSects {
   387  			segSize += newSect.Size
   388  		}
   389  		seg.Filesz = segSize
   390  	} else {
   391  		seg.Filesz = dwarfsize
   392  	}
   393  
   394  	// We want the DWARF segment to be considered non-loadable, so
   395  	// force vmaddr and vmsize to zero. In addition, set the initial
   396  	// protection to zero so as to make the dynamic loader happy,
   397  	// since otherwise it may complain that that the vm size and file
   398  	// size don't match for the segment. See issues 21647 and 32673
   399  	// for more context. Also useful to refer to the Apple dynamic
   400  	// loader source, specifically ImageLoaderMachO::sniffLoadCommands
   401  	// in ImageLoaderMachO.cpp (various versions can be found online, see
   402  	// https://opensource.apple.com/source/dyld/dyld-519.2.2/src/ImageLoaderMachO.cpp.auto.html
   403  	// as one example).
   404  	seg.Addr = 0
   405  	seg.Memsz = 0
   406  	seg.Prot = 0
   407  
   408  	if err := r.WriteAt(0, &seg); err != nil {
   409  		return err
   410  	}
   411  	return machoUpdateSections(*r, &seg, uint64(dwarfstart)-realdwarf.Offset, compressedSects)
   412  }
   413  
   414  func machoUpdateLoadCommand(r loadCmdReader, linkseg *macho.Segment, linkoffset uint64, cmd interface{}, fields ...string) error {
   415  	if err := r.ReadAt(0, cmd); err != nil {
   416  		return err
   417  	}
   418  	value := reflect.Indirect(reflect.ValueOf(cmd))
   419  
   420  	for _, name := range fields {
   421  		field := value.FieldByName(name)
   422  		if fieldval := field.Uint(); fieldval >= linkseg.Offset {
   423  			field.SetUint(fieldval + linkoffset)
   424  		}
   425  	}
   426  	if err := r.WriteAt(0, cmd); err != nil {
   427  		return err
   428  	}
   429  	return nil
   430  }
   431  

View as plain text