github.com/notti/nocgo@v0.0.0-20190619201224-fc443047424c/internal/fakecgo/symbols.go (about)

     1  package fakecgo
     2  
     3  import _ "unsafe" //needed for go:linkname
     4  
     5  // The following "fakes" all the necessary stuff for pretending we're using cgo, without actually doing that
     6  // -> iscgo will be set to true and all functions that are then required by the runtime implemented
     7  // This is necessary to get TLS working in the mainthread (cgo_init) and in all other threads (cgo_thread_start).
     8  // If we leave this out, libc can't use TLS since go runtime overwrites it (printf with %f already needs that)
     9  
    10  // The actual functions are implemented in assembly trampolines (trampoline_*.s) that call into carefully crafted golang (cgo.go)
    11  
    12  //go:linkname _cgo_init _cgo_init
    13  //go:linkname x_cgo_init_trampoline x_cgo_init_trampoline
    14  var x_cgo_init_trampoline byte
    15  var _cgo_init = &x_cgo_init_trampoline
    16  
    17  //go:linkname x_cgo_thread_start_trampoline x_cgo_thread_start_trampoline
    18  //go:linkname _cgo_thread_start _cgo_thread_start
    19  var x_cgo_thread_start_trampoline byte
    20  var _cgo_thread_start = &x_cgo_thread_start_trampoline
    21  
    22  //go:linkname x_cgo_notify_runtime_init_done_trampoline x_cgo_notify_runtime_init_done_trampoline
    23  //go:linkname _cgo_notify_runtime_init_done _cgo_notify_runtime_init_done
    24  var x_cgo_notify_runtime_init_done_trampoline byte
    25  var _cgo_notify_runtime_init_done = &x_cgo_notify_runtime_init_done_trampoline
    26  
    27  //go:linkname x_cgo_setenv_trampoline x_cgo_setenv_trampoline
    28  //go:linkname _cgo_setenv runtime._cgo_setenv
    29  var x_cgo_setenv_trampoline byte
    30  var _cgo_setenv = &x_cgo_setenv_trampoline
    31  
    32  //go:linkname x_cgo_unsetenv_trampoline x_cgo_unsetenv_trampoline
    33  //go:linkname _cgo_unsetenv runtime._cgo_unsetenv
    34  var x_cgo_unsetenv_trampoline byte
    35  var _cgo_unsetenv = &x_cgo_unsetenv_trampoline
    36  
    37  // let's pretend we have cgo:
    38  //go:linkname _iscgo runtime.iscgo
    39  var _iscgo = true
    40  
    41  // Now all the symbols we need to import from various libraries to implement the above functions:
    42  // (just using one variable and taking the address in libcFuncs.go works with amd64 - but the two variable dance is needed for 386, where we get an unknown symbol relocation otherwise :/)
    43  
    44  //go:linkname libc_pthread_attr_init_x libc_pthread_attr_init_x
    45  var libc_pthread_attr_init_x libcFunc
    46  var libc_pthread_attr_init = &libc_pthread_attr_init_x
    47  
    48  //go:linkname libc_pthread_attr_getstacksize_x libc_pthread_attr_getstacksize_x
    49  var libc_pthread_attr_getstacksize_x libcFunc
    50  var libc_pthread_attr_getstacksize = &libc_pthread_attr_getstacksize_x
    51  
    52  //go:linkname libc_pthread_attr_destroy_x libc_pthread_attr_destroy_x
    53  var libc_pthread_attr_destroy_x libcFunc
    54  var libc_pthread_attr_destroy = &libc_pthread_attr_destroy_x
    55  
    56  //go:linkname libc_pthread_sigmask_x libc_pthread_sigmask_x
    57  var libc_pthread_sigmask_x libcFunc
    58  var libc_pthread_sigmask = &libc_pthread_sigmask_x
    59  
    60  //go:linkname libc_pthread_create_x libc_pthread_create_x
    61  var libc_pthread_create_x libcFunc
    62  var libc_pthread_create = &libc_pthread_create_x
    63  
    64  //go:linkname libc_pthread_detach_x libc_pthread_detach_x
    65  var libc_pthread_detach_x libcFunc
    66  var libc_pthread_detach = &libc_pthread_detach_x
    67  
    68  //go:linkname libc_setenv_x libc_setenv_x
    69  var libc_setenv_x libcFunc
    70  var libc_setenv = &libc_setenv_x
    71  
    72  //go:linkname libc_unsetenv_x libc_unsetenv_x
    73  var libc_unsetenv_x libcFunc
    74  var libc_unsetenv = &libc_unsetenv_x
    75  
    76  //go:linkname libc_malloc_x libc_malloc_x
    77  var libc_malloc_x libcFunc
    78  var libc_malloc = &libc_malloc_x
    79  
    80  //go:linkname libc_free_x libc_free_x
    81  var libc_free_x libcFunc
    82  var libc_free = &libc_free_x
    83  
    84  //go:linkname libc_nanosleep_x libc_nanosleep_x
    85  var libc_nanosleep_x libcFunc
    86  var libc_nanosleep = &libc_nanosleep_x
    87  
    88  //go:linkname libc_sigfillset_x libc_sigfillset_x
    89  var libc_sigfillset_x libcFunc
    90  var libc_sigfillset = &libc_sigfillset_x
    91  
    92  //go:linkname libc_abort_x libc_abort_x
    93  var libc_abort_x libcFunc
    94  var libc_abort = &libc_abort_x
    95  
    96  //go:linkname libc_dprintf_x libc_dprintf_x
    97  var libc_dprintf_x libcFunc
    98  var libc_dprintf = &libc_dprintf_x
    99  
   100  //go:linkname libc_strerror_x libc_strerror_x
   101  var libc_strerror_x libcFunc
   102  var libc_strerror = &libc_strerror_x