blob: e7f9cac6cf6c3b7d86b06a95dafea6c1bbce0876 [file] [log] [blame]
Alexey Samsonov28a98952012-06-07 06:15:12 +00001//===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer.
11//
12// Information about the process mappings.
13//===----------------------------------------------------------------------===//
14#ifndef SANITIZER_PROCMAPS_H
15#define SANITIZER_PROCMAPS_H
16
17#include "sanitizer_internal_defs.h"
18
19namespace __sanitizer {
20
21class ProcessMaps {
22 public:
23 ProcessMaps();
24 bool Next(uptr *start, uptr *end, uptr *offset,
25 char filename[], uptr filename_size);
26 void Reset();
27 // Gets the object file name and the offset in that object for a given
28 // address 'addr'. Returns true on success.
29 bool GetObjectNameAndOffset(uptr addr, uptr *offset,
30 char filename[], uptr filename_size);
31 ~ProcessMaps();
Kostya Serebryany98390d02012-06-20 15:19:17 +000032
Alexey Samsonov28a98952012-06-07 06:15:12 +000033 private:
34 // Default implementation of GetObjectNameAndOffset.
35 // Quite slow, because it iterates through the whole process map for each
36 // lookup.
37 bool IterateForObjectNameAndOffset(uptr addr, uptr *offset,
38 char filename[], uptr filename_size) {
39 Reset();
40 uptr start, end, file_offset;
41 for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size);
42 i++) {
43 if (addr >= start && addr < end) {
Alexey Samsonov961276a2012-07-03 08:24:14 +000044 // Don't subtract 'start' for the first entry:
45 // * If a binary is compiled w/o -pie, then the first entry in
46 // process maps is likely the binary itself (all dynamic libs
47 // are mapped higher in address space). For such a binary,
48 // instruction offset in binary coincides with the actual
49 // instruction address in virtual memory (as code section
50 // is mapped to a fixed memory range).
51 // * If a binary is compiled with -pie, all the modules are
52 // mapped high at address space (in particular, higher than
53 // shadow memory of the tool), so the module can't be the
54 // first entry.
Alexey Samsonov28a98952012-06-07 06:15:12 +000055 *offset = (addr - (i ? start : 0)) + file_offset;
56 return true;
57 }
58 }
59 if (filename_size)
60 filename[0] = '\0';
61 return false;
62 }
63
64#if defined __linux__
65 char *proc_self_maps_buff_;
66 uptr proc_self_maps_buff_mmaped_size_;
67 uptr proc_self_maps_buff_len_;
68 char *current_;
69#elif defined __APPLE__
70 template<u32 kLCSegment, typename SegmentCommand>
71 bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
72 char filename[], uptr filename_size);
73 int current_image_;
74 u32 current_magic_;
75 int current_load_cmd_count_;
76 char *current_load_cmd_addr_;
77#endif
78};
79
80} // namespace __sanitizer
81
82#endif // SANITIZER_PROCMAPS_H