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