llvm.org GIT mirror llvm / f85959e
yaml2obj, COFF: Consider the DOS stub when laying out section headers While this program worked correctly with small example programs, larger ones tickled this bug. I'm working on a reduction because my program is quite large. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222078 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 4 years ago
1 changed file(s) with 12 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
151151 return true;
152152 }
153153
154 namespace {
155 enum { DOSStubSize = 128 };
156 }
157
154158 // Take a CP and assign addresses and sizes to everything. Returns false if the
155159 // layout is not valid to do.
156160 static bool layoutCOFF(COFFParser &CP) {
158162 // optional header.
159163 CP.SectionTableStart =
160164 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
165 if (CP.isPE())
166 CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic);
161167 CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
162168
163169 uint32_t CurrentSectionDataOffset =
352358 // 0x40.
353359 DH.AddressOfRelocationTable = sizeof(DH);
354360 // This is the address of the PE signature.
355 DH.AddressOfNewExeHeader = 128;
361 DH.AddressOfNewExeHeader = DOSStubSize;
356362
357363 // Write out our DOS stub.
358364 OS.write(reinterpret_cast(&DH), sizeof(DH));
359365 // Write padding until we reach the position of where our PE signature
360366 // should live.
361 OS << num_zeros(DH.AddressOfNewExeHeader - sizeof(DH));
367 OS << num_zeros(DOSStubSize - sizeof(DH));
362368 // Write out the PE signature.
363369 OS.write(COFF::PEMagic, sizeof(COFF::PEMagic));
364370 }
410416 OS << zeros(uint32_t(0));
411417 }
412418
419 assert(OS.tell() == CP.SectionTableStart);
413420 // Output section table.
414421 for (std::vector::iterator i = CP.Obj.Sections.begin(),
415422 e = CP.Obj.Sections.end();
425432 << binary_le(i->Header.NumberOfLineNumbers)
426433 << binary_le(i->Header.Characteristics);
427434 }
435 assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize);
428436
429437 unsigned CurSymbol = 0;
430438 StringMap SymbolTableIndexMap;
439447 for (const COFFYAML::Section &S : CP.Obj.Sections) {
440448 if (!S.Header.SizeOfRawData)
441449 continue;
450 assert(S.Header.PointerToRawData >= OS.tell());
442451 OS << num_zeros(S.Header.PointerToRawData - OS.tell());
443452 S.SectionData.writeAsBinary(OS);
453 assert(S.Header.SizeOfRawData >= S.SectionData.binary_size());
444454 OS << num_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size());
445455 for (const COFFYAML::Relocation &R : S.Relocations) {
446456 uint32_t SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];