static void add_symtab(Elf *elf) { String *symtabb = make_string(); String *strtabb = make_string(); o1(strtabb, 0); // Null symbol for (int i = 0; i < 24; i++) o1(symtabb, 0); // File symbol o4(symtabb, STRING_LEN(strtabb)); // st_name ostr(strtabb, "noname"); o1(symtabb, ELF64_ST_INFO(STB_LOCAL, STT_FILE)); // st_info o1(symtabb, 0); // other o2(symtabb, SHN_ABS); // st_shndx o8(symtabb, 0); // st_value o8(symtabb, 0); // st_size int index = 2; write_sym_to_buf(elf, &index, symtabb, strtabb, true); int localidx = index; write_section_sym(elf, &index, symtabb, strtabb); write_sym_to_buf(elf, &index, symtabb, strtabb, false); elf->symtabnum = LIST_LEN(elf->sections) + 1; Section *symtab = make_section(".symtab", SHT_SYMTAB); symtab->body = symtabb; symtab->link = LIST_LEN(elf->sections) + 2; symtab->info = localidx + 2; symtab->entsize = 24; symtab->align = 4; add_section(elf, symtab); Section *strtab = make_section(".strtab", SHT_STRTAB); strtab->body = strtabb; add_section(elf, strtab); }
Elf *new_elf(void) { Elf *elf = make_elf(); Section *data = make_section(".data", SHT_PROGBITS); data->flags = SHF_ALLOC | SHF_WRITE; data->align = 4; add_section(elf, data); Section *text = make_section(".text", SHT_PROGBITS); text->flags = SHF_ALLOC | SHF_EXECINSTR; text->align = 16; add_section(elf, text); return elf; }
int main(int argc, char *argv[]) { Loader loader = NULL; printf("Hello world!\n"); if (argc < 2) { printf("Usage: %s <filename>\n", argv[0]); exit(1); } if (!is_pe(argv[1])) { printf("Error: %s is not a valid PE\n", argv[1]); exit(1); } loader = (Loader)calloc(1, sizeof(Struct_Loader)); if (loader == NULL) { printf("Error: cannot allocate memory for the loader\n"); exit(1); } init_loader(loader, x86_32_jump_far, 0x1); add_section(argv[1], loader); return 0; }
/* add version resources to the dll by copying them from the source module */ static BOOL add_version_resource( HMODULE module, struct dll_info *dll_info ) { BOOL ret = FALSE; DWORD rva_start = ~0U, rva_end = 0, dir_size, total_size; const IMAGE_RESOURCE_DIRECTORY *basedir; const BYTE *data_start, *root; BYTE *buffer; if (!module) return TRUE; if (LdrFindResourceDirectory_U( module, NULL, 0, &basedir ) != STATUS_SUCCESS) return TRUE; root = (const BYTE *)basedir; get_resource_data( basedir, root, &rva_start, &rva_end ); data_start = (const BYTE *)module + rva_start; if (data_start <= root) return FALSE; dir_size = data_start - root; if (!(buffer = HeapAlloc( GetProcessHeap(), 0, dir_size ))) return FALSE; memcpy( buffer, root, dir_size ); fixup_resources( (IMAGE_RESOURCE_DIRECTORY *)buffer, buffer, dll_info->mem_pos + dir_size - rva_start ); if (!xwrite( dll_info, buffer, dir_size, dll_info->file_pos )) goto done; if (!xwrite( dll_info, data_start, rva_end - rva_start, dll_info->file_pos + dir_size )) goto done; total_size = dir_size + rva_end - rva_start; add_directory( dll_info, IMAGE_DIRECTORY_ENTRY_RESOURCE, dll_info->mem_pos, total_size ); add_section( dll_info, ".rsrc", total_size, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ ); ret = TRUE; done: HeapFree( GetProcessHeap(), 0, buffer ); return ret; }
static void add_reloc(Elf *elf) { char name[100]; for (int i = 0; i < LIST_LEN(elf->sections); i++) { Section *sect = LIST_REF(elf->sections, i); if (LIST_LEN(sect->rels) == 0) continue; String *b = make_string(); for (int j = 0; j < LIST_LEN(sect->rels); j++) { Reloc *rel = LIST_REF(sect->rels, j); o8(b, rel->off); if (rel->sym) { o8(b, ELF64_R_INFO(find_symbol(elf, rel->sym)->index, rel->type)); } else { o8(b, ELF64_R_INFO(rel->section->symindex, rel->type)); } o8(b, rel->addend); } strcpy(name, ".rela"); strcpy(name + 5, sect->name); Section *relsec = make_section(name, SHT_RELA); relsec->link = elf->symtabnum; relsec->info = i + 1; relsec->body = b; relsec->entsize = 24; relsec->align = 4; add_section(elf, relsec); } }
/* make a linked list of all sections containing ALLOCatable data */ static void read_sections(int fd, Elf_Ehdr *ehdr, char *shstrtab, struct elf_section **head) { int i; Elf_Shdr shdr; *head = NULL; /* scan through section headers */ for (i = 0; i < ehdr->e_shnum; i++) { struct elf_section *s; read_section_header(fd, ehdr, i, &shdr); if ((shdr.sh_flags & SHF_ALLOC) == 0 && shdr.sh_type != SHT_STRTAB && shdr.sh_type != SHT_SYMTAB && shdr.sh_type != SHT_DYNSYM) { /* skip non-ALLOC sections */ continue; } s = malloc(sizeof(*s)); if (s == NULL) errx(1, "failed to allocate %lu bytes", (u_long)sizeof(*s)); s->name = shstrtab + shdr.sh_name; s->type = shdr.sh_type; s->addr = (void *)shdr.sh_addr; s->offset = shdr.sh_offset; s->size = shdr.sh_size; s->align = shdr.sh_addralign; add_section(head, s); } }
void usdt_dof_file_generate(usdt_dof_file_t *file, usdt_strtab_t *strtab) { dof_hdr_t header; uint64_t filesz; uint64_t loadsz; usdt_dof_section_t *sec; size_t offset; dof_header(&header); header.dofh_secnum = 6; filesz = sizeof(dof_hdr_t) + (sizeof(dof_sec_t) * header.dofh_secnum); loadsz = filesz; strtab->offset = filesz; pad_section((usdt_dof_section_t *)strtab); filesz += strtab->size + strtab->pad; if (strtab->flags & 1) loadsz += strtab->size + strtab->pad; for (sec = file->sections; sec != NULL; sec = sec->next) { sec->offset = filesz; pad_section(sec); filesz += sec->size + sec->pad; if (sec->flags & 1) loadsz += sec->size + sec->pad; } header.dofh_loadsz = loadsz; header.dofh_filesz = filesz; memcpy(file->dof, &header, sizeof(dof_hdr_t)); offset = sizeof(dof_hdr_t); offset = add_header(file, offset, (usdt_dof_section_t *)strtab); for (sec = file->sections; sec != NULL; sec = sec->next) offset = add_header(file, offset, sec); offset = add_section(file, offset, (usdt_dof_section_t *)strtab); for (sec = file->sections; sec != NULL; sec = sec->next) offset = add_section(file, offset, sec); }
char *add_dsection(char *buf, Meinkompf *db) { char *str; char *result; str = my_printf_str("[%s]", buf); result = add_section(str, db); xfree(str); return (result); }
void Config::set(const char* section, const char* key, double value) { ConfigSection* sc = add_section(section); char* locale = nls_locale_to_c(); char tmp[32]; snprintf(tmp, sizeof(tmp)-1, "%g", value); nls_locale_from_c(locale); sc->add_entry(key, tmp); }
static void cc_ua_panel_init_hearing (CcUaPanel *self) { CcUaPanelPrivate *priv = self->priv; GtkWidget *list; GtkWidget *dialog; list = WID ("list_hearing"); add_section (list, self); add_separators (GTK_LIST_BOX (list)); g_signal_connect_swapped (list, "row-activated", G_CALLBACK (activate_row), self); /* set the initial visual bell values */ visual_bell_type_notify_cb (NULL, NULL, self); /* and listen */ g_settings_bind (priv->wm_settings, KEY_VISUAL_BELL_ENABLED, WID ("visual_alerts_switch"), "active", G_SETTINGS_BIND_DEFAULT); g_settings_bind_with_mapping (priv->wm_settings, KEY_VISUAL_BELL_ENABLED, WID ("value_visual_alerts"), "label", G_SETTINGS_BIND_GET, on_off_label_mapping_get, NULL, NULL, NULL); g_object_bind_property (WID ("visual_alerts_switch"), "active", WID ("visual_alerts_window_radio"), "sensitive", G_BINDING_SYNC_CREATE); g_object_bind_property (WID ("visual_alerts_switch"), "active", WID ("visual_alerts_screen_radio"), "sensitive", G_BINDING_SYNC_CREATE); g_signal_connect (priv->wm_settings, "changed::" KEY_VISUAL_BELL_TYPE, G_CALLBACK (visual_bell_type_notify_cb), self); g_signal_connect (WID ("visual_alerts_window_radio"), "toggled", G_CALLBACK (visual_bell_type_toggle_cb), self); dialog = WID ("visual_alerts_dialog"); g_object_set_data (G_OBJECT (WID ("row_visual_alerts")), "dialog", dialog); g_signal_connect_swapped (WID ("visual_alerts_done"), "clicked", G_CALLBACK (gtk_widget_hide), dialog); g_signal_connect (dialog, "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL); g_signal_connect (WID ("visual_alerts_test_button"), "clicked", G_CALLBACK (gdk_beep), NULL); g_signal_connect (WID ("heading_visual_alerts"), "mnemonic-activate", G_CALLBACK (mnemonic_activate), self); }
static void add_shstrtab(Elf *elf) { Section *shstr = make_section(".shstrtab", SHT_STRTAB); elf->shnum = LIST_LEN(elf->sections) + 1; add_section(elf, shstr); String *b = make_string(); o1(b, 0); for (int i = 0; i < LIST_LEN(elf->sections); i++) { Section *sect = LIST_REF(elf->sections, i); sect->shstrtab_off = STRING_LEN(b); ostr(b, sect->name); } shstr->body = b; }
GtkWidget* eee_account_wizard_page(EPlugin *epl, EConfigHookItemFactoryData *data) { GtkWidget *page, *panel, *section, *checkbutton_status, *label; char *title = _("3e Calendar Account"); assistant = GTK_ASSISTANT(data->parent); if (data->old) return data->old; page = gtk_vbox_new (FALSE, 12); gtk_container_set_border_width (GTK_CONTAINER (page), 12); // toplevel vbox contains frames that group 3E account settings into various // groups // Status group section = add_section(page, _("Enable 3e calendar account")); // char* note = g_strdup(_("3e calendar server has been found for your domain. You can enable\n" // "calendar account if you have it. If you don't know ask your system\n" // "administrator or provider of your email service. Go to email account\n" // "preferences to change this setting later.")); label = GTK_WIDGET(g_object_new(GTK_TYPE_LABEL, "label", "", "use-markup", TRUE, "justify", GTK_JUSTIFY_LEFT, "xalign", 0, "yalign", 0.5, NULL)); // g_free(note); lbl = (GtkLabel*)label; gtk_box_pack_start(GTK_BOX(section), label, FALSE, FALSE, 0); checkbutton_status = gtk_check_button_new_with_label(_("Enable 3e calendar account")); gtk_widget_set_can_focus (checkbutton_status, FALSE); gtk_box_pack_start(GTK_BOX(section), checkbutton_status, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_status), TRUE); g_signal_connect(checkbutton_status, "toggled", G_CALLBACK(wizard_chb_status_changed), (gpointer)title); gtk_widget_show_all(page); gtk_assistant_append_page(GTK_ASSISTANT(data->parent), page); gtk_assistant_set_page_title (GTK_ASSISTANT(data->parent), page, title); gtk_assistant_set_page_type (GTK_ASSISTANT(data->parent), page, GTK_ASSISTANT_PAGE_CONTENT); // g_object_set_data((GObject *)data->parent, "restore", GINT_TO_POINTER(FALSE)); return GTK_WIDGET(page); }
int config_read_string(const char *sectionname, const char *str) /* All the config parameters are placed in the given section in memory.*/ { int pos = 0; ConfigSection *s; /* We use a nested function to transfer the characters from buffer to parser*/ char get_next_char() { return str[pos++]; } if ((s = find_section(sectionname)) == NULL) s = add_section(sectionname); return process_config(&s, get_next_char, "command line", NULL); }
static int process_sections(t_file_info *file_info, struct segment_command_64 *seg, size_t cmd_offset) { uint32_t i; size_t offset; i = 0; offset = cmd_offset + sizeof(struct segment_command_64); while (i < seg->nsects) { add_section(file_info->data + offset, file_info); offset += sizeof(struct section_64); i++; } return (SUCCESS); }
GtkWidget* eee_account_wizard_page(EPlugin *epl, EConfigHookItemFactoryData *data) { GtkWidget *page, *panel, *section, *checkbutton_status, *label; char *title = _("3e Calendar Account"); assistant = GTK_ASSISTANT(data->parent); if (data->old) return data->old; page = gtk_vbox_new (FALSE, 12); gtk_container_set_border_width (GTK_CONTAINER (page), 12); // toplevel vbox contains frames that group 3E account settings into various // groups // Status group section = add_section(page, _("Enable 3e calendar account")); label = gtk_label_new (NULL); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5); lbl = GTK_LABEL (label); gtk_box_pack_start(GTK_BOX(section), label, FALSE, FALSE, 0); checkbutton_status = gtk_check_button_new_with_label(_("Enable 3e calendar account")); gtk_widget_set_can_focus (checkbutton_status, FALSE); gtk_box_pack_start(GTK_BOX(section), checkbutton_status, FALSE, FALSE, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_status), TRUE); g_signal_connect(checkbutton_status, "toggled", G_CALLBACK(wizard_chb_status_changed), (gpointer)title); gtk_widget_show_all(page); gtk_assistant_append_page(GTK_ASSISTANT(data->parent), page); gtk_assistant_set_page_title (GTK_ASSISTANT(data->parent), page, title); gtk_assistant_set_page_type (GTK_ASSISTANT(data->parent), page, GTK_ASSISTANT_PAGE_CONTENT); // g_object_set_data((GObject *)data->parent, "restore", GINT_TO_POINTER(FALSE)); return page; }
bool regini_file::open(string filename) { this->filename = filename; std::ifstream file(filename); vector<string> line; section.clear(); if (file.is_open()) { while (!file.eof()) { string newline; std::getline(file, newline); line.push_back(newline); } file.close(); if (!line.empty()) { current_section = nullptr; for (string str : line) { if (str.empty()) continue; size_t size = str.size(); if (str[0] == '[' && str[size - 1] == ']') { add_section(str); } else if (str[0] == '"' && str.find('=') != string::npos && current_section != 0) { add_entry(str); } } } } return !section.empty(); }
GtkWidget *eee_account_properties_page(EPlugin *epl, EConfigHookItemFactoryData *data) { EMConfigTargetAccount *target = (EMConfigTargetAccount *)data->config->target; const char *name = e_account_get_string(target->original_account, E_ACCOUNT_ID_ADDRESS); GtkWidget *panel, *section, *checkbutton_status, *label; if (data->old) { return data->old; } // toplevel vbox contains frames that group 3E account settings into various // groups panel = gtk_vbox_new(FALSE, 12); gtk_container_set_border_width(GTK_CONTAINER(panel), 12); // Status group section = add_section(panel, _("3e Account Status")); char *note = g_strdup_printf(_("If you have 3e account <i>%s</i>, you can turn it on/off here."), name); label = GTK_WIDGET(g_object_new(GTK_TYPE_LABEL, "label", note, "use-markup", TRUE, "justify", GTK_JUSTIFY_LEFT, "xalign", 0, "yalign", 0.5, NULL)); g_free(note); gtk_box_pack_start(GTK_BOX(section), label, FALSE, FALSE, 0); checkbutton_status = gtk_check_button_new_with_label(_("Enable 3e Account")); gtk_box_pack_start(GTK_BOX(section), checkbutton_status, FALSE, FALSE, 0); //XXX: update button based on live account status gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbutton_status), !eee_accounts_manager_account_is_disabled(mgr(), name)); g_signal_connect(checkbutton_status, "toggled", G_CALLBACK(status_changed), (gpointer)name); // <<< this should be ok gtk_widget_show_all(panel); gtk_notebook_insert_page(GTK_NOTEBOOK(data->parent), panel, gtk_label_new(_("3e Settings")), 4); return panel; }
// Load progbits, symtab, strtab and rels from an ELF object file int object::open( char *fname ) { Elf32_Ehdr ehdr; Elf32_Shdr *shdr; char *shstrtab = 0; sections = NULL; locals = NULL; next = NULL; filename = strdup( fname ); fp = fopen( filename, "rb" ); if( !fp ) { error( "could not open object file '%s'", filename ); return 1; } if( fread( &ehdr, sizeof ehdr, 1, fp ) != 1 ) { error( "cannot read elf header from '%s'", filename ); return 1; } if( ehdr.e_magic != ELFMAGIC || ehdr.e_class != ELFCLASS32 || ehdr.e_data != ELFDATA2LSB || ehdr.e_machine != EM_386 || ehdr.e_version != EV_CURRENT) { error( "bad elf file header in '%s'", filename ); return 1; } if( ehdr.e_type != ET_REL ) { warning( "file '%s' is not relocatable, skipped!", filename ); return 1; } if( ehdr.e_shoff == 0 ) { warning( "section header is not present in '%s', skipped!", filename ); return 1; } if( ehdr.e_shstrndx == SHN_UNDEF ) { warning( "section string table is absent in '%s', possibly broken file", filename ); } // read section headers char *shdrs = new char [ehdr.e_shentsize * ehdr.e_shnum]; fseek( fp, ehdr.e_shoff, SEEK_SET ); if( fread( shdrs, ehdr.e_shentsize, ehdr.e_shnum, fp ) != ehdr.e_shnum ) { error( "cannot read section header from '%s'", filename ); return 1; } // now find and load section string table shdr = (Elf32_Shdr *)(shdrs + ehdr.e_shstrndx * ehdr.e_shentsize); if( shdr->sh_size != 0 ) { shstrtab = new char [shdr->sh_size]; fseek( fp, shdr->sh_offset, SEEK_SET ); if( fread( shstrtab, shdr->sh_size, 1, fp ) != 1 ) { error( "could not read section string table from '%s'", filename ); return 1; } } // now read sections and append to object's sections list for( int i = 0; i < ehdr.e_shnum; i++ ) { shdr = (Elf32_Shdr *)(shdrs + i * ehdr.e_shentsize); char *data = NULL; if( ((shdr->sh_type == SHT_PROGBITS && (shdr->sh_flags & SHF_ALLOC)) || // progbits (shdr->sh_type == SHT_SYMTAB) || // symtab (shdr->sh_type == SHT_STRTAB && (i != ehdr.e_shstrndx)) || // strtab (shdr->sh_type == SHT_REL)) // rel ) { DEBUG(( stderr, "reading section %d of type %d [size %d at position %d]\n", i, shdr->sh_type, shdr->sh_size, shdr->sh_offset )); // empty .text sections must be loaded to avoid warnings // we load other important sections as well, even if they are empty if( shdr->sh_size == 0 ) { add_section( shstrtab + shdr->sh_name, shdr, i, NULL ); } else { data = new char [shdr->sh_size]; fseek( fp, shdr->sh_offset, SEEK_SET ); if( fread( data, shdr->sh_size, 1, fp ) != 1 ) { error( "could not read section from '%s'", filename ); return 1; } add_section( shstrtab + shdr->sh_name, shdr, i, data ); } } else if( (shdr->sh_type == SHT_NOBITS) && (shdr->sh_flags & SHF_ALLOC) ) // .bss { add_section( shstrtab + shdr->sh_name, shdr, i, data ); } else DEBUG(( stderr, "skipping section %s of type %d [size %08x, flags %08x]\n", shstrtab + shdr->sh_name, shdr->sh_type, shdr->sh_size, shdr->sh_flags )); } delete shstrtab; delete shdrs; return 0; }
struct ini_file *ini_parse(const char *text, int *err, int *line) { jmp_buf on_error; int e_code; struct ini_file *ini = NULL; ini_section *cur_sec = NULL; const char *tstart, *tend; int t; if(err) *err = SUCCESS; if(line) *line = 1; ini = make_ini(); if((e_code = setjmp(on_error)) != 0) { if(err) *err = e_code; ini_free(ini); return NULL; } while((t = get_token(&text, &tstart, &tend, line, on_error)) != T_END) { if(t == '[') { char *section_name; if(get_token(&text, &tstart, &tend, line, on_error) != T_VALUE) { longjmp(on_error, EMPTY_SECTION); } section_name = get_string(tstart, tend, on_error); cur_sec = add_section(&ini->sections, section_name); if(!cur_sec) longjmp(on_error, OUT_OF_MEMORY); if(get_token(&text, &tstart, &tend, line, on_error) != ']') { longjmp(on_error, MISSING_END_BRACE); } } else if (t == T_VALUE ) { char *par, *val; par = get_string(tstart, tend, on_error); t = get_token(&text, &tstart, &tend, line, on_error); if(t != '=' && t != ':') { longjmp(on_error, EXPECTED_EQUALS); } if(get_token(&text, &tstart, &tend, line, on_error) != T_VALUE) { longjmp(on_error, EXPECTED_VALUE); } val = get_string(tstart, tend, on_error); if(cur_sec) add_pair(cur_sec, par, val); else { /* Add the parameter and value to the INI file's globals */ ini_pair *pair; if(!(pair = malloc(sizeof *pair))) longjmp(on_error, OUT_OF_MEMORY); pair->param = par; pair->value = val; pair->left = pair->right = NULL; if(!ini->globals) ini->globals = pair; else insert_pair(ini->globals, pair); } } else longjmp(on_error, EXPECTED_PARAMETER); } return ini; }
GtkWidget * do_cursors (GtkWidget *do_widget) { static GtkWidget *window = NULL; if (!window) { GtkWidget *sw; GtkWidget *box; GtkWidget *section; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_screen (GTK_WINDOW (window), gtk_widget_get_screen (do_widget)); gtk_window_set_title (GTK_WINDOW (window), "Cursors"); gtk_window_set_default_size (GTK_WINDOW (window), 500, 500); g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window); sw = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_container_add (GTK_CONTAINER (window), sw); box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); g_object_set (box, "margin-start", 20, "margin-end", 20, "margin-bottom", 10, NULL); gtk_container_add (GTK_CONTAINER (sw), box); section = add_section (box, "General"); add_button (section, "default"); add_button (section, "none"); section = add_section (box, "Link & Status"); add_button (section, "context-menu"); add_button (section, "help"); add_button (section, "pointer"); add_button (section, "progress"); add_button (section, "wait"); section = add_section (box, "Selection"); add_button (section, "cell"); add_button (section, "crosshair"); add_button (section, "text"); add_button (section, "vertical-text"); section = add_section (box, "Drag & Drop"); add_button (section, "alias"); add_button (section, "copy"); add_button (section, "move"); add_button (section, "no-drop"); add_button (section, "not-allowed"); add_button (section, "grab"); add_button (section, "grabbing"); section = add_section (box, "Resize & Scrolling"); add_button (section, "all-scroll"); add_button (section, "col-resize"); add_button (section, "row-resize"); add_button (section, "n-resize"); add_button (section, "e-resize"); add_button (section, "s-resize"); add_button (section, "w-resize"); add_button (section, "ne-resize"); add_button (section, "nw-resize"); add_button (section, "se-resize"); add_button (section, "sw-resize"); add_button (section, "ew-resize"); add_button (section, "ns-resize"); add_button (section, "nesw-resize"); add_button (section, "nwse-resize"); section = add_section (box, "Zoom"); add_button (section, "zoom-in"); add_button (section, "zoom-out"); } if (!gtk_widget_get_visible (window)) { gtk_widget_show_all (window); } else { gtk_widget_destroy (window); window = NULL; } return window; }
tree_type_t *tree_load(char *name) { tree_type_t *res = malloc(sizeof(tree_type_t) + 300 * sizeof(tree_section_t)); assert(name && strlen(name) < TREE_NAME_SZ); strcpy(res->name, name); res->section_count=0; float branch_data[][3]= { {0.00, 1.00, 1.00}, {0.06, 1.33, 1.03}, {0.11, 0.90, 1.20}, {0.18, 1.33, 0.57}, {0.28, 0.57, 0.93}, {0.53, 0.57, 0.84}, {0.62, 0.31, 1.07}, {0.74, 0.64, 0.80}, {0.91, 0.38, 0.80}, {1.00, 0.70, 0.70} } ; tree_section_type_t *t1 = malloc(sizeof(tree_section_type_t) + 10*sizeof(float[3])); t1->count=10; memcpy(t1->data, branch_data, 10*sizeof(float[3])); #include "tree_branch_data.c" /* Stem */ add_section( res, 0, 0, 0.0, 0.0, 0.0); add_section( res, stem1_tid, 0.77, -0.1, -0.06, 1.99); add_section( res, stem3_tid, 0.55, -0.1, -1.09, 2.89); add_section( res, stem2_tid, 0.33, 0.1, 0.0, 4.21); add_section( res, stem3_tid, 0.21, 0.2, 1.38, 3.10); add_section( res, stem3_tid, 0.18, 0.3, 0.65, 2.31); add_section( res, stem3_tid, 0.15, 0.4, -0.11, 3.22); add_section( res, branch2_tid, 0.09, 0.5, 0.13, 3.6); add_section( res, branch4_tid, 0.08, 0.53, 0.32, 3.41); add_section( res, branch4_tid, 0.07, 0.55, 0.25,3.34); /* Branch 1 */ add_section( res, 0, 0, 0.13, 0.0, 0.91); add_section( res, branch1_tid, 0.15, 0.84, 0.0, 2.0); add_section( res, branch2_tid, 0.05, 0.55, 0.0, 2.08); add_section( res, branch2_tid, 0.06, 0.39, 0.0, 1.83); add_section( res, branch5_tid, 0.05, 0.53, 0.0, 1.81); add_section( res, 0, 0, 0.75, 0.0, 1.87); add_section( res, branch2_tid, 0.05, 1.26, 0.0, 1.59); add_section( res, branch2_tid, 0.04, 0.95, 0.0, 1.11 ); add_section( res, branch2_tid, 0.04, 0.7, 0.0, 1.2); add_section( res, 0,0, 0.91, 0.0, 1.75); add_section( res, branch3_tid, 0.04, 1.22, 0.01, 1.85); add_section( res, branch2_tid, 0.03, 1.25, 0.02, 1.75); /* Branch 2 */ add_section( res, 0,0, -0.13, 0.0, 1.21); add_section( res, branch1_tid, 0.1, -0.84, 0.0, 2.5); add_section( res, branch2_tid, 0.03, -0.55, 0.0, 2.58); add_section( res, branch2_tid, 0.025, -0.39, 0.1, 1.83+0.5); add_section( res, 0,0, -0.75, 0.0, 1.84+0.5); add_section( res, branch2_tid, 0.035, -1.26, 0.0, 1.59+0.5); add_section( res, branch2_tid, 0.03, -0.95, 0.0, 1.11+0.5 ); add_section( res, branch2_tid, 0.03, -0.7, 0.0, 1.2+0.5); add_section( res, branch2_tid, 0.025, -0.75, 0.0, 1.3+0.5); add_section( res, 0,0, -0.91, 0.0, 1.75+0.5); add_section( res, branch3_tid, 0.04, -1.22, 0.01, 1.85+0.5); add_section( res, branch2_tid, 0.03, -1.25, 0.02, 1.75+0.5); /* Branch 3 */ add_section( res, 0,0, 0.0, -0.48, 2.23); add_section( res, branch2_tid, 0.1, 0.0, -1.44, 2.35); add_section( res, branch3_tid, 0.1, 0.0, -1.5, 2.6); add_section( res, branch2_tid, 0.05, 0.0, -1.29, 2.63); add_section( res, branch2_tid, 0.04, 0.0, -1.23, 2.53 ); /* Branch 4*/ add_section( res, 0,0, 0.0, -0.69, 3.4); add_section( res, branch1_tid, 0.15, 0.05, -0.8, 4.64); add_section( res, branch2_tid, 0.06, 0.05, -0.28, 4.52); add_section( res, branch2_tid, 0.04, 0.05, -0.35, 4.26); add_section( res, branch2_tid, 0.03, 0.05, -0.53, 4.24); /* Branch 5 */ add_section( res, 0, 0, 0.0, -1.08, 2.86); add_section( res, branch1_tid, 0.10, -0.3, -1.59, 3.56); add_section( res, branch2_tid, 0.04, -0.4, -1.38, 3.79); add_section( res, branch2_tid, 0.03, -0.45, -1.22, 3.5); add_section( res, 0,0, 0.035, -0.70, 4.27); add_section( res, branch1_tid, 0.10, 0.035, -1.65, 4.24); add_section( res, branch2_tid, 0.04, 0.05, -1.7, 4.46); add_section( res, branch2_tid, 0.03, 0.08, -1.53, 4.46); add_section( res, 0,0, 0.35, 0.39, 2.62); add_section( res, branch4_tid, 0.15, 0.15, -0.3, 2.66); add_section( res, branch2_tid, 0.03, 0.05, -0.41, 2.99); add_section( res, branch2_tid, 0.02, 0.05, -0.08, 2.99); /* add_section( res, branch2_tid, 0.025, 0.03, 0.08, -1.53, 4.46); */ /* Branch 6 */ add_section( res, 0,0, 0.15, 0.06, 4.19); add_section( res, branch1_tid, 0.1, 0.15, 1.08, 4.22); add_section( res, branch2_tid, 0.02, 0.15, 1.07, 4.34); add_section( res, 0,0, 0.15, 0.70, 4.21); add_section( res, branch2_tid, 0.05, 0.40, 0.78, 4.22); add_section( res, branch2_tid, 0.02, 0.45, 0.27, 4.18); add_section( res, branch2_tid, 0.02, 0.65, 0.31, 4.17); /* Branch 7 */ add_section( res, 0,0, 0.2, 1.35, 3.16); add_section( res, branch4_tid, 0.15, 0.2, 1.55, 2.5); add_section( res, branch2_tid, 0.02, 0.2, 1.33, 2.31); /* Roots */ add_section( res, 0,0, 0.0, 0.5* -0.29, 0.16); add_section( res, root1_tid, 0.16, 0.0, -0.86, 0.08 ); add_section( res, 0,0, 0.5*-0.27, 0.5*-0.08, 0.16); add_section( res, root1_tid, 0.16, -0.81, -0.26, 0.08 ); add_section( res, 0,0, 0.5*-0.17, 0.5*0.234, 0.16); add_section( res, root1_tid, 0.16, -0.5, 0.69, 0.08 ); add_section( res, 0,0, 0.5*0.17, 0.5*0.234, 0.16); add_section( res, root1_tid, 0.16, 0.5, 0.69, 0.08 ); add_section( res, 0,0, 0.5*0.27, 0.5*-0.08, 0.16); add_section( res, root1_tid, 0.16, 0.81, -0.69, 0.08 ); return res; }
/* build a complete fake dll from scratch */ static BOOL build_fake_dll( HANDLE file, const WCHAR *name ) { static const WCHAR dotexeW[] = { '.','e','x','e',0 }; IMAGE_DOS_HEADER *dos; IMAGE_NT_HEADERS *nt; struct dll_info info; const WCHAR *ext; BYTE *buffer; BOOL ret = FALSE; DWORD lfanew = (sizeof(*dos) + sizeof(fakedll_signature) + 15) & ~15; DWORD size, header_size = lfanew + sizeof(*nt); info.handle = file; buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, header_size + 8 * sizeof(IMAGE_SECTION_HEADER) ); dos = (IMAGE_DOS_HEADER *)buffer; dos->e_magic = IMAGE_DOS_SIGNATURE; dos->e_cblp = sizeof(*dos); dos->e_cp = 1; dos->e_cparhdr = lfanew / 16; dos->e_minalloc = 0; dos->e_maxalloc = 0xffff; dos->e_ss = 0x0000; dos->e_sp = 0x00b8; dos->e_lfarlc = lfanew; dos->e_lfanew = lfanew; memcpy( dos + 1, fakedll_signature, sizeof(fakedll_signature) ); nt = info.nt = (IMAGE_NT_HEADERS *)(buffer + lfanew); /* some fields are copied from the source dll */ #if defined __x86_64__ nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64; #elif defined __aarch64__ nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64; #elif defined __arm__ nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT; #elif defined __powerpc__ nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC; #else nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386; #endif nt->FileHeader.TimeDateStamp = 0; nt->FileHeader.Characteristics = 0; nt->OptionalHeader.MajorLinkerVersion = 1; nt->OptionalHeader.MinorLinkerVersion = 0; nt->OptionalHeader.MajorOperatingSystemVersion = 1; nt->OptionalHeader.MinorOperatingSystemVersion = 0; nt->OptionalHeader.MajorImageVersion = 1; nt->OptionalHeader.MinorImageVersion = 0; nt->OptionalHeader.MajorSubsystemVersion = 4; nt->OptionalHeader.MinorSubsystemVersion = 0; nt->OptionalHeader.Win32VersionValue = 0; nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI; nt->OptionalHeader.DllCharacteristics = 0; nt->OptionalHeader.SizeOfStackReserve = 0; nt->OptionalHeader.SizeOfStackCommit = 0; nt->OptionalHeader.SizeOfHeapReserve = 0; nt->OptionalHeader.SizeOfHeapCommit = 0; /* other fields have fixed values */ nt->Signature = IMAGE_NT_SIGNATURE; nt->FileHeader.NumberOfSections = 0; nt->FileHeader.SizeOfOptionalHeader = IMAGE_SIZEOF_NT_OPTIONAL_HEADER; nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC; nt->OptionalHeader.ImageBase = 0x10000000; nt->OptionalHeader.SectionAlignment = section_alignment; nt->OptionalHeader.FileAlignment = file_alignment; nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES; header_size = (BYTE *)(nt + 1) - buffer; info.mem_pos = ALIGN( header_size, section_alignment ); info.file_pos = ALIGN( header_size, file_alignment ); nt->OptionalHeader.AddressOfEntryPoint = info.mem_pos; nt->OptionalHeader.BaseOfCode = info.mem_pos; ext = strrchrW( name, '.' ); if (!ext || strcmpiW( ext, dotexeW )) nt->FileHeader.Characteristics |= IMAGE_FILE_DLL; if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) { size = sizeof(dll_code_section); if (!xwrite( &info, dll_code_section, size, info.file_pos )) goto done; } else { size = sizeof(exe_code_section); if (!xwrite( &info, exe_code_section, size, info.file_pos )) goto done; } nt->OptionalHeader.SizeOfCode = size; add_section( &info, ".text", size, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ ); if (!xwrite( &info, &reloc_section, sizeof(reloc_section), info.file_pos )) goto done; add_directory( &info, IMAGE_DIRECTORY_ENTRY_BASERELOC, info.mem_pos, sizeof(reloc_section) ); add_section( &info, ".reloc", sizeof(reloc_section), IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_READ ); header_size += nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER); nt->OptionalHeader.SizeOfHeaders = ALIGN( header_size, file_alignment ); nt->OptionalHeader.SizeOfImage = ALIGN( info.mem_pos, section_alignment ); ret = xwrite( &info, buffer, header_size, 0 ); done: HeapFree( GetProcessHeap(), 0, buffer ); return ret; }
static void read_section(struct GlobalVars *gv,struct ObjectUnit *u, uint32_t index,struct vobj_symbol *vsyms,int nsyms) { struct Section *s; lword dsize,fsize; int nrelocs; uint8_t type = ST_DATA; uint8_t prot = SP_READ; uint8_t flags = 0; uint8_t align,*data; char *attr; char *name = p; struct Reloc *last_reloc; int last_sym = -1; lword last_offs; uint16_t last_bpos = INVALID; skip_string(); /* section name */ for (attr=p; *attr; attr++) { switch (tolower((unsigned char)*attr)) { case 'w': prot |= SP_WRITE; break; case 'x': prot |= SP_EXEC; break; case 'c': type = ST_CODE; break; case 'd': type = ST_DATA; break; case 'u': type = ST_UDATA; flags |= SF_UNINITIALIZED; break; case 'a': flags |= SF_ALLOC; } } skip_string(); read_number(); /* ignore flags */ align = (uint8_t)lshiftcnt(read_number()); dsize = read_number(); /* total size of section */ nrelocs = (int)read_number(); /* number of relocation entries */ fsize = read_number(); /* size in file, without 0-bytes */ if (type == ST_UDATA) { data = NULL; } else if (dsize > fsize) { /* recreate 0-bytes at end of section */ data = alloczero((size_t)dsize); memcpy(data,p,(size_t)fsize); } else data = p; /* create and add section */ p += fsize; s = add_section(u,name,data,(unsigned long)dsize,type,flags,prot,align,0); s->id = index; /* create relocations and unkown symbol references for this section */ for (last_reloc=NULL,last_offs=-1; nrelocs>0; nrelocs--) { struct Reloc *r; char *xrefname = NULL; lword offs,mask,addend; uint16_t bpos,bsiz; uint8_t flags; int sym_idx; /* read one relocation entry */ type = (uint8_t)read_number(); offs = read_number(); bpos = (uint16_t)read_number(); bsiz = (uint16_t)read_number(); mask = read_number(); addend = read_number(); sym_idx = (int)read_number() - 1; /* symbol index */ flags = 0; if (type>R_NONE && type<=LAST_STANDARD_RELOC && offs>=0 && bsiz<=(sizeof(lword)<<3) && sym_idx>=0 && sym_idx<nsyms) { if (vsyms[sym_idx].type == LABSYM) { xrefname = NULL; index = vsyms[sym_idx].sec; } else if (vsyms[sym_idx].type == IMPORT) { xrefname = vsyms[sym_idx].name; if (vsyms[sym_idx].flags & WEAK) flags |= RELF_WEAK; /* undefined weak symbol */ index = 0; } else { /* VOBJ relocation not supported */ error(115,getobjname(u),fff[u->lnkfile->format]->tname, (int)type,(lword)offs,(int)bpos,(int)bsiz,(lword)mask, vsyms[sym_idx].name,(int)vsyms[sym_idx].type); } if (sym_idx==last_sym && offs==last_offs && bpos==last_bpos && last_reloc!=NULL) { r = last_reloc; } else { r = newreloc(gv,s,xrefname,NULL,index,(unsigned long)offs,type,addend); r->flags |= flags; last_reloc = r; last_offs = offs; last_bpos = bpos; last_sym = sym_idx; } addreloc(s,r,bpos,bsiz,mask); /* make sure that section reflects the addend for other formats */ writesection(gv,data+(uint32_t)offs,r,addend); } else if (type != R_NONE) { /* VOBJ relocation not supported */ error(115,getobjname(u),fff[u->lnkfile->format]->tname, (int)type,(lword)offs,(int)bpos,(int)bsiz,(lword)mask, (sym_idx>=0&&sym_idx<nsyms) ? vsyms[sym_idx].name : "?", (sym_idx>=0&&sym_idx<nsyms) ? (int)vsyms[sym_idx].type : 0); } } }
// Puts "field" from "section" from .ini file "f" into "s". // If "section" does not exist or "field" does not exist in // section then s=""; void ini_fgets(FILE *f, const char *section, const char *field, char *s) { int i; long start_pos,string_start_pos; char ts[INI_STRING_SIZE*2]; if (f!=current_file) reset_buffer(f); // Default to "Not found" s[0]=0; // See if section is in buffer for (i=0;i<NUM_SECTION_BUFFERS;i++) if (strnicmp(section_buffers[i].name,section,MAX_SECTION_WIDTH)==0) break; // If section is in buffer, seek to it if necessary if (i<NUM_SECTION_BUFFERS) { if (i!=current_section) { current_section=i; fseek(f,section_buffers[i].offset,SEEK_SET); } } // else look through .ini file for it. else { // Make sure we are not at eof or this will cause trouble. if (feof(f)) rewind(f); start_pos=ftell(f); while (1) { stripped_fgets(ts,INI_STRING_SIZE*2,f); // If it is a section, add it to the section buffer if (is_section(ts,"*")) current_section=add_section(ts,ftell(f)); // If it is the section we are looking for, break. if (is_section(ts,section)) break; // If we reach the end of the file, rewind to the start. if (feof(f)) rewind(f); if (ftell(f)==start_pos) return; } } // See if field is in buffer for (i=0;i<NUM_FIELD_BUFFERS;i++) if (field_buffers[i].section==current_section) if (strnicmp(field_buffers[i].name,field,MAX_FIELD_WIDTH)==0) break; // If field is in buffer, seek to it and read it if (i<NUM_FIELD_BUFFERS) { fseek(f,field_buffers[i].offset,SEEK_SET); stripped_fgets(ts,INI_STRING_SIZE*2,f); get_field_string(s,ts); } else // else search through section for field. { // Make sure we do not start at eof or this will cause problems. if (feof(f)) fseek(f,section_buffers[current_section].offset,SEEK_SET); start_pos=ftell(f); while (1) { string_start_pos=ftell(f); stripped_fgets(ts,INI_STRING_SIZE*2,f); // If it is a field, add it to the buffer if (is_field(ts,"*")) add_field(ts,current_section,string_start_pos); // If it is the field we are looking for, save it if (is_field(ts,field)) { get_field_string(s,ts); break; } // If we reach the end of the section, start over if (feof(f) || is_section(ts,"*")) fseek(f,section_buffers[current_section].offset,SEEK_SET); if (ftell(f)==start_pos) return; } } }
/* build a complete fake dll, optionally using module as a source */ static BOOL build_fake_dll( HANDLE file, HMODULE module ) { IMAGE_DOS_HEADER *dos; IMAGE_NT_HEADERS *nt; const IMAGE_NT_HEADERS *src_nt = NULL; struct dll_info info; BYTE *buffer; BOOL ret = FALSE; DWORD lfanew = (sizeof(*dos) + sizeof(fakedll_signature) + 15) & ~15; DWORD size, header_size = lfanew + sizeof(*nt); info.handle = file; buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, header_size + 8 * sizeof(IMAGE_SECTION_HEADER) ); dos = (IMAGE_DOS_HEADER *)buffer; dos->e_magic = IMAGE_DOS_SIGNATURE; dos->e_cblp = sizeof(*dos); dos->e_cp = 1; dos->e_cparhdr = lfanew / 16; dos->e_minalloc = 0; dos->e_maxalloc = 0xffff; dos->e_ss = 0x0000; dos->e_sp = 0x00b8; dos->e_lfarlc = lfanew; dos->e_lfanew = lfanew; memcpy( dos + 1, fakedll_signature, sizeof(fakedll_signature) ); nt = info.nt = (IMAGE_NT_HEADERS *)(buffer + lfanew); src_nt = RtlImageNtHeader( module ); /* some fields are copied from the source dll */ #define SET(field,def) nt->field = src_nt ? src_nt->field : def SET( FileHeader.Machine, IMAGE_FILE_MACHINE_I386 ); SET( FileHeader.TimeDateStamp, 0 ); SET( FileHeader.Characteristics, IMAGE_FILE_DLL ); SET( OptionalHeader.MajorLinkerVersion, 1 ); SET( OptionalHeader.MinorLinkerVersion, 0 ); SET( OptionalHeader.MajorOperatingSystemVersion, 1 ); SET( OptionalHeader.MinorOperatingSystemVersion, 0 ); SET( OptionalHeader.MajorImageVersion, 1 ); SET( OptionalHeader.MinorImageVersion, 0 ); SET( OptionalHeader.MajorSubsystemVersion, 4 ); SET( OptionalHeader.MinorSubsystemVersion, 0 ); SET( OptionalHeader.Win32VersionValue, 0 ); SET( OptionalHeader.Subsystem, IMAGE_SUBSYSTEM_WINDOWS_GUI ); SET( OptionalHeader.DllCharacteristics, 0 ); SET( OptionalHeader.SizeOfStackReserve, 0 ); SET( OptionalHeader.SizeOfStackCommit, 0 ); SET( OptionalHeader.SizeOfHeapReserve, 0 ); SET( OptionalHeader.SizeOfHeapCommit, 0 ); #undef SET /* other fields have fixed values */ nt->Signature = IMAGE_NT_SIGNATURE; nt->FileHeader.NumberOfSections = 0; nt->FileHeader.SizeOfOptionalHeader = IMAGE_SIZEOF_NT_OPTIONAL_HEADER; nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC; nt->OptionalHeader.ImageBase = 0x10000000; nt->OptionalHeader.SectionAlignment = section_alignment; nt->OptionalHeader.FileAlignment = file_alignment; nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES; header_size = (BYTE *)(nt + 1) - buffer; info.mem_pos = ALIGN( header_size, section_alignment ); info.file_pos = ALIGN( header_size, file_alignment ); nt->OptionalHeader.AddressOfEntryPoint = info.mem_pos; nt->OptionalHeader.BaseOfCode = info.mem_pos; if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL) { size = sizeof(dll_code_section); if (!xwrite( &info, dll_code_section, size, info.file_pos )) goto done; } else { size = sizeof(exe_code_section); if (!xwrite( &info, exe_code_section, size, info.file_pos )) goto done; } nt->OptionalHeader.SizeOfCode = size; add_section( &info, ".text", size, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ ); if (!xwrite( &info, &reloc_section, sizeof(reloc_section), info.file_pos )) goto done; add_directory( &info, IMAGE_DIRECTORY_ENTRY_BASERELOC, info.mem_pos, sizeof(reloc_section) ); add_section( &info, ".reloc", sizeof(reloc_section), IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_READ ); if (!add_version_resource( module, &info )) goto done; header_size += nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER); nt->OptionalHeader.SizeOfHeaders = ALIGN( header_size, file_alignment ); nt->OptionalHeader.SizeOfImage = ALIGN( info.mem_pos, section_alignment ); ret = xwrite( &info, buffer, header_size, 0 ); done: HeapFree( GetProcessHeap(), 0, buffer ); return ret; }
static int write_elf(const struct rmod_context *ctx, const struct buffer *in, struct buffer *out) { int ret; int bit64; size_t loc; size_t rmod_data_size; struct elf_writer *ew; struct buffer rmod_data; struct buffer rmod_header; struct buffer program; struct buffer relocs; Elf64_Xword total_size; Elf64_Addr addr; Elf64_Ehdr ehdr; bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64; /* * 3 sections will be added to the ELF file. * +------------------+ * | rmodule header | * +------------------+ * | program | * +------------------+ * | relocations | * +------------------+ */ /* Create buffer for header and relocations. */ rmod_data_size = sizeof(struct rmodule_header); if (bit64) rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr); else rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr); if (buffer_create(&rmod_data, rmod_data_size, "rmod")) return -1; buffer_splice(&rmod_header, &rmod_data, 0, sizeof(struct rmodule_header)); buffer_clone(&relocs, &rmod_data); buffer_seek(&relocs, sizeof(struct rmodule_header)); /* Reset current location. */ buffer_set_size(&rmod_header, 0); buffer_set_size(&relocs, 0); /* Program contents. */ buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz); /* Create ELF writer with modified entry point. */ memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr)); ew = elf_writer_init(&ehdr); if (ew == NULL) { ERROR("Failed to create ELF writer.\n"); buffer_delete(&rmod_data); return -1; } /* Write out rmodule_header. */ ctx->xdr->put16(&rmod_header, RMODULE_MAGIC); ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1); ctx->xdr->put8(&rmod_header, 0); /* payload_begin_offset */ loc = sizeof(struct rmodule_header); ctx->xdr->put32(&rmod_header, loc); /* payload_end_offset */ loc += ctx->phdr->p_filesz; ctx->xdr->put32(&rmod_header, loc); /* relocations_begin_offset */ ctx->xdr->put32(&rmod_header, loc); /* relocations_end_offset */ if (bit64) loc += ctx->nrelocs * sizeof(Elf64_Addr); else loc += ctx->nrelocs * sizeof(Elf32_Addr); ctx->xdr->put32(&rmod_header, loc); /* module_link_start_address */ ctx->xdr->put32(&rmod_header, ctx->phdr->p_vaddr); /* module_program_size */ ctx->xdr->put32(&rmod_header, ctx->phdr->p_memsz); /* module_entry_point */ ctx->xdr->put32(&rmod_header, ctx->pelf.ehdr.e_entry); /* parameters_begin */ ctx->xdr->put32(&rmod_header, ctx->parameters_begin); /* parameters_end */ ctx->xdr->put32(&rmod_header, ctx->parameters_end); /* bss_begin */ ctx->xdr->put32(&rmod_header, ctx->bss_begin); /* bss_end */ ctx->xdr->put32(&rmod_header, ctx->bss_end); /* padding[4] */ ctx->xdr->put32(&rmod_header, 0); ctx->xdr->put32(&rmod_header, 0); ctx->xdr->put32(&rmod_header, 0); ctx->xdr->put32(&rmod_header, 0); /* Write the relocations. */ for (unsigned i = 0; i < ctx->nrelocs; i++) { if (bit64) ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]); else ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]); } total_size = 0; addr = 0; /* * There are 2 cases to deal with. The program has a large NOBITS * section and the relocations can fit entirely within occupied memory * region for the program. The other is that the relocations increase * the memory footprint of the program if it was loaded directly into * the region it would run. The rmdoule header is a fixed cost that * is considered a part of the program. */ total_size += buffer_size(&rmod_header); if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) { total_size += buffer_size(&relocs); total_size += ctx->phdr->p_filesz; } else { total_size += ctx->phdr->p_memsz; } ret = add_section(ew, &rmod_header, ".header", addr, buffer_size(&rmod_header)); if (ret < 0) goto out; addr += buffer_size(&rmod_header); ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz); if (ret < 0) goto out; addr += ctx->phdr->p_filesz; if (ctx->nrelocs) { ret = add_section(ew, &relocs, ".relocs", addr, buffer_size(&relocs)); if (ret < 0) goto out; addr += buffer_size(&relocs); } if (total_size != addr) { ret = add_section(ew, NULL, ".empty", addr, total_size - addr); if (ret < 0) goto out; } /* * Ensure last section has a memory usage that meets the required * total size of the program in memory. */ ret = elf_writer_serialize(ew, out); if (ret < 0) ERROR("Failed to serialize ELF to buffer.\n"); out: buffer_delete(&rmod_data); elf_writer_destroy(ew); return ret; }
void Config::set(const char* section, const char* key, const char* value) { ConfigSection* sc = add_section(section); // it will create entry, if needed sc->add_entry(key, value); }
void Config::set(const char* section, const char* key, bool value) { ConfigSection* sc = add_section(section); const char* v = ((value) ? "1" : "0"); sc->add_entry(key, v); }
static int process_config(ConfigSection **current_section, const char *source_descr, FILE *f) #endif { int state = ST_INITIAL; int ch; char sectionname[MAXSECTIONLABELLENGTH+1]; int sectionname_pos = 0; char keyname[MAXKEYNAMELENGTH+1]; int keyname_pos = 0; char value[MAXVALUELENGTH+1]; int value_pos = 0; int escape = 0; ConfigKey *k; int line_nr = 1; int error = 0; #if !defined(LCDPROC_CONFIG_READ_STRING) if (f == NULL) return(0); #endif while (state != ST_END) { #if defined(LCDPROC_CONFIG_READ_STRING) ch = (f != NULL) ? get_next_char(f) : get_next_char(); #else ch = fgetc(f); if (ch == EOF) ch = '\0'; #endif /* Secretly keep count of the line numbers */ if (ch == '\n') line_nr++; switch (state) { case ST_INITIAL: switch (ch) { case '#': case ';': /* comment start */ state = ST_COMMENT; /* fall through */ case '\0': case '\n': case '\r': case '\t': case ' ': /* ignore spaces */ break; case '[': /* section name */ state = ST_SECTIONLABEL; sectionname_pos = 0; sectionname[sectionname_pos] = '\0'; break; default: /* key word */ state = ST_KEYNAME; keyname_pos = 0; keyname[keyname_pos++] = ch; keyname[keyname_pos] = '\0'; } break; case ST_SECTIONLABEL: /* section label: "["{non-space chars}+"]" */ switch (ch) { case '\0': case '\n': /* premature end of label */ report(RPT_WARNING, "Unterminated section label on line %d of %s: %s", line_nr, source_descr, sectionname); error = 1; state = ST_INITIAL; /* already at the end, no resync required */ break; case ']': /* label terminated: find/create section */ if (!(*current_section = find_section(sectionname))) { *current_section = add_section(sectionname); } state = ST_SECTIONLABEL_DONE; break; // case '\r': // case '\t': // case ' ': // /* no spaces allowed in section labels WHY? */ // report(RPT_WARNING, "Invalid character in section label on line %d of %s: %s", // line_nr, source_descr, sectionname); // error = 1; // state = ST_INVALID_SECTIONLABEL; /* resync required */ // break; default: /* append char to section label */ if (sectionname_pos < MAXSECTIONLABELLENGTH) { sectionname[sectionname_pos++] = ch; sectionname[sectionname_pos] = '\0'; break; } report(RPT_WARNING, "Section name too long on line %d of %s: %s", line_nr, source_descr, sectionname); error = 1; state = ST_INVALID_SECTIONLABEL; /* resync required */ } break; case ST_KEYNAME: /* key name: {non-space chars}+ */ switch (ch) { case '\r': case '\t': case ' ': /* ignore trailing spaces */ if (keyname_pos != 0) state = ST_ASSIGNMENT; break; case '\0': case '\n': /* premature end of line */ report(RPT_WARNING, "Loose word found on line %d of %s: %s", line_nr, source_descr, keyname); error = 1; state = ST_INITIAL; /* already at the end; no resync required */ break; case '=': /* end of key reached, "=" found, now we need a value */ state = ST_VALUE; value[0] = '\0'; value_pos = 0; break; // case '"': // case '[': // case ']': // /* character invalid in key names WHY ? */ // report(RPT_WARNING, "Invalid character in key name on line %d of %s: %s", // line_nr, source_descr, keyname); // error = 1; // state = ST_INVALID_KEYNAME; /* resync required */ // break; default: /* append char to key name */ if (keyname_pos < MAXKEYNAMELENGTH) { keyname[keyname_pos++] = ch; keyname[keyname_pos] = '\0'; break; } report(RPT_WARNING, "Key name too long on line %d of %s: %s", line_nr, source_descr, keyname); error = 1; state = ST_INVALID_KEYNAME; /* resync required */ } break; case ST_ASSIGNMENT: /* assignment: "=" */ switch (ch) { case '\t': case ' ': /* ignore leading spaces */ break; case '=': /* "=" found, now we need a value */ state = ST_VALUE; value[0] = '\0'; value_pos = 0; break; default: report(RPT_WARNING, "Assignment expected on line %d of %s: %s", line_nr, source_descr, keyname); error = 1; state = ST_INVALID_ASSIGNMENT; } break; case ST_VALUE: /* value: {non-space char}+ | "\""{any potentially-quoted char}+"\"" */ switch (ch) { case '#': case ';': /* allow comment if we already had a value */ /* WHY ONLY THEN ? 'xx=' can be seen as equivalent to 'xx=""' */ if (value_pos > 0) { state = ST_COMMENT; break; } /* fall through */ case '[': case ']': case '=': /* illegal characters WHY? */ report(RPT_WARNING, "Invalid character '%c' in value on line %d of %s, at key: %s", ch, line_nr, source_descr, keyname); error = 1; state = ST_INVALID_VALUE; break; case '\t': case ' ': /* ignore leading spaces */ if (value_pos == 0) break; /* fall through */ case '\0': case '\n': case '\r': /* value complete */ if (!*current_section) { report(RPT_WARNING, "Data outside sections on line %d of %s with key: %s", line_nr, source_descr, keyname); error = 1; } else { /* Store the value*/ k = add_key(*current_section, keyname, value); } /* And be ready for next thing...*/ state = ((ch == ' ') || (ch == '\t')) ? ST_VALUE_DONE : ST_INITIAL; break; case '"': /* quoted string */ state = ST_QUOTEDVALUE; break; default: /* append char to value */ if (value_pos < MAXVALUELENGTH) { value[value_pos++] = ch; value[value_pos] = '\0'; break; } report(RPT_WARNING, "Value too long on line %d of %s, at key: %s", line_nr, source_descr, keyname); error = 1; state = ST_INVALID_VALUE; } break; case ST_QUOTEDVALUE: /* a quoted part of a string */ switch (ch) { case '\0': case '\n': report(RPT_WARNING, "Premature end of quoted string on line %d of %s: %s", line_nr, source_descr, keyname); error = 1; state = ST_INITIAL; break; case '\\': if (!escape) { escape = 1; break; } /* fall through */ case '"': if (!escape) { state = ST_VALUE; break; } /* fall through */ default: if (escape) { switch (ch) { case 'a': ch = '\a'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; /* default: literal char (i.e. ignore '\') */ } escape = 0; } value[value_pos++] = ch; value[value_pos] = '\0'; } break; case ST_SECTIONLABEL_DONE: case ST_VALUE_DONE: switch (ch) { case ';': case '#': state = ST_COMMENT; break; case '\0': case '\n': state = ST_INITIAL; break; case '\t': case ' ': break; default: /* illegal characters */ report(RPT_WARNING, "Invalid character '%c' on line %d of %s", ch, line_nr, source_descr); error = 1; state = ST_INVALID_VALUE; } case ST_INVALID_SECTIONLABEL: /* invalid section label: resync up to end of label/next line */ if (ch == ']') state = ST_INITIAL; /* fall through */ case ST_INVALID_ASSIGNMENT: case ST_INVALID_KEYNAME: case ST_INVALID_VALUE: case ST_COMMENT: /* comment or error: ignore anything up to the next line */ if (ch == '\n') state = ST_INITIAL; } if (ch == '\0') { if ((!error) && (state != ST_INITIAL) && (state != ST_COMMENT) && (state != ST_SECTIONLABEL_DONE) && (state != ST_VALUE_DONE)) { report(RPT_WARNING, "Premature end of configuration on line %d of %s: %d", line_nr, source_descr, state); error = 1; } state = ST_END; } } return (error) ? -1 : 0; }
void Config::set(const char* section, const char* key, long value) { ConfigSection* sc = add_section(section); char tmp[128]; snprintf(tmp, sizeof(tmp)-1, "%ld", value); sc->add_entry(key, tmp); }