Corrected ELF Import issue where FileBytes can not be used in some load

situations (e.g., PIC-30)
This commit is contained in:
ghidra1 2019-07-19 10:56:01 -04:00
parent 6d05537b7f
commit 6a6bb63932
3 changed files with 85 additions and 40 deletions

View file

@ -392,6 +392,8 @@ public class ElfLoadAdapter {
/**
* Return filtered InputStream for loading a memory block (includes non-loaded OTHER blocks).
* NOTE: If this method is overriden, the {@link #hasFilteredLoadInputStream(ElfLoadHelper, MemoryLoadable, Address)}
* must also be overriden in a consistent fashion.
* @param elfLoadHelper
* @param loadable Corresponding ElfSectionHeader or ElfProgramHeader for the memory block to be created.
* @param start memory load address
@ -404,6 +406,20 @@ public class ElfLoadAdapter {
return dataInput;
}
/**
* Determine if the use of {@link #getFilteredLoadInputStream(ElfLoadHelper, MemoryLoadable, Address, long, InputStream)}
* is required when loading a memory block. If a filtered input stream is required this will prevent the use of a direct
* mapping to file bytes.
* @param elfLoadHelper
* @param loadable Corresponding ElfSectionHeader or ElfProgramHeader for the memory block to be loaded.
* @param start memory load address
* @return true if the use of a filtered input stream is required
*/
public boolean hasFilteredLoadInputStream(ElfLoadHelper elfLoadHelper, MemoryLoadable loadable,
Address start) {
return false;
}
/**
* Get the ElfRelocation class which should be used to properly parse
* the relocation tables.

View file

@ -2845,6 +2845,15 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
return sym;
}
/**
* Get a suitable input stream for loading a memory block defined by a specified loadable.
* @param loadable Corresponding ElfSectionHeader or ElfProgramHeader for the memory block to be created.
* @param start memory load address
* @param fileOffset byte provider offset
* @param dataLength the in-memory data length in bytes (actual bytes read from dataInput may be more)
* @return input stream for loading memory block
* @throws IOException
*/
private InputStream getInitializedBlockInputStream(MemoryLoadable loadable, Address start,
long fileOffset, long dataLength) throws IOException {
InputStream dataInput = elf.getReader().getByteProvider().getInputStream(fileOffset);
@ -2909,6 +2918,17 @@ class ElfProgramBuilder extends MemorySectionResolver implements ElfLoadHelper {
blockComment += " (section truncated to " + sizeGB + " GByte)";
}
if (elf.getLoadAdapter().hasFilteredLoadInputStream(this, loadable, start)) {
// block is unable to map directly to file bytes - load from input stream
try (InputStream dataInput =
getInitializedBlockInputStream(loadable, start, fileOffset, revisedLength)) {
return MemoryBlockUtils.createInitializedBlock(program, isOverlay, name, start,
dataInput, revisedLength, blockComment, BLOCK_SOURCE_NAME, r, w, x, log,
monitor);
}
}
// create block using direct mapping to file bytes
return MemoryBlockUtils.createInitializedBlock(program, isOverlay, name, start, fileBytes,
fileOffset, revisedLength, blockComment, BLOCK_SOURCE_NAME, r, w, x, log);
}

View file

@ -31,7 +31,6 @@ public class PIC30_ElfExtension extends ElfExtension {
public static final int SHF_PSV = (1 << 28); /* Constants in program memory */
@Override
public boolean canHandle(ElfHeader elf) {
return elf.e_machine() == EM_DSPIC30F;
@ -109,8 +108,8 @@ public class PIC30_ElfExtension extends ElfExtension {
}
@Override
public InputStream getFilteredLoadInputStream(ElfLoadHelper elfLoadHelper, MemoryLoadable loadable, Address start, long dataLength,
InputStream dataInput) {
public InputStream getFilteredLoadInputStream(ElfLoadHelper elfLoadHelper,
MemoryLoadable loadable, Address start, long dataLength, InputStream dataInput) {
Language language = elfLoadHelper.getProgram().getLanguage();
if (!language.getDefaultDataSpace().equals(start.getAddressSpace().getPhysicalSpace())) {
return dataInput;
@ -131,6 +130,13 @@ public class PIC30_ElfExtension extends ElfExtension {
return new PIC30FilteredDataInputStream(dataInput);
}
@Override
public boolean hasFilteredLoadInputStream(ElfLoadHelper elfLoadHelper, MemoryLoadable loadable,
Address start) {
Language language = elfLoadHelper.getProgram().getLanguage();
return language.getDefaultDataSpace().equals(start.getAddressSpace().getPhysicalSpace());
}
private static class PIC30FilteredDataInputStream extends FilterInputStream {
// BYTES: <byte> <pad>
@ -169,9 +175,11 @@ public class PIC30_ElfExtension extends ElfExtension {
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
}
else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
}
else if (len == 0) {
return 0;
}
@ -200,6 +208,7 @@ public class PIC30_ElfExtension extends ElfExtension {
firstByteToggle = true;
}
@Override
protected int readNextByte() throws IOException {
int r = in.read();
++pos;