1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00
mold/input_chunks.cc

117 lines
2.7 KiB
C++
Raw Normal View History

2020-10-20 08:54:35 +03:00
#include "mold.h"
2020-10-13 14:35:35 +03:00
2020-10-20 08:27:00 +03:00
using namespace llvm::ELF;
std::atomic_int num_relocs;
2020-10-19 16:55:52 +03:00
2020-10-13 14:35:35 +03:00
InputSection::InputSection(ObjectFile *file, const ELF64LE::Shdr *hdr, StringRef name)
2020-10-17 07:52:46 +03:00
: file(file), hdr(hdr) {
this->name = name;
2020-10-22 10:35:17 +03:00
this->output_section = OutputSection::get_instance(this);
2020-10-19 17:37:29 +03:00
2020-10-19 18:08:07 +03:00
uint64_t align = (hdr->sh_addralign == 0) ? 1 : hdr->sh_addralign;
if (align > UINT32_MAX)
2020-10-19 17:37:29 +03:00
error(toString(file) + ": section sh_addralign is too large");
2020-10-19 18:08:07 +03:00
if (__builtin_popcount(align) != 1)
error(toString(file) + ": section sh_addralign is not a power of two");
this->alignment = align;
2020-10-17 07:52:46 +03:00
}
2020-10-14 10:27:45 +03:00
2020-10-15 13:27:35 +03:00
uint64_t InputSection::get_size() const {
2020-10-14 10:27:45 +03:00
return hdr->sh_size;
}
2020-10-15 13:27:35 +03:00
2020-10-16 10:38:03 +03:00
void InputSection::copy_to(uint8_t *buf) {
2020-10-20 04:00:53 +03:00
if (hdr->sh_type == SHT_NOBITS || hdr->sh_size == 0)
return;
2020-10-16 10:38:03 +03:00
ArrayRef<uint8_t> data = check(file->obj.getSectionContents(*hdr));
memcpy(buf + offset, &data[0], data.size());
2020-10-15 13:27:35 +03:00
}
2020-10-19 17:37:29 +03:00
2020-10-23 03:21:40 +03:00
thread_local int count;
void InputSection::scan_relocations() {
if (rels.empty())
return;
for (const ELF64LE::Rela &rel : rels) {
Symbol *sym = file->symbols[rel.getSymbol(false)];
if (!sym)
continue;
switch (rel.getType(false)) {
case R_X86_64_GOTPCREL:
case R_X86_64_TLSGD:
case R_X86_64_GOTTPOFF:
case R_X86_64_PLT32:
if (!sym->needs_got)
sym->needs_got = true;
break;
default:
count++;
}
}
}
2020-10-20 04:32:32 +03:00
void InputSection::relocate(uint8_t *buf) {
2020-10-20 08:27:00 +03:00
if (rels.empty())
2020-10-20 04:32:32 +03:00
return;
2020-10-20 08:27:00 +03:00
int i = 0;
for (const ELF64LE::Rela &rel : rels) {
uint8_t *loc = buf + offset + rel.r_offset;
uint64_t val = 5;
switch (rel.getType(false)) {
case R_X86_64_8:
*loc = val;
break;
case R_X86_64_PC8:
*loc = val;
break;
case R_X86_64_16:
*(uint16_t *)loc = val;
break;
case R_X86_64_PC16:
*(uint16_t *)loc = val;
break;
case R_X86_64_32:
*(uint32_t *)loc = val;
break;
case R_X86_64_32S:
case R_X86_64_TPOFF32:
case R_X86_64_GOT32:
case R_X86_64_GOTPC32:
case R_X86_64_GOTPC32_TLSDESC:
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_PC32:
case R_X86_64_GOTTPOFF:
case R_X86_64_PLT32:
case R_X86_64_TLSGD:
case R_X86_64_TLSLD:
case R_X86_64_DTPOFF32:
case R_X86_64_SIZE32:
*(uint32_t *)loc = val;
break;
case R_X86_64_64:
case R_X86_64_DTPOFF64:
case R_X86_64_PC64:
case R_X86_64_SIZE64:
case R_X86_64_GOT64:
case R_X86_64_GOTOFF64:
case R_X86_64_GOTPC64:
*(uint64_t *)loc = val;
break;
default:
error(toString(this) + ": unknown relocation");
}
2020-10-22 19:14:11 +03:00
num_relocs++;
2020-10-20 08:27:00 +03:00
}
2020-10-20 04:32:32 +03:00
}
2020-10-19 17:37:29 +03:00
std::string toString(InputSection *isec) {
return (toString(isec->file) + ":(" + isec->name + ")").str();
}