1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-24 00:43:18 +03:00
This commit is contained in:
Rui Ueyama 2021-03-25 12:29:41 +09:00
parent 5f104f5684
commit fe626ed878
2 changed files with 25 additions and 53 deletions

View File

@ -877,12 +877,6 @@ static bool should_write_to_global_symtab(Symbol &sym) {
return sym.get_type() != STT_SECTION && sym.is_alive();
}
static u32 get_binding(Symbol &sym) {
if (sym.ver_idx == VER_NDX_LOCAL)
return STB_LOCAL;
return sym.esym->st_bind;
}
void ObjectFile::compute_symtab() {
if (config.strip_all)
return;
@ -908,11 +902,7 @@ void ObjectFile::compute_symtab() {
if (sym.file == this && should_write_to_global_symtab(sym)) {
strtab_size += sym.name.size() + 1;
sym.write_to_symtab = true;
if (get_binding(sym) == STB_LOCAL)
num_local_symtab++;
else
num_global_symtab++;
num_global_symtab++;
}
}
}
@ -921,11 +911,12 @@ void ObjectFile::write_symtab() {
u8 *symtab_base = out::buf + out::symtab->shdr.sh_offset;
u8 *strtab_base = out::buf + out::strtab->shdr.sh_offset;
i64 strtab_off = strtab_offset;
i64 symtab_off;
auto write_sym = [&](i64 i, u32 binding, i64 &offset) {
auto write_sym = [&](i64 i) {
Symbol &sym = *symbols[i];
ElfSym &esym = *(ElfSym *)(symtab_base + offset);
offset += sizeof(ElfSym);
ElfSym &esym = *(ElfSym *)(symtab_base + symtab_off);
symtab_off += sizeof(ElfSym);
esym = elf_syms[i];
esym.st_name = strtab_off;
@ -948,22 +939,15 @@ void ObjectFile::write_symtab() {
strtab_off += sym.name.size() + 1;
};
i64 local_offset = local_symtab_offset;
i64 global_offset = global_symtab_offset;
symtab_off = local_symtab_offset;
for (i64 i = 1; i < first_global; i++)
if (symbols[i]->write_to_symtab)
write_sym(i, STB_LOCAL, local_offset);
write_sym(i);
for (i64 i = first_global; i < elf_syms.size(); i++) {
if (symbols[i]->file == this && symbols[i]->write_to_symtab) {
u32 binding = get_binding(*symbols[i]);
if (binding == STB_LOCAL)
write_sym(i, binding, local_offset);
else
write_sym(i, binding, global_offset);
}
}
symtab_off = global_symtab_offset;
for (i64 i = first_global; i < elf_syms.size(); i++)
if (symbols[i]->file == this && symbols[i]->write_to_symtab)
write_sym(i);
}
bool is_c_identifier(std::string_view name) {

View File

@ -17,34 +17,22 @@ cat <<EOF | cc -o $t/b.o -c -x assembler -
.globl this_is_global
local2:
this_is_global:
.globl module_local
module_local:
EOF
../mold -o $t/exe $t/a.o $t/b.o
echo '{ local: module_local; };' > $t/c.map
readelf --symbols $t/exe | grep -q "
Symbol table '.symtab' contains 21 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000201010 0 NOTYPE LOCAL DEFAULT 6 local1
2: 0000000000201011 0 NOTYPE LOCAL DEFAULT 6 local2
3: 0000000000201010 0 NOTYPE GLOBAL DEFAULT 6 foo
4: 0000000000201010 0 NOTYPE GLOBAL DEFAULT 6 bar
5: 0000000000201011 0 NOTYPE GLOBAL DEFAULT 6 this_is_global
6: 0000000000200000 0 NOTYPE GLOBAL HIDDEN 1 __ehdr_start
7: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __rela_iplt_start
8: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __rela_iplt_end
9: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __init_array_start
10: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __init_array_end
11: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __fini_array_start
12: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __fini_array_end
13: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __preinit_array_start
14: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __preinit_array_end
15: 0000000000202018 0 NOTYPE GLOBAL HIDDEN 8 _DYNAMIC
16: 0000000000202000 0 NOTYPE GLOBAL HIDDEN 7 _GLOBAL_OFFSET_TABLE_
17: 0000000000000000 0 NOTYPE GLOBAL HIDDEN ABS __bss_start
18: 0000000000202168 0 NOTYPE GLOBAL HIDDEN 8 _end
19: 0000000000201011 0 NOTYPE GLOBAL HIDDEN 6 _etext
20: 0000000000202168 0 NOTYPE GLOBAL HIDDEN 8 _edata
"
../mold -o $t/exe $t/a.o $t/b.o --version-script=$t/c.map
readelf --symbols $t/exe > $t/log
grep -Pq '0 NOTYPE LOCAL DEFAULT \d+ local1' $t/log
grep -Pq '0 NOTYPE LOCAL DEFAULT \d+ local2' $t/log
grep -Pq '0 NOTYPE GLOBAL DEFAULT \d+ foo' $t/log
grep -Pq '0 NOTYPE GLOBAL DEFAULT \d+ bar' $t/log
grep -Pq '0 NOTYPE GLOBAL DEFAULT \d+ this_is_global' $t/log
grep -Pq '0 NOTYPE GLOBAL DEFAULT \d+ module_local' $t/log
echo OK