1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-25 17:34:02 +03:00

Add --discard-locals

This commit is contained in:
Rui Ueyama 2020-12-20 10:12:10 +09:00
parent 4c7858ac68
commit 87472ab62f
2 changed files with 14 additions and 3 deletions

3
mold.h
View File

@ -56,7 +56,7 @@ struct Config {
std::string entry = "_start";
std::string output;
bool as_needed = false;
bool discard_locals = true;
bool discard_locals = false;
bool export_dynamic = false;
bool fork = true;
bool is_static = false;
@ -211,6 +211,7 @@ public:
u8 is_imported : 1 = false;
u8 is_weak : 1 = false;
u8 is_undef_weak : 1 = false;
u8 write_symtab : 1 = false;
u8 traced : 1 = false;
std::atomic_uint8_t flags = ATOMIC_VAR_INIT(0);

View File

@ -146,6 +146,14 @@ void ObjectFile::initialize_sections() {
}
}
static bool should_write_symtab(const ElfSym &esym, std::string_view name) {
if (esym.st_type == STT_SECTION)
return false;
if (config.discard_locals && name.starts_with(".L"))
return false;
return true;
}
void ObjectFile::initialize_symbols() {
if (!symtab_sec)
return;
@ -173,6 +181,7 @@ void ObjectFile::initialize_symbols() {
sym.type = esym.st_type;
sym.value = esym.st_value;
sym.esym = &esym;
sym.write_symtab = should_write_symtab(esym, name);
if (!esym.is_abs()) {
if (esym.is_common())
@ -182,7 +191,7 @@ void ObjectFile::initialize_symbols() {
symbols.push_back(&local_symbols.back());
if (esym.st_type != STT_SECTION) {
if (sym.write_symtab) {
strtab_size += name.size() + 1;
local_symtab_size += sizeof(ElfSym);
}
@ -577,7 +586,8 @@ void ObjectFile::write_symtab() {
symtab_off = local_symtab_offset;
for (int i = 1; i < first_global; i++)
write_sym(i);
if (symbols[i]->write_symtab)
write_sym(i);
symtab_off = global_symtab_offset;
for (int i = first_global; i < elf_syms.size(); i++)