home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / wizards / 5267 < prev    next >
Encoding:
Text File  |  1992-12-18  |  3.8 KB  |  83 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!newsflash.concordia.ca!sifon!thunder.mcrcim.mcgill.edu!mouse
  3. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  4. Subject: Re: What is text, data & bss anyway?
  5. Message-ID: <1992Dec18.012116.14772@thunder.mcrcim.mcgill.edu>
  6. Organization: McGill Research Centre for Intelligent Machines
  7. References: <1992Dec14.234221.6644@saifr00.cfsat.honeywell.com>
  8. Date: Fri, 18 Dec 92 01:21:16 GMT
  9. Lines: 73
  10.  
  11. In article <1992Dec14.234221.6644@saifr00.cfsat.honeywell.com>, klaus@saifr00.cfsat.honeywell.com (Todd Klaus) writes:
  12.  
  13. > My collegues and I are experiencing some confusion as to the exact
  14. > definition of the text, data and bss segments of a executable image
  15. > on disk and how this changes when it is loaded into core memory.
  16.  
  17. (If we're going to be talking about "exact definitions", then perhaps
  18. "core memory" is not the best term to use :-)
  19.  
  20. > My understanding (simplistic) is that the text segment is executable
  21. > instructions, data is initialized data, and bss is uinitialized data.
  22.  
  23. This is roughly correct; however, the the bss segment is, in most
  24. machines now filled with all 0 bits rather than unspecified contents.
  25. Also, the text segment contains things other than executable
  26. instructions.  Some compilers put string literals there, and anything
  27. read-only can go there, though on most systems there is no way to put
  28. anything but code (and sometimes, string literals) there from C.
  29.  
  30. When an executable file is run, text and data segments are set up.  The
  31. text segment from the file becomes the text segment in-core; the
  32. in-core data segment is the data+bss from the file, with bss cleared to
  33. zeroes.  A third segment exists at run-time, the stack segment, which
  34. is set up by the system, typically containing the environment and argv
  35. strings and an initial stack frame or two to call main().
  36.  
  37. > Could someone please show me a code fragment showing what types of
  38. > variables would fall into each category?
  39.  
  40. Variables do not normally go into the text segment.  All the discussion
  41. below refers to the following:
  42.  
  43.     int foo = 5;
  44.     static int bar;
  45.     static float blee;
  46.     static char *bloop;
  47.     int baz;
  48.  
  49. Normally, foo will go in the data segment and bar in bss.  (I assume,
  50. of course, the absence of a later "static int bar = 12;", which would
  51. make the compiler put bar into the data segment.)
  52.  
  53. blee and bloop can go in bss if, and only if, all-0-bits is
  54. floating-point zero (for blee) or nil-pointer-to-char (for bloop).  (Of
  55. course, even if this isn't the case, the compiler is at liberty to put
  56. them in bss anyway, provided it arranges for them to be set to the
  57. correct initial values before they can first be accessed.  This is true
  58. of any variable that would normally go in data.)
  59.  
  60. baz is a more interesting case.  Most systems use one of two models for
  61. such variables: the def/ref model and the common model.  (Either one is
  62. permitted by ANSI C.)  In the def/ref model, exactly one such
  63. definition must appear in the program for baz; all other references
  64. must declare it as extern.  In the common model, multiple files may
  65. declare baz as above, and at most one of them may declare it with an
  66. initial value (such as was done for foo).  Additional files may declare
  67. it with extern.  A common-model linker is then responsible for
  68. determining whether there is an initialized instance of baz, and if so,
  69. effectively turning all the other references into externs; if not, it
  70. will allocate space in bss for it.
  71.  
  72. > As an aside, does copy-on-write have anything to do with this scheme?
  73.  
  74. Only as a plausible method of implementation.  The run-time text and
  75. data might, for example, be set up by mapping the text and data from
  76. the file into memory, with the text read-only and the data c-o-w.  (The
  77. bss would normally be set up as demand-zero pages, so that real memory
  78. isn't allocated until it's needed, for faster startup.)
  79.  
  80.                     der Mouse
  81.  
  82.                 mouse@larry.mcrcim.mcgill.edu
  83.