1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 18:40:59 +03:00
mold/input_files.cc

297 lines
8.1 KiB
C++
Raw Normal View History

2020-10-20 08:54:35 +03:00
#include "mold.h"
2020-10-04 12:00:33 +03:00
using namespace llvm;
2020-10-09 14:47:45 +03:00
using namespace llvm::ELF;
2020-10-04 12:00:33 +03:00
2020-10-13 14:35:35 +03:00
std::atomic_int num_defined;
std::atomic_int num_undefined;
2020-10-23 07:17:21 +03:00
std::atomic_int num_all_syms;
2020-10-22 19:14:11 +03:00
std::atomic_int num_comdats;
2020-10-23 07:17:21 +03:00
std::atomic_int num_regular_sections;
2020-10-10 13:15:16 +03:00
std::atomic_int num_files;
2020-10-23 07:23:12 +03:00
std::atomic_int num_relocs_alloc;
2020-10-22 18:52:36 +03:00
std::atomic_int num_string_pieces;
2020-10-10 13:15:16 +03:00
2020-10-14 12:42:54 +03:00
ObjectFile::ObjectFile(MemoryBufferRef mb, StringRef archive_name)
2020-10-16 10:38:03 +03:00
: mb(mb), archive_name(archive_name),
obj(check(ELFFile<ELF64LE>::create(mb.getBuffer()))) {}
2020-10-04 12:00:33 +03:00
2020-10-09 14:47:45 +03:00
MemoryBufferRef readFile(StringRef path) {
auto mbOrErr = MemoryBuffer::getFile(path, -1, false);
if (auto ec = mbOrErr.getError())
error("cannot open " + path + ": " + ec.message());
2020-10-04 12:00:33 +03:00
2020-10-09 14:47:45 +03:00
std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
MemoryBufferRef mbref = mb->getMemBufferRef();
mb.release();
return mbref;
}
static const ELF64LE::Shdr
*findSection(ArrayRef<ELF64LE::Shdr> sections, uint32_t type) {
for (const ELF64LE::Shdr &sec : sections)
if (sec.sh_type == type)
return &sec;
return nullptr;
}
2020-10-19 14:05:34 +03:00
void ObjectFile::initialize_sections() {
StringRef section_strtab = CHECK(obj.getSectionStringTable(elf_sections), this);
2020-10-19 14:17:32 +03:00
sections.resize(elf_sections.size());
for (int i = 0; i < elf_sections.size(); i++) {
const ELF64LE::Shdr &shdr = elf_sections[i];
2020-10-22 16:42:09 +03:00
if ((shdr.sh_flags & SHF_EXCLUDE) && !(shdr.sh_flags & SHF_ALLOC))
continue;
2020-10-19 14:17:32 +03:00
switch (shdr.sh_type) {
case SHT_GROUP: {
// Get the signature of this section group.
if (shdr.sh_info >= elf_syms.size())
error(toString(this) + ": invalid symbol index");
const ELF64LE::Sym &sym = elf_syms[shdr.sh_info];
StringRef signature = CHECK(sym.getName(string_table), this);
2020-10-19 15:32:57 +03:00
// Get comdat group members.
ArrayRef<ELF64LE::Word> entries =
CHECK(obj.template getSectionContentsAsArray<ELF64LE::Word>(shdr), this);
if (entries.empty())
error(toString(this) + ": empty SHT_GROUP");
if (entries[0] == 0)
continue;
if (entries[0] != GRP_COMDAT)
error(toString(this) + ": unsupported SHT_GROUP format");
2020-10-19 14:17:32 +03:00
static ConcurrentMap<ComdatGroup> map;
2020-10-26 09:37:12 +03:00
ComdatGroup *group = map.insert(signature, ComdatGroup(nullptr, 0));
comdat_groups.push_back({group, i});
2020-10-26 08:46:07 +03:00
// num_comdats++;
2020-10-19 15:32:57 +03:00
break;
2020-10-19 14:17:32 +03:00
}
2020-10-19 16:55:52 +03:00
case SHT_SYMTAB_SHNDX:
error(toString(this) + ": SHT_SYMTAB_SHNDX section is not supported");
break;
case SHT_SYMTAB:
case SHT_STRTAB:
case SHT_REL:
case SHT_RELA:
case SHT_NULL:
break;
2020-10-19 14:17:32 +03:00
default: {
2020-10-26 08:46:07 +03:00
// num_regular_sections++;
2020-10-22 18:52:36 +03:00
if ((shdr.sh_flags & SHF_STRINGS) && !(shdr.sh_flags & SHF_WRITE) &&
shdr.sh_entsize == 1) {
2020-10-22 18:34:46 +03:00
read_string_pieces(shdr);
break;
}
2020-10-19 14:17:32 +03:00
StringRef name = CHECK(obj.getSectionName(shdr, section_strtab), this);
2020-10-25 03:29:03 +03:00
this->sections[i] = new InputSection(this, shdr, name);
2020-10-19 14:17:32 +03:00
break;
}
}
2020-10-13 14:35:35 +03:00
}
2020-10-20 04:32:32 +03:00
for (int i = 0; i < elf_sections.size(); i++) {
const ELF64LE::Shdr &shdr = elf_sections[i];
if (shdr.sh_type != SHT_RELA)
continue;
if (shdr.sh_info >= sections.size())
error(toString(this) + ": invalid relocated section index: " +
Twine(shdr.sh_info));
InputSection *target = sections[shdr.sh_info];
2020-10-23 07:23:12 +03:00
if (target) {
2020-10-20 04:32:32 +03:00
target->rels = CHECK(obj.relas(shdr), this);
2020-10-26 08:46:07 +03:00
// if (target->shdr.sh_flags & SHF_ALLOC)
// num_relocs_alloc += target->rels.size();
2020-10-23 07:23:12 +03:00
}
2020-10-20 04:32:32 +03:00
}
2020-10-19 14:05:34 +03:00
}
2020-10-09 14:47:45 +03:00
2020-10-19 14:05:34 +03:00
void ObjectFile::initialize_symbols() {
2020-10-10 10:49:02 +03:00
if (!symtab_sec)
return;
2020-10-26 08:43:52 +03:00
this->symbols.resize(elf_syms.size() - first_global);
2020-10-10 06:18:11 +03:00
2020-10-26 08:43:52 +03:00
for (int i = 0, j = first_global; j < elf_syms.size(); i++, j++) {
StringRef name = CHECK(elf_syms[j].getName(string_table), this);
2020-10-25 08:42:44 +03:00
symbols[i] = Symbol::intern(name);
2020-10-09 17:26:26 +03:00
}
2020-10-10 06:18:11 +03:00
}
void ObjectFile::remove_comdat_members(uint32_t section_idx) {
const ELF64LE::Shdr &shdr = elf_sections[section_idx];
ArrayRef<ELF64LE::Word> entries =
CHECK(obj.template getSectionContentsAsArray<ELF64LE::Word>(shdr), this);
for (uint32_t i : entries)
sections[i] = nullptr;
}
2020-10-22 18:34:46 +03:00
void ObjectFile::read_string_pieces(const ELF64LE::Shdr &shdr) {
2020-10-22 18:52:36 +03:00
static ConcurrentMap<StringPiece> map1;
static ConcurrentMap<StringPiece> map2;
bool is_alloc = shdr.sh_type & SHF_ALLOC;
ConcurrentMap<StringPiece> &map = is_alloc ? map1 : map2;
2020-10-22 18:34:46 +03:00
ArrayRef<uint8_t> arr = CHECK(obj.getSectionContents(shdr), this);
StringRef data((const char *)&arr[0], arr.size());
while (!data.empty()) {
size_t end = data.find('\0');
if (end == StringRef::npos)
error(toString(this) + ": string is not null terminated");
StringRef substr = data.substr(0, end + 1);
StringPiece *piece = map.insert(substr, StringPiece(substr));
2020-10-22 18:52:36 +03:00
if (is_alloc)
merged_strings_alloc.push_back(piece);
else
merged_strings_noalloc.push_back(piece);
2020-10-22 18:34:46 +03:00
data = data.substr(end + 1);
2020-10-26 08:46:07 +03:00
// num_string_pieces++;
2020-10-22 18:34:46 +03:00
}
}
2020-10-19 14:05:34 +03:00
void ObjectFile::parse() {
num_files++;
2020-10-19 14:17:32 +03:00
bool is_dso = (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object);
elf_sections = CHECK(obj.sections(), this);
symtab_sec = findSection(elf_sections, is_dso ? SHT_DYNSYM : SHT_SYMTAB);
first_global = symtab_sec->sh_info;
elf_syms = CHECK(obj.symbols(symtab_sec), this);
string_table = CHECK(obj.getStringTableForSymtab(*symtab_sec, elf_sections), this);
2020-10-26 08:46:07 +03:00
// num_all_syms += elf_syms.size();
2020-10-23 07:17:21 +03:00
2020-10-19 14:05:34 +03:00
initialize_sections();
initialize_symbols();
}
2020-10-18 14:19:57 +03:00
class Spinlock {
public:
Spinlock(std::atomic_flag &lock) : lock(lock) {
while (lock.test_and_set(std::memory_order_acquire));
}
~Spinlock() {
lock.clear(std::memory_order_release);
}
private:
std::atomic_flag &lock;
};
2020-10-10 06:18:11 +03:00
void ObjectFile::register_defined_symbols() {
2020-10-26 08:43:52 +03:00
for (int i = 0, j = first_global; j < elf_syms.size(); i++, j++) {
if (!elf_syms[j].isDefined())
2020-10-13 14:35:35 +03:00
continue;
2020-10-26 10:45:53 +03:00
if (sections[elf_syms[j].st_shndx] == nullptr)
continue;
2020-10-26 08:46:07 +03:00
// num_defined++;
2020-10-18 13:00:39 +03:00
2020-10-18 14:19:57 +03:00
Symbol *sym = symbols[i];
2020-10-26 09:15:38 +03:00
bool is_weak = (elf_syms[j].getBinding() == STB_WEAK);
2020-10-26 10:45:53 +03:00
Spinlock lock(sym->lock);
2020-10-26 09:15:03 +03:00
if (!sym->file || this->priority < sym->file->priority ||
(sym->is_weak && !is_weak)) {
sym->file = this;
2020-10-26 09:15:38 +03:00
sym->visibility = elf_syms[j].getVisibility();
2020-10-26 09:15:03 +03:00
sym->is_weak = is_weak;
}
2020-10-13 14:35:35 +03:00
}
}
void ObjectFile::register_undefined_symbols() {
2020-10-18 15:03:51 +03:00
if (is_alive.exchange(true))
2020-10-18 14:19:57 +03:00
return;
2020-10-26 08:43:52 +03:00
for (int i = 0, j = first_global; j < elf_syms.size(); i++, j++) {
if (elf_syms[j].isDefined())
2020-10-13 14:35:35 +03:00
continue;
2020-10-26 08:46:07 +03:00
// num_undefined++;
2020-10-18 14:19:57 +03:00
Symbol *sym = symbols[i];
2020-10-18 15:03:51 +03:00
if (sym->file && sym->file->is_in_archive() && !sym->file->is_alive)
2020-10-18 14:19:57 +03:00
sym->file->register_undefined_symbols();
2020-10-09 16:29:25 +03:00
}
2020-10-09 14:47:45 +03:00
}
2020-10-19 15:50:33 +03:00
void ObjectFile::eliminate_duplicate_comdat_groups() {
for (auto &pair : comdat_groups) {
ComdatGroup *g = pair.first;
uint32_t section_idx = pair.second;
2020-10-23 06:43:22 +03:00
ObjectFile *other = g->file;
if (other && other->priority < this->priority) {
this->remove_comdat_members(section_idx);
continue;
}
2020-10-23 06:41:17 +03:00
2020-10-23 06:43:22 +03:00
ObjectFile *file;
uint32_t idx;
2020-10-23 06:41:17 +03:00
2020-10-23 06:24:25 +03:00
{
Spinlock lock(g->lock);
if (g->file == nullptr) {
g->file = this;
g->section_idx = section_idx;
2020-10-23 06:24:52 +03:00
continue;
}
2020-10-23 06:43:22 +03:00
if (g->file.load()->priority < this->priority) {
2020-10-23 06:24:25 +03:00
file = this;
idx = section_idx;
} else {
file = g->file;
idx = g->section_idx;
2020-10-23 06:41:17 +03:00
g->file = this;
g->section_idx = section_idx;
2020-10-23 06:24:25 +03:00
}
2020-10-19 15:50:33 +03:00
}
2020-10-23 06:24:52 +03:00
file->remove_comdat_members(idx);
2020-10-19 15:50:33 +03:00
}
}
2020-10-23 03:21:40 +03:00
void ObjectFile::scan_relocations() {
for (InputSection *isec : sections)
if (isec)
isec->scan_relocations();
}
2020-10-26 08:57:22 +03:00
void ObjectFile::fix_sym_addrs() {
for (int i = 0, j = first_global; j < elf_syms.size(); i++, j++) {
if (symbols[i]->file != this)
continue;
InputSection *isec = sections[elf_syms[j].st_shndx];
OutputSection *osec = isec->output_section;
symbols[i]->addr = osec->shdr.sh_addr + isec->offset + elf_syms[j].st_value;
}
}
2020-10-18 13:05:28 +03:00
StringRef ObjectFile::get_filename() {
return mb.getBufferIdentifier();
}
bool ObjectFile::is_in_archive() {
return !archive_name.empty();
}
2020-10-09 14:47:45 +03:00
std::string toString(ObjectFile *obj) {
2020-10-14 13:04:36 +03:00
StringRef s = obj->get_filename();
if (obj->archive_name == "")
return s.str();
return (obj->archive_name + ":" + s).str();
2020-10-09 14:47:45 +03:00
}