@@ -277,9 +277,12 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm,
277
277
W.write <uint32_t >(VMAddr); // address
278
278
W.write <uint32_t >(SectionSize); // size
279
279
}
280
+ assert (isUInt<32 >(FileOffset) && " Cannot encode offset of section" );
280
281
W.write <uint32_t >(FileOffset);
281
282
282
283
W.write <uint32_t >(Log2 (Section.getAlign ()));
284
+ assert ((!NumRelocations || isUInt<32 >(RelocationsStart)) &&
285
+ " Cannot encode offset of relocations" );
283
286
W.write <uint32_t >(NumRelocations ? RelocationsStart : 0 );
284
287
W.write <uint32_t >(NumRelocations);
285
288
W.write <uint32_t >(Flags);
@@ -775,6 +778,7 @@ void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
775
778
776
779
uint64_t MachObjectWriter::writeObject (MCAssembler &Asm) {
777
780
uint64_t StartOffset = W.OS .tell ();
781
+ auto NumBytesWritten = [&] { return W.OS .tell () - StartOffset; };
778
782
779
783
populateAddrSigSection (Asm);
780
784
@@ -904,6 +908,18 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
904
908
unsigned Flags = Sec.getTypeAndAttributes ();
905
909
if (Sec.hasInstructions ())
906
910
Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
911
+ if (!cast<MCSectionMachO>(Sec).isVirtualSection () &&
912
+ !isUInt<32 >(SectionStart)) {
913
+ Asm.getContext ().reportError (
914
+ SMLoc (), " cannot encode offset of section; object file too large" );
915
+ return NumBytesWritten ();
916
+ }
917
+ if (NumRelocs && !isUInt<32 >(RelocTableEnd)) {
918
+ Asm.getContext ().reportError (
919
+ SMLoc (),
920
+ " cannot encode offset of relocations; object file too large" );
921
+ return NumBytesWritten ();
922
+ }
907
923
writeSection (Asm, Sec, getSectionAddress (&Sec), SectionStart, Flags,
908
924
RelocTableEnd, NumRelocs);
909
925
RelocTableEnd += NumRelocs * sizeof (MachO::any_relocation_info);
@@ -1088,7 +1104,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
1088
1104
StringTable.write (W.OS );
1089
1105
}
1090
1106
1091
- return W. OS . tell () - StartOffset ;
1107
+ return NumBytesWritten () ;
1092
1108
}
1093
1109
1094
1110
std::unique_ptr<MCObjectWriter>
0 commit comments