[lld] 82c2fcf - ELF: Respect MEMORY command when specified without a SECTIONS command.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 1 22:40:55 PST 2023


Author: Peter Collingbourne
Date: 2023-03-01T22:40:32-08:00
New Revision: 82c2fcffc27319ba4851783498de7c98335f306e

URL: https://github.com/llvm/llvm-project/commit/82c2fcffc27319ba4851783498de7c98335f306e
DIFF: https://github.com/llvm/llvm-project/commit/82c2fcffc27319ba4851783498de7c98335f306e.diff

LOG: ELF: Respect MEMORY command when specified without a SECTIONS command.

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

Differential Revision: https://reviews.llvm.org/D145132

Added: 
    lld/test/ELF/linkerscript/memory-no-sections.test

Modified: 
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 6efc56a81e057..53a7c4b1e673b 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -60,6 +60,7 @@ template <class ELFT> class Writer {
   void finalizeAddressDependentContent();
   void optimizeBasicBlockJumps();
   void sortInputSections();
+  void sortOrphanSections();
   void finalizeSections();
   void checkExecuteOnly();
   void setReservedSymbolSections();
@@ -1456,9 +1457,13 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
   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 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
     std::rotate(pos, nonScriptI, end);
     nonScriptI = end;
   }
-
-  script->adjustSectionsAfterSorting();
 }
 
 static bool compareByFilePosition(InputSection *a, InputSection *b) {

diff  --git a/lld/test/ELF/linkerscript/memory-no-sections.test b/lld/test/ELF/linkerscript/memory-no-sections.test
new file mode 100644
index 0000000000000..c5ce9cd1a4af6
--- /dev/null
+++ b/lld/test/ELF/linkerscript/memory-no-sections.test
@@ -0,0 +1,26 @@
+REQUIRES: x86
+
+## Check that we respect MEMORY commands in linker scripts without
+## SECTIONS commands.
+
+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


        


More information about the llvm-commits mailing list