home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / darwin / darwinx86.iso / usr / local / include / dyld / images.h < prev   
Encoding:
C/C++ Source or Header  |  2001-09-14  |  11.6 KB  |  317 lines

  1. /*
  2.  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  3.  *
  4.  * @APPLE_LICENSE_HEADER_START@
  5.  * 
  6.  * Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
  7.  * Reserved.  This file contains Original Code and/or Modifications of
  8.  * Original Code as defined in and that are subject to the Apple Public
  9.  * Source License Version 1.1 (the "License").  You may not use this file
  10.  * except in compliance with the License.  Please obtain a copy of the
  11.  * License at http://www.apple.com/publicsource and read it before using
  12.  * this file.
  13.  * 
  14.  * The Original Code and all software distributed under the License are
  15.  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  16.  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT.  Please see the
  19.  * License for the specific language governing rights and limitations
  20.  * under the License.
  21.  * 
  22.  * @APPLE_LICENSE_HEADER_END@
  23.  */
  24. #import <mach-o/loader.h>
  25. #import <sys/types.h>
  26. #import <sys/stat.h>
  27. #ifdef EGREGIOUS_HACK_FOR_SAMPLER
  28. #import <dyld/bool.h>
  29. #else
  30. #import "stuff/bool.h"
  31. #endif
  32. #import <sys/types.h>
  33.  
  34. enum link_state {
  35.     UNLINKED,        /* the starting point for UNLINKED modules */
  36.     BEING_LINKED,    /* moduled selected to be link into the program */
  37.     RELOCATED,        /* moduled relocated dyld can now use the module */
  38.     REGISTERING,    /* functions registered for modules being link called */
  39.     INITIALIZING,    /* module initializers being called */
  40.     LINKED,        /* module initializers called user can now use module */
  41.     FULLY_LINKED,    /* module fully linked (all lazy symbols resolved) */
  42.  
  43.     PREBOUND_UNLINKED,    /* the module is prebound image but unlinked after */
  44.             /*  the program was launch. */
  45.  
  46.     BEING_UNLINKED,    /* not yet used.  TODO unlinking */
  47.     REPLACED,        /* not yet used.  TODO replacing */
  48.  
  49.     UNUSED        /* a module handle that is now unused */
  50. };
  51.  
  52. struct image {
  53.     char *name;            /* Image name for reporting errors. */
  54.     unsigned long vmaddr_slide; /* The amount the vmaddresses are slid in the */
  55.                 /*  image from the staticly link addresses. */
  56.     struct mach_header *mh;    /* The mach header of the image. */
  57.     unsigned long valid;    /* TRUE if this is struct is valid */
  58.     /*
  59.      * The above four fields can't change without breaking gdb(1) see the
  60.      * comments in <mach-o/dyld_gdb.h>.
  61.      */
  62.     unsigned long vmaddr_size;  /* The size of the vm this image uses */
  63.     unsigned long seg1addr;    /* The address of the first segment */
  64.     unsigned long        /* The address of the first read-write segment*/
  65.     segs_read_write_addr;    /*  used only for MH_SPLIT_SEGS images. */
  66.     struct symtab_command *st;    /* The symbol table command for the image. */
  67.     struct dysymtab_command    /* The dynamic symbol table command for the */
  68.     *dyst;            /*  image. */
  69.     struct segment_command    /* The link edit segment command for the */
  70.     *linkedit_segment;    /*  image. */
  71.     struct routines_command *rc;/* The routines command for the image */
  72.     struct twolevel_hints_command/* The twolevel hints command for the image */
  73.     *hints_cmd;
  74.     struct section *init;    /* The mod init section */
  75.     struct section *term;    /* The mod term section */
  76. #ifdef __ppc__
  77.     unsigned long         /* the image's dyld_stub_binding_helper */
  78.     dyld_stub_binding_helper; /* address */
  79. #endif
  80.     unsigned long
  81.       prebound:1,        /* Link states set from prebound state */
  82.       change_protect_on_reloc:1,/* The image has relocations in read-only */
  83.                 /*  segments and protection needs to change. */
  84.       cache_sync_on_reloc:1,    /* The image has relocations for instructions */
  85.                 /*  and the i cache needs to sync with d cache*/
  86.       registered:1,        /* The functions registered for add images */
  87.                 /*  have been called */
  88.       private:1,        /* global symbols are not used for linking */
  89.       init_bound:1,        /* the image init routine has been bound */
  90.       init_called:1,        /* the image init routine has been called */
  91.       lazy_init:1,        /* the image init routine to be called lazy */
  92.       has_coalesced_sections:1, /* the image has coalesced sections */
  93.       sub_images_setup:1,    /* the sub images have been set up */
  94.       umbrella_images_setup:1,  /* the umbrella_images have been set up */
  95.       two_level_debug_printed:1,/* printed when TWO_LEVEL_DEBUG is on */
  96.       undone_prebound_lazy_pointers:1, /* all prebound lazy pointer un done */
  97.       subtrees_twolevel_prebound_setup:1, /* state of this and deps setup */
  98.       subtrees_twolevel_prebound:1, /* this and deps twolevel and prebound */
  99.       image_can_use_hints:1,    /* set when the hints are usable in this image*/
  100.       subs_can_use_hints:1,    /* set when the hints are usable for images */
  101.                 /*  that have this image as a sub image */
  102.       unused:16;
  103.     /*
  104.      * For two-level namespace images this is the array of pointers to the
  105.      * dependent images and the count of them.
  106.      */
  107.     struct image **dependent_images;
  108.     unsigned long ndependent_images;
  109.     /*
  110.      * If this is a library image which has a framework name or library name
  111.      * then this is the part that would be the umbrella name or library name
  112.      * and the size of the name.  This points into the name and since framework
  113.      * and library names may have suffixes the size is needed to exclude it.
  114.      * This is only needed for two-level namespace images.  umbrella_name and
  115.      * or library_name will be NULL and name_size will be 0 if there is no
  116.      * umbrella name.
  117.      */
  118.     char *umbrella_name;
  119.     char *library_name;
  120.     unsigned long name_size;
  121.  
  122.     /* array of pointers to sub-frameworks and sub-umbrellas and count */
  123.     struct image **sub_images;
  124.     unsigned long nsub_images;
  125.     /* array of pointers to back to umbrella frameworks and sub-umbrellas
  126.      if any which the image is a part of and the count of them. */
  127.     struct image **umbrella_images;
  128.     unsigned long numbrella_images;
  129.  
  130.     /*
  131.      * This fields is only used to point back to the library image or object
  132.      * image structure.  This is so that the for two-level namespace lookups
  133.      * the library_image and object_image can be quickly obtained from the image
  134.      * structure for a calls to lookup_symbol_in_library_image() and
  135.      * lookup_symbol_in_object_image().
  136.      */
  137.     void *outer_image;
  138. };
  139.  
  140. /*
  141.  * This is really an enum link_state.  Originally there was a module structure
  142.  * that had an enum link_state field.  Because the minimum structure aligment 
  143.  * is more than one-byte aligned this wasted space.  Since this is one of the
  144.  * few allocated and written data structures of dyld it is important it is as
  145.  * small as reasonable.  It needs to be addressable so using less than a byte
  146.  * is not acceptable.
  147.  */
  148. typedef char module_state;
  149.  
  150. /*
  151.  * To keep track of which modules are being fully bound the 0x80 bit of the
  152.  * module state is used.  Fully bound is where all of the dependent references
  153.  * are bound into the program.  Where fully linked in here means that the single
  154.  * module has all it's lazy as well as it's not lazy symbols linked.  If an
  155.  * image has an image initialization routine then the module containing that
  156.  * routine is full bound before it is called.
  157.  */
  158. #define GET_LINK_STATE(m) ((enum link_state)((m) & 0xf))
  159. #define SET_LINK_STATE(m,l) m = (((m) & 0xf0) | ((l) & 0xf))
  160. #define GET_FULLYBOUND_STATE(m) ((m) & 0x80)
  161. #define SET_FULLYBOUND_STATE(m) m = ((m) | 0x80)
  162. #define CLEAR_FULLYBOUND_STATE(m) m = ((m) & 0x7f)
  163.  
  164. /*
  165.  * To allow image initialization routines (shared library init routines) to
  166.  * force modules in there image to have there module initialization routines
  167.  * run (C++ initializers, via a call to __initializeCplusplus) the 0x40 bit of
  168.  * the module state is used to keep track of if the module initialization
  169.  * routine as been run.  The module initialization routines are normally run
  170.  * after the the image initialization routines so this bit is needed to make
  171.  * sure a module initialization routine is not run twice.
  172.  */
  173. #define GET_MODINIT_STATE(m) ((m) & 0x40)
  174. #define SET_MODINIT_STATE(m) m = ((m) | 0x40)
  175.  
  176. /*
  177.  * To support module termination routines (to be used for C++ destructors) the
  178.  * 0x20 bit of the module state is used to keep track of if the module
  179.  * termination routine has been run.
  180.  */
  181. #define GET_MODTERM_STATE(m) ((m) & 0x20)
  182. #define SET_MODTERM_STATE(m) m = ((m) | 0x20)
  183.  
  184. /*
  185.  * To support calling image initialization routines (shared library init
  186.  * routines) in their dependency order each module that defines a shared library
  187.  * init routine and its dependents needs to be checked.  As each module is
  188.  * checked it is marked so that is only checked once.
  189.  */
  190. #define GET_IMAGE_INIT_DEPEND_STATE(m) ((m) & 0x10)
  191. #define SET_IMAGE_INIT_DEPEND_STATE(m) m = ((m) | 0x10)
  192.  
  193. struct object_image {
  194.     struct image image;
  195.     module_state module;
  196.     enum bool module_state_saved;
  197.     module_state saved_module_state;
  198. };
  199.  
  200. struct library_image {
  201.     struct image image;
  202.     unsigned long nmodules;
  203.     module_state *modules;
  204.     struct dylib_command *dlid;
  205.     dev_t dev;
  206.     ino_t ino;
  207.     enum bool dependent_libraries_loaded;
  208.     enum bool remove_on_error;
  209.     enum bool module_states_saved;
  210.     module_state *saved_module_states;
  211.     unsigned long library_offset;
  212. };
  213.  
  214. /*
  215.  * Using /System/Library/CoreServices/Desktop.app/Contents/MacOS/Desktop from
  216.  * MacOS X Public Beta (Kodiak1G7)
  217.  * TOTAL number of bundles    4
  218.  * TOTAL number of libraries    58
  219.  */
  220. enum nobject_images { NOBJECT_IMAGES = 5 };
  221. struct object_images {
  222.     struct object_image images[NOBJECT_IMAGES];
  223.     unsigned long nimages;
  224.     struct object_images *next_images;
  225.     /*
  226.      * The above three fields can't change without breaking gdb(1) see the
  227.      * comments in <mach-o/dyld_gdb.h>.
  228.      */
  229. };
  230. extern struct object_images object_images;
  231.  
  232. enum nlibrary_images { NLIBRARY_IMAGES = 60 };
  233. struct library_images {
  234.     struct library_image images[NLIBRARY_IMAGES];
  235.     unsigned long nimages;
  236.     struct library_images *next_images;
  237.     /*
  238.      * The above three fields can't change without breaking gdb(1) see the
  239.      * comments in <mach-o/dyld_gdb.h>.
  240.      */
  241. };
  242. extern struct library_images library_images;
  243.  
  244. extern void (*dyld_monaddition)(char *lowpc, char *highpc);
  245.  
  246. extern void load_executable_image(
  247.     char *name,
  248.     struct mach_header *mh_execute,
  249.     unsigned long *entry_point);
  250.  
  251. extern enum bool load_dependent_libraries(
  252.     void);
  253.  
  254. extern enum bool load_library_image(
  255.     struct dylib_command *dl,
  256.     char *dylib_name,
  257.     enum bool force_searching,
  258.     struct image **image_pointer);
  259.  
  260. extern void unload_remove_on_error_libraries(
  261.     void);
  262.  
  263. extern void clear_remove_on_error_libraries(
  264.     void);
  265.  
  266. extern struct object_image *map_bundle_image(
  267.     char *name,
  268.     char *object_addr,
  269.     unsigned long object_size);
  270.  
  271. extern void unload_bundle_image(
  272.     struct object_image *object_image,
  273.     enum bool keepMemoryMapped,
  274.     enum bool reset_lazy_references);
  275.  
  276. extern void shared_pcsample_buffer(
  277.     char *name,
  278.     struct section *s,
  279.     unsigned long slide_value);
  280.  
  281. extern enum bool set_images_to_prebound(
  282.     void);
  283.  
  284. extern void undo_prebound_images(
  285.     void);
  286.  
  287. extern void find_twolevel_prebound_lib_subtrees(
  288.     void);
  289.  
  290. extern void try_to_use_prebound_libraries(
  291.     void);
  292.  
  293. extern void call_image_init_routines(
  294.     enum bool make_delayed_calls);
  295.  
  296. extern char *executables_name;
  297.  
  298. extern char *save_string(
  299.     char *name);
  300.  
  301. extern void create_executables_path(
  302.     char *exec_path);
  303.  
  304. extern struct object_image *find_object_image(
  305.     struct image *image);
  306.  
  307. extern enum bool is_library_loaded_by_name(
  308.     char *dylib_name,
  309.     struct dylib_command *dl,
  310.     struct image **image_pointer);
  311.  
  312. extern enum bool is_library_loaded_by_stat(
  313.     char *dylib_name,
  314.     struct dylib_command *dl,
  315.     struct stat *stat_buf,
  316.     struct image **image_pointer);
  317.