Skip to content

Commit

Permalink
Only get the ElfSectionHeader in the RelocationHandler if it's not a …
Browse files Browse the repository at this point in the history
…special section header index to avoid a ArrayIndexOutOfBoundsException
  • Loading branch information
Maschell committed Oct 11, 2019
1 parent 0005907 commit fb3ec7f
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions src/main/java/cafeloader/Cafe_ElfRelocationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,43 @@ public void relocate(ElfRelocationContext elfRelocationContext, ElfRelocation re
* reference because it will be too far away for the actual relocation to
* be valid itself.
*/
ElfSectionHeader symbolSection = elf.getSections()[sym.getSectionHeaderIndex()];
if (symbolSection.getType() == Cafe_ElfExtension.SHT_RPL_IMPORTS.value) {
String symbolSectionName = symbolSection.getNameAsString();
boolean isDataImport = false;
if (symbolSectionName.startsWith(".dimport_")) {
program.getReferenceManager().addMemoryReference(relocationAddress,
elfRelocationContext.getSymbolAddress(sym), RefType.DATA, SourceType.IMPORTED, 0);
isDataImport = true;
} else if (symbolSectionName.startsWith(".fimport_")) {
program.getReferenceManager().addMemoryReference(relocationAddress,
elfRelocationContext.getSymbolAddress(sym), RefType.UNCONDITIONAL_CALL, SourceType.IMPORTED, 0);
}

String rplName = symbolSectionName.split("import_")[1];
if (!rplName.endsWith(".rpl")) {
rplName += ".rpl";
}

ExternalLocation location = program.getExternalManager().getUniqueExternalLocation(rplName, sym.getNameAsString());
if (location != null) {
try {
if (isDataImport) {
program.getReferenceManager().addExternalReference(relocationAddress, 1,
location, SourceType.IMPORTED, RefType.DATA);
} else {
program.getReferenceManager().addExternalReference(relocationAddress, 1,
location, SourceType.IMPORTED, RefType.UNCONDITIONAL_CALL);
}
} catch (InvalidInputException e) {
Msg.warn(this, "addExternalReference failed with " + e);
}
} else {
Msg.warn(this, "Failed to find location for " + sym.getNameAsString());
}

if (sym.getSectionHeaderIndex() > 0) {
ElfSectionHeader symbolSection = elf.getSections()[sym.getSectionHeaderIndex()];
if (symbolSection.getType() == Cafe_ElfExtension.SHT_RPL_IMPORTS.value) {
String symbolSectionName = symbolSection.getNameAsString();
boolean isDataImport = false;
if (symbolSectionName.startsWith(".dimport_")) {
program.getReferenceManager().addMemoryReference(relocationAddress,
elfRelocationContext.getSymbolAddress(sym), RefType.DATA, SourceType.IMPORTED, 0);
isDataImport = true;
} else if (symbolSectionName.startsWith(".fimport_")) {
program.getReferenceManager().addMemoryReference(relocationAddress,
elfRelocationContext.getSymbolAddress(sym), RefType.UNCONDITIONAL_CALL, SourceType.IMPORTED, 0);
}

String rplName = symbolSectionName.split("import_")[1];
if (!rplName.endsWith(".rpl")) {
rplName += ".rpl";
}

ExternalLocation location = program.getExternalManager().getUniqueExternalLocation(rplName, sym.getNameAsString());
if (location != null) {
try {
if (isDataImport) {
program.getReferenceManager().addExternalReference(relocationAddress, 1,
location, SourceType.IMPORTED, RefType.DATA);
} else {
program.getReferenceManager().addExternalReference(relocationAddress, 1,
location, SourceType.IMPORTED, RefType.UNCONDITIONAL_CALL);
}
} catch (InvalidInputException e) {
Msg.warn(this, "addExternalReference failed with " + e);
}
} else {
Msg.warn(this, "Failed to find location for " + sym.getNameAsString());
}
}
}

switch (type) {
Expand Down

0 comments on commit fb3ec7f

Please sign in to comment.