1 // Copyright (c) 2011 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31
32 // module.cc: Implement google_breakpad::Module. See module.h.
33
34 #include "common/module.h"
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include <iostream>
42 #include <utility>
43
44 namespace google_breakpad {
45
46 using std::dec;
47 using std::hex;
48
49
Module(const string & name,const string & os,const string & architecture,const string & id,const string & code_id)50 Module::Module(const string &name, const string &os,
51 const string &architecture, const string &id,
52 const string &code_id /* = "" */) :
53 name_(name),
54 os_(os),
55 architecture_(architecture),
56 id_(id),
57 code_id_(code_id),
58 load_address_(0) { }
59
~Module()60 Module::~Module() {
61 for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); ++it)
62 delete it->second;
63 for (FunctionSet::iterator it = functions_.begin();
64 it != functions_.end(); ++it) {
65 delete *it;
66 }
67 for (vector<StackFrameEntry *>::iterator it = stack_frame_entries_.begin();
68 it != stack_frame_entries_.end(); ++it) {
69 delete *it;
70 }
71 for (ExternSet::iterator it = externs_.begin(); it != externs_.end(); ++it)
72 delete *it;
73 }
74
SetLoadAddress(Address address)75 void Module::SetLoadAddress(Address address) {
76 load_address_ = address;
77 }
78
SetAddressRanges(const vector<Range> & ranges)79 void Module::SetAddressRanges(const vector<Range>& ranges) {
80 address_ranges_ = ranges;
81 }
82
AddFunction(Function * function)83 void Module::AddFunction(Function *function) {
84 // FUNC lines must not hold an empty name, so catch the problem early if
85 // callers try to add one.
86 assert(!function->name.empty());
87
88 if (!AddressIsInModule(function->address)) {
89 return;
90 }
91
92 // FUNCs are better than PUBLICs as they come with sizes, so remove an extern
93 // with the same address if present.
94 Extern ext(function->address);
95 ExternSet::iterator it_ext = externs_.find(&ext);
96 if (it_ext == externs_.end() &&
97 architecture_ == "arm" &&
98 (function->address & 0x1) == 0) {
99 // ARM THUMB functions have bit 0 set. ARM64 does not have THUMB.
100 Extern arm_thumb_ext(function->address | 0x1);
101 it_ext = externs_.find(&arm_thumb_ext);
102 }
103 if (it_ext != externs_.end()) {
104 delete *it_ext;
105 externs_.erase(it_ext);
106 }
107 #if _DEBUG
108 {
109 // There should be no other PUBLIC symbols that overlap with the function.
110 for (const Range& range : function->ranges) {
111 Extern debug_ext(range.address);
112 ExternSet::iterator it_debug = externs_.lower_bound(&ext);
113 assert(it_debug == externs_.end() ||
114 (*it_debug)->address >= range.address + range.size);
115 }
116 }
117 #endif
118
119 std::pair<FunctionSet::iterator,bool> ret = functions_.insert(function);
120 if (!ret.second && (*ret.first != function)) {
121 // Free the duplicate that was not inserted because this Module
122 // now owns it.
123 delete function;
124 }
125 }
126
AddFunctions(vector<Function * >::iterator begin,vector<Function * >::iterator end)127 void Module::AddFunctions(vector<Function *>::iterator begin,
128 vector<Function *>::iterator end) {
129 for (vector<Function *>::iterator it = begin; it != end; ++it)
130 AddFunction(*it);
131 }
132
AddStackFrameEntry(StackFrameEntry * stack_frame_entry)133 void Module::AddStackFrameEntry(StackFrameEntry *stack_frame_entry) {
134 if (!AddressIsInModule(stack_frame_entry->address)) {
135 return;
136 }
137
138 stack_frame_entries_.push_back(stack_frame_entry);
139 }
140
AddExtern(Extern * ext)141 void Module::AddExtern(Extern *ext) {
142 if (!AddressIsInModule(ext->address)) {
143 return;
144 }
145
146 std::pair<ExternSet::iterator,bool> ret = externs_.insert(ext);
147 if (!ret.second) {
148 // Free the duplicate that was not inserted because this Module
149 // now owns it.
150 delete ext;
151 }
152 }
153
GetFunctions(vector<Function * > * vec,vector<Function * >::iterator i)154 void Module::GetFunctions(vector<Function *> *vec,
155 vector<Function *>::iterator i) {
156 vec->insert(i, functions_.begin(), functions_.end());
157 }
158
GetExterns(vector<Extern * > * vec,vector<Extern * >::iterator i)159 void Module::GetExterns(vector<Extern *> *vec,
160 vector<Extern *>::iterator i) {
161 vec->insert(i, externs_.begin(), externs_.end());
162 }
163
FindFile(const string & name)164 Module::File *Module::FindFile(const string &name) {
165 // A tricky bit here. The key of each map entry needs to be a
166 // pointer to the entry's File's name string. This means that we
167 // can't do the initial lookup with any operation that would create
168 // an empty entry for us if the name isn't found (like, say,
169 // operator[] or insert do), because such a created entry's key will
170 // be a pointer the string passed as our argument. Since the key of
171 // a map's value type is const, we can't fix it up once we've
172 // created our file. lower_bound does the lookup without doing an
173 // insertion, and returns a good hint iterator to pass to insert.
174 // Our "destiny" is where we belong, whether we're there or not now.
175 FileByNameMap::iterator destiny = files_.lower_bound(&name);
176 if (destiny == files_.end()
177 || *destiny->first != name) { // Repeated string comparison, boo hoo.
178 File *file = new File(name);
179 file->source_id = -1;
180 destiny = files_.insert(destiny,
181 FileByNameMap::value_type(&file->name, file));
182 }
183 return destiny->second;
184 }
185
FindFile(const char * name)186 Module::File *Module::FindFile(const char *name) {
187 string name_string = name;
188 return FindFile(name_string);
189 }
190
FindExistingFile(const string & name)191 Module::File *Module::FindExistingFile(const string &name) {
192 FileByNameMap::iterator it = files_.find(&name);
193 return (it == files_.end()) ? NULL : it->second;
194 }
195
GetFiles(vector<File * > * vec)196 void Module::GetFiles(vector<File *> *vec) {
197 vec->clear();
198 for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); ++it)
199 vec->push_back(it->second);
200 }
201
GetStackFrameEntries(vector<StackFrameEntry * > * vec) const202 void Module::GetStackFrameEntries(vector<StackFrameEntry *> *vec) const {
203 *vec = stack_frame_entries_;
204 }
205
AssignSourceIds()206 void Module::AssignSourceIds() {
207 // First, give every source file an id of -1.
208 for (FileByNameMap::iterator file_it = files_.begin();
209 file_it != files_.end(); ++file_it) {
210 file_it->second->source_id = -1;
211 }
212
213 // Next, mark all files actually cited by our functions' line number
214 // info, by setting each one's source id to zero.
215 for (FunctionSet::const_iterator func_it = functions_.begin();
216 func_it != functions_.end(); ++func_it) {
217 Function *func = *func_it;
218 for (vector<Line>::iterator line_it = func->lines.begin();
219 line_it != func->lines.end(); ++line_it)
220 line_it->file->source_id = 0;
221 }
222
223 // Finally, assign source ids to those files that have been marked.
224 // We could have just assigned source id numbers while traversing
225 // the line numbers, but doing it this way numbers the files in
226 // lexicographical order by name, which is neat.
227 int next_source_id = 0;
228 for (FileByNameMap::iterator file_it = files_.begin();
229 file_it != files_.end(); ++file_it) {
230 if (!file_it->second->source_id)
231 file_it->second->source_id = next_source_id++;
232 }
233 }
234
ReportError()235 bool Module::ReportError() {
236 fprintf(stderr, "error writing symbol file: %s\n",
237 strerror(errno));
238 return false;
239 }
240
WriteRuleMap(const RuleMap & rule_map,std::ostream & stream)241 bool Module::WriteRuleMap(const RuleMap &rule_map, std::ostream &stream) {
242 for (RuleMap::const_iterator it = rule_map.begin();
243 it != rule_map.end(); ++it) {
244 if (it != rule_map.begin())
245 stream << ' ';
246 stream << it->first << ": " << it->second;
247 }
248 return stream.good();
249 }
250
AddressIsInModule(Address address) const251 bool Module::AddressIsInModule(Address address) const {
252 if (address_ranges_.empty()) {
253 return true;
254 }
255 for (const auto& segment : address_ranges_) {
256 if (address >= segment.address &&
257 address < segment.address + segment.size) {
258 return true;
259 }
260 }
261 return false;
262 }
263
Write(std::ostream & stream,SymbolData symbol_data)264 bool Module::Write(std::ostream &stream, SymbolData symbol_data) {
265 stream << "MODULE " << os_ << " " << architecture_ << " "
266 << id_ << " " << name_ << "\n";
267 if (!stream.good())
268 return ReportError();
269
270 if (!code_id_.empty()) {
271 stream << "INFO CODE_ID " << code_id_ << "\n";
272 }
273
274 if (symbol_data != ONLY_CFI) {
275 AssignSourceIds();
276
277 // Write out files.
278 for (FileByNameMap::iterator file_it = files_.begin();
279 file_it != files_.end(); ++file_it) {
280 File *file = file_it->second;
281 if (file->source_id >= 0) {
282 stream << "FILE " << file->source_id << " " << file->name << "\n";
283 if (!stream.good())
284 return ReportError();
285 }
286 }
287
288 // Write out functions and their lines.
289 for (FunctionSet::const_iterator func_it = functions_.begin();
290 func_it != functions_.end(); ++func_it) {
291 Function *func = *func_it;
292 vector<Line>::iterator line_it = func->lines.begin();
293 for (auto range_it = func->ranges.cbegin();
294 range_it != func->ranges.cend(); ++range_it) {
295 stream << "FUNC " << hex
296 << (range_it->address - load_address_) << " "
297 << range_it->size << " "
298 << func->parameter_size << " "
299 << func->name << dec << "\n";
300
301 if (!stream.good())
302 return ReportError();
303
304 while ((line_it != func->lines.end()) &&
305 (line_it->address >= range_it->address) &&
306 (line_it->address < (range_it->address + range_it->size))) {
307 stream << hex
308 << (line_it->address - load_address_) << " "
309 << line_it->size << " "
310 << dec
311 << line_it->number << " "
312 << line_it->file->source_id << "\n";
313
314 if (!stream.good())
315 return ReportError();
316
317 ++line_it;
318 }
319 }
320 }
321
322 // Write out 'PUBLIC' records.
323 for (ExternSet::const_iterator extern_it = externs_.begin();
324 extern_it != externs_.end(); ++extern_it) {
325 Extern *ext = *extern_it;
326 stream << "PUBLIC " << hex
327 << (ext->address - load_address_) << " 0 "
328 << ext->name << dec << "\n";
329 }
330 }
331
332 if (symbol_data != NO_CFI) {
333 // Write out 'STACK CFI INIT' and 'STACK CFI' records.
334 vector<StackFrameEntry *>::const_iterator frame_it;
335 for (frame_it = stack_frame_entries_.begin();
336 frame_it != stack_frame_entries_.end(); ++frame_it) {
337 StackFrameEntry *entry = *frame_it;
338 stream << "STACK CFI INIT " << hex
339 << (entry->address - load_address_) << " "
340 << entry->size << " " << dec;
341 if (!stream.good()
342 || !WriteRuleMap(entry->initial_rules, stream))
343 return ReportError();
344
345 stream << "\n";
346
347 // Write out this entry's delta rules as 'STACK CFI' records.
348 for (RuleChangeMap::const_iterator delta_it = entry->rule_changes.begin();
349 delta_it != entry->rule_changes.end(); ++delta_it) {
350 stream << "STACK CFI " << hex
351 << (delta_it->first - load_address_) << " " << dec;
352 if (!stream.good()
353 || !WriteRuleMap(delta_it->second, stream))
354 return ReportError();
355
356 stream << "\n";
357 }
358 }
359 }
360
361 return true;
362 }
363
364 } // namespace google_breakpad
365