From 5a57684b84979a74a9b2b0a115a75728eb1a1f30 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 12 Nov 2020 14:40:39 +0900 Subject: [PATCH] temporary --- Makefile | 2 +- main.cc | 22 +++++++++------------- mold.h | 2 +- object_file.cc | 6 +++++- test/dynamic.s | 11 +++++++++++ test/filler.s | 8 +++++--- 6 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 test/dynamic.s diff --git a/Makefile b/Makefile index f7afd099..90741f4b 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 -O2 +CPPFLAGS=-g $(shell $(LLVM_CONFIG) --cxxflags) -IoneTBB/include -pthread -std=c++17 LDFLAGS=$(shell $(LLVM_CONFIG) --ldflags) -L$(TBB_LIBDIR) -Wl,-rpath=$(TBB_LIBDIR) -fuse-ld=lld -Wl,-hash-style=sysv 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/main.cc b/main.cc index 56ee8b0a..bdd9f267 100644 --- a/main.cc +++ b/main.cc @@ -414,17 +414,12 @@ static void write_got(u8 *buf, ArrayRef files) { u8 *got = buf + out::got->shdr.sh_offset; u8 *plt = buf + out::plt->shdr.sh_offset; u8 *relplt = buf + out::relplt->shdr.sh_offset; - u8 *dynsym = nullptr; - u8 *dynstr = nullptr; + u8 *dynsym = buf + out::dynsym->shdr.sh_offset; + u8 *dynstr = buf + out::dynstr->shdr.sh_offset; memset(buf + out::gotplt->shdr.sh_offset, 0, out::gotplt->shdr.sh_size); - - if (out::dynstr) { - dynsym = buf + out::dynsym->shdr.sh_offset; - dynstr = buf + out::dynstr->shdr.sh_offset; - memset(dynsym, 0, sizeof(ELF64LE::Sym)); - dynstr[0] = '\0'; - } + memset(dynsym, 0, sizeof(ELF64LE::Sym)); + dynstr[0] = '\0'; tbb::parallel_for_each(files, [&](ObjectFile *file) { u32 dynstr_offset = file->dynstr_offset; @@ -943,20 +938,21 @@ int main(int argc, char **argv) { out::shstrtab = new ShstrtabSection; out::plt = new PltSection; out::symtab = new SymtabSection(".symtab", 0); + out::dynsym = new SymtabSection(".dynsym", SHF_ALLOC); + out::dynstr = new SpecialSection(".dynstr", SHT_STRTAB, SHF_ALLOC); + + out::dynsym->shdr.sh_size = sizeof(ELF64LE::Sym); + out::dynstr->shdr.sh_size = 1; if (!config.is_static) { out::interp = new SpecialSection(".interp", SHT_PROGBITS, SHF_ALLOC); out::dynamic = new SpecialSection(".dynamic", SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE, 8, sizeof(ELF64LE::Dyn)); - out::dynstr = new SpecialSection(".dynstr", SHT_STRTAB, SHF_ALLOC); - out::dynsym = new SymtabSection(".dynsym", SHF_ALLOC); out::reldyn = new SpecialSection(".rela.dyn", SHT_RELA, SHF_ALLOC, 8, sizeof(ELF64LE::Rela)); out::hash = new HashSection; out::interp->shdr.sh_size = config.dynamic_linker.size() + 1; - out::dynstr->shdr.sh_size = 1; - out::dynsym->shdr.sh_size = sizeof(ELF64LE::Sym); } // Set priorities to files diff --git a/mold.h b/mold.h index 0f5536ce..ffd4f9c7 100644 --- a/mold.h +++ b/mold.h @@ -532,7 +532,7 @@ public: void write_local_symtab(u8 *buf, u64 symtab_off, u64 strtab_off); void write_global_symtab(u8 *buf, u64 symtab_off, u64 strtab_off); - static ObjectFile *create_internal_file(ArrayRef chunks); + static ObjectFile *create_internal_file(std::vector chunks); std::string name; StringRef archive_name; diff --git a/object_file.cc b/object_file.cc index 1ce50cc1..6d134d61 100644 --- a/object_file.cc +++ b/object_file.cc @@ -515,7 +515,7 @@ bool is_c_identifier(StringRef name) { return std::regex_match(name.begin(), name.end(), re); } -ObjectFile *ObjectFile::create_internal_file(ArrayRef chunks) { +ObjectFile *ObjectFile::create_internal_file(std::vector chunks) { // Create a dummy object file. constexpr int bufsz = 256; char *buf = new char[bufsz]; @@ -566,6 +566,10 @@ ObjectFile *ObjectFile::create_internal_file(ArrayRef chunks) { out::_etext = add("_etext", STB_GLOBAL); out::_edata = add("_edata", STB_GLOBAL); + std::sort(chunks.begin(), chunks.end(), [](OutputChunk *x, OutputChunk *y) { + return x->name < y->name; + }); + for (OutputChunk *chunk : chunks) { if (!is_c_identifier(chunk->name)) continue; diff --git a/test/dynamic.s b/test/dynamic.s new file mode 100644 index 00000000..5ffe3188 --- /dev/null +++ b/test/dynamic.s @@ -0,0 +1,11 @@ +// RUN: mold -o %t.exe /usr/lib/x86_64-linux-gnu/crt1.o +// RUN: /usr/lib/x86_64-linux-gnu/crti.o \ +// RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o \ +// RUN: /home/ruiu/mold/test/Output/hello-dynamic.s.tmp.o \ +// RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o \ +// RUN: /usr/lib/x86_64-linux-gnu/crtn.o \ +// RUN: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc.a \ +// RUN: /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 \ +// RUN: /lib/x86_64-linux-gnu/libc.so.6 \ +// RUN: /usr/lib/x86_64-linux-gnu/libc_nonshared.a \ +// RUN: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 diff --git a/test/filler.s b/test/filler.s index 28b3da21..9d4198c8 100644 --- a/test/filler.s +++ b/test/filler.s @@ -1,5 +1,5 @@ // RUN: cc -o %t.o -c %s -// RUN: mold -static --filler=0xfe -o %t1.exe \ +// RUN: mold -static --filler 0xfe -o %t1.exe \ // RUN: /usr/lib/x86_64-linux-gnu/crt1.o \ // RUN: /usr/lib/x86_64-linux-gnu/crti.o \ // RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o \ @@ -9,7 +9,7 @@ // RUN: /usr/lib/x86_64-linux-gnu/libc.a \ // RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o \ // RUN: /usr/lib/x86_64-linux-gnu/crtn.o -// RUN: mold -static --filler=0 -o %t2.exe \ +// RUN: mold -static --filler 0x0 -o %t2.exe \ // RUN: /usr/lib/x86_64-linux-gnu/crt1.o \ // RUN: /usr/lib/x86_64-linux-gnu/crti.o \ // RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginT.o \ @@ -19,7 +19,9 @@ // RUN: /usr/lib/x86_64-linux-gnu/libc.a \ // RUN: /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o \ // RUN: /usr/lib/x86_64-linux-gnu/crtn.o -// RUN: diff %t1.exe %t2.exe +// RUN: hexdump -C %t1.exe > %t1.txt +// RUN: hexdump -C %t2.exe > %t2.txt +// RUN: diff %t1.txt %t2.txt .text .globl main