Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] Head[applied]: more work done on GNU Elf Binary Parser

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/ChangeLog,v
retrieving revision 1.160
diff -u -r1.160 ChangeLog
--- ChangeLog	6 Oct 2003 20:16:38 -0000	1.160
+++ ChangeLog	8 Oct 2003 18:55:41 -0000
@@ -1,3 +1,15 @@
+2003-10-07 Alain Magloire
+
+	More work on the GNU Elf Binary parser.
+	Move some calls that extends the Addr2line class.
+
+	* utils/org/eclipse/cdt/utils/elf/BinaryFile.java
+	* utils/org/eclipse/cdt/utils/elf/BinaryObject.java
+	* utils/org/eclipse/cdt/utils/elf/BinaryArchive.java
+	* utils/org/eclipse/cdt/utils/elf/ElfParser.java
+	* utils/org/eclipse/cdt/utils/elf/GNUElfParser.java
+	* utils/org/eclipse/cdt/utils/Addr2line.java
+
 2003-10-06 Alain Magloire
 
 	Implementation of the GNU Elf parser, where you can
Index: utils/org/eclipse/cdt/utils/Addr2line.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Addr2line.java,v
retrieving revision 1.2
diff -u -r1.2 Addr2line.java
--- utils/org/eclipse/cdt/utils/Addr2line.java	6 Oct 2003 20:16:29 -0000	1.2
+++ utils/org/eclipse/cdt/utils/Addr2line.java	8 Oct 2003 18:55:41 -0000
@@ -50,14 +50,58 @@
 		return lastsymbol;
 	}	
 
+	/**
+	 * The format of the output:
+	 *  addr2line -C -f -e hello
+	 *  08048442
+	 *  main
+	 *  hello.c:39
+	 */
+	public String getFileName(long address) throws IOException {
+		String filename = null;
+		String line = getLine(address);
+		int index1, index2;
+		if (line != null && (index1 = line.lastIndexOf(':')) != -1) {
+			// we do this because addr2line on win produces
+			// <cygdrive/pathtoexc/C:/pathtofile:##>
+			index2 = line.indexOf(':');
+			if (index1 == index2) {
+				index2 = 0;
+			} else {
+				index2--;
+			}
+			filename = line.substring(index2, index1);
+		}
+		return filename;
+	}
+
+	/**
+	 * The format of the output:
+	 *  addr2line -C -f -e hello
+	 *  08048442
+	 *  main
+	 *  hello.c:39
+	 */
+	public int getLineNumber(long address) throws IOException {
+		int lineno = -1;
+		String line = getLine(address);
+		int colon;
+		if (line != null && (colon = line.lastIndexOf(':')) != -1) {
+			try {
+				lineno = Integer.parseInt(line.substring(colon + 1));
+				lineno = (lineno == 0) ? -1 : lineno;
+			} catch(Exception e) {
+			}
+		}
+		return lineno;
+	}
+
 	public void dispose() {
 		try {
-			//stdin.write(-1);
 			stdout.close();
 			stdin.close();
 			addr2line.getErrorStream().close();		
-		}
-		catch (IOException e) {
+		} catch (IOException e) {
 		}
 		addr2line.destroy();
 	}
Index: utils/org/eclipse/cdt/utils/elf/parser/ARMember.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/ARMember.java,v
retrieving revision 1.1
diff -u -r1.1 ARMember.java
--- utils/org/eclipse/cdt/utils/elf/parser/ARMember.java	17 Sep 2003 02:11:05 -0000	1.1
+++ utils/org/eclipse/cdt/utils/elf/parser/ARMember.java	8 Oct 2003 18:55:41 -0000
@@ -1,10 +1,15 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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: 
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
 
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -60,7 +65,7 @@
 
 	protected void addSymbols(Elf.Symbol[] array, int type) {
 		for (int i = 0; i < array.length; i++) {
-			Symbol sym = new Symbol();
+			Symbol sym = new Symbol(this);
 			sym.type = type;
 			sym.name = array[i].toString();
 			sym.addr = array[i].st_value;
Index: utils/org/eclipse/cdt/utils/elf/parser/BinaryArchive.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/BinaryArchive.java,v
retrieving revision 1.2
diff -u -r1.2 BinaryArchive.java
--- utils/org/eclipse/cdt/utils/elf/parser/BinaryArchive.java	6 Oct 2003 20:16:20 -0000	1.2
+++ utils/org/eclipse/cdt/utils/elf/parser/BinaryArchive.java	8 Oct 2003 18:55:41 -0000
@@ -1,9 +1,14 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
-
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
Index: utils/org/eclipse/cdt/utils/elf/parser/BinaryExecutable.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/BinaryExecutable.java,v
retrieving revision 1.1
diff -u -r1.1 BinaryExecutable.java
--- utils/org/eclipse/cdt/utils/elf/parser/BinaryExecutable.java	17 Sep 2003 02:11:05 -0000	1.1
+++ utils/org/eclipse/cdt/utils/elf/parser/BinaryExecutable.java	8 Oct 2003 18:55:41 -0000
@@ -1,9 +1,14 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
-
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 
 import java.io.IOException;
 import java.util.ArrayList;
Index: utils/org/eclipse/cdt/utils/elf/parser/BinaryFile.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/BinaryFile.java,v
retrieving revision 1.2
diff -u -r1.2 BinaryFile.java
--- utils/org/eclipse/cdt/utils/elf/parser/BinaryFile.java	6 Oct 2003 20:16:20 -0000	1.2
+++ utils/org/eclipse/cdt/utils/elf/parser/BinaryFile.java	8 Oct 2003 18:55:41 -0000
@@ -1,9 +1,14 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
-
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 
 import java.io.ByteArrayInputStream;
 import java.io.FileInputStream;
Index: utils/org/eclipse/cdt/utils/elf/parser/BinaryObject.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/BinaryObject.java,v
retrieving revision 1.2
diff -u -r1.2 BinaryObject.java
--- utils/org/eclipse/cdt/utils/elf/parser/BinaryObject.java	6 Oct 2003 20:16:20 -0000	1.2
+++ utils/org/eclipse/cdt/utils/elf/parser/BinaryObject.java	8 Oct 2003 18:55:41 -0000
@@ -1,7 +1,13 @@
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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: 
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
 
 import java.io.FileInputStream;
@@ -258,7 +264,7 @@
 
 	protected void addSymbols(Elf.Symbol[] array, int type, Addr2line addr2line, CPPFilt cppfilt) {
 		for (int i = 0; i < array.length; i++) {
-			Symbol sym = new Symbol();
+			Symbol sym = new Symbol(this);
 			sym.type = type;
 			sym.name = array[i].toString();
 			if (cppfilt != null) {
@@ -268,55 +274,16 @@
 				}
 			}
 			sym.addr = array[i].st_value;
-			try {
-				// This can fail if we use addr2line
-				// but we can safely ignore the error.
-				long value = sym.addr;
-				int lineno = -1;
-				String filename = null; 
-				if (addr2line != null) {
-					// We try to get the nearest match
-					// since the symbol may not exactly align with debug info.
-					// In C line number 0 is invalid, line starts at 1 for file, we use
-					// this for validation.
-					String line = null;
-					for (int j = 0; j <= 20; j += 4, value += j) {
-						line = addr2line.getLine(value);
-						if (line != null) {
-							int colon = line.lastIndexOf(':');
-							if (colon != -1) {
-								String number = line.substring(colon + 1);
-								if (!number.startsWith("0")) {
-									break; // potential candidate bail out
-								}
-							}
-						}
-					}
-
-					int index1, index2;
-					if (line != null && (index1 = line.lastIndexOf(':')) != -1) {
-						// we do this because addr2line on win produces
-						// <cygdrive/pathtoexc/C:/pathtofile:##>
-						index2 = line.indexOf(':');
-						if ( index1 == index2 ) {
-							index2 = 0;
-						} else {
-							index2--;
-						}
-						filename = line.substring(index2, index1);
-						try {
-							lineno = Integer.parseInt(line.substring(index1 + 1));
-							lineno = (lineno == 0) ? -1 : lineno;
-						} catch(Exception e) {
-							lineno = -1;
-						}
-					}
+			sym.filename = null;
+			sym.startLine =  -1;
+			sym.endLine = sym.startLine;
+			if (addr2line != null) {
+				try {
+					sym.filename =  addr2line.getFileName(sym.addr);
+					sym.startLine = addr2line.getLineNumber(sym.addr);
+					sym.endLine = addr2line.getLineNumber(sym.addr + array[i].st_size - 1);
+				} catch (IOException e) {
 				}
-				sym.filename =  filename;
-				sym.startLine = lineno;
-				sym.endLine = sym.startLine;
-			} catch (IOException e) {
-				//e.printStackTrace();
 			}
 			addSymbol(sym);
 		}
Index: utils/org/eclipse/cdt/utils/elf/parser/BinaryShared.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/BinaryShared.java,v
retrieving revision 1.1
diff -u -r1.1 BinaryShared.java
--- utils/org/eclipse/cdt/utils/elf/parser/BinaryShared.java	17 Sep 2003 02:11:05 -0000	1.1
+++ utils/org/eclipse/cdt/utils/elf/parser/BinaryShared.java	8 Oct 2003 18:55:41 -0000
@@ -1,9 +1,14 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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:
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
-
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
 
 import java.io.IOException;
 
Index: utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java
===================================================================
RCS file: utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java
diff -N utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ utils/org/eclipse/cdt/utils/elf/parser/ElfParser.java	8 Oct 2003 18:55:41 -0000
@@ -0,0 +1,70 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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: 
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
+package org.eclipse.cdt.utils.elf.parser;
+ 
+import java.io.IOException;
+
+import org.eclipse.cdt.core.AbstractCExtension;
+import org.eclipse.cdt.core.IBinaryParser;
+import org.eclipse.cdt.utils.elf.Elf;
+import org.eclipse.cdt.utils.elf.Elf.Attribute;
+import org.eclipse.core.runtime.IPath;
+
+/**
+ */
+public class ElfParser extends AbstractCExtension implements IBinaryParser {
+
+	/**
+	 * @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IPath)
+	 */
+	public IBinaryFile getBinary(IPath path) throws IOException {
+		if (path == null) {
+			throw new IOException("path is null");
+		}
+
+		BinaryFile binary = null;
+		try {
+			Elf.Attribute attribute = Elf.getAttributes(path.toOSString());
+			if (attribute != null) {
+				switch (attribute.getType()) {
+					case Attribute.ELF_TYPE_EXE :
+						binary = new BinaryExecutable(path);
+						break;
+
+					case Attribute.ELF_TYPE_SHLIB :
+						binary = new BinaryShared(path);
+						break;
+
+					case Attribute.ELF_TYPE_OBJ :
+						binary = new BinaryObject(path);
+						break;
+
+					case Attribute.ELF_TYPE_CORE :
+						BinaryObject obj = new BinaryObject(path);
+						obj.setType(IBinaryFile.CORE);
+						binary = obj;
+						break;
+				}
+			}
+		} catch (IOException e) {
+			binary = new BinaryArchive(path);
+		}
+		return binary;
+	}
+
+	/**
+	 * @see org.eclipse.cdt.core.model.IBinaryParser#getFormat()
+	 */
+	public String getFormat() {
+		return "ELF";
+	}
+
+}
Index: utils/org/eclipse/cdt/utils/elf/parser/GNUElfParser.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/GNUElfParser.java,v
retrieving revision 1.2
diff -u -r1.2 GNUElfParser.java
--- utils/org/eclipse/cdt/utils/elf/parser/GNUElfParser.java	6 Oct 2003 20:16:20 -0000	1.2
+++ utils/org/eclipse/cdt/utils/elf/parser/GNUElfParser.java	8 Oct 2003 18:55:41 -0000
@@ -1,61 +1,35 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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: 
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
 
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
- 
 import java.io.IOException;
 
-import org.eclipse.cdt.core.AbstractCExtension;
 import org.eclipse.cdt.core.IBinaryParser;
 import org.eclipse.cdt.core.ICExtensionReference;
-import org.eclipse.cdt.utils.elf.Elf;
-import org.eclipse.cdt.utils.elf.Elf.Attribute;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 
 /**
  */
-public class GNUElfParser extends AbstractCExtension implements IBinaryParser {
+public class GNUElfParser extends ElfParser implements IBinaryParser {
 
 	/**
 	 * @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IPath)
 	 */
 	public IBinaryFile getBinary(IPath path) throws IOException {
-		if (path == null) {
-			throw new IOException("path is null");
-		}
-
-		BinaryFile binary = null;
-		try {
-			Elf.Attribute attribute = Elf.getAttributes(path.toOSString());
-			if (attribute != null) {
-				switch (attribute.getType()) {
-					case Attribute.ELF_TYPE_EXE :
-						binary = new BinaryExecutable(path);
-						break;
-
-					case Attribute.ELF_TYPE_SHLIB :
-						binary = new BinaryShared(path);
-						break;
-
-					case Attribute.ELF_TYPE_OBJ :
-						binary = new BinaryObject(path);
-						break;
-
-					case Attribute.ELF_TYPE_CORE :
-						BinaryObject obj = new BinaryObject(path);
-						obj.setType(IBinaryFile.CORE);
-						binary = obj;
-						break;
-				}
-			}
-		} catch (IOException e) {
-			binary = new BinaryArchive(path);
+		IBinaryFile binary = super.getBinary(path);
+		if (binary instanceof BinaryFile) {
+			((BinaryFile)binary).setAddr2LinePath(getAddr2LinePath());
+			((BinaryFile)binary).setCPPFiltPath(getCPPFiltPath());
 		}
-		binary.setAddr2LinePath(getAddr2LinePath());
-		binary.setCPPFiltPath(getCPPFiltPath());
 		return binary;
 	}
 
@@ -63,23 +37,23 @@
 	 * @see org.eclipse.cdt.core.model.IBinaryParser#getFormat()
 	 */
 	public String getFormat() {
-		return "ELF";
+		return "GNU ELF";
 	}
 
 	public IPath getAddr2LinePath() {
 		ICExtensionReference ref = getExtensionReference();
-		String value =  ref.getExtensionData("addr2line");
+		String value =  ref.getExtensionData("addr2line"); //$NON-NLS-1
 		if (value == null || value.length() == 0) {
-			value = "addr2line";
+			value = "addr2line"; //$NON-NLS-1
 		}
 		return new Path(value);
 	}
 
 	public IPath getCPPFiltPath() {
 		ICExtensionReference ref = getExtensionReference();
-		String value = ref.getExtensionData("c++filt");
+		String value = ref.getExtensionData("c++filt"); //$NON-NLS-1
 		if (value == null || value.length() == 0) {
-			value = "c++filt";
+			value = "c++filt"; //$NON-NLS-1
 		}
 		return new Path(value);
 	}
Index: utils/org/eclipse/cdt/utils/elf/parser/Symbol.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/elf/parser/Symbol.java,v
retrieving revision 1.1
diff -u -r1.1 Symbol.java
--- utils/org/eclipse/cdt/utils/elf/parser/Symbol.java	17 Sep 2003 02:11:05 -0000	1.1
+++ utils/org/eclipse/cdt/utils/elf/parser/Symbol.java	8 Oct 2003 18:55:41 -0000
@@ -1,14 +1,24 @@
+/**********************************************************************
+ * Copyright (c) 2002,2003 QNX Software Systems 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: 
+ * QNX Software Systems - Initial API and implementation
+***********************************************************************/
 package org.eclipse.cdt.utils.elf.parser;
 
-import org.eclipse.cdt.core.IBinaryParser.ISymbol;
+import java.io.IOException;
 
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
+import org.eclipse.cdt.core.IBinaryParser.ISymbol;
+import org.eclipse.cdt.utils.Addr2line;
 
 public class Symbol implements ISymbol {
 
+	BinaryObject binary;
+
 	public String filename;
 	public int startLine;
 	public int endLine;
@@ -16,6 +26,9 @@
 	public String name;
 	public int type;
 
+	public Symbol(BinaryObject bin) {
+		binary = bin;		
+	}
 	/**
 	 * @see org.eclipse.cdt.core.model.IBinaryParser.ISymbol#getFilename()
 	 */
@@ -57,6 +70,22 @@
 	 */
 	public int getStartLine() {
 		return startLine;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.cdt.core.IBinaryParser.ISymbol#getLineNumber(long)
+	 */
+	public int getLineNumber(long offset) {
+		int line = -1;
+		try {
+			Addr2line addr2line = binary.getAddr2Line();
+			if (addr2line != null) {
+				line = addr2line.getLineNumber(addr + offset);
+				addr2line.dispose();
+			}
+		} catch (IOException e) {		
+		}
+		return line;
 	}
 
 }



Back to the top