1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00
mold/input_sections.cc

546 lines
16 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
2021-01-24 15:10:19 +03:00
#include <limits>
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),
2020-12-17 15:19:29 +03:00
output_section(OutputSection::get_instance(name, shdr.sh_type, shdr.sh_flags)) {}
2020-10-14 10:27:45 +03:00
2021-01-22 11:13:52 +03:00
std::string_view InputChunk::get_contents() const {
return file->get_string(shdr);
}
2021-01-24 06:01:43 +03:00
static std::string rel_to_string(u64 r_type) {
2021-01-08 14:33:29 +03:00
switch (r_type) {
case R_X86_64_NONE: return "R_X86_64_NONE";
case R_X86_64_8: return "R_X86_64_8";
case R_X86_64_16: return "R_X86_64_16";
case R_X86_64_32: return "R_X86_64_32";
case R_X86_64_32S: return "R_X86_64_32S";
case R_X86_64_64: return "R_X86_64_64";
case R_X86_64_PC8: return "R_X86_64_PC8";
case R_X86_64_PC16: return "R_X86_64_PC16";
case R_X86_64_PC32: return "R_X86_64_PC32";
case R_X86_64_PC64: return "R_X86_64_PC64";
case R_X86_64_GOT32: return "R_X86_64_GOT32";
case R_X86_64_GOTPC32: return "R_X86_64_GOTPC32";
case R_X86_64_GOTPCREL: return "R_X86_64_GOTPCREL";
case R_X86_64_GOTPCRELX: return "R_X86_64_GOTPCRELX";
case R_X86_64_REX_GOTPCRELX: return "R_X86_64_REX_GOTPCRELX";
case R_X86_64_PLT32: return "R_X86_64_PLT32";
case R_X86_64_TLSGD: return "R_X86_64_TLSGD";
case R_X86_64_TLSLD: return "R_X86_64_TLSLD";
case R_X86_64_TPOFF32: return "R_X86_64_TPOFF32";
case R_X86_64_DTPOFF32: return "R_X86_64_DTPOFF32";
case R_X86_64_TPOFF64: return "R_X86_64_TPOFF64";
case R_X86_64_DTPOFF64: return "R_X86_64_DTPOFF64";
case R_X86_64_GOTTPOFF: return "R_X86_64_GOTTPOFF";
}
unreachable();
}
2021-01-24 06:01:43 +03:00
static void overflow_check(InputSection *sec, Symbol &sym, u64 r_type, u64 val) {
2021-01-08 14:33:29 +03:00
switch (r_type) {
case R_X86_64_8:
if (val != (u8)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< val << " is not in [0, 255]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
case R_X86_64_PC8:
if (val != (i8)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< (i64)val << " is not in [-128, 127]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
case R_X86_64_16:
if (val != (u16)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< val << " is not in [0, 65535]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
case R_X86_64_PC16:
if (val != (i16)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< (i64)val << " is not in [-32768, 32767]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
case R_X86_64_32:
if (val != (u32)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< val << " is not in [0, 4294967296]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
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:
if (val != (i32)val)
2021-01-21 13:24:37 +03:00
Error() << *sec << ": relocation " << rel_to_string(r_type)
<< " against " << sym.name << " out of range: "
<< (i64)val << " is not in [-2147483648, 2147483647]";
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
case R_X86_64_NONE:
case R_X86_64_64:
case R_X86_64_PC64:
case R_X86_64_TPOFF64:
case R_X86_64_DTPOFF64:
2021-01-10 08:54:03 +03:00
return;
2021-01-08 14:33:29 +03:00
}
2021-01-10 08:54:03 +03:00
unreachable();
2021-01-08 14:33:29 +03:00
}
2021-01-24 06:01:43 +03:00
static void write_val(u64 r_type, u8 *loc, u64 val) {
2020-12-15 16:24:08 +03:00
switch (r_type) {
case R_X86_64_NONE:
2021-01-10 08:54:03 +03:00
return;
2020-12-18 07:05:22 +03:00
case R_X86_64_8:
case R_X86_64_PC8:
2021-01-10 08:53:29 +03:00
*loc = val;
2021-01-10 08:54:03 +03:00
return;
2020-12-18 07:05:22 +03:00
case R_X86_64_16:
case R_X86_64_PC16:
2021-01-10 08:53:29 +03:00
*(u16 *)loc = val;
2021-01-10 08:54:03 +03:00
return;
2020-12-15 16:24:08 +03:00
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:
2021-01-10 08:53:29 +03:00
*(u32 *)loc = val;
2021-01-10 08:54:03 +03:00
return;
2020-12-15 16:24:08 +03:00
case R_X86_64_64:
case R_X86_64_PC64:
case R_X86_64_TPOFF64:
case R_X86_64_DTPOFF64:
2021-01-10 08:53:29 +03:00
*(u64 *)loc = val;
2021-01-10 08:54:03 +03:00
return;
2020-12-15 16:24:08 +03:00
}
2021-01-10 08:54:03 +03:00
unreachable();
2020-12-15 16:24:08 +03:00
}
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
2021-01-17 10:55:39 +03:00
u8 *base = out::buf + output_section->shdr.sh_offset + offset;
2021-01-22 11:13:52 +03:00
std::string_view contents = get_contents();
2021-01-20 10:28:18 +03:00
memcpy(base, contents.data(), contents.size());
2020-10-23 03:21:40 +03:00
2020-11-07 18:33:13 +03:00
// Apply relocations
2021-01-16 15:58:28 +03:00
if (shdr.sh_flags & SHF_ALLOC)
apply_reloc_alloc(base);
else
apply_reloc_nonalloc(base);
2021-01-16 15:52:49 +03:00
}
2021-01-23 07:16:16 +03:00
// Apply relocations to SHF_ALLOC sections (i.e. sections that are
// mapped to memory at runtime) based on the result of
// scan_relocations().
2021-01-16 15:58:28 +03:00
void InputSection::apply_reloc_alloc(u8 *base) {
2021-01-24 06:01:43 +03:00
i64 ref_idx = 0;
2020-12-16 15:16:34 +03:00
ElfRela *dynrel = nullptr;
2021-01-13 18:58:21 +03:00
2020-12-18 04:56:56 +03:00
if (out::reldyn)
2020-12-16 15:16:34 +03:00
dynrel = (ElfRela *)(out::buf + out::reldyn->shdr.sh_offset +
file->reldyn_offset + reldyn_offset);
2020-11-24 13:47:30 +03:00
2021-01-24 06:01:43 +03:00
for (i64 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-07 18:33:13 +03:00
u8 *loc = base + rel.r_offset;
2020-10-29 16:46:00 +03:00
2021-01-22 09:10:32 +03:00
const SectionFragmentRef *ref = nullptr;
if (has_rel_frag[i])
ref = &rel_fragments[ref_idx++];
2020-12-19 02:32:07 +03:00
2020-12-18 07:00:09 +03:00
auto write = [&](u64 val) {
2021-01-17 11:24:35 +03:00
overflow_check(this, sym, rel.r_type, val);
2021-01-10 08:53:29 +03:00
write_val(rel.r_type, loc, val);
2020-12-18 07:00:09 +03:00
};
2021-01-22 09:10:32 +03:00
#define S (ref ? ref->frag->get_addr() \
2021-01-12 07:18:55 +03:00
: (sym.plt_idx == -1 ? sym.get_addr() : sym.get_plt_addr()))
2020-12-19 02:32:07 +03:00
#define A (ref ? ref->addend : rel.r_addend)
2020-12-17 10:32:49 +03:00
#define P (output_section->shdr.sh_addr + offset + rel.r_offset)
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
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:
2020-12-18 07:00:09 +03:00
write(S + A);
2021-01-13 15:57:12 +03:00
break;
case R_ABS_DYN:
write(S + A);
2021-01-14 10:16:50 +03:00
*dynrel++ = {P, R_X86_64_RELATIVE, 0, (i64)(S + A)};
2020-10-20 08:27:00 +03:00
break;
2020-12-16 14:56:04 +03:00
case R_DYN:
2021-01-14 10:16:50 +03:00
*dynrel++ = {P, R_X86_64_64, sym.dynsym_idx, A};
2020-12-16 14:56:04 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_PC:
2020-12-18 07:00:09 +03:00
write(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-18 07:00:09 +03:00
write(G + A);
2020-10-29 16:54:11 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_GOTPC:
2020-12-18 07:00:09 +03:00
write(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-18 07:00:09 +03:00
write(G + GOT + A - P);
2020-10-20 08:27:00 +03:00
break;
2020-12-15 16:24:08 +03:00
case R_TLSGD:
2020-12-18 07:00:09 +03:00
write(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:
2021-01-14 13:58:21 +03:00
write(out::got->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[] = {
2021-01-09 16:30:14 +03:00
// mov %fs:0, %rax
0x66, 0x66, 0x66, 0x64, 0x48, 0x8b, 0x04, 0x25, 0, 0, 0, 0,
2020-12-15 16:24:08 +03:00
};
memcpy(loc - 3, insn, sizeof(insn));
i++;
break;
}
2021-01-14 12:23:29 +03:00
case R_DTPOFF:
write(S + A - out::tls_begin);
break;
2020-12-15 16:24:08 +03:00
case R_TPOFF:
2020-12-18 07:00:09 +03:00
write(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-12-18 07:00:09 +03:00
write(sym.get_gottpoff_addr() + A - P);
2020-11-25 09:13:06 +03:00
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 G
#undef GOT
2020-10-20 08:27:00 +03:00
}
2020-12-18 07:05:22 +03:00
}
2020-11-06 07:56:11 +03:00
2021-01-23 07:16:16 +03:00
// This function is responsible for applying relocations against
// non-SHF_ALLOC sections (i.e. sections that are not mapped to memory
// at runtime).
//
// Relocations against non-SHF_ALLOC sections are much easier to
// handle than that against SHF_ALLOC sections. It is because, since
// they are not mapped to memory, they don't contain any variable or
// function and never need PLT or GOT. Non-SHF_ALLOC sections are
// mostly debug info sections.
//
// Relocations against non-SHF_ALLOC sections are not scanned by
// scan_relocations.
2021-01-16 15:58:28 +03:00
void InputSection::apply_reloc_nonalloc(u8 *base) {
static Counter counter("reloc_nonalloc");
counter.inc(rels.size());
2021-01-24 06:01:43 +03:00
i64 ref_idx = 0;
2021-01-16 16:12:41 +03:00
2021-01-24 06:01:43 +03:00
for (i64 i = 0; i < rels.size(); i++) {
2021-01-16 15:58:28 +03:00
const ElfRela &rel = rels[i];
Symbol &sym = *file->symbols[rel.r_sym];
if (!sym.file || sym.is_placeholder) {
Error() << "undefined symbol: " << *file << ": " << sym.name;
continue;
}
2021-01-22 09:10:32 +03:00
const SectionFragmentRef *ref = nullptr;
if (has_rel_frag[i])
ref = &rel_fragments[ref_idx++];
2021-01-16 16:12:41 +03:00
2021-01-16 15:58:28 +03:00
switch (rel.r_type) {
case R_X86_64_NONE:
break;
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: {
u8 *loc = base + rel.r_offset;
2021-01-22 09:10:32 +03:00
u64 val = ref ? ref->frag->get_addr() : sym.get_addr();
2021-01-17 11:24:35 +03:00
overflow_check(this, sym, rel.r_type, val);
2021-01-16 15:58:28 +03:00
write_val(rel.r_type, loc, val);
break;
}
case R_X86_64_PC8:
case R_X86_64_PC16:
case R_X86_64_PC32:
case R_X86_64_PC64:
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_DTPOFF32:
case R_X86_64_DTPOFF64:
case R_X86_64_TPOFF32:
case R_X86_64_TPOFF64:
case R_X86_64_GOTTPOFF:
Error() << *this << ": invalid relocation for non-allocated sections: "
<< rel.r_type;
break;
default:
Error() << *this << ": unknown relocation: " << rel.r_type;
}
}
}
2021-01-23 07:16:16 +03:00
// Linker has to create data structures in an output file to apply
// some type of relocations. For example, if a relocation refers a GOT
// or a PLT entry of a symbol, linker has to create an entry in .got
// or in .plt for that symbol. In order to fix the file layout, we
// need to scan relocations.
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;
2021-01-16 15:58:28 +03:00
static Counter counter("reloc_alloc");
2020-12-18 07:05:22 +03:00
counter.inc(rels.size());
2021-01-16 16:12:41 +03:00
this->reldyn_offset = file->num_dynrel * sizeof(ElfRela);
this->rel_types.resize(rels.size());
2021-01-24 06:01:43 +03:00
for (i64 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];
2021-01-14 05:52:44 +03:00
bool is_readonly = !(shdr.sh_flags & SHF_WRITE);
2021-01-15 10:32:33 +03:00
bool is_code = (sym.st_type == STT_FUNC);
2020-11-26 12:09:32 +03:00
if (!sym.file || sym.is_placeholder) {
2021-01-13 17:52:11 +03:00
Error() << "undefined symbol: " << *file << ": " << sym.name;
2020-11-26 12:09:32 +03:00
continue;
}
2020-11-06 03:59:08 +03:00
2021-01-14 05:52:44 +03:00
auto report_error = [&]() {
Error() << *this << ": " << rel_to_string(rel.r_type)
<< " relocation against symbol `" << sym.name
<< "' can not be used; recompile with -fPIE";
2021-01-14 04:01:54 +03:00
};
2021-01-13 14:09:20 +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;
2020-12-18 07:05:22 +03:00
case R_X86_64_8:
case R_X86_64_16:
case R_X86_64_32:
case R_X86_64_32S:
2021-01-14 05:52:44 +03:00
if (config.pie && sym.is_relative())
report_error();
2021-01-13 15:57:54 +03:00
if (sym.is_imported)
2021-01-13 15:25:45 +03:00
sym.flags |= is_code ? NEEDS_PLT : NEEDS_COPYREL;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_ABS;
2020-12-15 16:24:08 +03:00
break;
2020-12-16 14:56:04 +03:00
case R_X86_64_64:
2021-01-13 15:53:47 +03:00
if (config.pie) {
if (sym.is_imported) {
2021-01-14 05:52:44 +03:00
if (is_readonly)
report_error();
2021-01-13 15:53:47 +03:00
sym.flags |= NEEDS_DYNSYM;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_DYN;
2021-01-13 15:53:47 +03:00
file->num_dynrel++;
2021-01-13 15:57:12 +03:00
} else if (sym.is_relative()) {
2021-01-14 05:52:44 +03:00
if (is_readonly)
report_error();
2021-01-13 15:57:12 +03:00
rel_types[i] = R_ABS_DYN;
file->num_dynrel++;
2021-01-13 15:53:47 +03:00
} else {
rel_types[i] = R_ABS;
}
2020-12-16 14:56:04 +03:00
} else {
2021-01-13 15:53:47 +03:00
if (sym.is_imported)
sym.flags |= is_code ? NEEDS_PLT : NEEDS_COPYREL;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_ABS;
2020-12-16 14:56:04 +03:00
}
break;
2020-12-18 07:05:22 +03:00
case R_X86_64_PC8:
case R_X86_64_PC16:
case R_X86_64_PC32:
2020-12-16 12:38:13 +03:00
case R_X86_64_PC64:
2021-01-13 14:09:20 +03:00
if (sym.is_imported)
sym.flags |= is_code ? NEEDS_PLT : NEEDS_COPYREL;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_PC;
break;
2020-12-16 12:38:13 +03:00
case R_X86_64_GOT32:
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_GOT;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_GOT;
2020-12-15 16:24:08 +03:00
break;
2020-12-16 12:38:13 +03:00
case R_X86_64_GOTPC32:
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_GOT;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_GOTPC;
2020-12-15 16:24:08 +03:00
break;
case R_X86_64_GOTPCREL:
case R_X86_64_GOTPCRELX:
2020-12-16 12:38:13 +03:00
case R_X86_64_REX_GOTPCRELX:
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_GOT;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_GOTPCREL;
break;
case R_X86_64_PLT32:
2021-01-13 18:22:32 +03:00
if (sym.is_imported || sym.st_type == STT_GNU_IFUNC)
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_PLT;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_PC;
break;
case R_X86_64_TLSGD:
2021-01-14 04:01:54 +03:00
if (i + 1 == rels.size() || rels[i + 1].r_type != R_X86_64_PLT32)
2021-01-13 17:52:11 +03:00
Error() << *this << ": TLSGD reloc not followed by PLT32";
2020-12-16 12:38:13 +03:00
2021-01-14 12:23:29 +03:00
if (config.relax && !sym.is_imported) {
rel_types[i] = R_TLSGD_RELAX_LE;
i++;
} else {
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_TLSGD;
2021-01-14 07:19:21 +03:00
sym.flags |= NEEDS_DYNSYM;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_TLSGD;
2020-12-13 14:18:49 +03:00
}
break;
case R_X86_64_TLSLD:
2021-01-14 04:01:54 +03:00
if (i + 1 == rels.size() || rels[i + 1].r_type != R_X86_64_PLT32)
2021-01-13 17:52:11 +03:00
Error() << *this << ": TLSLD reloc not followed by PLT32";
2021-01-14 12:20:56 +03:00
if (sym.is_imported)
Error() << *this << ": TLSLD reloc refers external symbol " << sym.name;
2020-12-16 12:38:13 +03:00
2021-01-14 12:20:56 +03:00
if (config.relax) {
rel_types[i] = R_TLSLD_RELAX_LE;
i++;
} else {
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_TLSLD;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_TLSLD;
2020-12-13 14:18:49 +03:00
}
break;
2020-11-25 11:20:48 +03:00
case R_X86_64_DTPOFF32:
2020-11-30 12:43:04 +03:00
case R_X86_64_DTPOFF64:
2021-01-14 12:23:29 +03:00
if (sym.is_imported)
Error() << *this << ": DTPOFF reloc refers external symbol " << sym.name;
rel_types[i] = config.relax ? R_TPOFF : R_DTPOFF;
break;
case R_X86_64_TPOFF32:
case R_X86_64_TPOFF64:
2020-12-15 16:24:08 +03:00
rel_types[i] = R_TPOFF;
2020-11-25 11:20:48 +03:00
break;
2020-12-16 12:38:13 +03:00
case R_X86_64_GOTTPOFF:
2020-12-17 02:05:53 +03:00
sym.flags |= NEEDS_GOTTPOFF;
2021-01-14 04:01:54 +03:00
rel_types[i] = R_GOTTPOFF;
break;
default:
2021-01-13 17:52:11 +03:00
Error() << *this << ": unknown relocation: " << rel.r_type;
}
2020-11-06 03:59:08 +03:00
}
}
static size_t find_null(std::string_view data, u64 entsize) {
if (entsize == 1)
return data.find('\0');
for (i64 i = 0; i <= data.size() - entsize; i += entsize)
if (data.substr(i, i + entsize).find_first_not_of('\0') ==
std::string_view::npos)
return i;
return std::string_view::npos;
}
2021-01-23 07:16:16 +03:00
// Mergeable sections (sections with SHF_MERGE bit) typically contain
// string literals. Linker is expected to split the section contents
// into null-terminated strings, merge them with mergeable strings
// from other object files, and emit uniquified strings to an output
// file.
//
// This mechanism reduces the size of an output file. If two source
// files happen to contain the same string literal, the output will
// contain only a single copy of it.
//
// It is less common than string literals, but mergeable sections can
// contain fixed-sized read-only records too.
//
// This function splits the section contents into small pieces that we
// call "section fragments". Section fragment is a unit of merging.
//
// We do not support mergeable sections that have relocations.
2021-01-22 11:13:52 +03:00
MergeableSection::MergeableSection(InputSection *isec)
2020-11-29 11:47:51 +03:00
: InputChunk(isec->file, isec->shdr, isec->name),
2020-12-17 15:19:29 +03:00
parent(*MergedSection::get_instance(isec->name, isec->shdr.sh_type,
isec->shdr.sh_flags)) {
2021-01-22 11:13:52 +03:00
std::string_view data = isec->get_contents();
2021-01-22 13:47:11 +03:00
const char *begin = data.data();
u64 entsize = isec->shdr.sh_entsize;
2020-11-08 08:13:59 +03:00
2021-01-24 15:10:19 +03:00
static_assert(sizeof(SectionFragment::alignment) == 2);
if (isec->shdr.sh_addralign >= (1 << 16))
Fatal() << *isec << ": alignment too large";
2021-01-22 13:47:11 +03:00
if (isec->shdr.sh_flags & SHF_STRINGS) {
while (!data.empty()) {
size_t end = find_null(data, entsize);
2021-01-22 13:47:11 +03:00
if (end == std::string_view::npos)
Error() << *this << ": string is not null terminated";
2020-11-08 08:13:59 +03:00
std::string_view substr = data.substr(0, end + entsize);
data = data.substr(end + entsize);
2021-01-22 13:47:11 +03:00
SectionFragment *frag = parent.insert(substr, isec->shdr.sh_addralign);
fragments.push_back(frag);
frag_offsets.push_back(substr.data() - begin);
}
} else {
if (data.size() % entsize)
Fatal() << *isec << ": section size is not multiple of sh_entsize";
while (!data.empty()) {
std::string_view substr = data.substr(0, entsize);
data = data.substr(entsize);
SectionFragment *frag = parent.insert(substr, isec->shdr.sh_addralign);
fragments.push_back(frag);
frag_offsets.push_back(substr.data() - begin);
}
2020-11-08 08:13:59 +03:00
}
2021-01-22 09:10:32 +03:00
static Counter counter("string_fragments");
counter.inc(fragments.size());
2020-11-08 08:13:59 +03:00
}