Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Userspace runtime loader support for sub-library c18n #2267

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 53 additions & 33 deletions libexec/rtld-elf/aarch64/reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ arch_digest_note(struct Struct_Obj_Entry *obj __unused, const Elf_Note *note)
}

void
init_pltgot(Obj_Entry *obj)
init_pltgot(Plt_Entry *plt)
{

if (obj->pltgot != NULL) {
if (plt->pltgot != NULL) {
#ifdef CHERI_LIB_C18N
if (C18N_ENABLED)
obj->pltgot[1] = (uintptr_t)cheri_seal(obj,
plt->pltgot[1] = (uintptr_t)cheri_seal(plt,
sealer_pltgot);
else
#endif
obj->pltgot[1] = (uintptr_t)obj;
plt->pltgot[1] = (uintptr_t)plt;
#ifdef CHERI_LIB_C18N
if (C18N_ENABLED)
obj->pltgot[2] = (uintptr_t)&_rtld_bind_start_c18n;
plt->pltgot[2] = (uintptr_t)&_rtld_bind_start_c18n;
else
#endif
obj->pltgot[2] = (uintptr_t)&_rtld_bind_start;
plt->pltgot[2] = (uintptr_t)&_rtld_bind_start;
}
}

Expand Down Expand Up @@ -406,8 +406,9 @@ reloc_tlsdesc(const Obj_Entry *obj, const Elf_Rela *rela,
* Process the PLT relocations.
*/
int
reloc_plt(Obj_Entry *obj, int flags, RtldLockState *lockstate)
reloc_plt(Plt_Entry *plt, int flags, RtldLockState *lockstate)
{
Obj_Entry *obj = plt->obj;
const Obj_Entry *defobj;
const Elf_Rela *relalim;
const Elf_Rela *rela;
Expand All @@ -417,13 +418,12 @@ reloc_plt(Obj_Entry *obj, int flags, RtldLockState *lockstate)
#endif
bool lazy;

relalim = (const Elf_Rela *)((const char *)obj->pltrela +
obj->pltrelasize);
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
#ifdef __CHERI_PURE_CAPABILITY__
jump_slot_base = (uintptr_t)cheri_clearperm(obj->text_rodata_cap,
FUNC_PTR_REMOVE_PERMS);
#endif
for (rela = obj->pltrela; rela < relalim; rela++) {
for (rela = plt->rela; rela < relalim; rela++) {
uintptr_t *where, target;
#ifdef __CHERI_PURE_CAPABILITY__
Elf_Addr *fragment;
Expand Down Expand Up @@ -541,19 +541,19 @@ reloc_plt(Obj_Entry *obj, int flags, RtldLockState *lockstate)
* LD_BIND_NOW was set - force relocation for all jump slots
*/
int
reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
reloc_jmpslots(Plt_Entry *plt, int flags, RtldLockState *lockstate)
{
Obj_Entry *obj = plt->obj;
const Obj_Entry *defobj;
const Elf_Rela *relalim;
const Elf_Rela *rela;
const Elf_Sym *def;

if (obj->jmpslots_done)
if (plt->jmpslots_done)
return (0);

relalim = (const Elf_Rela *)((const char *)obj->pltrela +
obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
uintptr_t *where, target;

where = (uintptr_t *)(obj->relocbase + rela->r_offset);
Expand Down Expand Up @@ -585,7 +585,7 @@ reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
break;
}
}
obj->jmpslots_done = true;
plt->jmpslots_done = true;

return (0);
}
Expand Down Expand Up @@ -647,28 +647,36 @@ reloc_iresolve_one(Obj_Entry *obj, const Elf_Rela *rela,
*where = target;
}

int
reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
static void
reloc_iresolve_plt(Plt_Entry *plt, struct Struct_RtldLockState *lockstate)
{
const Elf_Rela *relalim;
const Elf_Rela *rela;

if (!obj->irelative)
return (0);
obj->irelative = false;
relalim = (const Elf_Rela *)((const char *)obj->pltrela +
obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
switch (ELF_R_TYPE(rela->r_info)) {
#ifdef __CHERI_PURE_CAPABILITY__
case R_MORELLO_IRELATIVE:
#else
case R_AARCH64_IRELATIVE:
#endif
reloc_iresolve_one(obj, rela, lockstate);
reloc_iresolve_one(plt->obj, rela, lockstate);
break;
}
}
}

int
reloc_iresolve(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
{
unsigned long i;

if (!obj->irelative)
return (0);
obj->irelative = false;
for (i = 0; i < obj->nplts; i++)
reloc_iresolve_plt(&obj->plts[i], lockstate);
return (0);
}

Expand Down Expand Up @@ -696,20 +704,18 @@ reloc_iresolve_nonplt(Obj_Entry *obj, struct Struct_RtldLockState *lockstate)
return (0);
}

int
reloc_gnu_ifunc(Obj_Entry *obj, int flags,
struct Struct_RtldLockState *lockstate)
static bool
reloc_gnu_ifunc_plt(Plt_Entry *plt, int flags, RtldLockState *lockstate)
{
Obj_Entry *obj = plt->obj;
const Elf_Rela *relalim;
const Elf_Rela *rela;
uintptr_t *where, target;
const Elf_Sym *def;
const Obj_Entry *defobj;

if (!obj->gnu_ifunc)
return (0);
relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
where = (uintptr_t *)(obj->relocbase + rela->r_offset);
switch (ELF_R_TYPE(rela->r_info)) {
#ifdef __CHERI_PURE_CAPABILITY__
Expand All @@ -720,7 +726,7 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags,
def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
SYMLOOK_IN_PLT | flags, NULL, lockstate);
if (def == NULL)
return (-1);
return (false);
if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
continue;
lock_release(rtld_bind_lock, lockstate);
Expand All @@ -738,6 +744,20 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags,
(const Elf_Rel *)rela);
}
}
return (true);
}

int
reloc_gnu_ifunc(Obj_Entry *obj, int flags,
struct Struct_RtldLockState *lockstate)
{
unsigned long i;

if (!obj->gnu_ifunc)
return (0);
for (i = 0; i < obj->nplts; i++)
if (!reloc_gnu_ifunc_plt(&obj->plts[i], flags, lockstate))
return (-1);
obj->gnu_ifunc = false;
return (0);
}
Expand Down
2 changes: 2 additions & 0 deletions libexec/rtld-elf/aarch64/rtld_machdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

struct Struct_Obj_Entry;

#define MD_PLT_ENTRY

#define MD_OBJ_ENTRY \
bool variant_pcs : 1; /* Object has a variant pcs function */

Expand Down
2 changes: 1 addition & 1 deletion libexec/rtld-elf/aarch64/rtld_start.S
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ ENTRY(C18N_SYM(_rtld_bind_start))
#endif
add x1, x1, x3 /* reloff = x3 + offset = (3 or 1.5) * offset */

/* Load obj */
/* Load plt */
ldr PTR(0), [PTR(16), #-PTR_WIDTH]

/* Call into rtld */
Expand Down
81 changes: 52 additions & 29 deletions libexec/rtld-elf/amd64/reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ do_copy_relocations(Obj_Entry *dstobj)

/* Initialize the special GOT entries. */
void
init_pltgot(Obj_Entry *obj)
init_pltgot(Plt_Entry *plt)
{
if (obj->pltgot != NULL) {
obj->pltgot[1] = (Elf_Addr) obj;
obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
if (plt->pltgot != NULL) {
plt->pltgot[1] = (Elf_Addr) plt;
plt->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
}
}

Expand Down Expand Up @@ -326,13 +326,14 @@ reloc_non_plt(Obj_Entry *obj, Obj_Entry *obj_rtld, int flags,

/* Process the PLT relocations. */
int
reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)
reloc_plt(Plt_Entry *plt, int flags __unused, RtldLockState *lockstate __unused)
{
Obj_Entry *obj = plt->obj;
const Elf_Rela *relalim;
const Elf_Rela *rela;

relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
Elf_Addr *where;

switch(ELF_R_TYPE(rela->r_info)) {
Expand All @@ -357,15 +358,16 @@ reloc_plt(Obj_Entry *obj, int flags __unused, RtldLockState *lockstate __unused)

/* Relocate the jump slots in an object. */
int
reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
reloc_jmpslots(Plt_Entry *plt, int flags, RtldLockState *lockstate)
{
Obj_Entry *obj = plt->obj;
const Elf_Rela *relalim;
const Elf_Rela *rela;

if (obj->jmpslots_done)
if (plt->jmpslots_done)
return 0;
relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
Elf_Addr *where, target;
const Elf_Sym *def;
const Obj_Entry *defobj;
Expand Down Expand Up @@ -394,7 +396,7 @@ reloc_jmpslots(Obj_Entry *obj, int flags, RtldLockState *lockstate)
return (-1);
}
}
obj->jmpslots_done = true;
plt->jmpslots_done = true;
return 0;
}

Expand Down Expand Up @@ -427,21 +429,30 @@ reloc_iresolve_one(Obj_Entry *obj, const Elf_Rela *rela,
*where = target;
}

int
reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
static void
reloc_iresolve_plt(Plt_Entry *plt, RtldLockState *lockstate)
{
const Elf_Rela *relalim;
const Elf_Rela *rela;

relalim = (const Elf_Rela *)((const char *)plt->rela +
plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
if (ELF_R_TYPE(rela->r_info) == R_X86_64_IRELATIVE)
reloc_iresolve_one(plt->obj, rela, lockstate);
}
}

int
reloc_iresolve(Obj_Entry *obj, RtldLockState *lockstate)
{
unsigned long i;

if (!obj->irelative)
return (0);
obj->irelative = false;
relalim = (const Elf_Rela *)((const char *)obj->pltrela +
obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
if (ELF_R_TYPE(rela->r_info) == R_X86_64_IRELATIVE)
reloc_iresolve_one(obj, rela, lockstate);
}
for (i = 0; i < obj->nplts; i++)
reloc_iresolve_plt(&obj->plts[i], lockstate);
return (0);
}

Expand All @@ -462,16 +473,15 @@ reloc_iresolve_nonplt(Obj_Entry *obj, RtldLockState *lockstate)
return (0);
}

int
reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
static bool
reloc_gnu_ifunc_plt(Plt_Entry *plt, int flags, RtldLockState *lockstate)
{
Obj_Entry *obj = plt->obj;
const Elf_Rela *relalim;
const Elf_Rela *rela;

if (!obj->gnu_ifunc)
return (0);
relalim = (const Elf_Rela *)((const char *)obj->pltrela + obj->pltrelasize);
for (rela = obj->pltrela; rela < relalim; rela++) {
relalim = (const Elf_Rela *)((const char *)plt->rela + plt->relasize);
for (rela = plt->rela; rela < relalim; rela++) {
Elf_Addr *where, target;
const Elf_Sym *def;
const Obj_Entry *defobj;
Expand All @@ -482,7 +492,7 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
def = find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj,
SYMLOOK_IN_PLT | flags, NULL, lockstate);
if (def == NULL)
return (-1);
return (false);
if (ELF_ST_TYPE(def->st_info) != STT_GNU_IFUNC)
continue;
lock_release(rtld_bind_lock, lockstate);
Expand All @@ -492,8 +502,21 @@ reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
break;
}
}
obj->gnu_ifunc = false;
return (0);
return (true);
}

int
reloc_gnu_ifunc(Obj_Entry *obj, int flags, RtldLockState *lockstate)
{
unsigned long i;

if (!obj->gnu_ifunc)
return (0);
for (i = 0; i < obj->nplts; i++)
if (!reloc_gnu_ifunc_plt(&obj->plts[i], flags, lockstate))
return (-1);
obj->gnu_ifunc = false;
return (0);
}

uint32_t cpu_feature, cpu_feature2, cpu_stdext_feature, cpu_stdext_feature2;
Expand Down
2 changes: 2 additions & 0 deletions libexec/rtld-elf/amd64/rtld_machdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

struct Struct_Obj_Entry;

#define MD_PLT_ENTRY

#define MD_OBJ_ENTRY

/* Return the address of the .dynamic section in the dynamic linker. */
Expand Down
Loading