cprover
read_bin_goto_object.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Read goto object files.
4 
5 Author: CM Wintersteiger
6 
7 Date: June 2006
8 
9 \*******************************************************************/
10 
13 
14 #include "read_bin_goto_object.h"
15 
16 #include <util/namespace.h>
17 #include <util/message.h>
18 #include <util/symbol_table.h>
20 
21 #include "goto_functions.h"
22 
27  std::istream &in,
28  symbol_tablet &symbol_table,
29  goto_functionst &functions,
30  irep_serializationt &irepconverter)
31 {
32  std::size_t count = irepconverter.read_gb_word(in); // # of symbols
33 
34  for(std::size_t i=0; i<count; i++)
35  {
36  symbolt sym;
37 
38  irepconverter.reference_convert(in, sym.type);
39  irepconverter.reference_convert(in, sym.value);
40  irepconverter.reference_convert(in, sym.location);
41 
42  sym.name = irepconverter.read_string_ref(in);
43  sym.module = irepconverter.read_string_ref(in);
44  sym.base_name = irepconverter.read_string_ref(in);
45  sym.mode = irepconverter.read_string_ref(in);
46  sym.pretty_name = irepconverter.read_string_ref(in);
47 
48  // obsolete: symordering
49  irepconverter.read_gb_word(in);
50 
51  std::size_t flags=irepconverter.read_gb_word(in);
52 
53  sym.is_weak = (flags &(1 << 16))!=0;
54  sym.is_type = (flags &(1 << 15))!=0;
55  sym.is_property = (flags &(1 << 14))!=0;
56  sym.is_macro = (flags &(1 << 13))!=0;
57  sym.is_exported = (flags &(1 << 12))!=0;
58  sym.is_input = (flags &(1 << 11))!=0;
59  sym.is_output = (flags &(1 << 10))!=0;
60  sym.is_state_var = (flags &(1 << 9))!=0;
61  sym.is_parameter = (flags &(1 << 8))!=0;
62  sym.is_auxiliary = (flags &(1 << 7))!=0;
63  // sym.binding = (flags &(1 << 6))!=0;
64  sym.is_lvalue = (flags &(1 << 5))!=0;
65  sym.is_static_lifetime = (flags &(1 << 4))!=0;
66  sym.is_thread_local = (flags &(1 << 3))!=0;
67  sym.is_file_local = (flags &(1 << 2))!=0;
68  sym.is_extern = (flags &(1 << 1))!=0;
69  sym.is_volatile = (flags &1)!=0;
70 
71  if(!sym.is_type && sym.type.id()==ID_code)
72  {
73  // makes sure there is an empty function
74  // for every function symbol and fixes
75  // the function types.
76  functions.function_map[sym.name].type=to_code_type(sym.type);
77  }
78 
79  symbol_table.add(sym);
80  }
81 
82  count=irepconverter.read_gb_word(in); // # of functions
83 
84  for(std::size_t fct_index = 0; fct_index < count; ++fct_index)
85  {
86  irep_idt fname=irepconverter.read_gb_string(in);
87  goto_functionst::goto_functiont &f = functions.function_map[fname];
88 
89  typedef std::map<goto_programt::targett, std::list<unsigned> > target_mapt;
90  target_mapt target_map;
91  typedef std::map<unsigned, goto_programt::targett> rev_target_mapt;
92  rev_target_mapt rev_target_map;
93 
94  bool hidden=false;
95 
96  std::size_t ins_count = irepconverter.read_gb_word(in); // # of instructions
97  for(std::size_t ins_index = 0; ins_index < ins_count; ++ins_index)
98  {
99  goto_programt::targett itarget = f.body.add_instruction();
100  goto_programt::instructiont &instruction=*itarget;
101 
102  irepconverter.reference_convert(in, instruction.code);
103  instruction.function = irepconverter.read_string_ref(in);
104  irepconverter.reference_convert(in, instruction.source_location);
105  instruction.type = (goto_program_instruction_typet)
106  irepconverter.read_gb_word(in);
107  instruction.guard.make_nil();
108  irepconverter.reference_convert(in, instruction.guard);
109  irepconverter.read_string_ref(in); // former event
110  instruction.target_number = irepconverter.read_gb_word(in);
111  if(instruction.is_target() &&
112  rev_target_map.insert(
113  rev_target_map.end(),
114  std::make_pair(instruction.target_number, itarget))->second!=itarget)
115  UNREACHABLE;
116 
117  std::size_t t_count = irepconverter.read_gb_word(in); // # of targets
118  for(std::size_t i=0; i<t_count; i++)
119  // just save the target numbers
120  target_map[itarget].push_back(irepconverter.read_gb_word(in));
121 
122  std::size_t l_count = irepconverter.read_gb_word(in); // # of labels
123 
124  for(std::size_t i=0; i<l_count; i++)
125  {
126  irep_idt label=irepconverter.read_string_ref(in);
127  instruction.labels.push_back(label);
128  if(label == CPROVER_PREFIX "HIDE")
129  hidden=true;
130  // The above info is normally in the type of the goto_functiont object,
131  // which should likely be stored in the binary.
132  }
133  }
134 
135  // Resolve targets
136  for(target_mapt::iterator tit = target_map.begin();
137  tit!=target_map.end();
138  tit++)
139  {
140  goto_programt::targett ins = tit->first;
141 
142  for(std::list<unsigned>::iterator nit = tit->second.begin();
143  nit!=tit->second.end();
144  nit++)
145  {
146  unsigned n=*nit;
147  rev_target_mapt::const_iterator entry=rev_target_map.find(n);
148  INVARIANT(
149  entry != rev_target_map.end(),
150  "something from the target map should also be in the reverse target "
151  "map");
152  ins->targets.push_back(entry->second);
153  }
154  }
155 
156  f.body.update();
157 
158  if(hidden)
159  f.make_hidden();
160  }
161 
162  functions.compute_location_numbers();
163 
164  return false;
165 }
166 
171  std::istream &in,
172  const std::string &filename,
173  symbol_tablet &symbol_table,
174  goto_functionst &functions,
175  message_handlert &message_handler)
176 {
177  messaget message(message_handler);
178 
179  {
180  char hdr[4];
181  hdr[0]=static_cast<char>(in.get());
182  hdr[1]=static_cast<char>(in.get());
183  hdr[2]=static_cast<char>(in.get());
184 
185  if(hdr[0]=='G' && hdr[1]=='B' && hdr[2]=='F')
186  {
187  // OK!
188  }
189  else
190  {
191  hdr[3]=static_cast<char>(in.get());
192  if(hdr[0]==0x7f && hdr[1]=='G' && hdr[2]=='B' && hdr[3]=='F')
193  {
194  // OK!
195  }
196  else if(hdr[0]==0x7f && hdr[1]=='E' && hdr[2]=='L' && hdr[3]=='F')
197  {
198  if(filename!="")
199  message.error() << "Sorry, but I can't read ELF binary `"
200  << filename << "'" << messaget::eom;
201  else
202  message.error() << "Sorry, but I can't read ELF binaries"
203  << messaget::eom;
204 
205  return true;
206  }
207  else
208  {
209  message.error() << "`" << filename << "' is not a goto-binary"
210  << messaget::eom;
211  return true;
212  }
213  }
214  }
215 
217  irep_serializationt irepconverter(ic);
218  // symbol_serializationt symbolconverter(ic);
219 
220  {
221  std::size_t version=irepconverter.read_gb_word(in);
222 
223  switch(version)
224  {
225  case 1:
226  case 2:
227  case 3:
228  message.error() <<
229  "The input was compiled with an old version of "
230  "goto-cc; please recompile" << messaget::eom;
231  return true;
232 
233  case 4:
235  in, symbol_table, functions, irepconverter);
236  break;
237 
238  default:
239  message.error() <<
240  "The input was compiled with an unsupported version of "
241  "goto-cc; please recompile" << messaget::eom;
242  return true;
243  }
244  }
245 
246  return false;
247 }
exprt guard
Guard for gotos, assume, assert.
Definition: goto_program.h:193
irep_idt function
The function this instruction belongs to.
Definition: goto_program.h:184
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool is_output
Definition: symbol.h:61
bool is_thread_local
Definition: symbol.h:65
#define CPROVER_PREFIX
irep_idt mode
Language mode.
Definition: symbol.h:49
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:190
Read goto object files.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:982
Goto Programs with Functions.
exprt value
Initial value of symbol.
Definition: symbol.h:34
irep_idt read_string_ref(std::istream &)
Read a string reference from the stream.
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
function_mapt function_map
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
Symbol table entry.
Definition: symbol.h:27
bool is_static_lifetime
Definition: symbol.h:65
bool is_input
Definition: symbol.h:61
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:400
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:178
unsigned target_number
A number to identify branch targets.
Definition: goto_program.h:376
const irep_idt & id() const
Definition: irep.h:259
void compute_location_numbers()
instructionst::iterator targett
Definition: goto_program.h:414
bool is_exported
Definition: symbol.h:61
bool is_parameter
Definition: symbol.h:66
static bool read_bin_goto_object_v4(std::istream &in, symbol_tablet &symbol_table, goto_functionst &functions, irep_serializationt &irepconverter)
read goto binary format v4
The symbol table.
Definition: symbol_table.h:19
mstreamt & error() const
Definition: message.h:386
::goto_functiont goto_functiont
void reference_convert(std::istream &, irept &irep)
goto_program_instruction_typet
The type of an instruction in a GOTO program.
Definition: goto_program.h:31
A collection of goto functions.
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:144
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
Author: Diffblue Ltd.
binary irep conversions with hashing
bool is_volatile
Definition: symbol.h:66
bool read_bin_goto_object(std::istream &in, const std::string &filename, symbol_tablet &symbol_table, goto_functionst &functions, message_handlert &message_handler)
reads a goto binary file back into a symbol and a function table
bool is_extern
Definition: symbol.h:66
static eomt eom
Definition: message.h:284
irep_idt read_gb_string(std::istream &)
reads a string from the stream
typet type
Type of symbol.
Definition: symbol.h:31
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:187
bool is_state_var
Definition: symbol.h:61
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:478
bool is_file_local
Definition: symbol.h:66
void make_nil()
Definition: irep.h:315
bool is_weak
Definition: symbol.h:66
static std::size_t read_gb_word(std::istream &)
Interpret a stream of byte as a 7-bit encoded unsigned number.
bool is_auxiliary
Definition: symbol.h:66
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
bool is_type
Definition: symbol.h:61
bool is_property
Definition: symbol.h:61
bool is_target() const
Is this node a branch target?
Definition: goto_program.h:234
bool is_macro
Definition: symbol.h:61
bool is_lvalue
Definition: symbol.h:66