home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / info / dld.info < prev    next >
Encoding:
GNU Info File  |  1996-10-15  |  20.6 KB  |  565 lines

  1. This is Info file dld.info, produced by Makeinfo-1.64 from the input
  2. file dld.texinfo.
  3.  
  4.    This file documents dld, a dynamic link/unlink editor.
  5.  
  6.    This is edition 1.3 of the dld documentation.
  7.  
  8.    Copyright (C) 1991 W. Wilson Ho.  Copyright (C) 1995 A. Jaffer.
  9.  
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.  
  14.    Permission is granted to copy and distribute modified versions of
  15. this manual under the conditions for verbatim copying, provided that
  16. the entire resulting derived work is distributed under the terms of a
  17. permission notice identical to this one.
  18.  
  19. 
  20. File: dld.info,  Node: Top,  Next: dld_init,  Prev: (dir),  Up: (dir)
  21.  
  22. What is dld?
  23. ************
  24.  
  25.    "Dld" is a library package of C functions that performs "dynamic
  26. link editing".  Programs that use dld can add compiled object code to
  27. or remove such code from a process anytime during its execution.
  28. Loading modules, searching libraries, resolving external references, and
  29. allocating storage for global and static data structures are all
  30. performed at run time.
  31.  
  32.    Dld is now available for VAX, Sun 3, SPARCstation, Sequent Symmetry,
  33. Atari ST, and Linux.
  34.  
  35.    This text describes how the dld functions can be called and some
  36. technical details that should be aware of.  For the internals of dld and
  37. sample applications, please refer to `An Approach to Genuine Dynamic
  38. Linking', Software-Practice and Experirnce, Vol. 21(4), 375-390 (April
  39. 1991).  An early draft of that paper is included with the original
  40. distribution (prep.ai.mit.edu:pub/gnu/dld-3.2.3.tar.gz).
  41.  
  42. * Menu:
  43.  
  44. * dld_init::            Initializing Dld
  45. * dld_link::            Dynamically Linking in New Modules
  46. * dld_unlink::            Unlinking a Module
  47. * dld_get_func::        Invoking Dynamically Linked Functions
  48. * Executability::        Determining If a Function is Executable
  49. * dld_list_undefined_sym::    Listing the Undefined Symbols
  50. * dld_create_reference::    Explicitly Referencing a Symbol
  51. * Explicit Definition::        Explicitly Defining a Symbol
  52. * C++ support::
  53. * dld_perror::            Printing out the Error Messages
  54. * Error Codes::            Definition of Error Codes
  55.  
  56. 
  57. File: dld.info,  Node: dld_init,  Next: dld_link,  Prev: Top,  Up: Top
  58.  
  59. Initializing Dld
  60. ================
  61.  
  62.    To use any of the dld functions, you must include the header file
  63. `dld.h' for declaration of the functions and definition of the error
  64. code constants.
  65.  
  66.    The function `dld_init' must be called before any other dld
  67. functions.
  68.  
  69.  - Function: int dld_init (char *PATH)
  70.      where PATH is a string containing the path name of the executable
  71.      file of the executing process.
  72.  
  73.      This function initializes internal data structures of dld and
  74.      loads into memory symbol definitions of the executing process.  By
  75.      doing so, other dynamically loaded functions can reference symbols
  76.      already defined or share functions already exist in the executing
  77.      process.
  78.  
  79.      `dld_init' returns 0 when successful; otherwise, it returns an
  80.      error code that is non-zero (*note Definition of Error Codes:
  81.      Error Codes.).
  82.  
  83. Locating the Executable File
  84. ----------------------------
  85.  
  86.    The path name of the executing process as required by `dld_init'
  87. might not be easily obtained all the time.  Not all systems pass the
  88. entire path name of the executable file as the first argument
  89. (`argv[0]') to `main'.  In order to obtain the full path of the
  90. executable file, the `dld_find_executable' function can be used.
  91.  
  92.  - Function: char *dld_find_executable (char *COMMAND)
  93.      `dld_find_executable' returns the absolute path name of the file
  94.      that would be executed if COMMAND were given as a command.  It
  95.      looks up the environment variable PATH, searches in each of the
  96.      directory listed for COMMAND, and returns the absolute path name
  97.      for the first occurrence.  Thus, it is advisable to invoke
  98.      `dld_init' as:
  99.  
  100.           main (int argc, char **argv)
  101.           {
  102.               ...
  103.               if (dld_init (dld_find_executable (argv[0]))) {
  104.                   ...
  105.               }
  106.               ...
  107.           }
  108.  
  109.           *Note:* If the current process is executed using the `execve'
  110.           call without passing the correct path name as argument 0,
  111.           `dld_find_executable (argv[0]) ' will also fail to locate the
  112.           executable file.
  113.  
  114.      `dld_find_executable' returns zero if `command' is not found in
  115.      any of the directories listed in `PATH'.
  116.  
  117. 
  118. File: dld.info,  Node: dld_link,  Next: dld_unlink,  Prev: dld_init,  Up: Top
  119.  
  120. Dynamically Linking in New Modules
  121. ==================================
  122.  
  123.    The function `dld_link' dynamically links in the named relocatable
  124. object or library file into memory.
  125.  
  126.  - Function: int dld_link (char *FILENAME)
  127.      where FILENAME is the path name of the file to be linked.
  128.      Specifically, if the named file is a relocatable object file, it is
  129.      completely loaded into memory.  If it is a library file, only those
  130.      modules defining an unresolved external reference are loaded.
  131.      Since a module in the library may itself reference other routines
  132.      in the library, loading it may generate more unresolved external
  133.      references.  Therefore, a library file is searched repeatedly
  134.      until a scan through all library members is made without having to
  135.      load any new modules.
  136.  
  137.      Storage for the text and data of the dynamically linked modules is
  138.      allocated using `malloc'.  In other words, they are kept in the
  139.      *heap* of the executing process.
  140.  
  141.      After all modules are loaded, `dld_link' resolves as many external
  142.      references as possible.  Note that some symbols might still be
  143.      undefined at this stage, because the modules defining them have
  144.      not yet been loaded.
  145.  
  146.      If the specified module is linked successfully, `dld_link' returns
  147.      0; otherwise, it returns a non-zero error code (*note Definition
  148.      of Error Codes: Error Codes.).
  149.  
  150. 
  151. File: dld.info,  Node: dld_unlink,  Next: dld_get_func,  Prev: dld_link,  Up: Top
  152.  
  153. Unlinking a Module
  154. ==================
  155.  
  156.    The major difference between dld and other dynamic linker is that
  157. dld allows object modules to be removed from the process anytime during
  158. execution.  Unlinking a module is simply the reverse of the link
  159. operation (*note Important Points in Using Unlink: dld_unlink.).  The
  160. specified module is removed and the memory allocated to it is
  161. reclaimed.  Additionally, resolution of external references must be
  162. undone.
  163.  
  164.    There are two unlink functions:
  165.  
  166.  - Function: int dld_unlink_by_file (char *PATH, int HARD)
  167.  - Function: int dld_unlink_by_symbol (char *ID, int HARD)
  168.      The two unlink functions are basically the same except that
  169.      `dld_unlink_by_file' takes as argument the path name (PATH) of a
  170.      file corresponding to a module previously linked in by `dld_link',
  171.      but `dld_unlink_by_symbol' unlinks the module that defines the
  172.      specified symbol (ID).
  173.  
  174.      Both functions take a second argument HARD.  When HARD is non-zero
  175.      ("hard unlink"), the specified module is removed from memory
  176.      unconditionally.  On the other hand, if HARD is zero ("soft
  177.      unlink"), this module is removed from memory only if it is not
  178.      referenced by any other modules.  Furthermore, if unlinking a
  179.      module results in leaving some other modules being unreferenced,
  180.      these unreferenced modules are also removed.
  181.  
  182.      Hard unlink is usually used when you want to explicitly remove a
  183.      module and probably replace it by a different module with the same
  184.      name.  For example, you may want to replace the system's `printf'
  185.      by your own version.  When you link in your version of `printf',
  186.      dld will automatically redirect all references to `printf' to the
  187.      new version.
  188.  
  189.      Soft unlink should be used when you are not sure if the specified
  190.      module is still needed.  If you just want to clean up unnecessary
  191.      functions, it is always safe to use soft unlink.
  192.  
  193.      Both unlink functions returns 0 if the specified object file or
  194.      symbol is previously loaded.  Otherwise, they return a non-zero
  195.      error code (*note Definition of Error Codes: Error Codes.).
  196.  
  197. Important Points in Using Unlink
  198. --------------------------------
  199.  
  200.    When a module is being unlinked, dld tries to clean up as much as it
  201. can to restore the executing process to a state as if this module has
  202. never been linked.  This clean up includes removing and reclaiming the
  203. memory for storing the text and data segment of the module, and
  204. "un-defining" any global symbols defined by this module.
  205.  
  206.    However, side effects--such as modification of global variables,
  207. input/output operations, and allocations of new memory blocks--caused
  208. by the execution of any function in this module are not reversed.  Thus,
  209. it is the responsibility of the programmer to explicitly carry out all
  210. necessary clean up operations before unlinking a module.
  211.  
  212. 
  213. File: dld.info,  Node: dld_get_func,  Next: Executability,  Prev: dld_unlink,  Up: Top
  214.  
  215. Invoking Dynamically Linked Functions
  216. =====================================
  217.  
  218.    Dynamically linked functions may still be invoked from modules (e.g.,
  219. `main') that do not contain references to such functions.
  220.  
  221.  - Function: unsigned long dld_get_symbol (char *ID)
  222.      Returns the entry point of the function named ID if found, 0 if
  223.      not found.  Non-zero returned values can be used as pointers to the
  224.      functions.
  225.  
  226.  - Function: unsigned long dld_get_func (char *FUNC)
  227.      Returns the address of the global variable named FUNC if found, 0
  228.      if not found.
  229.  
  230.      A typical use of `dld_get_func' would be:
  231.  
  232.           {
  233.               void (*func) ();
  234.               int error_code;
  235.           
  236.               ...
  237.           
  238.               /* First, link in the object file "my_object_file.o".  Proceed
  239.                  only if the link operation is successful, i.e. it returns 0.
  240.                  "my_new_func" is a function defined in "my_object_file.o".
  241.                  Set func to point at the entry point of this function and
  242.                  then invoke it indirectly through func. */
  243.           
  244.               if ((error_code = dld_link ("my_object_file.o")) == 0) {
  245.                   if ((func = (void (*) ()) get_func ("my_new_func")) != 0)
  246.                       (*func) ();
  247.                   ...
  248.               } else {
  249.           
  250.               ...
  251.               }
  252.           }
  253.  
  254. 
  255. File: dld.info,  Node: Executability,  Next: dld_list_undefined_sym,  Prev: dld_get_func,  Up: Top
  256.  
  257. Determining If a Function is Executable
  258. =======================================
  259.  
  260.    Since dld allows modules to be added to or removed from an executing
  261. process dynamically, some global symbols may not be defined.  As a
  262. result, an invocation of a function might reference an undefined symbol.
  263. We say that a function is "executable" if and only if all its external
  264. references have been fully resolved and all functions that it might
  265. call are executable.
  266.  
  267.  - Function: int dld_function_executable_p (char *FUNC)
  268.      The predicate function `dld_function_executable_p' helps solve this
  269.      problem by tracing the cross references between modules and returns
  270.      non-zero only if the named function is executable.
  271.  
  272.      Note that the implementation of `dld_function_executable_p' is not
  273.      complete according to the (recursive) definition of executability.
  274.      External references through pointers are not traced.  That is,
  275.      `dld_function_executable_p' will still return non-zero if the named
  276.      function uses a pointer to indirectly call another function which
  277.      has already been unlinked.  Furthermore, if one external reference
  278.      of a object module is unresolved, all functions defined in this
  279.      module are considered unexecutable.  Therefore,
  280.      `dld_function_executable_p' is usually too conservative.
  281.  
  282.      However, it is advisable to use `dld_function_executable_p' to
  283.      check if a function is executable before its invocation.  In such a
  284.      dynamic environment where object modules are being added and
  285.      removed, a function that is executable at one point in time might
  286.      not be executable at another.  Under most circumstances,
  287.      `dld_function_executable_p' is accurate.  Also, the implementation
  288.      of this function has been optimized and it is relatively cheap to
  289.      use.
  290.  
  291. 
  292. File: dld.info,  Node: dld_list_undefined_sym,  Next: dld_create_reference,  Prev: Executability,  Up: Top
  293.  
  294. Listing the Undefined Symbols
  295. =============================
  296.  
  297.  - Function: char **dld_list_undefined_sym ()
  298.      The function `dld_list_undefined_sym' returns an array of undefined
  299.      global symbol names.
  300.  
  301.      The list returned contains all the symbols that have been
  302.      referenced by some modules but have not been defined.  This
  303.      function is designed for debugging, especially in the case when a
  304.      function is found to be not executable but you do not know what
  305.      the missing symbols are.
  306.  
  307.      The length of the array is given by the global variable
  308.      `dld_undefined_sym_count', which always holds the current total
  309.      number of undefined global symbols.  Note that all C symbols are
  310.      listed in their internal representation--i.e., they are prefixed
  311.      by the underscore character `_'.
  312.  
  313.      Storage for the array returned is allocated by `malloc'.  It is the
  314.      programmer's responsibility to release this storage by `free' when
  315.      it is not needed anymore.
  316.  
  317. 
  318. File: dld.info,  Node: dld_create_reference,  Next: Explicit Definition,  Prev: dld_list_undefined_sym,  Up: Top
  319.  
  320. Explicitly Referencing a Symbol
  321. ===============================
  322.  
  323.    Normally, a library module is loaded only when it defines one of more
  324. symbols that has been referenced.  To force a library routine to be
  325. loaded, one need to explicitly create a reference to a symbol defined by
  326. that library routine.  The function `dld_create_reference' is designed
  327. for this purpose:
  328.  
  329.  - Function: int dld_create_reference (char *NAME)
  330.      Usually NAME is the name of the library routine that should be
  331.      loaded, but it can be any symbol defined by that routine.  After
  332.      such a reference has been created, linking the appropriate library
  333.      by `dld_link' would cause the required library routine to be
  334.      loaded.
  335.  
  336.      If the call is successful, `dld_create_reference' returns 0;
  337.      otherwise, it returns a non-zero error code (*note Definition of
  338.      Error Codes: Error Codes.).
  339.  
  340.      The library routine loaded by this method can be unlinked by
  341.      `dld_unlink_by_symbol (NAME)'.  Once it has been unlinked, the
  342.      corresponding reference created by `dld_create_reference' is also
  343.      removed so that this routine will not be loaded in again by
  344.      subsequent linking of the library.
  345.  
  346. 
  347. File: dld.info,  Node: Explicit Definition,  Next: C++ support,  Prev: dld_create_reference,  Up: Top
  348.  
  349. Explicitly Defining a Symbol
  350. ============================
  351.  
  352.    Dld allows a programmer to explicitly define global symbols.  That
  353. is, a programmer can force a symbol to have storage assigned for it.
  354. This is especially useful in incremental program testing where the
  355. function being tested needs to access some global variables which are
  356. defined by another function not yet linked in (or even not yet
  357. written).  There are two functions related to explicit definition:
  358.  
  359.  - Function: int dld_define_sym (char *NAME, unsigned int SIZE)
  360.      `dld_define_sym' forces dld to allocate SIZE bytes for symbol
  361.      NAME.  It can be called before or after a reference to NAME is
  362.      made.  If references to NAME already exist when it is defined, all
  363.      such references are directed to point to the correct address
  364.      allocated for NAME.
  365.  
  366.      `dld_define_sym' returns 0 if successful.  Otherwise, it returns a
  367.      non-zero error code (*note Definition of Error Codes: Error
  368.      Codes.).  The typical error is a multiple definition of NAME.
  369.  
  370.  - Function: void dld_remove_defined_symbol (char *NAME)
  371.      When the definition of NAME is no longer needed, it can be removed
  372.      by `dld_remove_define_symbol'.
  373.  
  374. 
  375. File: dld.info,  Node: C++ support,  Next: dld_perror,  Prev: Explicit Definition,  Up: Top
  376.  
  377. C++ support
  378. ===========
  379.  
  380.    From: Thomas Hiller, Hiller@tu-harburg.d400.de
  381.  
  382.    The original DLD supports only C function linking. When using C++
  383. there is a problem with the global constructors and destructors. The
  384. new DLD version contains code which mimics the original "collect2"
  385. supplied by gcc (*note ??: (gcc)Top.).  The way GNU `ld' (*note ??:
  386. (binutils)Top.) does this is better (more direct).  But I'm not
  387. familiar with the internals of GNU `ld' and DLD.
  388.  
  389.    The file `gxxload.cc' contains two C functions (`dyn_load' and
  390. `dyn_unload'), which function as `dld_load' and `dld_unload', but they
  391. take account that the C++ objects or libraries may contain global
  392. constructors and destructors.
  393.  
  394.  - Function: void dyn_load (char *NAME)
  395.      Behaves like `dld_link(name)' but constructors are called before
  396.      control returns to the program.
  397.  
  398.  - Function: void dyn_unload (char *NAME)
  399.      Behaves like `dld_unlink' but the destructors are called before the
  400.      unlink takes place.
  401.  
  402.  - Global Array: char* dyn_libraries
  403.      The caller needs to define the null terminated array of strings
  404.      `dyn_libraries'.  The strings in the array `dyn_libraries', give
  405.      the pathnames of the files which should be used to resolve link
  406.      references after a call is made to `dyn_load'.  As in the example
  407.      below, `dyn_libraries' might contain:
  408.         foo "/usr/lib/libg++.a"
  409.  
  410.         foo "/usr/lib/libm.a"
  411.  
  412.         foo "/usr/lib/libc.a"
  413.  
  414.    A small demonstration program is included in the `test'
  415. sub-directory's files `gxxtest.cc' and `sub.cc'.  When invoked,
  416. `gxxtest' should print (1)  :
  417.  
  418.      DLD function:
  419.      dyn_load:
  420.      Hello, this is A
  421.      dyn_unload:
  422.      Hello, this was A
  423.      ---
  424.  
  425.    The constructor (which prints the first `Hello' message) is called
  426. by `dyn_load'.  `dyn_unload' calls the destructor (second `Hello'
  427. message).  The final `---' message demonstrates that the destructor is
  428. called by `dyn_unload' and not the `exit()' function.
  429.  
  430. sub.cc
  431. ------
  432.  
  433.      #include <iostream.h>
  434.      
  435.      class A {
  436.      public:
  437.          A() { cout<<"Hello, this is A"<<endl; }
  438.          ~A() { cout<<"Hello, this was A"<<endl; }
  439.      };
  440.      
  441.      
  442.      A a;
  443.  
  444. gxxtest.cc
  445. ----------
  446.  
  447.      #include <iostream.h>
  448.      
  449.      #include <dld.h>
  450.      
  451.      char* dyn_libraries[] = {
  452.      "/usr/lib/libg++.a",    /* link in libg++ */
  453.      "/usr/lib/libm.a",    /* link in libm */
  454.      "/usr/lib/libc.a",    /* link in libc */
  455.      LIBGCC,            /* link in libgcc */
  456.      0
  457.      };
  458.      
  459.      main()
  460.      {
  461.          cout<<"DLD function: "<<endl;
  462.          dld_link("sub.o");
  463.          dld_unlink_by_file("sub.o",0);
  464.          cout<<"dyn_load: "<<endl;
  465.          dyn_load("sub.o");
  466.          cout<<"dyn_unload: "<<endl;
  467.          dyn_unload("sub.o");
  468.          cout<<"---"<<endl;
  469.      }
  470.  
  471.    ---------- Footnotes ----------
  472.  
  473.    (1)  Running this example on my machine does not print the `Hello'
  474. lines.  But I don't know enough C++ to fix it.
  475.  
  476. 
  477. File: dld.info,  Node: dld_perror,  Next: Error Codes,  Prev: C++ support,  Up: Top
  478.  
  479. Printing out the Error Messages
  480. ===============================
  481.  
  482.  - Function: void dld_perror (char *USER_MESG)
  483.      where USER_MESG is a user-supplied string prepended to the error
  484.      message.  The function `dld_perror' prints out a short message
  485.      explaining the error returns by the last dld functions.
  486.  
  487.  - Function: char * dld_strerror (int CODE)
  488.      The function `dld_strerror' returns the error message string
  489.      corresponding to the given error code (from `dld_errno').
  490.  
  491. 
  492. File: dld.info,  Node: Error Codes,  Prev: dld_perror,  Up: Top
  493.  
  494. Definition of Error Codes
  495. =========================
  496.  
  497.    The dld functions return a non-zero error code when they fail.  The
  498. definitions of these error codes are:
  499.  
  500.     `DLD_ENOFILE'
  501.           cannot open file.
  502.  
  503.     `DLD_EBADMAGIC'
  504.           bad magic number.
  505.  
  506.     `DLD_EBADHEADER'
  507.           failure reading header.
  508.  
  509.     `DLD_ENOTEXT'
  510.           premature eof in text section.
  511.  
  512.     `DLD_ENOSYMBOLS'
  513.           premature eof in symbols.
  514.  
  515.     `DLD_ENOSTRINGS'
  516.           bad string table.
  517.  
  518.     `DLD_ENOTXTRELOC'
  519.           premature eof in text relocation.
  520.  
  521.     `DLD_ENODATA'
  522.           premature eof in data section.
  523.  
  524.     `DLD_ENODATRELOC'
  525.           premature eof in data relocation.
  526.  
  527.     `DLD_EMULTDEFS'
  528.           multiple definitions of symbol.
  529.  
  530.     `DLD_EBADLIBRARY'
  531.           malformed library archive.
  532.  
  533.     `DLD_EBADCOMMON'
  534.           common block not supported.
  535.  
  536.     `DLD_EBADOBJECT'
  537.           malformed input file (not object file or archive).
  538.  
  539.     `DLD_EBADRELOC'
  540.           bad relocation info.
  541.  
  542.     `DLD_ENOMEMORY'
  543.           virtual memory exhausted.
  544.  
  545.     `DLD_EUNDEFSYM'
  546.           undefined symbol.
  547.  
  548.  
  549. 
  550. Tag Table:
  551. Node: Top681
  552. Node: dld_init2166
  553. Node: dld_link4466
  554. Node: dld_unlink5947
  555. Node: dld_get_func8919
  556. Node: Executability10396
  557. Node: dld_list_undefined_sym12318
  558. Node: dld_create_reference13420
  559. Node: Explicit Definition14727
  560. Node: C++ support16032
  561. Node: dld_perror19017
  562. Node: Error Codes19590
  563. 
  564. End Tag Table
  565.