You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

How-Overlays-Work.html 9.6KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Free Software" and "Free Software Needs
  8. Free Documentation", with the Front-Cover Texts being "A GNU Manual,"
  9. and with the Back-Cover Texts as in (a) below.
  10. (a) The FSF's Back-Cover Text is: "You are free to copy and modify
  11. this GNU Manual. Buying copies from GNU Press supports the FSF in
  12. developing GNU and promoting software freedom." -->
  13. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>How Overlays Work (Debugging with GDB)</title>
  17. <meta name="description" content="How Overlays Work (Debugging with GDB)">
  18. <meta name="keywords" content="How Overlays Work (Debugging with GDB)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Overlays.html#Overlays" rel="up" title="Overlays">
  26. <link href="Overlay-Commands.html#Overlay-Commands" rel="next" title="Overlay Commands">
  27. <link href="Overlays.html#Overlays" rel="prev" title="Overlays">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style: oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="How-Overlays-Work"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Overlay-Commands.html#Overlay-Commands" accesskey="n" rel="next">Overlay Commands</a>, Up: <a href="Overlays.html#Overlays" accesskey="u" rel="up">Overlays</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="How-Overlays-Work-1"></a>
  64. <h3 class="section">14.1 How Overlays Work</h3>
  65. <a name="index-mapped-overlays"></a>
  66. <a name="index-unmapped-overlays"></a>
  67. <a name="index-load-address_002c-overlay_0027s"></a>
  68. <a name="index-mapped-address"></a>
  69. <a name="index-overlay-area"></a>
  70. <p>Suppose you have a computer whose instruction address space is only 64
  71. kilobytes long, but which has much more memory which can be accessed by
  72. other means: special instructions, segment registers, or memory
  73. management hardware, for example. Suppose further that you want to
  74. adapt a program which is larger than 64 kilobytes to run on this system.
  75. </p>
  76. <p>One solution is to identify modules of your program which are relatively
  77. independent, and need not call each other directly; call these modules
  78. <em>overlays</em>. Separate the overlays from the main program, and place
  79. their machine code in the larger memory. Place your main program in
  80. instruction memory, but leave at least enough space there to hold the
  81. largest overlay as well.
  82. </p>
  83. <p>Now, to call a function located in an overlay, you must first copy that
  84. overlay&rsquo;s machine code from the large memory into the space set aside
  85. for it in the instruction memory, and then jump to its entry point
  86. there.
  87. </p>
  88. <div class="smallexample">
  89. <pre class="smallexample"> Data Instruction Larger
  90. Address Space Address Space Address Space
  91. +-----------+ +-----------+ +-----------+
  92. | | | | | |
  93. +-----------+ +-----------+ +-----------+&lt;-- overlay 1
  94. | program | | main | .----| overlay 1 | load address
  95. | variables | | program | | +-----------+
  96. | and heap | | | | | |
  97. +-----------+ | | | +-----------+&lt;-- overlay 2
  98. | | +-----------+ | | | load address
  99. +-----------+ | | | .-| overlay 2 |
  100. | | | | | |
  101. mapped ---&gt;+-----------+ | | +-----------+
  102. address | | | | | |
  103. | overlay | &lt;-' | | |
  104. | area | &lt;---' +-----------+&lt;-- overlay 3
  105. | | &lt;---. | | load address
  106. +-----------+ `--| overlay 3 |
  107. | | | |
  108. +-----------+ | |
  109. +-----------+
  110. | |
  111. +-----------+
  112. <a name="A-code-overlay"></a>A code overlay
  113. </pre></div>
  114. <p>The diagram (see <a href="#A-code-overlay">A code overlay</a>) shows a system with separate data
  115. and instruction address spaces. To map an overlay, the program copies
  116. its code from the larger address space to the instruction address space.
  117. Since the overlays shown here all use the same mapped address, only one
  118. may be mapped at a time. For a system with a single address space for
  119. data and instructions, the diagram would be similar, except that the
  120. program variables and heap would share an address space with the main
  121. program and the overlay area.
  122. </p>
  123. <p>An overlay loaded into instruction memory and ready for use is called a
  124. <em>mapped</em> overlay; its <em>mapped address</em> is its address in the
  125. instruction memory. An overlay not present (or only partially present)
  126. in instruction memory is called <em>unmapped</em>; its <em>load address</em>
  127. is its address in the larger memory. The mapped address is also called
  128. the <em>virtual memory address</em>, or <em>VMA</em>; the load address is also
  129. called the <em>load memory address</em>, or <em>LMA</em>.
  130. </p>
  131. <p>Unfortunately, overlays are not a completely transparent way to adapt a
  132. program to limited instruction memory. They introduce a new set of
  133. global constraints you must keep in mind as you design your program:
  134. </p>
  135. <ul>
  136. <li> Before calling or returning to a function in an overlay, your program
  137. must make sure that overlay is actually mapped. Otherwise, the call or
  138. return will transfer control to the right address, but in the wrong
  139. overlay, and your program will probably crash.
  140. </li><li> If the process of mapping an overlay is expensive on your system, you
  141. will need to choose your overlays carefully to minimize their effect on
  142. your program&rsquo;s performance.
  143. </li><li> The executable file you load onto your system must contain each
  144. overlay&rsquo;s instructions, appearing at the overlay&rsquo;s load address, not its
  145. mapped address. However, each overlay&rsquo;s instructions must be relocated
  146. and its symbols defined as if the overlay were at its mapped address.
  147. You can use GNU linker scripts to specify different load and relocation
  148. addresses for pieces of your program; see <a href="http://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description">Overlay Description</a> in <cite>Using ld: the GNU linker</cite>.
  149. </li><li> The procedure for loading executable files onto your system must be able
  150. to load their contents into the larger address space as well as the
  151. instruction and data spaces.
  152. </li></ul>
  153. <p>The overlay system described above is rather simple, and could be
  154. improved in many ways:
  155. </p>
  156. <ul>
  157. <li> If your system has suitable bank switch registers or memory management
  158. hardware, you could use those facilities to make an overlay&rsquo;s load area
  159. contents simply appear at their mapped address in instruction space.
  160. This would probably be faster than copying the overlay to its mapped
  161. area in the usual way.
  162. </li><li> If your overlays are small enough, you could set aside more than one
  163. overlay area, and have more than one overlay mapped at a time.
  164. </li><li> You can use overlays to manage data, as well as instructions. In
  165. general, data overlays are even less transparent to your design than
  166. code overlays: whereas code overlays only require care when you call or
  167. return to functions, data overlays require care every time you access
  168. the data. Also, if you change the contents of a data overlay, you
  169. must copy its contents back out to its load address before you can copy a
  170. different data overlay into the same mapped area.
  171. </li></ul>
  172. <hr>
  173. <div class="header">
  174. <p>
  175. Next: <a href="Overlay-Commands.html#Overlay-Commands" accesskey="n" rel="next">Overlay Commands</a>, Up: <a href="Overlays.html#Overlays" accesskey="u" rel="up">Overlays</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
  176. </div>
  177. </body>
  178. </html>