diff --git a/main.cc b/main.cc index fa7479cd..bb0dc550 100644 --- a/main.cc +++ b/main.cc @@ -113,38 +113,6 @@ static std::vector> split(const std::vector &input, int unit) { return vec; } -static ObjectFile *create_internal_file() { - // Create a dummy object file. - static char buf[256]; - std::unique_ptr mb = - MemoryBuffer::getMemBuffer(StringRef(buf, sizeof(buf))); - auto *obj = new ObjectFile(mb->getMemBufferRef(), ""); - obj->name = ""; - mb.release(); - - // Create linker-synthesized symbols. - auto *elf_syms = new std::vector; - - auto create = [&](StringRef name) { - Symbol *sym = Symbol::intern(name); - sym->file = obj; - obj->symbols.push_back(sym); - - ELF64LE::Sym esym = {}; - esym.setType(STT_NOTYPE); - esym.setBinding(STB_GLOBAL); - elf_syms->push_back(esym); - return sym; - }; - - out::__bss_start = create("__bss_start"); - out::__ehdr_start = create("__ehdr_start"); - out::__rela_iplt_start = create("__rela_iplt_start"); - out::__rela_iplt_end = create("__rela_iplt_end"); - obj->elf_syms = *elf_syms; - return obj; -} - static void bin_sections(std::vector &files) { #if 1 int unit = (files.size() + 127) / 128; @@ -559,7 +527,9 @@ int main(int argc, char **argv) { for_each(files, [](ObjectFile *file) { file->hanlde_undefined_weak_symbols(); }); } - files.push_back(create_internal_file()); + // Create a dummy file containing linker-synthesized symbols + // (e.g. `__bss_start`). + files.push_back(ObjectFile::create_internal_file()); // Eliminate duplicate comdat groups. { diff --git a/mold.h b/mold.h index fdcf7801..d72698ab 100644 --- a/mold.h +++ b/mold.h @@ -535,6 +535,8 @@ public: bool is_in_archive(); + static ObjectFile *create_internal_file(); + std::vector sections; StringRef archive_name; ELFFile obj; diff --git a/object_file.cc b/object_file.cc index 26599514..1f2c34a4 100644 --- a/object_file.cc +++ b/object_file.cc @@ -421,6 +421,40 @@ bool ObjectFile::is_in_archive() { return !archive_name.empty(); } +ObjectFile *ObjectFile::create_internal_file() { + // Create a dummy object file. + constexpr int bufsz = 256; + char *buf = new char[bufsz]; + std::unique_ptr mb = + MemoryBuffer::getMemBuffer(StringRef(buf, bufsz)); + + auto *obj = new ObjectFile(mb->getMemBufferRef(), ""); + obj->name = ""; + mb.release(); + + // Create linker-synthesized symbols. + auto *elf_syms = new std::vector; + obj->elf_syms = *elf_syms; + + auto add = [&](StringRef name) { + Symbol *sym = Symbol::intern(name); + sym->file = obj; + obj->symbols.push_back(sym); + + ELF64LE::Sym esym = {}; + esym.setType(STT_NOTYPE); + esym.setBinding(STB_GLOBAL); + elf_syms->push_back(esym); + return sym; + }; + + out::__bss_start = add("__bss_start"); + out::__ehdr_start = add("__ehdr_start"); + out::__rela_iplt_start = add("__rela_iplt_start"); + out::__rela_iplt_end = add("__rela_iplt_end"); + return obj; +} + std::string toString(ObjectFile *obj) { StringRef s = obj->name; if (obj->archive_name == "")