@@ -79,6 +79,11 @@ static_assert(sizeof(dosProgram) % 8 == 0,
79
79
80
80
static const int dosStubSize = sizeof (dos_header) + sizeof (dosProgram);
81
81
static_assert (dosStubSize % 8 == 0 , " DOSStub size must be multiple of 8" );
82
+ static const uint32_t coffHeaderOffset = dosStubSize + sizeof (PEMagic);
83
+ static const uint32_t peHeaderOffset =
84
+ coffHeaderOffset + sizeof (coff_file_header);
85
+ static const uint32_t dataDirOffset64 =
86
+ peHeaderOffset + sizeof (pe32plus_header);
82
87
83
88
static const int numberOfDataDirectory = 16 ;
84
89
@@ -1600,6 +1605,7 @@ void Writer::assignAddresses() {
1600
1605
for (OutputSection *sec : ctx.outputSections ) {
1601
1606
llvm::TimeTraceScope timeScope (" Section: " , sec->name );
1602
1607
if (sec == relocSec) {
1608
+ sec->chunks .clear ();
1603
1609
addBaserels ();
1604
1610
if (ctx.dynamicRelocs ) {
1605
1611
ctx.dynamicRelocs ->finalize ();
@@ -1680,6 +1686,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
1680
1686
buf += sizeof (PEMagic);
1681
1687
1682
1688
// Write COFF header
1689
+ assert (coffHeaderOffset == buf - buffer->getBufferStart ());
1683
1690
auto *coff = reinterpret_cast <coff_file_header *>(buf);
1684
1691
buf += sizeof (*coff);
1685
1692
switch (config->machine ) {
@@ -1712,6 +1719,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
1712
1719
sizeof (PEHeaderTy) + sizeof (data_directory) * numberOfDataDirectory;
1713
1720
1714
1721
// Write PE header
1722
+ assert (peHeaderOffset == buf - buffer->getBufferStart ());
1715
1723
auto *pe = reinterpret_cast <PEHeaderTy *>(buf);
1716
1724
buf += sizeof (*pe);
1717
1725
pe->Magic = config->is64 () ? PE32Header::PE32_PLUS : PE32Header::PE32;
@@ -1777,6 +1785,8 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
1777
1785
pe->SizeOfInitializedData = getSizeOfInitializedData ();
1778
1786
1779
1787
// Write data directory
1788
+ assert (!ctx.config .is64 () ||
1789
+ dataDirOffset64 == buf - buffer->getBufferStart ());
1780
1790
auto *dir = reinterpret_cast <data_directory *>(buf);
1781
1791
buf += sizeof (*dir) * numberOfDataDirectory;
1782
1792
if (edataStart) {
@@ -2532,7 +2542,6 @@ uint32_t Writer::getSizeOfInitializedData() {
2532
2542
void Writer::addBaserels () {
2533
2543
if (!ctx.config .relocatable )
2534
2544
return ;
2535
- relocSec->chunks .clear ();
2536
2545
std::vector<Baserel> v;
2537
2546
for (OutputSection *sec : ctx.outputSections ) {
2538
2547
if (sec->header .Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
@@ -2570,24 +2579,20 @@ void Writer::createDynamicRelocs() {
2570
2579
if (!ctx.dynamicRelocs )
2571
2580
return ;
2572
2581
2573
- const uint32_t coffHeaderOffset = dosStubSize + sizeof (PEMagic);
2574
- const uint32_t peHeaderOffset = coffHeaderOffset + sizeof (coff_file_header);
2575
- const uint32_t dataDirOffset = peHeaderOffset + sizeof (pe32plus_header);
2576
-
2577
2582
// Adjust the Machine field in the COFF header to AMD64.
2578
2583
ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint16_t ),
2579
2584
coffHeaderOffset + offsetof (coff_file_header, Machine),
2580
2585
AMD64);
2581
2586
2582
- // Adjust the load config directory.
2587
+ // Clear the load config directory.
2583
2588
// FIXME: Use the hybrid load config value instead.
2584
2589
ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint32_t ),
2585
- dataDirOffset +
2590
+ dataDirOffset64 +
2586
2591
LOAD_CONFIG_TABLE * sizeof (data_directory) +
2587
2592
offsetof (data_directory, RelativeVirtualAddress),
2588
2593
0 );
2589
2594
ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint32_t ),
2590
- dataDirOffset +
2595
+ dataDirOffset64 +
2591
2596
LOAD_CONFIG_TABLE * sizeof (data_directory) +
2592
2597
offsetof (data_directory, Size),
2593
2598
0 );
0 commit comments