1// -*- mode: c++ -*-
2
3// Copyright (c) 2010 Google Inc.
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
33
34// The DwarfLineToModule class accepts line number information from a
35// DWARF parser and adds it to a google_breakpad::Module. The Module
36// can write that data out as a Breakpad symbol file.
37
38#ifndef COMMON_LINUX_DWARF_LINE_TO_MODULE_H
39#define COMMON_LINUX_DWARF_LINE_TO_MODULE_H
40
41#include <string>
42
43#include "common/module.h"
44#include "common/dwarf/dwarf2reader.h"
45#include "common/using_std_string.h"
46
47namespace google_breakpad {
48
49// A class for producing a vector of google_breakpad::Module::Line
50// instances from parsed DWARF line number data.
51//
52// An instance of this class can be provided as a handler to a
53// LineInfo DWARF line number information parser. The
54// handler accepts source location information from the parser and
55// uses it to produce a vector of google_breakpad::Module::Line
56// objects, referring to google_breakpad::Module::File objects added
57// to a particular google_breakpad::Module.
58//
59// GNU toolchain omitted sections support:
60// ======================================
61//
62// Given the right options, the GNU toolchain will omit unreferenced
63// functions from the final executable. Unfortunately, when it does so, it
64// does not remove the associated portions of the DWARF line number
65// program; instead, it gives the DW_LNE_set_address instructions referring
66// to the now-deleted code addresses of zero. Given this input, the DWARF
67// line parser will call AddLine with a series of lines starting at address
68// zero. For example, here is the output from 'readelf -wl' for a program
69// with four functions, the first three of which have been omitted:
70//
71// Line Number Statements:
72// Extended opcode 2: set Address to 0x0
73// Advance Line by 14 to 15
74// Copy
75// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16
76// Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18
77// Advance PC by 2 to 0xd
78// Extended opcode 1: End of Sequence
79//
80// Extended opcode 2: set Address to 0x0
81// Advance Line by 14 to 15
82// Copy
83// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 16
84// Special opcode 119: advance Address by 8 to 0xb and Line by 2 to 18
85// Advance PC by 2 to 0xd
86// Extended opcode 1: End of Sequence
87//
88// Extended opcode 2: set Address to 0x0
89// Advance Line by 19 to 20
90// Copy
91// Special opcode 48: advance Address by 3 to 0x3 and Line by 1 to 21
92// Special opcode 76: advance Address by 5 to 0x8 and Line by 1 to 22
93// Advance PC by 2 to 0xa
94// Extended opcode 1: End of Sequence
95//
96// Extended opcode 2: set Address to 0x80483a4
97// Advance Line by 23 to 24
98// Copy
99// Special opcode 202: advance Address by 14 to 0x80483b2 and Line by 1 to 25
100// Special opcode 76: advance Address by 5 to 0x80483b7 and Line by 1 to 26
101// Advance PC by 6 to 0x80483bd
102// Extended opcode 1: End of Sequence
103//
104// Instead of collecting runs of lines describing code that is not there,
105// we try to recognize and drop them. Since the linker doesn't explicitly
106// distinguish references to dropped sections from genuine references to
107// code at address zero, we must use a heuristic. We have chosen:
108//
109// - If a line starts at address zero, omit it. (On the platforms
110// breakpad targets, it is extremely unlikely that there will be code
111// at address zero.)
112//
113// - If a line starts immediately after an omitted line, omit it too.
114class DwarfLineToModule: public LineInfoHandler {
115 public:
116 // As the DWARF line info parser passes us line records, add source
117 // files to MODULE, and add all lines to the end of LINES. LINES
118 // need not be empty. If the parser hands us a zero-length line, we
119 // omit it. If the parser hands us a line that extends beyond the
120 // end of the address space, we clip it. It's up to our client to
121 // sort out which lines belong to which functions; we don't add them
122 // to any particular function in MODULE ourselves.
123 DwarfLineToModule(Module* module,
124 const string& compilation_dir,
125 vector<Module::Line>* lines,
126 std::map<uint32_t, Module::File*>* files)
127 : module_(module),
128 compilation_dir_(compilation_dir),
129 lines_(lines),
130 files_(files),
131 highest_file_number_(-1),
132 omitted_line_end_(0),
133 warned_bad_file_number_(false),
134 warned_bad_directory_number_(false) { }
135
136 ~DwarfLineToModule() { }
137
138 void DefineDir(const string& name, uint32_t dir_num);
139 void DefineFile(const string& name, int32_t file_num,
140 uint32_t dir_num, uint64_t mod_time,
141 uint64_t length);
142 void AddLine(uint64_t address, uint64_t length,
143 uint32_t file_num, uint32_t line_num, uint32_t column_num);
144
145 private:
146
147 typedef std::map<uint32_t, string> DirectoryTable;
148 typedef std::map<uint32_t, Module::File*> FileTable;
149
150 // The module we're contributing debugging info to. Owned by our
151 // client.
152 Module *module_;
153
154 // The compilation directory for the current compilation unit whose
155 // lines are being accumulated.
156 string compilation_dir_;
157
158 // The vector of lines we're accumulating. Owned by our client.
159 //
160 // In a Module, as in a breakpad symbol file, lines belong to
161 // specific functions, but DWARF simply assigns lines to addresses;
162 // one must infer the line/function relationship using the
163 // functions' beginning and ending addresses. So we can't add these
164 // to the appropriate function from module_ until we've read the
165 // function info as well. Instead, we accumulate lines here, and let
166 // whoever constructed this sort it all out.
167 vector<Module::Line>* lines_;
168
169 // A table mapping directory numbers to paths.
170 DirectoryTable directories_;
171
172 // A table mapping file numbers to Module::File pointers.
173 FileTable* files_;
174
175 // The highest file number we've seen so far, or -1 if we've seen
176 // none. Used for dynamically defined file numbers.
177 int32_t highest_file_number_;
178
179 // This is the ending address of the last line we omitted, or zero if we
180 // didn't omit the previous line. It is zero before we have received any
181 // AddLine calls.
182 uint64_t omitted_line_end_;
183
184 // True if we've warned about:
185 bool warned_bad_file_number_; // bad file numbers
186 bool warned_bad_directory_number_; // bad directory numbers
187};
188
189} // namespace google_breakpad
190
191#endif // COMMON_LINUX_DWARF_LINE_TO_MODULE_H
192