Annotation of parser3/src/lib/ltdl/loaders/load_add_on.c, revision 1.2

1.1       moko        1: /* loader-load_add_on.c --  dynamic linking for BeOS
                      2: 
1.2     ! moko        3:    Copyright (C) 1998-2000, 2004, 2006-2008, 2011-2019, 2021-2024 Free
        !             4:    Software Foundation, Inc.
1.1       moko        5:    Written by Thomas Tanner, 1998
                      6: 
                      7:    NOTE: The canonical source of this file is maintained with the
                      8:    GNU Libtool package.  Report bugs to bug-libtool@gnu.org.
                      9: 
                     10: GNU Libltdl is free software; you can redistribute it and/or
                     11: modify it under the terms of the GNU Lesser General Public
                     12: License as published by the Free Software Foundation; either
                     13: version 2 of the License, or (at your option) any later version.
                     14: 
                     15: As a special exception to the GNU Lesser General Public License,
                     16: if you distribute this file as part of a program or library that
                     17: is built using GNU Libtool, you may include this file under the
                     18: same distribution terms that you use for the rest of that program.
                     19: 
                     20: GNU Libltdl is distributed in the hope that it will be useful,
                     21: but WITHOUT ANY WARRANTY; without even the implied warranty of
                     22: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     23: GNU Lesser General Public License for more details.
                     24: 
                     25: You should have received a copy of the GNU Lesser General Public
1.2     ! moko       26: License along with GNU Libltdl.  If not, see <https://www.gnu.org/licenses/>.
1.1       moko       27: */
                     28: 
                     29: #include "lt__private.h"
                     30: #include "lt_dlloader.h"
                     31: 
                     32: /* Use the preprocessor to rename non-static symbols to avoid namespace
                     33:    collisions when the loader code is statically linked into libltdl.
                     34:    Use the "<module_name>_LTX_" prefix so that the symbol addresses can
                     35:    be fetched from the preloaded symbol list by lt_dlsym():  */
                     36: #define get_vtable     load_add_on_LTX_get_vtable
                     37: 
                     38: LT_BEGIN_C_DECLS
                     39: LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
                     40: LT_END_C_DECLS
                     41: 
                     42: 
                     43: /* Boilerplate code to set up the vtable for hooking this loader into
                     44:    libltdl's loader list:  */
                     45: static int      vl_exit  (lt_user_data loader_data);
                     46: static lt_module vm_open  (lt_user_data loader_data, const char *filename,
                     47:                            lt_dladvise advise);
                     48: static int      vm_close (lt_user_data loader_data, lt_module module);
                     49: static void *   vm_sym   (lt_user_data loader_data, lt_module module,
                     50:                          const char *symbolname);
                     51: 
                     52: static lt_dlvtable *vtable = 0;
                     53: 
                     54: /* Return the vtable for this loader, only the name and sym_prefix
                     55:    attributes (plus the virtual function implementations, obviously)
                     56:    change between loaders.  */
                     57: lt_dlvtable *
                     58: get_vtable (lt_user_data loader_data)
                     59: {
                     60:   if (!vtable)
                     61:     {
                     62:       vtable = lt__zalloc (sizeof *vtable);
                     63:     }
                     64: 
                     65:   if (vtable && !vtable->name)
                     66:     {
                     67:       vtable->name             = "lt_load_add_on";
                     68:       vtable->module_open      = vm_open;
                     69:       vtable->module_close     = vm_close;
                     70:       vtable->find_sym         = vm_sym;
                     71:       vtable->dlloader_exit    = vl_exit;
                     72:       vtable->dlloader_data    = loader_data;
                     73:       vtable->priority         = LT_DLLOADER_APPEND;
                     74:     }
                     75: 
                     76:   if (vtable && (vtable->dlloader_data != loader_data))
                     77:     {
                     78:       LT__SETERROR (INIT_LOADER);
                     79:       return 0;
                     80:     }
                     81: 
                     82:   return vtable;
                     83: }
                     84: 
                     85: 
                     86: 
                     87: /* --- IMPLEMENTATION --- */
                     88: 
                     89: 
                     90: #include <kernel/image.h>
                     91: 
                     92: /* A function called through the vtable when this loader is no
                     93:    longer needed by the application.  */
                     94: static int
1.2     ! moko       95: vl_exit (lt_user_data loader_data LT__UNUSED)
1.1       moko       96: {
                     97:   vtable = NULL;
                     98:   return 0;
                     99: }
                    100: 
                    101: /* A function called through the vtable to open a module with this
                    102:    loader.  Returns an opaque representation of the newly opened
                    103:    module for processing with this loader's other vtable functions.  */
                    104: static lt_module
1.2     ! moko      105: vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
        !           106:          lt_dladvise advise LT__UNUSED)
1.1       moko      107: {
                    108:   image_id image = 0;
                    109: 
                    110:   if (filename)
                    111:     {
                    112:       image = load_add_on (filename);
                    113:     }
                    114:   else
                    115:     {
                    116:       image_info info;
                    117:       int32 cookie = 0;
                    118:       if (get_next_image_info (0, &cookie, &info) == B_OK)
                    119:        image = load_add_on (info.name);
                    120:     }
                    121: 
                    122:   if (image <= 0)
                    123:     {
                    124:       LT__SETERROR (CANNOT_OPEN);
                    125:       image = 0;
                    126:     }
                    127: 
                    128:   return (lt_module) image;
                    129: }
                    130: 
                    131: 
                    132: /* A function called through the vtable when a particular module
                    133:    should be unloaded.  */
                    134: static int
1.2     ! moko      135: vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
1.1       moko      136: {
                    137:   int errors = 0;
                    138: 
                    139:   if (unload_add_on ((image_id) module) != B_OK)
                    140:     {
                    141:       LT__SETERROR (CANNOT_CLOSE);
                    142:       ++errors;
                    143:     }
                    144: 
                    145:   return errors;
                    146: }
                    147: 
                    148: 
                    149: /* A function called through the vtable to get the address of
                    150:    a symbol loaded from a particular module.  */
                    151: static void *
1.2     ! moko      152: vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
1.1       moko      153: {
                    154:   void *address = 0;
                    155:   image_id image = (image_id) module;
                    156: 
                    157:   if (get_image_symbol (image, name, B_SYMBOL_TYPE_ANY, address) != B_OK)
                    158:     {
                    159:       LT__SETERROR (SYMBOL_NOT_FOUND);
                    160:       address = 0;
                    161:     }
                    162: 
                    163:   return address;
                    164: }

E-mail: