1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-25 17:34:02 +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

61
main.cc
View File

@ -148,9 +148,11 @@ void read_file(MemoryBufferRef mb) {
out::objs.push_back(new ObjectFile(member, mb.getBufferIdentifier()));
break;
case file_magic::elf_relocatable:
case file_magic::elf_shared_object:
out::objs.push_back(new ObjectFile(mb, ""));
break;
case file_magic::elf_shared_object:
out::dsos.push_back(new ObjectFile(mb, ""));
break;
case file_magic::unknown:
parse_linker_script(mb.getBufferIdentifier(), mb.getBuffer());
break;
@ -177,13 +179,13 @@ static void resolve_symbols() {
MyTimer t("resolve_symbols", before_copy_timer);
// Register defined symbols
tbb::parallel_for_each(out::objs,
[](ObjectFile *file) { file->resolve_symbols(); });
tbb::parallel_for_each(out::objs, [](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.
std::vector<ObjectFile *> root;
for (ObjectFile *file : out::objs)
if (file->is_alive && !file->is_dso)
if (file->is_alive)
root.push_back(file);
tbb::parallel_do(
@ -405,12 +407,12 @@ static void scan_rels() {
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) {
ObjectFile *file = out::objs[i];
for (Symbol *sym : file->symbols)
if (sym->file == file && sym->flags)
tbb::parallel_for(0, (int)files.size(), [&](int i) {
for (Symbol *sym : files[i]->symbols)
if (sym->file == files[i] && sym->flags)
vec[i].push_back(sym);
});
@ -716,17 +718,17 @@ int main(int argc, char **argv) {
{
MyTimer t("parse", parse_timer);
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
{
llvm::StringSet<> seen;
for (auto it = out::objs.begin(); it != out::objs.end();) {
StringRef soname = (*it)->soname;
if (!soname.empty() && !seen.insert(soname).second)
out::objs.erase(it);
for (int i = 0; i < out::dsos.size();) {
if (seen.insert(out::dsos[i]->soname).second)
i++;
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.
int priority = 2;
for (ObjectFile *file : out::objs)
if (!file->is_in_archive && !file->is_dso)
if (!file->is_in_archive)
file->priority = priority++;
for (ObjectFile *file : out::objs)
if (file->is_in_archive && !file->is_dso)
file->priority = priority++;
for (ObjectFile *file : out::objs)
if (file->is_dso)
if (file->is_in_archive)
file->priority = priority++;
for (ObjectFile *file : out::dsos)
file->priority = priority++;
// Resolve symbols and fix the set of object files that are
// included to the final output.
resolve_symbols();
if (args.hasArg(OPT_trace))
if (args.hasArg(OPT_trace)) {
for (ObjectFile *file : out::objs)
message(toString(file));
for (ObjectFile *file : out::dsos)
message(toString(file));
}
// Remove redundant comdat sections (e.g. duplicate inline functions).
eliminate_comdats();
@ -862,9 +866,8 @@ int main(int argc, char **argv) {
check_undefined_symbols();
// Copy shared object name strings to .dynsym
for (ObjectFile *file : out::objs)
if (file->is_alive && file->is_dso)
out::dynstr->add_string(file->soname);
for (ObjectFile *file : out::dsos)
out::dynstr->add_string(file->soname);
// Add headers and sections that have to be at the beginning
// or the ending of a file.
@ -939,23 +942,17 @@ int main(int argc, char **argv) {
if (config.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
Counter num_input_sections("input_sections");
for (ObjectFile *file : out::objs)
num_input_sections.inc(file->sections.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::print();

View File

@ -4,16 +4,16 @@
using namespace llvm;
void print_map(ArrayRef<ObjectFile *> files, ArrayRef<OutputChunk *> output_sections) {
void print_map() {
// Construct a section-to-symbol map.
std::unordered_multimap<InputChunk *, Symbol *> map;
for (ObjectFile *file : files)
for (ObjectFile *file : out::objs)
for (Symbol *sym : file->symbols)
if (sym->file == file && sym->input_section)
map.insert({sym->input_section, sym});
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 ",
(u64)osec->shdr.sh_addr,
(u64)osec->shdr.sh_size,

10
mold.h
View File

@ -618,6 +618,7 @@ namespace out {
using namespace llvm::ELF;
inline std::vector<ObjectFile *> objs;
inline std::vector<ObjectFile *> dsos;
inline std::vector<OutputChunk *> chunks;
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));
}
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>
inline std::vector<T> flatten(std::vector<std::vector<T>> &vec) {
std::vector<T> ret;
@ -846,7 +854,7 @@ private:
// mapfile.cc
//
void print_map(ArrayRef<ObjectFile *> files, ArrayRef<OutputChunk *> output_sections);
void print_map();
//
// 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) {
sym.file = this;
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.type = (is_dso && esym.getType() == STT_GNU_IFUNC) ? STT_FUNC : esym.getType();
sym.binding = esym.getBinding();

View File

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