1
// Copyright (c) 2010 Google Inc. All Rights Reserved.
3
// Redistribution and use in source and binary forms, with or without
4
// modification, are permitted provided that the following conditions are
7
// * Redistributions of source code must retain the above copyright
8
// notice, this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above
10
// copyright notice, this list of conditions and the following disclaimer
11
// in the documentation and/or other materials provided with the
13
// * Neither the name of Google Inc. nor the names of its
14
// contributors may be used to endorse or promote products derived from
15
// this software without specific prior written permission.
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31
// This file implements the google_breakpad::StabsReader class.
32
// See stabs_reader.h.
34
#include "common/stabs_reader.h"
42
namespace google_breakpad {
44
StabsReader::EntryIterator::EntryIterator(const ByteBuffer *buffer,
45
bool big_endian, size_t value_size)
46
: value_size_(value_size), cursor_(buffer, big_endian) {
47
// Actually, we could handle weird sizes just fine, but they're
48
// probably mistakes --- expressed in bits, say.
49
assert(value_size == 4 || value_size == 8);
54
void StabsReader::EntryIterator::Fetch() {
56
.Read(4, false, &entry_.name_offset)
57
.Read(1, false, &entry_.type)
58
.Read(1, false, &entry_.other)
59
.Read(2, false, &entry_.descriptor)
60
.Read(value_size_, false, &entry_.value);
61
entry_.at_end = !cursor_;
64
StabsReader::StabsReader(const uint8_t *stab, size_t stab_size,
65
const uint8_t *stabstr, size_t stabstr_size,
66
bool big_endian, size_t value_size, bool unitized,
67
StabsHandler *handler)
68
: entries_(stab, stab_size),
69
strings_(stabstr, stabstr_size),
70
iterator_(&entries_, big_endian, value_size),
74
next_cu_string_offset_(0),
75
current_source_file_(NULL) { }
77
const char *StabsReader::SymbolString() {
78
ptrdiff_t offset = string_offset_ + iterator_->name_offset;
79
if (offset < 0 || (size_t) offset >= strings_.Size()) {
80
handler_->Warning("symbol %d: name offset outside the string section\n",
82
// Return our null string, to keep our promise about all names being
83
// taken from the string section.
86
return reinterpret_cast<const char *>(strings_.start + offset);
89
bool StabsReader::Process() {
90
while (!iterator_->at_end) {
91
if (iterator_->type == N_SO) {
92
if (! ProcessCompilationUnit())
94
} else if (iterator_->type == N_UNDF && unitized_) {
95
// In unitized STABS (including Linux STABS, and pretty much anything
96
// else that puts STABS data in sections), at the head of each
97
// compilation unit's entries there is an N_UNDF stab giving the
98
// number of symbols in the compilation unit, and the number of bytes
99
// that compilation unit's strings take up in the .stabstr section.
100
// Each CU's strings are separate; the n_strx values are offsets
101
// within the current CU's portion of the .stabstr section.
103
// As an optimization, the GNU linker combines all the
104
// compilation units into one, with a single N_UNDF at the
105
// beginning. However, other linkers, like Gold, do not perform
106
// this optimization.
107
string_offset_ = next_cu_string_offset_;
108
next_cu_string_offset_ = iterator_->value;
111
#if defined(HAVE_MACH_O_NLIST_H)
112
// Export symbols in Mach-O binaries look like this.
113
// This is necessary in order to be able to dump symbols
114
// from OS X system libraries.
115
else if ((iterator_->type & N_STAB) == 0 &&
116
(iterator_->type & N_TYPE) == N_SECT) {
127
bool StabsReader::ProcessCompilationUnit() {
128
assert(!iterator_->at_end && iterator_->type == N_SO);
130
// There may be an N_SO entry whose name ends with a slash,
131
// indicating the directory in which the compilation occurred.
132
// The build directory defaults to NULL.
133
const char *build_directory = NULL;
135
const char *name = SymbolString();
136
if (name[0] && name[strlen(name) - 1] == '/') {
137
build_directory = name;
142
// We expect to see an N_SO entry with a filename next, indicating
143
// the start of the compilation unit.
145
if (iterator_->at_end || iterator_->type != N_SO)
147
const char *name = SymbolString();
148
if (name[0] == '\0') {
149
// This seems to be a stray end-of-compilation-unit marker;
150
// consume it, but don't report the end, since we didn't see a
155
current_source_file_ = name;
158
if (! handler_->StartCompilationUnit(current_source_file_,
165
// The STABS documentation says that some compilers may emit
166
// additional N_SO entries with names immediately following the
167
// first, and that they should be ignored. However, the original
168
// Breakpad STABS reader doesn't ignore them, so we won't either.
170
// Process the body of the compilation unit, up to the next N_SO.
171
while (!iterator_->at_end && iterator_->type != N_SO) {
172
if (iterator_->type == N_FUN) {
173
if (! ProcessFunction())
175
} else if (iterator_->type == N_SLINE) {
176
// Mac OS X STABS place SLINE records before functions.
178
// The value of an N_SLINE entry that appears outside a function is
179
// the absolute address of the line.
180
line.address = iterator_->value;
181
line.filename = current_source_file_;
182
// The n_desc of a N_SLINE entry is the line number. It's a
183
// signed 16-bit field; line numbers from 32768 to 65535 are
184
// stored as n-65536.
185
line.number = (uint16_t) iterator_->descriptor;
186
queued_lines_.push_back(line);
188
} else if (iterator_->type == N_SOL) {
189
current_source_file_ = SymbolString();
192
// Ignore anything else.
197
// An N_SO with an empty name indicates the end of the compilation
198
// unit. Default to zero.
199
uint64_t ending_address = 0;
200
if (!iterator_->at_end) {
201
assert(iterator_->type == N_SO);
202
const char *name = SymbolString();
203
if (name[0] == '\0') {
204
ending_address = iterator_->value;
209
if (! handler_->EndCompilationUnit(ending_address))
212
queued_lines_.clear();
217
bool StabsReader::ProcessFunction() {
218
assert(!iterator_->at_end && iterator_->type == N_FUN);
220
uint64_t function_address = iterator_->value;
221
// The STABS string for an N_FUN entry is the name of the function,
222
// followed by a colon, followed by type information for the
223
// function. We want to pass the name alone to StartFunction.
224
const char *stab_string = SymbolString();
225
const char *name_end = strchr(stab_string, ':');
227
name_end = stab_string + strlen(stab_string);
228
std::string name(stab_string, name_end - stab_string);
229
if (! handler_->StartFunction(name, function_address))
233
// If there were any SLINE records given before the function, report them now.
234
for (vector<Line>::const_iterator it = queued_lines_.begin();
235
it != queued_lines_.end(); it++) {
236
if (!handler_->Line(it->address, it->filename, it->number))
239
queued_lines_.clear();
241
while (!iterator_->at_end) {
242
if (iterator_->type == N_SO || iterator_->type == N_FUN)
244
else if (iterator_->type == N_SLINE) {
245
// The value of an N_SLINE entry is the offset of the line from
246
// the function's start address.
247
uint64_t line_address = function_address + iterator_->value;
248
// The n_desc of a N_SLINE entry is the line number. It's a
249
// signed 16-bit field; line numbers from 32768 to 65535 are
250
// stored as n-65536.
251
uint16_t line_number = iterator_->descriptor;
252
if (! handler_->Line(line_address, current_source_file_, line_number))
255
} else if (iterator_->type == N_SOL) {
256
current_source_file_ = SymbolString();
259
// Ignore anything else.
263
// We've reached the end of the function. See if we can figure out its
265
uint64_t ending_address = 0;
266
if (!iterator_->at_end) {
267
assert(iterator_->type == N_SO || iterator_->type == N_FUN);
268
if (iterator_->type == N_FUN) {
269
const char *symbol_name = SymbolString();
270
if (symbol_name[0] == '\0') {
271
// An N_FUN entry with no name is a terminator for this function;
272
// its value is the function's size.
273
ending_address = function_address + iterator_->value;
276
// An N_FUN entry with a name is the next function, and we can take
277
// its value as our ending address. Don't advance the iterator, as
278
// we'll use this symbol to start the next function as well.
279
ending_address = iterator_->value;
282
// An N_SO entry could be an end-of-compilation-unit marker, or the
283
// start of the next compilation unit, but in either case, its value
284
// is our ending address. We don't advance the iterator;
285
// ProcessCompilationUnit will decide what to do with this symbol.
286
ending_address = iterator_->value;
290
if (! handler_->EndFunction(ending_address))
296
bool StabsReader::ProcessExtern() {
297
#if defined(HAVE_MACH_O_NLIST_H)
298
assert(!iterator_->at_end &&
299
(iterator_->type & N_STAB) == 0 &&
300
(iterator_->type & N_TYPE) == N_SECT);
303
// TODO(mark): only do symbols in the text section?
304
if (!handler_->Extern(SymbolString(), iterator_->value))
311
} // namespace google_breakpad