Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ static const char helpmsg[] = R"(
--no-demangle
--detach Create separate debug info file in the background (default)
--no-detach
--discard-section SECTION Discard an input section
--no-discard-section SECTION
--enable-new-dtags Emit DT_RUNPATH for --rpath (default)
--disable-new-dtags Emit DT_RPATH for --rpath
--execute-only Make executable segments unreadable
Expand Down Expand Up @@ -1128,6 +1130,10 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.print_gc_sections = true;
} else if (read_flag("no-print-gc-sections")) {
ctx.arg.print_gc_sections = false;
} else if (read_arg("discard-section")) {
ctx.arg.discard_section.insert(arg);
} else if (read_arg("no-discard-section")) {
ctx.arg.discard_section.erase(arg);
} else if (read_arg("icf")) {
if (arg == "all") {
ctx.arg.icf = true;
Expand Down
4 changes: 4 additions & 0 deletions src/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ void ObjectFile<E>::initialize_sections(Context<E> &ctx) {
is_debug_section(shdr, name))
continue;

// Ignore section is specified by --discard-section.
if (ctx.arg.discard_section.contains(name))
continue;

if (name == ".comment" &&
this->get_string(ctx, shdr).starts_with("rustc "))
this->is_rust_obj = true;
Expand Down
1 change: 1 addition & 0 deletions src/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,7 @@ struct Context {
std::optional<std::vector<Symbol<E> *>> retain_symbols_file;
std::unordered_map<std::string_view, u64> section_align;
std::unordered_map<std::string_view, u64> section_start;
std::unordered_set<std::string_view> discard_section;
std::unordered_set<std::string_view> ignore_ir_file;
std::unordered_set<std::string_view> wrap;
std::vector<SectionOrder> section_order;
Expand Down
Empty file added test/discard-section.s
Empty file.
25 changes: 25 additions & 0 deletions test/discard-section.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<'EOF' | $CC -x assembler -c -o $t/a.o -x assembler -

.section .foo,"a"
.ascii "foo\0"
.section .bar,"a"
.ascii "bar\0"
.section .text
.globl _start
_start:
EOF

./mold -o $t/exe0 $t/a.o
readelf -S $t/exe0 | grep '.foo'
readelf -S $t/exe0 | grep '.bar'

./mold -o $t/exe1 $t/a.o --discard-section='.foo'
readelf -S $t/exe1 | grep -v '.foo'
readelf -S $t/exe1 | grep '.bar'

./mold -o $t/exe2 $t/a.o --discard-section='foo' --discard-section='boo' --no-discard-section='foo'
readelf -S $t/exe2 | grep '.foo'
readelf -S $t/exe2 | grep -v '.bar'
Binary file added test/out/test/x86_64/repro/exe
Binary file not shown.