mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
More work done to clean the GNU Elf Binary Parser
This commit is contained in:
parent
943f1dbc94
commit
4a143b03a8
10 changed files with 242 additions and 133 deletions
|
@ -50,14 +50,58 @@ public class Addr2line {
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -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 @@ public class ARMember extends BinaryObject {
|
|||
|
||||
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;
|
||||
|
|
|
@ -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.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
|
|
@ -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.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
|
|
@ -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.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
|
|
@ -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 @@ public class BinaryObject extends BinaryFile implements IBinaryObject {
|
|||
|
||||
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 @@ public class BinaryObject extends BinaryFile implements IBinaryObject {
|
|||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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.IOException;
|
||||
|
||||
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
IBinaryFile binary = super.getBinary(path);
|
||||
if (binary instanceof BinaryFile) {
|
||||
((BinaryFile)binary).setAddr2LinePath(getAddr2LinePath());
|
||||
((BinaryFile)binary).setCPPFiltPath(getCPPFiltPath());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
binary.setAddr2LinePath(getAddr2LinePath());
|
||||
binary.setCPPFiltPath(getCPPFiltPath());
|
||||
return binary;
|
||||
}
|
||||
|
||||
|
@ -63,23 +37,23 @@ public class GNUElfParser extends AbstractCExtension implements IBinaryParser {
|
|||
* @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);
|
||||
}
|
||||
|
|
|
@ -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 class Symbol implements ISymbol {
|
|||
public String name;
|
||||
public int type;
|
||||
|
||||
public Symbol(BinaryObject bin) {
|
||||
binary = bin;
|
||||
}
|
||||
/**
|
||||
* @see org.eclipse.cdt.core.model.IBinaryParser.ISymbol#getFilename()
|
||||
*/
|
||||
|
@ -59,4 +72,20 @@ public class Symbol implements ISymbol {
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue