1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
10 // Information about the process mappings.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_PROCMAPS_H
13 #define SANITIZER_PROCMAPS_H
15 #include "sanitizer_internal_defs.h"
17 namespace __sanitizer
{
20 class MemoryMappingLayout
{
22 MemoryMappingLayout() {}
23 bool GetObjectNameAndOffset(uptr addr
, uptr
*offset
,
24 char filename
[], uptr filename_size
) {
30 class MemoryMappingLayout
{
32 MemoryMappingLayout();
33 bool Next(uptr
*start
, uptr
*end
, uptr
*offset
,
34 char filename
[], uptr filename_size
);
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
);
40 ~MemoryMappingLayout();
43 // Default implementation of GetObjectNameAndOffset.
44 // Quite slow, because it iterates through the whole process map for each
46 bool IterateForObjectNameAndOffset(uptr addr
, uptr
*offset
,
47 char filename
[], uptr filename_size
) {
49 uptr start
, end
, file_offset
;
50 for (int i
= 0; Next(&start
, &end
, &file_offset
, filename
, filename_size
);
52 if (addr
>= start
&& addr
< end
) {
53 // 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
64 *offset
= (addr
- (i
? start
: 0)) + file_offset
;
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_
;
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
);
84 u32 current_filetype_
;
85 int current_load_cmd_count_
;
86 char *current_load_cmd_addr_
;
92 } // namespace __sanitizer
94 #endif // SANITIZER_PROCMAPS_H