Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Mar 19, 2021
1 parent bbb0192 commit 79a973d
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions input_sections.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,15 @@ void InputSection::apply_reloc_nonalloc(u8 *base) {
}
}

static int get_sym_type(Symbol &sym) {
static i64 get_output_type() {
if (config.shared)
return 0;
if (config.pie)
return 1;
return 2;
}

static i64 get_sym_type(Symbol &sym) {
if (sym.is_absolute())
return 0;
if (!sym.is_imported)
Expand All @@ -502,7 +510,6 @@ void InputSection::scan_relocations() {

this->reldyn_offset = file.num_dynrel * sizeof(ElfRela);
bool is_readonly = !(shdr.sh_flags & SHF_WRITE);
i64 output_type = config.shared ? 2 : (config.pie ? 1 : 0);

// Scan relocations
for (i64 i = 0; i < rels.size(); i++) {
Expand Down Expand Up @@ -572,49 +579,49 @@ void InputSection::scan_relocations() {
// error if we cannot relocate them even at load-time.
Action table[][4] = {
// Absolute Local Imported data Imported code
{ NONE, NONE, COPYREL, PLT }, // PDE
{ NONE, ERROR, ERROR, ERROR }, // PIE
{ NONE, ERROR, ERROR, ERROR }, // DSO
{ NONE, ERROR, ERROR, ERROR }, // PIE
{ NONE, NONE, COPYREL, PLT }, // PDE
};

dispatch(table[output_type][get_sym_type(sym)], R_ABS);
dispatch(table[get_output_type()][get_sym_type(sym)], R_ABS);
break;
}
case R_X86_64_64: {
// Unlike the above, we can use R_X86_64_RELATIVE and R_86_64_64
// relocations.
Action table[][4] = {
// Absolute Local Imported data Imported code
{ NONE, NONE, COPYREL, PLT }, // PDE
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
{ NONE, BASEREL, DYNREL, DYNREL }, // DSO
{ NONE, BASEREL, DYNREL, DYNREL }, // PIE
{ NONE, NONE, COPYREL, PLT }, // PDE
};

dispatch(table[output_type][get_sym_type(sym)], R_ABS);
dispatch(table[get_output_type()][get_sym_type(sym)], R_ABS);
break;
}
case R_X86_64_PC8:
case R_X86_64_PC16:
case R_X86_64_PC32: {
Action table[][4] = {
// Absolute Local Imported data Imported code
{ NONE, NONE, COPYREL, PLT }, // PDE
{ ERROR, NONE, COPYREL, PLT }, // PIE
{ ERROR, NONE, ERROR, ERROR }, // DSO
{ ERROR, NONE, COPYREL, PLT }, // PIE
{ NONE, NONE, COPYREL, PLT }, // PDE
};

dispatch(table[output_type][get_sym_type(sym)], R_PC);
dispatch(table[get_output_type()][get_sym_type(sym)], R_PC);
break;
}
case R_X86_64_PC64: {
Action table[][4] = {
// Absolute Local Imported data Imported code
{ NONE, NONE, COPYREL, PLT }, // PDE
{ BASEREL, NONE, COPYREL, PLT }, // PIE
{ BASEREL, NONE, ERROR, ERROR }, // DSO
{ BASEREL, NONE, COPYREL, PLT }, // PIE
{ NONE, NONE, COPYREL, PLT }, // PDE
};

dispatch(table[output_type][get_sym_type(sym)], R_PC);
dispatch(table[get_output_type()][get_sym_type(sym)], R_PC);
break;
}
case R_X86_64_GOT32:
Expand Down

0 comments on commit 79a973d

Please sign in to comment.