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