2021-03-30 16:55:58 +03:00
|
|
|
#include "mold.h"
|
|
|
|
|
2021-09-02 12:19:43 +03:00
|
|
|
namespace mold::elf {
|
|
|
|
|
2021-08-19 12:43:59 +03:00
|
|
|
template <>
|
|
|
|
void GotPltSection<I386>::copy_buf(Context<I386> &ctx) {
|
|
|
|
u32 *buf = (u32 *)(ctx.buf + this->shdr.sh_offset);
|
|
|
|
|
|
|
|
// The first slot of .got.plt points to _DYNAMIC.
|
|
|
|
buf[0] = ctx.dynamic ? ctx.dynamic->shdr.sh_addr : 0;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = 0;
|
|
|
|
|
|
|
|
for (Symbol<I386> *sym : ctx.plt->symbols)
|
|
|
|
buf[sym->get_gotplt_idx(ctx)] = sym->get_plt_addr(ctx) + 6;
|
|
|
|
}
|
|
|
|
|
2021-08-30 12:00:09 +03:00
|
|
|
static void write_plt_header(Context<I386> &ctx, u8 *buf) {
|
2021-03-31 06:15:03 +03:00
|
|
|
if (ctx.arg.pic) {
|
|
|
|
static const u8 plt0[] = {
|
|
|
|
0xff, 0xb3, 0x04, 0, 0, 0, // pushl 4(%ebx)
|
|
|
|
0xff, 0xa3, 0x08, 0, 0, 0, // jmp *8(%ebx)
|
|
|
|
0x90, 0x90, 0x90, 0x90, // nop
|
|
|
|
};
|
|
|
|
memcpy(buf, plt0, sizeof(plt0));
|
|
|
|
} else {
|
|
|
|
static const u8 plt0[] = {
|
|
|
|
0xff, 0x35, 0, 0, 0, 0, // pushl (GOTPLT+4)
|
|
|
|
0xff, 0x25, 0, 0, 0, 0, // jmp *(GOTPLT+8)
|
|
|
|
0x90, 0x90, 0x90, 0x90, // nop
|
|
|
|
};
|
|
|
|
memcpy(buf, plt0, sizeof(plt0));
|
2021-03-31 06:45:10 +03:00
|
|
|
*(u32 *)(buf + 2) = ctx.gotplt->shdr.sh_addr + 4;
|
|
|
|
*(u32 *)(buf + 8) = ctx.gotplt->shdr.sh_addr + 8;
|
2021-03-31 06:15:03 +03:00
|
|
|
}
|
2021-08-30 12:00:09 +03:00
|
|
|
}
|
2021-03-31 06:15:03 +03:00
|
|
|
|
2021-08-30 12:00:09 +03:00
|
|
|
static void write_plt_entry(Context<I386> &ctx, u8 *buf, Symbol<I386> &sym,
|
|
|
|
i64 idx) {
|
|
|
|
u8 *ent = buf + sym.get_plt_idx(ctx) * I386::plt_size;
|
|
|
|
|
|
|
|
if (ctx.arg.pic) {
|
|
|
|
static const u8 data[] = {
|
|
|
|
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
|
|
|
0x68, 0, 0, 0, 0, // pushl $reloc_offset
|
|
|
|
0xe9, 0, 0, 0, 0, // jmp .PLT0@PC
|
|
|
|
};
|
|
|
|
memcpy(ent, data, sizeof(data));
|
|
|
|
*(u32 *)(ent + 2) = sym.get_gotplt_addr(ctx) - ctx.gotplt->shdr.sh_addr;
|
|
|
|
} else {
|
|
|
|
static const u8 data[] = {
|
|
|
|
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
|
|
|
0x68, 0, 0, 0, 0, // pushl $reloc_offset
|
|
|
|
0xe9, 0, 0, 0, 0, // jmp .PLT0@PC
|
|
|
|
};
|
|
|
|
memcpy(ent, data, sizeof(data));
|
|
|
|
*(u32 *)(ent + 2) = sym.get_gotplt_addr(ctx);
|
|
|
|
}
|
2021-03-31 06:15:03 +03:00
|
|
|
|
2021-08-30 12:00:09 +03:00
|
|
|
*(u32 *)(ent + 7) = idx * sizeof(ElfRel<I386>);
|
|
|
|
*(u32 *)(ent + 12) = ctx.plt->shdr.sh_addr - sym.get_plt_addr(ctx) - 16;
|
|
|
|
}
|
2021-03-31 06:15:03 +03:00
|
|
|
|
2021-08-30 12:00:09 +03:00
|
|
|
template <>
|
|
|
|
void PltSection<I386>::copy_buf(Context<I386> &ctx) {
|
|
|
|
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
|
|
|
write_plt_header(ctx, buf);
|
2021-03-31 06:15:03 +03:00
|
|
|
|
2021-08-30 12:00:09 +03:00
|
|
|
for (i64 i = 0; i < symbols.size(); i++)
|
|
|
|
write_plt_entry(ctx, buf, *symbols[i], i);
|
2021-03-31 04:12:16 +03:00
|
|
|
}
|
|
|
|
|
2021-04-01 11:46:34 +03:00
|
|
|
template <>
|
|
|
|
void PltGotSection<I386>::copy_buf(Context<I386> &ctx) {
|
|
|
|
u8 *buf = ctx.buf + this->shdr.sh_offset;
|
|
|
|
|
|
|
|
if (ctx.arg.pic) {
|
|
|
|
static const u8 data[] = {
|
|
|
|
0xff, 0xa3, 0, 0, 0, 0, // jmp *foo@GOT(%ebx)
|
|
|
|
0x66, 0x90, // nop
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i64 i = 0; i < symbols.size(); i++) {
|
|
|
|
u8 *ent = buf + i * sizeof(data);
|
|
|
|
memcpy(ent, data, sizeof(data));
|
2021-06-07 09:24:03 +03:00
|
|
|
*(u32 *)(ent + 2) = symbols[i]->get_got_addr(ctx) - ctx.gotplt->shdr.sh_addr;
|
2021-04-01 11:46:34 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
static const u8 data[] = {
|
|
|
|
0xff, 0x25, 0, 0, 0, 0, // jmp *foo@GOT
|
|
|
|
0x66, 0x90, // nop
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i64 i = 0; i < symbols.size(); i++) {
|
|
|
|
u8 *ent = buf + i * sizeof(data);
|
|
|
|
memcpy(ent, data, sizeof(data));
|
|
|
|
*(u32 *)(ent + 2) = symbols[i]->get_got_addr(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 15:17:09 +03:00
|
|
|
template <>
|
|
|
|
void EhFrameSection<I386>::apply_reloc(Context<I386> &ctx,
|
2021-04-15 15:48:30 +03:00
|
|
|
ElfRel<I386> &rel,
|
2021-04-03 15:17:09 +03:00
|
|
|
u64 loc, u64 val) {
|
|
|
|
u8 *base = ctx.buf + this->shdr.sh_offset;
|
|
|
|
|
2021-04-15 15:48:30 +03:00
|
|
|
switch (rel.r_type) {
|
2021-06-08 13:39:22 +03:00
|
|
|
case R_386_NONE:
|
|
|
|
return;
|
2021-04-03 15:17:09 +03:00
|
|
|
case R_386_32:
|
|
|
|
*(u32 *)(base + loc) = val;
|
|
|
|
return;
|
|
|
|
case R_386_PC32:
|
|
|
|
*(u32 *)(base + loc) = val - this->shdr.sh_addr - loc;
|
|
|
|
return;
|
|
|
|
}
|
2021-09-30 18:35:42 +03:00
|
|
|
unreachable();
|
2021-04-03 15:17:09 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:55:58 +03:00
|
|
|
template <>
|
|
|
|
void InputSection<I386>::apply_reloc_alloc(Context<I386> &ctx, u8 *base) {
|
2021-03-30 17:47:01 +03:00
|
|
|
ElfRel<I386> *dynrel = nullptr;
|
2021-04-01 19:35:10 +03:00
|
|
|
std::span<ElfRel<I386>> rels = get_rels(ctx);
|
2021-12-07 08:44:04 +03:00
|
|
|
i64 frag_idx = 0;
|
2021-03-30 17:47:01 +03:00
|
|
|
|
|
|
|
if (ctx.reldyn)
|
|
|
|
dynrel = (ElfRel<I386> *)(ctx.buf + ctx.reldyn->shdr.sh_offset +
|
|
|
|
file.reldyn_offset + this->reldyn_offset);
|
|
|
|
|
|
|
|
for (i64 i = 0; i < rels.size(); i++) {
|
2021-07-06 06:27:40 +03:00
|
|
|
const ElfRel<I386> &rel = rels[i];
|
|
|
|
if (rel.r_type == R_386_NONE)
|
2021-07-04 15:01:39 +03:00
|
|
|
continue;
|
|
|
|
|
2021-03-30 17:47:01 +03:00
|
|
|
Symbol<I386> &sym = *file.symbols[rel.r_sym];
|
|
|
|
u8 *loc = base + rel.r_offset;
|
|
|
|
|
2021-12-07 08:44:04 +03:00
|
|
|
const SectionFragmentRef<I386> *ref = nullptr;
|
|
|
|
if (rel_fragments && rel_fragments[frag_idx].idx == i)
|
|
|
|
ref = &rel_fragments[frag_idx++];
|
2021-03-30 17:47:01 +03:00
|
|
|
|
2021-08-29 18:05:07 +03:00
|
|
|
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
2021-08-30 11:32:08 +03:00
|
|
|
if (val < lo || hi <= val)
|
2021-08-30 12:07:54 +03:00
|
|
|
Error(ctx) << *this << ": relocation " << rel << " against "
|
2021-08-30 11:32:08 +03:00
|
|
|
<< sym << " out of range: " << val << " is not in ["
|
|
|
|
<< lo << ", " << hi << ")";
|
2021-08-29 18:05:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto write8 = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, 0, 1 << 8);
|
2021-08-29 18:05:07 +03:00
|
|
|
*loc = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto write8s = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, -(1 << 7), 1 << 7);
|
2021-08-29 18:05:07 +03:00
|
|
|
*loc = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto write16 = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, 0, 1 << 16);
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u16 *)loc = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto write16s = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, -(1 << 15), 1 << 15);
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u16 *)loc = val;
|
2021-03-30 17:47:01 +03:00
|
|
|
};
|
|
|
|
|
2022-01-02 09:39:14 +03:00
|
|
|
auto write32 = [&](u64 val) {
|
|
|
|
*(u32 *)loc = val;
|
|
|
|
};
|
|
|
|
|
2021-12-07 08:44:04 +03:00
|
|
|
#define S (ref ? ref->frag->get_addr(ctx) : sym.get_addr(ctx))
|
2021-07-26 10:52:41 +03:00
|
|
|
#define A (ref ? ref->addend : this->get_addend(rel))
|
2021-06-07 09:24:03 +03:00
|
|
|
#define P (output_section->shdr.sh_addr + offset + rel.r_offset)
|
|
|
|
#define G (sym.get_got_addr(ctx) - ctx.got->shdr.sh_addr)
|
|
|
|
#define GOTPLT ctx.gotplt->shdr.sh_addr
|
2021-03-30 17:47:01 +03:00
|
|
|
|
2021-09-28 07:03:18 +03:00
|
|
|
if (needs_dynrel[i]) {
|
2021-04-01 06:14:27 +03:00
|
|
|
*dynrel++ = {P, R_386_32, (u32)sym.get_dynsym_idx(ctx)};
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(A);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-28 07:03:18 +03:00
|
|
|
if (needs_baserel[i]) {
|
2021-09-27 17:01:51 +03:00
|
|
|
*dynrel++ = {P, R_386_RELATIVE, 0};
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A);
|
2021-09-27 17:01:51 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-06 06:27:40 +03:00
|
|
|
switch (rel.r_type) {
|
|
|
|
case R_386_8:
|
2021-08-29 18:05:07 +03:00
|
|
|
write8(S + A);
|
|
|
|
continue;
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_16:
|
2021-08-29 18:05:07 +03:00
|
|
|
write16(S + A);
|
|
|
|
continue;
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_32:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_PC8:
|
2021-08-29 18:05:07 +03:00
|
|
|
write8s(S + A);
|
|
|
|
continue;
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_PC16:
|
2021-08-29 18:05:07 +03:00
|
|
|
write16s(S + A);
|
|
|
|
continue;
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_PC32:
|
|
|
|
case R_386_PLT32:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A - P);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_GOT32X:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.get_got_addr(ctx) + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_GOTOFF:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_GOTPC:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(GOTPLT + A - P);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_GOTIE:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.get_gottp_addr(ctx) + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_LE:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A - ctx.tls_end);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_IE:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.get_gottp_addr(ctx) + A);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_GD:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.get_tlsgd_addr(ctx) + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_LDM:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(ctx.got->get_tlsld_addr(ctx) + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_LDO_32:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A - ctx.tls_begin);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_SIZE32:
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.esym().st_size + A);
|
2021-07-06 06:27:40 +03:00
|
|
|
continue;
|
|
|
|
case R_386_TLS_GOTDESC:
|
|
|
|
if (sym.get_tlsdesc_idx(ctx) == -1) {
|
|
|
|
static const u8 insn[] = {
|
|
|
|
0x8d, 0x05, 0, 0, 0, 0, // lea 0, %eax
|
|
|
|
};
|
|
|
|
memcpy(loc - 2, insn, sizeof(insn));
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(S + A - ctx.tls_end);
|
2021-07-06 06:27:40 +03:00
|
|
|
} else {
|
2022-01-02 09:39:14 +03:00
|
|
|
write32(sym.get_tlsdesc_addr(ctx) + A - GOTPLT);
|
2021-07-06 06:27:40 +03:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case R_386_TLS_DESC_CALL:
|
|
|
|
if (ctx.arg.relax && !ctx.arg.shared) {
|
|
|
|
// call *(%rax) -> nop
|
|
|
|
loc[0] = 0x66;
|
|
|
|
loc[1] = 0x90;
|
|
|
|
}
|
|
|
|
continue;
|
2021-03-30 17:47:01 +03:00
|
|
|
default:
|
2021-09-30 18:35:42 +03:00
|
|
|
unreachable();
|
2021-03-30 17:47:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef S
|
|
|
|
#undef A
|
|
|
|
#undef P
|
|
|
|
#undef G
|
2021-06-07 09:24:03 +03:00
|
|
|
#undef GOTPLT
|
2021-03-30 17:47:01 +03:00
|
|
|
}
|
2021-03-30 16:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void InputSection<I386>::apply_reloc_nonalloc(Context<I386> &ctx, u8 *base) {
|
2021-04-01 19:35:10 +03:00
|
|
|
std::span<ElfRel<I386>> rels = get_rels(ctx);
|
2021-12-07 08:44:04 +03:00
|
|
|
i64 frag_idx = 0;
|
2021-03-30 17:30:14 +03:00
|
|
|
|
|
|
|
for (i64 i = 0; i < rels.size(); i++) {
|
|
|
|
const ElfRel<I386> &rel = rels[i];
|
2021-06-29 15:27:34 +03:00
|
|
|
if (rel.r_type == R_386_NONE)
|
|
|
|
continue;
|
|
|
|
|
2021-03-30 17:30:14 +03:00
|
|
|
Symbol<I386> &sym = *file.symbols[rel.r_sym];
|
|
|
|
u8 *loc = base + rel.r_offset;
|
|
|
|
|
|
|
|
if (!sym.file) {
|
2021-06-13 14:45:02 +03:00
|
|
|
report_undef(ctx, sym);
|
2021-03-30 17:30:14 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-07 08:44:04 +03:00
|
|
|
const SectionFragmentRef<I386> *ref = nullptr;
|
|
|
|
if (rel_fragments && rel_fragments[frag_idx].idx == i)
|
|
|
|
ref = &rel_fragments[frag_idx++];
|
2021-03-30 17:30:14 +03:00
|
|
|
|
2021-08-29 18:05:07 +03:00
|
|
|
auto overflow_check = [&](i64 val, i64 lo, i64 hi) {
|
2021-08-30 11:32:08 +03:00
|
|
|
if (val < lo || hi <= val)
|
2021-08-30 12:07:54 +03:00
|
|
|
Error(ctx) << *this << ": relocation " << rel << " against "
|
2021-08-30 11:32:08 +03:00
|
|
|
<< sym << " out of range: " << val << " is not in ["
|
|
|
|
<< lo << ", " << hi << ")";
|
2021-08-29 18:05:07 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
auto write8 = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, 0, 1 << 8);
|
2021-08-29 18:05:07 +03:00
|
|
|
*loc = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto write8s = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, -(1 << 7), 1 << 7);
|
2021-08-29 18:05:07 +03:00
|
|
|
*loc = val;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto write16 = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, 0, 1 << 16);
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u16 *)loc = val;
|
2021-03-30 17:30:14 +03:00
|
|
|
};
|
|
|
|
|
2021-08-29 18:05:07 +03:00
|
|
|
auto write16s = [&](u64 val) {
|
2021-08-30 11:32:08 +03:00
|
|
|
overflow_check(val, -(1 << 15), 1 << 15);
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u16 *)loc = val;
|
|
|
|
};
|
|
|
|
|
2021-12-07 08:44:04 +03:00
|
|
|
#define S (ref ? ref->frag->get_addr(ctx) : sym.get_addr(ctx))
|
2021-08-29 18:05:07 +03:00
|
|
|
#define A (ref ? ref->addend : this->get_addend(rel))
|
|
|
|
#define G (sym.get_got_addr(ctx) - ctx.got->shdr.sh_addr)
|
|
|
|
#define GOTPLT ctx.gotplt->shdr.sh_addr
|
|
|
|
|
2021-03-30 17:30:14 +03:00
|
|
|
switch (rel.r_type) {
|
|
|
|
case R_386_8:
|
2021-08-29 18:05:07 +03:00
|
|
|
write8(S + A);
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_16:
|
2021-08-29 18:05:07 +03:00
|
|
|
write16(S + A);
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_32:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = S + A;
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_PC8:
|
2021-08-29 18:05:07 +03:00
|
|
|
write8s(S + A);
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_PC16:
|
2021-08-29 18:05:07 +03:00
|
|
|
write16s(S + A);
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_PC32:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = S + A;
|
|
|
|
continue;
|
2021-06-20 12:51:36 +03:00
|
|
|
case R_386_GOTPC:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = GOTPLT + A;
|
|
|
|
continue;
|
2021-04-13 15:06:21 +03:00
|
|
|
case R_386_GOTOFF:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = S + A - GOTPLT;
|
|
|
|
continue;
|
2021-04-13 15:06:21 +03:00
|
|
|
case R_386_TLS_LDO_32:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = S + A - ctx.tls_begin;
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
case R_386_SIZE32:
|
2021-08-29 18:05:07 +03:00
|
|
|
*(u32 *)loc = sym.esym().st_size + A;
|
|
|
|
continue;
|
2021-03-30 17:30:14 +03:00
|
|
|
default:
|
2021-09-30 18:35:42 +03:00
|
|
|
unreachable();
|
2021-03-30 17:30:14 +03:00
|
|
|
}
|
2021-08-29 18:05:07 +03:00
|
|
|
|
|
|
|
#undef S
|
|
|
|
#undef A
|
|
|
|
#undef GOTPLT
|
2021-03-30 17:30:14 +03:00
|
|
|
}
|
2021-03-30 16:55:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void InputSection<I386>::scan_relocations(Context<I386> &ctx) {
|
2021-09-08 13:51:02 +03:00
|
|
|
assert(shdr.sh_flags & SHF_ALLOC);
|
2021-06-12 09:02:11 +03:00
|
|
|
|
2021-03-30 17:17:08 +03:00
|
|
|
this->reldyn_offset = file.num_dynrel * sizeof(ElfRel<I386>);
|
2021-04-01 19:35:10 +03:00
|
|
|
std::span<ElfRel<I386>> rels = get_rels(ctx);
|
2021-03-30 17:17:08 +03:00
|
|
|
|
|
|
|
// Scan relocations
|
|
|
|
for (i64 i = 0; i < rels.size(); i++) {
|
|
|
|
const ElfRel<I386> &rel = rels[i];
|
2021-07-06 06:27:40 +03:00
|
|
|
if (rel.r_type == R_386_NONE)
|
2021-07-05 07:59:56 +03:00
|
|
|
continue;
|
2021-06-08 09:39:23 +03:00
|
|
|
|
2021-03-30 17:17:08 +03:00
|
|
|
Symbol<I386> &sym = *file.symbols[rel.r_sym];
|
|
|
|
|
|
|
|
if (!sym.file) {
|
2021-06-13 14:45:02 +03:00
|
|
|
report_undef(ctx, sym);
|
2021-03-30 17:17:08 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-04-01 11:11:22 +03:00
|
|
|
if (sym.get_type() == STT_GNU_IFUNC) {
|
|
|
|
sym.flags |= NEEDS_GOT;
|
2021-03-30 17:17:08 +03:00
|
|
|
sym.flags |= NEEDS_PLT;
|
2021-04-01 11:11:22 +03:00
|
|
|
}
|
2021-03-30 17:17:08 +03:00
|
|
|
|
|
|
|
switch (rel.r_type) {
|
|
|
|
case R_386_8:
|
|
|
|
case R_386_16: {
|
|
|
|
Action table[][4] = {
|
|
|
|
// Absolute Local Imported data Imported code
|
|
|
|
{ NONE, ERROR, ERROR, ERROR }, // DSO
|
|
|
|
{ NONE, ERROR, ERROR, ERROR }, // PIE
|
|
|
|
{ NONE, NONE, COPYREL, PLT }, // PDE
|
|
|
|
};
|
2021-09-25 15:28:28 +03:00
|
|
|
dispatch(ctx, table, i, rel, sym);
|
2021-03-30 17:17:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_32: {
|
|
|
|
Action table[][4] = {
|
|
|
|
// Absolute Local Imported data Imported code
|
|
|
|
{ NONE, BASEREL, DYNREL, DYNREL }, // DSO
|
|
|
|
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
|
2021-09-25 12:10:26 +03:00
|
|
|
{ NONE, NONE, COPYREL, PLT }, // PDE
|
2021-03-30 17:17:08 +03:00
|
|
|
};
|
2021-09-25 15:28:28 +03:00
|
|
|
dispatch(ctx, table, i, rel, sym);
|
2021-03-30 17:17:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_PC8:
|
|
|
|
case R_386_PC16: {
|
|
|
|
Action table[][4] = {
|
|
|
|
// Absolute Local Imported data Imported code
|
|
|
|
{ ERROR, NONE, ERROR, ERROR }, // DSO
|
|
|
|
{ ERROR, NONE, COPYREL, PLT }, // PIE
|
|
|
|
{ NONE, NONE, COPYREL, PLT }, // PDE
|
|
|
|
};
|
2021-09-25 15:28:28 +03:00
|
|
|
dispatch(ctx, table, i, rel, sym);
|
2021-03-30 17:17:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_PC32: {
|
|
|
|
Action table[][4] = {
|
|
|
|
// Absolute Local Imported data Imported code
|
|
|
|
{ BASEREL, NONE, ERROR, ERROR }, // DSO
|
|
|
|
{ BASEREL, NONE, COPYREL, PLT }, // PIE
|
|
|
|
{ NONE, NONE, COPYREL, PLT }, // PDE
|
|
|
|
};
|
2021-09-25 15:28:28 +03:00
|
|
|
dispatch(ctx, table, i, rel, sym);
|
2021-03-30 17:17:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case R_386_GOT32:
|
|
|
|
case R_386_GOT32X:
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_GOTPC:
|
2021-03-30 17:17:08 +03:00
|
|
|
sym.flags |= NEEDS_GOT;
|
|
|
|
break;
|
|
|
|
case R_386_PLT32:
|
|
|
|
if (sym.is_imported)
|
|
|
|
sym.flags |= NEEDS_PLT;
|
|
|
|
break;
|
|
|
|
case R_386_TLS_GOTIE:
|
|
|
|
case R_386_TLS_LE:
|
2021-07-03 08:14:44 +03:00
|
|
|
case R_386_TLS_IE:
|
|
|
|
sym.flags |= NEEDS_GOTTP;
|
|
|
|
break;
|
2021-03-30 17:17:08 +03:00
|
|
|
case R_386_TLS_GD:
|
2021-04-13 15:06:21 +03:00
|
|
|
sym.flags |= NEEDS_TLSGD;
|
|
|
|
break;
|
2021-03-30 17:17:08 +03:00
|
|
|
case R_386_TLS_LDM:
|
2021-04-13 15:06:21 +03:00
|
|
|
sym.flags |= NEEDS_TLSLD;
|
2021-03-30 17:17:08 +03:00
|
|
|
break;
|
|
|
|
case R_386_TLS_GOTDESC:
|
2021-07-06 06:27:40 +03:00
|
|
|
if (!ctx.arg.relax || ctx.arg.shared)
|
2021-04-14 10:31:41 +03:00
|
|
|
sym.flags |= NEEDS_TLSDESC;
|
2021-04-14 08:58:32 +03:00
|
|
|
break;
|
2021-07-06 06:27:40 +03:00
|
|
|
case R_386_GOTOFF:
|
|
|
|
case R_386_TLS_LDO_32:
|
|
|
|
case R_386_SIZE32:
|
2021-03-30 17:17:08 +03:00
|
|
|
case R_386_TLS_DESC_CALL:
|
2021-04-13 17:37:42 +03:00
|
|
|
break;
|
|
|
|
default:
|
2021-08-30 12:07:54 +03:00
|
|
|
Error(ctx) << *this << ": unknown relocation: " << rel;
|
2021-03-30 17:17:08 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-30 16:55:58 +03:00
|
|
|
}
|
2021-09-02 12:19:43 +03:00
|
|
|
|
|
|
|
} // namespace mold::elf
|