| 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 | |
| 21 | class 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 Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 32 | |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 33 | 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 Samsonov | 961276a | 2012-07-03 08:24:14 +0000 | [diff] [blame^] | 44 | // 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 Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 55 | *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 |