Stab-Section-Basics.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1992-2019 Free Software Foundation, Inc.
  4. Contributed by Cygnus Support. Written by Julia Menapace, Jim Kingdon,
  5. and David MacKenzie.
  6. Permission is granted to copy, distribute and/or modify this document
  7. under the terms of the GNU Free Documentation License, Version 1.3 or
  8. any later version published by the Free Software Foundation; with no
  9. Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
  10. Texts. A copy of the license is included in the section entitled "GNU
  11. Free Documentation License". -->
  12. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  13. <head>
  14. <title>STABS: Stab Section Basics</title>
  15. <meta name="description" content="STABS: Stab Section Basics">
  16. <meta name="keywords" content="STABS: Stab Section Basics">
  17. <meta name="resource-type" content="document">
  18. <meta name="distribution" content="global">
  19. <meta name="Generator" content="makeinfo">
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  21. <link href="index.html#Top" rel="start" title="Top">
  22. <link href="Symbol-Types-Index.html#Symbol-Types-Index" rel="index" title="Symbol Types Index">
  23. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  24. <link href="Stab-Sections.html#Stab-Sections" rel="up" title="Stab Sections">
  25. <link href="ELF-Linker-Relocation.html#ELF-Linker-Relocation" rel="next" title="ELF Linker Relocation">
  26. <link href="Stab-Sections.html#Stab-Sections" rel="prev" title="Stab Sections">
  27. <style type="text/css">
  28. <!--
  29. a.summary-letter {text-decoration: none}
  30. blockquote.smallquotation {font-size: smaller}
  31. div.display {margin-left: 3.2em}
  32. div.example {margin-left: 3.2em}
  33. div.indentedblock {margin-left: 3.2em}
  34. div.lisp {margin-left: 3.2em}
  35. div.smalldisplay {margin-left: 3.2em}
  36. div.smallexample {margin-left: 3.2em}
  37. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  38. div.smalllisp {margin-left: 3.2em}
  39. kbd {font-style:oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. pre.smalldisplay {font-family: inherit; font-size: smaller}
  45. pre.smallexample {font-size: smaller}
  46. pre.smallformat {font-family: inherit; font-size: smaller}
  47. pre.smalllisp {font-size: smaller}
  48. span.nocodebreak {white-space:nowrap}
  49. span.nolinebreak {white-space:nowrap}
  50. span.roman {font-family:serif; 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" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  57. <a name="Stab-Section-Basics"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="ELF-Linker-Relocation.html#ELF-Linker-Relocation" accesskey="n" rel="next">ELF Linker Relocation</a>, Up: <a href="Stab-Sections.html#Stab-Sections" accesskey="u" rel="up">Stab Sections</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Types-Index.html#Symbol-Types-Index" title="Index" rel="index">Index</a>]</p>
  61. </div>
  62. <hr>
  63. <a name="How-to-Embed-Stabs-in-Sections"></a>
  64. <h3 class="appendixsec">F.1 How to Embed Stabs in Sections</h3>
  65. <p>The assembler creates two custom sections, a section named <code>.stab</code>
  66. which contains an array of fixed length structures, one struct per stab,
  67. and a section named <code>.stabstr</code> containing all the variable length
  68. strings that are referenced by stabs in the <code>.stab</code> section. The
  69. byte order of the stabs binary data depends on the object file format.
  70. For ELF, it matches the byte order of the ELF file itself, as determined
  71. from the <code>EI_DATA</code> field in the <code>e_ident</code> member of the ELF
  72. header. For SOM, it is always big-endian (is this true??? FIXME). For
  73. COFF, it matches the byte order of the COFF headers. The meaning of the
  74. fields is the same as for a.out (see <a href="Symbol-Table-Format.html#Symbol-Table-Format">Symbol Table Format</a>), except
  75. that the <code>n_strx</code> field is relative to the strings for the current
  76. compilation unit (which can be found using the synthetic N_UNDF stab
  77. described below), rather than the entire string table.
  78. </p>
  79. <p>The first stab in the <code>.stab</code> section for each compilation unit is
  80. synthetic, generated entirely by the assembler, with no corresponding
  81. <code>.stab</code> directive as input to the assembler. This stab contains
  82. the following fields:
  83. </p>
  84. <dl compact="compact">
  85. <dt><code>n_strx</code></dt>
  86. <dd><p>Offset in the <code>.stabstr</code> section to the source filename.
  87. </p>
  88. </dd>
  89. <dt><code>n_type</code></dt>
  90. <dd><p><code>N_UNDF</code>.
  91. </p>
  92. </dd>
  93. <dt><code>n_other</code></dt>
  94. <dd><p>Unused field, always zero.
  95. This may eventually be used to hold overflows from the count in
  96. the <code>n_desc</code> field.
  97. </p>
  98. </dd>
  99. <dt><code>n_desc</code></dt>
  100. <dd><p>Count of upcoming symbols, i.e., the number of remaining stabs for this
  101. source file.
  102. </p>
  103. </dd>
  104. <dt><code>n_value</code></dt>
  105. <dd><p>Size of the string table fragment associated with this source file, in
  106. bytes.
  107. </p></dd>
  108. </dl>
  109. <p>The <code>.stabstr</code> section always starts with a null byte (so that string
  110. offsets of zero reference a null string), followed by random length strings,
  111. each of which is null byte terminated.
  112. </p>
  113. <p>The ELF section header for the <code>.stab</code> section has its
  114. <code>sh_link</code> member set to the section number of the <code>.stabstr</code>
  115. section, and the <code>.stabstr</code> section has its ELF section
  116. header <code>sh_type</code> member set to <code>SHT_STRTAB</code> to mark it as a
  117. string table. SOM and COFF have no way of linking the sections together
  118. or marking them as string tables.
  119. </p>
  120. <p>For COFF, the <code>.stab</code> and <code>.stabstr</code> sections may be simply
  121. concatenated by the linker. GDB then uses the <code>n_desc</code> fields to
  122. figure out the extent of the original sections. Similarly, the
  123. <code>n_value</code> fields of the header symbols are added together in order
  124. to get the actual position of the strings in a desired <code>.stabstr</code>
  125. section. Although this design obviates any need for the linker to
  126. relocate or otherwise manipulate <code>.stab</code> and <code>.stabstr</code>
  127. sections, it also requires some care to ensure that the offsets are
  128. calculated correctly. For instance, if the linker were to pad in
  129. between the <code>.stabstr</code> sections before concatenating, then the
  130. offsets to strings in the middle of the executable&rsquo;s <code>.stabstr</code>
  131. section would be wrong.
  132. </p>
  133. <p>The GNU linker is able to optimize stabs information by merging
  134. duplicate strings and removing duplicate header file information
  135. (see <a href="Include-Files.html#Include-Files">Include Files</a>). When some versions of the GNU linker optimize
  136. stabs in sections, they remove the leading <code>N_UNDF</code> symbol and
  137. arranges for all the <code>n_strx</code> fields to be relative to the start of
  138. the <code>.stabstr</code> section.
  139. </p>
  140. <hr>
  141. <div class="header">
  142. <p>
  143. Next: <a href="ELF-Linker-Relocation.html#ELF-Linker-Relocation" accesskey="n" rel="next">ELF Linker Relocation</a>, Up: <a href="Stab-Sections.html#Stab-Sections" accesskey="u" rel="up">Stab Sections</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Types-Index.html#Symbol-Types-Index" title="Index" rel="index">Index</a>]</p>
  144. </div>
  145. </body>
  146. </html>