@@ -79,6 +79,11 @@ static_assert(sizeof(dosProgram) % 8 == 0,
7979
8080static const int dosStubSize = sizeof (dos_header) + sizeof (dosProgram);
8181static_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);
8287
8388static const int numberOfDataDirectory = 16 ;
8489
@@ -1600,6 +1605,7 @@ void Writer::assignAddresses() {
16001605 for (OutputSection *sec : ctx.outputSections ) {
16011606 llvm::TimeTraceScope timeScope (" Section: " , sec->name );
16021607 if (sec == relocSec) {
1608+ sec->chunks .clear ();
16031609 addBaserels ();
16041610 if (ctx.dynamicRelocs ) {
16051611 ctx.dynamicRelocs ->finalize ();
@@ -1680,6 +1686,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
16801686 buf += sizeof (PEMagic);
16811687
16821688 // Write COFF header
1689+ assert (coffHeaderOffset == buf - buffer->getBufferStart ());
16831690 auto *coff = reinterpret_cast <coff_file_header *>(buf);
16841691 buf += sizeof (*coff);
16851692 switch (config->machine ) {
@@ -1712,6 +1719,7 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
17121719 sizeof (PEHeaderTy) + sizeof (data_directory) * numberOfDataDirectory;
17131720
17141721 // Write PE header
1722+ assert (peHeaderOffset == buf - buffer->getBufferStart ());
17151723 auto *pe = reinterpret_cast <PEHeaderTy *>(buf);
17161724 buf += sizeof (*pe);
17171725 pe->Magic = config->is64 () ? PE32Header::PE32_PLUS : PE32Header::PE32;
@@ -1777,6 +1785,8 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
17771785 pe->SizeOfInitializedData = getSizeOfInitializedData ();
17781786
17791787 // Write data directory
1788+ assert (!ctx.config .is64 () ||
1789+ dataDirOffset64 == buf - buffer->getBufferStart ());
17801790 auto *dir = reinterpret_cast <data_directory *>(buf);
17811791 buf += sizeof (*dir) * numberOfDataDirectory;
17821792 if (edataStart) {
@@ -2532,7 +2542,6 @@ uint32_t Writer::getSizeOfInitializedData() {
25322542void Writer::addBaserels () {
25332543 if (!ctx.config .relocatable )
25342544 return ;
2535- relocSec->chunks .clear ();
25362545 std::vector<Baserel> v;
25372546 for (OutputSection *sec : ctx.outputSections ) {
25382547 if (sec->header .Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
@@ -2570,24 +2579,20 @@ void Writer::createDynamicRelocs() {
25702579 if (!ctx.dynamicRelocs )
25712580 return ;
25722581
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-
25772582 // Adjust the Machine field in the COFF header to AMD64.
25782583 ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint16_t ),
25792584 coffHeaderOffset + offsetof (coff_file_header, Machine),
25802585 AMD64);
25812586
2582- // Adjust the load config directory.
2587+ // Clear the load config directory.
25832588 // FIXME: Use the hybrid load config value instead.
25842589 ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint32_t ),
2585- dataDirOffset +
2590+ dataDirOffset64 +
25862591 LOAD_CONFIG_TABLE * sizeof (data_directory) +
25872592 offsetof (data_directory, RelativeVirtualAddress),
25882593 0 );
25892594 ctx.dynamicRelocs ->add (IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE, sizeof (uint32_t ),
2590- dataDirOffset +
2595+ dataDirOffset64 +
25912596 LOAD_CONFIG_TABLE * sizeof (data_directory) +
25922597 offsetof (data_directory, Size),
25932598 0 );
0 commit comments