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

305 lines
7.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-12-10 09:59:24 +03:00
InputChunk::InputChunk(ObjectFile *file, const ElfShdr &shdr,
2020-12-10 07:44:58 +03:00
std::string_view name)
2020-11-29 11:47:51 +03:00
: file(file), shdr(shdr), name(name),
output_section(OutputSection::get_instance(name, shdr.sh_flags, shdr.sh_type)) {}
2020-10-14 10:27:45 +03:00
2020-12-15 16:24:08 +03:00
static int get_rel_size(u32 r_type) {
switch (r_type) {
case R_X86_64_NONE:
return 0;
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_PC32:
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:
case R_X86_64_PLT32:
case R_X86_64_TLSGD:
case R_X86_64_TLSLD:
case R_X86_64_TPOFF32:
case R_X86_64_DTPOFF32:
case R_X86_64_GOTTPOFF:
return 4;
case R_X86_64_64:
case R_X86_64_PC64:
case R_X86_64_TPOFF64:
case R_X86_64_DTPOFF64:
return 8;
}
unreachable();
}
2020-11-17 07:56:40 +03:00
void InputSection::copy_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-12-12 06:59:54 +03:00
std::string_view view = file->get_string(shdr);
memcpy(out::buf + output_section->shdr.sh_offset + offset, view.data(), view.size());
2020-10-23 03:21:40 +03:00
2020-11-07 18:33:13 +03:00
// Apply relocations
2020-11-17 07:56:40 +03:00
u8 *base = out::buf + output_section->shdr.sh_offset + offset;
2020-11-07 18:33:13 +03:00
u64 sh_addr = output_section->shdr.sh_addr + offset;
2020-11-24 13:47:30 +03:00
2020-11-07 14:05:51 +03:00
for (int i = 0; i < rels.size(); i++) {
2020-12-10 13:54:01 +03:00
const ElfRela &rel = rels[i];
2020-11-07 14:05:51 +03:00
StringPieceRef &ref = rel_pieces[i];
2020-12-10 13:54:01 +03:00
Symbol &sym = *file->symbols[rel.r_sym];
2020-11-07 18:33:13 +03:00
u8 *loc = base + rel.r_offset;
2020-10-29 16:46:00 +03:00
2020-11-20 13:27:39 +03:00
if (!sym.file)
continue;
2020-11-26 07:34:42 +03:00
#define S (ref.piece ? ref.piece->get_addr() : sym.get_addr())
#define A (ref.piece ? ref.addend : rel.r_addend)
#define P (sh_addr + rel.r_offset)
#define L sym.get_plt_addr()
2020-12-13 11:21:03 +03:00
#define G (sym.get_got_addr() - out::got->shdr.sh_addr)
2020-11-26 07:34:42 +03:00
#define GOT out::got->shdr.sh_addr
2020-10-29 16:46:00 +03:00
2020-12-15 16:24:08 +03:00
int sz = get_rel_size(rel.r_type);
switch (rel_types[i]) {
case R_NONE:
2020-12-14 14:24:46 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_ABS:
if (sz == 4)
*(u32 *)loc = S + A;
else
*(u64 *)loc = S + A;
2020-10-20 08:27:00 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_PC:
if (sz == 4)
*(u32 *)loc = S + A - P;
else
*(u64 *)loc = S + A - P;
2020-12-14 14:24:46 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_GOT:
2020-12-15 15:23:15 +03:00
*(u32 *)loc = G + A;
2020-10-29 16:54:11 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_GOTPC:
2020-12-14 14:24:46 +03:00
*(u32 *)loc = GOT + A - P;
2020-10-30 05:40:38 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_GOTPCREL:
2020-12-13 11:21:03 +03:00
*(u32 *)loc = G + GOT + A - P;
2020-10-20 08:27:00 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_PLT:
if (sz == 4)
2020-12-14 14:24:46 +03:00
*(u32 *)loc = L + A - P;
else
2020-12-15 16:24:08 +03:00
*(u64 *)loc = L + A - P;
2020-10-27 09:21:09 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_TLSGD:
*(u32 *)loc = sym.get_tlsgd_addr() + A - P;
2020-11-21 05:51:21 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_TLSGD_RELAX_LE: {
// Relax GD to LE
static const u8 insn[] = {
0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // mov %fs:0, %rax
0x48, 0x8d, 0x80, 0, 0, 0, 0, // lea x@tpoff, %rax
};
memcpy(loc - 4, insn, sizeof(insn));
*(u32 *)(loc + 8) = S - out::tls_end + A + 4;
i++;
2020-11-02 09:56:41 +03:00
break;
2020-12-15 16:24:08 +03:00
}
case R_TLSLD:
*(u32 *)loc = sym.get_tlsld_addr() + A - P;
2020-10-29 14:16:20 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_TLSLD_RELAX_LE: {
// Relax LD to LE
static const u8 insn[] = {
0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0, // mov %fs:0, %rax
};
memcpy(loc - 3, insn, sizeof(insn));
i++;
break;
}
case R_TPOFF:
if (sz == 4)
*(u32 *)loc = S + A - out::tls_end;
else
*(u64 *)loc = S + A - out::tls_end;
2020-11-30 12:43:04 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_GOTTPOFF:
2020-11-25 09:13:06 +03:00
*(u32 *)loc = sym.get_gottpoff_addr() + A - P;
break;
2020-10-20 08:27:00 +03:00
default:
2020-12-15 16:24:08 +03:00
unreachable();
2020-10-20 08:27:00 +03:00
}
2020-11-26 07:34:42 +03:00
#undef S
#undef A
#undef P
#undef L
#undef G
#undef GOT
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-25 07:28:29 +03:00
for (int i = 0; i < rels.size(); i++) {
2020-12-10 13:54:01 +03:00
const ElfRela &rel = rels[i];
Symbol &sym = *file->symbols[rel.r_sym];
2020-11-26 12:09:32 +03:00
if (!sym.file || sym.is_placeholder) {
file->has_error = true;
continue;
}
2020-11-06 03:59:08 +03:00
2020-12-10 13:54:01 +03:00
switch (rel.r_type) {
2020-11-17 14:22:52 +03:00
case R_X86_64_NONE:
2020-12-15 16:24:08 +03:00
rel_types[i] = R_NONE;
2020-11-25 11:20:48 +03:00
break;
case R_X86_64_32:
case R_X86_64_32S:
case R_X86_64_64:
2020-12-15 16:24:08 +03:00
if (sym.is_imported) {
std::lock_guard lock(sym.mu);
if (sym.type == STT_OBJECT) {
sym.needs_copyrel = true;
rel_types[i] = R_ABS;
} else {
sym.needs_plt = true;
rel_types[i] = R_PLT;
}
} else {
rel_types[i] = R_ABS;
}
break;
case R_X86_64_PC32:
case R_X86_64_PC64:
2020-11-26 07:34:42 +03:00
if (sym.is_imported) {
2020-12-13 14:18:49 +03:00
std::lock_guard lock(sym.mu);
2020-12-15 16:24:08 +03:00
if (sym.type == STT_OBJECT) {
2020-12-13 14:18:49 +03:00
sym.needs_copyrel = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_PC;
} else {
2020-12-13 14:18:49 +03:00
sym.needs_plt = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_PLT;
}
} else {
rel_types[i] = R_PC;
2020-11-26 07:34:42 +03:00
}
break;
2020-12-15 16:24:08 +03:00
case R_X86_64_GOT32:{
std::lock_guard lock(sym.mu);
sym.needs_got = true;
rel_types[i] = R_GOT;
break;
}
case R_X86_64_GOTPC32: {
std::lock_guard lock(sym.mu);
sym.needs_got = true;
rel_types[i] = R_GOTPC;
break;
}
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
2020-12-13 14:18:49 +03:00
case R_X86_64_REX_GOTPCRELX: {
std::lock_guard lock(sym.mu);
sym.needs_got = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_GOTPCREL;
break;
2020-12-13 14:18:49 +03:00
}
case R_X86_64_PLT32:
2020-12-13 14:18:49 +03:00
if (sym.is_imported || sym.type == STT_GNU_IFUNC) {
std::lock_guard lock(sym.mu);
sym.needs_plt = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_PLT;
} else {
rel_types[i] = R_PC;
2020-12-13 14:18:49 +03:00
}
break;
case R_X86_64_TLSGD:
2020-12-14 04:19:37 +03:00
if (rels[i + 1].r_type != R_X86_64_PLT32)
error(to_string(this) + ": TLSGD reloc not followed by PLT32");
2020-12-13 14:18:49 +03:00
if (sym.is_imported) {
std::lock_guard lock(sym.mu);
sym.needs_tlsgd = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TLSGD;
2020-12-13 14:18:49 +03:00
} else {
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TLSGD_RELAX_LE;
2020-11-25 07:28:29 +03:00
i++;
2020-12-13 14:18:49 +03:00
}
break;
case R_X86_64_TLSLD:
2020-12-14 04:19:37 +03:00
if (rels[i + 1].r_type != R_X86_64_PLT32)
error(to_string(this) + ": TLSLD reloc not followed by PLT32");
2020-12-13 14:18:49 +03:00
if (sym.is_imported) {
std::lock_guard lock(sym.mu);
sym.needs_tlsld = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TLSLD;
2020-12-13 14:18:49 +03:00
} else {
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TLSLD_RELAX_LE;
2020-11-25 08:06:13 +03:00
i++;
2020-12-13 14:18:49 +03:00
}
break;
2020-11-25 11:20:48 +03:00
case R_X86_64_TPOFF32:
case R_X86_64_DTPOFF32:
2020-12-15 16:24:08 +03:00
case R_X86_64_TPOFF64:
2020-11-30 12:43:04 +03:00
case R_X86_64_DTPOFF64:
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TPOFF;
2020-11-25 11:20:48 +03:00
break;
2020-12-13 14:18:49 +03:00
case R_X86_64_GOTTPOFF: {
std::lock_guard lock(sym.mu);
sym.needs_gottpoff = true;
2020-12-15 16:24:08 +03:00
rel_types[i] = R_GOTTPOFF;
break;
2020-12-13 14:18:49 +03:00
}
default:
2020-12-11 03:15:08 +03:00
error(to_string(this) + ": unknown relocation: " + std::to_string(rel.r_type));
}
2020-11-06 03:59:08 +03:00
}
}
2020-11-26 12:09:32 +03:00
void InputSection::report_undefined_symbols() {
if (!(shdr.sh_flags & SHF_ALLOC))
return;
2020-12-10 13:54:01 +03:00
for (const ElfRela &rel : rels) {
Symbol &sym = *file->symbols[rel.r_sym];
2020-11-26 12:09:32 +03:00
if (!sym.file || sym.is_placeholder)
2020-12-11 03:15:08 +03:00
std::cerr << "undefined symbol: " << to_string(file)
2020-12-10 16:33:28 +03:00
<< ": " << sym.name << "\n";
2020-11-26 12:09:32 +03:00
}
}
2020-12-10 14:11:45 +03:00
MergeableSection::MergeableSection(InputSection *isec, std::string_view data)
2020-11-29 11:47:51 +03:00
: InputChunk(isec->file, isec->shdr, isec->name),
2020-11-08 08:13:59 +03:00
parent(*MergedSection::get_instance(isec->name, isec->shdr.sh_flags,
isec->shdr.sh_type)) {
u32 offset = 0;
while (!data.empty()) {
size_t end = data.find('\0');
2020-12-10 07:44:58 +03:00
if (end == std::string_view::npos)
2020-12-11 03:15:08 +03:00
error(to_string(this) + ": string is not null terminated");
2020-11-08 08:13:59 +03:00
2020-12-10 07:44:58 +03:00
std::string_view 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-12-11 03:15:08 +03:00
std::string to_string(InputChunk *chunk) {
return to_string(chunk->file) + ":(" + std::string(chunk->name) + ")";
2020-10-19 17:37:29 +03:00
}