1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-29 11:24:36 +03:00
mold/input_sections.cc

119 lines
3.1 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-27 09:14:31 +03:00
using namespace llvm;
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-25 07:17:10 +03:00
InputSection::InputSection(ObjectFile *file, const ELF64LE::Shdr &shdr, StringRef name)
: file(file), shdr(shdr) {
2020-10-17 07:52:46 +03:00
this->name = name;
2020-10-27 06:45:59 +03:00
this->output_section = OutputSection::get_instance(name, shdr.sh_flags, shdr.sh_type);
2020-10-19 17:37:29 +03:00
2020-10-25 07:17:10 +03:00
uint64_t align = (shdr.sh_addralign == 0) ? 1 : shdr.sh_addralign;
2020-10-19 18:08:07 +03:00
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");
2020-10-17 07:52:46 +03:00
}
2020-10-14 10:27:45 +03:00
2020-10-16 10:38:03 +03:00
void InputSection::copy_to(uint8_t *buf) {
2020-10-25 07:17:10 +03:00
if (shdr.sh_type == SHT_NOBITS || shdr.sh_size == 0)
2020-10-20 04:00:53 +03:00
return;
2020-10-27 13:35:49 +03:00
2020-10-25 07:17:10 +03:00
ArrayRef<uint8_t> data = check(file->obj.getSectionContents(shdr));
2020-10-27 13:35:49 +03:00
buf = buf + output_section->shdr.sh_offset + offset;
2020-10-27 13:43:00 +03:00
#if 1
2020-10-27 13:35:49 +03:00
for (int i = 0; i < data.size(); i++) {
uint8_t val = __builtin_nontemporal_load(&data[0] + i);
__builtin_nontemporal_store(val, buf + i);
}
#else
memcpy(buf, &data[0], data.size());
#endif
2020-10-15 13:27:35 +03:00
}
2020-10-19 17:37:29 +03:00
2020-10-23 03:21:40 +03:00
void InputSection::scan_relocations() {
for (const ELF64LE::Rela &rel : rels) {
2020-10-26 08:52:55 +03:00
Symbol *sym = file->get_symbol(rel.getSymbol(false));
if (!sym)
2020-10-23 03:21:40 +03:00
continue;
switch (rel.getType(false)) {
2020-10-26 14:31:02 +03:00
case R_X86_64_GOT32:
2020-10-26 14:31:21 +03:00
case R_X86_64_GOT64:
case R_X86_64_GOTOFF64:
2020-10-26 14:31:02 +03:00
case R_X86_64_GOTPC32:
case R_X86_64_GOTPC32_TLSDESC:
2020-10-26 14:31:21 +03:00
case R_X86_64_GOTPC64:
2020-10-23 03:21:40 +03:00
case R_X86_64_GOTPCREL:
2020-10-26 14:31:02 +03:00
case R_X86_64_GOTPCRELX:
2020-10-23 03:21:40 +03:00
case R_X86_64_GOTTPOFF:
2020-10-26 14:31:21 +03:00
case R_X86_64_REX_GOTPCRELX:
2020-10-26 14:29:39 +03:00
sym->needs_got = true;
break;
2020-10-23 03:21:40 +03:00
case R_X86_64_PLT32:
2020-10-26 14:29:39 +03:00
sym->needs_got = true;
sym->needs_plt = true;
2020-10-23 03:21:40 +03:00
break;
}
}
}
2020-10-20 04:32:32 +03:00
void InputSection::relocate(uint8_t *buf) {
2020-10-20 08:27:00 +03:00
int i = 0;
for (const ELF64LE::Rela &rel : rels) {
2020-10-25 11:37:24 +03:00
uint8_t *loc = buf + output_section->shdr.sh_offset + offset + rel.r_offset;
2020-10-26 14:10:23 +03:00
uint64_t cur = output_section->shdr.sh_addr + offset + rel.r_offset;
2020-10-26 14:12:16 +03:00
uint64_t dst = file->get_symbol_value(rel.getSymbol(false));
2020-10-20 08:27:00 +03:00
switch (rel.getType(false)) {
case R_X86_64_8:
2020-10-26 14:10:23 +03:00
*loc = dst;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_PC8:
2020-10-26 14:10:23 +03:00
*loc = dst - cur;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_16:
2020-10-26 14:10:23 +03:00
*(uint16_t *)loc = dst;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_PC16:
2020-10-26 14:10:23 +03:00
*(uint16_t *)loc = dst - cur - 4;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_32:
2020-10-27 09:19:50 +03:00
case R_X86_64_32S:
2020-10-26 14:10:23 +03:00
*(uint32_t *)loc = dst;
2020-10-20 08:27:00 +03:00
break;
2020-10-26 14:31:21 +03:00
case R_X86_64_PC32:
2020-10-26 14:10:23 +03:00
*(uint32_t *)loc = dst - cur - 4;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_64:
2020-10-26 14:10:23 +03:00
*(uint64_t *)loc = dst;
2020-10-20 08:27:00 +03:00
break;
2020-10-27 09:21:09 +03:00
case R_X86_64_PC64:
*(uint64_t *)loc = dst - cur - 4;
break;
case R_X86_64_DTPOFF32:
2020-10-27 09:19:50 +03:00
case R_X86_64_GOTPC32:
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_GOTTPOFF:
case R_X86_64_PLT32:
case R_X86_64_REX_GOTPCRELX:
case R_X86_64_TLSGD:
2020-10-27 09:21:09 +03:00
case R_X86_64_TLSLD:
2020-10-27 09:19:50 +03:00
case R_X86_64_TPOFF32:
break;
2020-10-20 08:27:00 +03:00
default:
2020-10-27 09:14:31 +03:00
error(toString(this) + ": unknown relocation: " + std::to_string(rel.getType(false)));
2020-10-20 08:27:00 +03:00
}
2020-10-23 06:09:27 +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();
}