1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

fixed bug with mips dyn section reading

new method
cleanup
This commit is contained in:
David Inglis 2003-05-28 12:59:08 +00:00
parent 1ce9ba9ea0
commit 8ab6e1bb4c
3 changed files with 360 additions and 419 deletions

View file

@ -1,3 +1,12 @@
2003-05-29 David Inglis
* utils/org/eclipse/cdt/utils/elf/Elf.java
fixed toString buf for Sestion.
added findSesctionByName() method
fixed bug where reading DYN section would fail for mips
* utils/org/eclipse/cdt/utils/elf/ElfHelper.java
use new findSectionByName.
cleaup
2003-04-29 Alain Magloire 2003-04-29 Alain Magloire
* model/org/eclipse/cdt/internal/core/model/parser/PEParser.java (getBinary): * model/org/eclipse/cdt/internal/core/model/parser/PEParser.java (getBinary):

View file

@ -139,6 +139,8 @@ public class Elf {
public final static int SHT_SHLIB = 10; public final static int SHT_SHLIB = 10;
public final static int SHT_DYNSYM = 11; public final static int SHT_DYNSYM = 11;
public final static int SHT_LOPROC = 0x70000000;
/* sh_flags */ /* sh_flags */
public final static int SHF_WRITE = 1; public final static int SHF_WRITE = 1;
public final static int SHF_ALLOC = 2; public final static int SHF_ALLOC = 2;
@ -173,7 +175,7 @@ public class Elf {
efile.read(section_strtab); efile.read(section_strtab);
} }
int str_size = 0; int str_size = 0;
if ( sh_name > sh_size || ( sh_name + str_size + 1) > section_strtab.length) { if ( sh_name > section_strtab.length) {
return EMPTY_STRING; return EMPTY_STRING;
} }
while( section_strtab[(int)sh_name + str_size] != 0) while( section_strtab[(int)sh_name + str_size] != 0)
@ -483,6 +485,8 @@ public class Elf {
} }
public class Dynamic { public class Dynamic {
public final static int DYN_ENT_SIZE = 8;
public final static int DT_NULL = 0; public final static int DT_NULL = 0;
public final static int DT_NEEDED = 1; public final static int DT_NEEDED = 1;
public final static int DT_PLTRELSZ = 2; public final static int DT_PLTRELSZ = 2;
@ -501,7 +505,6 @@ public class Elf {
public final static int DT_RPATH = 15; public final static int DT_RPATH = 15;
public long d_tag; public long d_tag;
public long d_val; public long d_val;
private Section section; private Section section;
private String name; private String name;
@ -538,16 +541,15 @@ public class Elf {
} }
ArrayList dynList = new ArrayList(); ArrayList dynList = new ArrayList();
efile.seek(section.sh_offset); efile.seek(section.sh_offset);
if (section.sh_entsize == 0) { int off = 0;
Dynamic dynEnt = new Dynamic(section, efile.readIntE(), efile.readIntE()); // We must assume the section is a table ignoring the sh_entsize as it is not
dynList.add(dynEnt); // set for MIPS.
} else { while( off < section.sh_size ) {
for (int i = 0; i < section.sh_size / section.sh_entsize; i++ ) {
Dynamic dynEnt = new Dynamic(section, efile.readIntE(), efile.readIntE()); Dynamic dynEnt = new Dynamic(section, efile.readIntE(), efile.readIntE());
if ( dynEnt.d_tag == Dynamic.DT_NULL ) if ( dynEnt.d_tag == Dynamic.DT_NULL )
break; break;
dynList.add(dynEnt); dynList.add(dynEnt);
} off+= Dynamic.DYN_ENT_SIZE;
} }
return (Dynamic[])dynList.toArray(new Dynamic[0]); return (Dynamic[])dynList.toArray(new Dynamic[0]);
} }
@ -727,6 +729,17 @@ public class Elf {
} }
} }
public Section getSectionByName(String name) throws IOException {
if ( sections == null )
getSections();
for( int i = 0; i < sections.length; i++) {
if ( sections[i].toString().equals(name)) {
return sections[i];
}
}
return null;
}
public Section[] getSections(int type) throws IOException { public Section[] getSections(int type) throws IOException {
if ( sections == null ) if ( sections == null )
getSections(); getSections();

View file

@ -10,7 +10,6 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Vector; import java.util.Vector;
/** /**
* <code>ElfHelper</code> is a wrapper class for the <code>Elf</code> class * <code>ElfHelper</code> is a wrapper class for the <code>Elf</code> class
* to provide higher level API for sorting/searching the ELF data. * to provide higher level API for sorting/searching the ELF data.
@ -27,10 +26,8 @@ public class ElfHelper {
private Elf.Section[] sections; private Elf.Section[] sections;
private Elf.Dynamic[] dynamics; private Elf.Dynamic[] dynamics;
public void dispose() { public void dispose() {
if( elf != null ) if (elf != null) {
{
elf.dispose(); elf.dispose();
elf = null; elf = null;
} }
@ -41,52 +38,42 @@ public class ElfHelper {
public long data; public long data;
public long bss; public long bss;
public long total; public long total;
public Sizes( long t, long d, long b ) { public Sizes(long t, long d, long b) {
text = t; text = t;
data = d; data = d;
bss = b; bss = b;
total = text+data+bss; total = text + data + bss;
} }
} }
private void loadSymbols() throws IOException { private void loadSymbols() throws IOException {
if( symbols == null ) if (symbols == null) {
{
elf.loadSymbols(); elf.loadSymbols();
symbols = elf.getSymtabSymbols(); symbols = elf.getSymtabSymbols();
dynsyms = elf.getDynamicSymbols(); dynsyms = elf.getDynamicSymbols();
if( symbols.length <= 0 ) if (symbols.length <= 0)
symbols = dynsyms; symbols = dynsyms;
if( dynsyms.length <= 0 ) if (dynsyms.length <= 0)
dynsyms = symbols; dynsyms = symbols;
} }
} }
private void loadSections() throws IOException { private void loadSections() throws IOException {
if( sections == null ) if (sections == null)
sections = elf.getSections(); sections = elf.getSections();
} }
private void loadDynamics() throws IOException { private void loadDynamics() throws IOException {
loadSections(); if (dynamics == null) {
if( dynamics == null )
{
dynamics = new Elf.Dynamic[0]; dynamics = new Elf.Dynamic[0];
for( int i=0; i<sections.length; i++ ) Elf.Section dynSect = elf.getSectionByName(".dynamic");
{ if (dynSect != null) {
if( sections[i].sh_type == Elf.Section.SHT_DYNAMIC ) dynamics = elf.getDynamicSections(dynSect);
{
dynamics = elf.getDynamicSections( sections[i] );
break;
}
}
}
} }
}
}
/** Common code used by all constructors */ /** Common code used by all constructors */
private void commonSetup() throws IOException { private void commonSetup() throws IOException {
@ -100,7 +87,7 @@ public class ElfHelper {
* @param elf An existing Elf object to wrap. * @param elf An existing Elf object to wrap.
* @throws IOException Error processing the Elf file. * @throws IOException Error processing the Elf file.
*/ */
public ElfHelper( Elf elf ) throws IOException { public ElfHelper(Elf elf) throws IOException {
this.elf = elf; this.elf = elf;
commonSetup(); commonSetup();
} }
@ -112,13 +99,13 @@ public class ElfHelper {
* @throws IOException Error processing the Elf file. * @throws IOException Error processing the Elf file.
* @see Elf#Elf( String ) * @see Elf#Elf( String )
*/ */
public ElfHelper( String filename ) throws IOException { public ElfHelper(String filename) throws IOException {
elf = new Elf( filename ); elf = new Elf(filename);
commonSetup(); commonSetup();
} }
public ElfHelper( String filename, boolean filton ) throws IOException { public ElfHelper(String filename, boolean filton) throws IOException {
elf = new Elf( filename, filton ); elf = new Elf(filename, filton);
commonSetup(); commonSetup();
} }
@ -127,219 +114,170 @@ public class ElfHelper {
return elf; return elf;
} }
public Elf.Symbol[] getExternalFunctions() public Elf.Symbol[] getExternalFunctions() throws IOException {
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
loadSections(); loadSections();
for( int i=0; i<dynsyms.length; i++ ) for (int i = 0; i < dynsyms.length; i++) {
{ if (dynsyms[i].st_bind() == Elf.Symbol.STB_GLOBAL && dynsyms[i].st_type() == Elf.Symbol.STT_FUNC) {
if( dynsyms[i].st_bind() == Elf.Symbol.STB_GLOBAL &&
dynsyms[i].st_type() == Elf.Symbol.STT_FUNC )
{
int idx = dynsyms[i].st_shndx; int idx = dynsyms[i].st_shndx;
if( idx < 0 ) if (idx < 0)
continue; continue;
if( sections[idx].sh_type == Elf.Section.SHT_NULL ) if (sections[idx].sh_type == Elf.Section.SHT_NULL)
v.add( dynsyms[i] ); v.add(dynsyms[i]);
} }
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Symbol[] getExternalObjects() throws IOException {
public Elf.Symbol[] getExternalObjects()
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
loadSections(); loadSections();
for( int i=0; i<dynsyms.length; i++ ) for (int i = 0; i < dynsyms.length; i++) {
{ if (dynsyms[i].st_bind() == Elf.Symbol.STB_GLOBAL && dynsyms[i].st_type() == Elf.Symbol.STT_OBJECT) {
if( dynsyms[i].st_bind() == Elf.Symbol.STB_GLOBAL &&
dynsyms[i].st_type() == Elf.Symbol.STT_OBJECT )
{
int idx = dynsyms[i].st_shndx; int idx = dynsyms[i].st_shndx;
if( idx < 0 ) if (idx < 0)
continue; continue;
if( sections[idx].sh_type == Elf.Section.SHT_NULL ) if (sections[idx].sh_type == Elf.Section.SHT_NULL)
v.add( dynsyms[i] ); v.add(dynsyms[i]);
} }
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Symbol[] getUndefined() throws IOException {
public Elf.Symbol[] getUndefined()
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
for( int i=0; i<dynsyms.length; i++ ) for (int i = 0; i < dynsyms.length; i++) {
{ if (dynsyms[i].st_shndx == Elf.Symbol.SHN_UNDEF)
if( dynsyms[i].st_shndx == Elf.Symbol.SHN_UNDEF ) v.add(dynsyms[i]);
v.add( dynsyms[i] );
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Symbol[] getLocalFunctions() throws IOException {
public Elf.Symbol[] getLocalFunctions()
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
loadSections(); loadSections();
for( int i=0; i<symbols.length; i++ ) for (int i = 0; i < symbols.length; i++) {
{ if (symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL && symbols[i].st_type() == Elf.Symbol.STT_FUNC) {
if( symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL &&
symbols[i].st_type() == Elf.Symbol.STT_FUNC )
{
int idx = symbols[i].st_shndx; int idx = symbols[i].st_shndx;
if( idx < 0 ) if (idx < 0)
continue; continue;
if( sections[idx].sh_type != Elf.Section.SHT_NULL ) if (sections[idx].sh_type != Elf.Section.SHT_NULL)
v.add( symbols[i] ); v.add(symbols[i]);
} }
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Symbol[] getLocalObjects() throws IOException {
public Elf.Symbol[] getLocalObjects()
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
loadSections(); loadSections();
for( int i=0; i<symbols.length; i++ ) for (int i = 0; i < symbols.length; i++) {
{ if (symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL && symbols[i].st_type() == Elf.Symbol.STT_OBJECT) {
if( symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL &&
symbols[i].st_type() == Elf.Symbol.STT_OBJECT )
{
int idx = symbols[i].st_shndx; int idx = symbols[i].st_shndx;
if( idx < 0 ) if (idx < 0)
continue; continue;
if( sections[idx].sh_type != Elf.Section.SHT_NULL ) if (sections[idx].sh_type != Elf.Section.SHT_NULL)
v.add( symbols[i] ); v.add(symbols[i]);
} }
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Symbol[] getCommonObjects() throws IOException {
public Elf.Symbol[] getCommonObjects() throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadSymbols(); loadSymbols();
loadSections(); loadSections();
for( int i=0; i<symbols.length; i++ ) for (int i = 0; i < symbols.length; i++) {
{ if (symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL && symbols[i].st_type() == Elf.Symbol.STT_OBJECT) {
if( symbols[i].st_bind() == Elf.Symbol.STB_GLOBAL &&
symbols[i].st_type() == Elf.Symbol.STT_OBJECT )
{
int idx = symbols[i].st_shndx; int idx = symbols[i].st_shndx;
if( idx == Elf.Symbol.SHN_COMMON ) if (idx == Elf.Symbol.SHN_COMMON) {
{ v.add(symbols[i]);
v.add( symbols[i] );
} }
} }
} }
Elf.Symbol[] ret = (Elf.Symbol[])v.toArray( new Elf.Symbol[0] ); Elf.Symbol[] ret = (Elf.Symbol[]) v.toArray(new Elf.Symbol[0]);
Arrays.sort( ret, new SymbolSortCompare() ); Arrays.sort(ret, new SymbolSortCompare());
return ret; return ret;
} }
public Elf.Dynamic[] getNeeded() throws IOException {
public Elf.Dynamic[] getNeeded()
throws IOException
{
Vector v = new Vector(); Vector v = new Vector();
loadDynamics(); loadDynamics();
for( int i=0; i<dynamics.length; i++ ) for (int i = 0; i < dynamics.length; i++) {
{ if (dynamics[i].d_tag == Elf.Dynamic.DT_NEEDED)
if( dynamics[i].d_tag == Elf.Dynamic.DT_NEEDED ) v.add(dynamics[i]);
v.add( dynamics[i] );
} }
return( Elf.Dynamic[])v.toArray( new Elf.Dynamic[0] ); return (Elf.Dynamic[]) v.toArray(new Elf.Dynamic[0]);
} }
public String getSoname() throws IOException { public String getSoname() throws IOException {
String soname = ""; String soname = "";
loadDynamics(); loadDynamics();
for( int i=0; i<dynamics.length; i++ ) for (int i = 0; i < dynamics.length; i++) {
{ if (dynamics[i].d_tag == Elf.Dynamic.DT_SONAME)
if( dynamics[i].d_tag == Elf.Dynamic.DT_SONAME )
soname = dynamics[i].toString(); soname = dynamics[i].toString();
} }
return soname; return soname;
} }
private String getSubUsage(String full, String name) {
private String getSubUsage( String full, String name ) {
int start, end; int start, end;
//boolean has_names = false; //boolean has_names = false;
//boolean has_languages = false; //boolean has_languages = false;
start = 0; start = 0;
end = 0; end = 0;
for( int i=0; i<full.length(); i++ ) for (int i = 0; i < full.length(); i++) {
{ if (full.charAt(i) == '%') {
if( full.charAt( i ) == '%' ) if (full.charAt(i + 1) == '-') {
{ if (start == 0) {
if( full.charAt( i+1 ) == '-' ) int eol = full.indexOf('\n', i + 2);
{ String temp = full.substring(i + 2, eol);
if( start == 0 ) if (temp.compareTo(name) == 0)
{
int eol = full.indexOf( '\n', i+2 );
String temp = full.substring( i+2, eol );
if( temp.compareTo( name ) == 0 )
start = eol; start = eol;
//has_names = true; //has_names = true;
} } else if (end == 0) {
else if( end == 0 )
{
end = i - 1; end = i - 1;
} }
} }
@ -349,47 +287,40 @@ public class ElfHelper {
} }
} }
if( end == 0 ) if (end == 0)
end = full.length(); end = full.length();
if( start == 0 ) if (start == 0)
return full; return full;
return full.substring( start, end ); return full.substring(start, end);
} }
public String getQnxUsage() throws IOException { public String getQnxUsage() throws IOException {
loadSections(); loadSections();
for( int i=0; i<sections.length; i++ ) for (int i = 0; i < sections.length; i++) {
{ if (sections[i].toString().compareTo("QNX_usage") == 0) {
if( sections[i].toString().compareTo( "QNX_usage" ) == 0 ) File file = new File(elf.getFilename());
{
File file = new File( elf.getFilename() );
String full_usage = new String( sections[i].loadSectionData() ); String full_usage = new String(sections[i].loadSectionData());
String usage = getSubUsage( full_usage, file.getName() ); String usage = getSubUsage(full_usage, file.getName());
StringBuffer buffer = new StringBuffer( usage ); StringBuffer buffer = new StringBuffer(usage);
for (int j = 0; j < buffer.length(); j++) {
for( int j=0; j<buffer.length(); j++ ) if (buffer.charAt(j) == '%') {
{ if (buffer.charAt(j + 1) == 'C')
if( buffer.charAt( j ) == '%' ) buffer.replace(j, j + 2, file.getName());
{
if( buffer.charAt( j+1 ) == 'C' )
buffer.replace( j, j+2, file.getName() );
} }
} }
return buffer.toString(); return buffer.toString();
} }
} }
return new String( "" ); return new String("");
} }
public Sizes getSizes() throws IOException { public Sizes getSizes() throws IOException {
long text, data, bss; long text, data, bss;
@ -399,33 +330,21 @@ public class ElfHelper {
loadSections(); loadSections();
for( int i=0; i<sections.length; i++ ) for (int i = 0; i < sections.length; i++) {
{ if (sections[i].sh_type != Elf.Section.SHT_NOBITS) {
if( sections[i].sh_type != Elf.Section.SHT_NOBITS ) if (sections[i].sh_flags == (Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC)) {
{
if( sections[i].sh_flags ==
( Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC ) )
{
data += sections[i].sh_size; data += sections[i].sh_size;
} } else if ((sections[i].sh_flags & Elf.Section.SHF_ALLOC) != 0) {
else if( ( sections[i].sh_flags & Elf.Section.SHF_ALLOC ) != 0 )
{
text += sections[i].sh_size; text += sections[i].sh_size;
} }
} } else {
else if (sections[i].sh_flags == (Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC)) {
{
if( sections[i].sh_flags ==
( Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC ) )
{
bss += sections[i].sh_size; bss += sections[i].sh_size;
} }
} }
} }
return new Sizes( text, data, bss ); return new Sizes(text, data, bss);
} }
} }