loader_latest.go 3.4 KB

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