1
1
mirror of https://github.com/rui314/mold.git synced 2024-12-27 02:13:33 +03:00
mold/input_sections.cc

160 lines
4.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;
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
u64 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-29 16:32:55 +03:00
void InputSection::copy_to(u8 *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-29 16:32:55 +03:00
ArrayRef<u8> data = check(file->obj.getSectionContents(shdr));
2020-10-27 13:35:49 +03:00
buf = buf + output_section->shdr.sh_offset + offset;
2020-11-03 11:01:23 +03:00
memcpy(buf, &data[0], data.size());
2020-10-15 13:27:35 +03:00
}
2020-10-19 17:37:29 +03:00
2020-11-03 05:01:25 +03:00
void InputSection::scan_relocations() {
2020-10-23 03:21:40 +03:00
for (const ELF64LE::Rela &rel : rels) {
2020-11-03 09:08:05 +03:00
Symbol *sym = file->symbols[rel.getSymbol(false)];
2020-11-03 11:08:05 +03:00
if (!sym->file)
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-31 13:33:34 +03:00
case R_X86_64_REX_GOTPCRELX: {
std::lock_guard lock(sym->mu);
2020-11-03 15:51:40 +03:00
if (!sym->needs_got) {
sym->needs_got = true;
2020-11-03 05:01:25 +03:00
file->num_got++;
2020-10-31 13:33:34 +03:00
}
2020-10-26 14:29:39 +03:00
break;
2020-10-31 13:33:34 +03:00
}
case R_X86_64_GOTTPOFF: {
std::lock_guard lock(sym->mu);
2020-11-03 15:51:40 +03:00
if (!sym->needs_gottp) {
sym->needs_gottp = true;
2020-11-03 05:01:25 +03:00
file->num_got++;
2020-10-31 13:33:34 +03:00
}
2020-10-30 11:42:39 +03:00
break;
2020-10-31 13:33:34 +03:00
}
2020-10-31 13:39:55 +03:00
case R_X86_64_PLT32: {
2020-11-01 09:27:04 +03:00
if (sym->type != STT_GNU_IFUNC)
break;
2020-10-31 13:39:55 +03:00
std::lock_guard lock(sym->mu);
2020-11-03 15:51:40 +03:00
if (!sym->needs_plt) {
assert(!sym->needs_gotplt);
sym->needs_plt = true;
sym->needs_gotplt = true;
2020-11-03 05:01:25 +03:00
file->num_plt++;
file->num_gotplt++;
file->num_relplt++;
2020-10-31 13:39:55 +03:00
}
2020-10-23 03:21:40 +03:00
break;
2020-10-31 13:39:55 +03:00
}
2020-10-23 03:21:40 +03:00
}
}
}
2020-10-29 16:32:55 +03:00
void InputSection::relocate(u8 *buf) {
2020-10-20 08:27:00 +03:00
for (const ELF64LE::Rela &rel : rels) {
2020-11-03 16:39:44 +03:00
Symbol *sym = file->symbols[rel.getSymbol(false)];
2020-10-29 16:54:21 +03:00
u8 *loc = buf + output_section->shdr.sh_offset + offset + rel.r_offset;
2020-10-29 16:46:00 +03:00
2020-11-03 09:16:09 +03:00
u64 G = sym->got_offset;
u64 GOT = out::got->shdr.sh_addr;
u64 S = sym->addr;
2020-11-03 09:16:09 +03:00
u64 A = rel.r_addend;
u64 P = output_section->shdr.sh_addr + offset + rel.r_offset;
u64 L = out::plt->shdr.sh_addr + sym->plt_offset;
2020-10-29 16:46:00 +03:00
2020-10-20 08:27:00 +03:00
switch (rel.getType(false)) {
2020-10-29 16:54:11 +03:00
case R_X86_64_NONE:
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_64:
*(u64 *)loc = S + A;
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_PC32:
*(u32 *)loc = S + A - P;
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_GOT32:
2020-11-03 04:31:39 +03:00
*(u64 *)loc = G + A;
2020-10-29 16:54:11 +03:00
break;
case R_X86_64_PLT32:
2020-11-03 09:16:33 +03:00
if (sym->type == STT_GNU_IFUNC)
2020-11-01 11:46:08 +03:00
*(u32 *)loc = out::plt->shdr.sh_addr + sym->plt_offset + A - P;
else
*(u32 *)loc = S + A - P; // todo
2020-10-30 05:40:38 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_GOTPCREL:
2020-11-03 04:31:39 +03:00
*(u32 *)loc = G + GOT + A - P;
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-29 16:47:04 +03:00
*(u32 *)loc = S + A;
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_16:
*(u16 *)loc = S + A;
2020-10-29 16:46:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_PC16:
*(u16 *)loc = S + A - P;
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_8:
*loc = S + A;
2020-10-20 08:27:00 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_PC8:
*loc = S + A - P;
2020-10-27 09:21:09 +03:00
break;
2020-10-27 09:19:50 +03:00
case R_X86_64_TLSGD:
2020-10-27 09:21:09 +03:00
case R_X86_64_TLSLD:
2020-10-29 14:16:20 +03:00
case R_X86_64_DTPOFF32:
2020-11-02 09:56:41 +03:00
// TODO
break;
2020-11-03 09:16:54 +03:00
case R_X86_64_GOTTPOFF:
*(u32 *)loc = sym->gottp_offset + GOT + A - P;
2020-10-30 10:55:59 +03:00
break;
2020-10-27 09:19:50 +03:00
case R_X86_64_TPOFF32:
2020-10-30 11:08:35 +03:00
*(u32 *)loc = S - out::tls_end;
2020-10-29 14:16:20 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_PC64:
*(u64 *)loc = S + A - P;
break;
case R_X86_64_GOTPC32:
*(u32 *)loc = GOT + A - P;
break;
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
2020-11-03 04:31:39 +03:00
*(u32 *)loc = G + GOT + A - P;
2020-10-29 16:54:11 +03:00
break;
2020-10-20 08:27:00 +03:00
default:
2020-11-03 04:31:39 +03:00
error(toString(this) + ": unknown relocation: " +
std::to_string(rel.getType(false)));
2020-10-20 08:27:00 +03:00
}
2020-11-03 11:52:39 +03:00
static Counter counter("relocs");
counter.inc();
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();
}