1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-11 05:46:58 +03:00
mold/input_sections.cc

122 lines
3.2 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
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-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 18:26:31 +03:00
memcpy_nontemporal(buf, &data[0], data.size());
2020-10-15 13:27:35 +03:00
}
2020-10-19 17:37:29 +03:00
2020-10-29 10:41:17 +03:00
std::tuple<u64, u64> InputSection::scan_relocations() {
uint64_t num_got = 0;
uint64_t num_plt = 0;
2020-10-23 03:21:40 +03:00
for (const ELF64LE::Rela &rel : rels) {
2020-10-26 08:52:55 +03:00
Symbol *sym = file->get_symbol(rel.getSymbol(false));
2020-10-29 13:13:46 +03:00
if (!sym || !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-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-29 10:42:27 +03:00
num_got += !sym->needs_got.exchange(true);
2020-10-26 14:29:39 +03:00
break;
2020-10-29 13:13:46 +03:00
#if 0
2020-10-23 03:21:40 +03:00
case R_X86_64_PLT32:
2020-10-29 10:42:27 +03:00
num_got += !sym->needs_got.exchange(true);
num_plt += !sym->needs_plt.exchange(true);
2020-10-23 03:21:40 +03:00
break;
2020-10-29 13:13:46 +03:00
#endif
2020-10-23 03:21:40 +03:00
}
}
2020-10-29 10:41:17 +03:00
return {num_got, num_plt};
2020-10-23 03:21:40 +03:00
}
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-29 12:40:43 +03:00
u32 sym_idx = rel.getSymbol(false);
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
u64 cur = output_section->shdr.sh_addr + offset + rel.r_offset;
2020-10-29 12:40:43 +03:00
Symbol *sym = file->get_symbol(sym_idx);
u64 dst = sym ? sym->addr : file->get_symbol_addr(sym_idx);
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-29 14:16:20 +03:00
*(u16 *)loc = dst;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_PC16:
2020-10-29 14:16:20 +03:00
*(u16 *)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:
*(u32 *)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-29 13:23:07 +03:00
case R_X86_64_PLT32:
2020-10-29 12:40:43 +03:00
*(u32 *)loc = dst - cur - 4;
break;
2020-10-29 10:01:22 +03:00
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
2020-10-29 13:23:07 +03:00
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPC32:
2020-10-29 12:40:43 +03:00
*(u32 *)loc = out::got->shdr.sh_addr + sym->got_addr - cur - 4;
2020-10-20 08:27:00 +03:00
break;
case R_X86_64_64:
*(u64 *)loc = dst;
2020-10-20 08:27:00 +03:00
break;
2020-10-27 09:21:09 +03:00
case R_X86_64_PC64:
*(u64 *)loc = dst - cur - 4;
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:
case R_X86_64_GOTTPOFF:
2020-10-27 09:19:50 +03:00
case R_X86_64_TPOFF32:
2020-10-29 14:16:20 +03:00
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();
}