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:
parent
1ce9ba9ea0
commit
8ab6e1bb4c
3 changed files with 360 additions and 419 deletions
|
@ -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
|
||||
|
||||
* model/org/eclipse/cdt/internal/core/model/parser/PEParser.java (getBinary):
|
||||
|
|
|
@ -139,6 +139,8 @@ public class Elf {
|
|||
public final static int SHT_SHLIB = 10;
|
||||
public final static int SHT_DYNSYM = 11;
|
||||
|
||||
public final static int SHT_LOPROC = 0x70000000;
|
||||
|
||||
/* sh_flags */
|
||||
public final static int SHF_WRITE = 1;
|
||||
public final static int SHF_ALLOC = 2;
|
||||
|
@ -173,7 +175,7 @@ public class Elf {
|
|||
efile.read(section_strtab);
|
||||
}
|
||||
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;
|
||||
}
|
||||
while( section_strtab[(int)sh_name + str_size] != 0)
|
||||
|
@ -483,6 +485,8 @@ public class Elf {
|
|||
}
|
||||
|
||||
public class Dynamic {
|
||||
public final static int DYN_ENT_SIZE = 8;
|
||||
|
||||
public final static int DT_NULL = 0;
|
||||
public final static int DT_NEEDED = 1;
|
||||
public final static int DT_PLTRELSZ = 2;
|
||||
|
@ -501,7 +505,6 @@ public class Elf {
|
|||
public final static int DT_RPATH = 15;
|
||||
public long d_tag;
|
||||
public long d_val;
|
||||
|
||||
private Section section;
|
||||
private String name;
|
||||
|
||||
|
@ -538,16 +541,15 @@ public class Elf {
|
|||
}
|
||||
ArrayList dynList = new ArrayList();
|
||||
efile.seek(section.sh_offset);
|
||||
if (section.sh_entsize == 0) {
|
||||
Dynamic dynEnt = new Dynamic(section, efile.readIntE(), efile.readIntE());
|
||||
dynList.add(dynEnt);
|
||||
} else {
|
||||
for (int i = 0; i < section.sh_size / section.sh_entsize; i++ ) {
|
||||
int off = 0;
|
||||
// We must assume the section is a table ignoring the sh_entsize as it is not
|
||||
// set for MIPS.
|
||||
while( off < section.sh_size ) {
|
||||
Dynamic dynEnt = new Dynamic(section, efile.readIntE(), efile.readIntE());
|
||||
if ( dynEnt.d_tag == Dynamic.DT_NULL )
|
||||
break;
|
||||
dynList.add(dynEnt);
|
||||
}
|
||||
off+= Dynamic.DYN_ENT_SIZE;
|
||||
}
|
||||
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 {
|
||||
if ( sections == null )
|
||||
getSections();
|
||||
|
|
|
@ -10,7 +10,6 @@ import java.io.IOException;
|
|||
import java.util.Arrays;
|
||||
import java.util.Vector;
|
||||
|
||||
|
||||
/**
|
||||
* <code>ElfHelper</code> is a wrapper class for the <code>Elf</code> class
|
||||
* to provide higher level API for sorting/searching the ELF data.
|
||||
|
@ -27,10 +26,8 @@ public class ElfHelper {
|
|||
private Elf.Section[] sections;
|
||||
private Elf.Dynamic[] dynamics;
|
||||
|
||||
|
||||
public void dispose() {
|
||||
if( elf != null )
|
||||
{
|
||||
if (elf != null) {
|
||||
elf.dispose();
|
||||
elf = null;
|
||||
}
|
||||
|
@ -49,10 +46,8 @@ public class ElfHelper {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private void loadSymbols() throws IOException {
|
||||
if( symbols == null )
|
||||
{
|
||||
if (symbols == null) {
|
||||
elf.loadSymbols();
|
||||
symbols = elf.getSymtabSymbols();
|
||||
dynsyms = elf.getDynamicSymbols();
|
||||
|
@ -69,24 +64,16 @@ public class ElfHelper {
|
|||
sections = elf.getSections();
|
||||
}
|
||||
|
||||
|
||||
private void loadDynamics() throws IOException {
|
||||
loadSections();
|
||||
|
||||
if( dynamics == null )
|
||||
{
|
||||
if (dynamics == null) {
|
||||
dynamics = new Elf.Dynamic[0];
|
||||
for( int i=0; i<sections.length; i++ )
|
||||
{
|
||||
if( sections[i].sh_type == Elf.Section.SHT_DYNAMIC )
|
||||
{
|
||||
dynamics = elf.getDynamicSections( sections[i] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Elf.Section dynSect = elf.getSectionByName(".dynamic");
|
||||
if (dynSect != null) {
|
||||
dynamics = elf.getDynamicSections(dynSect);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/** Common code used by all constructors */
|
||||
private void commonSetup() throws IOException {
|
||||
|
@ -127,19 +114,14 @@ public class ElfHelper {
|
|||
return elf;
|
||||
}
|
||||
|
||||
public Elf.Symbol[] getExternalFunctions()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getExternalFunctions() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
loadSymbols();
|
||||
loadSections();
|
||||
|
||||
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 )
|
||||
{
|
||||
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) {
|
||||
int idx = dynsyms[i].st_shndx;
|
||||
if (idx < 0)
|
||||
continue;
|
||||
|
@ -153,20 +135,14 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public Elf.Symbol[] getExternalObjects()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getExternalObjects() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
loadSymbols();
|
||||
loadSections();
|
||||
|
||||
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 )
|
||||
{
|
||||
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) {
|
||||
int idx = dynsyms[i].st_shndx;
|
||||
if (idx < 0)
|
||||
continue;
|
||||
|
@ -181,16 +157,12 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public Elf.Symbol[] getUndefined()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getUndefined() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
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)
|
||||
v.add(dynsyms[i]);
|
||||
}
|
||||
|
@ -200,21 +172,14 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Elf.Symbol[] getLocalFunctions()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getLocalFunctions() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
loadSymbols();
|
||||
loadSections();
|
||||
|
||||
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 )
|
||||
{
|
||||
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) {
|
||||
int idx = symbols[i].st_shndx;
|
||||
if (idx < 0)
|
||||
continue;
|
||||
|
@ -229,20 +194,14 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public Elf.Symbol[] getLocalObjects()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getLocalObjects() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
loadSymbols();
|
||||
loadSections();
|
||||
|
||||
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 )
|
||||
{
|
||||
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) {
|
||||
int idx = symbols[i].st_shndx;
|
||||
if (idx < 0)
|
||||
continue;
|
||||
|
@ -257,23 +216,16 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Elf.Symbol[] getCommonObjects() throws IOException
|
||||
{
|
||||
public Elf.Symbol[] getCommonObjects() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
loadSymbols();
|
||||
loadSections();
|
||||
|
||||
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 )
|
||||
{
|
||||
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) {
|
||||
int idx = symbols[i].st_shndx;
|
||||
if( idx == Elf.Symbol.SHN_COMMON )
|
||||
{
|
||||
if (idx == Elf.Symbol.SHN_COMMON) {
|
||||
v.add(symbols[i]);
|
||||
}
|
||||
}
|
||||
|
@ -284,38 +236,30 @@ public class ElfHelper {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Elf.Dynamic[] getNeeded()
|
||||
throws IOException
|
||||
{
|
||||
public Elf.Dynamic[] getNeeded() throws IOException {
|
||||
Vector v = new Vector();
|
||||
|
||||
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)
|
||||
v.add(dynamics[i]);
|
||||
}
|
||||
return (Elf.Dynamic[]) v.toArray(new Elf.Dynamic[0]);
|
||||
}
|
||||
|
||||
|
||||
public String getSoname() throws IOException {
|
||||
String soname = "";
|
||||
|
||||
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)
|
||||
soname = dynamics[i].toString();
|
||||
}
|
||||
return soname;
|
||||
}
|
||||
|
||||
|
||||
private String getSubUsage(String full, String name) {
|
||||
int start, end;
|
||||
//boolean has_names = false;
|
||||
|
@ -323,23 +267,17 @@ public class ElfHelper {
|
|||
start = 0;
|
||||
end = 0;
|
||||
|
||||
for( int i=0; i<full.length(); i++ )
|
||||
{
|
||||
if( full.charAt( i ) == '%' )
|
||||
{
|
||||
if( full.charAt( i+1 ) == '-' )
|
||||
{
|
||||
if( start == 0 )
|
||||
{
|
||||
for (int i = 0; i < full.length(); i++) {
|
||||
if (full.charAt(i) == '%') {
|
||||
if (full.charAt(i + 1) == '-') {
|
||||
if (start == 0) {
|
||||
int eol = full.indexOf('\n', i + 2);
|
||||
String temp = full.substring(i + 2, eol);
|
||||
if (temp.compareTo(name) == 0)
|
||||
start = eol;
|
||||
|
||||
//has_names = true;
|
||||
}
|
||||
else if( end == 0 )
|
||||
{
|
||||
} else if (end == 0) {
|
||||
end = i - 1;
|
||||
}
|
||||
}
|
||||
|
@ -358,26 +296,20 @@ public class ElfHelper {
|
|||
return full.substring(start, end);
|
||||
}
|
||||
|
||||
|
||||
public String getQnxUsage() throws IOException {
|
||||
|
||||
loadSections();
|
||||
|
||||
for( int i=0; i<sections.length; i++ )
|
||||
{
|
||||
if( sections[i].toString().compareTo( "QNX_usage" ) == 0 )
|
||||
{
|
||||
for (int i = 0; i < sections.length; i++) {
|
||||
if (sections[i].toString().compareTo("QNX_usage") == 0) {
|
||||
File file = new File(elf.getFilename());
|
||||
|
||||
String full_usage = new String(sections[i].loadSectionData());
|
||||
String usage = getSubUsage(full_usage, file.getName());
|
||||
StringBuffer buffer = new StringBuffer(usage);
|
||||
|
||||
|
||||
for( int j=0; j<buffer.length(); j++ )
|
||||
{
|
||||
if( buffer.charAt( j ) == '%' )
|
||||
{
|
||||
for (int j = 0; j < buffer.length(); j++) {
|
||||
if (buffer.charAt(j) == '%') {
|
||||
if (buffer.charAt(j + 1) == 'C')
|
||||
buffer.replace(j, j + 2, file.getName());
|
||||
}
|
||||
|
@ -389,7 +321,6 @@ public class ElfHelper {
|
|||
return new String("");
|
||||
}
|
||||
|
||||
|
||||
public Sizes getSizes() throws IOException {
|
||||
long text, data, bss;
|
||||
|
||||
|
@ -399,25 +330,15 @@ public class ElfHelper {
|
|||
|
||||
loadSections();
|
||||
|
||||
for( int i=0; i<sections.length; i++ )
|
||||
{
|
||||
if( sections[i].sh_type != Elf.Section.SHT_NOBITS )
|
||||
{
|
||||
if( sections[i].sh_flags ==
|
||||
( Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC ) )
|
||||
{
|
||||
for (int i = 0; i < sections.length; i++) {
|
||||
if (sections[i].sh_type != Elf.Section.SHT_NOBITS) {
|
||||
if (sections[i].sh_flags == (Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( sections[i].sh_flags ==
|
||||
( Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC ) )
|
||||
{
|
||||
} else {
|
||||
if (sections[i].sh_flags == (Elf.Section.SHF_WRITE | Elf.Section.SHF_ALLOC)) {
|
||||
bss += sections[i].sh_size;
|
||||
}
|
||||
}
|
||||
|
@ -427,5 +348,3 @@ public class ElfHelper {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue