[PATCH] D145132: ELF: Respect MEMORY command when specified without a SECTIONS command.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 1 17:08:57 PST 2023


pcc created this revision.
pcc added a reviewer: MaskRay.
Herald added subscribers: arichardson, emaste.
Herald added a project: All.
pcc requested review of this revision.
Herald added a project: LLVM.

We were previously ignoring the MEMORY command unless SECTIONS was also
specified. Fix it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145132

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/linkerscript/memory-no-sections.test


Index: lld/test/ELF/linkerscript/memory-no-sections.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/memory-no-sections.test
@@ -0,0 +1,23 @@
+REQUIRES: x86
+
+RUN: split-file %s %ts
+RUN: llvm-mc -filetype=obj -triple=x86_64 %ts/asm.s -o %t.o
+RUN: ld.lld -o %t -T %ts/script %t.o
+RUN: llvm-readelf -S %t | FileCheck %s
+
+CHECK: .text             PROGBITS        0000000000001000
+CHECK: .data             PROGBITS        0000000010000000
+
+#--- script
+
+MEMORY {
+  flash (rx!w): org = 0x1000, len = 0x3000
+  ram (rwx): org = 0x10000000, len = 0x1000
+}
+
+#--- asm.s
+
+.text
+.byte 1
+.data
+.byte 2
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -60,6 +60,7 @@
   void finalizeAddressDependentContent();
   void optimizeBasicBlockJumps();
   void sortInputSections();
+  void sortOrphanSections();
   void finalizeSections();
   void checkExecuteOnly();
   void setReservedSymbolSections();
@@ -1456,9 +1457,13 @@
   script->processInsertCommands();
   script->adjustOutputSections();
 
-  if (!script->hasSectionsCommand)
-    return;
+  if (script->hasSectionsCommand)
+    sortOrphanSections();
 
+  script->adjustSectionsAfterSorting();
+}
+
+template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
   // Orphan sections are sections present in the input files which are
   // not explicitly placed into the output file by the linker script.
   //
@@ -1533,8 +1538,6 @@
     std::rotate(pos, nonScriptI, end);
     nonScriptI = end;
   }
-
-  script->adjustSectionsAfterSorting();
 }
 
 static bool compareByFilePosition(InputSection *a, InputSection *b) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145132.501703.patch
Type: text/x-patch
Size: 1743 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230302/b1bbffe6/attachment.bin>


More information about the llvm-commits mailing list