1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

update IAddress as per 69908

This commit is contained in:
David Inglis 2004-09-23 15:28:21 +00:00
parent a1c754d019
commit 61499b865d
14 changed files with 231 additions and 217 deletions

View file

@ -12,7 +12,7 @@ package org.eclipse.cdt.core;
import java.math.BigInteger;
/*
/**
* Represents C/C++ address in CDT. All implementors of this inteface should be
* immutable, i.e. all methods should not modify objects, they should return
* new object.
@ -20,63 +20,93 @@ import java.math.BigInteger;
* Please see Addr32 and Addr64 classes to see how this interface should
* be extended
*/
public interface IAddress
public interface IAddress extends Comparable
{
/*
* Return adds offset to address and returns new address object
/**
* Adds offset to address and returns new address object
* which is the result
* @param offset to add
* @return the new address
*/
IAddress add(BigInteger offset);
/*
/**
* Adds offset to address and returns new address object
* which is the result
* <br><br>Note: This method has an offset limit of Long.MAX and Long.MIN, which under some addressing schems
* may impose an unnesseary limitation, see <code>IAddressa.add(BigInteger offset)</code> to handle larger offsets.
* @param offset to add
* @return the new address
*/
IAddress add(long offset);
/**
* Returns maximal offset possible for address. The offset
* should be Identicall for all addresses of given class.
* @return the max offset for this address class
*/
BigInteger getMaxOffset();
/*
* Returns distance between two addresses. Distance may be positive or negative
/**
* Returns distance to address. Distance may be positive or negative
* @param other address which distance is calculated to.
* @return distance to address
*/
BigInteger distance(IAddress other);
/*
* Compares two addresses.
*
* Returns:
* -1 if this < addr
* 0 if this == addr
* 1 if this > addr
BigInteger distanceTo(IAddress other);
/**
* Returns the value of the address.
* @return
*/
int compareTo(IAddress addr);
/*
* Returns true if addresses are equal
BigInteger getValue();
/**
* Returns whether this address equals the given object.
*
* @param obj the other object
* @return <code>true</code> if the addresses are equivalent,
* and <code>false</code> if they are not
*/
boolean equals(IAddress addr);
/*
boolean equals(Object addr);
/**
* Return true if address is zero, i.e. minimal possible
*/
* @return true is address is zero
*/
boolean isZero();
/*
/**
* Return true if address is maximal, i.e. maximal possible
*/
* @return true if address is maximal
*/
boolean isMax();
/*
/**
* Converts address to string as an unsigned number with given radix
* @param radix to use for strng conversion
* @return a string representation of address
*/
String toString(int radix);
/*
/**
* Identical to toString(10)
* @return a string representation of address using a radix of 10
*/
String toString();
/*
/**
* Converts address to the hex representation with '0x' prefix and
* with all leading zeros. The length of returned string should be
* the same for all addresses of given class. I.e. 10 for 32-bit
* addresses and 18 for 64-bit addresses
* @return
*/
String toHexAddressString();
/*
/**
* Returns amount of symbols in hex representation. Is identical to
* toHexAddressString().length(). It is present for perfomance purpose.
* @return the nmber os chararcter symbols to represent this address in hex.
*/
int getCharsNum();

View file

@ -15,7 +15,6 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
@ -112,7 +111,7 @@ public class Addr2line {
// this for validation.
//IPF_TODO: check
for (int i = 0; i <= 20; i += 4, address = address.add(BigInteger.valueOf(i))) {
for (int i = 0; i <= 20; i += 4, address = address.add(i)) {
String line = getLine(address);
if (line != null) {
int colon = line.lastIndexOf(':');

View file

@ -1,119 +1,111 @@
/*******************************************************************************
* Copyright (c) 2004 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* Copyright (c) 2004 Intel Corporation and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Common Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Intel Corporation - Initial API and implementation
*******************************************************************************/
* Contributors: Intel Corporation - Initial API and implementation
******************************************************************************/
package org.eclipse.cdt.utils;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
/*
*/
final public class Addr32 implements IAddress
{
public static final Addr32 ZERO=new Addr32(0);
public static final Addr32 MAX=new Addr32(0xffffffffL);
public class Addr32 implements IAddress {
public static final BigInteger MAX_OFFSET = BigInteger.valueOf(0xffffffffL);
private static final long MAX_ADDR = 0xffffffffL;
public static final Addr32 ZERO = new Addr32(0);
public static final Addr32 MAX = new Addr32(MAX_ADDR);
public static final BigInteger MAX_OFFSET = BigInteger.valueOf(MAX_ADDR);
public static final int BYTES_NUM = 4;
public static final int DIGITS_NUM = BYTES_NUM * 2;
public static final int CHARS_NUM = DIGITS_NUM + 2;
private long address;
public static final int CHARS_NUM = DIGITS_NUM + 2;
private final long address;
/*
* addrBytes should be 4 bytes length
*/
public Addr32(byte [] addrBytes)
{
/*We should mask out sign bits to have correct value*/
this.address = ( ( ((long)addrBytes[0]) << 24 ) & 0xFF000000L) +
( ( ((long)addrBytes[1]) << 16 ) & 0x00FF0000L) +
( ( ((long)addrBytes[2]) << 8 ) & 0x0000FF00L) +
( ((long)addrBytes[3]) & 0x000000FFL);
public Addr32(byte[] addrBytes) {
if (addrBytes.length != 4)
throw (new NumberFormatException("Invalid address array")); //$NON-NLS-1$
/* We should mask out sign bits to have correct value */
this.address = ( ( ((long)addrBytes[0]) << 24) & 0xFF000000L) + ( ( ((long)addrBytes[1]) << 16) & 0x00FF0000L)
+ ( ( ((long)addrBytes[2]) << 8) & 0x0000FF00L) + (addrBytes[3] & 0x000000FFL);
}
public Addr32(long rawaddress)
{
this.address=rawaddress;
}
public Addr32(String addr)
{
addr = addr.toLowerCase();
if ( addr.startsWith( "0x" ) )
{
this.address = Long.parseLong(addr.substring(2), 16);
}
else
{
this.address = Long.parseLong(addr, 10);
public Addr32(long rawaddress) {
if (rawaddress > MAX_ADDR || rawaddress < 0) {
rawaddress &= MAX_ADDR; // truncate
}
this.address = rawaddress;
}
public Addr32(String addr, int radix)
{
this.address=Long.parseLong(addr, radix);
public Addr32(String addr) {
this(Long.decode(addr).longValue());
}
final public IAddress add(BigInteger offset)
{
return new Addr32(this.address + offset.longValue());
public Addr32(String addr, int radix) {
this(Long.parseLong(addr, radix));
}
final public BigInteger getMaxOffset()
{
public IAddress add(BigInteger offset) {
return new Addr32(this.address + offset.longValue());
}
public IAddress add(long offset) {
return new Addr32(this.address + offset);
}
public BigInteger getMaxOffset() {
return MAX_OFFSET;
}
final public BigInteger distance(IAddress other)
{
return BigInteger.valueOf(address - ((Addr32)other).address);
}
final public int compareTo(IAddress addr)
{
if (address > ((Addr32)addr).address)
{
public BigInteger getValue() {
return BigInteger.valueOf(address);
}
public BigInteger distanceTo(IAddress other) {
if (!(other instanceof Addr32)) {
throw new IllegalArgumentException();
}
return BigInteger.valueOf(((Addr32)other).address - address);
}
public int compareTo(Object other) {
if (!(other instanceof Addr32)) {
throw new IllegalArgumentException();
}
if (address > ((Addr32)other).address) {
return 1;
}
if (address < ((Addr32)addr).address)
{
}
if (address < ((Addr32)other).address) {
return -1;
}
}
return 0;
}
final public boolean isMax()
{
public boolean isMax() {
return address == MAX.address;
}
final public boolean isZero()
{
public boolean isZero() {
return address == ZERO.address;
}
final public String toString()
{
return toString(10);
public String toString() {
return toString(10);
}
final public String toString(int radix)
{
public String toString(int radix) {
return Long.toString(address, radix);
}
final public boolean equals(IAddress x)
{
public boolean equals(Object x) {
if (x == this)
return true;
if (!(x instanceof Addr32))
@ -121,23 +113,23 @@ final public class Addr32 implements IAddress
return this.address == ((Addr32)x).address;
}
final public String toHexAddressString( )
{
String addressString = Long.toString(address,16);
StringBuffer sb = new StringBuffer( CHARS_NUM );
int count = DIGITS_NUM - addressString.length();
sb.append( "0x" );
for ( int i = 0; i < count ; ++i )
{
sb.append( '0' );
public int hashCode() {
return (int)(address ^ (address >> 32));
}
public String toHexAddressString() {
String addressString = Long.toString(address, 16);
StringBuffer sb = new StringBuffer(CHARS_NUM);
int count = DIGITS_NUM - addressString.length();
sb.append("0x"); //$NON-NLS-1$
for (int i = 0; i < count; ++i) {
sb.append('0');
}
sb.append( addressString );
sb.append(addressString);
return sb.toString();
}
final public int getCharsNum()
{
public int getCharsNum() {
return CHARS_NUM;
}
}
}

View file

@ -1,129 +1,130 @@
/*******************************************************************************
* Copyright (c) 2004 Intel Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
* Copyright (c) 2004 Intel Corporation and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Common Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* Intel Corporation - Initial API and implementation
*******************************************************************************/
* Contributors: Intel Corporation - Initial API and implementation
******************************************************************************/
package org.eclipse.cdt.utils;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
/*
*/
final public class Addr64 implements IAddress
{
public static final Addr64 ZERO=new Addr64("0");
public static final Addr64 MAX=new Addr64("ffffffffffffffff",16);
public class Addr64 implements IAddress {
public static final BigInteger MAX_OFFSET = new BigInteger("ffffffffffffffff",16);
public static final int BYTES_NUM = 8;
public static final int DIGITS_NUM = BYTES_NUM * 2;
public static final int CHARS_NUM = DIGITS_NUM + 2;
private BigInteger address;
public static final Addr64 ZERO = new Addr64("0"); //$NON-NLS-1$
public static final Addr64 MAX = new Addr64("ffffffffffffffff", 16); //$NON-NLS-1$
public Addr64(byte [] addrBytes)
{
if( addrBytes.length != 8)
throw(new NumberFormatException("Invalid address array"));
this.address = new BigInteger(1, addrBytes);
public static final BigInteger MAX_OFFSET = new BigInteger("ffffffffffffffff", 16); //$NON-NLS-1$
public static final int BYTES_NUM = 8;
public static final int DIGITS_NUM = BYTES_NUM * 2;
public static final int CHARS_NUM = DIGITS_NUM + 2;
private final BigInteger address;
public Addr64(byte[] addrBytes) {
address = checkAddress(new BigInteger(1, addrBytes));
}
public Addr64(BigInteger rawaddress)
{
this.address=rawaddress;
}
public Addr64(String addr)
{
public Addr64(BigInteger rawaddress) {
address = checkAddress(rawaddress);
}
public Addr64(String addr) {
addr = addr.toLowerCase();
if ( addr.startsWith( "0x" ) )
{
this.address = new BigInteger(addr.substring(2), 16);
if (addr.startsWith("0x")) { //$NON-NLS-1$
address = checkAddress(new BigInteger(addr.substring(2), 16));
} else {
address = checkAddress(new BigInteger(addr, 10));
}
else
{
this.address = new BigInteger(addr, 10);
}
}
public Addr64(String addr, int radix)
{
this.address=new BigInteger(addr, radix);
}
final public IAddress add(BigInteger offset)
{
public Addr64(String addr, int radix) {
this(new BigInteger(addr, radix));
}
private BigInteger checkAddress(BigInteger addr) {
if (addr.signum() == -1) {
throw new IllegalArgumentException("Invalid Address, must be positive value"); //$NON-NLS-1$
}
if (addr.bitLength() > 64 ) {
return addr.and(MAX.getValue()); // truncate
}
return addr;
}
public IAddress add(BigInteger offset) {
return new Addr64(this.address.add(offset));
}
final public BigInteger getMaxOffset()
{
public IAddress add(long offset) {
return new Addr64(this.address.add(BigInteger.valueOf(offset)));
}
public BigInteger getMaxOffset() {
return MAX_OFFSET;
}
final public BigInteger distance(IAddress other)
{
return address.add(((Addr64)other).address.negate());
public BigInteger distanceTo(IAddress other) {
if (! (other instanceof Addr64)) {
throw new IllegalArgumentException();
}
return ((Addr64)other).address.add(address.negate());
}
final public boolean isMax()
{
public boolean isMax() {
return address.equals(MAX);
}
final public boolean isZero()
{
public boolean isZero() {
return address.equals(ZERO);
}
final public int compareTo(IAddress addr)
{
return this.address.compareTo(((Addr64)addr).address);
public BigInteger getValue() {
return address;
}
final public boolean equals(IAddress x)
{
public int compareTo(Object other) {
return this.address.compareTo(((Addr64)other).address);
}
public boolean equals(Object x) {
if (x == this)
return true;
if (!(x instanceof Addr64))
if (! (x instanceof Addr64))
return false;
return this.address.equals(((Addr64)x).address);
}
final public String toString()
{
return toString(10);
public int hashCode() {
return address.hashCode();
}
final public String toString(int radix)
{
public String toString() {
return toString(10);
}
public String toString(int radix) {
return address.toString(radix);
}
final public String toHexAddressString( )
{
public String toHexAddressString() {
String addressString = address.toString(16);
StringBuffer sb = new StringBuffer( CHARS_NUM );
int count = DIGITS_NUM - addressString.length();
sb.append( "0x" );
for ( int i = 0; i < count; ++i )
{
sb.append( '0' );
StringBuffer sb = new StringBuffer(CHARS_NUM);
int count = DIGITS_NUM - addressString.length();
sb.append("0x"); //$NON-NLS-1$
for (int i = 0; i < count; ++i) {
sb.append('0');
}
sb.append( addressString );
sb.append(addressString);
return sb.toString();
}
final public int getCharsNum()
{
public int getCharsNum() {
return CHARS_NUM;
}
}

View file

@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.cdt.utils;
import java.math.BigInteger;
import java.util.Arrays;
import org.eclipse.cdt.core.IAddress;
@ -61,7 +60,7 @@ public abstract class BinaryObjectAdapter extends BinaryFile implements IBinaryO
}
insertion = -insertion - 1;
ISymbol symbol = syms[insertion - 1];
if (addr.compareTo(symbol.getAddress().add(BigInteger.valueOf(symbol.getSize()))) < 0) {
if (addr.compareTo(symbol.getAddress().add(symbol.getSize())) < 0) {
return syms[insertion - 1];
}
return null;

View file

@ -11,7 +11,6 @@ package org.eclipse.cdt.utils.coff.parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.List;
import org.eclipse.cdt.core.IAddress;
@ -227,7 +226,7 @@ public class CygwinPEBinaryObject extends PEBinaryObject {
}
IPath file = filename != null ? new Path(filename) : Path.EMPTY;
int startLine = symbolLoadingAddr2line.getLineNumber(addr);
int endLine = symbolLoadingAddr2line.getLineNumber(addr.add(BigInteger.valueOf(size - 1)));
int endLine = symbolLoadingAddr2line.getLineNumber(addr.add(size - 1));
list.add(new CygwinSymbol(this, name, type, addr, size, file, startLine, endLine));
} catch (IOException e) {
symbolLoadingAddr2line.dispose();

View file

@ -7,7 +7,6 @@
package org.eclipse.cdt.utils.coff.parser;
import java.io.IOException;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.utils.Addr2line;
@ -57,7 +56,7 @@ class CygwinSymbol extends Symbol {
Addr2line addr2line = ((CygwinPEBinaryObject)binary).getAddr2line(true);
if (addr2line != null) {
try {
return addr2line.getLineNumber(getAddress().add(BigInteger.valueOf(offset)));
return addr2line.getLineNumber(getAddress().add(offset));
} catch (IOException e) {
// ignore
}

View file

@ -978,8 +978,9 @@ public class Elf {
numSyms = (int)section.sh_size / (int)section.sh_entsize;
}
ArrayList symList = new ArrayList(numSyms);
for (int c = 0; c < numSyms; c++) {
efile.seek(section.sh_offset + (section.sh_entsize * c));
long offset = section.sh_offset;
for (int c = 0; c < numSyms; offset += section.sh_entsize, c++) {
efile.seek(offset);
Symbol symbol = new Symbol(section);
switch (ehdr.e_ident[ELFhdr.EI_CLASS]) {
case ELFhdr.ELFCLASS32 : {

View file

@ -11,7 +11,6 @@ package org.eclipse.cdt.utils.elf.parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.List;
import org.eclipse.cdt.core.IAddress;
@ -183,7 +182,7 @@ public class GNUElfBinaryObject extends ElfBinaryObject {
// the file.
IPath file = (filename != null && !filename.equals("??")) ? new Path(filename) : Path.EMPTY; //$NON-NLS-1$
int startLine = symbolLoadingAddr2line.getLineNumber(addr);
int endLine = symbolLoadingAddr2line.getLineNumber(addr.add(BigInteger.valueOf(size - 1)));
int endLine = symbolLoadingAddr2line.getLineNumber(addr.add(size - 1));
list.add(new GNUSymbol(this, name, type, addr, size, file, startLine, endLine));
} catch (IOException e) {
symbolLoadingAddr2line.dispose();

View file

@ -11,7 +11,6 @@
package org.eclipse.cdt.utils.elf.parser;
import java.io.IOException;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.utils.Addr2line;
@ -36,7 +35,7 @@ public class GNUSymbol extends Symbol {
Addr2line addr2line = ((GNUElfBinaryObject)binary).getAddr2line(true);
if (addr2line != null) {
try {
return addr2line.getLineNumber(getAddress().add(BigInteger.valueOf(offset)));
return addr2line.getLineNumber(getAddress().add(offset));
} catch (IOException e) {
// ignore
}

View file

@ -13,7 +13,6 @@ package org.eclipse.cdt.utils.som.parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -217,7 +216,7 @@ public class SOMBinaryObject extends BinaryObjectAdapter {
IPath file = filename != null ? new Path(filename) : Path.EMPTY;
int startLine = addr2line.getLineNumber(addr);
int endLine = addr2line.getLineNumber(addr.add(BigInteger.valueOf(size - 1)));
int endLine = addr2line.getLineNumber(addr.add(size - 1));
list.add(new SomSymbol(this, name, type, addr, size, file, startLine, endLine));
} catch (IOException e) {
addr2line = null;

View file

@ -11,7 +11,6 @@
package org.eclipse.cdt.utils.som.parser;
import java.io.IOException;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.utils.Addr2line;
@ -59,7 +58,7 @@ public class SomSymbol extends Symbol {
Addr2line addr2line = ((SOMBinaryObject)binary).getAddr2line(true);
if (addr2line != null) {
try {
return addr2line.getLineNumber(getAddress().add(BigInteger.valueOf(offset)));
return addr2line.getLineNumber(getAddress().add(offset));
} catch (IOException e) {
// ignore
}

View file

@ -11,7 +11,6 @@ package org.eclipse.cdt.utils.xcoff.parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -206,7 +205,7 @@ public class XCOFFBinaryObject extends BinaryObjectAdapter {
IPath file = filename != null ? new Path(filename) : Path.EMPTY;
int startLine = addr2line.getLineNumber(addr);
int endLine = addr2line.getLineNumber(addr.add(BigInteger.valueOf(size - 1)));
int endLine = addr2line.getLineNumber(addr.add(size - 1));
list.add(new XCoffSymbol(this, name, type, addr, size, file, startLine, endLine));
} catch (IOException e) {
addr2line = null;

View file

@ -7,7 +7,6 @@
package org.eclipse.cdt.utils.xcoff.parser;
import java.io.IOException;
import java.math.BigInteger;
import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.utils.Addr2line;
@ -58,7 +57,7 @@ public class XCoffSymbol extends Symbol {
Addr2line addr2line = ((XCOFFBinaryObject)binary).getAddr2line(true);
if (addr2line != null) {
try {
return addr2line.getLineNumber(getAddress().add(BigInteger.valueOf(offset)));
return addr2line.getLineNumber(getAddress().add(offset));
} catch (IOException e) {
// ignore
}