]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: define correct complex types values for COFF symbols
authorqmuntal <quimmuntal@gmail.com>
Fri, 10 Mar 2023 15:19:58 +0000 (16:19 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Fri, 10 Mar 2023 22:47:16 +0000 (22:47 +0000)
This CL updates IMAGE_SYM_DTYPE_FUNCTION and IMAGE_SYM_DTYPE_ARRAY
definition and usage so their value can be set to what's defined in
the Microsoft PE docs [1], fixing a long-standing TODO.

[1] https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#type-representation

Change-Id: I93c19eb78e8a770e8c72245fe9495647e2c5ae5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/475355
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/link/internal/ld/pe.go
src/cmd/link/internal/loadpe/ldpe.go

index 10c1dc4ab3ea73cf632f72fee61e7ab8d49ece7b..266e165920c001b8228c4233d115960ecf959512 100644 (file)
@@ -98,11 +98,10 @@ const (
 // See https://docs.microsoft.com/en-us/windows/win32/debug/pe-format.
 // TODO(crawshaw): add these constants to debug/pe.
 const (
-       // TODO: the Microsoft doco says IMAGE_SYM_DTYPE_ARRAY is 3 and IMAGE_SYM_DTYPE_FUNCTION is 2
        IMAGE_SYM_TYPE_NULL      = 0
        IMAGE_SYM_TYPE_STRUCT    = 8
-       IMAGE_SYM_DTYPE_FUNCTION = 0x20
-       IMAGE_SYM_DTYPE_ARRAY    = 0x30
+       IMAGE_SYM_DTYPE_FUNCTION = 2
+       IMAGE_SYM_DTYPE_ARRAY    = 3
        IMAGE_SYM_CLASS_EXTERNAL = 2
        IMAGE_SYM_CLASS_STATIC   = 3
 
@@ -754,14 +753,12 @@ func (f *peFile) writeSymbols(ctxt *Link) {
                if ctxt.IsExternal() {
                        peSymType = IMAGE_SYM_TYPE_NULL
                } else {
-                       // TODO: fix IMAGE_SYM_DTYPE_ARRAY value and use following expression, instead of 0x0308
-                       // peSymType = IMAGE_SYM_DTYPE_ARRAY<<8 + IMAGE_SYM_TYPE_STRUCT
-                       peSymType = 0x0308 // "array of structs"
+                       peSymType = IMAGE_SYM_DTYPE_ARRAY<<8 + IMAGE_SYM_TYPE_STRUCT
                }
                sect, value, err := f.mapToPESection(ldr, s, ctxt.LinkMode)
                if err != nil {
                        if t == sym.SDYNIMPORT || t == sym.SHOSTOBJ || t == sym.SUNDEFEXT {
-                               peSymType = IMAGE_SYM_DTYPE_FUNCTION
+                               peSymType = IMAGE_SYM_DTYPE_FUNCTION << 8
                        } else {
                                ctxt.Errorf(s, "addpesym: %v", err)
                        }
index 0d33823e4ee2e9750469dcf83c767651ea10a021..7ad4db9052b0636769acc42a704ea20a8923ed00 100644 (file)
@@ -21,7 +21,6 @@ import (
 )
 
 const (
-       // TODO: the Microsoft doco says IMAGE_SYM_DTYPE_ARRAY is 3 (same with IMAGE_SYM_DTYPE_POINTER and IMAGE_SYM_DTYPE_FUNCTION)
        IMAGE_SYM_UNDEFINED              = 0
        IMAGE_SYM_ABSOLUTE               = -1
        IMAGE_SYM_DEBUG                  = -2
@@ -43,9 +42,9 @@ const (
        IMAGE_SYM_TYPE_DWORD             = 15
        IMAGE_SYM_TYPE_PCODE             = 32768
        IMAGE_SYM_DTYPE_NULL             = 0
-       IMAGE_SYM_DTYPE_POINTER          = 0x10
-       IMAGE_SYM_DTYPE_FUNCTION         = 0x20
-       IMAGE_SYM_DTYPE_ARRAY            = 0x30
+       IMAGE_SYM_DTYPE_POINTER          = 1
+       IMAGE_SYM_DTYPE_FUNCTION         = 2
+       IMAGE_SYM_DTYPE_ARRAY            = 3
        IMAGE_SYM_CLASS_END_OF_FUNCTION  = -1
        IMAGE_SYM_CLASS_NULL             = 0
        IMAGE_SYM_CLASS_AUTOMATIC        = 1
@@ -673,7 +672,7 @@ func (state *peLoaderState) readpesym(pesym *pe.COFFSymbol) (*loader.SymbolBuild
 
        var s loader.Sym
        var bld *loader.SymbolBuilder
-       switch pesym.Type {
+       switch uint8(pesym.Type >> 8) {
        default:
                return nil, 0, fmt.Errorf("%s: invalid symbol type %d", symname, pesym.Type)