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

189 lines
5.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;
2020-11-08 08:13:59 +03:00
InputChunk::InputChunk(Kind kind, ObjectFile *file, const ELF64LE::Shdr &shdr,
StringRef name)
: kind(kind), file(file), shdr(shdr), name(name) {
2020-11-08 06:26:40 +03:00
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
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-11-07 18:33:13 +03:00
// Copy data
2020-10-29 16:32:55 +03:00
ArrayRef<u8> data = check(file->obj.getSectionContents(shdr));
2020-11-06 03:59:08 +03:00
memcpy(buf + output_section->shdr.sh_offset + offset, &data[0], data.size());
2020-10-23 03:21:40 +03:00
2020-11-07 18:33:13 +03:00
// Apply relocations
u8 *base = buf + output_section->shdr.sh_offset + offset;
u64 sh_addr = output_section->shdr.sh_addr + offset;
2020-11-11 15:32:41 +03:00
u64 GOT = out::got->shdr.sh_addr;
2020-11-07 18:33:13 +03:00
2020-11-07 14:05:51 +03:00
for (int i = 0; i < rels.size(); i++) {
const ELF64LE::Rela &rel = rels[i];
StringPieceRef &ref = rel_pieces[i];
Symbol &sym = *file->symbols[rel.getSymbol(false)];
2020-11-07 18:33:13 +03:00
u8 *loc = base + rel.r_offset;
2020-10-29 16:46:00 +03:00
2020-11-07 16:46:37 +03:00
u64 S = ref.piece ? ref.piece->get_addr() : sym.get_addr();
u64 A = ref.piece ? ref.addend : rel.r_addend;
2020-11-07 18:33:13 +03:00
u64 P = sh_addr + rel.r_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-15 10:19:21 +03:00
*(u64 *)loc = sym.get_got_addr() - GOT + A;
2020-10-29 16:54:11 +03:00
break;
case R_X86_64_PLT32:
2020-11-13 10:39:56 +03:00
if (config.is_static && sym.type != STT_GNU_IFUNC)
2020-11-01 11:46:08 +03:00
*(u32 *)loc = S + A - P; // todo
2020-11-13 10:39:56 +03:00
else
2020-11-15 10:19:21 +03:00
*(u32 *)loc = sym.get_plt_addr() + A - P;
2020-10-30 05:40:38 +03:00
break;
2020-10-29 16:54:11 +03:00
case R_X86_64_GOTPCREL:
2020-11-15 10:19:21 +03:00
*(u32 *)loc = sym.get_got_addr() + 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:
2020-11-15 08:07:40 +03:00
*(u32 *)loc = sym.get_gottp_addr() + 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-15 10:19:21 +03:00
*(u32 *)loc = sym.get_got_addr() + 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-06 07:56:11 +03:00
static Counter counter("relocs");
counter.inc(rels.size());
2020-10-20 04:32:32 +03:00
}
2020-11-06 06:01:52 +03:00
void InputSection::scan_relocations() {
2020-11-07 21:11:44 +03:00
if (!(shdr.sh_flags & SHF_ALLOC))
return;
2020-11-06 03:59:08 +03:00
for (const ELF64LE::Rela &rel : rels) {
Symbol *sym = file->symbols[rel.getSymbol(false)];
if (!sym->file || !sym->file->is_alive)
continue;
switch (rel.getType(false)) {
case R_X86_64_8:
case R_X86_64_16:
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_64:
case R_X86_64_PC8:
case R_X86_64_PC16:
case R_X86_64_PC32:
case R_X86_64_PC64:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_ADDR_REL;
break;
case R_X86_64_GOT32:
case R_X86_64_GOTPC32:
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
case R_X86_64_REX_GOTPCRELX:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_GOT_REL;
break;
case R_X86_64_PLT32:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_PLT_REL;
break;
case R_X86_64_TLSGD:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_TLSGD_REL;
break;
case R_X86_64_TLSLD:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_TLSLD_REL;
break;
case R_X86_64_GOTTPOFF:
2020-11-15 10:19:21 +03:00
sym->rels |= Symbol::HAS_GOTTP_REL;
case R_X86_64_DTPOFF32:
case R_X86_64_TPOFF32:
2020-11-15 04:06:11 +03:00
sym->rels |= Symbol::HAS_TPOFF_REL;
break;
case R_X86_64_NONE:
break;
default:
error(toString(this) + ": unknown relocation: " +
std::to_string(rel.getType(false)));
}
2020-11-06 03:59:08 +03:00
}
}
2020-11-08 08:13:59 +03:00
MergeableSection::MergeableSection(InputSection *isec, ArrayRef<u8> contents)
: InputChunk(MERGEABLE, isec->file, isec->shdr, isec->name),
parent(*MergedSection::get_instance(isec->name, isec->shdr.sh_flags,
isec->shdr.sh_type)) {
StringRef data((const char *)&contents[0], contents.size());
u32 offset = 0;
while (!data.empty()) {
size_t end = data.find('\0');
if (end == StringRef::npos)
error(toString(this) + ": string is not null terminated");
2020-11-10 09:03:40 +03:00
StringRef substr = data.substr(0, end + 1);
2020-11-08 08:13:59 +03:00
data = data.substr(end + 1);
StringPiece *piece = parent.map.insert(substr, StringPiece(substr));
pieces.push_back({piece, offset});
2020-11-10 09:03:40 +03:00
offset += substr.size();
2020-11-08 08:13:59 +03:00
}
static Counter counter("string_pieces");
counter.inc(pieces.size());
}
2020-11-08 06:36:08 +03:00
std::string toString(InputChunk *chunk) {
return (toString(chunk->file) + ":(" + chunk->name + ")").str();
2020-10-19 17:37:29 +03:00
}