1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 09:16:02 +02:00

New impelementation of PE parser.

This commit is contained in:
Alain Magloire 2003-10-19 01:14:19 +00:00
parent d06d62c1c2
commit 3736b2c199
9 changed files with 875 additions and 0 deletions

View file

@ -0,0 +1,88 @@
/**********************************************************************
* 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.coff.parser;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.cdt.core.IBinaryParser.ISymbol;
import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.coff.Coff;
import org.eclipse.cdt.utils.coff.PE;
import org.eclipse.cdt.utils.coff.PEArchive;
import org.eclipse.core.runtime.IPath;
/**
*/
public class ARMember extends BinaryObject {
PEArchive.ARHeader header;
public ARMember(IPath p, PEArchive.ARHeader h) throws IOException {
super(p);
header = h;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getContents()
*/
public InputStream getContents() {
InputStream stream = null;
if (path != null && header != null) {
try {
stream = new ByteArrayInputStream(header.getObjectData());
} catch (IOException e) {
}
}
if (stream == null) {
stream = super.getContents();
}
return stream;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getName()
*/
public String getName() {
if (header != null) {
return header.getObjectName();
}
return "";
}
protected PE getPE() throws IOException {
if (header != null) {
return header.getPE();
}
throw new IOException("No file assiocated with Binary");
}
protected void addSymbols(Coff.Symbol[] peSyms, byte[] table, Addr2line addr2line, CPPFilt cppfilt, CygPath cypath) {
for (int i = 0; i < peSyms.length; i++) {
if (peSyms[i].isFunction() || peSyms[i].isPointer() ||peSyms[i].isArray()) {
String name = peSyms[i].getName(table);
if (name == null || name.trim().length() == 0 ||
!Character.isJavaIdentifierStart(name.charAt(0))) {
continue;
}
Symbol sym = new Symbol(this);
sym.type = peSyms[i].isFunction() ? ISymbol.FUNCTION : ISymbol.VARIABLE;
sym.name = name;
sym.addr = peSyms[i].n_value;
addSymbol(sym);
}
}
}
}

View file

@ -0,0 +1,88 @@
package org.eclipse.cdt.utils.coff.parser;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import java.io.IOException;
import java.util.ArrayList;
import org.eclipse.cdt.core.IBinaryParser.IBinaryArchive;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
import org.eclipse.cdt.utils.coff.PEArchive;
import org.eclipse.cdt.utils.coff.PE.Attribute;
import org.eclipse.core.runtime.IPath;
/**
*/
public class BinaryArchive extends BinaryFile implements IBinaryArchive {
ArrayList children;
public BinaryArchive(IPath p) throws IOException {
super(p);
new PEArchive(p.toOSString()).dispose(); // check file type
children = new ArrayList(5);
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryArchive#getObjects()
*/
public IBinaryObject[] getObjects() {
if (hasChanged()) {
children.clear();
PEArchive ar = null;
try {
ar = new PEArchive(getPath().toOSString());
PEArchive.ARHeader[] headers = ar.getHeaders();
for (int i = 0; i < headers.length; i++) {
IBinaryObject bin = new ARMember(path, headers[i]);
children.add(bin);
}
} catch (IOException e) {
//e.printStackTrace();
}
if (ar != null) {
ar.dispose();
}
children.trimToSize();
}
return (IBinaryObject[])children.toArray(new IBinaryObject[0]);
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile()
*/
public IPath getPath() {
return path;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getType()
*/
public int getType() {
return IBinaryFile.ARCHIVE;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryArchive#add(IBinaryObject[])
*/
public void add(IBinaryObject[] objs) throws IOException {
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryArchive#delete(IBinaryObject[])
*/
public void delete(IBinaryObject[] objs) throws IOException {
}
/* (non-Javadoc)
* @see org.eclipse.cdt.utils.coff.parser.BinaryFile#getAttribute()
*/
protected Attribute getAttribute() {
return null;
}
}

View file

@ -0,0 +1,41 @@
/**********************************************************************
* 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.coff.parser;
import java.io.IOException;
import org.eclipse.cdt.core.IBinaryParser.IBinaryExecutable;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.core.runtime.IPath;
/**
*/
public class BinaryExecutable extends BinaryObject implements IBinaryExecutable {
public BinaryExecutable(IPath path) throws IOException {
super(path);
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryExecutable#getNeededSharedLibs()
*/
public String[] getNeededSharedLibs() {
return new String[0];
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getType()
*/
public int getType() {
return IBinaryFile.EXECUTABLE;
}
}

View file

@ -0,0 +1,103 @@
/**********************************************************************
* 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.coff.parser;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.ICygwinToolsProvider;
import org.eclipse.cdt.utils.coff.PE.Attribute;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.PlatformObject;
/**
*
*/
public abstract class BinaryFile extends PlatformObject implements IBinaryFile {
protected IPath path;
protected ICygwinToolsProvider toolsProvider;
protected long timestamp;
public BinaryFile(IPath p) {
path = p;
}
public void setToolsProvider(ICygwinToolsProvider p) {
toolsProvider = p;
}
public Addr2line getAddr2Line() {
if (toolsProvider != null)
return toolsProvider.getAddr2Line(path);
return null;
}
public CPPFilt getCPPFilt() {
if (toolsProvider != null)
return toolsProvider.getCPPFilt();
return null;
}
public CygPath getCygPath() {
if (toolsProvider != null)
return toolsProvider.getCygPath();
return null;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getFile()
*/
public IPath getPath() {
return path;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getType()
*/
public abstract int getType();
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getContents()
*/
public InputStream getContents() {
InputStream stream = null;
if (path != null) {
try {
stream = new FileInputStream(path.toFile());
} catch (IOException e) {
}
}
if (stream == null) {
stream = new ByteArrayInputStream(new byte[0]);
}
return stream;
}
/**
* @return
*/
protected abstract Attribute getAttribute();
protected boolean hasChanged() {
long modification = getPath().toFile().lastModified();
boolean changed = modification != timestamp;
timestamp = modification;
return changed;
}
}

View file

@ -0,0 +1,232 @@
package org.eclipse.cdt.utils.coff.parser;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import java.io.IOException;
import java.util.ArrayList;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
import org.eclipse.cdt.core.IBinaryParser.ISymbol;
import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.coff.Coff;
import org.eclipse.cdt.utils.coff.PE;
import org.eclipse.cdt.utils.coff.PE.Attribute;
import org.eclipse.core.runtime.IPath;
/**
*/
public class BinaryObject extends BinaryFile implements IBinaryObject {
PE.Attribute attribute;
ArrayList symbols;
int type = IBinaryFile.OBJECT;
public BinaryObject(IPath p) throws IOException {
super(p);
loadInformation();
hasChanged();
}
public void setType(int t) {
type = t;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getType()
*/
public int getType() {
return type;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getBSS()
*/
public long getBSS() {
return 0;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getCPU()
*/
public String getCPU() {
Attribute attr = getAttribute();
if (attr != null) {
return attribute.getCPU();
}
return "";
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getData()
*/
public long getData() {
return 0;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getName()
*/
public String getName() {
return getPath().lastSegment().toString();
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getSymbols()
*/
public ISymbol[] getSymbols() {
if (hasChanged() || symbols == null) {
if (symbols == null) {
symbols = new ArrayList(5);
}
try {
loadInformation();
} catch (IOException e) {
}
}
return (ISymbol[])symbols.toArray(new ISymbol[0]);
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#getText()
*/
public long getText() {
return 0;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#hasDebug()
*/
public boolean hasDebug() {
Attribute attr = getAttribute();
if (attr != null) {
return attr.hasDebug();
}
return false;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryObject#isLittleEndian()
*/
public boolean isLittleEndian() {
Attribute attr = getAttribute();
if (attr != null) {
return attr.isLittleEndian();
}
return false;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryExecutable#getNeededSharedLibs()
*/
public String[] getNeededSharedLibs() {
return new String[0];
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryShared#getSoName()
*/
public String getSoName() {
return "";
}
protected PE getPE() throws IOException {
return new PE(getPath().toOSString());
}
protected PE.Attribute getAttribute() {
if (hasChanged()) {
try {
loadInformation();
} catch (IOException e) {
}
}
return attribute;
}
protected void loadInformation() throws IOException {
PE pe = getPE();
loadInformation(pe);
pe.dispose();
}
private void loadInformation(PE pe) throws IOException {
loadAttribute(pe);
if (symbols != null) {
symbols.clear();
loadSymbols(pe);
symbols.trimToSize();
}
}
private void loadAttribute(PE pe) throws IOException {
attribute = pe.getAttribute();
}
private void loadSymbols(PE pe) throws IOException {
Addr2line addr2line = getAddr2Line();
CPPFilt cppfilt = getCPPFilt();
CygPath cygpath = getCygPath();
Coff.Symbol[] peSyms = pe.getSymbols();
byte[] table = pe.getStringTable();
addSymbols(peSyms, table, addr2line, cppfilt, cygpath);
if (addr2line != null) {
addr2line.dispose();
}
if (cppfilt != null) {
cppfilt.dispose();
}
if (cygpath != null) {
cygpath.dispose();
}
symbols.trimToSize();
}
protected void addSymbols(Coff.Symbol[] peSyms, byte[] table, Addr2line addr2line, CPPFilt cppfilt, CygPath cygpath) {
for (int i = 0; i < peSyms.length; i++) {
if (peSyms[i].isFunction() || peSyms[i].isPointer() ||peSyms[i].isArray()) {
String name = peSyms[i].getName(table);
if (name == null || name.trim().length() == 0 ||
!Character.isJavaIdentifierStart(name.charAt(0))) {
continue;
}
Symbol sym = new Symbol(this);
sym.type = peSyms[i].isFunction() ? ISymbol.FUNCTION : ISymbol.VARIABLE;
sym.addr = peSyms[i].n_value;
sym.name = name;
if (cppfilt != null) {
try {
sym.name = cppfilt.getFunction(sym.name);
} catch (IOException e1) {
}
}
sym.filename = null;
sym.startLine = 0;
sym.endLine = 0;
if (addr2line != null) {
try {
sym.filename = addr2line.getFileName(sym.addr);
if (cygpath != null)
sym.filename = cygpath.getFileName(sym.filename);
sym.startLine = addr2line.getLineNumber(sym.addr);
} catch (IOException e) {
}
}
addSymbol(sym);
}
}
}
protected void addSymbol(Symbol sym) {
symbols.add(sym);
}
}

View file

@ -0,0 +1,51 @@
/**********************************************************************
* 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.coff.parser;
import java.io.IOException;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
import org.eclipse.cdt.core.IBinaryParser.IBinaryShared;
import org.eclipse.core.runtime.IPath;
/**
*/
public class BinaryShared extends BinaryExecutable implements IBinaryShared {
String soname;
public BinaryShared(IPath path) throws IOException {
super(path);
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryShared#getSoName()
*/
public String getSoName() {
if (hasChanged()) {
try {
loadInformation();
} catch (IOException e) {
}
}
if (soname != null) {
return soname;
}
return "";
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.IBinaryFile#getType()
*/
public int getType() {
return IBinaryFile.SHARED;
}
}

View file

@ -0,0 +1,108 @@
/**********************************************************************
* 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.coff.parser;
import java.io.IOException;
import org.eclipse.cdt.core.IBinaryParser;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.cdt.utils.Addr2line;
import org.eclipse.cdt.utils.CPPFilt;
import org.eclipse.cdt.utils.CygPath;
import org.eclipse.cdt.utils.ICygwinToolsProvider;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/**
*/
public class CygwinPEParser extends PEParser implements IBinaryParser, ICygwinToolsProvider {
/**
* @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IPath)
*/
public IBinaryFile getBinary(IPath path) throws IOException {
IBinaryFile binary = super.getBinary(path);
if (binary instanceof BinaryFile) {
((BinaryFile)binary).setToolsProvider(this);
}
return binary;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser#getFormat()
*/
public String getFormat() {
return "Cygwin PE";
}
public IPath getAddr2LinePath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("addr2line"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "addr2line"; //$NON-NLS-1
}
return new Path(value);
}
public IPath getCPPFiltPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("c++filt"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "c++filt"; //$NON-NLS-1
}
return new Path(value);
}
public IPath getCygPathPath() {
ICExtensionReference ref = getExtensionReference();
String value = ref.getExtensionData("cygpath"); //$NON-NLS-1
if (value == null || value.length() == 0) {
value = "cygpath"; //$NON-NLS-1
}
return new Path(value);
}
public Addr2line getAddr2Line(IPath execFile) {
IPath addr2LinePath = getAddr2LinePath();
Addr2line addr2line = null;
if (addr2LinePath != null && !addr2LinePath.isEmpty()) {
try {
addr2line = new Addr2line(addr2LinePath.toOSString(), execFile.toOSString());
} catch (IOException e1) {
}
}
return addr2line;
}
public CPPFilt getCPPFilt() {
IPath cppFiltPath = getCPPFiltPath();
CPPFilt cppfilt = null;
if (cppFiltPath != null && ! cppFiltPath.isEmpty()) {
try {
cppfilt = new CPPFilt(cppFiltPath.toOSString());
} catch (IOException e2) {
}
}
return cppfilt;
}
public CygPath getCygPath() {
IPath cygPathPath = getCygPathPath();
CygPath cygpath = null;
if (cygPathPath != null && !cygPathPath.isEmpty()) {
try {
cygpath = new CygPath(cygPathPath.toOSString());
} catch (IOException e1) {
}
}
return cygpath;
}
}

View file

@ -0,0 +1,73 @@
/**********************************************************************
* 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.coff.parser;
import java.io.IOException;
import org.eclipse.cdt.core.AbstractCExtension;
import org.eclipse.cdt.core.IBinaryParser;
import org.eclipse.cdt.utils.coff.PE;
import org.eclipse.cdt.utils.coff.PE.Attribute;
import org.eclipse.core.runtime.IPath;
/**
*/
public class PEParser extends AbstractCExtension implements IBinaryParser {
/**
* @see org.eclipse.cdt.core.model.IBinaryParser#getBinary(IFile)
*/
public IBinaryFile getBinary(IPath path) throws IOException {
if (path == null) {
throw new IOException("path is null");
}
BinaryFile binary = null;
try {
PE.Attribute attribute = PE.getAttributes(path.toOSString());
if (attribute != null) {
switch (attribute.getType()) {
case Attribute.PE_TYPE_EXE :
binary = new BinaryExecutable(path);
break;
case Attribute.PE_TYPE_SHLIB :
binary = new BinaryShared(path);
break;
case Attribute.PE_TYPE_OBJ :
binary = new BinaryObject(path);
break;
case Attribute.PE_TYPE_CORE :
BinaryObject obj = new BinaryObject(path);
obj.setType(IBinaryFile.CORE);
binary = obj;
break;
}
}
} catch (IOException e) {
// Is it an Archive?
binary = new BinaryArchive(path);
}
return binary;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser#getFormat()
*/
public String getFormat() {
return "PE";
}
}

View file

@ -0,0 +1,91 @@
/**********************************************************************
* 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.coff.parser;
import java.io.IOException;
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;
public long addr;
public String name;
public int type;
public Symbol(BinaryObject bin) {
binary = bin;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.ISymbol#getFilename()
*/
public String getFilename() {
return filename;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.ISymbol#getName()
*/
public String getName() {
return name;
}
/**
* @see org.eclipse.cdt.core.model.IBinaryParser.ISymbol#getType()
*/
public int getType() {
return type;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.IBinaryParser.ISymbol#getAdress()
*/
public long getAddress() {
return addr;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.IBinaryParser.ISymbol#getEndLine()
*/
public int getEndLine() {
return endLine;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.IBinaryParser.ISymbol#getStartLine()
*/
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;
}
}