[lld] de0dda5 - [ELF] Warn changed output section address

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 08:14:00 PST 2020


Author: Fangrui Song
Date: 2020-02-21T08:13:29-08:00
New Revision: de0dda54d38137d0714c279a540074fe73822b8b

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

LOG: [ELF] Warn changed output section address

When the output section address (addrExpr) is specified, GNU ld warns if
sh_addr is different. This patch implements the warning.

Note, LinkerScript::assignAddresses can be called more than once. We
need to record the changed section addresses, and only report the
warnings after the addresses are finalized.

Reviewed By: grimar

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

Added: 
    

Modified: 
    lld/ELF/LinkerScript.cpp
    lld/ELF/LinkerScript.h
    lld/ELF/Writer.cpp
    lld/test/ELF/linkerscript/lma-align.test
    lld/test/ELF/linkerscript/section-align2.test

Removed: 
    


################################################################################
diff  --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 8cdb8addd2cd..9c9aacc7eceb 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -839,7 +839,10 @@ void LinkerScript::assignOffsets(OutputSection *sec) {
     expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
                        ctx->memRegion->name, sec->name);
 
+  uint64_t oldDot = dot;
   switchTo(sec);
+  if (sec->addrExpr && oldDot != dot)
+    changedSectionAddresses.push_back({sec, oldDot});
 
   ctx->lmaOffset = 0;
 
@@ -1111,6 +1114,7 @@ const Defined *LinkerScript::assignAddresses() {
   auto deleter = std::make_unique<AddressState>();
   ctx = deleter.get();
   errorOnMissingSection = true;
+  changedSectionAddresses.clear();
   switchTo(aether);
 
   SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);

diff  --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 848e3e406917..1c981b4ba3e0 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -320,6 +320,10 @@ class LinkerScript final {
   // Used to implement INSERT [AFTER|BEFORE]. Contains output sections that need
   // to be reordered.
   std::vector<InsertCommand> insertCommands;
+
+  // Sections whose addresses are not equal to their addrExpr values.
+  std::vector<std::pair<const OutputSection *, uint64_t>>
+      changedSectionAddresses;
 };
 
 extern LinkerScript *script;

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 456c4e7d10d3..9c89b41f0e2c 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1634,6 +1634,17 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
       }
     }
   }
+
+  // If a SECTIONS command is given, addrExpr, if set, is the specified output
+  // section address. Warn if the computed value is 
diff erent from the actual
+  // address.
+  if (!script->hasSectionsCommand)
+    return;
+  for (auto changed : script->changedSectionAddresses) {
+    const OutputSection *os = changed.first;
+    warn("start of section " + os->name + " changes from 0x" +
+         utohexstr(changed.second) + " to 0x" + utohexstr(os->addr));
+  }
 }
 
 static void finalizeSynthetic(SyntheticSection *sec) {

diff  --git a/lld/test/ELF/linkerscript/lma-align.test b/lld/test/ELF/linkerscript/lma-align.test
index 0c03f45f3374..37d7becf9e76 100644
--- a/lld/test/ELF/linkerscript/lma-align.test
+++ b/lld/test/ELF/linkerscript/lma-align.test
@@ -2,9 +2,12 @@
 # RUN: echo '.globl _start; _start: ret; .data.rel.ro; .balign 16; .byte 0; \
 # RUN:   .data; .balign 32; .byte 0; .bss; .byte 0' | \
 # RUN:   llvm-mc -filetype=obj -triple=x86_64 - -o %t.o
-# RUN: ld.lld -T %s %t.o -o %t
+# RUN: ld.lld -T %s %t.o -o %t 2>&1 | FileCheck --check-prefix=WARN %s --implicit-check-not=warning:
 # RUN: llvm-readelf -S -l %t | FileCheck %s
 
+# WARN: warning: start of section .data changes from 0x11001 to 0x11010
+# WARN: warning: start of section .bss changes from 0x11021 to 0x11040
+
 # CHECK:      Name         Type     Address          Off    Size   ES Flg Lk Inf Al
 # CHECK-NEXT:              NULL     0000000000000000 000000 000000 00      0   0  0
 # CHECK-NEXT: .text        PROGBITS 0000000000001000 001000 000001 00  AX  0   0  4

diff  --git a/lld/test/ELF/linkerscript/section-align2.test b/lld/test/ELF/linkerscript/section-align2.test
index 9225cddc3a08..0c48c9c7397a 100644
--- a/lld/test/ELF/linkerscript/section-align2.test
+++ b/lld/test/ELF/linkerscript/section-align2.test
@@ -4,9 +4,15 @@
 # RUN: echo '.globl _start; _start: ret; .data.rel.ro; .balign 8; .byte 0; .data; .byte 0; \
 # RUN:   .section .data2,"aw"; .balign 8; .byte 0; .bss; .balign 32; .byte 0' | \
 # RUN:   llvm-mc -filetype=obj -triple=aarch64 - -o %t.o
-# RUN: ld.lld -T %s %t.o -o %t 2>&1 | count 0
+# RUN: ld.lld -T %s %t.o -o %t 2>&1 | FileCheck --check-prefix=WARN %s --implicit-check-not=warning:
 # RUN: llvm-readelf -S %t | FileCheck %s
 
+## Check we don't warn in the absence of SECTIONS.
+# RUN: ld.lld --fatal-warnings -Ttext=0x10000 %t.o -o /dev/null
+
+# WARN: warning: start of section .data.rel.ro changes from 0x10004 to 0x10010
+# WARN: warning: start of section .bss changes from 0x20009 to 0x20010
+
 # CHECK:      Name         Type     Address          Off    Size   ES Flg Lk Inf Al
 # CHECK-NEXT:              NULL     0000000000000000 000000 000000 00      0   0  0
 # CHECK-NEXT: .text        PROGBITS 0000000000010000 010000 000004 00  AX  0   0  4


        


More information about the llvm-commits mailing list