From 42faa439c553f21c87ab9098b02ae152cfbe222e0057ade1eea76130571da306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Tue, 19 Apr 2022 06:44:11 +0000 Subject: [PATCH 1/8] - Add fix-gdb-index.patch. - Enable tests. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=20 --- fix-gdb-index.patch | 203 ++++++++++++++++++++++++++++++++++++++++++++ mold.changes | 6 ++ mold.spec | 12 +++ 3 files changed, 221 insertions(+) create mode 100644 fix-gdb-index.patch diff --git a/fix-gdb-index.patch b/fix-gdb-index.patch new file mode 100644 index 0000000..66ad467 --- /dev/null +++ b/fix-gdb-index.patch @@ -0,0 +1,203 @@ +From c90801e62906f454eb3d478683cd54bce5d8d7b3 Mon Sep 17 00:00:00 2001 +From: Rui Ueyama +Date: Sat, 16 Apr 2022 13:18:09 +0800 +Subject: [PATCH] [ELF] Make --gdb-index work for compressed input debug + sections + +Previously, --gdb-index tries to read bogus compressed data from +input sections if input debug sections are compressed. + +Fixes https://github.com/rui314/mold/issues/431 +--- + elf/arch-riscv64.cc | 4 ++-- + elf/input-files.cc | 10 ++-------- + elf/input-sections.cc | 44 +++++++++++++++++++++++++------------------ + elf/mold.h | 14 ++++++-------- + elf/output-chunks.cc | 2 ++ + test/elf/gdb-index.sh | 21 +++++++++++++-------- + 6 files changed, 51 insertions(+), 44 deletions(-) + +diff --git a/elf/arch-riscv64.cc b/elf/arch-riscv64.cc +index aa5d7b1c..955407f2 100644 +--- a/elf/arch-riscv64.cc ++++ b/elf/arch-riscv64.cc +@@ -490,8 +490,8 @@ template <> + void InputSection::copy_contents_riscv(Context &ctx, u8 *buf) { + // A non-alloc section isn't relaxed, so just copy it as one big chunk. + if (!(shdr().sh_flags & SHF_ALLOC)) { +- if (is_compressed()) +- uncompress(ctx, buf); ++ if (compressed) ++ uncompress_to(ctx, buf); + else + memcpy(buf, contents.data(), contents.size()); + return; +diff --git a/elf/input-files.cc b/elf/input-files.cc +index 6980e21a..d44216af 100644 +--- a/elf/input-files.cc ++++ b/elf/input-files.cc +@@ -524,16 +524,10 @@ split_section(Context &ctx, InputSection &sec) { + sec.shdr().sh_flags); + rec->p2align = sec.p2align; + +- std::string_view data = sec.contents; +- + // If thes section contents are compressed, uncompress them. +- if (sec.is_compressed()) { +- u8 *buf = new u8[sec.sh_size]; +- sec.uncompress(ctx, buf); +- data = {(char *)buf, sec.sh_size}; +- ctx.string_pool.emplace_back(buf); +- } ++ sec.uncompress(ctx); + ++ std::string_view data = sec.contents; + const char *begin = data.data(); + u64 entsize = sec.shdr().sh_entsize; + HyperLogLog estimator; +diff --git a/elf/input-sections.cc b/elf/input-sections.cc +index cfe1c570..81663cdb 100644 +--- a/elf/input-sections.cc ++++ b/elf/input-sections.cc +@@ -38,8 +38,6 @@ InputSection::InputSection(Context &ctx, ObjectFile &file, + if (shndx < 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); +@@ -55,28 +53,37 @@ InputSection::InputSection(Context &ctx, ObjectFile &file, + 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); +- } ++ // Sections may have been compressed. We usually uncompress them ++ // directly into the mmap'ed output file, but we want to uncompress ++ // early for REL-type ELF types to read relocation addends from ++ // section contents. For RELA-type, we don't need to do this because ++ // addends are in relocations. ++ if (E::is_rel) ++ uncompress(ctx); + + output_section = + OutputSection::get_instance(ctx, name, shdr().sh_type, shdr().sh_flags); + } + + template +-bool InputSection::is_compressed() { +- return !E::is_rel && +- (name().starts_with(".zdebug") || (shdr().sh_flags & SHF_COMPRESSED)); ++void InputSection::uncompress(Context &ctx) { ++ if (!compressed || uncompressed) ++ return; ++ ++ u8 *buf = new u8[sh_size]; ++ uncompress_to(ctx, buf); ++ contents = {(char *)buf, sh_size}; ++ ctx.string_pool.emplace_back(buf); ++ uncompressed = true; + } + + template +-void InputSection::uncompress(Context &ctx, u8 *buf) { ++void InputSection::uncompress_to(Context &ctx, u8 *buf) { ++ if (!compressed || uncompressed) { ++ memcpy(buf, contents.data(), contents.size()); ++ return; ++ } ++ + auto do_uncompress = [&](std::string_view data) { + unsigned long size = sh_size; + if (::uncompress(buf, &size, (u8 *)data.data(), data.size()) != Z_OK) +@@ -100,7 +107,8 @@ void InputSection::uncompress(Context &ctx, u8 *buf) { + + ElfChdr &hdr = *(ElfChdr *)&contents[0]; + if (hdr.ch_type != ELFCOMPRESS_ZLIB) +- Fatal(ctx) << *this << ": unsupported compression type"; ++ Fatal(ctx) << *this << ": unsupported compression type: 0x" ++ << std::hex << hdr.ch_type; + do_uncompress(contents.substr(sizeof(ElfChdr))); + } + +@@ -213,8 +221,8 @@ void InputSection::write_to(Context &ctx, u8 *buf) { + // Copy data + if constexpr (std::is_same_v) { + copy_contents_riscv(ctx, buf); +- } else if (is_compressed()) { +- uncompress(ctx, buf); ++ } else if (compressed) { ++ uncompress_to(ctx, buf); + } else { + memcpy(buf, contents.data(), contents.size()); + } +diff --git a/elf/mold.h b/elf/mold.h +index b36446cb..d38fec9a 100644 +--- a/elf/mold.h ++++ b/elf/mold.h +@@ -266,8 +266,8 @@ class InputSection { + InputSection(Context &ctx, ObjectFile &file, std::string_view name, + i64 shndx); + +- bool is_compressed(); +- void uncompress(Context &ctx, u8 *buf); ++ void uncompress(Context &ctx); ++ void uncompress_to(Context &ctx, u8 *buf); + void scan_relocations(Context &ctx); + void write_to(Context &ctx, u8 *buf); + void apply_reloc_alloc(Context &ctx, u8 *base); +@@ -307,10 +307,12 @@ class InputSection { + + // For COMDAT de-duplication and garbage collection + std::atomic_bool is_alive = true; +- bool killed_by_icf = false; +- + u8 p2align = 0; + ++ u8 compressed : 1 = false; ++ u8 uncompressed : 1 = false; ++ u8 killed_by_icf : 1 = false; ++ + private: + typedef enum : u8 { NONE, ERROR, COPYREL, PLT, CPLT, DYNREL, BASEREL } Action; + +@@ -1142,10 +1144,6 @@ class ObjectFile : public InputFile { + const ElfSym &esym, i64 symidx); + void merge_visibility(Context &ctx, Symbol &sym, u8 visibility); + +- std::pair *> +- uncompress_contents(Context &ctx, const ElfShdr &shdr, +- std::string_view name); +- + bool has_common_symbol = false; + + std::string_view symbol_strtab; +diff --git a/elf/output-chunks.cc b/elf/output-chunks.cc +index abb278ef..315172bb 100644 +--- a/elf/output-chunks.cc ++++ b/elf/output-chunks.cc +@@ -2407,6 +2407,7 @@ void GdbIndexSection::write_address_areas(Context &ctx) { + template + std::vector + GdbIndexSection::read_compunits(Context &ctx, ObjectFile &file) { ++ file.debug_info->uncompress(ctx); + std::string_view data = file.debug_info->contents; + std::vector vec; + +@@ -2444,6 +2445,7 @@ GdbIndexSection::read_pubnames(Context &ctx, ObjectFile &file) { + }; + + auto read = [&](InputSection &isec) { ++ isec.uncompress(ctx); + std::string_view contents = isec.contents; + + while (!contents.empty()) { diff --git a/mold.changes b/mold.changes index 7c298ac..f47c9be 100644 --- a/mold.changes +++ b/mold.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Tue Apr 19 06:43:50 UTC 2022 - Martin Liška + +- Add fix-gdb-index.patch. +- Enable tests. + ------------------------------------------------------------------- Fri Apr 15 11:53:51 UTC 2022 - Martin Liška diff --git a/mold.spec b/mold.spec index ece0594..ae7f4cb 100644 --- a/mold.spec +++ b/mold.spec @@ -23,6 +23,7 @@ Summary: A Modern Linker (mold) License: AGPL-3.0-or-later URL: https://github.com/rui314/mold Source: https://github.com/rui314/mold/archive/v%{version}/mold-%{version}.tar.gz +Patch0: fix-gdb-index.patch BuildRequires: cmake %if %{suse_version} < 1550 BuildRequires: gcc10-c++ @@ -32,6 +33,8 @@ BuildRequires: gcc-c++ BuildRequires: mimalloc-devel BuildRequires: tbb-devel %endif +BuildRequires: glibc-devel-static +BuildRequires: libdwarf-tools BuildRequires: openssl-devel BuildRequires: xxhash-devel BuildRequires: zlib-devel @@ -82,6 +85,15 @@ LIBDIR=%{_libdir} \ LIBEXECDIR=%{_libexecdir} \ %{build_args} +%check +%make_build test -e \ +PREFIX=%{_prefix} \ +BINDIR=%{_bindir} \ +MANDIR=%{_mandir} \ +LIBDIR=%{_libdir} \ +LIBEXECDIR=%{_libexecdir} \ +%{build_args} + %post "%_sbindir/update-alternatives" --install \ "%_bindir/ld" ld "%_bindir/ld.mold" 1 From bc12ac6696454bd94c1323ae3c9783737c78739b3cd9bee48986f0fba1318513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Tue, 19 Apr 2022 07:21:49 +0000 Subject: [PATCH 2/8] Fix BuildRequires for older releases. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=21 --- mold.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mold.spec b/mold.spec index ae7f4cb..0243e70 100644 --- a/mold.spec +++ b/mold.spec @@ -30,11 +30,11 @@ BuildRequires: gcc10-c++ %else # These libraries are not present for openSUSE Leap BuildRequires: gcc-c++ +BuildRequires: libdwarf-tools BuildRequires: mimalloc-devel BuildRequires: tbb-devel %endif BuildRequires: glibc-devel-static -BuildRequires: libdwarf-tools BuildRequires: openssl-devel BuildRequires: xxhash-devel BuildRequires: zlib-devel From cb283e322436fd2d1bb627f66a7125d3741f53bc4ff7012240a3464bf63d0907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 06:51:04 +0000 Subject: [PATCH 3/8] - Run test serially. - Install some packages in order to increase test coverage. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=22 --- mold.changes | 6 ++++++ mold.spec | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mold.changes b/mold.changes index f47c9be..374ba20 100644 --- a/mold.changes +++ b/mold.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Wed Apr 20 06:50:40 UTC 2022 - Martin Liška + +- Run test serially. +- Install some packages in order to increase test coverage. + ------------------------------------------------------------------- Tue Apr 19 06:43:50 UTC 2022 - Martin Liška diff --git a/mold.spec b/mold.spec index 0243e70..8315e44 100644 --- a/mold.spec +++ b/mold.spec @@ -26,10 +26,13 @@ Source: https://github.com/rui314/mold/archive/v%{version}/mold-%{versio Patch0: fix-gdb-index.patch BuildRequires: cmake %if %{suse_version} < 1550 +BuildRequires: gcc10-32bit BuildRequires: gcc10-c++ %else # These libraries are not present for openSUSE Leap BuildRequires: gcc-c++ +BuildRequires: clang +BuildRequires: gcc-32bit BuildRequires: libdwarf-tools BuildRequires: mimalloc-devel BuildRequires: tbb-devel @@ -86,7 +89,7 @@ LIBEXECDIR=%{_libexecdir} \ %{build_args} %check -%make_build test -e \ +make test -e \ PREFIX=%{_prefix} \ BINDIR=%{_bindir} \ MANDIR=%{_mandir} \ From bc9dcddb132fe1f06c19ee8cc1e97d3147c7cbb73bc330947462f2a091c0e178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 06:53:26 +0000 Subject: [PATCH 4/8] Fix dependency. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=23 --- mold.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mold.spec b/mold.spec index 8315e44..9494088 100644 --- a/mold.spec +++ b/mold.spec @@ -32,10 +32,12 @@ BuildRequires: gcc10-c++ # These libraries are not present for openSUSE Leap BuildRequires: gcc-c++ BuildRequires: clang -BuildRequires: gcc-32bit BuildRequires: libdwarf-tools BuildRequires: mimalloc-devel BuildRequires: tbb-devel +%ifarch aarch64 x86_64 +BuildRequires: gcc-32bit +%endif %endif BuildRequires: glibc-devel-static BuildRequires: openssl-devel From ed425b4dab3a9810ccf4750c45cf799fa85f089c9a6878b8e0d895761ab52312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 06:53:47 +0000 Subject: [PATCH 5/8] Fix dependency. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=24 --- mold.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mold.spec b/mold.spec index 9494088..2591261 100644 --- a/mold.spec +++ b/mold.spec @@ -35,7 +35,7 @@ BuildRequires: clang BuildRequires: libdwarf-tools BuildRequires: mimalloc-devel BuildRequires: tbb-devel -%ifarch aarch64 x86_64 +%ifarch x86_64 BuildRequires: gcc-32bit %endif %endif From 32a66047e6ced9b5fd35a02b4f672f709ebdb1f0c2b6f5602f8c8544f7f7809e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 06:55:48 +0000 Subject: [PATCH 6/8] Remove one build dependency. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=25 --- mold.spec | 1 - 1 file changed, 1 deletion(-) diff --git a/mold.spec b/mold.spec index 2591261..4e19f53 100644 --- a/mold.spec +++ b/mold.spec @@ -26,7 +26,6 @@ Source: https://github.com/rui314/mold/archive/v%{version}/mold-%{versio Patch0: fix-gdb-index.patch BuildRequires: cmake %if %{suse_version} < 1550 -BuildRequires: gcc10-32bit BuildRequires: gcc10-c++ %else # These libraries are not present for openSUSE Leap From 1840c4f05c177c621c9f14f00c2a66180eeef83c994426302e43410175034aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 07:01:37 +0000 Subject: [PATCH 7/8] Add one more dependency. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=26 --- mold.spec | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mold.spec b/mold.spec index 4e19f53..f36cfef 100644 --- a/mold.spec +++ b/mold.spec @@ -32,6 +32,7 @@ BuildRequires: gcc10-c++ BuildRequires: gcc-c++ BuildRequires: clang BuildRequires: libdwarf-tools +BuildRequires: llvm-gold BuildRequires: mimalloc-devel BuildRequires: tbb-devel %ifarch x86_64 @@ -90,7 +91,7 @@ LIBEXECDIR=%{_libexecdir} \ %{build_args} %check -make test -e \ +make test -k -e \ PREFIX=%{_prefix} \ BINDIR=%{_bindir} \ MANDIR=%{_mandir} \ From aff34a2a4dfc51cf350c90d3ffe60dad9c1d8057df53c8fd75750bbbb24aeef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Wed, 20 Apr 2022 11:55:42 +0000 Subject: [PATCH 8/8] - Use ExclusiveArch. OBS-URL: https://build.opensuse.org/package/show/devel:tools:compiler/mold?expand=0&rev=27 --- mold.changes | 1 + mold.spec | 1 + 2 files changed, 2 insertions(+) diff --git a/mold.changes b/mold.changes index 374ba20..7b11beb 100644 --- a/mold.changes +++ b/mold.changes @@ -3,6 +3,7 @@ Wed Apr 20 06:50:40 UTC 2022 - Martin Liška - Run test serially. - Install some packages in order to increase test coverage. +- Use ExclusiveArch. ------------------------------------------------------------------- Tue Apr 19 06:43:50 UTC 2022 - Martin Liška diff --git a/mold.spec b/mold.spec index f36cfef..d9a585e 100644 --- a/mold.spec +++ b/mold.spec @@ -24,6 +24,7 @@ License: AGPL-3.0-or-later URL: https://github.com/rui314/mold Source: https://github.com/rui314/mold/archive/v%{version}/mold-%{version}.tar.gz Patch0: fix-gdb-index.patch +ExclusiveArch: x86_64 aarch64 riscv64 BuildRequires: cmake %if %{suse_version} < 1550 BuildRequires: gcc10-c++