Skip to content

Commit 6f5ded5

Browse files
committed
GP-3707 Add additional ELF RISCV relocations and some cleanup
1 parent 353a798 commit 6f5ded5

File tree

2 files changed

+38
-75
lines changed

2 files changed

+38
-75
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ private void processGOTSections(TaskMonitor monitor) throws CancelledException {
8181
for (MemoryBlock gotBlock : blocks) {
8282
monitor.checkCancelled();
8383

84-
if (!gotBlock.getName().startsWith(ElfSectionHeaderConstants.dot_got)) {
84+
if (!gotBlock.getName().startsWith(ElfSectionHeaderConstants.dot_got) ||
85+
!gotBlock.isInitialized()) {
8586
continue;
8687
}
88+
8789
// Assume the .got section is read_only. This is not true, but it helps with analysis
8890
gotBlock.setWrite(false);
8991

@@ -399,9 +401,10 @@ private void processGOT(Address gotStart, Address gotEnd, TaskMonitor monitor)
399401
boolean imageBaseAlreadySet = elf.isPreLinked();
400402

401403
try {
404+
int pointerSize = program.getDataTypeManager().getDataOrganization().getPointerSize();
402405
Address newImageBase = null;
403406
Address nextGotAddr = gotStart;
404-
while (nextGotAddr.compareTo(gotEnd) <= 0) {
407+
while (gotEnd.subtract(nextGotAddr) >= pointerSize) {
405408

406409
data = createPointer(nextGotAddr, true);
407410
if (data == null) {
@@ -450,7 +453,8 @@ private void processPLTSection(TaskMonitor monitor) throws CancelledException {
450453

451454
MemoryBlock pltBlock = memory.getBlock(ElfSectionHeaderConstants.dot_plt);
452455
// TODO: This is a band-aid since there are many PLT implementations and this assumes only one.
453-
if (pltBlock == null || !pltBlock.isExecute() || pltBlock.getSize() <= assumedPltHeadSize) {
456+
if (pltBlock == null || !pltBlock.isExecute() || !pltBlock.isInitialized() ||
457+
pltBlock.getSize() <= assumedPltHeadSize) {
454458
return;
455459
}
456460

Ghidra/Processors/RISCV/src/main/java/ghidra/app/util/bin/format/elf/relocation/RISCV_ElfRelocationHandler.java

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
6666
case 5:
6767
break;
6868
default:
69-
System.out.println("DEBUG RISCV: " +
70-
type + " " + relocationAddress + " " +
71-
String.format("%x", symbolValue) + " " +
72-
String.format("%x", addend) + " " +
73-
String.format("%x", offset) + " " +
74-
String.format("%x", base));// + " " +
69+
// System.out.println("DEBUG RISCV: " +
70+
// type + " " + relocationAddress + " " +
71+
// String.format("%x", symbolValue) + " " +
72+
// String.format("%x", addend) + " " +
73+
// String.format("%x", offset) + " " +
74+
// String.format("%x", base));// + " " +
7575
//String.format("%x", memory.getInt(relocationAddress)));
7676
break;
7777
}
@@ -146,43 +146,37 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
146146
case RISCV_ElfRelocationConstants.R_RISCV_TLS_DTPMOD32:
147147
// TLS relocation word32 = S->TLSINDEX
148148
markAsWarning(program, relocationAddress, "R_RISCV_TLS_DTPMOD32", symbolName,
149-
symbolIndex,
150-
"TODO, needs support ", elfRelocationContext.getLog());
149+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
151150
return RelocationResult.UNSUPPORTED;
152151

153152
case RISCV_ElfRelocationConstants.R_RISCV_TLS_DTPMOD64:
154153
// TLS relocation word64 = S->TLSINDEX
155154
markAsWarning(program, relocationAddress, "R_RISCV_TLS_DTPMOD32", symbolName,
156-
symbolIndex,
157-
"TODO, needs support ", elfRelocationContext.getLog());
155+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
158156
return RelocationResult.UNSUPPORTED;
159157

160158
case RISCV_ElfRelocationConstants.R_RISCV_TLS_DTPREL32:
161159
// TLS relocation word32 = TLS + S + A - TLS_TP_OFFSET
162160
markAsWarning(program, relocationAddress, "R_RISCV_TLS_DTPREL32", symbolName,
163-
symbolIndex,
164-
"TODO, needs support ", elfRelocationContext.getLog());
161+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
165162
return RelocationResult.UNSUPPORTED;
166163

167164
case RISCV_ElfRelocationConstants.R_RISCV_TLS_DTPREL64:
168165
// TLS relocation word64 = TLS + S + A - TLS_TP_OFFSET
169166
markAsWarning(program, relocationAddress, "R_RISCV_TLS_DTPREL64", symbolName,
170-
symbolIndex,
171-
"TODO, needs support ", elfRelocationContext.getLog());
167+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
172168
return RelocationResult.UNSUPPORTED;
173169

174170
case RISCV_ElfRelocationConstants.R_RISCV_TLS_TPREL32:
175171
// TLS relocation word32 = TLS + S + A + S_TLS_OFFSET - TLS_DTV_OFFSET
176172
markAsWarning(program, relocationAddress, "R_RISCV_TLS_DTREL32", symbolName,
177-
symbolIndex,
178-
"TODO, needs support ", elfRelocationContext.getLog());
173+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
179174
return RelocationResult.UNSUPPORTED;
180175

181176
case RISCV_ElfRelocationConstants.R_RISCV_TLS_TPREL64:
182177
// TLS relocation word64 = TLS + S + A + S_TLS_OFFSET - TLS_DTV_OFFSET
183178
markAsWarning(program, relocationAddress, "R_RISCV_TLS_TPREL64", symbolName,
184-
symbolIndex,
185-
"TODO, needs support ", elfRelocationContext.getLog());
179+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
186180
return RelocationResult.UNSUPPORTED;
187181

188182
case RISCV_ElfRelocationConstants.R_RISCV_BRANCH:
@@ -217,50 +211,43 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
217211
case RISCV_ElfRelocationConstants.R_RISCV_CALL_PLT:
218212
// PC-relative call (PLT) MACRO call,tail (auipc+jalr pair) PIC
219213
markAsWarning(program, relocationAddress, "R_RISCV_CALL_PLT", symbolName,
220-
symbolIndex,
221-
"TODO, needs support ", elfRelocationContext.getLog());
214+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
222215
return RelocationResult.UNSUPPORTED;
223216

224217
case RISCV_ElfRelocationConstants.R_RISCV_GOT_HI20:
225218
// PC-relative GOT reference MACRO la
226219
markAsWarning(program, relocationAddress, "R_RISCV_GOT_HI20", symbolName,
227-
symbolIndex,
228-
"TODO, needs support ", elfRelocationContext.getLog());
220+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
229221
return RelocationResult.UNSUPPORTED;
230222

231223
case RISCV_ElfRelocationConstants.R_RISCV_TLS_GOT_HI20:
232224
// PC-relative TLS IE GOT offset MACRO la.tls.ie
233225
markAsWarning(program, relocationAddress, "R_RISCV_TLS_GOT_HI20", symbolName,
234-
symbolIndex,
235-
"TODO, needs support ", elfRelocationContext.getLog());
226+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
236227
return RelocationResult.UNSUPPORTED;
237228

238229
case RISCV_ElfRelocationConstants.R_RISCV_TLS_GD_HI20:
239230
// PC-relative TLS GD reference MACRO la.tls.gd
240231
markAsWarning(program, relocationAddress, "R_RISCV_TLS_GD_HI20", symbolName,
241-
symbolIndex,
242-
"TODO, needs support ", elfRelocationContext.getLog());
232+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
243233
return RelocationResult.UNSUPPORTED;
244234

245235
case RISCV_ElfRelocationConstants.R_RISCV_PCREL_HI20:
246236
// PC-relative reference %pcrel_hi(symbol) (U-Type)
247237
markAsWarning(program, relocationAddress, "R_RISCV_PCREL_HI20", symbolName,
248-
symbolIndex,
249-
"TODO, needs support ", elfRelocationContext.getLog());
238+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
250239
return RelocationResult.UNSUPPORTED;
251240

252241
case RISCV_ElfRelocationConstants.R_RISCV_PCREL_LO12_I:
253242
// PC-relative reference %pcrel_lo(symbol) (I-Type)
254243
markAsWarning(program, relocationAddress, "R_RISCV_PCREL_LO12_I", symbolName,
255-
symbolIndex,
256-
"TODO, needs support ", elfRelocationContext.getLog());
244+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
257245
return RelocationResult.UNSUPPORTED;
258246

259247
case RISCV_ElfRelocationConstants.R_RISCV_PCREL_LO12_S:
260248
// PC-relative reference %pcrel_lo(symbol) (S-Type)
261249
markAsWarning(program, relocationAddress, "R_RISCV_PCREL_LO12_S", symbolName,
262-
symbolIndex,
263-
"TODO, needs support ", elfRelocationContext.getLog());
250+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
264251
return RelocationResult.UNSUPPORTED;
265252

266253
case RISCV_ElfRelocationConstants.R_RISCV_HI20:
@@ -288,35 +275,29 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
288275
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_HI20:
289276
// TLS LE thread offset %tprel_hi(symbol) (U-Type)
290277
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_HI20", symbolName,
291-
symbolIndex,
292-
"TODO, needs support ", elfRelocationContext.getLog());
278+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
293279
return RelocationResult.UNSUPPORTED;
294280

295281
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_LO12_I:
296282
// TLS LE thread offset %tprel_lo(symbol) (I-Type)
297283
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_LO12_I", symbolName,
298-
symbolIndex,
299-
"TODO, needs support ", elfRelocationContext.getLog());
284+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
300285
return RelocationResult.UNSUPPORTED;
301286

302287
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_LO12_S:
303288
// TLS LE thread offset %tprel_lo(symbol) (S-Type)
304289
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_LO12_S", symbolName,
305-
symbolIndex,
306-
"TODO, needs support ", elfRelocationContext.getLog());
290+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
307291
return RelocationResult.UNSUPPORTED;
308292

309293
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_ADD:
310294
// TLS LE thread usage %tprel_add(symbol)
311295
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_ADD", symbolName,
312-
symbolIndex,
313-
"TODO, needs support ", elfRelocationContext.getLog());
296+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
314297
return RelocationResult.UNSUPPORTED;
315298

316299
case RISCV_ElfRelocationConstants.R_RISCV_ADD8:
317300
// 8-bit label addition word8 = old + S + A
318-
markAsWarning(program, relocationAddress, "R_RISCV_ADD8", symbolName, symbolIndex,
319-
"TODO, needs support ", elfRelocationContext.getLog());
320301
value8 = memory.getByte(relocationAddress);
321302
value8 += (byte) symbolValue;
322303
value8 += (byte) addend;
@@ -326,8 +307,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
326307

327308
case RISCV_ElfRelocationConstants.R_RISCV_ADD16:
328309
// 16-bit label addition word16 = old + S + A
329-
markAsWarning(program, relocationAddress, "R_RISCV_ADD16", symbolName, symbolIndex,
330-
"TODO, needs support ", elfRelocationContext.getLog());
331310
value16 = memory.getShort(relocationAddress);
332311
value16 += (short) symbolValue;
333312
value16 += (short) addend;
@@ -337,8 +316,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
337316

338317
case RISCV_ElfRelocationConstants.R_RISCV_ADD32:
339318
// 32-bit label addition word32 = old + S + A
340-
markAsWarning(program, relocationAddress, "R_RISCV_ADD32", symbolName, symbolIndex,
341-
"TODO, needs support ", elfRelocationContext.getLog());
342319
value32 = memory.getInt(relocationAddress);
343320
value32 += (int) symbolValue;
344321
value32 += (int) addend;
@@ -347,8 +324,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
347324

348325
case RISCV_ElfRelocationConstants.R_RISCV_ADD64:
349326
// 64-bit label addition word64 = old + S + A
350-
markAsWarning(program, relocationAddress, "R_RISCV_ADD64", symbolName, symbolIndex,
351-
"TODO, needs support ", elfRelocationContext.getLog());
352327
value64 = memory.getLong(relocationAddress);
353328
value64 += symbolValue;
354329
value64 += addend;
@@ -358,8 +333,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
358333

359334
case RISCV_ElfRelocationConstants.R_RISCV_SUB8:
360335
// 8-bit label subtraction word8 = old - S - A
361-
markAsWarning(program, relocationAddress, "R_RISCV_SUB8", symbolName, symbolIndex,
362-
"TODO, needs support ", elfRelocationContext.getLog());
363336
value8 = memory.getByte(relocationAddress);
364337
value8 -= (byte) symbolValue;
365338
value8 -= (byte) addend;
@@ -369,8 +342,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
369342

370343
case RISCV_ElfRelocationConstants.R_RISCV_SUB16:
371344
// 16-bit label subtraction word16 = old - S - A
372-
markAsWarning(program, relocationAddress, "R_RISCV_SUB16", symbolName, symbolIndex,
373-
"TODO, needs support ", elfRelocationContext.getLog());
374345
value16 = memory.getShort(relocationAddress);
375346
value16 -= (short) symbolValue;
376347
value16 -= (short) addend;
@@ -380,8 +351,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
380351

381352
case RISCV_ElfRelocationConstants.R_RISCV_SUB32:
382353
// 32-bit label subtraction word32 = old - S - A
383-
markAsWarning(program, relocationAddress, "R_RISCV_SUB32", symbolName, symbolIndex,
384-
"TODO, needs support ", elfRelocationContext.getLog());
385354
value32 = memory.getInt(relocationAddress);
386355
value32 -= (int) symbolValue;
387356
value32 -= (int) addend;
@@ -390,8 +359,6 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
390359

391360
case RISCV_ElfRelocationConstants.R_RISCV_SUB64:
392361
// 64-bit label subtraction word64 = old - S - A
393-
markAsWarning(program, relocationAddress, "R_RISCV_SUB64", symbolName, symbolIndex,
394-
"TODO, needs support ", elfRelocationContext.getLog());
395362
value64 = memory.getLong(relocationAddress);
396363
value64 -= symbolValue;
397364
value64 -= addend;
@@ -402,15 +369,13 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
402369
case RISCV_ElfRelocationConstants.R_RISCV_GNU_VTINHERIT:
403370
// GNU C++ vtable hierarchy
404371
markAsWarning(program, relocationAddress, "R_RISCV_GNU_VTINHERIT", symbolName,
405-
symbolIndex,
406-
"TODO, needs support ", elfRelocationContext.getLog());
372+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
407373
return RelocationResult.UNSUPPORTED;
408374

409375
case RISCV_ElfRelocationConstants.R_RISCV_GNU_VTENTRY:
410376
// GNU C++ vtable member usage
411377
markAsWarning(program, relocationAddress, "R_RISCV_GNU_VTENTRY", symbolName,
412-
symbolIndex,
413-
"TODO, needs support ", elfRelocationContext.getLog());
378+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
414379
return RelocationResult.UNSUPPORTED;
415380

416381
case RISCV_ElfRelocationConstants.R_RISCV_ALIGN:
@@ -451,36 +416,31 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
451416
case RISCV_ElfRelocationConstants.R_RISCV_RVC_LUI:
452417
// Absolute address (CI-Type)
453418
markAsWarning(program, relocationAddress, "R_RISCV_RVC_LUI", symbolName,
454-
symbolIndex,
455-
"TODO, needs support ", elfRelocationContext.getLog());
419+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
456420
return RelocationResult.UNSUPPORTED;
457421

458422
case RISCV_ElfRelocationConstants.R_RISCV_GPREL_I:
459423
// GP-relative reference (I-Type)
460424
markAsWarning(program, relocationAddress, "R_RISCV_GPREL_I", symbolName,
461-
symbolIndex,
462-
"TODO, needs support ", elfRelocationContext.getLog());
425+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
463426
return RelocationResult.UNSUPPORTED;
464427

465428
case RISCV_ElfRelocationConstants.R_RISCV_GPREL_S:
466429
// GP-relative reference (S-Type)
467430
markAsWarning(program, relocationAddress, "R_RISCV_GPREL_S", symbolName,
468-
symbolIndex,
469-
"TODO, needs support ", elfRelocationContext.getLog());
431+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
470432
return RelocationResult.UNSUPPORTED;
471433

472434
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_I:
473435
// TP-relative TLS LE load (I-Type)
474436
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_I", symbolName,
475-
symbolIndex,
476-
"TODO, needs support ", elfRelocationContext.getLog());
437+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
477438
return RelocationResult.UNSUPPORTED;
478439

479440
case RISCV_ElfRelocationConstants.R_RISCV_TPREL_S:
480441
// TP-relative TLS LE store (S-Type)
481442
markAsWarning(program, relocationAddress, "R_RISCV_TPREL_S", symbolName,
482-
symbolIndex,
483-
"TODO, needs support ", elfRelocationContext.getLog());
443+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
484444
return RelocationResult.UNSUPPORTED;
485445

486446
case RISCV_ElfRelocationConstants.R_RISCV_RELAX:
@@ -522,8 +482,7 @@ public RelocationResult relocate(ElfRelocationContext elfRelocationContext,
522482
case RISCV_ElfRelocationConstants.R_RISCV_32_PCREL:
523483
// 32-bit PC relative
524484
markAsWarning(program, relocationAddress, "R_RISCV_32_PCREL", symbolName,
525-
symbolIndex,
526-
"TODO, needs support ", elfRelocationContext.getLog());
485+
symbolIndex, "TODO, needs support ", elfRelocationContext.getLog());
527486
return RelocationResult.UNSUPPORTED;
528487

529488
default:

0 commit comments

Comments
 (0)