loader_latest.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // +build go1.16,!go1.23
  2. /*
  3. * Copyright 2021 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package loader
  18. import (
  19. `github.com/bytedance/sonic/loader/internal/rt`
  20. )
  21. // LoadFuncs loads only one function as module, and returns the function pointer
  22. // - text: machine code
  23. // - funcName: function name
  24. // - frameSize: stack frame size.
  25. // - argSize: argument total size (in bytes)
  26. // - argPtrs: indicates if a slot (8 Bytes) of arguments memory stores pointer, from low to high
  27. // - localPtrs: indicates if a slot (8 Bytes) of local variants memory stores pointer, from low to high
  28. //
  29. // WARN:
  30. // - the function MUST has fixed SP offset equaling to this, otherwise it go.gentraceback will fail
  31. // - the function MUST has only one stack map for all arguments and local variants
  32. func (self Loader) LoadOne(text []byte, funcName string, frameSize int, argSize int, argPtrs []bool, localPtrs []bool) Function {
  33. size := uint32(len(text))
  34. fn := Func{
  35. Name: funcName,
  36. TextSize: size,
  37. ArgsSize: int32(argSize),
  38. }
  39. // NOTICE: suppose the function has fixed SP offset equaling to frameSize, thus make only one pcsp pair
  40. fn.Pcsp = &Pcdata{
  41. {PC: size, Val: int32(frameSize)},
  42. }
  43. if self.NoPreempt {
  44. fn.PcUnsafePoint = &Pcdata{
  45. {PC: size, Val: PCDATA_UnsafePointUnsafe},
  46. }
  47. } else {
  48. fn.PcUnsafePoint = &Pcdata{
  49. {PC: size, Val: PCDATA_UnsafePointSafe},
  50. }
  51. }
  52. // NOTICE: suppose the function has only one stack map at index 0
  53. fn.PcStackMapIndex = &Pcdata{
  54. {PC: size, Val: 0},
  55. }
  56. if argPtrs != nil {
  57. args := rt.StackMapBuilder{}
  58. for _, b := range argPtrs {
  59. args.AddField(b)
  60. }
  61. fn.ArgsPointerMaps = args.Build()
  62. }
  63. if localPtrs != nil {
  64. locals := rt .StackMapBuilder{}
  65. for _, b := range localPtrs {
  66. locals.AddField(b)
  67. }
  68. fn.LocalsPointerMaps = locals.Build()
  69. }
  70. out := Load(text, []Func{fn}, self.Name + funcName, []string{self.File})
  71. return out[0]
  72. }
  73. // Load loads given machine codes and corresponding function information into go moduledata
  74. // and returns runnable function pointer
  75. // WARN: this API is experimental, use it carefully
  76. func Load(text []byte, funcs []Func, modulename string, filenames []string) (out []Function) {
  77. ids := make([]string, len(funcs))
  78. for i, f := range funcs {
  79. ids[i] = f.Name
  80. }
  81. // generate module data and allocate memory address
  82. mod := makeModuledata(modulename, filenames, &funcs, text)
  83. // verify and register the new module
  84. moduledataverify1(mod)
  85. registerModule(mod)
  86. //
  87. // encapsulate function address
  88. out = make([]Function, len(funcs))
  89. for i, s := range ids {
  90. for _, f := range funcs {
  91. if f.Name == s {
  92. m := uintptr(mod.text + uintptr(f.EntryOff))
  93. out[i] = Function(&m)
  94. }
  95. }
  96. }
  97. return
  98. }