Skip to content

Commit

Permalink
Merge pull request #12 from nicodex/master
Browse files Browse the repository at this point in the history
Create function segments at next free aligned memory block
  • Loading branch information
lab313ru authored Feb 22, 2021
2 parents 28280db + c7fd25c commit 7102d67
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/main/java/amigahunk/AmigaHunkAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,8 @@ public boolean added(Program program, AddressSetView set, TaskMonitor monitor, M
FlatProgramAPI fpa = new FlatProgramAPI(program);

try {
String[] libs = funcsList.getLibsList(filter);

for (String lib : libs) {
int i = funcsList.findLibIndex(lib) + 1;

if (i == 0) {
continue;
}

createFunctionsSegment(fpa, lib, AmigaHunkLoader.getImageBase(imageBaseOffset) + i * 0x10000, funcsList.getFunctionTableByLib(lib), log);
for (String lib : funcsList.getLibsList(filter)) {
createFunctionsSegment(fpa, lib, funcsList.getFunctionTableByLib(lib), log);
}
} catch (InvalidInputException | DuplicateNameException | CodeUnitInsertionException e) {
log.appendException(e);
Expand Down Expand Up @@ -168,15 +160,34 @@ public boolean added(Program program, AddressSetView set, TaskMonitor monitor, M
return true;
}

private static void createFunctionsSegment(FlatProgramAPI fpa, String lib, long segAddr, FdLibFunctions funcs, MessageLog log) throws InvalidInputException, DuplicateNameException, CodeUnitInsertionException {
if (fpa.getMemoryBlock(fpa.toAddr(segAddr)) != null) {
private static void createFunctionsSegment(FlatProgramAPI fpa, String lib, FdLibFunctions funcs, MessageLog log) throws InvalidInputException, DuplicateNameException, CodeUnitInsertionException {
if ((null == funcs) || (fpa.getMemoryBlock(lib) != null)) {
return;
}
FdFunction[] funcArr = funcs.getFunctions();
long segAlign = Math.max(2, 0x1000);
long segSize = 6 * Math.max(5, 7); // Library 5+, Device 7+
for (FdFunction func : funcArr) {
segSize = Math.max(segSize, Math.abs(func.getBias()) + 6);
}
segSize = ((segSize + (segAlign - 1)) / segAlign) * segAlign;
Address segAddr = fpa.toAddr(AmigaHunkLoader.getImageBase(0));
for (MemoryBlock memBlock : fpa.getMemoryBlocks()) {
if (memBlock.contains(segAddr) || memBlock.contains(segAddr.add(segSize - 1)) || (
(segAddr.getOffset() <= memBlock.getStart().getOffset()) &&
(memBlock.getEnd().getOffset() <= segAddr.add(segSize - 1).getOffset()))) {
segAddr = memBlock.getEnd().add(1);
long segRem = segAddr.getOffset() % segAlign;
if (segRem > 0) {
segAddr = segAddr.add(segAlign - segRem);
}
}
}

AmigaHunkLoader.createSegment(null, fpa, lib, segAddr, 0x1000, true, true, log);
AmigaHunkLoader.createSegment(null, fpa, lib, segAddr.getOffset(), segSize, true, true, log);

for (FdFunction func : funcs.getFunctions()) {
Address funcAddress = fpa.toAddr(segAddr + Math.abs(func.getBias()));
for (FdFunction func : funcArr) {
Address funcAddress = segAddr.add(Math.abs(func.getBias()));
AmigaHunkLoader.setFunction(fpa, funcAddress, func.getName(true).replace(FdFunction.LIB_SPLITTER, "_"), log);
Function function = fpa.getFunctionAt(funcAddress);
function.setCustomVariableStorage(true);
Expand Down

0 comments on commit 7102d67

Please sign in to comment.