2020-10-28 14:49:43 +03:00
|
|
|
#include "mold.h"
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2021-03-15 18:53:53 +03:00
|
|
|
#include <fstream>
|
2020-12-10 16:32:47 +03:00
|
|
|
#include <iomanip>
|
|
|
|
#include <ios>
|
2021-03-15 19:11:55 +03:00
|
|
|
#include <tbb/parallel_for_each.h>
|
2020-12-12 14:34:13 +03:00
|
|
|
#include <unordered_map>
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2021-03-15 18:53:53 +03:00
|
|
|
static std::ofstream *open_output_file(std::string path) {
|
|
|
|
std::ofstream *file = new std::ofstream;
|
|
|
|
file->open(path.c_str());
|
|
|
|
if (!file->is_open())
|
|
|
|
Fatal() << "cannot open " << config.Map << ": " << strerror(errno);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:31:05 +03:00
|
|
|
void print_map() {
|
2021-03-15 19:11:55 +03:00
|
|
|
typedef tbb::concurrent_hash_map<InputSection *, std::vector<Symbol *>> MapTy;
|
|
|
|
|
2021-03-15 18:53:53 +03:00
|
|
|
std::ostream *out = &std::cout;
|
|
|
|
std::ofstream *file = nullptr;
|
|
|
|
|
|
|
|
if (!config.Map.empty())
|
|
|
|
out = file = open_output_file(config.Map);
|
|
|
|
|
2020-10-29 06:24:54 +03:00
|
|
|
// Construct a section-to-symbol map.
|
2021-03-15 19:11:55 +03:00
|
|
|
MapTy map;
|
|
|
|
|
|
|
|
tbb::parallel_for_each(out::objs, [&](ObjectFile *file) {
|
|
|
|
for (Symbol *sym : file->symbols) {
|
2021-03-12 08:32:19 +03:00
|
|
|
if (sym->file == file && sym->input_section &&
|
2021-03-15 19:11:55 +03:00
|
|
|
sym->get_type() != STT_SECTION) {
|
|
|
|
assert(file == &sym->input_section->file);
|
2020-12-12 14:34:13 +03:00
|
|
|
|
2021-03-15 19:11:55 +03:00
|
|
|
MapTy::accessor acc;
|
|
|
|
map.insert(acc, {sym->input_section, {}});
|
|
|
|
acc->second.push_back(sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
tbb::parallel_for(map.range(), [](const MapTy::range_type &range) {
|
|
|
|
for (auto it = range.begin(); it != range.end(); it++) {
|
|
|
|
std::vector<Symbol *> &vec = it->second;
|
|
|
|
sort(vec, [](Symbol *a, Symbol *b) { return a->value < b->value; });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
*out << " VMA Size Align Out In Symbol\n";
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2020-11-24 08:31:05 +03:00
|
|
|
for (OutputChunk *osec : out::chunks) {
|
2021-03-15 18:53:53 +03:00
|
|
|
*out << std::setw(16) << (u64)osec->shdr.sh_addr
|
2021-03-15 19:11:55 +03:00
|
|
|
<< std::setw(11) << (u64)osec->shdr.sh_size
|
2021-03-15 18:53:53 +03:00
|
|
|
<< std::setw(6) << (u64)osec->shdr.sh_addralign
|
|
|
|
<< " " << osec->name << "\n";
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2020-11-08 10:09:01 +03:00
|
|
|
if (osec->kind != OutputChunk::REGULAR)
|
|
|
|
continue;
|
|
|
|
|
2021-03-11 11:58:43 +03:00
|
|
|
for (InputSection *mem : ((OutputSection *)osec)->members) {
|
2021-03-15 18:53:53 +03:00
|
|
|
*out << std::setw(16) << (osec->shdr.sh_addr + mem->offset)
|
2021-03-15 19:11:55 +03:00
|
|
|
<< std::setw(11) << (u64)mem->shdr.sh_size
|
2021-03-15 18:53:53 +03:00
|
|
|
<< std::setw(6) << (u64)mem->shdr.sh_addralign
|
|
|
|
<< " " << *mem << "\n";
|
2020-10-29 06:24:54 +03:00
|
|
|
|
2021-03-15 19:11:55 +03:00
|
|
|
MapTy::const_accessor acc;
|
|
|
|
if (!map.find(acc, mem))
|
2020-12-12 14:34:13 +03:00
|
|
|
continue;
|
|
|
|
|
2021-03-15 19:11:55 +03:00
|
|
|
std::vector<Symbol *> syms = acc->second;
|
2020-12-12 14:34:13 +03:00
|
|
|
for (Symbol *sym : syms)
|
2021-03-15 18:53:53 +03:00
|
|
|
*out << std::setw(16) << sym->get_addr()
|
2021-03-15 19:11:55 +03:00
|
|
|
<< " 0 0 "
|
2021-03-15 18:53:53 +03:00
|
|
|
<< *sym << "\n";
|
2020-10-29 06:24:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 18:53:53 +03:00
|
|
|
|
|
|
|
if (file)
|
|
|
|
file->close();
|
2020-10-29 06:24:54 +03:00
|
|
|
}
|