Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:12

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 /**
0029  * @file
0030  *
0031  * @ingroup rtems_rtl
0032  *
0033  * @brief RTEMS Run-Time Linker
0034  *
0035  * This is the POSIX interface to run-time loading of code into RTEMS.
0036  */
0037 
0038 #if !defined (_RTEMS_RTL_H_)
0039 #define _RTEMS_RTL_H_
0040 
0041 #include <link.h>
0042 #include <rtems/chain.h>
0043 #include <rtems/thread.h>
0044 
0045 #include <rtems/rtl/rtl-allocator.h>
0046 #include <rtems/rtl/rtl-archive.h>
0047 #include <rtems/rtl/rtl-fwd.h>
0048 #include <rtems/rtl/rtl-obj.h>
0049 #include <rtems/rtl/rtl-obj-cache.h>
0050 #include <rtems/rtl/rtl-obj-comp.h>
0051 #include <rtems/rtl/rtl-sym.h>
0052 #include <rtems/rtl/rtl-unresolved.h>
0053 
0054 #ifdef __cplusplus
0055 extern "C" {
0056 #endif /* __cplusplus */
0057 
0058 /**
0059  * @defgroup rtems_rtl RTEMS Runtime Link Editor
0060  *
0061  * @ingroup RTEMSAPI
0062  *
0063  * The module implements a runtime link editor with the standard dlopen, and
0064  * dlclose family of functions.
0065  *
0066  * The runtime link editor is different to that found on Unix type systems. The
0067  * object modules are compiled for PIC or position independent code and
0068  * therefore require relocating when loaded.
0069  *
0070  * The object file format is currently ELF and object files can be separate
0071  * files or in an archive. Object files in an archive are referenced by
0072  * specifying 'archive:object' format. For example 'libfoo.a:bar.o'.
0073  */
0074 
0075 /**
0076  * Macros to glue two tokens.
0077  */
0078 #ifdef __STDC__
0079 #define RTL_XGLUE(a,b) a##b
0080 #else
0081 #define RTL_XGLUE(a,b) a/**/b
0082 #endif
0083 
0084 #define RTL_GLUE(a,b) RTL_XGLUE(a,b)
0085 
0086 /**
0087  * The number of buckets in the global symbol table.
0088  */
0089 #define RTEMS_RTL_SYMS_GLOBAL_BUCKETS (32)
0090 
0091 /**
0092  * The number of relocation record per block in the unresolved table.
0093  */
0094 #define RTEMS_RTL_UNRESOLVED_BLOCK_SIZE (256)
0095 
0096 /**
0097  * The number of dependency record per block in the dependency table.
0098  */
0099 #define RTEMS_RTL_DEPENDENCY_BLOCK_SIZE (16)
0100 
0101 /**
0102  * The global debugger interface variable.
0103  */
0104 extern struct r_debug _rtld_debug;
0105 
0106 /**
0107  * Debugger break function. Call when debugging to have it read the _rtld_debug
0108  * variable.
0109  */
0110 void _rtld_debug_state (void);
0111 
0112 /**
0113  * The type of constructor/destructor function.
0114  */
0115 typedef void (*rtems_rtl_cdtor)(void);
0116 
0117 /**
0118  * The global RTL data. This structure is allocated on the heap when the first
0119  * call to the RTL is made and never released.
0120  *
0121  * The global symbol table is a hash table held in this structure and the
0122  * actual symbols are part of the object's structure. If this is a problem we
0123  * could look at a hash table per object file.
0124  */
0125 struct rtems_rtl_data
0126 {
0127   rtems_recursive_mutex lock;           /**< The RTL lock */
0128   rtems_rtl_alloc_data  allocator;      /**< The allocator data. */
0129   rtems_chain_control   objects;        /**< List if loaded object files. */
0130   rtems_chain_control   pending;        /**< Listof object files needing work. */
0131   const char*           paths;          /**< Search paths for archives. */
0132   rtems_rtl_symbols     globals;        /**< Global symbol table. */
0133   rtems_rtl_archives    archives;       /**< Archive search and loader. */
0134   rtems_rtl_unresolved  unresolved;     /**< Unresolved symbols. */
0135   rtems_rtl_obj*        base;           /**< Base object file. */
0136   rtems_rtl_obj_cache   symbols;        /**< Symbols object file cache. */
0137   rtems_rtl_obj_cache   strings;        /**< Strings object file cache. */
0138   rtems_rtl_obj_cache   relocs;         /**< Relocations object file cache. */
0139   rtems_rtl_obj_comp    decomp;         /**< The decompression compressor. */
0140   int                   last_errno;     /**< Last error number. */
0141   char                  last_error[64]; /**< Last error string. */
0142 };
0143 
0144 /**
0145  * Get the RTL data with out locking. This call assumes the RTL is locked.
0146  *
0147  * @return rtems_rtl_data* The RTL data after being locked.
0148  * @retval NULL The RTL data is not initialised.
0149  */
0150 rtems_rtl_data* rtems_rtl_data_unprotected (void);
0151 
0152 /**
0153  * Get the RTL global symbol table with out locking. This call assumes
0154  * the RTL is locked.
0155  *
0156  * @return rtems_rtl_symbols* The RTL global symbols after being locked.
0157  * @retval NULL The RTL data is not initialised.
0158  */
0159 rtems_rtl_symbols* rtems_rtl_global_symbols (void);
0160 
0161 /**
0162  * Get the RTL last error string with out locking. This call assumes the RTL is
0163  * locked.
0164  *
0165  * @return const char* The RTL's laste error.
0166  * @retval NULL The RTL data is not initialised.
0167  */
0168 const char* rtems_rtl_last_error_unprotected (void);
0169 
0170 /**
0171  * Get the RTL objects table with out locking. This call assumes the RTL
0172  * is locked.
0173  *
0174  * @return rtems_chain_control* The RTL objects chain.
0175  * @retval NULL The RTL data is not initialised.
0176  */
0177 rtems_chain_control* rtems_rtl_objects_unprotected (void);
0178 
0179 /**
0180  * Get the RTL pending with out locking. This call assumes the RTL is locked.
0181  *
0182  * @return rtems_chain_control* The RTL pending list control.
0183  * @retval NULL The RTL data is not initialised.
0184  */
0185 rtems_chain_control* rtems_rtl_pending_unprotected (void);
0186 
0187 /**
0188  * Get the RTL unresolved table with out locking. This call assumes the RTL
0189  * is locked.
0190  *
0191  * @return rtems_rtl_unresolv* The RTL unresolved symbols and reloc records.
0192  * @retval NULL The RTL data is not initialised.
0193  */
0194 rtems_rtl_unresolved* rtems_rtl_unresolved_unprotected (void);
0195 
0196 /**
0197  * Get the RTL archives with out locking. This call assumes the RTL is locked.
0198  *
0199  * @return rtems_rtl_archives* The RTL acrhives.
0200  * @retval NULL The RTL data is not initialised.
0201  */
0202 rtems_rtl_archives* rtems_rtl_archives_unprotected (void);
0203 
0204 /**
0205  * Get the RTL symbols, strings, or relocations object file caches. This call
0206  * assmes the RTL is locked.
0207  *
0208  * @param symbols Pointer to the location to set the cache into. Returns NULL
0209  *                is rtl is not initialised. If NULL is passed in no value set.
0210  * @param strings Pointer to the location to set the cache into. Returns NULL
0211  *                is rtl is not initialised. If NULL is passed in no value set.
0212  * @param relocs Pointer to the location to set the cache into. Returns NULL
0213  *               is rtl is not initialised. If NULL is passed in no value set.
0214  */
0215 void rtems_rtl_obj_caches (rtems_rtl_obj_cache** symbols,
0216                            rtems_rtl_obj_cache** strings,
0217                            rtems_rtl_obj_cache** relocs);
0218 
0219 /**
0220  * Flush all the object file caches.
0221  */
0222 void rtems_rtl_obj_caches_flush (void);
0223 
0224 /**
0225  * Get the RTL decompressor setting for the cache and the offset in the file
0226  * the compressed stream starts. This call assumes the RTL is locked.
0227  *
0228  * @param decomp Pointer to the location to set the compressor into. Returns
0229  *               NULL is rtl is not initialised.
0230  * @param cache The cache to read the file with. Saves needing an extrs buffer.
0231  * @param offset The offset in the file the compressed stream starts.
0232  */
0233 void rtems_rtl_obj_decompress (rtems_rtl_obj_comp** decomp,
0234                                rtems_rtl_obj_cache* cache,
0235                                int                  fd,
0236                                int                  compression,
0237                                off_t                offset);
0238 
0239 /**
0240  * Update the mask in the object files. You can clear flags and then set
0241  * flags. A zero (0) does not clear or set the flags. This is global to all
0242  * object files that are laoded.
0243  *
0244  * @param clear The flag's clear mask, a 0 does not clear any flags.
0245  * @param set The flag's set mask, a 0 does not set any flags.
0246  */
0247 void rtems_rtl_obj_update_flags (uint32_t clear, uint32_t set);
0248 
0249 /**
0250  * Lock the Run-time Linker.
0251  *
0252  * @return rtems_rtl_data* The RTL data after being locked.
0253  * @retval NULL The RTL data could not be initialised or locked. Typically this
0254  *              means the lock could not be created.
0255  */
0256 rtems_rtl_data* rtems_rtl_lock (void);
0257 
0258 /**
0259  * Unlock the Run-time Linker.
0260  */
0261 void rtems_rtl_unlock (void);
0262 
0263 /**
0264  * Check a pointer is a valid object file descriptor returning the pointer as
0265  * that type.
0266  *
0267  * Assumes the RTL has been locked.
0268  *
0269  * @param handle Pointer to the object file to be validated.
0270  * @return rtl_obj* The object file descriptor. NULL is returned if invalid.
0271  */
0272 rtems_rtl_obj* rtems_rtl_check_handle (void* handle);
0273 
0274 /**
0275  * Find the object given a file name.
0276  *
0277  * @param name The name of the object file.
0278  * @retval NULL No object file with that name found.
0279  * @return rtems_rtl_obj* The object file descriptor.
0280  */
0281 rtems_rtl_obj* rtems_rtl_find_obj (const char* name);
0282 
0283 /**
0284  * Find the object file a symbol is exported from.
0285  *
0286  * @param sym The symbol to search with.
0287  * @retval NULL No object file found.
0288  * @return rtems_rtl_obj* Reference to the symbol.
0289  */
0290 rtems_rtl_obj* rtems_rtl_find_obj_with_symbol (const rtems_rtl_obj_sym* sym);
0291 
0292 /**
0293  * Load an object file into memory relocating it. It will not be resolved
0294  * against other symbols in other object files or the base image.
0295  *
0296  * The name can be a file name for an object file or it can be encoded to
0297  * reference an archive of object modules (static library). This encoding is
0298  * specific to RTEMS and allows dependences to specify an archive without the
0299  * searching overhead normally incurred by linkers locating object files in an
0300  * archive. The file name format rules are:
0301  *
0302  *  1. Absolute file references a specific object file in the architecture
0303  *     specific location on the file system.
0304  *
0305  *  2. Relative file references an object format file in the search path.
0306  *
0307  *  3. Absolute archive and file reference to a specific location in the file
0308  *     system. The archive and file are encoded as 'archive:file [@@offset]'
0309  *     where 'archive' is a valid file at the absolute path in the file system,
0310  *     and 'file' is a contained in the archive, and optionally an offset to
0311  *     the 'file' in the 'archive'. If no offset is provided the archive is
0312  *     searched.
0313  *
0314  *  4. Relative archive and file in the search path. The encoding is the same
0315  *     as described in item 3 of this list.
0316  *
0317  * Assumes the RTL has been locked.
0318  *
0319  * @param name The name of the object file.
0320  * @param mode The mode of the load as defined by the dlopen call.
0321  * @return rtl_obj* The object file descriptor. NULL is returned if the load fails.
0322  */
0323 rtems_rtl_obj* rtems_rtl_load_object (const char* name, int mode);
0324 
0325 /**
0326  * Unload an object file. This only happens when the user count is 0.
0327  *
0328  * Assumes the RTL has been locked.
0329  *
0330  * @param obj The object file descriptor.
0331  * @retval true The object file has been unloaded.
0332  * @retval false The object file could not be unloaded.
0333  */
0334 bool rtems_rtl_unload_object (rtems_rtl_obj* obj);
0335 
0336 /**
0337  * Load an object file.  This is the user accessable interface to unloading an
0338  * object file. See @rtems_rtl_load_object.
0339  *
0340  * @param name The name of the object file.
0341  * @param mode The mode of the load as defined by the dlopen call.
0342  * @return rtl_obj* The object file descriptor. NULL is returned if the load fails.
0343  */
0344 rtems_rtl_obj* rtems_rtl_load (const char* name, int mode);
0345 
0346 /**
0347  * Unload an object file. This is the user accessable interface to unloading an
0348  * object file. See @rtems_rtl_unload_object.
0349  *
0350  * Assumes the RTL has been locked.
0351  *
0352  * @param obj The object file descriptor.
0353  * @retval true The object file has been unloaded.
0354  * @retval false The object file could not be unloaded.
0355  */
0356 bool rtems_rtl_unload (rtems_rtl_obj* obj);
0357 
0358 /**
0359  * Get the last error message clearing it. This operation locks the run time
0360  * linker and therefore keeps the RTL thread safe but this call is not thread
0361  * safe is multiple threads are loading object files at the same time. This
0362  * call is provided to map to the dlopen family of calls.
0363  *
0364  * @param message Pointer to a buffer to copy the message into.
0365  * @param max_message The maximum message that can be copied.
0366  * @return int The last error number.
0367  */
0368 int rtems_rtl_get_error (char* message, size_t max_message);
0369 
0370 /**
0371  * Append the path to the search path.
0372  *
0373  * @param path The path to append.
0374  * @retval false The path could not be appended.
0375  * @retval true The path was appended.
0376  */
0377 bool rtems_rtl_path_append (const char* path);
0378 
0379 /**
0380  * Prepend the path to the search path.
0381  *
0382  * @param path The path to prepend.
0383  * @retval false The path could not be prepended.
0384  * @retval true The path was prepended.
0385  */
0386 
0387 bool rtems_rtl_path_prepend (const char* path);
0388 
0389 /**
0390  * Add an exported symbol table to the global symbol table. This call is
0391  * normally used by an object file when loaded that contains a global symbol
0392  * table.
0393  *
0394  * @param esyms The exported symbol table.
0395  * @param count The size of the exported symbol table.
0396  * @param tls_offsets The TLS offsets table. If NULL none provided.
0397  * @param tls_size The number TLS offset entries in the table.
0398  */
0399 void rtems_rtl_base_sym_global_add (const unsigned char*        esyms,
0400                                     unsigned int                count,
0401                                     const rtems_rtl_tls_offset* tls_offsets,
0402                                     unsigned int                tls_size);
0403 
0404 /**
0405  * Return the object file descriptor for the base image. The object file
0406  * descriptor returned is created when the run time linker is initialised.
0407  *
0408  * Assumes the RTL has been locked.
0409  *
0410  * @return rtl_obj* The object file descriptor for the base image. NULL is
0411  *                  returned if the load fails.
0412  */
0413 rtems_rtl_obj* rtems_rtl_baseimage (void);
0414 
0415 #ifdef __cplusplus
0416 }
0417 #endif /* __cplusplus */
0418 
0419 #endif