home *** CD-ROM | disk | FTP | other *** search
- #include <a.out.h>
- #include "assoc.h"
- #include "dyna_link.h
-
-
- lk_get_header(fd, hdrp, offset) /@ reads an a.out header into memory @/
- int fd; /@ an open file-descriptor @/
- struct exec *hdrp; /@ pointer to memory for a.out header @/
- long offset; /@ displacement of file (for archives)@/
-
- lk_get_symbols(fd, hdrp, offset) /@ Reads symbols into memory @/
- int fd; /@ an open file-descriptor @/
- struct exec *hdrp; /@ pointer to previously initialized header @/
- long offset; /@ displacement of file (for archives) @/
-
- assoc_mem
- lk_sym_hash(hdrp, syms); /@ Creates lookup-table for symbols @/
- struct exec *hdrp; /@ pointer to previously initialized header @/
- struct nlist *syms; /@ pointer to previously initialized symbols @/
-
- func_ptr
- lk_dynaload(codep, hdrp, hash_table, filename) /@ loads named file @/
- int* codep; /@ pointer to memory for return-code @/
- struct exec* hdrp; /@ pointer to previously initialized header @/
- assoc_mem hash_table; /@ previously initialized lookup-table @/
- char* filename;
-
- func_ptr /@ loads a file, given by file-descriptor and offset. @/
- lk_dynaload_fd(codep, hdrp, hash_tab, fd, offset)
- int *codep; /@ pointer to memory for return-code @/
- struct exec *hdrp; /@ pointer to previously initialized header @/
- assoc_mem hash_tab; /@ previously initialized lookup-table @/
- int fd; /@ an open file-descriptor @/
- long disp; /@ displacement of file (for archives) @/
-
- This library furnishes routines for doing a dynamic load of an executable
- segment ( .o file).
-
- The caller first must build a lookup-table for the symbols
- of the executing program. (See assoc.doc for description
- of lookup-table routines.)
-
- Once the lookup-table has been made, the program may repeatedly call
- lk_dynaload() or lk_dynaload_fd() to load segments.
- Loaded segments may be freed using free().
-
- The routines to be used for building the lookup-table are
- lk_get_header(), lk_get_symbols(), and lk_sym_hash(). These are
- also potentially useful for other link-editor applications, and
- for that reason are parameterized so that they may be used on
- archive members, as well as on individual a.out files.
-
- lk_get_header() returns 0 on success, -1 on failure. Sets errno.
-
- lk_get_symbols() returns a buffer from malloc() on success, null
- on failure. Sets errno.
-
- lk_sym_hash() returns an assoc_mem on success (see assoc.doc), null
- on failure. Sets errno.
-
- lk_dynaload() and lk_dynaload_fd() return a pointer to the entry-point
- of the .o file on success, null on failure. Sets *codep (see
- parameters). The values for *codep are defined in dyna_link.h:
-
- #define lk_ok 0
- #define lk_undefd 1
- #define lk_bad_format 2
- #define lk_perror -1
-
- lk_ok means "okay."
- lk_undefd means that one or more symbols required by the .o file
- are not defined in the lookup-table. In this case the char
- lk_undefined will have been set to point to a vector containing
- the undefined symbols.
- lk_bad_format means that the .o file is not formatted occording
- to the a.out file conventions.
- lk_perror means that errno has been set.
-
-
- Tutorial example:
-
- main(argc, argv)
- char argv;
- {
- char* me = (char*) (argv[0]);
- char* prog = (char*)(argv[1]);
- func_ptr entry_point;
- assoc_mem hash_tab;
- struct exec main_hdr;
- struct nlist * main_syms;
- int code;
- int fd = open( me, O_RDONLY );
-
- lk_get_header(fd, &main_hdr, 0 );
- main_syms = lk_get_symbols(fd, &main_hdr, 0 );
- close(fd);
- hash_tab = lk_sym_hash(&main_hdr, main_syms);
-
- (func_ptr) entry_point
- = lk_dynaload( &code, &main_hdr, hash_tab, prog );
- if(entry_point)
- (*(func_ptr) entry_point)();
- else
- switch(code)
- {
- case lk_undefd:
- { char undef = lk_undefined;
- printf("undefined:\n");
- while(*undef) printf("%s\n", *undef++);
- }
- break;
- case lk_perror:
- perror(prog);
- break;
- case lk_bad_format:
- printf("bad format\n");
- break;
- }
- }
- }
- exit(0);
- }
-
- */
-