Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 8, 2024
1 parent 74129bf commit 3ec4b17
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 17 deletions.
7 changes: 7 additions & 0 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,13 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.dependency_file = ctx.arg.chroot + "/" + ctx.arg.dependency_file;
}

// Mark GC root symbols
for (Symbol<E> *sym : ctx.arg.undefined)
sym->gc_root = true;
for (Symbol<E> *sym : ctx.arg.require_defined)
sym->gc_root = true;
ctx.arg.entry->gc_root = true;

if (version_shown && remaining.empty())
exit(0);
return remaining;
Expand Down
19 changes: 2 additions & 17 deletions elf/gc-sections.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,13 @@ static void collect_root_set(Context<E> &ctx,
}
});

// Add sections containing exported symbols
// Add sections containing gc root or exported symbols
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->symbols)
if (sym->file == file && sym->is_exported)
if (sym->file == file && (sym->gc_root || sym->is_exported))
enqueue_symbol(sym);
});

// Add sections referenced by root symbols.
for (Symbol<E> *sym : ctx.arg.undefined)
enqueue_symbol(sym);

for (Symbol<E> *sym : ctx.arg.require_defined)
enqueue_symbol(sym);

if (!ctx.arg.undefined_glob.empty()) {
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
for (Symbol<E> *sym : file->get_global_syms())
if (sym->file == file && ctx.arg.undefined_glob.find(sym->name()))
enqueue_symbol(sym);
});
}

// .eh_frame consists of variable-length records called CIE and FDE
// records, and they are a unit of inclusion or exclusion.
// We just keep all CIEs and everything that are referenced by them.
Expand Down
3 changes: 3 additions & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,9 @@ class Symbol {
bool has_copyrel : 1 = false;
bool is_copyrel_readonly : 1 = false;

// For --gc-sections
bool gc_root : 1 = false;

// For LTO. True if the symbol is referenced by a regular object (as
// opposed to IR object).
bool referenced_by_regular_obj : 1 = false;
Expand Down
1 change: 1 addition & 0 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ static void mark_live_objects(Context<E> &ctx) {
for (Symbol<E> *sym : file->get_global_syms()) {
if (sym->file == file && ctx.arg.undefined_glob.find(sym->name())) {
file->is_alive = true;
sym->gc_root = true;
break;
}
}
Expand Down

0 comments on commit 3ec4b17

Please sign in to comment.