[PATCH] D43863: [ELF] - Do not remove empty sections that use symbols in expressions.
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 28 10:33:13 PST 2018
LGTM.
If we even decide to drop these sections something like this would be
needed to split ReferencedSymbols into a per CMD list.
Cheers,
Rafael
George Rimar via Phabricator via llvm-commits
<llvm-commits at lists.llvm.org> writes:
> grimar updated this revision to Diff 136265.
> grimar added a comment.
>
> - Updated testcase in according to review comments.
>
>
> https://reviews.llvm.org/D43863
>
> Files:
> ELF/OutputSections.cpp
> ELF/OutputSections.h
> ELF/ScriptParser.cpp
> test/ELF/linkerscript/empty-sections-expressions.s
>
>
> Index: test/ELF/linkerscript/empty-sections-expressions.s
> ===================================================================
> --- test/ELF/linkerscript/empty-sections-expressions.s
> +++ test/ELF/linkerscript/empty-sections-expressions.s
> @@ -0,0 +1,18 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> +
> +## We remove empty sections that do not reference symbols in address,
> +## LMA, align and subalign expressions. Here we check that.
> +
> +# RUN: echo "SECTIONS { .debug_info 0 : { *(.debug_info) } }" > %t.script
> +# RUN: ld.lld -o %t --script %t.script %t.o
> +# RUN: llvm-objdump -section-headers %t | FileCheck %s
> +# CHECK-NOT: .debug_info
> +
> +# RUN: echo "SECTIONS { .debug_info foo : { *(.debug_info) } }" > %t2.script
> +# RUN: ld.lld -o %t2 --script %t2.script %t.o
> +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=SEC
> +# SEC: .debug_info
> +
> +.globl foo
> +foo:
> Index: ELF/ScriptParser.cpp
> ===================================================================
> --- ELF/ScriptParser.cpp
> +++ ELF/ScriptParser.cpp
> @@ -668,6 +668,8 @@
> OutputSection *Cmd =
> Script->createOutputSection(OutSec, getCurrentLocation());
>
> + size_t SymbolsReferenced = Script->ReferencedSymbols.size();
> +
> if (peek() != ":")
> readSectionAddressType(Cmd);
> expect(":");
> @@ -734,6 +736,8 @@
> // Consume optional comma following output section command.
> consume(",");
>
> + if (Script->ReferencedSymbols.size() > SymbolsReferenced)
> + Cmd->ExpressionsUseSymbols = true;
> return Cmd;
> }
>
> Index: ELF/OutputSections.h
> ===================================================================
> --- ELF/OutputSections.h
> +++ ELF/OutputSections.h
> @@ -103,6 +103,7 @@
> std::string LMARegionName;
> bool NonAlloc = false;
> bool Noload = false;
> + bool ExpressionsUseSymbols = false;
>
> template <class ELFT> void finalize();
> template <class ELFT> void writeTo(uint8_t *Buf);
> Index: ELF/OutputSections.cpp
> ===================================================================
> --- ELF/OutputSections.cpp
> +++ ELF/OutputSections.cpp
> @@ -84,11 +84,10 @@
> if (!Phdrs.empty())
> return false;
>
> - // We do not want to remove sections that have custom address or align
> - // expressions set even if them are empty. We keep them because we
> - // want to be sure that any expressions can be evaluated and report
> - // an error otherwise.
> - if (AddrExpr || AlignExpr || LMAExpr)
> + // We do not want to remove sections that reference symbols in address and
> + // other expressions. We add script symbols as undefined, and want to ensure
> + // all of them are defined in output, hence have to keep them.
> + if (ExpressionsUseSymbols)
> return false;
>
> for (BaseCommand *Base : SectionCommands)
>
>
> Index: test/ELF/linkerscript/empty-sections-expressions.s
> ===================================================================
> --- test/ELF/linkerscript/empty-sections-expressions.s
> +++ test/ELF/linkerscript/empty-sections-expressions.s
> @@ -0,0 +1,18 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> +
> +## We remove empty sections that do not reference symbols in address,
> +## LMA, align and subalign expressions. Here we check that.
> +
> +# RUN: echo "SECTIONS { .debug_info 0 : { *(.debug_info) } }" > %t.script
> +# RUN: ld.lld -o %t --script %t.script %t.o
> +# RUN: llvm-objdump -section-headers %t | FileCheck %s
> +# CHECK-NOT: .debug_info
> +
> +# RUN: echo "SECTIONS { .debug_info foo : { *(.debug_info) } }" > %t2.script
> +# RUN: ld.lld -o %t2 --script %t2.script %t.o
> +# RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=SEC
> +# SEC: .debug_info
> +
> +.globl foo
> +foo:
> Index: ELF/ScriptParser.cpp
> ===================================================================
> --- ELF/ScriptParser.cpp
> +++ ELF/ScriptParser.cpp
> @@ -668,6 +668,8 @@
> OutputSection *Cmd =
> Script->createOutputSection(OutSec, getCurrentLocation());
>
> + size_t SymbolsReferenced = Script->ReferencedSymbols.size();
> +
> if (peek() != ":")
> readSectionAddressType(Cmd);
> expect(":");
> @@ -734,6 +736,8 @@
> // Consume optional comma following output section command.
> consume(",");
>
> + if (Script->ReferencedSymbols.size() > SymbolsReferenced)
> + Cmd->ExpressionsUseSymbols = true;
> return Cmd;
> }
>
> Index: ELF/OutputSections.h
> ===================================================================
> --- ELF/OutputSections.h
> +++ ELF/OutputSections.h
> @@ -103,6 +103,7 @@
> std::string LMARegionName;
> bool NonAlloc = false;
> bool Noload = false;
> + bool ExpressionsUseSymbols = false;
>
> template <class ELFT> void finalize();
> template <class ELFT> void writeTo(uint8_t *Buf);
> Index: ELF/OutputSections.cpp
> ===================================================================
> --- ELF/OutputSections.cpp
> +++ ELF/OutputSections.cpp
> @@ -84,11 +84,10 @@
> if (!Phdrs.empty())
> return false;
>
> - // We do not want to remove sections that have custom address or align
> - // expressions set even if them are empty. We keep them because we
> - // want to be sure that any expressions can be evaluated and report
> - // an error otherwise.
> - if (AddrExpr || AlignExpr || LMAExpr)
> + // We do not want to remove sections that reference symbols in address and
> + // other expressions. We add script symbols as undefined, and want to ensure
> + // all of them are defined in output, hence have to keep them.
> + if (ExpressionsUseSymbols)
> return false;
>
> for (BaseCommand *Base : SectionCommands)
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list