Skip to content

Commit 17a4f8f

Browse files
committed
Merge remote-tracking branch 'origin/GP-4239_ghidra1_ElfRelocationTypeEnum--SQUASHED'
2 parents 3efa8ce + 3ead54f commit 17a4f8f

File tree

71 files changed

+4899
-4912
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4899
-4912
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.app.plugin.core.reloc;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import ghidra.app.util.bin.format.elf.relocation.ElfRelocationType;
22+
23+
public abstract class ElfRelocationFixupHandler extends RelocationFixupHandler {
24+
25+
private Map<Integer, ElfRelocationType> relocationTypesMap;
26+
27+
/**
28+
* Abstract constructor for an {@link ElfRelocationFixupHandler}.
29+
*
30+
* @param relocationEnumClass specifies the {@link ElfRelocationType} enum which defines
31+
* all supported relocation types for this relocation handler.
32+
*/
33+
protected ElfRelocationFixupHandler(Class<? extends ElfRelocationType> relocationEnumClass) {
34+
initRelocationTypeMap(relocationEnumClass);
35+
}
36+
37+
private void initRelocationTypeMap(Class<? extends ElfRelocationType> relocationEnumClass) {
38+
if (!relocationEnumClass.isEnum() ||
39+
!ElfRelocationType.class.isAssignableFrom(relocationEnumClass)) {
40+
throw new IllegalArgumentException(
41+
"Invalid class specified - expected enum which implements ElfRelocationType: " +
42+
relocationEnumClass.getName());
43+
}
44+
relocationTypesMap = new HashMap<>();
45+
for (ElfRelocationType t : relocationEnumClass.getEnumConstants()) {
46+
relocationTypesMap.put(t.typeId(), t);
47+
}
48+
}
49+
50+
/**
51+
* Get the relocation type enum value which corresponds to the specified type value.
52+
*
53+
* @param type relocation type value
54+
* @return relocation type enum value or null if type not found or this handler was not
55+
* constructed with a {@link ElfRelocationType} enum class. The returned value may be
56+
* safely cast to the relocation enum class specified during handler construction.
57+
*/
58+
public ElfRelocationType getRelocationType(int type) {
59+
if (relocationTypesMap == null) {
60+
return null;
61+
}
62+
return relocationTypesMap.get(type);
63+
}
64+
65+
}

Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/reloc/RelocationFixupHandler.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* ###
22
* IP: GHIDRA
3-
* REVIEWED: YES
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -26,15 +25,21 @@
2625

2726
public abstract class RelocationFixupHandler implements ExtensionPoint {
2827

28+
/**
29+
* Default abstract constructor for an {@link RelocationFixupHandler}.
30+
*/
31+
protected RelocationFixupHandler() {
32+
}
33+
2934
public abstract boolean processRelocation(Program program, Relocation relocation,
30-
Address oldImageBase, Address newImageBase) throws MemoryAccessException,
31-
CodeUnitInsertionException;
35+
Address oldImageBase, Address newImageBase)
36+
throws MemoryAccessException, CodeUnitInsertionException;
3237

3338
public abstract boolean handlesProgram(Program program);
3439

3540
protected boolean process32BitRelocation(Program program, Relocation relocation,
36-
Address oldImageBase, Address newImageBase) throws MemoryAccessException,
37-
CodeUnitInsertionException {
41+
Address oldImageBase, Address newImageBase)
42+
throws MemoryAccessException, CodeUnitInsertionException {
3843
long diff = newImageBase.subtract(oldImageBase);
3944

4045
Address address = relocation.getAddress();
@@ -52,8 +57,8 @@ protected boolean process32BitRelocation(Program program, Relocation relocation,
5257
}
5358

5459
public boolean process64BitRelocation(Program program, Relocation relocation,
55-
Address oldImageBase, Address newImageBase) throws MemoryAccessException,
56-
CodeUnitInsertionException {
60+
Address oldImageBase, Address newImageBase)
61+
throws MemoryAccessException, CodeUnitInsertionException {
5762

5863
long diff = newImageBase.subtract(oldImageBase);
5964

Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocation.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import ghidra.app.util.bin.StructConverter;
2323
import ghidra.app.util.bin.format.elf.extend.ElfLoadAdapter;
2424
import ghidra.app.util.bin.format.elf.relocation.ElfRelocationContext;
25-
import ghidra.app.util.bin.format.elf.relocation.ElfRelocationHandler;
25+
import ghidra.app.util.bin.format.elf.relocation.AbstractElfRelocationHandler;
2626
import ghidra.program.model.data.*;
2727

2828
/**
@@ -62,7 +62,7 @@
6262
*
6363
* RELR entry (see SHT_RELR, DT_RELR):
6464
* NOTE: Relocation type is data <i>relative</i> and must be specified by appropriate relocation handler
65-
* (see {@link ElfRelocationHandler#getRelrRelocationType()}) since it is not contained within the
65+
* (see {@link AbstractElfRelocationHandler#getRelrRelocationType()}) since it is not contained within the
6666
* relocation table which only specifies <i>r_offset</i> for each entry.
6767
*
6868
* </pre>
@@ -272,27 +272,27 @@ public int getSymbolIndex() {
272272
}
273273

274274
/**
275-
* The type of relocation to apply.
276-
* NOTE 1: Relocation types are processor-specific (see {@link ElfRelocationHandler}).
277-
* NOTE 2: A type of 0 is returned by default for RELR relocations and must be updated
275+
* The type ID value for this relocation
276+
* NOTE 1: Relocation types are processor-specific (see {@link AbstractElfRelocationHandler}).
277+
* NOTE 2: A type ID of 0 is returned by default for RELR relocations and must be updated
278278
* during relocation processing (see {@link #setType(long)}). The appropriate RELR
279279
* relocation type can be obtained from the appropriate
280-
* {@link ElfRelocationHandler#getRelrRelocationType()} or
280+
* {@link AbstractElfRelocationHandler#getRelrRelocationType()} or
281281
* {@link ElfRelocationContext#getRelrRelocationType()} if available.
282-
* @return type of relocation to apply
282+
* @return type ID for this relocation
283283
*/
284284
public int getType() {
285285
return (int) (is32bit ? (r_info & BYTE_MASK) : (r_info & INT_MASK));
286286
}
287287

288288
/**
289-
* Set the relocation type associated with this relocation.
289+
* Set the relocation type ID associated with this relocation.
290290
* Updating the relocation type is required for RELR relocations.
291-
* @param type relocation type to be applied
291+
* @param typeId relocation type ID value for this relocation
292292
*/
293-
public void setType(long type) {
293+
public void setType(long typeId) {
294294
long mask = is32bit ? BYTE_MASK : INT_MASK;
295-
r_info = (r_info & ~mask) + (type & mask);
295+
r_info = (r_info & ~mask) + (typeId & mask);
296296
}
297297

298298
/**

0 commit comments

Comments
 (0)