[llvm] [yaml2obj][XOFF] Update yaml2obj for XCOFF to create valid XCOFF files in more cases. (PR #77620)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 01:04:04 PST 2024


================
@@ -134,23 +144,46 @@ bool XCOFFWriter::initSectionHeader(uint64_t &CurrentOffset) {
       }
     }
 
-    // Calculate the physical/virtual address. This field should contain 0 for
-    // all sections except the text, data and bss sections.
-    if (InitSections[I].Flags != XCOFF::STYP_TEXT &&
-        InitSections[I].Flags != XCOFF::STYP_DATA &&
-        InitSections[I].Flags != XCOFF::STYP_BSS)
-      InitSections[I].Address = 0;
-    else
-      InitSections[I].Address = CurrentSecAddr;
+    if (!InitSections[I].Size)
+      InitSections[I].Size = InitSections[I].SectionData.binary_size();
+
+    // We cannot compute section addresses in general. We only enforce
+    // the rule .data and .bss are consecutive, as are .tdata and .tbss.
+    switch (InitSections[I].Flags) {
+    case XCOFF::STYP_DATA:
+      CurrentDataAddr = InitSections[I].Address + InitSections[I].Size;
+      break;
+    case XCOFF::STYP_BSS:
+      if (!InitSections[I].Address)
+        InitSections[I].Address = CurrentDataAddr;
+      break;
+    case XCOFF::STYP_TDATA:
+      CurrentTDataAddr = InitSections[I].Address + InitSections[I].Size;
+      break;
+    case XCOFF::STYP_TBSS:
+      if (!InitSections[I].Address)
+        InitSections[I].Address = CurrentTDataAddr;
+      break;
+    }
 
-    // Calculate the FileOffsetToData and data size for sections.
     if (InitSections[I].SectionData.binary_size()) {
-      InitSections[I].FileOffsetToData = CurrentOffset;
+      if (InitSections[I].FileOffsetToData) {
+        // Use the providedFileOffsetToData.
+        if (CurrentOffset > InitSections[I].FileOffsetToData) {
+          ErrHandler("Specified FileOffsetToData will overwrite existing data");
----------------
jh7370 wrote:

Nit: error style

For what it's worth, I think technically this message may be incorrect. You could have something with a specified offset jump a long way forward in the file, leaving a big gap, and then have a later thing want to be written in that hole. I'm not suggesting you try to address this now, especially as I don't know what the ordering rules are for XCOFF files, but it might ultimately be worth considering an approach where everything is a "block" that can be written practically anywhere, with things in place to check that there are no overlaps between blocks.

https://github.com/llvm/llvm-project/pull/77620


More information about the llvm-commits mailing list