diff --git a/main.cc b/main.cc index 1b1a784b..df75e211 100644 --- a/main.cc +++ b/main.cc @@ -451,6 +451,23 @@ static void scan_rels() { } } +static void export_dynamic() { + MyTimer t("export_dynamic", before_copy_timer); + if (!config.export_dynamic) + return; + + std::vector> vec(out::objs.size()); + + tbb::parallel_for(0, (int)out::objs.size(), [&](int i) { + for (Symbol *sym : out::objs[i]->symbols) + if (sym->file == out::objs[i]) + vec[i].push_back(sym); + }); + + for (Symbol *sym : flatten(vec)) + out::dynsym->add_symbol(sym); +} + static void fill_symbol_versions() { MyTimer t("fill_symbol_versions", before_copy_timer); @@ -1002,6 +1019,9 @@ int main(int argc, char **argv) { // .got.plt, .dynsym, .dynstr, etc. scan_rels(); + // Put symbols to .dynsym. + export_dynamic(); + // Fill .gnu.version and .gnu.version_r section contents. fill_symbol_versions(); @@ -1014,8 +1034,8 @@ int main(int argc, char **argv) { // section indices to them, so we can fix section header contents // for all output sections. tbb::parallel_for_each(out::chunks, [](OutputChunk *chunk) { - chunk->update_shdr(); - }); + chunk->update_shdr(); + }); // Assign offsets to output sections u64 filesize = set_osec_offsets(out::chunks); diff --git a/output_chunks.cc b/output_chunks.cc index ae78f606..7f6ff6ea 100644 --- a/output_chunks.cc +++ b/output_chunks.cc @@ -27,11 +27,10 @@ void OutputEhdr::copy_buf() { } void OutputShdr::update_shdr() { - int i = 1; + shdr.sh_size = sizeof(ELF64LE::Shdr); for (OutputChunk *chunk : out::chunks) if (chunk->kind != OutputChunk::HEADER) - i++; - shdr.sh_size = i * sizeof(ELF64LE::Shdr); + shdr.sh_size += sizeof(ELF64LE::Shdr); } void OutputShdr::copy_buf() { diff --git a/test/export-dynamic.s b/test/export-dynamic.s index bbe4fcb7..8ae98ff9 100644 --- a/test/export-dynamic.s +++ b/test/export-dynamic.s @@ -1,5 +1,6 @@ -// RUN: cc -o %t.o -c %s -// RUN: mold -o %t.exe %t.o --export-dynamic +// RUN: cc -o %t1.o -c %s +// RUN: cc -shared -fPIC -o %t2.so -xc - < /dev/null +// RUN: mold -o %t.exe %t1.o %t2.so --export-dynamic // RUN: readelf --dyn-syms %t.exe | FileCheck %s .text