Source file src/cmd/internal/objfile/pe.go

     1  // Copyright 2013 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  // Parsing of PE executables (Microsoft Windows).
     6  
     7  package objfile
     8  
     9  import (
    10  	"debug/dwarf"
    11  	"debug/pe"
    12  	"fmt"
    13  	"io"
    14  	"slices"
    15  	"sort"
    16  )
    17  
    18  type peFile struct {
    19  	pe *pe.File
    20  }
    21  
    22  func openPE(r io.ReaderAt) (rawFile, error) {
    23  	f, err := pe.NewFile(r)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	return &peFile{f}, nil
    28  }
    29  
    30  func (f *peFile) symbols() ([]Sym, error) {
    31  	// Build sorted list of addresses of all symbols.
    32  	// We infer the size of a symbol by looking at where the next symbol begins.
    33  	var addrs []uint64
    34  
    35  	imageBase, _ := f.imageBase()
    36  
    37  	// When using internal linking we currently put BSS into the
    38  	// .data section. In order to set the B code correctly,
    39  	// we find the address of runtime.bss. Anything in a data section
    40  	// past runtime.bss is a BSS symbol.
    41  	//
    42  	// Just checking the section size in insufficient,
    43  	// as it will be rounded up to the page size.
    44  	// That is, there can be BSS symbols at the end of the page
    45  	// that holds the last data symbols.
    46  	var bssAddr uint32
    47  	var bssSectionNumber int16
    48  	for _, s := range f.pe.Symbols {
    49  		if s.Name == "runtime.bss" {
    50  			bssAddr = s.Value
    51  			bssSectionNumber = s.SectionNumber
    52  			break
    53  		}
    54  	}
    55  
    56  	var syms []Sym
    57  	for _, s := range f.pe.Symbols {
    58  		const (
    59  			N_UNDEF = 0  // An undefined (extern) symbol
    60  			N_ABS   = -1 // An absolute symbol (e_value is a constant, not an address)
    61  			N_DEBUG = -2 // A debugging symbol
    62  		)
    63  		sym := Sym{Name: s.Name, Addr: uint64(s.Value), Code: '?'}
    64  		switch s.SectionNumber {
    65  		case N_UNDEF:
    66  			sym.Code = 'U'
    67  		case N_ABS:
    68  			sym.Code = 'C'
    69  		case N_DEBUG:
    70  			sym.Code = '?'
    71  		default:
    72  			if s.SectionNumber < 0 || len(f.pe.Sections) < int(s.SectionNumber) {
    73  				return nil, fmt.Errorf("invalid section number in symbol table")
    74  			}
    75  			sect := f.pe.Sections[s.SectionNumber-1]
    76  			const (
    77  				text  = 0x20
    78  				data  = 0x40
    79  				bss   = 0x80
    80  				permW = 0x80000000
    81  			)
    82  			ch := sect.Characteristics
    83  			switch {
    84  			case ch&text != 0:
    85  				sym.Code = 'T'
    86  			case ch&data != 0:
    87  				if ch&permW == 0 {
    88  					sym.Code = 'R'
    89  				} else if bssSectionNumber == s.SectionNumber && bssAddr > 0 && s.Value >= bssAddr {
    90  					// Past runtime.bss is BSS.
    91  					sym.Code = 'B'
    92  				} else if s.Value >= sect.Size {
    93  					// Past section size is BSS.
    94  					sym.Code = 'B'
    95  				} else {
    96  					sym.Code = 'D'
    97  				}
    98  			case ch&bss != 0:
    99  				sym.Code = 'B'
   100  			}
   101  			sym.Addr += imageBase + uint64(sect.VirtualAddress)
   102  		}
   103  		syms = append(syms, sym)
   104  		addrs = append(addrs, sym.Addr)
   105  	}
   106  
   107  	slices.Sort(addrs)
   108  	for i := range syms {
   109  		j := sort.Search(len(addrs), func(x int) bool { return addrs[x] > syms[i].Addr })
   110  		if j < len(addrs) {
   111  			syms[i].Size = int64(addrs[j] - syms[i].Addr)
   112  		}
   113  	}
   114  
   115  	return syms, nil
   116  }
   117  
   118  func (f *peFile) pcln() (textStart uint64, pclntab []byte, err error) {
   119  	imageBase, err := f.imageBase()
   120  	if err != nil {
   121  		return 0, nil, err
   122  	}
   123  
   124  	if sect := f.pe.Section(".text"); sect != nil {
   125  		textStart = imageBase + uint64(sect.VirtualAddress)
   126  	}
   127  	if pclntab, err = loadPETable(f.pe, "runtime.pclntab", "runtime.epclntab"); err != nil {
   128  		// We didn't find the symbols, so look for the names used in 1.3 and earlier.
   129  		// TODO: Remove code looking for the old symbols when we no longer care about 1.3.
   130  		var err2 error
   131  		if pclntab, err2 = loadPETable(f.pe, "pclntab", "epclntab"); err2 != nil {
   132  			return 0, nil, err
   133  		}
   134  	}
   135  	return textStart, pclntab, nil
   136  }
   137  
   138  func (f *peFile) text() (textStart uint64, text []byte, err error) {
   139  	imageBase, err := f.imageBase()
   140  	if err != nil {
   141  		return 0, nil, err
   142  	}
   143  
   144  	sect := f.pe.Section(".text")
   145  	if sect == nil {
   146  		return 0, nil, fmt.Errorf("text section not found")
   147  	}
   148  	textStart = imageBase + uint64(sect.VirtualAddress)
   149  	text, err = sect.Data()
   150  	return
   151  }
   152  
   153  func findPESymbol(f *pe.File, name string) (*pe.Symbol, error) {
   154  	for _, s := range f.Symbols {
   155  		if s.Name != name {
   156  			continue
   157  		}
   158  		if s.SectionNumber <= 0 {
   159  			return nil, fmt.Errorf("symbol %s: invalid section number %d", name, s.SectionNumber)
   160  		}
   161  		if len(f.Sections) < int(s.SectionNumber) {
   162  			return nil, fmt.Errorf("symbol %s: section number %d is larger than max %d", name, s.SectionNumber, len(f.Sections))
   163  		}
   164  		return s, nil
   165  	}
   166  	return nil, fmt.Errorf("no %s symbol found", name)
   167  }
   168  
   169  func loadPETable(f *pe.File, sname, ename string) ([]byte, error) {
   170  	ssym, err := findPESymbol(f, sname)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	esym, err := findPESymbol(f, ename)
   175  	if err != nil {
   176  		return nil, err
   177  	}
   178  	if ssym.SectionNumber != esym.SectionNumber {
   179  		return nil, fmt.Errorf("%s and %s symbols must be in the same section", sname, ename)
   180  	}
   181  	sect := f.Sections[ssym.SectionNumber-1]
   182  	data, err := sect.Data()
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	return data[ssym.Value:esym.Value], nil
   187  }
   188  
   189  func (f *peFile) goarch() string {
   190  	switch f.pe.Machine {
   191  	case pe.IMAGE_FILE_MACHINE_I386:
   192  		return "386"
   193  	case pe.IMAGE_FILE_MACHINE_AMD64:
   194  		return "amd64"
   195  	case pe.IMAGE_FILE_MACHINE_ARM64:
   196  		return "arm64"
   197  	default:
   198  		return ""
   199  	}
   200  }
   201  
   202  func (f *peFile) loadAddress() (uint64, error) {
   203  	return f.imageBase()
   204  }
   205  
   206  func (f *peFile) imageBase() (uint64, error) {
   207  	switch oh := f.pe.OptionalHeader.(type) {
   208  	case *pe.OptionalHeader32:
   209  		return uint64(oh.ImageBase), nil
   210  	case *pe.OptionalHeader64:
   211  		return oh.ImageBase, nil
   212  	default:
   213  		return 0, fmt.Errorf("pe file format not recognized")
   214  	}
   215  }
   216  
   217  func (f *peFile) dwarf() (*dwarf.Data, error) {
   218  	return f.pe.DWARF()
   219  }
   220  

View as plain text