Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Constructs a mapper that maps addresses into code entries. |
| 31 | * |
| 32 | * @constructor |
| 33 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 34 | function CodeMap() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Dynamic code entries. Used for JIT compiled code. |
| 37 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 38 | this.dynamics_ = new SplayTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Name generator for entries having duplicate names. |
| 42 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 43 | this.dynamicsNameGen_ = new CodeMap.NameGenerator(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Static code entries. Used for statically compiled code. |
| 47 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 48 | this.statics_ = new SplayTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Libraries entries. Used for the whole static code libraries. |
| 52 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 53 | this.libraries_ = new SplayTree(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Map of memory pages occupied with static code. |
| 57 | */ |
| 58 | this.pages_ = []; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * The number of alignment bits in a page address. |
| 64 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 65 | CodeMap.PAGE_ALIGNMENT = 12; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | /** |
| 69 | * Page size in bytes. |
| 70 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 71 | CodeMap.PAGE_SIZE = |
| 72 | 1 << CodeMap.PAGE_ALIGNMENT; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | /** |
| 76 | * Adds a dynamic (i.e. moveable and discardable) code entry. |
| 77 | * |
| 78 | * @param {number} start The starting address. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 79 | * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 81 | CodeMap.prototype.addCode = function(start, codeEntry) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 82 | this.deleteAllCoveredNodes_(this.dynamics_, start, start + codeEntry.size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | this.dynamics_.insert(start, codeEntry); |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Moves a dynamic code entry. Throws an exception if there is no dynamic |
| 89 | * code entry with the specified starting address. |
| 90 | * |
| 91 | * @param {number} from The starting address of the entry being moved. |
| 92 | * @param {number} to The destination address. |
| 93 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 94 | CodeMap.prototype.moveCode = function(from, to) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | var removedNode = this.dynamics_.remove(from); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 96 | this.deleteAllCoveredNodes_(this.dynamics_, to, to + removedNode.value.size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 97 | this.dynamics_.insert(to, removedNode.value); |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Discards a dynamic code entry. Throws an exception if there is no dynamic |
| 103 | * code entry with the specified starting address. |
| 104 | * |
| 105 | * @param {number} start The starting address of the entry being deleted. |
| 106 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 107 | CodeMap.prototype.deleteCode = function(start) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | var removedNode = this.dynamics_.remove(start); |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Adds a library entry. |
| 114 | * |
| 115 | * @param {number} start The starting address. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 116 | * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 118 | CodeMap.prototype.addLibrary = function( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | start, codeEntry) { |
| 120 | this.markPages_(start, start + codeEntry.size); |
| 121 | this.libraries_.insert(start, codeEntry); |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Adds a static code entry. |
| 127 | * |
| 128 | * @param {number} start The starting address. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 129 | * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 131 | CodeMap.prototype.addStaticCode = function( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 132 | start, codeEntry) { |
| 133 | this.statics_.insert(start, codeEntry); |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * @private |
| 139 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 140 | CodeMap.prototype.markPages_ = function(start, end) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 141 | for (var addr = start; addr <= end; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 142 | addr += CodeMap.PAGE_SIZE) { |
| 143 | this.pages_[addr >>> CodeMap.PAGE_ALIGNMENT] = 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * @private |
| 150 | */ |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 151 | CodeMap.prototype.deleteAllCoveredNodes_ = function(tree, start, end) { |
| 152 | var to_delete = []; |
| 153 | var addr = end - 1; |
| 154 | while (addr >= start) { |
| 155 | var node = tree.findGreatestLessThan(addr); |
| 156 | if (!node) break; |
| 157 | var start2 = node.key, end2 = start2 + node.value.size; |
| 158 | if (start2 < end && start < end2) to_delete.push(start2); |
| 159 | addr = start2 - 1; |
| 160 | } |
| 161 | for (var i = 0, l = to_delete.length; i < l; ++i) tree.remove(to_delete[i]); |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * @private |
| 167 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 168 | CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | return addr >= node.key && addr < (node.key + node.value.size); |
| 170 | }; |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * @private |
| 175 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 176 | CodeMap.prototype.findInTree_ = function(tree, addr) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | var node = tree.findGreatestLessThan(addr); |
| 178 | return node && this.isAddressBelongsTo_(addr, node) ? node.value : null; |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | /** |
| 183 | * Finds a code entry that contains the specified address. Both static and |
| 184 | * dynamic code entries are considered. |
| 185 | * |
| 186 | * @param {number} addr Address. |
| 187 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 188 | CodeMap.prototype.findEntry = function(addr) { |
| 189 | var pageAddr = addr >>> CodeMap.PAGE_ALIGNMENT; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 190 | if (pageAddr in this.pages_) { |
| 191 | // Static code entries can contain "holes" of unnamed code. |
| 192 | // In this case, the whole library is assigned to this address. |
| 193 | return this.findInTree_(this.statics_, addr) || |
| 194 | this.findInTree_(this.libraries_, addr); |
| 195 | } |
| 196 | var min = this.dynamics_.findMin(); |
| 197 | var max = this.dynamics_.findMax(); |
| 198 | if (max != null && addr < (max.key + max.value.size) && addr >= min.key) { |
| 199 | var dynaEntry = this.findInTree_(this.dynamics_, addr); |
| 200 | if (dynaEntry == null) return null; |
| 201 | // Dedupe entry name. |
| 202 | if (!dynaEntry.nameUpdated_) { |
| 203 | dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name); |
| 204 | dynaEntry.nameUpdated_ = true; |
| 205 | } |
| 206 | return dynaEntry; |
| 207 | } |
| 208 | return null; |
| 209 | }; |
| 210 | |
| 211 | |
| 212 | /** |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 213 | * Returns a dynamic code entry using its starting address. |
| 214 | * |
| 215 | * @param {number} addr Address. |
| 216 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 217 | CodeMap.prototype.findDynamicEntryByStartAddress = |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 218 | function(addr) { |
| 219 | var node = this.dynamics_.find(addr); |
| 220 | return node ? node.value : null; |
| 221 | }; |
| 222 | |
| 223 | |
| 224 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | * Returns an array of all dynamic code entries. |
| 226 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 227 | CodeMap.prototype.getAllDynamicEntries = function() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | return this.dynamics_.exportValues(); |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | /** |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 233 | * Returns an array of pairs of all dynamic code entries and their addresses. |
| 234 | */ |
| 235 | CodeMap.prototype.getAllDynamicEntriesWithAddresses = function() { |
| 236 | return this.dynamics_.exportKeysAndValues(); |
| 237 | }; |
| 238 | |
| 239 | |
| 240 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | * Returns an array of all static code entries. |
| 242 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 243 | CodeMap.prototype.getAllStaticEntries = function() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 244 | return this.statics_.exportValues(); |
| 245 | }; |
| 246 | |
| 247 | |
| 248 | /** |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 249 | * Returns an array of pairs of all static code entries and their addresses. |
| 250 | */ |
| 251 | CodeMap.prototype.getAllStaticEntriesWithAddresses = function() { |
| 252 | return this.statics_.exportKeysAndValues(); |
| 253 | }; |
| 254 | |
| 255 | |
| 256 | /** |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | * Returns an array of all libraries entries. |
| 258 | */ |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 259 | CodeMap.prototype.getAllLibrariesEntries = function() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | return this.libraries_.exportValues(); |
| 261 | }; |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * Creates a code entry object. |
| 266 | * |
| 267 | * @param {number} size Code entry size in bytes. |
| 268 | * @param {string} opt_name Code entry name. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 269 | * @param {string} opt_type Code entry type, e.g. SHARED_LIB, CPP. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 270 | * @constructor |
| 271 | */ |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 272 | CodeMap.CodeEntry = function(size, opt_name, opt_type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | this.size = size; |
| 274 | this.name = opt_name || ''; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 275 | this.type = opt_type || ''; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 276 | this.nameUpdated_ = false; |
| 277 | }; |
| 278 | |
| 279 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 280 | CodeMap.CodeEntry.prototype.getName = function() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | return this.name; |
| 282 | }; |
| 283 | |
| 284 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 285 | CodeMap.CodeEntry.prototype.toString = function() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | return this.name + ': ' + this.size.toString(16); |
| 287 | }; |
| 288 | |
| 289 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 290 | CodeMap.NameGenerator = function() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 291 | this.knownNames_ = {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 295 | CodeMap.NameGenerator.prototype.getName = function(name) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | if (!(name in this.knownNames_)) { |
| 297 | this.knownNames_[name] = 0; |
| 298 | return name; |
| 299 | } |
| 300 | var count = ++this.knownNames_[name]; |
| 301 | return name + ' {' + count + '}'; |
| 302 | }; |