~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to thirdparty/breakpad/common/stabs_reader.cc

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010 Google Inc. All Rights Reserved.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without
 
4
// modification, are permitted provided that the following conditions are
 
5
// met:
 
6
//
 
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
 
12
// distribution.
 
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.
 
16
//
 
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.
 
28
 
 
29
// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
 
30
 
 
31
// This file implements the google_breakpad::StabsReader class.
 
32
// See stabs_reader.h.
 
33
 
 
34
#include "common/stabs_reader.h"
 
35
 
 
36
#include <assert.h>
 
37
#include <stab.h>
 
38
#include <string.h>
 
39
 
 
40
using std::vector;
 
41
 
 
42
namespace google_breakpad {
 
43
 
 
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);
 
50
  entry_.index = 0;
 
51
  Fetch();
 
52
}
 
53
 
 
54
void StabsReader::EntryIterator::Fetch() {
 
55
  cursor_
 
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_;
 
62
}
 
63
 
 
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),
 
71
      unitized_(unitized),
 
72
      handler_(handler),
 
73
      string_offset_(0),
 
74
      next_cu_string_offset_(0),
 
75
      current_source_file_(NULL) { }
 
76
 
 
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",
 
81
                      iterator_->index);
 
82
    // Return our null string, to keep our promise about all names being
 
83
    // taken from the string section.
 
84
    offset = 0;
 
85
  }
 
86
  return reinterpret_cast<const char *>(strings_.start + offset);
 
87
}
 
88
 
 
89
bool StabsReader::Process() {
 
90
  while (!iterator_->at_end) {
 
91
    if (iterator_->type == N_SO) {
 
92
      if (! ProcessCompilationUnit())
 
93
        return false;
 
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.
 
102
      //
 
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;
 
109
      ++iterator_;
 
110
    }
 
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) {
 
117
      ProcessExtern();
 
118
    }
 
119
#endif
 
120
    else {
 
121
      ++iterator_;
 
122
    }
 
123
  }
 
124
  return true;
 
125
}
 
126
 
 
127
bool StabsReader::ProcessCompilationUnit() {
 
128
  assert(!iterator_->at_end && iterator_->type == N_SO);
 
129
 
 
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;
 
134
  {
 
135
    const char *name = SymbolString();
 
136
    if (name[0] && name[strlen(name) - 1] == '/') {
 
137
      build_directory = name;
 
138
      ++iterator_;
 
139
    }
 
140
  }
 
141
 
 
142
  // We expect to see an N_SO entry with a filename next, indicating
 
143
  // the start of the compilation unit.
 
144
  {
 
145
    if (iterator_->at_end || iterator_->type != N_SO)
 
146
      return true;
 
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
 
151
      // beginning.
 
152
      ++iterator_;
 
153
      return true;
 
154
    }
 
155
    current_source_file_ = name;
 
156
  }
 
157
 
 
158
  if (! handler_->StartCompilationUnit(current_source_file_,
 
159
                                       iterator_->value,
 
160
                                       build_directory))
 
161
    return false;
 
162
 
 
163
  ++iterator_;
 
164
 
 
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.
 
169
 
 
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())
 
174
        return false;
 
175
    } else if (iterator_->type == N_SLINE) {
 
176
      // Mac OS X STABS place SLINE records before functions.
 
177
      Line line;
 
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);
 
187
      ++iterator_;
 
188
    } else if (iterator_->type == N_SOL) {
 
189
      current_source_file_ = SymbolString();
 
190
      ++iterator_;
 
191
    } else {
 
192
      // Ignore anything else.
 
193
      ++iterator_;
 
194
    }
 
195
  }
 
196
 
 
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;
 
205
      ++iterator_;
 
206
    }
 
207
  }
 
208
 
 
209
  if (! handler_->EndCompilationUnit(ending_address))
 
210
    return false;
 
211
 
 
212
  queued_lines_.clear();
 
213
 
 
214
  return true;
 
215
}
 
216
 
 
217
bool StabsReader::ProcessFunction() {
 
218
  assert(!iterator_->at_end && iterator_->type == N_FUN);
 
219
 
 
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, ':');
 
226
  if (! name_end)
 
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))
 
230
    return false;
 
231
  ++iterator_;
 
232
 
 
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))
 
237
      return false;
 
238
  }
 
239
  queued_lines_.clear();
 
240
 
 
241
  while (!iterator_->at_end) {
 
242
    if (iterator_->type == N_SO || iterator_->type == N_FUN)
 
243
      break;
 
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))
 
253
        return false;
 
254
      ++iterator_;
 
255
    } else if (iterator_->type == N_SOL) {
 
256
      current_source_file_ = SymbolString();
 
257
      ++iterator_;
 
258
    } else
 
259
      // Ignore anything else.
 
260
      ++iterator_;
 
261
  }
 
262
 
 
263
  // We've reached the end of the function. See if we can figure out its
 
264
  // ending address.
 
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;
 
274
        ++iterator_;
 
275
      } else {
 
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;
 
280
      }
 
281
    } else {
 
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;
 
287
    }
 
288
  }
 
289
 
 
290
  if (! handler_->EndFunction(ending_address))
 
291
    return false;
 
292
 
 
293
  return true;
 
294
}
 
295
 
 
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);
 
301
#endif
 
302
 
 
303
  // TODO(mark): only do symbols in the text section?
 
304
  if (!handler_->Extern(SymbolString(), iterator_->value))
 
305
    return false;
 
306
 
 
307
  ++iterator_;
 
308
  return true;
 
309
}
 
310
 
 
311
} // namespace google_breakpad