From fb3ec7fe286dca5027a0e8d5b3c959f21b1900b7 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 11 Oct 2019 14:47:48 +0200 Subject: [PATCH] Only get the ElfSectionHeader in the RelocationHandler if it's not a special section header index to avoid a ArrayIndexOutOfBoundsException --- .../cafeloader/Cafe_ElfRelocationHandler.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main/java/cafeloader/Cafe_ElfRelocationHandler.java b/src/main/java/cafeloader/Cafe_ElfRelocationHandler.java index 9fb2d29..67d74b1 100644 --- a/src/main/java/cafeloader/Cafe_ElfRelocationHandler.java +++ b/src/main/java/cafeloader/Cafe_ElfRelocationHandler.java @@ -44,40 +44,43 @@ public class Cafe_ElfRelocationHandler extends ElfRelocationHandler { * 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) {