mirror of
https://github.com/rui314/mold.git
synced 2024-10-04 16:48:04 +03:00
Revert "[ELF] Garbage-collect C identifier sections"
This reverts commit b23c47ae6d
because
it broke CI.
This commit is contained in:
parent
b23c47ae6d
commit
bb999c5e93
@ -4,7 +4,6 @@
|
||||
|
||||
#include "mold.h"
|
||||
|
||||
#include <tbb/concurrent_set.h>
|
||||
#include <tbb/concurrent_vector.h>
|
||||
#include <tbb/parallel_for_each.h>
|
||||
|
||||
@ -73,49 +72,11 @@ static void visit(Context<E> &ctx, InputSection<E> *isec,
|
||||
}
|
||||
}
|
||||
|
||||
// We synthesize `__start_foo` and `__stop_foo` symbols for section
|
||||
// `foo` if the section name is valid as a C identifier (i.e. it
|
||||
// doesn't start with a '.' and doesn't contain punctuations).
|
||||
//
|
||||
// Such references are implicit, so we need to handle them separately.
|
||||
// In this function, we scan all symbols and keep section `foo` if
|
||||
// there is `__start_foo` or `__stop_foo` symbol.
|
||||
template <typename E>
|
||||
void
|
||||
collect_start_stop_sections(Context<E> &ctx,
|
||||
tbb::concurrent_vector<InputSection<E> *> &rootset) {
|
||||
Timer t(ctx, "collect_start_stop_sections");
|
||||
|
||||
tbb::concurrent_set<std::string_view> set;
|
||||
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
for (Symbol<E> *sym : file->symbols) {
|
||||
if (!sym->file) {
|
||||
std::string_view s = sym->name();
|
||||
if (s.starts_with("__start_"))
|
||||
set.insert(s.substr(8));
|
||||
else if (s.starts_with("__stop_"))
|
||||
set.insert(s.substr(7));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (set.empty())
|
||||
return;
|
||||
|
||||
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
|
||||
for (std::unique_ptr<InputSection<E>> &isec : file->sections)
|
||||
if (isec && isec->is_alive && is_c_identifier(isec->name()) &&
|
||||
set.contains(isec->name()) && isec->is_visited.exchange(true))
|
||||
rootset.push_back(isec.get());
|
||||
});
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
static void
|
||||
collect_root_set(Context<E> &ctx,
|
||||
tbb::concurrent_vector<InputSection<E> *> &rootset) {
|
||||
static tbb::concurrent_vector<InputSection<E> *>
|
||||
collect_root_set(Context<E> &ctx) {
|
||||
Timer t(ctx, "collect_root_set");
|
||||
tbb::concurrent_vector<InputSection<E> *> rootset;
|
||||
|
||||
auto enqueue_section = [&](InputSection<E> *isec) {
|
||||
if (mark_section(isec))
|
||||
@ -145,8 +106,8 @@ collect_root_set(Context<E> &ctx,
|
||||
if (!(flags & SHF_ALLOC))
|
||||
isec->is_visited = true;
|
||||
|
||||
if (is_init_fini(*isec) || (flags & SHF_GNU_RETAIN) ||
|
||||
isec->shdr().sh_type == SHT_NOTE)
|
||||
if (is_init_fini(*isec) || is_c_identifier(isec->name()) ||
|
||||
(flags & SHF_GNU_RETAIN) || isec->shdr().sh_type == SHT_NOTE)
|
||||
enqueue_section(isec.get());
|
||||
}
|
||||
});
|
||||
@ -175,6 +136,8 @@ collect_root_set(Context<E> &ctx,
|
||||
for (const ElfRel<E> &rel : cie.get_rels())
|
||||
enqueue_symbol(file->symbols[rel.r_sym]);
|
||||
});
|
||||
|
||||
return rootset;
|
||||
}
|
||||
|
||||
// Mark all reachable sections
|
||||
@ -228,9 +191,7 @@ void gc_sections(Context<E> &ctx) {
|
||||
|
||||
mark_nonalloc_fragments(ctx);
|
||||
|
||||
tbb::concurrent_vector<InputSection<E> *> rootset;
|
||||
collect_start_stop_sections(ctx, rootset);
|
||||
collect_root_set(ctx, rootset);
|
||||
tbb::concurrent_vector<InputSection<E> *> rootset = collect_root_set(ctx);
|
||||
mark(ctx, rootset);
|
||||
sweep(ctx);
|
||||
}
|
||||
|
@ -1,37 +0,0 @@
|
||||
#!/bin/bash
|
||||
export LC_ALL=C
|
||||
set -e
|
||||
CC="${TEST_CC:-cc}"
|
||||
CXX="${TEST_CXX:-c++}"
|
||||
GCC="${TEST_GCC:-gcc}"
|
||||
GXX="${TEST_GXX:-g++}"
|
||||
OBJDUMP="${OBJDUMP:-objdump}"
|
||||
MACHINE="${MACHINE:-$(uname -m)}"
|
||||
testname=$(basename "$0" .sh)
|
||||
echo -n "Testing $testname ... "
|
||||
t=out/test/elf/$MACHINE/$testname
|
||||
mkdir -p $t
|
||||
|
||||
cat <<EOF | $CC -o $t/a.o -c -xc -
|
||||
int x1 __attribute__((section("foo_section"))) = 3;
|
||||
int x2 __attribute__((section("bar_section"))) = 5;
|
||||
EOF
|
||||
|
||||
cat <<EOF | $CC -o $t/b.o -c -xc -
|
||||
#include <stdio.h>
|
||||
|
||||
extern int __start_foo_section[];
|
||||
|
||||
int main() {
|
||||
printf("foo=%d\n", __start_foo_section[0]);
|
||||
}
|
||||
EOF
|
||||
|
||||
$CC -B. -o $t/exe $t/a.o $t/b.o -Wl,-gc-sections
|
||||
$t/exe | grep -q 'foo=3'
|
||||
|
||||
readelf --sections $t/exe > $t/log
|
||||
grep -q foo_section $t/log
|
||||
! grep -q bar_section $t/log || false
|
||||
|
||||
echo OK
|
Loading…
Reference in New Issue
Block a user