// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pe

import (
	
	
	
)

const COFFSymbolSize = 18

// COFFSymbol represents single COFF symbol table record.
type COFFSymbol struct {
	Name               [8]uint8
	Value              uint32
	SectionNumber      int16
	Type               uint16
	StorageClass       uint8
	NumberOfAuxSymbols uint8
}

func readCOFFSymbols( *FileHeader,  io.ReadSeeker) ([]COFFSymbol, error) {
	if .PointerToSymbolTable == 0 {
		return nil, nil
	}
	if .NumberOfSymbols <= 0 {
		return nil, nil
	}
	,  := .Seek(int64(.PointerToSymbolTable), seekStart)
	if  != nil {
		return nil, fmt.Errorf("fail to seek to symbol table: %v", )
	}
	 := make([]COFFSymbol, .NumberOfSymbols)
	 = binary.Read(, binary.LittleEndian, )
	if  != nil {
		return nil, fmt.Errorf("fail to read symbol table: %v", )
	}
	return , nil
}

// isSymNameOffset checks symbol name if it is encoded as offset into string table.
func isSymNameOffset( [8]byte) (bool, uint32) {
	if [0] == 0 && [1] == 0 && [2] == 0 && [3] == 0 {
		return true, binary.LittleEndian.Uint32([4:])
	}
	return false, 0
}

// FullName finds real name of symbol sym. Normally name is stored
// in sym.Name, but if it is longer then 8 characters, it is stored
// in COFF string table st instead.
func ( *COFFSymbol) ( StringTable) (string, error) {
	if ,  := isSymNameOffset(.Name);  {
		return .String()
	}
	return cstring(.Name[:]), nil
}

func removeAuxSymbols( []COFFSymbol,  StringTable) ([]*Symbol, error) {
	if len() == 0 {
		return nil, nil
	}
	 := make([]*Symbol, 0)
	 := uint8(0)
	for ,  := range  {
		if  > 0 {
			--
			continue
		}
		,  := .FullName()
		if  != nil {
			return nil, 
		}
		 = .NumberOfAuxSymbols
		 := &Symbol{
			Name:          ,
			Value:         .Value,
			SectionNumber: .SectionNumber,
			Type:          .Type,
			StorageClass:  .StorageClass,
		}
		 = append(, )
	}
	return , nil
}

// Symbol is similar to COFFSymbol with Name field replaced
// by Go string. Symbol also does not have NumberOfAuxSymbols.
type Symbol struct {
	Name          string
	Value         uint32
	SectionNumber int16
	Type          uint16
	StorageClass  uint8
}