1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 16:48:04 +03:00

[ELF] Write section symbols to .symtab

This commit is contained in:
Rui Ueyama 2022-02-04 16:59:11 +09:00
parent 0542a2254b
commit e4c03c238e
2 changed files with 46 additions and 1 deletions

View File

@ -460,11 +460,18 @@ template <typename E>
void SymtabSection<E>::update_shdr(Context<E> &ctx) {
i64 nsyms = 1;
// Section symbols
for (Chunk<E> *chunk : ctx.chunks)
if (chunk->shndx && (chunk->shdr.sh_flags & SHF_ALLOC))
nsyms++;
// Local symbols
for (ObjectFile<E> *file : ctx.objs) {
file->local_symtab_idx = nsyms;
nsyms += file->num_local_symtab;
}
// Global symbols
for (ObjectFile<E> *file : ctx.objs) {
file->global_symtab_idx = nsyms;
nsyms += file->num_global_symtab;
@ -477,9 +484,24 @@ void SymtabSection<E>::update_shdr(Context<E> &ctx) {
template <typename E>
void SymtabSection<E>::copy_buf(Context<E> &ctx) {
memset(ctx.buf + this->shdr.sh_offset, 0, sizeof(ElfSym<E>));
ElfSym<E> *buf = (ElfSym<E> *)(ctx.buf + this->shdr.sh_offset);
memset(buf, 0, sizeof(ElfSym<E>));
// Write the initial NUL byte to .strtab.
ctx.buf[ctx.strtab->shdr.sh_offset] = '\0';
// Create section symbols
for (Chunk<E> *chunk : ctx.chunks) {
if (chunk->shndx && (chunk->shdr.sh_flags & SHF_ALLOC)) {
ElfSym<E> &sym = buf[chunk->shndx];
memset(&sym, 0, sizeof(sym));
sym.st_type = STT_SECTION;
sym.st_value = chunk->shdr.sh_addr;
sym.st_shndx = chunk->shndx;
}
}
// Copy symbols and symbol names from input files
tbb::parallel_for_each(ctx.objs, [&](ObjectFile<E> *file) {
file->write_symtab(ctx);
});

23
test/elf/section-symbols.sh Executable file
View File

@ -0,0 +1,23 @@
#!/bin/bash
export LANG=
set -e
CC="${CC:-cc}"
CXX="${CXX:-c++}"
testname=$(basename "$0" .sh)
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
mold="$(pwd)/mold"
t=out/test/elf/$testname
mkdir -p $t
cat <<EOF | $CC -o $t/a.o -c -xc -
#include <stdio.h>
int main() {
printf("Hello world\n");
}
EOF
$CC -B. -o $t/exe $t/a.o
readelf -s $t/exe | grep -q 'SECTION LOCAL DEFAULT'
echo OK