blob: 2e00952376d9472e83f7339f379aad9592473091 [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
Alexey Samsonovcc622112012-08-27 13:48:48 +000021#ifdef _WIN32
22class MemoryMappingLayout {
23 MemoryMappingLayout() {
24 UNIMPLEMENTED();
25 }
26};
27
28#else // _WIN32
29
30class MemoryMappingLayout {
Alexey Samsonov28a98952012-06-07 06:15:12 +000031 public:
Alexey Samsonovcc622112012-08-27 13:48:48 +000032 MemoryMappingLayout();
Alexey Samsonov28a98952012-06-07 06:15:12 +000033 bool Next(uptr *start, uptr *end, uptr *offset,
34 char filename[], uptr filename_size);
35 void Reset();
36 // Gets the object file name and the offset in that object for a given
37 // address 'addr'. Returns true on success.
38 bool GetObjectNameAndOffset(uptr addr, uptr *offset,
39 char filename[], uptr filename_size);
Alexey Samsonovcc622112012-08-27 13:48:48 +000040 ~MemoryMappingLayout();
Kostya Serebryany98390d02012-06-20 15:19:17 +000041
Alexey Samsonov28a98952012-06-07 06:15:12 +000042 private:
43 // Default implementation of GetObjectNameAndOffset.
44 // Quite slow, because it iterates through the whole process map for each
45 // lookup.
46 bool IterateForObjectNameAndOffset(uptr addr, uptr *offset,
47 char filename[], uptr filename_size) {
48 Reset();
49 uptr start, end, file_offset;
50 for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size);
51 i++) {
52 if (addr >= start && addr < end) {
Alexey Samsonov961276a2012-07-03 08:24:14 +000053 // Don't subtract 'start' for the first entry:
54 // * If a binary is compiled w/o -pie, then the first entry in
55 // process maps is likely the binary itself (all dynamic libs
56 // are mapped higher in address space). For such a binary,
57 // instruction offset in binary coincides with the actual
58 // instruction address in virtual memory (as code section
59 // is mapped to a fixed memory range).
60 // * If a binary is compiled with -pie, all the modules are
61 // mapped high at address space (in particular, higher than
62 // shadow memory of the tool), so the module can't be the
63 // first entry.
Alexey Samsonov28a98952012-06-07 06:15:12 +000064 *offset = (addr - (i ? start : 0)) + file_offset;
65 return true;
66 }
67 }
68 if (filename_size)
69 filename[0] = '\0';
70 return false;
71 }
72
73#if defined __linux__
74 char *proc_self_maps_buff_;
75 uptr proc_self_maps_buff_mmaped_size_;
76 uptr proc_self_maps_buff_len_;
77 char *current_;
78#elif defined __APPLE__
79 template<u32 kLCSegment, typename SegmentCommand>
80 bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
81 char filename[], uptr filename_size);
82 int current_image_;
83 u32 current_magic_;
84 int current_load_cmd_count_;
85 char *current_load_cmd_addr_;
86#endif
87};
88
Alexey Samsonovcc622112012-08-27 13:48:48 +000089#endif // _WIN32
90
Alexey Samsonov28a98952012-06-07 06:15:12 +000091} // namespace __sanitizer
92
93#endif // SANITIZER_PROCMAPS_H