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