home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / gcc.info-22 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  44KB  |  788 lines

  1. This is Info file gcc.info, produced by Makeinfo-1.55 from the input
  2. file gcc.texi.
  3.    This file documents the use and the internals of the GNU compiler.
  4.    Published by the Free Software Foundation 675 Massachusetts Avenue
  5. Cambridge, MA 02139 USA
  6.    Copyright (C) 1988, 1989, 1992, 1993, 1994 Free Software Foundation,
  7.    Permission is granted to make and distribute verbatim copies of this
  8. manual provided the copyright notice and this permission notice are
  9. preserved on all copies.
  10.    Permission is granted to copy and distribute modified versions of
  11. this manual under the conditions for verbatim copying, provided also
  12. that the sections entitled "GNU General Public License," "Funding for
  13. Free Software," and "Protect Your Freedom--Fight `Look And Feel'" are
  14. included exactly as in the original, and provided that the entire
  15. resulting derived work is distributed under the terms of a permission
  16. notice identical to this one.
  17.    Permission is granted to copy and distribute translations of this
  18. manual into another language, under the above conditions for modified
  19. versions, except that the sections entitled "GNU General Public
  20. License," "Funding for Free Software," and "Protect Your Freedom--Fight
  21. `Look And Feel'", and this permission notice, may be included in
  22. translations approved by the Free Software Foundation instead of in the
  23. original English.
  24. File: gcc.info,  Node: Initialization,  Next: Macros for Initialization,  Prev: Label Output,  Up: Assembler Format
  25. How Initialization Functions Are Handled
  26. ----------------------------------------
  27.    The compiled code for certain languages includes "constructors"
  28. (also called "initialization routines")--functions to initialize data
  29. in the program when the program is started.  These functions need to be
  30. called before the program is "started"--that is to say, before `main'
  31. is called.
  32.    Compiling some languages generates "destructors" (also called
  33. "termination routines") that should be called when the program
  34. terminates.
  35.    To make the initialization and termination functions work, the
  36. compiler must output something in the assembler code to cause those
  37. functions to be called at the appropriate time.  When you port the
  38. compiler to a new system, you need to specify how to do this.
  39.    There are two major ways that GCC currently supports the execution of
  40. initialization and termination functions.  Each way has two variants.
  41. Much of the structure is common to all four variations.
  42.    The linker must build two lists of these functions--a list of
  43. initialization functions, called `__CTOR_LIST__', and a list of
  44. termination functions, called `__DTOR_LIST__'.
  45.    Each list always begins with an ignored function pointer (which may
  46. hold 0, -1, or a count of the function pointers after it, depending on
  47. the environment).  This is followed by a series of zero or more function
  48. pointers to constructors (or destructors), followed by a function
  49. pointer containing zero.
  50.    Depending on the operating system and its executable file format,
  51. either `crtstuff.c' or `libgcc2.c' traverses these lists at startup
  52. time and exit time.  Constructors are called in forward order of the
  53. list; destructors in reverse order.
  54.    The best way to handle static constructors works only for object file
  55. formats which provide arbitrarily-named sections.  A section is set
  56. aside for a list of constructors, and another for a list of destructors.
  57. Traditionally these are called `.ctors' and `.dtors'.  Each object file
  58. that defines an initialization function also puts a word in the
  59. constructor section to point to that function.  The linker accumulates
  60. all these words into one contiguous `.ctors' section.  Termination
  61. functions are handled similarly.
  62.    To use this method, you need appropriate definitions of the macros
  63. `ASM_OUTPUT_CONSTRUCTOR' and `ASM_OUTPUT_DESTRUCTOR'.  Usually you can
  64. get them by including `svr4.h'.
  65.    When arbitrary sections are available, there are two variants,
  66. depending upon how the code in `crtstuff.c' is called.  On systems that
  67. support an "init" section which is executed at program startup, parts
  68. of `crtstuff.c' are compiled into that section.  The program is linked
  69. by the `gcc' driver like this:
  70.      ld -o OUTPUT_FILE crtbegin.o ... crtend.o -lgcc
  71.    The head of a function (`__do_global_ctors') appears in the init
  72. section of `crtbegin.o'; the remainder of the function appears in the
  73. init section of `crtend.o'.  The linker will pull these two parts of
  74. the section together, making a whole function.  If any of the user's
  75. object files linked into the middle of it contribute code, then that
  76. code will be executed as part of the body of `__do_global_ctors'.
  77.    To use this variant, you must define the `INIT_SECTION_ASM_OP' macro
  78. properly.
  79.    If no init section is available, do not define
  80. `INIT_SECTION_ASM_OP'.  Then `__do_global_ctors' is built into the text
  81. section like all other functions, and resides in `libgcc.a'.  When GCC
  82. compiles any function called `main', it inserts a procedure call to
  83. `__main' as the first executable code after the function prologue.  The
  84. `__main' function, also defined in `libgcc2.c', simply calls
  85. `__do_global_ctors'.
  86.    In file formats that don't support arbitrary sections, there are
  87. again two variants.  In the simplest variant, the GNU linker (GNU `ld')
  88. and an `a.out' format must be used.  In this case,
  89. `ASM_OUTPUT_CONSTRUCTOR' is defined to produce a `.stabs' entry of type
  90. `N_SETT', referencing the name `__CTOR_LIST__', and with the address of
  91. the void function containing the initialization code as its value.  The
  92. GNU linker recognizes this as a request to add the value to a "set";
  93. the values are accumulated, and are eventually placed in the executable
  94. as a vector in the format described above, with a leading (ignored)
  95. count and a trailing zero element.  `ASM_OUTPUT_DESTRUCTOR' is handled
  96. similarly.  Since no init section is available, the absence of
  97. `INIT_SECTION_ASM_OP' causes the compilation of `main' to call `__main'
  98. as above, starting the initialization process.
  99.    The last variant uses neither arbitrary sections nor the GNU linker.
  100. This is preferable when you want to do dynamic linking and when using
  101. file formats which the GNU linker does not support, such as `ECOFF'.  In
  102. this case, `ASM_OUTPUT_CONSTRUCTOR' does not produce an `N_SETT'
  103. symbol; initialization and termination functions are recognized simply
  104. by their names.  This requires an extra program in the linkage step,
  105. called `collect2'.  This program pretends to be the linker, for use
  106. with GNU CC; it does its job by running the ordinary linker, but also
  107. arranges to include the vectors of initialization and termination
  108. functions.  These functions are called via `__main' as described above.
  109.    Choosing among these configuration options has been simplified by a
  110. set of operating-system-dependent files in the `config' subdirectory.
  111. These files define all of the relevant parameters.  Usually it is
  112. sufficient to include one into your specific machine-dependent
  113. configuration file.  These files are:
  114. `aoutos.h'
  115.      For operating systems using the `a.out' format.
  116. `next.h'
  117.      For operating systems using the `MachO' format.
  118. `svr3.h'
  119.      For System V Release 3 and similar systems using `COFF' format.
  120. `svr4.h'
  121.      For System V Release 4 and similar systems using `ELF' format.
  122. `vms.h'
  123.      For the VMS operating system.
  124.    The following section describes the specific macros that control and
  125. customize the handling of initialization and termination functions.
  126. File: gcc.info,  Node: Macros for Initialization,  Next: Instruction Output,  Prev: Initialization,  Up: Assembler Format
  127. Macros Controlling Initialization Routines
  128. ------------------------------------------
  129.    Here are the macros that control how the compiler handles
  130. initialization and termination functions:
  131. `INIT_SECTION_ASM_OP'
  132.      If defined, a C string constant for the assembler operation to
  133.      identify the following data as initialization code.  If not
  134.      defined, GNU CC will assume such a section does not exist.  When
  135.      you are using special sections for initialization and termination
  136.      functions, this macro also controls how `crtstuff.c' and
  137.      `libgcc2.c' arrange to run the initializati