1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-26 18:02:30 +03:00

temporary

This commit is contained in:
Rui Ueyama 2020-11-24 14:31:05 +09:00
parent 02ada95c85
commit cb62d61ff0
5 changed files with 45 additions and 42 deletions

57
main.cc
View File

@ -148,9 +148,11 @@ void read_file(MemoryBufferRef mb) {
out::objs.push_back(new ObjectFile(member, mb.getBufferIdentifier())); out::objs.push_back(new ObjectFile(member, mb.getBufferIdentifier()));
break; break;
case file_magic::elf_relocatable: case file_magic::elf_relocatable:
case file_magic::elf_shared_object:
out::objs.push_back(new ObjectFile(mb, "")); out::objs.push_back(new ObjectFile(mb, ""));
break; break;
case file_magic::elf_shared_object:
out::dsos.push_back(new ObjectFile(mb, ""));
break;
case file_magic::unknown: case file_magic::unknown:
parse_linker_script(mb.getBufferIdentifier(), mb.getBuffer()); parse_linker_script(mb.getBufferIdentifier(), mb.getBuffer());
break; break;
@ -177,13 +179,13 @@ static void resolve_symbols() {
MyTimer t("resolve_symbols", before_copy_timer); MyTimer t("resolve_symbols", before_copy_timer);
// Register defined symbols // Register defined symbols
tbb::parallel_for_each(out::objs, tbb::parallel_for_each(out::objs, [](ObjectFile *file) { file->resolve_symbols(); });
[](ObjectFile *file) { file->resolve_symbols(); }); tbb::parallel_for_each(out::dsos, [](ObjectFile *file) { file->resolve_symbols(); });
// Mark archive members we include into the final output. // Mark archive members we include into the final output.
std::vector<ObjectFile *> root; std::vector<ObjectFile *> root;
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::objs)
if (file->is_alive && !file->is_dso) if (file->is_alive)
root.push_back(file); root.push_back(file);
tbb::parallel_do( tbb::parallel_do(
@ -405,12 +407,12 @@ static void scan_rels() {
isec->scan_relocations(); isec->scan_relocations();
}); });
std::vector<std::vector<Symbol *>> vec(out::objs.size()); std::vector<ObjectFile *> files = join(out::objs, out::dsos);
std::vector<std::vector<Symbol *>> vec(files.size());
tbb::parallel_for(0, (int)out::objs.size(), [&](int i) { tbb::parallel_for(0, (int)files.size(), [&](int i) {
ObjectFile *file = out::objs[i]; for (Symbol *sym : files[i]->symbols)
for (Symbol *sym : file->symbols) if (sym->file == files[i] && sym->flags)
if (sym->file == file && sym->flags)
vec[i].push_back(sym); vec[i].push_back(sym);
}); });
@ -716,17 +718,17 @@ int main(int argc, char **argv) {
{ {
MyTimer t("parse", parse_timer); MyTimer t("parse", parse_timer);
tbb::parallel_for_each(out::objs, [](ObjectFile *file) { file->parse(); }); tbb::parallel_for_each(out::objs, [](ObjectFile *file) { file->parse(); });
tbb::parallel_for_each(out::dsos, [](ObjectFile *file) { file->parse(); });
} }
// Uniquify shared object files with soname // Uniquify shared object files with soname
{ {
llvm::StringSet<> seen; llvm::StringSet<> seen;
for (auto it = out::objs.begin(); it != out::objs.end();) { for (int i = 0; i < out::dsos.size();) {
StringRef soname = (*it)->soname; if (seen.insert(out::dsos[i]->soname).second)
if (!soname.empty() && !seen.insert(soname).second) i++;
out::objs.erase(it);
else else
it++; out::dsos.erase(out::dsos.begin() + i);
} }
} }
@ -777,22 +779,24 @@ int main(int argc, char **argv) {
// Set priorities to files. File priority 1 is reserved for the internal file. // Set priorities to files. File priority 1 is reserved for the internal file.
int priority = 2; int priority = 2;
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::objs)
if (!file->is_in_archive && !file->is_dso) if (!file->is_in_archive)
file->priority = priority++; file->priority = priority++;
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::objs)
if (file->is_in_archive && !file->is_dso) if (file->is_in_archive)
file->priority = priority++; file->priority = priority++;
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::dsos)
if (file->is_dso)
file->priority = priority++; file->priority = priority++;
// Resolve symbols and fix the set of object files that are // Resolve symbols and fix the set of object files that are
// included to the final output. // included to the final output.
resolve_symbols(); resolve_symbols();
if (args.hasArg(OPT_trace)) if (args.hasArg(OPT_trace)) {
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::objs)
message(toString(file)); message(toString(file));
for (ObjectFile *file : out::dsos)
message(toString(file));
}
// Remove redundant comdat sections (e.g. duplicate inline functions). // Remove redundant comdat sections (e.g. duplicate inline functions).
eliminate_comdats(); eliminate_comdats();
@ -862,8 +866,7 @@ int main(int argc, char **argv) {
check_undefined_symbols(); check_undefined_symbols();
// Copy shared object name strings to .dynsym // Copy shared object name strings to .dynsym
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::dsos)
if (file->is_alive && file->is_dso)
out::dynstr->add_string(file->soname); out::dynstr->add_string(file->soname);
// Add headers and sections that have to be at the beginning // Add headers and sections that have to be at the beginning
@ -939,23 +942,17 @@ int main(int argc, char **argv) {
if (config.print_map) { if (config.print_map) {
MyTimer t("print_map"); MyTimer t("print_map");
print_map(out::objs, out::chunks); print_map();
} }
#if 0
for (ObjectFile *file : out::objs)
for (InputSection *isec : file->sections)
if (isec)
message(toString(isec));
#endif
// Show stat numbers // Show stat numbers
Counter num_input_sections("input_sections"); Counter num_input_sections("input_sections");
for (ObjectFile *file : out::objs) for (ObjectFile *file : out::objs)
num_input_sections.inc(file->sections.size()); num_input_sections.inc(file->sections.size());
Counter num_output_chunks("output_out::chunks", out::chunks.size()); Counter num_output_chunks("output_out::chunks", out::chunks.size());
Counter num_files("files", out::objs.size()); Counter num_objs("num_objs", out::objs.size());
Counter num_dsos("num_dsos", out::dsos.size());
Counter filesize_counter("filesize", filesize); Counter filesize_counter("filesize", filesize);
Counter::print(); Counter::print();

View File

@ -4,16 +4,16 @@
using namespace llvm; using namespace llvm;
void print_map(ArrayRef<ObjectFile *> files, ArrayRef<OutputChunk *> output_sections) { void print_map() {
// Construct a section-to-symbol map. // Construct a section-to-symbol map.
std::unordered_multimap<InputChunk *, Symbol *> map; std::unordered_multimap<InputChunk *, Symbol *> map;
for (ObjectFile *file : files) for (ObjectFile *file : out::objs)
for (Symbol *sym : file->symbols) for (Symbol *sym : file->symbols)
if (sym->file == file && sym->input_section) if (sym->file == file && sym->input_section)
map.insert({sym->input_section, sym}); map.insert({sym->input_section, sym});
llvm::outs() << " VMA Size Align Out In Symbol\n"; llvm::outs() << " VMA Size Align Out In Symbol\n";
for (OutputChunk *osec : output_sections) { for (OutputChunk *osec : out::chunks) {
llvm::outs() << format("%16llx %8llx %5lld ", llvm::outs() << format("%16llx %8llx %5lld ",
(u64)osec->shdr.sh_addr, (u64)osec->shdr.sh_addr,
(u64)osec->shdr.sh_size, (u64)osec->shdr.sh_size,

10
mold.h
View File

@ -618,6 +618,7 @@ namespace out {
using namespace llvm::ELF; using namespace llvm::ELF;
inline std::vector<ObjectFile *> objs; inline std::vector<ObjectFile *> objs;
inline std::vector<ObjectFile *> dsos;
inline std::vector<OutputChunk *> chunks; inline std::vector<OutputChunk *> chunks;
inline u8 *buf; inline u8 *buf;
@ -796,6 +797,13 @@ inline void write_vector(u8 *buf, const std::vector<T> &vec) {
memcpy(buf, vec.data(), vec.size() * sizeof(T)); memcpy(buf, vec.data(), vec.size() * sizeof(T));
} }
template <typename T>
inline std::vector<T> join(std::vector<T> &a, std::vector<T> &b) {
std::vector<T> vec(a);
vec.insert(vec.end(), b.begin(), b.end());
return vec;
}
template <typename T> template <typename T>
inline std::vector<T> flatten(std::vector<std::vector<T>> &vec) { inline std::vector<T> flatten(std::vector<std::vector<T>> &vec) {
std::vector<T> ret; std::vector<T> ret;
@ -846,7 +854,7 @@ private:
// mapfile.cc // mapfile.cc
// //
void print_map(ArrayRef<ObjectFile *> files, ArrayRef<OutputChunk *> output_sections); void print_map();
// //
// main.cc // main.cc

View File

@ -352,7 +352,7 @@ void ObjectFile::maybe_override_symbol(const ELF64LE::Sym &esym, Symbol &sym, in
if (new_rank < existing_rank) { if (new_rank < existing_rank) {
sym.file = this; sym.file = this;
sym.input_section = isec; sym.input_section = isec;
sym.piece_ref = sym_pieces[idx]; sym.piece_ref = (idx < sym_pieces.size()) ? sym_pieces[idx] : StringPieceRef();
sym.value = esym.st_value; sym.value = esym.st_value;
sym.type = (is_dso && esym.getType() == STT_GNU_IFUNC) ? STT_FUNC : esym.getType(); sym.type = (is_dso && esym.getType() == STT_GNU_IFUNC) ? STT_FUNC : esym.getType();
sym.binding = esym.getBinding(); sym.binding = esym.getBinding();

View File

@ -297,12 +297,10 @@ static std::vector<u64> create_dynamic_section() {
}; };
int i = 1; int i = 1;
for (ObjectFile *file : out::objs) { for (ObjectFile *file : out::dsos) {
if (!file->soname.empty()) {
define(DT_NEEDED, i); define(DT_NEEDED, i);
i += file->soname.size() + 1; i += file->soname.size() + 1;
} }
}
define(DT_RELA, out::reldyn->shdr.sh_addr); define(DT_RELA, out::reldyn->shdr.sh_addr);
define(DT_RELASZ, out::reldyn->shdr.sh_size); define(DT_RELASZ, out::reldyn->shdr.sh_size);