bool module_set_default_path() { static const char *HAKA_CORE_PATH = "/share/haka/core/*"; static const char *HAKA_MODULE_PATH = "/share/haka/modules/*"; static const char *HAKA_MODULE_CPATH = "/lib/haka/modules/*"; size_t path_len; char *path; const char *haka_path_s = haka_path(); /* Lua module path */ { /* format <haka_path_s><HAKA_CORE_PATH>;<haka_path_s><HAKA_MODULE_PATH>\0 * in a string. */ path_len = 2*strlen(haka_path_s) + strlen(HAKA_CORE_PATH) + 1 + strlen(HAKA_MODULE_PATH) + 1; path = malloc(path_len); if (!path) { error(L"memory error"); return false; } snprintf(path, path_len, "%s%s;%s%s", haka_path_s, HAKA_CORE_PATH, haka_path_s, HAKA_MODULE_PATH); module_set_path(path, false); free(path); } /* C module path */ { /* format <haka_path_s><HAKA_MODULE_CPATH>\0 * in a string. */ path_len = strlen(haka_path_s) + strlen(HAKA_MODULE_CPATH) + 1; path = malloc(path_len); if (!path) { error(L"memory error"); return false; } snprintf(path, path_len, "%s%s", haka_path_s, HAKA_MODULE_CPATH); module_set_path(path, true); free(path); } return true; }
static int riva_load(module_t *Module, const char *FileName) { riva_t *Riva = unew(riva_t); // This really should be new... module_setup(Module, Riva, (module_importer)riva_import); gzFile File = gzopen(FileName, "rb"); char *LoadPath; for (int I = strlen(FileName) - 1; I >= 0; --I) { if (FileName[I] == PATHCHR) { strncpy(LoadPath = (char *)GC_malloc_atomic(I + 2), FileName, I + 1); break; }; }; module_set_path(Module, LoadPath); uint32_t Magic; gzread(File, &Magic, 4); if (Magic != 0x41564952) { log_errorf("Error: %s is not a valid riva module\n", FileName); return 0; }; uint32_t NoOfSections; gzread(File, &NoOfSections, 4); uint32_t NoOfExports; gzread(File, &NoOfExports, 4); uint32_t NoOfRequires; gzread(File, &NoOfRequires, 4); jmp_buf OnError[1]; if (setjmp(OnError)) return 0; section_t **Sections = (Riva->Sections = (section_t **)GC_malloc(NoOfSections * sizeof(section_t *))); for (int I = 0; I < NoOfSections; ++I) Sections[I] = new(section_t); for (int I = 0; I < NoOfSections; ++I) { section_t *Section = Sections[I]; uint8_t Type; gzread(File, &Type, 1); switch (Type) { case SECT_CODE: { Section->Fixup = fixup_code_section; gzread(File, &Section->Flags, 1); uint32_t Length; gzread(File, &Length, 4); uint32_t NoOfRelocs; gzread(File, &NoOfRelocs, 4); Section->NoOfRelocs = NoOfRelocs; reloc_t *Relocs = (Section->Relocs = (reloc_t *)GC_malloc_uncollectable(NoOfRelocs * sizeof(reloc_t))); if (Section->Flags & FLAG_GC) { Section->Data = GC_malloc_uncollectable(Length); } else { Section->Data = GC_malloc_atomic_uncollectable(Length); }; gzread(File, Section->Data, Length); for (int J = 0; J < NoOfRelocs; ++J) { reloc_t *Reloc = &Relocs[J]; gzread(File, &Reloc->Size, 1); gzread(File, &Reloc->Flags, 1); gzread(File, &Reloc->Position, 4); uint32_t Index; gzread(File, &Index, 4); Reloc->Section = Sections[Index]; }; break;}; case SECT_LIBRARY: { Section->Fixup = fixup_library_section; gzread(File, &Section->Flags, 1); uint32_t Length; gzread(File, &Length, 4); gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length); Section->Name[Length] = 0; if (Section->Flags == LIBRARY_ABS) { Section->Path = 0; } else if (Section->Flags == LIBRARY_REL) { Section->Path = LoadPath; }; for (char *P = Section->Name; *P; ++P) if (*P == '/') *P = PATHCHR; break;}; case SECT_IMPORT: { Section->Fixup = fixup_import_section; gzread(File, &Section->Flags, 1); uint32_t Index; gzread(File, &Index, 4); Section->Library = Sections[Index]; uint32_t Length; gzread(File, &Length, 4); gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length); Section->Name[Length] = 0; break;}; case SECT_BSS: { Section->Fixup = fixup_bss_section; gzread(File, &Section->Flags, 1); uint32_t Size; gzread(File, &Size, 4); Section->Data = (uint8_t *)GC_malloc(Size); break;}; case SECT_SYMBOL: { Section->Fixup = fixup_symbol_section; gzread(File, &Section->Flags, 1); uint32_t Length; gzread(File, &Length, 4); gzread(File, Section->Name = (char *)GC_malloc_atomic(Length + 1), Length); Section->Name[Length] = 0; break;}; }; }; for (int I = 0; I < NoOfExports; ++I) { export_t *Export = new(export_t); gzread(File, &Export->Flags, 1); uint32_t Index; gzread(File, &Index, 4); Export->Section = Sections[Index]; gzread(File, &Export->Offset, 4); uint32_t Length; gzread(File, &Length, 4); char *Name = (char *)GC_malloc_atomic(Length + 1); gzread(File, Name, Length); Name[Length] = 0; stringtable_put(Riva->Exports, Name, Export); }; for (int I = 0; I < NoOfRequires; ++I) { uint8_t Flags; gzread(File, &Flags, 1); uint32_t Length; gzread(File, &Length, 4); char *Name = (char *)GC_malloc_atomic(Length + 1); char *Path = 0; gzread(File, Name, Length); Name[Length] = 0; if (Flags == LIBRARY_REL) Path = LoadPath; for (char *P = Name; *P; ++P) if (*P == '/') *P = PATHCHR; module_load(Path, Name); }; gzclose(File); void (*__init)(module_t *) = check_import(Riva, "__init", OnError); if (__init) __init(Module); void *Methods = check_import(Riva, "__methods", OnError); if (Methods) add_methods(Methods); return 1; };