12
12
#ifdef LOGGING
13
13
#include " log.h"
14
14
15
- static const char * const RelocName[] = {
16
- " Absolute" , " Unk1" , " Unk2" , " HighLow" , " Unk4" , " MapToken" ,
17
- " Relative" , " FilePos" , " CodeRel" , " Unk3" , " Dir64" , " AbsTag" };
15
+ static const char * const RelocName[] = { " Absolute" , " HighLow" , " MapToken" , " FilePos" };
18
16
static const char RelocSpaces[] = " " ;
19
17
20
18
#endif
@@ -36,18 +34,6 @@ inline static unsigned padLen(unsigned len, unsigned align) {
36
34
return (roundUp (len, align) - len);
37
35
}
38
36
39
- #ifndef IMAGE_DLLCHARACTERISTICS_NO_SEH
40
- #define IMAGE_DLLCHARACTERISTICS_NO_SEH 0x400
41
- #endif
42
-
43
- #ifndef IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
44
- #define IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE 0x0040
45
- #endif
46
-
47
- #ifndef IMAGE_DLLCHARACTERISTICS_NX_COMPAT
48
- #define IMAGE_DLLCHARACTERISTICS_NX_COMPAT 0x0100
49
- #endif
50
-
51
37
#define COPY_AND_ADVANCE (target, src, size ) { \
52
38
::memcpy ((void *) (target), (const void *) (src), (size)); \
53
39
(char *&) (target) += (size); }
@@ -294,20 +280,11 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
294
280
295
281
int curType = cur->type ;
296
282
DWORD curOffset = cur->offset ;
297
- bool isRelocPtr = ((curType & srRelocPtr) != 0 );
298
- bool noBaseBaseReloc = ((curType & srNoBaseReloc) != 0 );
299
283
UINT64 targetOffset = 0 ;
300
284
int slotNum = 0 ;
285
+ #ifdef LOGGING
301
286
INT64 oldStarPos;
302
-
303
- // If cur->section is NULL then this is a pointer outside the module.
304
- bool externalAddress = (cur->section == NULL );
305
-
306
- curType &= ~(srRelocPtr | srNoBaseReloc);
307
-
308
- /* If we see any srRelocHighLow's in a PE64 file we convert them into DIR64 relocs */
309
- if (!isPE32 && (curType == srRelocHighLow))
310
- curType = srRelocDir64;
287
+ #endif
311
288
312
289
DWORD curRVA = m_baseRVA; // RVA in the PE image of the reloc site
313
290
IfFailRet (AddOvf_RVA (curRVA, curOffset));
@@ -317,87 +294,32 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
317
294
318
295
#ifdef LOGGING
319
296
LOG ((LF_ZAP, LL_INFO1000000,
320
- " Reloc %s%s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
321
- RelocName[curType], (isRelocPtr) ? " Ptr" : " " ,
297
+ " Reloc %s%s at %-7s+%04x (RVA=%08x) at" FMT_ADDR,
322
298
&RelocSpaces[strlen (RelocName[curType])],
323
299
m_name, curOffset, curRVA, DBG_ADDR (pos)));
324
- #endif
325
- //
326
- // 'pos' is the site of the reloc
327
- // Compute 'targetOffset' from pointer if necessary
328
- //
329
-
330
- if (isRelocPtr)
331
- {
332
- // Calculate the value of ptr to pass to computeOffset
333
- char * ptr = (char *) pos;
334
300
335
- if (curType == srRelocRelative) {
336
- //
337
- // Here we add sizeof(int) because we need to calculate
338
- // ptr as the true call target address (x86 pc-rel)
339
- // We need to true call target address since pass it
340
- // to computeOffset and this function would fall if
341
- // the address we pass is before the start of a section
342
- //
343
- oldStarPos = (SSIZE_T) ptr;
344
- IfFailRet (AddOvf_S_S32 (oldStarPos, GET_UNALIGNED_INT32 (pos)));
345
- IfFailRet (AddOvf_S_U32 (oldStarPos, sizeof (int )));
346
- ptr = (char *) oldStarPos;
347
- targetOffset = externalAddress ? (size_t ) ptr
348
- : cur->section ->computeOffset (ptr);
349
- // We subtract off the four bytes that we added previous
350
- // since the code below depends upon this
351
- IfFailRet (SubOvf_U_U32 (targetOffset, sizeof (int )));
352
- IfFailRet (UnsignedFitsIn32Bits (targetOffset)); // Check for overflow
353
- SET_UNALIGNED_VAL32 (pos, targetOffset);
354
- }
355
- else {
356
- ptr = (char *) GET_UNALIGNED_VALPTR (ptr);
357
- oldStarPos = (SSIZE_T) ptr;
358
- targetOffset = externalAddress ? (size_t ) ptr
359
- : cur->section ->computeOffset (ptr);
360
- IfFailRet (UnsignedFitsIn32Bits (targetOffset)); // Check for overflow
361
- SET_UNALIGNED_VAL32 (pos, targetOffset);
362
- /* Zero the upper 32-bits for a machine with 64-bit pointers */
363
- if (!isPE32)
364
- SET_UNALIGNED_VAL32 (pos+1 , 0 );
365
- }
366
- }
367
- #ifdef LOGGING
368
- else
369
- {
370
- oldStarPos = GET_UNALIGNED_VAL32 (pos);
371
- }
301
+ oldStarPos = GET_UNALIGNED_VAL32 (pos);
372
302
#endif
373
303
374
304
//
375
305
// 'targetOffset' has now been computed. Write out the appropriate value.
376
306
// Record base relocs as necessary.
377
307
//
378
308
379
- bool fBaseReloc = false ;
380
- bool fNeedBrl = false ;
309
+ int baseReloc = 0 ;
381
310
INT64 newStarPos = 0 ; // oldStarPos gets updated to newStarPos
382
311
383
- if (curType == srRelocAbsolute || curType == srRelocAbsoluteTagged) {
384
- _ASSERTE (!externalAddress);
312
+ if (curType == srRelocAbsolute) {
385
313
386
314
newStarPos = GET_UNALIGNED_INT32 (pos);
387
315
388
- if (curType == srRelocAbsoluteTagged)
389
- newStarPos = (newStarPos & ~0x80000001 ) >> 1 ;
390
-
391
316
if (rdataRvaBase > 0 && ! strcmp ((const char *)(cur->section ->m_name ), " .rdata" ))
392
317
IfFailRet (AddOvf_S_U32 (newStarPos, rdataRvaBase));
393
318
else if (dataRvaBase > 0 && ! strcmp ((const char *)(cur->section ->m_name ), " .data" ))
394
319
IfFailRet (AddOvf_S_U32 (newStarPos, dataRvaBase));
395
320
else
396
321
IfFailRet (AddOvf_S_U32 (newStarPos, cur->section ->m_baseRVA ));
397
322
398
- if (curType == srRelocAbsoluteTagged)
399
- newStarPos = (newStarPos << 1 ) | 0x80000001 ;
400
-
401
323
SET_UNALIGNED_VAL32 (pos, newStarPos);
402
324
}
403
325
else if (curType == srRelocMapToken)
@@ -411,110 +333,61 @@ HRESULT PEWriterSection::applyRelocs(IMAGE_NT_HEADERS * pNtHeaders,
411
333
}
412
334
else if (curType == srRelocFilePos)
413
335
{
414
- _ASSERTE (!externalAddress);
415
336
newStarPos = GET_UNALIGNED_VAL32 (pos);
416
337
IfFailRet (AddOvf_S_U32 (newStarPos, cur->section ->m_filePos ));
417
338
SET_UNALIGNED_VAL32 (pos, newStarPos);
418
339
}
419
- else if (curType == srRelocRelative)
420
- {
421
- if (externalAddress) {
422
- #if defined(HOST_AMD64)
423
- newStarPos = GET_UNALIGNED_INT32 (pos);
424
- #else // x86
425
- UINT64 targetAddr = GET_UNALIGNED_VAL32 (pos);
426
- IfFailRet (SubOvf_U_U (newStarPos, targetAddr, imageBase));
427
- #endif
428
- }
429
- else {
430
- newStarPos = GET_UNALIGNED_INT32 (pos);
431
- IfFailRet (AddOvf_S_U32 (newStarPos, cur->section ->m_baseRVA ));
432
- }
433
- IfFailRet (SubOvf_S_U32 (newStarPos, curRVA));
434
- IfFailRet (SignedFitsIn31Bits (newStarPos)); // Check for overflow
435
- SET_UNALIGNED_VAL32 (pos, newStarPos);
436
- }
437
- else if (curType == srRelocCodeRelative)
438
- {
439
- newStarPos = GET_UNALIGNED_INT32 (pos);
440
- IfFailRet (SubOvf_S_U32 (newStarPos, codeRvaBase));
441
- if (externalAddress)
442
- IfFailRet (SubOvf_S_U (newStarPos, imageBase));
443
- else
444
- IfFailRet (AddOvf_S_U32 (newStarPos, cur->section ->m_baseRVA ));
445
- IfFailRet (SignedFitsIn31Bits (newStarPos)); // Check for overflow
446
- SET_UNALIGNED_VAL32 (pos, newStarPos);
447
-
448
- }
449
340
else if (curType == srRelocHighLow)
450
341
{
451
- _ASSERTE (isPE32);
452
-
453
- // we have a 32-bit value at pos
454
- UINT64 value = GET_UNALIGNED_VAL32 (pos);
455
-
456
- if (!externalAddress)
342
+ if (isPE32)
457
343
{
344
+ // we have a 32-bit value at pos
345
+ UINT64 value = GET_UNALIGNED_VAL32 (pos);
346
+
458
347
IfFailRet (AddOvf_U_U32 (value, cur->section ->m_baseRVA ));
459
348
IfFailRet (AddOvf_U_U (value, imageBase));
460
- }
461
349
462
- IfFailRet (UnsignedFitsIn32Bits (value)); // Check for overflow
463
- SET_UNALIGNED_VAL32 (pos, value);
350
+ IfFailRet (UnsignedFitsIn32Bits (value)); // Check for overflow
351
+ SET_UNALIGNED_VAL32 (pos, value);
464
352
465
- newStarPos = value;
353
+ newStarPos = value;
466
354
467
- fBaseReloc = true ;
468
- }
469
- else if (curType == srRelocDir64)
470
- {
471
- _ASSERTE (!isPE32);
472
-
473
- // we have a 64-bit value at pos
474
- UINT64 UNALIGNED * p_value = (UINT64 *) pos;
475
- targetOffset = *p_value;
476
-
477
- if (!externalAddress)
355
+ baseReloc = IMAGE_REL_BASED_HIGHLOW;
356
+ }
357
+ else
478
358
{
359
+ // we have a 64-bit value at pos
360
+ UINT64 UNALIGNED * p_value = (UINT64 *) pos;
361
+ targetOffset = *p_value;
362
+
479
363
// The upper bits of targetOffset must be zero
480
364
IfFailRet (UnsignedFitsIn32Bits (targetOffset));
481
365
482
366
IfFailRet (AddOvf_U_U32 (targetOffset, cur->section ->m_baseRVA ));
483
367
IfFailRet (AddOvf_U_U (targetOffset, imageBase));
484
- }
485
368
486
- *p_value = targetOffset;
487
- newStarPos = targetOffset;
488
- fBaseReloc = true ;
369
+ *p_value = targetOffset;
370
+ newStarPos = targetOffset;
371
+
372
+ baseReloc = IMAGE_REL_BASED_DIR64;
373
+ }
489
374
}
490
375
else
491
376
{
492
377
_ASSERTE (!" Unknown Relocation type" );
493
378
}
494
379
495
- if (fBaseReloc && !noBaseBaseReloc )
380
+ if (baseReloc != 0 )
496
381
{
497
- pBaseRelocSection->AddBaseReloc (curRVA, curType );
382
+ pBaseRelocSection->AddBaseReloc (curRVA, baseReloc );
498
383
}
499
384
500
385
#ifdef LOGGING
501
- const char * sectionName;
502
-
503
- if (externalAddress)
504
- {
505
- sectionName = " external" ;
506
- }
507
- else
508
- {
509
- sectionName = cur->section ->m_name ;
510
- }
511
-
512
386
LOG ((LF_ZAP, LL_INFO1000000,
513
- " to %-7s+%04x, old =" FMT_ADDR " new =" FMT_ADDR " %s%s \n " ,
514
- sectionName , targetOffset,
387
+ " to %-7s+%04x, old =" FMT_ADDR " new =" FMT_ADDR " %s\n " ,
388
+ cur-> section -> m_name , targetOffset,
515
389
DBG_ADDR (oldStarPos), DBG_ADDR (newStarPos),
516
- fBaseReloc ? " (BASE RELOC)" : " " ,
517
- fNeedBrl ? " (BRL)" : " " ));
390
+ baseReloc ? " (BASE RELOC)" : " " ));
518
391
#endif
519
392
520
393
}
0 commit comments