Skip to content

Commit

Permalink
process: unify load on NOMMU
Browse files Browse the repository at this point in the history
JIRA: RTOS-958
  • Loading branch information
badochov committed Oct 22, 2024
1 parent 84b66c2 commit 2a6dd63
Showing 1 changed file with 55 additions and 71 deletions.
126 changes: 55 additions & 71 deletions proc/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,10 @@ int process_load(process_t *process, vm_object_t *o, off_t base, size_t size, vo
Elf32_Ehdr *ehdr;
Elf32_Phdr *phdr;
Elf32_Shdr *shdr, *shstrshdr;
#ifdef __sparc__
Elf32_Rela *rela;
Elf32_Sym *sym;
ptr_t symTab;
#else
ptr_t symTab = NULL;
Elf32_Rel *rel;
#endif
unsigned prot, flags, reloffs;
int i, j, relocsz = 0, reltype, badreloc = 0, err;
void *relptr;
Expand Down Expand Up @@ -877,93 +874,80 @@ int process_load(process_t *process, vm_object_t *o, off_t base, size_t size, vo
return -ENOEXEC;
}

#ifdef __sparc__
/* find symtab */
for (i = 0, shdr = (void *)((char *)ehdr + ehdr->e_shoff); i < ehdr->e_shnum; i++, shdr++) {
if (hal_strcmp(&snameTab[shdr->sh_name], ".symtab") == 0) {
symTab = (ptr_t)ehdr + (ptr_t)shdr->sh_offset;
break;
}
}

if (i >= ehdr->e_shnum) {
return -ENOEXEC;
}
symTab = (ptr_t)ehdr + (ptr_t)shdr->sh_offset;

/* Perform data, init_array and fini_array relocation */
for (i = 0, shdr = (void *)((char *)ehdr + ehdr->e_shoff); i < ehdr->e_shnum; i++, shdr++) {
/* strncmp as there may be multiple .rela.* sections for different sections. */
if (hal_strncmp(&snameTab[shdr->sh_name], ".rela", 5) != 0) {
continue;
}

if ((shdr->sh_size == 0) || (shdr->sh_entsize == 0)) {
continue;
}

for (j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j) {
rela = (Elf32_Rela *)((ptr_t)shdr->sh_offset + (ptr_t)ehdr + (j * shdr->sh_entsize));
reltype = ELF32_R_TYPE(rela->r_info);

if (reltype == R_SPARC_32) {
relptr = (void *)rela->r_offset;
if (process_relocate(reloc, relocsz, (char **)&relptr) < 0) {
return -ENOEXEC;
}

/* Don't modify ELF file! */
if (((ptr_t)relptr >= (ptr_t)base) && ((ptr_t)relptr < ((ptr_t)base + size))) {
++badreloc;
continue;
}

sym = (Elf32_Sym *)(symTab + (ELF32_R_SYM(rela->r_info) * sizeof(Elf32_Sym)));

/* Write addend to the address */
*(char **)relptr = (char *)(sym->st_value + rela->r_addend);

if (process_relocate(reloc, relocsz, relptr) < 0) {
return -ENOEXEC;
/* strncmp as there may be multiple .rela.* or .rel.* sections for different sections. */
if (hal_strncmp(&snameTab[shdr->sh_name], ".rela.", 6) == 0) {
/* From currently supported NOMMU platforms only SPARC uses RELA */
for (j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j) {
rela = (Elf32_Rela *)((ptr_t)shdr->sh_offset + (ptr_t)ehdr + (j * shdr->sh_entsize));
reltype = ELF32_R_TYPE(rela->r_info);

if (reltype == R_SPARC_32) {
if (symTab == NULL) {
return -ENOEXEC;
}

relptr = (void *)rela->r_offset;
if (process_relocate(reloc, relocsz, (char **)&relptr) < 0) {
return -ENOEXEC;
}

/* Don't modify ELF file! */
if (((ptr_t)relptr >= (ptr_t)base) && ((ptr_t)relptr < ((ptr_t)base + size))) {
++badreloc;
continue;
}

sym = (Elf32_Sym *)(symTab + (ELF32_R_SYM(rela->r_info) * sizeof(Elf32_Sym)));

/* Write addend to the address */
*(char **)relptr = (char *)(sym->st_value + rela->r_addend);

if (process_relocate(reloc, relocsz, relptr) < 0) {
return -ENOEXEC;
}
}
}
}
}
#else
/* Perform data, init_array and fini_array relocation */
for (i = 0, shdr = (void *)((char *)ehdr + ehdr->e_shoff); i < ehdr->e_shnum; i++, shdr++) {
/* strncmp as there may be multiple .rel.* sections for different sections. */
if (hal_strncmp(&snameTab[shdr->sh_name], ".rel", 4) != 0) {
continue;
}

if ((shdr->sh_size == 0) || (shdr->sh_entsize == 0)) {
continue;
}

for (j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j) {
rel = (void *)((ptr_t)shdr->sh_offset + (ptr_t)ehdr + (j * shdr->sh_entsize));
reltype = ELF32_R_TYPE(rel->r_info);

if (reltype == R_ARM_ABS32 || reltype == R_ARM_TARGET1) {
relptr = (void *)rel->r_offset;

if (process_relocate(reloc, relocsz, (char **)&relptr) < 0) {
return -ENOEXEC;
}

/* Don't modify ELF file! */
if (((ptr_t)relptr >= (ptr_t)base) && ((ptr_t)relptr < ((ptr_t)base + size))) {
++badreloc;
continue;
}

if (process_relocate(reloc, relocsz, relptr) < 0) {
return -ENOEXEC;
else if (hal_strncmp(&snameTab[shdr->sh_name], ".rel.", 5) == 0) {
/* From currently supported NOMMU platforms only ARM uses RELA */
for (j = 0; j < shdr->sh_size / shdr->sh_entsize; ++j) {
rel = (void *)((ptr_t)shdr->sh_offset + (ptr_t)ehdr + (j * shdr->sh_entsize));
reltype = ELF32_R_TYPE(rel->r_info);

if (reltype == R_ARM_ABS32 || reltype == R_ARM_TARGET1) {
relptr = (void *)rel->r_offset;

if (process_relocate(reloc, relocsz, (char **)&relptr) < 0) {
return -ENOEXEC;
}

/* Don't modify ELF file! */
if (((ptr_t)relptr >= (ptr_t)base) && ((ptr_t)relptr < ((ptr_t)base + size))) {
++badreloc;
continue;
}

if (process_relocate(reloc, relocsz, relptr) < 0) {
return -ENOEXEC;
}
}
}
}
}
#endif

tlsNew.tls_base = NULL;
tlsNew.tdata_sz = 0;
Expand Down

0 comments on commit 2a6dd63

Please sign in to comment.