Пример #1
0
void
run_symtable(
	struct exec *exhdr,
	struct nlist *symtabl,
	char *stringtable,
	int strtbl_sz,
	int sym_names)
{
	int i, count;

	if (NULL == exhdr || NULL == symtabl || NULL == stringtable) {
		if (NULL == exhdr) fprintf(stderr, "a.out header NULL, ");
		if (NULL == symtabl) fprintf(stderr, "symbol table NULL, ");
		if (NULL == stringtable) fprintf(stderr, "string table NULL, ");
		fprintf(stderr, " can't possibly list the symbol table meaningfully\n");
		return;
	}

	count = exhdr->a_syms/sizeof(*symtabl);

	printf("%d symbols\n", count);

	for (i = 0; i < count; ++i) {
		if (!sym_names)
			printf("%4d name offset (%d), type 0x%x - %s, other 0x%x, desc 0x%x, value 0x%x (%d)\n",
				i,
				symtabl[i].n_un.n_strx,
				symtabl[i].n_type,
				symbol_type(&symtabl[i]),
				symtabl[i].n_other,
				symtabl[i].n_desc,
				symtabl[i].n_value,
				symtabl[i].n_value
			);
		else {
			char *sname = "";

			if (symtabl[i].n_un.n_strx >= 0 && symtabl[i].n_un.n_strx < strtbl_sz)
				sname = &stringtable[symtabl[i].n_un.n_strx];

			printf("%4d name \"%s\" (%d), type 0x%x - %s, other 0x%x, desc 0x%x, value 0x%x (%d)\n",
				i,
				sname,
				symtabl[i].n_un.n_strx,
				symtabl[i].n_type,
				symbol_type(&symtabl[i]),
				symtabl[i].n_other,
				symtabl[i].n_desc,
				symtabl[i].n_value,
				symtabl[i].n_value
			);
		}
	}
}
Пример #2
0
long dest_replace (SymbolEntry * z) {
    char tmp[1];

    if (z == NULL)
        quad_array[nextquad-1].dest = "-";
    else
        if (z->entryType == ENTRY_TEMPORARY)
            if (z->u.eTemporary.type->kind == TYPE_POINTER && quad_array[nextquad].type != ARRAY_QUAD) {
                char bufferz[256];
                sprintf(bufferz, "[%s]", (char *) z->id);
                quad_array[nextquad-1].dest = strdup(bufferz);
            } else
                quad_array[nextquad-1].dest = (char *) z->id;
        else
            quad_array[nextquad-1].dest = (char *) z->id;

    quad_array[nextquad-1].dest_req.type = symbol_type (z);
    quad_array[nextquad-1].dest_req.pm = symbol_pm (z);
    quad_array[nextquad-1].dest_req.kind = symbol_kind (z);
    quad_array[nextquad-1].dest_req.offset = symbol_offset (z);
    if (z != NULL)
        sprintf(tmp, "%u", z->nestingLevel);
    else
        sprintf(tmp, "-");
    quad_array[nextquad-1].dest_req.nesting = strdup(tmp);
    return nextquad-1;
}
Пример #3
0
int			output_64(void *l, char *st, t_list *sl)
{
	char				s;
	uint64_t			v;
	uint64_t			addr;
	uint64_t			name;
	struct nlist_64		*list;

	list = (struct nlist_64*)l;
	addr = 1;
	v = list->n_value;
	s = symbol_type(list->n_value, list->n_sect, list->n_type, sl);
	name = 1;
	if (check_option(&addr, &s) == SUCCESS)
		return (SUCCESS);
	if (addr)
		addr = (s != 'U') ? ft_printf("%.16llx ", v) : ft_printf("%16s ", "");
	if (s)
		ft_printf("%c ", s);
	if (name)
		ft_printf("%s\n", st + list->n_un.n_strx);
	return (SUCCESS);
}
Пример #4
0
int main(int argc, char *argv[])
{
    if (argc != 2) {
        fprintf(stderr, "usage: %s /path/to/elf64_file\n", argv[0]);
        return 1;
    }

    int fd;
    struct stat buf;
    u8 *f;

    fd = open(argv[1], O_RDONLY);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    if (fstat(fd, &buf) == -1) {
        perror("fstat");
        return 1;
    }

    f = mmap(NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
    if (f == MAP_FAILED) {
        perror("mmap");
        return 1;
    }

    Elf64_Ehdr *elf_header = (Elf64_Ehdr *)f;
    printf("ELF header:\n");
    printf("    header size = %#x\n", elf_header->e_ehsize);
    printf("    program header table at = %#lx, entry count = %d, entry size = %#x\n",
                                    elf_header->e_phoff,
                                    elf_header->e_phnum,
                                    elf_header->e_phentsize);
    printf("    section header table at = %#lx, entry count = %d, entry size = %#x\n",
                                    elf_header->e_shoff,
                                    elf_header->e_shnum,
                                    elf_header->e_shentsize);
    printf("    e_entry = %#lx\n", elf_header->e_entry);
    printf("    e_type  = %#x (ET_EXEC = %#x, ET_DYN = %#x, ET_CORE = %#x)\n",
                                elf_header->e_type,
                                ET_EXEC,
                                ET_DYN,
                                ET_CORE);
    printf("    e_machine = %#x (ET_X86_64 = %#x)\n", elf_header->e_machine, EM_X86_64);
    printf("    e_shstrndx = %d\n", elf_header->e_shstrndx);

    printf("\nProgram header table:\n");
    Elf64_Phdr *program_header_table = (Elf64_Phdr *)(f + elf_header->e_phoff);
    int i;
    Elf64_Phdr *ph;
    u8 *pt_note;
    int pt_note_count, pt_note_namesize, pt_note_descsize;
    Elf64_Dyn *dyn;
    for (i = 0; i < elf_header->e_phnum; i++) {
        ph = program_header_table + i;
        printf("    %4d: ", i);
        printf("type = %-16s", p_type(ph->p_type));
        if (ph->p_flags & PF_R) {
            printf("R");
        }
        if (ph->p_flags & PF_W) {
            printf("W");
        }
        if (ph->p_flags & PF_X) {
            printf("X");
        }
        printf("\t");
        printf("offset = %#lx size = %ld ", ph->p_offset, ph->p_filesz);
        if (ph->p_type == PT_LOAD) {
            printf("p_vaddr = %#lx p_memsz = %#lx ", ph->p_vaddr, ph->p_memsz);
        }
        printf("\n");
        if (ph->p_type == PT_INTERP) {
            printf("\t\t%s\n", (char *)(f + ph->p_offset));
        }
        if (ph->p_type == PT_NOTE) {
            pt_note = (u8 *)(f + ph->p_offset);
            while (pt_note_count < ph->p_filesz) {
                printf("\t\t");
                pt_note_namesize = (u32)(*pt_note);
                printf("name size = %d, ", pt_note_namesize);
                pt_note += 4;
                pt_note_count += 4;
                pt_note_descsize = (u32)(*pt_note);
                printf("desc size = %d, ", pt_note_descsize);
                pt_note += 4;
                pt_note_count += 4;
                printf("type = %d\n", (u32)(*pt_note));
                pt_note += 4;
                pt_note_count += 4;
                printf("\t\tname: ");
                while (pt_note_namesize--) {
                    printf("%#x ", *pt_note);
                    if (*pt_note) {
                        printf("(%c) ", *pt_note);
                    }
                    pt_note++;
                    pt_note_count++;
                }
                printf("\n");
                printf("\t\tdesc: ");
                while (pt_note_descsize--) {
                    printf("%#x ", *pt_note);
                    pt_note++;
                    pt_note_count++;
                }
                printf("\n");
            }
        } else if (ph->p_type == PT_DYNAMIC) {
            dyn = (Elf64_Dyn *)(f + ph->p_offset);
            while (dyn->d_tag != DT_NULL) {
                printf("\t\td_tag = %-20s d_un = %#lx\n", d_tag(dyn->d_tag), dyn->d_un.d_val);
                dyn = dyn + 1;
            }
        }
    }

    Elf64_Shdr *section_header_table = (Elf64_Shdr *)(f + elf_header->e_shoff);
    Elf64_Shdr *name_sh = NULL;
    printf("\nFind section name string table:\n");
    if (elf_header->e_shstrndx != SHN_UNDEF) {
        name_sh = section_header_table + elf_header->e_shstrndx;
        printf("    offset = %#lx, size = %ld\n", name_sh->sh_offset, name_sh->sh_size);
    }

    printf("\nSection header table:\n");
    Elf64_Shdr *sh;
    Elf64_Shdr *symtab_sh = NULL, *dynsym_sh = NULL,
               *strtab_sh = NULL, *dynstr_sh = NULL,
               *got_sh    = NULL, *gotplt_sh = NULL;
    char *name;
    Elf64_Rela *rela_sh;
    int rela_count, rela_idx;
    for (i = 0; i < elf_header->e_shnum; i++) {
        sh = section_header_table + i;
        printf("    %4d: ", i);
        if (name_sh) {
            name = (char *)(f + name_sh->sh_offset + sh->sh_name);
            printf("name = %-20s ", name);
            if (sh->sh_type == SHT_STRTAB) {
                if (strcmp(name, ".strtab") == 0) {
                    strtab_sh = sh;
                } else if (strcmp(name, ".dynstr") == 0) {
                    dynstr_sh = sh;
                }
            }
            if (strcmp(name, ".got") == 0) {
                got_sh = sh;
            }
            if (strcmp(name, ".got.plt") == 0) {
                gotplt_sh = sh;
            }
        }
        printf("type = %-16s ", sh_type(sh->sh_type));
        printf("offset = %#lx ", sh->sh_offset);
        printf("size = %ld ", sh->sh_size);
        printf("\n");
        if (sh->sh_type == SHT_SYMTAB) {
            symtab_sh = sh;
        } else if (sh->sh_type == SHT_DYNSYM) {
            dynsym_sh = sh;
        } else if (sh->sh_type == SHT_RELA) {
            printf("\t\tsh_link = %s, sh_info = %s\n",
                                (f + name_sh->sh_offset + (section_header_table + sh->sh_link)->sh_name),
                                (f + name_sh->sh_offset + (section_header_table + sh->sh_info)->sh_name));
            rela_sh = (Elf64_Rela *)(f + sh->sh_offset);
            rela_count = sh->sh_size / sizeof(Elf64_Rela);
            rela_idx = 0;
            while (rela_idx < rela_count - 1) {
                rela_sh = rela_sh + 1;
                printf("\t\tr_offset = %#lx, r_info = (SYM = %lu, TYPE = %#lx), r_addend = %#lx\n",
                                    rela_sh->r_offset,
                                    ELF64_R_SYM(rela_sh->r_info),
                                    ELF64_R_TYPE(rela_sh->r_info),
                                    rela_sh->r_addend);
                rela_idx++;
            }
        }
    }

    if (symtab_sh && strtab_sh) {
        printf("\nFound symbol table:\n");
        Elf64_Sym *symtab = (Elf64_Sym *)(f + symtab_sh->sh_offset);
        int count = symtab_sh->sh_size / symtab_sh->sh_entsize;
        Elf64_Sym *sym;
        for (i = 0; i < count; i++) {
            sym = symtab + i;
            printf("    %d: ", i);
            printf("name = %-30s ", f + strtab_sh->sh_offset + sym->st_name);
            printf("value = %#-8lx   ", sym->st_value);
            printf("size = %#-8lx ", sym->st_size);
            printf("type = %-16s ", symbol_type(sym->st_info));
            printf("binding = %-16s ", symbol_binding(sym->st_info));
            printf("defined section index = ");
            if (sym->st_shndx > 0 && sym->st_shndx < elf_header->e_shnum - 1) {
                printf("%s", f + name_sh->sh_offset + (section_header_table + sym->st_shndx)->sh_name);
            } else {
                printf("%s", special_seciton_name(sym->st_shndx));
            }
            printf("\n");
        }
    }

    if (dynsym_sh && dynstr_sh) {
        printf("\nFound dynamic linking symbol table:\n");
        Elf64_Sym *symtab = (Elf64_Sym *)(f + dynsym_sh->sh_offset);
        int count = dynsym_sh->sh_size / dynsym_sh->sh_entsize;
        Elf64_Sym *sym;
        for (i = 0; i < count; i++) {
            sym = symtab + i;
            printf("    %d: ", i);
            printf("name = %-30s ", f + dynstr_sh->sh_offset + sym->st_name);
            printf("value = %#-8lx   ", sym->st_value);
            printf("size = %#-8lx ", sym->st_size);
            printf("type = %-16s ", symbol_type(sym->st_info));
            printf("binding = %-16s ", symbol_binding(sym->st_info));
            printf("defined section index = ");
            if (sym->st_shndx > 0 && sym->st_shndx < elf_header->e_shnum - 1) {
                printf("%s", f + name_sh->sh_offset + (section_header_table + sym->st_shndx)->sh_name);
            } else {
                printf("%s", special_seciton_name(sym->st_shndx));
            }
            printf("\n");
        }
    }

    if (got_sh) {
        printf("\nGOT:\n");
        int count = got_sh->sh_size / 8;
        uint64_t *got = (uint64_t *)(f + got_sh->sh_offset);
        for (i = 0; i < count; i++) {
            printf("%d: %#lx\n", i, *got);
            got++;
        }
    }

    if (gotplt_sh) {
        printf("\nGOT.PLT:\n");
        int count = gotplt_sh->sh_size / 8;
        uint64_t *gotplt = (uint64_t *)(f + gotplt_sh->sh_offset);
        for (i = 0; i < count; i++) {
            printf("%d: %#lx\n", i, *gotplt);
            gotplt++;
        }
    }

    munmap(f, buf.st_size);
    close(fd);

    return 0;
}
Пример #5
0
void remove_virtual_functionst::remove_virtual_function(
  goto_programt &goto_program,
  goto_programt::targett target)
{
  const code_function_callt &code=
    to_code_function_call(target->code);

  const exprt &function=code.function();
  assert(function.id()==ID_virtual_function);
  assert(!code.arguments().empty());
  
  functionst functions;
  get_functions(function, functions);
  
  if(functions.empty())
  {
    target->make_skip();
    return; // give up
  }

  // the final target is a skip
  goto_programt final_skip;

  goto_programt::targett t_final=final_skip.add_instruction();
  t_final->make_skip();
  
  // build the calls and gotos

  goto_programt new_code_calls;
  goto_programt new_code_gotos;

  for(functionst::const_iterator
      it=functions.begin();
      it!=functions.end();
      it++)
  {
    // call function
    goto_programt::targett t1=new_code_calls.add_instruction();
    t1->make_function_call(code);
    to_code_function_call(t1->code).function()=it->symbol_expr;
    
    // goto final
    goto_programt::targett t3=new_code_calls.add_instruction();
    t3->make_goto(t_final, true_exprt());

    exprt this_expr=code.arguments()[0];    
    if(this_expr.type().id()!=ID_pointer ||
       this_expr.type().id()!=ID_struct)
    {
      symbol_typet symbol_type(it->class_id);
      this_expr=typecast_exprt(this_expr, pointer_typet(symbol_type));
    }
    
    exprt deref=dereference_exprt(this_expr, this_expr.type().subtype());
    exprt c_id1=constant_exprt(it->class_id, string_typet());
    exprt c_id2=build_class_identifier(deref);
    
    goto_programt::targett t4=new_code_gotos.add_instruction();
    t4->make_goto(t1, equal_exprt(c_id1, c_id2));
  }

  goto_programt new_code;
  
  // patch them all together
  new_code.destructive_append(new_code_gotos);
  new_code.destructive_append(new_code_calls);
  new_code.destructive_append(final_skip);
  
  // set locations
  Forall_goto_program_instructions(it, new_code)
  {
    irep_idt property_class=it->source_location.get_property_class();
    irep_idt comment=it->source_location.get_comment();
    it->source_location=target->source_location;
    it->function=target->function;
    if(!property_class.empty()) it->source_location.set_property_class(property_class);
    if(!comment.empty()) it->source_location.set_comment(comment);
  }
Пример #6
0
    void operator()(const hypergraph_type& source, hypergraph_type& target)
    {
      // first, copy...
      target = source;

      if (! source.is_valid()) return;

      phrase_type       binarized(2);
      hypergraph_type::edge_type::node_set_type tails(2);
      
      // we will traverse source-side in order to avoid confusion with newly created nodes...
      removed_type removed(source.edges.size(), false);

      position_set_type positions;
      node_chart_type   node_chart;
      label_chart_type  label_chart;
      
      label_map.clear();
      node_map.clear();
      
      hypergraph_type::node_set_type::const_iterator niter_end = source.nodes.end();
      for (hypergraph_type::node_set_type::const_iterator niter = source.nodes.begin(); niter != niter_end; ++ niter) {
	const hypergraph_type::node_type& node_source = *niter;
	
	hypergraph_type::node_type::edge_set_type::const_iterator eiter_end = node_source.edges.end();
	for (hypergraph_type::node_type::edge_set_type::const_iterator eiter = node_source.edges.begin(); eiter != eiter_end; ++ eiter) {
	  const hypergraph_type::edge_type& edge_source = source.edges[*eiter];
	  
	  if (edge_source.tails.size() <= 2) continue;
	  
	  removed[edge_source.id] = true;
	  
	  // we will create nodes in a chart structure, and exhaustively enumerate edges
	  
	  symbol_set_type rhs_sorted(edge_source.rule->rhs);
	  tail_set_type   tails_sorted(edge_source.tails);
	  
	  // first, compute non-terminal spans...
	  positions.clear();
	  int pos = 0;
	  for (size_t i = 0; i != rhs_sorted.size(); ++ i)
	    if (rhs_sorted[i].is_non_terminal()) {
	      const int non_terminal_index = rhs_sorted[i].non_terminal_index();
	      
	      tails_sorted[pos] = edge_source.tails[utils::bithack::branch(non_terminal_index == 0, pos, non_terminal_index - 1)];
	      
	      rhs_sorted[i] = rhs_sorted[i].non_terminal();
	      
	      positions.push_back(i);
	      
	      ++ pos;
	    }
	  
	  if (positions.size() != edge_source.tails.size())
	    throw std::runtime_error("invalid edge: # of non-terminals and tails size do not match");
	  
	  // seond, enumerate chart to compute node and edges...
	  node_chart.clear();
	  node_chart.resize(positions.size() + 1, hypergraph_type::invalid);

	  label_chart.clear();
	  label_chart.resize(positions.size() + 1);
	  
	  for (size_t i = 0; i != positions.size(); ++ i) {
	    node_chart(i, i + 1) = tails_sorted[i];
	    label_chart(i, i + 1) = rhs_sorted[positions[i]];
	  }
	  
	  for (size_t length = 2; length < positions.size(); ++ length)
	    for (size_t first = 0; first + length <= positions.size(); ++ first) {
	      const size_t last = first + length;
	      
	      const symbol_set_type subrhs(rhs_sorted.begin() + positions[first], rhs_sorted.begin() + positions[last - 1] + 1);
	      const tail_set_type   subtails(tails_sorted.begin() + first, tails_sorted.begin() + last);
	      
	      std::pair<label_map_type::iterator, bool> result_label = label_map.insert(std::make_pair(subtails, symbol_type()));
	      if (result_label.second) {
		const symbol_type::piece_type left = label_chart(first, last - 1).non_terminal_strip();
		const symbol_type::piece_type right = label_chart(last - 1, last).non_terminal_strip();

		if (length > 2)
		  result_label.first->second = '[' + std::string(left.begin(), left.end() - 1) + '+' + right + "^]";
		else
		  result_label.first->second = '[' + std::string(left) + '+' + right + "^]";
	      }
	      
	      std::pair<node_map_type::iterator, bool> result_node = node_map.insert(std::make_pair(tail_symbol_pair_type(subtails, subrhs), 0));
	      if (result_node.second)
		result_node.first->second = target.add_node().id;
	      
	      const symbol_type lhs = result_label.first->second;
	      const hypergraph_type::id_type head = result_node.first->second;
	      
	      node_chart(first, last) = head;
	      label_chart(first, last) = lhs;
	      
	      // if newly created, then, create edges
	      if (result_node.second)
		for (size_t middle = first + 1; middle != last; ++ middle) {
		  // [first, middle) and [middle, last)
		  
		  tails.front() = node_chart(first, middle);
		  tails.back()  = node_chart(middle, last);
		  
		  const size_t middle_first = positions[middle - 1] + 1;
		  const size_t middle_last  = positions[middle];
		  
		  binarized.clear();
		  binarized.push_back(label_chart(first, middle));
		  binarized.insert(binarized.end(), rhs_sorted.begin() + middle_first, rhs_sorted.begin() + middle_last);
		  binarized.push_back(label_chart(middle, last));
		  
		  hypergraph_type::edge_type& edge_new = target.add_edge(tails.begin(), tails.end());
		  edge_new.rule = rule_type::create(rule_type(lhs, binarized.begin(), binarized.end()));
		  
		  target.connect_edge(edge_new.id, head);
		}
	    }
	  
	  // root...
	  {
	    const size_t first = 0;
	    const size_t last = positions.size();
	    
	    const hypergraph_type::id_type head = node_source.id;
	    const symbol_type& lhs = edge_source.rule->lhs;
	    
	    node_chart(first, last) = head;
	    label_chart(first, last) = lhs;
	    
	    for (size_t middle = first + 1; middle != last; ++ middle) {
	      // [first, middle) and [middle, last)
	      
	      tails.front() = node_chart(first, middle);
	      tails.back()  = node_chart(middle, last);
	      
	      binarized.clear();
	      
	      const size_t prefix_first = 0;
	      const size_t prefix_last  = positions[first];
	      
	      binarized.insert(binarized.end(), rhs_sorted.begin() + prefix_first, rhs_sorted.begin() + prefix_last);
	      binarized.push_back(label_chart(first, middle));
	      
	      const size_t middle_first = positions[middle - 1] + 1;
	      const size_t middle_last  = positions[middle];
	      
	      binarized.insert(binarized.end(), rhs_sorted.begin() + middle_first, rhs_sorted.begin() + middle_last);
	      binarized.push_back(label_chart(middle, last));
	      
	      const size_t suffix_first = positions[last - 1] + 1;
	      const size_t suffix_last  = rhs_sorted.size();
	      
	      binarized.insert(binarized.end(), rhs_sorted.begin() + suffix_first, rhs_sorted.begin() + suffix_last);
	      
	      hypergraph_type::edge_type& edge_new = target.add_edge(tails.begin(), tails.end());
	      edge_new.rule       = rule_type::create(rule_type(lhs, binarized.begin(), binarized.end()));
	      edge_new.features   = edge_source.features;
	      edge_new.attributes = edge_source.attributes;
	      
	      target.connect_edge(edge_new.id, head);
	    }
	  }
	}
      }
      
      // further resize...
      removed.resize(target.edges.size(), false);
      
      hypergraph_type graph_removed;
      
      topologically_sort(target, graph_removed, filter(removed));
      
      target.swap(graph_removed);
    }
Пример #7
0
long GenQuad (QuadType q, SymbolEntry * x, SymbolEntry * y, SymbolEntry * z, int offset, char * prev_param_string) {
    char tmp[1];

    quad_array[nextquad].type = q;
    if (q == CALL_QUAD) {
        quad_array[nextquad].arg1_req.prev_param_string = strdup("-");
        quad_array[nextquad].arg2_req.prev_param_string = strdup("-");
        quad_array[nextquad].dest_req.prev_param_string = strdup(prev_param_string);
    } else {
        quad_array[nextquad].arg1_req.prev_param_string = strdup("-");
        quad_array[nextquad].arg2_req.prev_param_string = strdup("-");
        quad_array[nextquad].dest_req.prev_param_string = strdup("-");
    }
    if (x == NULL)
       quad_array[nextquad].arg1 = "-";
    else if (x->entryType == ENTRY_CONSTANT) {
        char bufferx[256];
        switch (x->u.eConstant.type->kind) {
            case TYPE_INTEGER:
                sprintf(bufferx, "%d", (int) x->u.eConstant.value.vInteger);
                break;
            case TYPE_BOOLEAN:
                sprintf(bufferx, "%u", (unsigned char) x->u.eConstant.value.vBoolean);
                break;
            case TYPE_CHAR:
                sprintf(bufferx, "%s", (char *) x->u.eConstant.value.vString);
                break;
            case TYPE_IARRAY:
                sprintf(bufferx, "%s", (char *) x->u.eConstant.value.vString);
            default:
                break;
        }
        quad_array[nextquad].arg1 = strdup(bufferx);
    }
    else
        if (x->entryType == ENTRY_TEMPORARY)
            if (x->u.eTemporary.type->kind == TYPE_POINTER) {
                char bufferx[256];
                sprintf(bufferx, "[%s]", (char *) x->id);
                quad_array[nextquad].arg1 = strdup(bufferx);
            }
            else if ((lookup_type_find_b(x)->kind == TYPE_LIST) && (lookup_type_find_b(x)->refType == typeVoid))
                quad_array[nextquad].arg1 = strdup("nil");
            else
                quad_array[nextquad].arg1 = (char *) x->id;
        else
            quad_array[nextquad].arg1 = (char *) x->id;

    quad_array[nextquad].arg1_req.type = symbol_type (x);
    quad_array[nextquad].arg1_req.pm = symbol_pm (x);
    quad_array[nextquad].arg1_req.kind = symbol_kind (x);
    quad_array[nextquad].arg1_req.offset = symbol_offset (x);
    if (x != NULL)
        sprintf(tmp, "%u", x->nestingLevel);
    else
        sprintf(tmp, "-");
    quad_array[nextquad].arg1_req.nesting = strdup(tmp);

    if (y == NULL)
        quad_array[nextquad].arg2 = "-";
    else if (y->entryType == ENTRY_CONSTANT) {
        char buffery[256];
        switch (y->u.eConstant.type->kind) {
            case TYPE_INTEGER:
                sprintf(buffery, "%d", (int) y->u.eConstant.value.vInteger);
                break;
            case TYPE_BOOLEAN:
                sprintf(buffery, "%u", (unsigned char) y->u.eConstant.value.vBoolean);
                break;
            case TYPE_CHAR:
                sprintf(buffery, "%s", (char *) y->u.eConstant.value.vString);
                break;
            case TYPE_IARRAY:
                sprintf(buffery, "%s", (char *) y->u.eConstant.value.vString);
            default:
                break;
        }
        quad_array[nextquad].arg2 = strdup(buffery);
    }
    else
        if (y->entryType == ENTRY_TEMPORARY)
            if (y->u.eTemporary.type->kind == TYPE_POINTER) {
                char buffery[256];
                sprintf(buffery, "[%s]", (char *) y->id);
                quad_array[nextquad].arg2 = strdup(buffery);
            }
            else if ((lookup_type_find_b(y)->kind == TYPE_LIST) && (lookup_type_find_b(y)->refType == typeVoid))
                quad_array[nextquad].arg2 = strdup("nil");
            else
                quad_array[nextquad].arg2 = (char *) y->id;
        else
            quad_array[nextquad].arg2 = (char *) y->id;

    quad_array[nextquad].arg2_req.type = symbol_type (y);
    quad_array[nextquad].arg2_req.pm = symbol_pm (y);
    quad_array[nextquad].arg2_req.kind = symbol_kind (y);
    quad_array[nextquad].arg2_req.offset = symbol_offset (y);
    if (y != NULL)
        sprintf(tmp, "%u", y->nestingLevel);
    else
        sprintf(tmp, "-");
    quad_array[nextquad].arg2_req.nesting = strdup(tmp);

    if (z == NULL)
        quad_array[nextquad].dest = "-";
    else
        if (z->entryType == ENTRY_TEMPORARY)
            if (z->u.eTemporary.type->kind == TYPE_POINTER && quad_array[nextquad].type != ARRAY_QUAD) {
                char bufferz[256];
                sprintf(bufferz, "[%s]", (char *) z->id);
                quad_array[nextquad].dest = strdup(bufferz);
            } else
                quad_array[nextquad].dest = (char *) z->id;
        else
            quad_array[nextquad].dest = (char *) z->id;

    quad_array[nextquad].dest_req.type = symbol_type (z);
    quad_array[nextquad].dest_req.pm = symbol_pm (z);
    quad_array[nextquad].dest_req.kind = symbol_kind (z);
    if (q == CALL_QUAD) {
        char buffer[256];
        sprintf(buffer, "%d", offset);
        quad_array[nextquad].dest_req.offset = strdup(buffer);
    } else
        quad_array[nextquad].dest_req.offset = symbol_offset (z);
    if (z != NULL)
        sprintf(tmp, "%u", z->nestingLevel);
    else
        sprintf(tmp, "-");
    quad_array[nextquad].dest_req.nesting = strdup(tmp);

    return nextquad++;
}
Пример #8
0
void
run_reloc(
	struct exec *exhdr,
	int size,
	struct relocation_info *reloc,
	struct nlist *symtabl,
	char *stringtable,
	int   strtbl_sz,
	int   correspondence
)
{
	int i, count;
	int max_ordinal;

	if (NULL == exhdr || NULL == symtabl || NULL == stringtable || NULL == reloc) {
		if (NULL == exhdr) fprintf(stderr, "a.out header NULL, ");
		if (NULL == symtabl) fprintf(stderr, "symbol table NULL, ");
		if (NULL == stringtable) fprintf(stderr, "string table NULL, ");
		if (NULL == reloc) fprintf(stderr, "relocation table NULL, ");
		fprintf(stderr, " can't possibly list the relocations meaningfully\n");
		return;
	}

	max_ordinal = exhdr->a_syms/sizeof(*symtabl);

	count = size/sizeof(*reloc);

	printf("%d relocations\n", count);

	for (i = 0; i < count; ++i) {
		printf("r_address 0x%x, symbol ordinal %d, ", reloc[i].r_address,
			reloc[i].r_symbolnum);

		if (reloc[i].r_extern)
			fputs("extern, ", stdout);
		else {
			printf("local, %s segment, ", print_reloc_segment(reloc[i].r_symbolnum));
		}

		printf(
			"r_addend 0x%x (%d), type %s (0x%x)",
			reloc[i].r_addend,
			reloc[i].r_addend,
			print_reloc_type(reloc[i].r_type), reloc[i].r_type
		);


		if (reloc[i].r_extern && correspondence && reloc[i].r_symbolnum <= max_ordinal) {
			char *sname = "";
			/* stuff from struct nlist associated with this relocation */

			if (symtabl[reloc[i].r_symbolnum].n_un.n_strx >= 4 &&
				symtabl[reloc[i].r_symbolnum].n_un.n_strx < strtbl_sz)
					sname = &stringtable[symtabl[reloc[i].r_symbolnum].n_un.n_strx];
			else
				fprintf(stderr,
					"relocation entry %d, symbol ordinal %d, n_strx is %d, "
					"string table size is %d\n",
					i, reloc[i].r_symbolnum, symtabl[reloc[i].r_symbolnum].n_un.n_strx,
					strtbl_sz);

			printf("\n  name \"%s\" (sym num %d), type 0x%x - %s, desc 0x%x, value 0x%x\n",
				sname,
				reloc[i].r_symbolnum,
				symtabl[reloc[i].r_symbolnum].n_type,
				symbol_type(&symtabl[reloc[i].r_symbolnum]),
				symtabl[reloc[i].r_symbolnum].n_desc,
				symtabl[reloc[i].r_symbolnum].n_value
			);
		} else
			fputc('\n', stdout);
	}
}
Пример #9
0
int main(int argc,char** argv)
{
	if(argc<2)
	{
		printf("usage %s [filename]\n",argv[0]);
		exit(0);
	}
	struct scanner* sc=sc_create(argv[1]);

	int token;
	int i=0;
	while(1)
	{
		i++;
		if(i%5==0)
		{
			printf("\n");
		}
		token=sc_next_token(sc);
		if(token==TOKEN_EOF)
		{
			break;
		}
		if(token==TOKEN_ERR)
		{
			goto err;
		}
		if(token==TOKEN_ID)
		{
			if(symbol_type(sc_token_string(sc))==TOKEN_ID)
			{
				printf("{variable,%s}  ",sc_token_string(sc));
			}
			else
			{
				printf("{keyword,%s}  ",sc_token_string(sc));
			}
			continue;
		}
		if(token==TOKEN_ANNO)
		{
			continue;

		}
		if(token==TOKEN_WS)
		{
			continue;
		}
		if(token==TOKEN_NEWLINE)
		{
			printf("{newline}  ");
			continue;
		}
		printf("{%s,%s}  ",token_name(token),sc_token_string(sc));
	};

	printf("\n");
	return 0;
err:
	printf("err token at line %d\n",sc->s_line);
	return -1;
}
void cpp_typecheckt::typecheck_compound_type(
  struct_union_typet &type)
{
  // first save qualifiers
  c_qualifierst qualifiers(type);

  // now clear them from the type
  type.remove(ID_C_constant);
  type.remove(ID_C_volatile);
  type.remove(ID_C_restricted);

  // get the tag name
  bool anonymous=type.find(ID_tag).is_nil();
  irep_idt base_name;
  cpp_scopet *dest_scope=NULL;
  bool has_body=type.find(ID_body).is_not_nil();
  bool tag_only_declaration=type.get_bool(ID_C_tag_only_declaration);

  if(anonymous)
  {
    base_name="#anon_"+type.id_string()+i2string(anon_counter++);
    type.set("#is_anonymous", true);
    // anonymous structs always go into the current scope
    dest_scope=&cpp_scopes.current_scope();
  }
  else
  {
    const cpp_namet &cpp_name=
      to_cpp_name(type.find(ID_tag));

    // scope given?
    if(cpp_name.is_simple_name())
    {
      base_name=cpp_name.get_base_name();
      dest_scope=&tag_scope(base_name, has_body, tag_only_declaration);
    }
    else
    {
      cpp_save_scopet cpp_save_scope(cpp_scopes);
      cpp_typecheck_resolvet cpp_typecheck_resolve(*this);
      cpp_template_args_non_tct t_args;
      dest_scope=&cpp_typecheck_resolve.resolve_scope(cpp_name, base_name, t_args);
    }
  }

  const irep_idt symbol_name=
    language_prefix+
    dest_scope->prefix+
    "tag."+id2string(base_name);

  // check if we have it already

  contextt::symbolst::iterator previous_symbol=
    context.symbols.find(symbol_name);

  if(previous_symbol!=context.symbols.end())
  {
    // we do!

    symbolt &symbol=previous_symbol->second;

    if(has_body)
    {
      if(symbol.type.id()=="incomplete_"+type.id_string())
      {
        // a previously incomplete struct/union becomes complete
        symbol.type.swap(type);
        typecheck_compound_body(symbol);
      }
      else
      {
        err_location(type.location());
        str << "error: struct symbol `" << base_name
            << "' declared previously" << std::endl;
        str << "location of previous definition: "
            << symbol.location;
        throw 0;
      }
    }
  }
  else
  {
    // produce new symbol
    symbolt symbol;

    symbol.name=symbol_name;
    symbol.base_name=base_name;
    symbol.value.make_nil();
    symbol.location=type.location();
    symbol.mode=ID_cpp;
    symbol.module=module;
    symbol.type.swap(type);
    symbol.is_type=true;
    symbol.is_macro=false;
    symbol.pretty_name=cpp_scopes.current_scope().prefix+id2string(symbol.base_name);
    symbol.type.set(ID_tag, symbol.pretty_name);

    // move early, must be visible before doing body
    symbolt *new_symbol;
 
    if(context.move(symbol, new_symbol))
      throw "cpp_typecheckt::typecheck_compound_type: context.move() failed";

    // put into dest_scope
    cpp_idt &id=cpp_scopes.put_into_scope(*new_symbol, *dest_scope);

    id.id_class=cpp_idt::CLASS;
    id.is_scope=true;
    id.prefix=cpp_scopes.current_scope().prefix+
              id2string(new_symbol->base_name)+"::";
    id.class_identifier=new_symbol->name;
    id.id_class=cpp_idt::CLASS;

    if(has_body)
      typecheck_compound_body(*new_symbol);
    else
    {
      typet new_type("incomplete_"+new_symbol->type.id_string());
      new_type.set(ID_tag, new_symbol->base_name);
      new_symbol->type.swap(new_type);
    }
  }

  // create type symbol
  typet symbol_type(ID_symbol);
  symbol_type.set(ID_identifier, symbol_name);
  qualifiers.write(symbol_type);
  type.swap(symbol_type);
}
Пример #11
0
 symbol_type string_to_symbol(string_type &st){return symbol_type(st);}