void init_program(void) { /* Get the value of load-base and use it to determine the correct loader to use */ ucell addr; feval("load-base"); addr = POP(); #ifdef CONFIG_LOADER_AOUT if (is_aout((struct exec *)cell2pointer(addr))) { aout_init_program(); return; } #endif #ifdef CONFIG_LOADER_BOOTCODE if (is_bootcode((char *)cell2pointer(addr))) { bootcode_init_program(); return; } #endif #ifdef CONFIG_LOADER_BOOTINFO if (is_bootinfo((char *)cell2pointer(addr))) { bootinfo_init_program(); return; } #endif #ifdef CONFIG_LOADER_ELF if (is_elf((Elf_ehdr *)cell2pointer(addr))) { elf_init_program(); return; } #endif #ifdef CONFIG_LOADER_FCODE if (is_fcode((unsigned char *)cell2pointer(addr))) { fcode_init_program(); return; } #endif #ifdef CONFIG_LOADER_FORTH if (is_forth((char *)cell2pointer(addr))) { forth_init_program(); return; } #endif #ifdef CONFIG_LOADER_XCOFF if (is_xcoff((COFF_filehdr_t *)cell2pointer(addr))) { xcoff_init_program(); return; } #endif }
void *load_elf(const char *filename) { Elf32_Ehdr *elf32_hdr; elf32_hdr = load_in_mem(filename); if(!is_elf(elf32_hdr)){ log_E("Invalid ELF"); //return NULL; } log_I("Valid ELF"); if(!is_elf_supported(elf32_hdr)){ log_E("Unsupported ELF"); //return NULL; } log_I("Supported ELF"); elf_load_rel(elf32_hdr); free(elf32_hdr); return NULL; }
uintptr_t load_elf32(paddr32_t addr) { Elf32_Ehdr hdr; Elf32_Phdr *phdr; Elf32_Phdr *phdr_start; int i; if((phdr_start = is_elf(&hdr, addr)) == NULL) { return(~0L); } if(!(shdr->flags1 & STARTUP_HDR_FLAGS1_VIRTUAL)) { phdr = phdr_start; for(i = 0; i < hdr.e_phnum; ++i, ++phdr) { switch(phdr->p_type) { case PT_LOAD: if(MAKE_1TO1_PTR(phdr->p_paddr) != (void *)phdr->p_vaddr) { alloc_ram(phdr->p_vaddr - shdr->paddr_bias, phdr->p_memsz, 1); memmove((void *)phdr->p_vaddr, MAKE_1TO1_PTR(addr + phdr->p_offset), phdr->p_filesz); memset((void *)(phdr->p_vaddr+phdr->p_filesz), 0, phdr->p_memsz - phdr->p_filesz); } break; } } #ifdef __X86__ } else if(load_elf32mmu_4m(addr, &hdr, phdr_start)) { // Nothing to do #endif } else { load_elf32mmu(addr, &hdr, phdr_start); } startup_memory_unmap(phdr_start); return hdr.e_entry; }
int find_elf(Elf_ehdr *ehdr) { int offset; for (offset = 0; offset < MAX_HEADERS * BS; offset += BS) { if ((size_t)read_io(fd, ehdr, sizeof ehdr) != sizeof ehdr) { debug("Can't read ELF header\n"); return 0; } if (is_elf(ehdr)) { debug("Found ELF header at offset %d\n", offset); return offset; } seek_io(fd, offset); } debug("Not a bootable ELF image\n"); return 0; }
static int domonnoise(struct monst *mtmp) { const char *pline_msg = 0, /* Monnam(mtmp) will be prepended */ *verbl_msg = 0; /* verbalize() */ const struct permonst *ptr = mtmp->data; /* presumably nearness checks have already been made */ if (!canhear()) return 0; if (is_silent(ptr)) return 0; /* Make sure its your role's quest quardian; adjust if not */ if (ptr->msound == MS_GUARDIAN && ptr != &pm_guardian) { int mndx = monsndx(ptr); ptr = &mons[genus(mndx, 1)]; } /* be sure to do this before talking; the monster might teleport away, in which case we want to check its pre-teleport position */ if (!canspotmon(mtmp)) map_invisible(mtmp->mx, mtmp->my); switch (ptr->msound) { case MS_ORACLE: return doconsult(mtmp); case MS_PRIEST: priest_talk(mtmp); break; case MS_LEADER: case MS_NEMESIS: case MS_GUARDIAN: quest_chat(mtmp); break; case MS_SELL: /* pitch, pay, total */ shk_chat(mtmp); break; case MS_VAMPIRE: { /* vampire messages are varied by tameness, peacefulness, and time of night */ boolean isnight = night(); boolean kindred = (Upolyd && (u.umonnum == PM_VAMPIRE || u.umonnum == PM_VAMPIRE_LORD)); boolean nightchild = (Upolyd && (u.umonnum == PM_WOLF || u.umonnum == PM_WINTER_WOLF || u.umonnum == PM_WINTER_WOLF_CUB)); const char *racenoun = (u.ufemale && urace.individual.f) ? urace. individual.f : (urace.individual.m) ? urace.individual. m : urace.noun; if (mtmp->mtame) { if (kindred) verbl_msg = msgprintf("Good %s to you Master%s", isnight ? "evening" : "day", isnight ? "!" : ". Why do we not rest?"); else verbl_msg = msgcat( nightchild ? "Child of the night, " : "", midnight()? "I can stand this craving no longer!" : isnight ? "I beg you, help me satisfy this growing craving!" : "I find myself growing a little weary."); } else if (mtmp->mpeaceful) { if (kindred && isnight) verbl_msg = msgprintf("Good feeding %s!", u.ufemale ? "sister" : "brother"); else if (nightchild && isnight) verbl_msg = "How nice to hear you, child of the night!"; else verbl_msg = "I only drink... potions."; } else { int vampindex; static const char *const vampmsg[] = { /* These first two (0 and 1) are specially handled below */ "I vant to suck your %s!", "I vill come after %s without regret!", /* other famous vampire quotes can follow here if desired */ }; if (kindred) verbl_msg = "This is my hunting ground that you dare to prowl!"; else if (youmonst.data == &mons[PM_SILVER_DRAGON] || youmonst.data == &mons[PM_BABY_SILVER_DRAGON]) { /* Silver dragons are silver in color, not made of silver */ verbl_msg = msgprintf( "%s! Your silver sheen does not frighten me!", youmonst.data == &mons[PM_SILVER_DRAGON] ? "Fool" : "Young Fool"); } else { vampindex = rn2(SIZE(vampmsg)); if (vampindex == 0) { verbl_msg = msgprintf( vampmsg[vampindex], body_part(BLOOD)); } else if (vampindex == 1) { verbl_msg = msgprintf( vampmsg[vampindex], Upolyd ? an(mons[u.umonnum].mname) : an(racenoun)); } else verbl_msg = vampmsg[vampindex]; } } } break; case MS_WERE: if (flags.moonphase == FULL_MOON && (night() ^ !rn2(13))) { pline("%s throws back %s head and lets out a blood curdling %s!", Monnam(mtmp), mhis(mtmp), ptr == &mons[PM_HUMAN_WERERAT] ? "shriek" : "howl"); wake_nearto(mtmp->mx, mtmp->my, 11 * 11); } else pline_msg = "whispers inaudibly. All you can make out is \"moon\"."; break; case MS_BARK: if (flags.moonphase == FULL_MOON && night()) { pline_msg = "howls."; } else if (mtmp->mpeaceful) { if (mtmp->mtame && (mtmp->mconf || mtmp->mflee || mtmp->mtrapped || moves > EDOG(mtmp)->hungrytime || mtmp->mtame < 5)) pline_msg = "whines."; else if (mtmp->mtame && EDOG(mtmp)->hungrytime > moves + 1000) pline_msg = "yips."; else { if (mtmp->data == &mons[PM_FOX]) pline_msg = whatthefoxsays(); else if (mtmp->data != &mons[PM_DINGO]) /* dingos do not actually bark */ pline_msg = "barks."; } } else { if (mtmp->data == &mons[PM_FOX]) pline_msg = whatthefoxsays(); else pline_msg = "growls."; } break; case MS_MEW: if (mtmp->mtame) { if (mtmp->mconf || mtmp->mflee || mtmp->mtrapped || mtmp->mtame < 5) pline_msg = "yowls."; else if (moves > EDOG(mtmp)->hungrytime) pline_msg = "meows."; else if (EDOG(mtmp)->hungrytime > moves + 1000) pline_msg = "purrs."; else pline_msg = "mews."; break; } /* else FALLTHRU */ case MS_GROWL: pline_msg = mtmp->mpeaceful ? "snarls." : "growls!"; break; case MS_ROAR: pline_msg = mtmp->mpeaceful ? "snarls." : "roars!"; break; case MS_SQEEK: pline_msg = "squeaks."; break; case MS_SQAWK: if (ptr == &mons[PM_RAVEN] && !mtmp->mpeaceful) verbl_msg = "Nevermore!"; else pline_msg = "squawks."; break; case MS_HISS: if (!mtmp->mpeaceful) pline_msg = "hisses!"; else return 0; /* no sound */ break; case MS_BUZZ: pline_msg = mtmp->mpeaceful ? "drones." : "buzzes angrily."; break; case MS_GRUNT: pline_msg = "grunts."; break; case MS_NEIGH: if (mtmp->mtame < 5) pline_msg = "neighs."; else if (moves > EDOG(mtmp)->hungrytime) pline_msg = "whinnies."; else pline_msg = "whickers."; break; case MS_WAIL: pline_msg = "wails mournfully."; break; case MS_GURGLE: pline_msg = "gurgles."; break; case MS_BURBLE: pline_msg = "burbles."; break; case MS_SHRIEK: pline_msg = "shrieks."; aggravate(); break; case MS_IMITATE: pline_msg = "imitates you."; break; case MS_BONES: pline("%s rattles noisily.", Monnam(mtmp)); pline("You freeze for a moment."); helpless(2, hr_afraid, "scared by rattling", NULL); break; case MS_LAUGH: { static const char *const laugh_msg[4] = { "giggles.", "chuckles.", "snickers.", "laughs.", }; pline_msg = laugh_msg[rn2(4)]; } break; case MS_MUMBLE: pline_msg = "mumbles incomprehensibly."; break; case MS_WISHGIVER: if (mtmp->mtame) { verbl_msg = "Sorry, I'm all out of wishes."; } else if (mtmp->mpeaceful) { if (ptr == &mons[PM_WATER_DEMON]) pline_msg = "gurgles."; else verbl_msg = "I'm free!"; } else verbl_msg = "This will teach you not to disturb me!"; break; case MS_BOAST: /* giants */ if (!mtmp->mpeaceful) { switch (rn2(4)) { case 0: pline("%s boasts about %s gem collection.", Monnam(mtmp), mhis(mtmp)); break; case 1: pline_msg = "complains about a diet of mutton."; break; default: pline_msg = "shouts \"Fee Fie Foe Foo!\" and guffaws."; wake_nearto(mtmp->mx, mtmp->my, 7 * 7); break; } break; } /* else FALLTHRU */ case MS_HUMANOID: if (!mtmp->mpeaceful) { if (In_endgame(&u.uz) && is_mplayer(ptr)) { mplayer_talk(mtmp); break; } else return 0; /* no sound */ } /* Generic peaceful humanoid behaviour. */ if (mtmp->mflee) pline_msg = "wants nothing to do with you."; else if (mtmp->mhp < mtmp->mhpmax / 4) pline_msg = "moans."; else if (mtmp->mconf || mtmp->mstun) verbl_msg = !rn2(3) ? "Huh?" : rn2(2) ? "What?" : "Eh?"; else if (!mtmp->mcansee) verbl_msg = "I can't see!"; else if (mtmp->mtrapped) { struct trap *t = t_at(level, mtmp->mx, mtmp->my); if (t) t->tseen = 1; verbl_msg = "I'm trapped!"; } else if (mtmp->mhp < mtmp->mhpmax / 2) pline_msg = "asks for a potion of healing."; else if (mtmp->mtame && !mtmp->isminion && moves > EDOG(mtmp)->hungrytime) verbl_msg = "I'm hungry."; /* Specific monsters' interests */ else if (is_elf(ptr)) pline_msg = "curses orcs."; else if (is_dwarf(ptr)) pline_msg = "talks about mining."; else if (likes_magic(ptr)) pline_msg = "talks about spellcraft."; else if (ptr->mlet == S_CENTAUR) pline_msg = "discusses hunting."; else switch (monsndx(ptr)) { case PM_HOBBIT: pline_msg = (mtmp->mhpmax - mtmp->mhp >= 10) ? "complains about unpleasant dungeon conditions." : "asks you about the One Ring."; break; case PM_ARCHEOLOGIST: pline_msg = "describes a recent article in \"Spelunker Today\" " "magazine."; break; case PM_TOURIST: verbl_msg = "Aloha."; break; case PM_PRISONER: verbl_msg = "Thank you for freeing me!"; break; default: pline_msg = "discusses dungeon exploration."; break; } break; case MS_SEDUCE: if (ptr->mlet != S_NYMPH && flags.seduce_enabled && could_seduce(mtmp, &youmonst, NULL) == 1) { doseduce(mtmp); break; } switch ((poly_gender() != (int)mtmp->female) ? rn2(3) : 0) { case 2: verbl_msg = "Hello, sailor."; break; case 1: pline_msg = "comes on to you."; break; default: pline_msg = "cajoles you."; } break; case MS_ARREST: if (mtmp->mpeaceful) verbalize("Just the facts, %s.", u.ufemale ? "Ma'am" : "Sir"); else { static const char *const arrest_msg[3] = { "Anything you say can be used against you.", "You're under arrest!", "Stop in the name of the Law!", }; verbl_msg = arrest_msg[rn2(3)]; } break; case MS_BRIBE: if (mtmp->mpeaceful && !mtmp->mtame) { demon_talk(mtmp); break; } /* fall through */ case MS_CUSS: if (!mtmp->mpeaceful) cuss(mtmp); break; case MS_SPELL: /* deliberately vague, since it's not actually casting any spell */ pline_msg = "seems to mutter a cantrip."; break; case MS_NURSE: if (uwep && (uwep->oclass == WEAPON_CLASS || is_weptool(uwep))) verbl_msg = "Put that weapon away before you hurt someone!"; else if (uarmc || (uarm && !uskin()) || uarmh || uarms || uarmg || uarmf) verbl_msg = Role_if(PM_HEALER) ? "Doc, I can't help you unless you cooperate." : "Please undress so I can examine you."; else if (uarmu) verbl_msg = "Take off your shirt, please."; else verbl_msg = "Relax, this won't hurt a bit."; break; case MS_GUARD: if (money_cnt(invent)) verbl_msg = "Please drop that gold and follow me."; else verbl_msg = "Please follow me."; break; case MS_SOLDIER: { static const char *const soldier_foe_msg[3] = { "Resistance is useless!", "You're dog meat!", "Surrender!", }, *const soldier_pax_msg[3] = { "What lousy pay we're getting here!", "The food's not fit for Orcs!", "My feet hurt, I've been on them all day!", }; verbl_msg = mtmp->mpeaceful ? soldier_pax_msg[rn2(3)] : soldier_foe_msg[rn2(3)]; } break; case MS_RIDER: if (ptr == &mons[PM_DEATH] && !rn2(10)) pline_msg = "is busy reading a copy of Sandman #8."; else verbl_msg = "Who do you think you are, War?"; break; } if (pline_msg) pline("%s %s", Monnam(mtmp), pline_msg); else if (verbl_msg) verbalize("%s", verbl_msg); return 1; }
/* http://downloads.openwatcom.org/ftp/devel/docs/elf-64-gen.pdf */ int32_t init_vmas_proc(process_t *proc, void* binary, size_t argv_count, size_t envp_count) { Elf64_Ehdr *hdr = NULL; Elf64_Phdr *phdr = NULL; uint64_t va = 0; uint64_t memsz = 0; uint64_t filesz = 0; int32_t rc = 0; uint64_t env_variable_size = 0; size_t argv_size = 0; hdr = (Elf64_Ehdr *)binary; phdr = (Elf64_Phdr *)( (uint64_t)binary + hdr->e_phoff); if (!is_elf(binary)) { #ifdef LOG printf("ELF Magic Number not Found\n"); #endif return -1; } //printf("%d program headers are present \n",hdr->e_phnum); for ( int i =0; i < hdr->e_phnum; i++ ) { if ( phdr[i].p_type != PT_LOAD ) { continue; } if ( phdr[i].p_filesz > phdr[i].p_memsz ) { /* http://www.daemon-systems.org/man/elf.5.html */ #ifdef LOG printf("ERROR: ELF File too big to fit into memory\n"); #endif return -1; } else { //va = ROUNDDOWN(phdr[i].p_vaddr, PGSIZE, uint64_t); va = (uint64_t)phdr[i].p_vaddr; memsz = phdr[i].p_memsz; filesz = phdr[i].p_filesz; rc = add_vma_to_proc(va, memsz, filesz, proc, phdr[i].p_flags, ((uint64_t)binary+ phdr[i].p_offset), 0); if ( rc ) { #ifdef LOG printf("ERROR: Unable to allocate VMAs for process %d\n", proc->pid); #endif return rc; } } } /* Add a VMA for stack */ rc = add_vma_to_proc(USER_STACK_TOP- INITIAL_STACK_PAGES*PGSIZE, INITIAL_STACK_PAGES*PGSIZE, INITIAL_STACK_PAGES*PGSIZE, proc, (VMA_PERM_WRITE | VMA_PERM_READ), 0, VMA_STACK); if (rc) { #ifdef LOG printf("Unable to add VMA for stack region\n"); #endif return rc; } /* Add a VMA for heap */ rc = add_vma_to_proc(USER_HEAP_START, 0, 0, proc, (VMA_PERM_WRITE | VMA_PERM_READ), 0, VMA_HEAP); if (rc) { #ifdef LOG printf("Unable to add VMA for heap region\n"); #endif return rc; } /* Add a VMA for environment variables */ env_variable_size = envp_count ? (envp_count * sizeof(execve_envp[0])) : sizeof(env_variables); rc = add_vma_to_proc(USER_ENV_VARIABLE_START, env_variable_size, env_variable_size, proc, (VMA_PERM_WRITE | VMA_PERM_READ), envp_count ? 0 : (uint64_t)&env_variables, VMA_ENV_VARIABLE); if (rc) { #ifdef LOG printf("Unable to add VMA for heap region\n"); #endif return rc; } argv_size = argv_count * sizeof(execve_argv[0]); /* Add a VMA for argv */ rc = add_vma_to_proc(USER_ARGV_START, argv_size, argv_size, proc, (VMA_PERM_WRITE | VMA_PERM_READ), 0, VMA_ARGV); if (rc) { #ifdef LOG printf("Unable to add VMA for heap region\n"); #endif return rc; } proc->entry_point = (uint64_t*)hdr->e_entry; //print_vmas(proc); return rc; }
/* * Loads an ELF 32 executable. */ PRIVATE addr_t load_elf32(struct inode *inode) { int i; /* Loop index. */ addr_t addr; /* Region address. */ addr_t entry; /* Program entry point. */ struct elf32_fhdr *elf; /* ELF file header. */ struct elf32_phdr *seg; /* ELF Program header. */ block_t blk; /* Working block number. */ buffer_t header; /* File headers block buffer. */ struct region *reg; /* Working memory region. */ struct pregion *preg; /* Working process memory region. */ blk = block_map(inode, 0, 0); /* Empty file. */ if (blk == BLOCK_NULL) { curr_proc->errno = -ENOEXEC; return (0); } /* Read ELF file header. */ header = bread(inode->dev, blk); elf = buffer_data(header); /* Bad ELF file. */ if (!is_elf(elf)) { brelse(header); curr_proc->errno = -ENOEXEC; return (0); } /* Bad ELF file. */ if (elf->e_phoff + elf->e_phnum*elf->e_phentsize > BLOCK_SIZE) { brelse(header); curr_proc->errno = -ENOEXEC; return (0); } seg = (struct elf32_phdr *)((char *)buffer_data(header) + elf->e_phoff); /* Load segments. */ for (i = 0; i < elf->e_phnum; i++) { /* Not loadable. */ if (seg[i].p_type != PT_LOAD) continue; /* Broken executable. */ if (seg[i].p_filesz > seg[i].p_memsz) { kprintf("broken executable"); brelse(header); curr_proc->errno = -ENOEXEC; return (0); } addr = ALIGN(seg[i].p_vaddr, seg[i].p_align); /* Text section. */ if (!(seg[i].p_flags ^ (PF_R | PF_X))) { preg = TEXT(curr_proc); reg = allocreg(S_IRUSR | S_IXUSR, seg[i].p_memsz, 0); } /* Data section. */ else { preg = DATA(curr_proc); reg = allocreg(S_IRUSR | S_IWUSR, seg[i].p_memsz, 0); } /* Failed to allocate region. */ if (reg == NULL) { brelse(header); curr_proc->errno = -ENOMEM; return (0); } /* Attach memory region. */ if (attachreg(curr_proc, preg, addr, reg)) { freereg(reg); brelse(header); curr_proc->errno = -ENOMEM; return (0); } loadreg(inode, reg, seg[i].p_offset, seg[i].p_filesz); unlockreg(reg); } entry = elf->e_entry; brelse(header); return (entry); }
symbol_table_t *load_symbol_table(const char *filename) { symbol_table_t *table = NULL; #if !defined(__APPLE__) ALOGV("Loading symbol table from '%s'.", filename); int fd = open(filename, O_RDONLY); if (fd < 0) { goto out; } struct stat sb; if (fstat(fd, &sb)) { goto out_close; } size_t length = sb.st_size; char *base = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0); if (base == MAP_FAILED) { goto out_close; } // Parse the file header Elf32_Ehdr *hdr = (Elf32_Ehdr *) base; if (!is_elf(hdr)) { goto out_close; } Elf32_Shdr *shdr = (Elf32_Shdr *) (base + hdr->e_shoff); // Search for the dynamic symbols section int sym_idx = -1; int dynsym_idx = -1; Elf32_Half i; for (i = 0; i < hdr->e_shnum; i++) { if (shdr[i].sh_type == SHT_SYMTAB) { sym_idx = i; } if (shdr[i].sh_type == SHT_DYNSYM) { dynsym_idx = i; } } if (dynsym_idx == -1 && sym_idx == -1) { goto out_unmap; } table = malloc(sizeof(symbol_table_t)); if (!table) { goto out_unmap; } table->num_symbols = 0; Elf32_Sym *dynsyms = NULL; int dynnumsyms = 0; char *dynstr = NULL; if (dynsym_idx != -1) { dynsyms = (Elf32_Sym *) (base + shdr[dynsym_idx].sh_offset); dynnumsyms = shdr[dynsym_idx].sh_size / shdr[dynsym_idx].sh_entsize; int dynstr_idx = shdr[dynsym_idx].sh_link; dynstr = base + shdr[dynstr_idx].sh_offset; } Elf32_Sym *syms = NULL; int numsyms = 0; char *str = NULL; if (sym_idx != -1) { syms = (Elf32_Sym *) (base + shdr[sym_idx].sh_offset); numsyms = shdr[sym_idx].sh_size / shdr[sym_idx].sh_entsize; int str_idx = shdr[sym_idx].sh_link; str = base + shdr[str_idx].sh_offset; } int dynsymbol_count = 0; if (dynsym_idx != -1) { // Iterate through the dynamic symbol table, and count how many symbols // are actually defined int i; for (i = 0; i < dynnumsyms; i++) { if (dynsyms[i].st_shndx != SHN_UNDEF) { dynsymbol_count++; } } } size_t symbol_count = 0; if (sym_idx != -1) { // Iterate through the symbol table, and count how many symbols // are actually defined int i; for (i = 0; i < numsyms; i++) { if (syms[i].st_shndx != SHN_UNDEF && str[syms[i].st_name] && syms[i].st_value && syms[i].st_size) { symbol_count++; } } } // Now, create an entry in our symbol table structure for each symbol... table->num_symbols += symbol_count + dynsymbol_count; table->symbols = malloc(table->num_symbols * sizeof(symbol_t)); if (!table->symbols) { free(table); table = NULL; goto out_unmap; } size_t symbol_index = 0; if (dynsym_idx != -1) { // ...and populate them int i; for (i = 0; i < dynnumsyms; i++) { if (dynsyms[i].st_shndx != SHN_UNDEF) { table->symbols[symbol_index].name = strdup(dynstr + dynsyms[i].st_name); table->symbols[symbol_index].start = dynsyms[i].st_value; table->symbols[symbol_index].end = dynsyms[i].st_value + dynsyms[i].st_size; ALOGV(" [%d] '%s' 0x%08x-0x%08x (DYNAMIC)", symbol_index, table->symbols[symbol_index].name, table->symbols[symbol_index].start, table->symbols[symbol_index].end); symbol_index += 1; } } } if (sym_idx != -1) { // ...and populate them int i; for (i = 0; i < numsyms; i++) { if (syms[i].st_shndx != SHN_UNDEF && str[syms[i].st_name] && syms[i].st_value && syms[i].st_size) { table->symbols[symbol_index].name = strdup(str + syms[i].st_name); table->symbols[symbol_index].start = syms[i].st_value; table->symbols[symbol_index].end = syms[i].st_value + syms[i].st_size; ALOGV(" [%d] '%s' 0x%08x-0x%08x", symbol_index, table->symbols[symbol_index].name, table->symbols[symbol_index].start, table->symbols[symbol_index].end); symbol_index += 1; } } } // Sort the symbol table entries, so they can be bsearched later qsort(table->symbols, table->num_symbols, sizeof(symbol_t), qcompar); out_unmap: munmap(base, length); out_close: close(fd); #endif out: return table; }
/* monster attempts ranged weapon attack against a square */ void thrwmq(struct monst *mtmp, int xdef, int ydef) { struct obj *otmp, *mwep; schar skill; int multishot; const char *onm; /* Rearranged beginning so monsters can use polearms not in a line */ if (mtmp->weapon_check == NEED_WEAPON || !MON_WEP(mtmp)) { mtmp->weapon_check = NEED_RANGED_WEAPON; /* mon_wield_item resets weapon_check as appropriate */ if (mon_wield_item(mtmp) != 0) return; } /* Pick a weapon */ otmp = select_rwep(mtmp); if (!otmp) return; if (is_pole(otmp)) { int dam, hitv; if (otmp != MON_WEP(mtmp)) return; /* polearm must be wielded */ /* TODO: LOE function between two arbitrary points. */ if (dist2(mtmp->mx, mtmp->my, xdef, ydef) > POLE_LIM || (xdef == u.ux && ydef == u.uy && !couldsee(mtmp->mx, mtmp->my))) return; /* Out of range, or intervening wall */ if (mon_visible(mtmp)) { onm = singular(otmp, xname); pline("%s thrusts %s.", Monnam(mtmp), obj_is_pname(otmp) ? the(onm) : an(onm)); } if (xdef == u.ux && ydef == u.uy) { dam = dmgval(otmp, &youmonst); hitv = 3 - distmin(u.ux, u.uy, mtmp->mx, mtmp->my); if (hitv < -4) hitv = -4; if (bigmonst(youmonst.data)) hitv++; hitv += 8 + otmp->spe; if (objects[otmp->otyp].oc_class == WEAPON_CLASS || objects[otmp->otyp].oc_class == VENOM_CLASS) hitv += objects[otmp->otyp].oc_hitbon; if (dam < 1) dam = 1; thitu(hitv, dam, otmp, NULL); action_interrupted(); } else if (MON_AT(level, xdef, ydef)) (void)ohitmon(m_at(level, xdef, ydef), otmp, 0, FALSE); else if (mon_visible(mtmp)) pline("But it misses wildly."); return; } if (!qlined_up(mtmp, xdef, ydef, FALSE, FALSE) || !ai_use_at_range(BOLT_LIM - distmin(mtmp->mx, mtmp->my, xdef, ydef))) return; skill = objects[otmp->otyp].oc_skill; mwep = MON_WEP(mtmp); /* wielded weapon */ /* Multishot calculations */ multishot = 1; if ((ammo_and_launcher(otmp, mwep) || skill == P_DAGGER || skill == -P_DART || skill == -P_SHURIKEN) && !mtmp->mconf) { /* Assumes lords are skilled, princes are expert */ if (is_prince(mtmp->data)) multishot += 2; else if (is_lord(mtmp->data)) multishot++; switch (monsndx(mtmp->data)) { case PM_RANGER: multishot++; break; case PM_ROGUE: if (skill == P_DAGGER) multishot++; break; case PM_NINJA: case PM_SAMURAI: if (otmp->otyp == YA && mwep && mwep->otyp == YUMI) multishot++; break; default: break; } /* racial bonus */ if ((is_elf(mtmp->data) && otmp->otyp == ELVEN_ARROW && mwep && mwep->otyp == ELVEN_BOW) || (is_orc(mtmp->data) && otmp->otyp == ORCISH_ARROW && mwep && mwep->otyp == ORCISH_BOW)) multishot++; if ((long)multishot > otmp->quan) multishot = (int)otmp->quan; if (multishot < 1) multishot = 1; else multishot = rnd(multishot); } if (mon_visible(mtmp)) { if (multishot > 1) { /* "N arrows"; multishot > 1 implies otmp->quan > 1, so xname()'s result will already be pluralized */ onm = msgprintf("%d %s", multishot, xname(otmp)); } else { /* "an arrow" */ onm = singular(otmp, xname); onm = obj_is_pname(otmp) ? the(onm) : an(onm); } m_shot.s = ammo_and_launcher(otmp, mwep) ? TRUE : FALSE; pline("%s %s %s!", Monnam(mtmp), m_shot.s ? "shoots" : "throws", onm); m_shot.o = otmp->otyp; } else { m_shot.o = STRANGE_OBJECT; /* don't give multishot feedback */ } m_shot.n = multishot; for (m_shot.i = 1; m_shot.i <= m_shot.n; m_shot.i++) { m_throw(mtmp, mtmp->mx, mtmp->my, sgn(tbx), sgn(tby), distmin(mtmp->mx, mtmp->my, xdef, ydef), otmp, TRUE); /* conceptually all N missiles are in flight at once, but if mtmp gets killed (shot kills adjacent gas spore and triggers explosion, perhaps), inventory will be dropped and otmp might go away via merging into another stack; if we then use it, we could cause undefined behavior */ if (mtmp->mhp <= 0 && m_shot.i < m_shot.n) { /* cancel pending shots (ought to give a message here since we gave one above about throwing/shooting N missiles) */ break; /* endmultishot(FALSE); */ } } m_shot.n = m_shot.i = 0; m_shot.o = STRANGE_OBJECT; m_shot.s = FALSE; action_interrupted(); }
void m_throw(struct monst *mon, int x, int y, int dx, int dy, int range, struct obj *obj, boolean verbose) { struct monst *mtmp; struct obj *singleobj; struct tmp_sym *tsym = 0; int hitu, blindinc = 0; bhitpos.x = x; bhitpos.y = y; if (obj->quan == 1L) { /* * Remove object from minvent. This cannot be done later on; * what if the player dies before then, leaving the monster * with 0 daggers? (This caused the infamous 2^32-1 orcish * dagger bug). * * VENOM is not in minvent - it should already be OBJ_FREE. * The extract below does nothing. */ /* not possibly_unwield, which checks the object's */ /* location, not its existence */ if (MON_WEP(mon) == obj) { setmnotwielded(mon, obj); MON_NOWEP(mon); } obj_extract_self(obj); singleobj = obj; obj = NULL; } else { singleobj = splitobj(obj, 1L); obj_extract_self(singleobj); } singleobj->owornmask = 0; /* threw one of multiple weapons in hand? */ singleobj->olev = level; /* object is on the same level as monster */ if ((singleobj->cursed || singleobj->greased) && (dx || dy) && !rn2(7)) { if (canseemon(mon) && flags.verbose) { if (is_ammo(singleobj)) pline("%s misfires!", Monnam(mon)); else pline("%s as %s throws it!", Tobjnam(singleobj, "slip"), mon_nam(mon)); } dx = rn2(3) - 1; dy = rn2(3) - 1; /* check validity of new direction */ if (!dx && !dy) { drop_throw(singleobj, 0, bhitpos.x, bhitpos.y); return; } } /* pre-check for doors, walls and boundaries. Also need to pre-check for bars regardless of direction; the random chance for small objects hitting bars is skipped when reaching them at point blank range */ if (!isok(bhitpos.x + dx, bhitpos.y + dy) || IS_ROCK(level->locations[bhitpos.x + dx][bhitpos.y + dy].typ) || closed_door(level, bhitpos.x + dx, bhitpos.y + dy) || (level->locations[bhitpos.x + dx][bhitpos.y + dy].typ == IRONBARS && hits_bars(&singleobj, bhitpos.x, bhitpos.y, 0, 0))) { drop_throw(singleobj, 0, bhitpos.x, bhitpos.y); return; } /* Note: drop_throw may destroy singleobj. Since obj must be destroyed early to avoid the dagger bug, anyone who modifies this code should be careful not to use either one after it's been freed. */ tsym = tmpsym_initobj(singleobj); while (range-- > 0) { /* Actually the loop is always exited by break */ bhitpos.x += dx; bhitpos.y += dy; if ((mtmp = m_at(level, bhitpos.x, bhitpos.y)) != 0) { if (ohitmon(mtmp, singleobj, range, verbose)) break; } else if (bhitpos.x == u.ux && bhitpos.y == u.uy) { action_interrupted(); if (singleobj->oclass == GEM_CLASS && singleobj->otyp <= LAST_GEM + 9 /* 9 glass colors */ && is_unicorn(youmonst.data) && !u_helpless(hm_all)) { if (singleobj->otyp > LAST_GEM) { pline("You catch the %s.", xname(singleobj)); pline("You are not interested in %s junk.", s_suffix(mon_nam(mon))); makeknown(singleobj->otyp); dropy(singleobj); } else { pline("You accept %s gift in the spirit in which it was " "intended.", s_suffix(mon_nam(mon))); hold_another_object(singleobj, "You catch, but drop, %s.", xname(singleobj), "You catch:"); } break; } if (singleobj->oclass == POTION_CLASS) { if (!Blind) singleobj->dknown = 1; potionhit(&youmonst, singleobj, FALSE); break; } switch (singleobj->otyp) { int dam, hitv; case EGG: if (!touch_petrifies(&mons[singleobj->corpsenm])) { impossible("monster throwing egg type %d", singleobj->corpsenm); hitu = 0; break; } /* fall through */ case CREAM_PIE: case BLINDING_VENOM: hitu = thitu(8, 0, singleobj, NULL); break; default: dam = dmgval(singleobj, &youmonst); hitv = 3 - distmin(u.ux, u.uy, mon->mx, mon->my); if (hitv < -4) hitv = -4; if (is_elf(mon->data) && objects[singleobj->otyp].oc_skill == P_BOW) { hitv++; if (MON_WEP(mon) && MON_WEP(mon)->otyp == ELVEN_BOW) hitv++; if (singleobj->otyp == ELVEN_ARROW) dam++; } if (bigmonst(youmonst.data)) hitv++; hitv += 8 + singleobj->spe; if (dam < 1) dam = 1; if (objects[singleobj->otyp].oc_class == WEAPON_CLASS || objects[singleobj->otyp].oc_class == VENOM_CLASS) { hitv += objects[singleobj->otyp].oc_hitbon; } hitu = thitu(hitv, dam, singleobj, NULL); } if (hitu && singleobj->opoisoned && is_poisonable(singleobj)) { poisoned(xname(singleobj), A_STR, killer_msg_obj(POISONING, singleobj), -10); } if (hitu && can_blnd(NULL, &youmonst, (uchar) (singleobj->otyp == BLINDING_VENOM ? AT_SPIT : AT_WEAP), singleobj)) { blindinc = rnd(25); if (singleobj->otyp == CREAM_PIE) { if (!Blind) pline("Yecch! You've been creamed."); else pline("There's something sticky all over your %s.", body_part(FACE)); } else if (singleobj->otyp == BLINDING_VENOM) { int num_eyes = eyecount(youmonst.data); /* venom in the eyes */ if (!Blind) pline("The venom blinds you."); else pline("Your %s sting%s.", (num_eyes == 1) ? body_part(EYE) : makeplural(body_part(EYE)), (num_eyes == 1) ? "s" : ""); } } if (hitu && singleobj->otyp == VAMPIRE_BLOOD) { if (!Drain_resistance) { losexp("vampire blood", FALSE); } } if (hitu && singleobj->otyp == EGG) { if (touched_monster(singleobj->corpsenm)) Stoned = 5; } action_interrupted(); if (hitu || !range) { drop_throw(singleobj, hitu, u.ux, u.uy); break; } } else if (!range /* reached end of path */ /* missile hits edge of screen */ || !isok(bhitpos.x + dx, bhitpos.y + dy) /* missile hits the wall */ || IS_ROCK(level-> locations[bhitpos.x + dx][bhitpos.y + dy].typ) /* missile hit closed door */ || closed_door(level, bhitpos.x + dx, bhitpos.y + dy) /* missile might hit iron bars */ || (level->locations[bhitpos.x + dx][bhitpos.y + dy].typ == IRONBARS && hits_bars(&singleobj, bhitpos.x, bhitpos.y, !rn2(5), 0)) /* Thrown objects "sink" */ || IS_SINK(level->locations[bhitpos.x][bhitpos.y].typ)) { if (singleobj) /* hits_bars might have destroyed it */ drop_throw(singleobj, 0, bhitpos.x, bhitpos.y); break; } tmpsym_at(tsym, bhitpos.x, bhitpos.y); win_delay_output(); } tmpsym_at(tsym, bhitpos.x, bhitpos.y); win_delay_output(); tmpsym_end(tsym); if (blindinc) { u.ucreamed += blindinc; make_blinded(Blinded + (long)blindinc, FALSE); if (!Blind) pline("Your vision quickly clears."); else if (flags.verbose) pline("Use the command #wipe to clean your %s.", body_part(FACE)); } }
/* fungi will eat even tainted food */ int dogfood(const struct monst *mon, struct obj *obj) { boolean carni = carnivorous(mon->data); boolean herbi = herbivorous(mon->data); const struct permonst *fptr = &mons[obj->corpsenm]; boolean starving; if (is_quest_artifact(obj) || obj_resists(obj, 0, 95)) return obj->cursed ? TABU : APPORT; switch (obj->oclass) { case FOOD_CLASS: if (obj->otyp == CORPSE && ((touch_petrifies(&mons[obj->corpsenm]) && !resists_ston(mon)) || is_rider(fptr))) return TABU; /* Ghouls only eat old corpses... yum! */ if (mon->data == &mons[PM_GHOUL]) return (obj->otyp == CORPSE && peek_at_iced_corpse_age(obj) + 50L <= moves) ? DOGFOOD : TABU; if (!carni && !herbi) return obj->cursed ? UNDEF : APPORT; /* a starving pet will eat almost anything */ starving = (mon->mtame && !mon->isminion && CONST_EDOG(mon)->mhpmax_penalty); switch (obj->otyp) { case TRIPE_RATION: case MEATBALL: case MEAT_RING: case MEAT_STICK: case HUGE_CHUNK_OF_MEAT: return carni ? DOGFOOD : MANFOOD; case EGG: if (touch_petrifies(&mons[obj->corpsenm]) && !resists_ston(mon)) return POISON; return carni ? CADAVER : MANFOOD; case CORPSE: if ((peek_at_iced_corpse_age(obj) + 50L <= moves && obj->corpsenm != PM_LIZARD && obj->corpsenm != PM_LICHEN && mon->data->mlet != S_FUNGUS) || (acidic(&mons[obj->corpsenm]) && !resists_acid(mon)) || (poisonous(&mons[obj->corpsenm]) && !resists_poison(mon))) return POISON; else if (vegan(fptr)) return herbi ? CADAVER : MANFOOD; else return carni ? CADAVER : MANFOOD; case CLOVE_OF_GARLIC: return (is_undead(mon->data) ? TABU : ((herbi || starving) ? ACCFOOD : MANFOOD)); case TIN: return metallivorous(mon->data) ? ACCFOOD : MANFOOD; case APPLE: case CARROT: return herbi ? DOGFOOD : starving ? ACCFOOD : MANFOOD; case BANANA: return ((mon->data->mlet == S_YETI) ? DOGFOOD : ((herbi || starving) ? ACCFOOD : MANFOOD)); case K_RATION: case C_RATION: case CRAM_RATION: case LEMBAS_WAFER: case FOOD_RATION: if (is_human(mon->data) || is_elf(mon->data) || is_dwarf(mon->data) || is_gnome(mon->data) || is_orc(mon->data)) return ACCFOOD; default: if (starving) return ACCFOOD; return (obj->otyp > SLIME_MOLD ? (carni ? ACCFOOD : MANFOOD) : (herbi ? ACCFOOD : MANFOOD)); } default: if (obj->otyp == AMULET_OF_STRANGULATION || obj->otyp == RIN_SLOW_DIGESTION) return TABU; if (hates_silver(mon->data) && objects[obj->otyp].oc_material == SILVER) return TABU; if (mon->data == &mons[PM_GELATINOUS_CUBE] && is_organic(obj)) return ACCFOOD; if (metallivorous(mon->data) && is_metallic(obj) && (is_rustprone(obj) || mon->data != &mons[PM_RUST_MONSTER])) { /* Non-rustproofed ferrous based metals are preferred. */ return (is_rustprone(obj) && !obj->oerodeproof) ? DOGFOOD : ACCFOOD; } if (!obj->cursed && obj->oclass != BALL_CLASS && obj->oclass != CHAIN_CLASS) return APPORT; /* fall into next case */ case ROCK_CLASS: return UNDEF; } }
void elf_init_program(void) { char *base; int i; Elf_ehdr *ehdr; Elf_phdr *phdr; size_t size, total_size = 0; char *addr; uintptr_t tmp; /* TODO: manage ELF notes section */ feval("0 state-valid !"); feval("load-base"); base = (char*)cell2pointer(POP()); ehdr = (Elf_ehdr *)base; if (!is_elf(ehdr)) { debug("Not a valid ELF memory image\n"); return; } phdr = (Elf_phdr *)(base + ehdr->e_phoff); for (i = 0; i < ehdr->e_phnum; i++) { #if DEBUG debug("filesz: %08lX memsz: %08lX p_offset: %08lX " "p_vaddr %08lX\n", (unsigned long)phdr[i].p_filesz, (unsigned long)phdr[i].p_memsz, (unsigned long)phdr[i].p_offset, (unsigned long)phdr[i].p_vaddr ); #endif size = MIN(phdr[i].p_filesz, phdr[i].p_memsz); if (!size) continue; #if !defined(CONFIG_SPARC32) && !defined(CONFIG_X86) if( ofmem_claim( phdr[i].p_vaddr, phdr[i].p_memsz, 0 ) == -1 ) { printk("Ignoring failed claim for va %lx memsz %lx!\n", (unsigned long)phdr[i].p_vaddr, (unsigned long)phdr[i].p_memsz); } #endif /* Workaround for archs where sizeof(int) != pointer size */ tmp = phdr[i].p_vaddr; addr = (char *)tmp; memcpy(addr, base + phdr[i].p_offset, size); total_size += size; #ifdef CONFIG_PPC flush_icache_range( addr, addr + size ); #endif } // Initialise saved-program-state PUSH(ehdr->e_entry); feval("saved-program-state >sps.entry !"); PUSH(total_size); feval("saved-program-state >sps.file-size !"); feval("elf saved-program-state >sps.file-type !"); feval("-1 state-valid !"); }
int execve(char *name, char **argv, char **env) { if(current->proc->flags & PROC_FLAG_DEBUG) { debug("[info]EXECVE(%s, %x, %x)\n", name, argv, env); } // Find the executable INODE executable = vfs_namei(name); if(!executable) { debug("[error] Executable %s not found.\n", name); errno = ENOENT; return -1; } if(is_elf(executable) != 2) { errno = ENOEXEC; debug("[error] Tried to load unexecutable file.\n"); return -1; } // Save environment in kernel space unsigned int envc = 0; char **temp_env; if(env) { while(env[envc++]); temp_env = calloc(envc, sizeof(char *)); unsigned int i = 0; while(env[i]) { temp_env[i] = strdup(env[i]); i++; } temp_env[envc-1] = 0; } // Save arguments in kernel space unsigned int argc = 0; char **temp_argv; if(argv) { while(argv[argc++]); temp_argv = calloc(argc, sizeof(char *)); unsigned int i = 0; while(argv[i]) { temp_argv[i] = strdup(argv[i]); i++; } temp_argv[argc-1] = 0; } if(current->proc->cmdline) free(current->proc->cmdline); current->proc->cmdline = strdup(name); // Clear all process memory areas procmm_removeall(current->proc); // Reset signal handlers memset(current->proc->signal_handler, 0, sizeof(sig_t)*NUM_SIGNALS); memset(current->proc->signal_blocked, 0, NUM_SIGNALS); init_list(current->proc->signal_queue); // Load executable load_elf(executable); // Reset thread registers and state current->r.eax = current->r.ebx = current->r.ecx = current->r.edx = 0; // Add an area for the process stack new_area(current->proc, USER_STACK_TOP, USER_STACK_TOP, MM_FLAG_WRITE | MM_FLAG_GROWSDOWN | MM_FLAG_ADDONUSE, MM_TYPE_STACK); current->kernel_thread = (registers_t *)current; uint32_t *pos = (uint32_t *)USER_STACK_TOP; // uint32_t since the stack should be word alligned // Restore environment if (env) { pos = pos - envc*sizeof(char *)/sizeof(uint32_t) - 1; env = (char **)pos; int i = 0; while(temp_env[i]) { pos = pos - strlen(temp_env[i])/sizeof(uint32_t) - 2; memcpy(pos, temp_env[i], strlen(temp_env[i])+1); env[i] = (char *)pos; i++; } env[envc-1] = 0; } // Restore arguments if(argv) { pos = pos - argc*sizeof(char *)/sizeof(uint32_t) - 1; argv = (char **)pos; int i = 0; while(temp_argv[i]) { pos = pos - strlen(temp_argv[i])/sizeof(uint32_t) - 2; memcpy(pos, temp_argv[i], strlen(temp_argv[i])+1); argv[i] = (char *)pos; i++; } argv[argc-1] = 0; } pos = pos - 3; pos[0] = (uint32_t)argc-1; pos[1] = (uint32_t)argv; pos[2] = (uint32_t)env; current->r.useresp = current->r.ebp = (uint32_t)pos; current->r.ecx = (uint32_t)pos; errno = 0; return 0; }
int print_output(char **output, int i, char **argv, int argc) { //stat files struct stat buf; int exists; int x; for(x = 0; x < i; x++) { exists = stat(output[x], &buf); if (exists < 0) { fprintf(stderr, "%s not found\n", output[x]); } else { ////////////get the argument int arge = find_arg_element(argv, argc); char* ls_arg; ls_arg = (char*)malloc(MAX_BUFFER_SIZE); if(arge != -1) ls_arg = strdup(argv[arge]); else ls_arg = ""; /////////////filter arguments // l option char *l_opt; l_opt = (char*)malloc(MAX_BUFFER_SIZE); if(str_index(ls_arg, "l") != -1) { //convert stat's date time_t rawtime = buf.st_mtime; char *date = ctime(&rawtime); //get rid of end newline date[strlen(date) - 1] = '\0'; int read, write, execute; read = buf.st_mode & S_IEXEC; read = (int)read; //read = sqrt(read) - 1; sprintf(l_opt, "%4d %d %d %4d %5d %s", buf.st_mode, buf.st_nlink, buf.st_uid, buf.st_gid, buf.st_size, date); } else {l_opt = "";} // t option char *t_opt; t_opt = (char*)malloc(MAX_BUFFER_SIZE); if(str_index(ls_arg, "t") != -1) { //convert stat's date time_t rawtime = buf.st_mtime; char *date = ctime(&rawtime); //get rid of end newline date[strlen(date) - 1] = '\0'; sprintf(t_opt, "%s", date); } else {t_opt = "";} // f option char *f_opt; f_opt = (char*)malloc(MAX_BUFFER_SIZE); if(str_index(ls_arg, "f") != -1) { //open file and see what its first bits look like FILE *fp; fp = fopen(output[x], "r"); char line [MAX_BUFFER_SIZE]; if(fp != NULL) { if(fgets(line, sizeof(line), fp ) != NULL) { //fputs ( line, stdout ); //print_ts_str(line); if(is_o(output[x]) == 1) f_opt = " - Relocatable .o file"; else if(is_elf(line) == 1) f_opt = " - ELF File"; else if(is_dos(fp) == 1) f_opt = " - ASCII File"; else if(is_ascii(fp) == 1) f_opt = " - ASCII File"; else f_opt = " - file unknow"; } else f_opt = " - directory"; } fclose(fp); } else {f_opt = "";} //print formatted text printf("%s %s %10s %s\n", t_opt, l_opt, output[x], f_opt); } } return(0); }