Skip to content

Commit

Permalink
GP-4799 Respect ELF Skip Relocation Option
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidra1 committed Jul 29, 2024
1 parent 649c182 commit 8cf1450
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -181,7 +181,8 @@ public int getRelrRelocationType() {
* Get a relocation context for a specfic Elf image and relocation table
* @param loadHelper Elf load helper
* @param symbolMap Elf symbol placement map
* @return relocation context object
* @return relocation context object. A generic context will be returned if a custom one
* is not defined.
*/
public static ElfRelocationContext<?> getRelocationContext(ElfLoadHelper loadHelper,
Map<ElfSymbol, Address> symbolMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -879,22 +879,21 @@ private void processRelocations(TaskMonitor monitor) throws CancelledException {
}
monitor.initialize(totalCount);

ElfRelocationContext context = ElfRelocationContext.getRelocationContext(this, symbolMap);
ElfRelocationContext<?> context =
ElfRelocationContext.getRelocationContext(this, symbolMap);
try {
for (ElfRelocationTable relocationTable : relocationTables) {
monitor.checkCancelled();
processRelocationTable(relocationTable, context, monitor);
}
}
finally {
if (context != null) {
context.dispose();
}
context.dispose();
}
}

private void processRelocationTable(ElfRelocationTable relocationTable,
ElfRelocationContext context, TaskMonitor monitor) throws CancelledException {
ElfRelocationContext<?> context, TaskMonitor monitor) throws CancelledException {

Address defaultBase = getDefaultAddress(elf.adjustAddressForPrelink(0));
AddressSpace defaultSpace = defaultBase.getAddressSpace();
Expand Down Expand Up @@ -953,12 +952,12 @@ else if (relocationSpace != defaultSpace) {
}

private void processRelocationTableEntries(ElfRelocationTable relocationTable,
ElfRelocationContext context, AddressSpace relocationSpace, long baseWordOffset,
ElfRelocationContext<?> context, AddressSpace relocationSpace, long baseWordOffset,
TaskMonitor monitor) throws CancelledException {

if (context != null) {
context.startRelocationTableProcessing(relocationTable);
}
boolean processRelocations = ElfLoaderOptionsFactory.performRelocations(options);

context.startRelocationTableProcessing(relocationTable);

ElfSymbolTable symbolTable = relocationTable.getAssociatedSymbolTable();
ElfRelocation[] relocs = relocationTable.getRelocations();
Expand All @@ -977,7 +976,7 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,

boolean relrTypeUnknown = false;
int relrRelocationType = 0;
if (relocationTable.isRelrTable() && context != null) {
if (relocationTable.isRelrTable()) {
relrRelocationType = context.getRelrRelocationType();
if (relrRelocationType == 0) {
relrTypeUnknown = true;
Expand All @@ -991,8 +990,8 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,
monitor.incrementProgress(1);

int type = reloc.getType();
if (type == 0) {
continue; // ignore relocation type 0 (i.e., ..._NONE)
if (type == 0 && !relocationTable.isRelrTable()) {
continue; // ignore relocation type 0 if not a RELR table (i.e., ..._NONE)
}

int symbolIndex = reloc.getSymbolIndex();
Expand All @@ -1004,9 +1003,7 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,
Address baseAddress = relocationSpace.getTruncatedAddress(baseWordOffset, true);

// relocation offset (r_offset) is defined to be a byte offset (assume byte size is 1)
Address relocAddr =
context != null ? context.getRelocationAddress(baseAddress, reloc.getOffset())
: baseAddress.addWrap(reloc.getOffset());
Address relocAddr = context.getRelocationAddress(baseAddress, reloc.getOffset());

long[] values = new long[] { reloc.getSymbolIndex() };

Expand All @@ -1018,6 +1015,11 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,
Status status = Status.SKIPPED;
int byteLength = 0;
try {

if (!processRelocations) {
continue; // skip and record relocation
}

if (unableToApplyRelocs) {
status = Status.FAILURE;
context.markRelocationError(relocAddr, type, symbolIndex, symbolName,
Expand All @@ -1044,17 +1046,15 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,
}
}

if (context != null) {
if (relrTypeUnknown) {
status = Status.UNSUPPORTED;
ElfRelocationHandler.bookmarkUnsupportedRelr(program, relocAddr,
symbolIndex, symbolName);
}
else {
RelocationResult result = context.processRelocation(reloc, relocAddr);
byteLength = result.byteLength();
status = result.status();
}
if (relrTypeUnknown) {
status = Status.UNSUPPORTED;
ElfRelocationHandler.bookmarkUnsupportedRelr(program, relocAddr, symbolIndex,
symbolName);
}
else {
RelocationResult result = context.processRelocation(reloc, relocAddr);
byteLength = result.byteLength();
status = result.status();
}
}
catch (MemoryAccessException e) {
Expand All @@ -1072,9 +1072,7 @@ private void processRelocationTableEntries(ElfRelocationTable relocationTable,
}
}

if (context != null) {
context.endRelocationTableProcessing();
}
context.endRelocationTableProcessing();
}

@Override
Expand Down

0 comments on commit 8cf1450

Please sign in to comment.