gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

funcdata_go115.go (4915B)


      1 // +build go1.15,!go1.16
      2 
      3 /*
      4  * Copyright 2021 ByteDance Inc.
      5  *
      6  * Licensed under the Apache License, Version 2.0 (the "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 
     19 package loader
     20 
     21 import (
     22     `unsafe`
     23 
     24     `github.com/bytedance/sonic/internal/rt`
     25 )
     26 
     27 type _Func struct {
     28     entry       uintptr // start pc
     29     nameoff     int32   // function name
     30     args        int32   // in/out args size
     31     deferreturn uint32  // offset of start of a deferreturn call instruction from entry, if any.
     32     pcsp        int32
     33     pcfile      int32
     34     pcln        int32
     35     npcdata     int32
     36     funcID      uint8   // set for certain special runtime functions
     37     _           [2]int8 // unused
     38     nfuncdata   uint8   // must be last
     39     argptrs     uintptr
     40     localptrs   uintptr
     41 }
     42 
     43 type _FuncTab struct {
     44     entry   uintptr
     45     funcoff uintptr
     46 }
     47 
     48 type _BitVector struct {
     49     n        int32 // # of bits
     50     bytedata *uint8
     51 }
     52 
     53 type _PtabEntry struct {
     54     name int32
     55     typ  int32
     56 }
     57 
     58 type _TextSection struct {
     59     vaddr    uintptr // prelinked section vaddr
     60     length   uintptr // section length
     61     baseaddr uintptr // relocated section address
     62 }
     63 
     64 type _ModuleData struct {
     65     pclntable             []byte
     66     ftab                  []_FuncTab
     67     filetab               []uint32
     68     findfunctab           *_FindFuncBucket
     69     minpc, maxpc          uintptr
     70     text, etext           uintptr
     71     noptrdata, enoptrdata uintptr
     72     data, edata           uintptr
     73     bss, ebss             uintptr
     74     noptrbss, enoptrbss   uintptr
     75     end, gcdata, gcbss    uintptr
     76     types, etypes         uintptr
     77     textsectmap           []_TextSection
     78     typelinks             []int32 // offsets from types
     79     itablinks             []*rt.GoItab
     80     ptab                  []_PtabEntry
     81     pluginpath            string
     82     pkghashes             []byte
     83     modulename            string
     84     modulehashes          []byte
     85     hasmain               uint8 // 1 if module contains the main function, 0 otherwise
     86     gcdatamask, gcbssmask _BitVector
     87     typemap               map[int32]*rt.GoType // offset to *_rtype in previous module
     88     bad                   bool                 // module failed to load and should be ignored
     89     next                  *_ModuleData
     90 }
     91 
     92 type _FindFuncBucket struct {
     93     idx        uint32
     94     subbuckets [16]byte
     95 }
     96 
     97 var findFuncTab = &_FindFuncBucket {
     98     idx: 1,
     99 }
    100 
    101 func registerFunction(name string, pc uintptr, textSize uintptr, fp int, args int, size uintptr, argPtrs []bool, localPtrs []bool) {
    102     mod := new(_ModuleData)
    103     minpc := pc
    104     maxpc := pc + size
    105 
    106     /* build the PC & line table */
    107     pclnt := []byte {
    108         0xfb, 0xff, 0xff, 0xff,     // magic   : 0xfffffffb
    109         0,                          // pad1    : 0
    110         0,                          // pad2    : 0
    111         1,                          // minLC   : 1
    112         4 << (^uintptr(0) >> 63),   // ptrSize : 4 << (^uintptr(0) >> 63)
    113     }
    114 
    115     // cache arg and local stackmap
    116     argptrs, localptrs := cacheStackmap(argPtrs, localPtrs, mod)
    117 
    118     /* add the function name */
    119     noff := len(pclnt)
    120     pclnt = append(append(pclnt, name...), 0)
    121 
    122     /* add PCDATA */
    123     pcsp := len(pclnt)
    124     pclnt = append(pclnt, encodeVariant((fp + 1) << 1)...)
    125     pclnt = append(pclnt, encodeVariant(int(size))...)
    126 
    127     /* function entry */
    128     fnv := _Func {
    129         entry     : pc,
    130         nameoff   : int32(noff),
    131         args      : int32(args),
    132         pcsp      : int32(pcsp),
    133         nfuncdata : 2,
    134         argptrs   : uintptr(argptrs),
    135         localptrs : uintptr(localptrs),
    136     }
    137 
    138     /* align the func to 8 bytes */
    139     if p := len(pclnt) % 8; p != 0 {
    140         pclnt = append(pclnt, make([]byte, 8 - p)...)
    141     }
    142 
    143     /* add the function descriptor */
    144     foff := len(pclnt)
    145     pclnt = append(pclnt, (*(*[unsafe.Sizeof(_Func{})]byte)(unsafe.Pointer(&fnv)))[:]...)
    146 
    147     /* function table */
    148     tab := []_FuncTab {
    149         {entry: pc, funcoff: uintptr(foff)},
    150         {entry: pc, funcoff: uintptr(foff)},
    151         {entry: maxpc},
    152     }
    153 
    154     /* module data */
    155     *mod = _ModuleData {
    156         pclntable   : pclnt,
    157         ftab        : tab,
    158         findfunctab : findFuncTab,
    159         minpc       : minpc,
    160         maxpc       : maxpc,
    161         modulename  : name,
    162         gcdata: uintptr(unsafe.Pointer(&emptyByte)),
    163         gcbss: uintptr(unsafe.Pointer(&emptyByte)),
    164     }
    165 
    166     /* verify and register the new module */
    167     moduledataverify1(mod)
    168     registerModule(mod)
    169 }