From bed207a4ec29a500fb930ebdad167b344c50c44e Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 6 Nov 2020 08:49:41 +0900 Subject: [PATCH] temporary --- Makefile | 2 +- input_sections.cc | 17 ++++++++++------- main.cc | 34 +++++++++++++++------------------- mold.h | 27 ++++++++++++++++++--------- object_file.cc | 8 ++++++++ 5 files changed, 52 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 70dd4c0e..3af8c20a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ LLVM_LIBS=$(wildcard llvm-project/build/lib/libLLVM*.a) CURRENT_DIR=$(shell pwd) TBB_LIBDIR=$(wildcard $(CURRENT_DIR)/oneTBB/build/linux_intel64_*_release/) -CPPFLAGS=-g $(shell $(LLVM_CONFIG) --cxxflags) -IoneTBB/include -pthread -std=c++17 +CPPFLAGS=-g $(shell $(LLVM_CONFIG) --cxxflags) -IoneTBB/include -pthread -std=c++17 -O2 LDFLAGS=$(shell $(LLVM_CONFIG) --ldflags) -L$(TBB_LIBDIR) -Wl,-rpath=$(TBB_LIBDIR) -fuse-ld=lld LIBS=-pthread -ltbb -lcurses -Wl,--start-group $(LLVM_LIBS) -Wl,--end-group OBJS=main.o object_file.o input_sections.o output_chunks.o mapfile.o perf.o diff --git a/input_sections.cc b/input_sections.cc index 8f282d06..e99ffced 100644 --- a/input_sections.cc +++ b/input_sections.cc @@ -24,7 +24,9 @@ void InputSection::copy_to(u8 *buf) { memcpy(buf, &data[0], data.size()); } -void InputSection::scan_relocations() { +ScanRelResult InputSection::scan_relocations() { + ScanRelResult res; + for (const ELF64LE::Rela &rel : rels) { Symbol *sym = file->symbols[rel.getSymbol(false)]; if (!sym->file || !sym->file->is_alive) @@ -43,7 +45,7 @@ void InputSection::scan_relocations() { std::lock_guard lock(sym->mu); if (!sym->needs_got) { sym->needs_got = true; - file->num_got++; + res.num_got++; } break; } @@ -51,7 +53,7 @@ void InputSection::scan_relocations() { std::lock_guard lock(sym->mu); if (!sym->needs_gottp) { sym->needs_gottp = true; - file->num_got++; + res.num_got++; } break; } @@ -62,14 +64,15 @@ void InputSection::scan_relocations() { std::lock_guard lock(sym->mu); if (!sym->needs_plt) { sym->needs_plt = true; - file->num_plt++; - file->num_gotplt++; - file->num_relplt++; + res.num_plt++; + res.num_gotplt++; + res.num_relplt++; } break; } } } + return res; } void InputSection::relocate(u8 *buf) { @@ -146,7 +149,7 @@ void InputSection::relocate(u8 *buf) { error(toString(this) + ": unknown relocation: " + std::to_string(rel.getType(false))); } - + static Counter counter("relocs"); counter.inc(); } diff --git a/main.cc b/main.cc index b3b96d91..5078e455 100644 --- a/main.cc +++ b/main.cc @@ -232,26 +232,22 @@ static void set_isec_offsets() { } static void scan_rels(ArrayRef files) { - std::atomic_int32_t num_got = 0; - std::atomic_int32_t num_gotplt = 0; - std::atomic_int32_t num_plt = 0; - std::atomic_int32_t num_relplt = 0; + ScanRelResult res = + tbb::parallel_reduce(tbb::blocked_range(0, files.size()), + ScanRelResult(), + [&](const tbb::blocked_range &r, ScanRelResult res) { + for (int i = r.begin(); i != r.end(); i++) + res = res.add(files[i]->scan_relocations()); + return res; + }, + [](const ScanRelResult &x, const ScanRelResult &y) { + return x.add(y); + }); - for_each(files, [&](ObjectFile *file) { - for (InputSection *isec : file->sections) - if (isec) - isec->scan_relocations(); - - num_got += file->num_got; - num_gotplt += file->num_gotplt; - num_plt += file->num_plt; - num_relplt += file->num_relplt; - }); - - out::got->shdr.sh_size = num_got * 8; - out::gotplt->shdr.sh_size = num_gotplt * 8; - out::plt->shdr.sh_size = num_plt * 16; - out::relplt->shdr.sh_size = num_relplt * sizeof(ELF64LE::Rela); + out::got->shdr.sh_size = res.num_got * 8; + out::gotplt->shdr.sh_size = res.num_gotplt * 8; + out::plt->shdr.sh_size = res.num_plt * 16; + out::relplt->shdr.sh_size = res.num_relplt * sizeof(ELF64LE::Rela); } static void assign_got_offsets(u8 *buf, ArrayRef files) { diff --git a/mold.h b/mold.h index 2d351f1e..4e1b29b7 100644 --- a/mold.h +++ b/mold.h @@ -12,6 +12,7 @@ #include "tbb/concurrent_hash_map.h" #include "tbb/global_control.h" #include "tbb/parallel_for_each.h" +#include "tbb/parallel_reduce.h" #include "tbb/spin_mutex.h" #include "tbb/task_group.h" @@ -193,16 +194,30 @@ inline std::string toString(Symbol sym) { } // -// input_chunks.cc +// input_sections.cc // +struct ScanRelResult { + ScanRelResult add(const ScanRelResult &other) const { + return {num_got + other.num_got, + num_gotplt + other.num_gotplt, + num_plt + other.num_plt, + num_relplt + other.num_relplt}; + } + + u32 num_got = 0; + u32 num_gotplt = 0; + u32 num_plt = 0; + u32 num_relplt = 0; +}; + class InputSection { public: InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name); void copy_to(u8 *buf); void relocate(u8 *buf); - void scan_relocations(); + ScanRelResult scan_relocations(); ObjectFile *file; OutputSection *output_section; @@ -498,6 +513,7 @@ public: void hanlde_undefined_weak_symbols(); void eliminate_duplicate_comdat_groups(); void convert_common_symbols(); + ScanRelResult scan_relocations(); void fix_sym_addrs(); void compute_symtab(); @@ -522,13 +538,6 @@ public: u64 global_symtab_size = 0; u64 global_strtab_size = 0; - // Number of .got, .got.plt, .plt and .rel.plt entries - // needed for this file. - i32 num_got = 0; - i32 num_gotplt = 0; - i32 num_plt = 0; - i32 num_relplt = 0; - private: void initialize_sections(); void initialize_symbols(); diff --git a/object_file.cc b/object_file.cc index 56349f08..15856067 100644 --- a/object_file.cc +++ b/object_file.cc @@ -401,6 +401,14 @@ void ObjectFile::convert_common_symbols() { } } +ScanRelResult ObjectFile::scan_relocations() { + ScanRelResult res; + for (InputSection *isec : sections) + if (isec) + res = res.add(isec->scan_relocations()); + return res; +} + void ObjectFile::fix_sym_addrs() { for (Symbol *sym : symbols) { if (sym->file != this)