Skip to content

Commit

Permalink
Do not strip linker-synthesized symbols when --retain-symbols-file is…
Browse files Browse the repository at this point in the history
… given

This is an attempt to fix #1346
  • Loading branch information
rui314 committed Sep 21, 2024
1 parent 87c9929 commit 0ee12e4
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,7 @@ template <typename E>
static void read_retain_symbols_file(Context<E> &ctx, std::string_view path) {
MappedFile *mf = must_open_file(ctx, std::string(path));
std::string_view data((char *)mf->data, mf->size);

ctx.arg.retain_symbols_file.reset(new std::unordered_set<std::string_view>);
std::vector<Symbol<E> *> vec;

while (!data.empty()) {
size_t pos = data.find('\n');
Expand All @@ -455,8 +454,10 @@ static void read_retain_symbols_file(Context<E> &ctx, std::string_view path) {

name = string_trim(name);
if (!name.empty())
ctx.arg.retain_symbols_file->insert(name);
vec.push_back(get_symbol(ctx, name));
}

ctx.arg.retain_symbols_file = std::move(vec);
}

static bool is_file(std::string_view path) {
Expand Down
6 changes: 0 additions & 6 deletions src/input-files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,6 @@ static bool should_write_to_local_symtab(Context<E> &ctx, Symbol<E> &sym) {

template <typename E>
void ObjectFile<E>::compute_symtab_size(Context<E> &ctx) {
if (ctx.arg.strip_all)
return;

this->output_sym_indices.resize(this->elf_syms.size(), -1);

auto is_alive = [&](Symbol<E> &sym) -> bool {
Expand Down Expand Up @@ -1449,9 +1446,6 @@ bool SharedFile<E>::is_readonly(Symbol<E> *sym) {

template <typename E>
void SharedFile<E>::compute_symtab_size(Context<E> &ctx) {
if (ctx.arg.strip_all)
return;

this->output_sym_indices.resize(this->elf_syms.size(), -1);

// Compute the size of global symbols.
Expand Down
7 changes: 4 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ int mold_main(int argc, char **argv) {

// Handle --retain-symbols-file options if any.
if (ctx.arg.retain_symbols_file)
for (std::string_view name : *ctx.arg.retain_symbols_file)
get_symbol(ctx, name)->write_to_symtab = true;
for (Symbol<E> *sym : *ctx.arg.retain_symbols_file)
sym->write_to_symtab = true;

for (std::string_view arg : ctx.arg.trace_symbol)
get_symbol(ctx, arg)->is_traced = true;
Expand Down Expand Up @@ -610,7 +610,8 @@ int mold_main(int argc, char **argv) {
ctx.verneed->construct(ctx);

// Compute .symtab and .strtab sizes for each file.
create_output_symtab(ctx);
if (!ctx.arg.strip_all)
create_output_symtab(ctx);

// .eh_frame is a special section from the linker's point of view,
// as its contents are parsed and reconstructed by the linker,
Expand Down
2 changes: 1 addition & 1 deletion src/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1990,7 +1990,7 @@ struct Context {
std::string soname;
std::string sysroot;
std::string_view emulation;
std::unique_ptr<std::unordered_set<std::string_view>> retain_symbols_file;
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> ignore_ir_file;
Expand Down
4 changes: 2 additions & 2 deletions src/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void StrtabSection<E>::update_shdr(Context<E> &ctx) {
// affect correctness of the program but helps disassembler to
// disassemble machine code appropriately.
if constexpr (is_arm32<E>)
if (!ctx.arg.strip_all && !ctx.arg.retain_symbols_file)
if (!ctx.arg.strip_all)
offset += sizeof("$a\0$t\0$d");

for (Chunk<E> *chunk : ctx.chunks) {
Expand All @@ -504,7 +504,7 @@ void StrtabSection<E>::copy_buf(Context<E> &ctx) {
buf[0] = '\0';

if constexpr (is_arm32<E>)
if (!ctx.arg.strip_all && !ctx.arg.retain_symbols_file)
if (!ctx.arg.strip_all)
memcpy(buf + 1, "$a\0$t\0$d", 9);
}

Expand Down
8 changes: 3 additions & 5 deletions src/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1744,11 +1744,9 @@ template <typename E>
void create_output_symtab(Context<E> &ctx) {
Timer t(ctx, "compute_symtab_size");

if (!ctx.arg.strip_all && !ctx.arg.retain_symbols_file) {
tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
chunk->compute_symtab_size(ctx);
});
}
tbb::parallel_for_each(ctx.chunks, [&](Chunk<E> *chunk) {
chunk->compute_symtab_size(ctx);
});

tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
file->compute_symtab_size(ctx);
Expand Down
8 changes: 4 additions & 4 deletions test/retain-symbols-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ EOF
$CC -B. -o $t/exe $t/a.o -Wl,--retain-symbols-file=$t/symbols
readelf -W --symbols $t/exe > $t/log

! grep -qw foo $t/log || false
! grep -qw bar $t/log || false
! grep -qw main $t/log || false
! grep -q ' foo$' $t/log || false
! grep -q ' bar$' $t/log || false
! grep -q ' main$' $t/log || false

grep -qw baz $t/log
grep -q ' baz$' $t/log

0 comments on commit 0ee12e4

Please sign in to comment.