00001 /* 00002 * See the dyninst/COPYRIGHT file for copyright information. 00003 * 00004 * We provide the Paradyn Tools (below described as "Paradyn") 00005 * on an AS IS basis, and do not warrant its validity or performance. 00006 * We reserve the right to update, modify, or discontinue this 00007 * software at any time. We shall have no obligation to supply such 00008 * updates or modifications or any other form of support to you. 00009 * 00010 * By your use of Paradyn, you understand and agree that we (or any 00011 * other person or entity with proprietary rights in Paradyn) are 00012 * under no obligation to provide either maintenance services, 00013 * update services, notices of latent defects, or correction of 00014 * defects for Paradyn. 00015 * 00016 * This library is free software; you can redistribute it and/or 00017 * modify it under the terms of the GNU Lesser General Public 00018 * License as published by the Free Software Foundation; either 00019 * version 2.1 of the License, or (at your option) any later version. 00020 * 00021 * This library is distributed in the hope that it will be useful, 00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00024 * Lesser General Public License for more details. 00025 * 00026 * You should have received a copy of the GNU Lesser General Public 00027 * License along with this library; if not, write to the Free Software 00028 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00029 */ 00030 00031 00032 // $Id: solarisKludges.C,v 1.8 2007/12/04 18:05:22 legendre Exp $ 00033 00034 #include "common/h/headers.h" 00035 #include "common/h/parseauxv.h" 00036 00037 #include <sys/auxv.h> 00038 00039 void * P_memcpy (void *A1, const void *A2, size_t SIZE) { 00040 return (memcpy(A1, A2, SIZE)); 00041 } 00042 00043 unsigned long long PDYN_div1000(unsigned long long in) { 00044 /* Divides by 1000 without an integer division instruction or library call, both of 00045 * which are slow. 00046 * We do only shifts, adds, and subtracts. 00047 * 00048 * We divide by 1000 in this way: 00049 * multiply by 1/1000, or multiply by (1/1000)*2^30 and then right-shift by 30. 00050 * So what is 1/1000 * 2^30? 00051 * It is 1,073,742. (actually this is rounded) 00052 * So we can multiply by 1,073,742 and then right-shift by 30 (neat, eh?) 00053 * 00054 * Now for multiplying by 1,073,742... 00055 * 1,073,742 = (1,048,576 + 16384 + 8192 + 512 + 64 + 8 + 4 + 2) 00056 * or, slightly optimized: 00057 * = (1,048,576 + 16384 + 8192 + 512 + 64 + 16 - 2) 00058 * for a total of 8 shifts and 6 add/subs, or 14 operations. 00059 * 00060 */ 00061 00062 unsigned long long temp = in << 20; // multiply by 1,048,576 00063 // beware of overflow; left shift by 20 is quite a lot. 00064 // If you know that the input fits in 32 bits (4 billion) then 00065 // no problem. But if it's much bigger then start worrying... 00066 00067 temp += in << 14; // 16384 00068 temp += in << 13; // 8192 00069 temp += in << 9; // 512 00070 temp += in << 6; // 64 00071 temp += in << 4; // 16 00072 temp -= in >> 2; // 2 00073 00074 return (temp >> 30); // divide by 2^30 00075 } 00076 00077 unsigned long long PDYN_divMillion(unsigned long long in) { 00078 /* Divides by 1,000,000 without an integer division instruction or library call, 00079 * both of which are slow. 00080 * We do only shifts, adds, and subtracts. 00081 * 00082 * We divide by 1,000,000 in this way: 00083 * multiply by 1/1,000,000, or multiply by (1/1,000,000)*2^30 and then right-shift 00084 * by 30. So what is 1/1,000,000 * 2^30? 00085 * It is 1,074. (actually this is rounded) 00086 * So we can multiply by 1,074 and then right-shift by 30 (neat, eh?) 00087 * 00088 * Now for multiplying by 1,074 00089 * 1,074 = (1024 + 32 + 16 + 2) 00090 * for a total of 4 shifts and 4 add/subs, or 8 operations. 00091 * 00092 * Note: compare with div1000 -- it's cheaper to divide by a million than 00093 * by a thousand (!) 00094 * 00095 */ 00096 00097 unsigned long long temp = in << 10; // multiply by 1024 00098 // beware of overflow...if the input arg uses more than 52 bits 00099 // than start worrying about whether (in << 10) plus the smaller additions 00100 // we're gonna do next will fit in 64... 00101 00102 temp += in << 5; // 32 00103 temp += in << 4; // 16 00104 temp += in << 1; // 2 00105 00106 return (temp >> 30); // divide by 2^30 00107 } 00108 00109 unsigned long long PDYN_mulMillion(unsigned long long in) { 00110 unsigned long long result = in; 00111 00112 /* multiply by 125 by multiplying by 128 and subtracting 3x */ 00113 result = (result << 7) - result - result - result; 00114 00115 /* multiply by 125 again, for a total of 15625x */ 00116 result = (result << 7) - result - result - result; 00117 00118 /* multiply by 64, for a total of 1,000,000x */ 00119 result <<= 6; 00120 00121 /* cost was: 3 shifts and 6 subtracts 00122 * cost of calling mul1000(mul1000()) would be: 6 shifts and 4 subtracts 00123 * 00124 * Another algorithm is to multiply by 2^6 and then 5^6. 00125 * The former is super-cheap (one shift); the latter is more expensive. 00126 * 5^6 = 15625 = 16384 - 512 - 256 + 8 + 1 00127 * so multiplying by 5^6 means 4 shift operations and 4 add/sub ops 00128 * so multiplying by 1000000 means 5 shift operations and 4 add/sub ops. 00129 * That may or may not be cheaper than what we're doing (3 shifts; 6 subtracts); 00130 * I'm not sure. --ari 00131 */ 00132 00133 return result; 00134 } 00135 00136 bool AuxvParser::readAuxvInfo() 00137 { 00138 auxv_t auxv_elm; 00139 00140 char buffer[32]; 00141 snprintf(buffer, 32, "/proc/%d/auxv", pid); 00142 int auxv_fd = P_open(buffer, O_RDONLY, pid); 00143 00144 while(read(auxv_fd, &auxv_elm, sizeof(auxv_elm)) == sizeof(auxv_elm)) { 00145 if (auxv_elm.a_type == AT_BASE) { 00146 interpreter_base = (Address)auxv_elm.a_un.a_ptr; 00147 P_close(auxv_fd); 00148 return true; 00149 } 00150 } 00151 00152 P_close(auxv_fd); 00153 return false; 00154 }
1.6.1