[AArch64] Add cost handling of CALLER_SAVE_REGS and POINTER_REGS
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_procmaps_mac.cc
blobc68530683467b713c13624d51290fd1c47c0448e
1 //===-- sanitizer_procmaps_mac.cc -----------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Information about the process mappings (Mac-specific parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
12 #if SANITIZER_MAC
13 #include "sanitizer_common.h"
14 #include "sanitizer_placement_new.h"
15 #include "sanitizer_procmaps.h"
17 #include <mach-o/dyld.h>
18 #include <mach-o/loader.h>
20 namespace __sanitizer {
22 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
23 Reset();
26 MemoryMappingLayout::~MemoryMappingLayout() {
29 // More information about Mach-O headers can be found in mach-o/loader.h
30 // Each Mach-O image has a header (mach_header or mach_header_64) starting with
31 // a magic number, and a list of linker load commands directly following the
32 // header.
33 // A load command is at least two 32-bit words: the command type and the
34 // command size in bytes. We're interested only in segment load commands
35 // (LC_SEGMENT and LC_SEGMENT_64), which tell that a part of the file is mapped
36 // into the task's address space.
37 // The |vmaddr|, |vmsize| and |fileoff| fields of segment_command or
38 // segment_command_64 correspond to the memory address, memory size and the
39 // file offset of the current memory segment.
40 // Because these fields are taken from the images as is, one needs to add
41 // _dyld_get_image_vmaddr_slide() to get the actual addresses at runtime.
43 void MemoryMappingLayout::Reset() {
44 // Count down from the top.
45 // TODO(glider): as per man 3 dyld, iterating over the headers with
46 // _dyld_image_count is thread-unsafe. We need to register callbacks for
47 // adding and removing images which will invalidate the MemoryMappingLayout
48 // state.
49 current_image_ = _dyld_image_count();
50 current_load_cmd_count_ = -1;
51 current_load_cmd_addr_ = 0;
52 current_magic_ = 0;
53 current_filetype_ = 0;
56 // static
57 void MemoryMappingLayout::CacheMemoryMappings() {
58 // No-op on Mac for now.
61 void MemoryMappingLayout::LoadFromCache() {
62 // No-op on Mac for now.
65 // Next and NextSegmentLoad were inspired by base/sysinfo.cc in
66 // Google Perftools, http://code.google.com/p/google-perftools.
68 // NextSegmentLoad scans the current image for the next segment load command
69 // and returns the start and end addresses and file offset of the corresponding
70 // segment.
71 // Note that the segment addresses are not necessarily sorted.
72 template<u32 kLCSegment, typename SegmentCommand>
73 bool MemoryMappingLayout::NextSegmentLoad(
74 uptr *start, uptr *end, uptr *offset,
75 char filename[], uptr filename_size, uptr *protection) {
76 if (protection)
77 UNIMPLEMENTED();
78 const char* lc = current_load_cmd_addr_;
79 current_load_cmd_addr_ += ((const load_command *)lc)->cmdsize;
80 if (((const load_command *)lc)->cmd == kLCSegment) {
81 const sptr dlloff = _dyld_get_image_vmaddr_slide(current_image_);
82 const SegmentCommand* sc = (const SegmentCommand *)lc;
83 if (start) *start = sc->vmaddr + dlloff;
84 if (end) *end = sc->vmaddr + sc->vmsize + dlloff;
85 if (offset) {
86 if (current_filetype_ == /*MH_EXECUTE*/ 0x2) {
87 *offset = sc->vmaddr;
88 } else {
89 *offset = sc->fileoff;
92 if (filename) {
93 internal_strncpy(filename, _dyld_get_image_name(current_image_),
94 filename_size);
96 return true;
98 return false;
101 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
102 char filename[], uptr filename_size,
103 uptr *protection) {
104 for (; current_image_ >= 0; current_image_--) {
105 const mach_header* hdr = _dyld_get_image_header(current_image_);
106 if (!hdr) continue;
107 if (current_load_cmd_count_ < 0) {
108 // Set up for this image;
109 current_load_cmd_count_ = hdr->ncmds;
110 current_magic_ = hdr->magic;
111 current_filetype_ = hdr->filetype;
112 switch (current_magic_) {
113 #ifdef MH_MAGIC_64
114 case MH_MAGIC_64: {
115 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header_64);
116 break;
118 #endif
119 case MH_MAGIC: {
120 current_load_cmd_addr_ = (char*)hdr + sizeof(mach_header);
121 break;
123 default: {
124 continue;
129 for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
130 switch (current_magic_) {
131 // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
132 #ifdef MH_MAGIC_64
133 case MH_MAGIC_64: {
134 if (NextSegmentLoad<LC_SEGMENT_64, struct segment_command_64>(
135 start, end, offset, filename, filename_size, protection))
136 return true;
137 break;
139 #endif
140 case MH_MAGIC: {
141 if (NextSegmentLoad<LC_SEGMENT, struct segment_command>(
142 start, end, offset, filename, filename_size, protection))
143 return true;
144 break;
148 // If we get here, no more load_cmd's in this image talk about
149 // segments. Go on to the next image.
151 return false;
154 uptr MemoryMappingLayout::DumpListOfModules(LoadedModule *modules,
155 uptr max_modules,
156 string_predicate_t filter) {
157 Reset();
158 uptr cur_beg, cur_end;
159 InternalScopedBuffer<char> module_name(kMaxPathLength);
160 uptr n_modules = 0;
161 for (uptr i = 0; n_modules < max_modules &&
162 Next(&cur_beg, &cur_end, 0, module_name.data(),
163 module_name.size(), 0);
164 i++) {
165 const char *cur_name = module_name.data();
166 if (cur_name[0] == '\0')
167 continue;
168 if (filter && !filter(cur_name))
169 continue;
170 LoadedModule *cur_module = 0;
171 if (n_modules > 0 &&
172 0 == internal_strcmp(cur_name, modules[n_modules - 1].full_name())) {
173 cur_module = &modules[n_modules - 1];
174 } else {
175 void *mem = &modules[n_modules];
176 cur_module = new(mem) LoadedModule(cur_name, cur_beg);
177 n_modules++;
179 cur_module->addAddressRange(cur_beg, cur_end);
181 return n_modules;
184 } // namespace __sanitizer
186 #endif // SANITIZER_MAC