@@ -367,89 +367,47 @@ void SectionChunk::writeTo(uint8_t *buf) const {
367
367
continue ;
368
368
}
369
369
370
- applyRelocation (buf + rel.VirtualAddress , rel);
371
- }
372
- }
370
+ uint8_t *off = buf + rel.VirtualAddress ;
373
371
374
- void SectionChunk::applyRelocation (uint8_t *off,
375
- const coff_relocation &rel) const {
376
- auto *sym = dyn_cast_or_null<Defined>(file->getSymbol (rel.SymbolTableIndex ));
377
-
378
- // Get the output section of the symbol for this relocation. The output
379
- // section is needed to compute SECREL and SECTION relocations used in debug
380
- // info.
381
- Chunk *c = sym ? sym->getChunk () : nullptr ;
382
- OutputSection *os = c ? c->getOutputSection () : nullptr ;
372
+ auto *sym =
373
+ dyn_cast_or_null<Defined>(file->getSymbol (rel.SymbolTableIndex ));
383
374
384
- // Skip the relocation if it refers to a discarded section, and diagnose it
385
- // as an error if appropriate. If a symbol was discarded early, it may be
386
- // null. If it was discarded late, the output section will be null, unless
387
- // it was an absolute or synthetic symbol.
388
- if (!sym ||
389
- (!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) {
390
- maybeReportRelocationToDiscarded (this , sym, rel);
391
- return ;
392
- }
375
+ // Get the output section of the symbol for this relocation. The output
376
+ // section is needed to compute SECREL and SECTION relocations used in debug
377
+ // info.
378
+ Chunk *c = sym ? sym->getChunk () : nullptr ;
379
+ OutputSection *os = c ? c->getOutputSection () : nullptr ;
380
+
381
+ // Skip the relocation if it refers to a discarded section, and diagnose it
382
+ // as an error if appropriate. If a symbol was discarded early, it may be
383
+ // null. If it was discarded late, the output section will be null, unless
384
+ // it was an absolute or synthetic symbol.
385
+ if (!sym ||
386
+ (!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) {
387
+ maybeReportRelocationToDiscarded (this , sym, rel);
388
+ continue ;
389
+ }
393
390
394
- uint64_t s = sym->getRVA ();
391
+ uint64_t s = sym->getRVA ();
395
392
396
- // Compute the RVA of the relocation for relative relocations.
397
- uint64_t p = rva + rel.VirtualAddress ;
398
- switch (config->machine ) {
399
- case AMD64:
400
- applyRelX64 (off, rel.Type , os, s, p);
401
- break ;
402
- case I386:
403
- applyRelX86 (off, rel.Type , os, s, p);
404
- break ;
405
- case ARMNT:
406
- applyRelARM (off, rel.Type , os, s, p);
407
- break ;
408
- case ARM64:
409
- applyRelARM64 (off, rel.Type , os, s, p);
410
- break ;
411
- default :
412
- llvm_unreachable (" unknown machine type" );
413
- }
414
- }
415
-
416
- // Defend against unsorted relocations. This may be overly conservative.
417
- void SectionChunk::sortRelocations () {
418
- auto cmpByVa = [](const coff_relocation &l, const coff_relocation &r) {
419
- return l.VirtualAddress < r.VirtualAddress ;
420
- };
421
- if (llvm::is_sorted (getRelocs (), cmpByVa))
422
- return ;
423
- warn (" some relocations in " + file->getName () + " are not sorted" );
424
- MutableArrayRef<coff_relocation> newRelocs (
425
- bAlloc.Allocate <coff_relocation>(relocsSize), relocsSize);
426
- memcpy (newRelocs.data (), relocsData, relocsSize * sizeof (coff_relocation));
427
- llvm::sort (newRelocs, cmpByVa);
428
- setRelocs (newRelocs);
429
- }
430
-
431
- // Similar to writeTo, but suitable for relocating a subsection of the overall
432
- // section.
433
- void SectionChunk::writeAndRelocateSubsection (ArrayRef<uint8_t > sec,
434
- ArrayRef<uint8_t > subsec,
435
- uint32_t &nextRelocIndex,
436
- uint8_t *buf) const {
437
- assert (!subsec.empty () && !sec.empty ());
438
- assert (sec.begin () <= subsec.begin () && subsec.end () <= sec.end () &&
439
- " subsection is not part of this section" );
440
- size_t vaBegin = std::distance (sec.begin (), subsec.begin ());
441
- size_t vaEnd = std::distance (sec.begin (), subsec.end ());
442
- memcpy (buf, subsec.data (), subsec.size ());
443
- for (; nextRelocIndex < relocsSize; ++nextRelocIndex) {
444
- const coff_relocation &rel = relocsData[nextRelocIndex];
445
- // Only apply relocations that apply to this subsection. These checks
446
- // assume that all subsections completely contain their relocations.
447
- // Relocations must not straddle the beginning or end of a subsection.
448
- if (rel.VirtualAddress < vaBegin)
449
- continue ;
450
- if (rel.VirtualAddress + 1 >= vaEnd)
393
+ // Compute the RVA of the relocation for relative relocations.
394
+ uint64_t p = rva + rel.VirtualAddress ;
395
+ switch (config->machine ) {
396
+ case AMD64:
397
+ applyRelX64 (off, rel.Type , os, s, p);
398
+ break ;
399
+ case I386:
400
+ applyRelX86 (off, rel.Type , os, s, p);
401
+ break ;
402
+ case ARMNT:
403
+ applyRelARM (off, rel.Type , os, s, p);
451
404
break ;
452
- applyRelocation (&buf[rel.VirtualAddress - vaBegin], rel);
405
+ case ARM64:
406
+ applyRelARM64 (off, rel.Type , os, s, p);
407
+ break ;
408
+ default :
409
+ llvm_unreachable (" unknown machine type" );
410
+ }
453
411
}
454
412
}
455
413
0 commit comments