Skip to content

Commit

Permalink
[ELF] Do not corrupt compressed debug sections on i386
Browse files Browse the repository at this point in the history
Previously, if an input debug section is compressed, mold produced
corrupted output debug section on i386.

Fixes #361
  • Loading branch information
rui314 committed Feb 24, 2022
1 parent ac3ae60 commit 3068364
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
18 changes: 17 additions & 1 deletion elf/input-sections.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,31 @@ InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,
if (section_idx < file.elf_sections.size())
contents = {(char *)file.mf->data + shdr().sh_offset, shdr().sh_size};

bool compressed;

if (name.starts_with(".zdebug")) {
sh_size = *(ubig64 *)&contents[4];
p2align = to_p2align(shdr().sh_addralign);
compressed = true;
} else if (shdr().sh_flags & SHF_COMPRESSED) {
ElfChdr<E> &chdr = *(ElfChdr<E> *)&contents[0];
sh_size = chdr.ch_size;
p2align = to_p2align(chdr.ch_addralign);
compressed = true;
} else {
sh_size = shdr().sh_size;
p2align = to_p2align(shdr().sh_addralign);
compressed = false;
}

// Uncompress early if the relocation is REL-type so that we can read
// addends from section contents. If RELA-type, we don't need to do this
// because addends are in relocations.
if (compressed && E::is_rel) {
u8 *buf = new u8[sh_size];
uncompress(ctx, buf);
contents = {(char *)buf, sh_size};
ctx.string_pool.emplace_back(buf);
}

output_section =
Expand All @@ -57,7 +72,8 @@ InputSection<E>::InputSection(Context<E> &ctx, ObjectFile<E> &file,

template <typename E>
bool InputSection<E>::is_compressed() {
return name().starts_with(".zdebug") || (shdr().sh_flags & SHF_COMPRESSED);
return !E::is_rel &&
(name().starts_with(".zdebug") || (shdr().sh_flags & SHF_COMPRESSED));
}

template <typename E>
Expand Down
33 changes: 33 additions & 0 deletions test/elf/compressed-debug-info-i386.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/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

[ "$(uname -m)" = x86_64 ] || { echo skipped; exit; }

which dwarfdump >& /dev/null || { echo skipped; exit; }

cat <<EOF | $CXX -c -o $t/a.o -g -gz=zlib-gnu -xc++ - -m32
int main() {
return 0;
}
EOF

cat <<EOF | $CXX -c -o $t/b.o -g -gz=zlib -xc++ - -m32
int foo() {
return 0;
}
EOF

$CC -B. -o $t/exe $t/a.o $t/b.o -m32
dwarfdump $t/exe > /dev/null
readelf --sections $t/exe | fgrep -q .debug_info

echo ' OK'
4 changes: 2 additions & 2 deletions test/elf/compressed-debug-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ mkdir -p $t

which dwarfdump >& /dev/null || { echo skipped; exit; }

cat <<EOF | g++ -c -o $t/a.o -g -gz=zlib-gnu -xc++ -
cat <<EOF | $CXX -c -o $t/a.o -g -gz=zlib-gnu -xc++ -
int main() {
return 0;
}
EOF

cat <<EOF | g++ -c -o $t/b.o -g -gz=zlib -xc++ -
cat <<EOF | $CXX -c -o $t/b.o -g -gz=zlib -xc++ -
int foo() {
return 0;
}
Expand Down

0 comments on commit 3068364

Please sign in to comment.