From 540506d33cc2c44d135dd35794dbdd29266b3a3f Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sun, 10 Jan 2021 14:53:29 +0900 Subject: [PATCH] temporary --- input_sections.cc | 49 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/input_sections.cc b/input_sections.cc index aaf3c6cf..e13fc862 100644 --- a/input_sections.cc +++ b/input_sections.cc @@ -34,31 +34,31 @@ static std::string rel_to_string(u32 r_type) { unreachable(); } -static void overflow_check(std::ostringstream &out, u32 r_type, u64 val) { +static void overflow_check(std::ostringstream &out, u32 r_type, u8 *loc, u64 val) { switch (r_type) { case R_X86_64_8: if (val != (u8)val) out << "relocation R_X86_64_8 out of range: " << val << " is not in [0, 255]"; - return; + break; case R_X86_64_PC8: if (val != (i8)val) out << "relocation R_X86_64_PC8 out of range: " << (i64)val << " is not in [-128, 127]"; - return; + break; case R_X86_64_16: if (val != (u16)val) out << "relocation R_X86_64_16 out of range: " << val << " is not in [0, 65535]"; - return; + break; case R_X86_64_PC16: if (val != (i16)val) out << "relocation R_X86_64_PC16 out of range: " << (i64)val << " is not in [-32768, 32767]"; - return; + break; case R_X86_64_32: if (val != (u32)val) out << "relocation R_X86_64_32 out of range: " << val << " is not in [0, 4294967296]"; - return; + break; case R_X86_64_32S: case R_X86_64_PC32: case R_X86_64_GOT32: @@ -75,27 +75,30 @@ static void overflow_check(std::ostringstream &out, u32 r_type, u64 val) { if (val != (i32)val) out << "relocation " << rel_to_string(r_type) << " out of range: " << (i64)val << " is not in [-2147483648, 2147483647]"; - return; + break; 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: - return; + break; + default: + unreachable(); } - unreachable(); } -static int get_rel_size(u32 r_type) { +static void write_val(u32 r_type, u8 *loc, u64 val) { switch (r_type) { case R_X86_64_NONE: - return 0; + break; case R_X86_64_8: case R_X86_64_PC8: - return 1; + *loc = val; + break; case R_X86_64_16: case R_X86_64_PC16: - return 2; + *(u16 *)loc = val; + break; case R_X86_64_32: case R_X86_64_32S: case R_X86_64_PC32: @@ -110,14 +113,17 @@ static int get_rel_size(u32 r_type) { case R_X86_64_TPOFF32: case R_X86_64_DTPOFF32: case R_X86_64_GOTTPOFF: - return 4; + *(u32 *)loc = val; + break; case R_X86_64_64: case R_X86_64_PC64: case R_X86_64_TPOFF64: case R_X86_64_DTPOFF64: - return 8; + *(u64 *)loc = val; + break; + default: + unreachable(); } - unreachable(); } void InputSection::copy_buf() { @@ -148,14 +154,8 @@ void InputSection::copy_buf() { ref = &rel_pieces[ref_idx++]; auto write = [&](u64 val) { - overflow_check(file->err_out, rel.r_type, val); - switch (get_rel_size(rel.r_type)) { - case 1: *loc = val; return; - case 2: *(u16 *)loc = val; return; - case 4: *(u32 *)loc = val; return; - case 8: *(u64 *)loc = val; return; - } - unreachable(); + overflow_check(file->err_out, rel.r_type, loc, val); + write_val(rel.r_type, loc, val); }; #define S (ref ? ref->piece->get_addr() : sym.get_addr()) @@ -172,7 +172,6 @@ void InputSection::copy_buf() { write(S + A); if (sym.needs_relative_rel()) { - assert(get_rel_size(rel.r_type) == 8); memset(dynrel, 0, sizeof(*dynrel)); dynrel->r_offset = P; dynrel->r_type = R_X86_64_RELATIVE;