diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -271,6 +271,10 @@ uint64_t dot; + // Sections whose addresses are not equal to their addrExpr values. + std::vector> + changedSectionAddresses; + public: OutputSection *createOutputSection(StringRef name, StringRef location); OutputSection *getOrCreateOutputSection(StringRef name); @@ -290,6 +294,7 @@ bool shouldKeep(InputSectionBase *s); const Defined *assignAddresses(); + void warnChangedSectionAddresses(); void allocateHeaders(std::vector &phdrs); void processSectionCommands(); void processSymbolAssignments(); diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -836,7 +836,10 @@ 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; @@ -1108,6 +1111,7 @@ auto deleter = std::make_unique(); ctx = deleter.get(); errorOnMissingSection = true; + changedSectionAddresses.clear(); switchTo(aether); SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands); @@ -1125,6 +1129,16 @@ return getChangedSymbolAssignment(oldValues); } +// If a SECTIONS command is given, addrExpr is the specified output section +// address. Warn if the computed value is different from the actual address. +void LinkerScript::warnChangedSectionAddresses() { + for (auto changed : changedSectionAddresses) { + const OutputSection *os = changed.first; + warn("start of section " + os->name + " changes from 0x" + + utohexstr(changed.second) + " to 0x" + utohexstr(os->addr)); + } +} + // Creates program headers as instructed by PHDRS linker script command. std::vector LinkerScript::createPhdrs() { std::vector ret; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -1612,6 +1612,9 @@ } } } + + if (script->hasSectionsCommand) + script->warnChangedSectionAddresses(); } static void finalizeSynthetic(SyntheticSection *sec) { diff --git a/lld/test/ELF/linkerscript/lma-align.test b/lld/test/ELF/linkerscript/lma-align.test --- 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 --- 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