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/message.h>
17 #include <util/symbol_table.h>
19 
20 #include "goto_functions.h"
21 #include "write_goto_binary.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  sym.type = static_cast<const typet &>(irepconverter.reference_convert(in));
39  sym.value = static_cast<const exprt &>(irepconverter.reference_convert(in));
40  sym.location = static_cast<const source_locationt &>(
41  irepconverter.reference_convert(in));
42 
43  sym.name = irepconverter.read_string_ref(in);
44  sym.module = irepconverter.read_string_ref(in);
45  sym.base_name = irepconverter.read_string_ref(in);
46  sym.mode = irepconverter.read_string_ref(in);
47  sym.pretty_name = irepconverter.read_string_ref(in);
48 
49  // obsolete: symordering
50  irepconverter.read_gb_word(in);
51 
52  std::size_t flags=irepconverter.read_gb_word(in);
53 
54  sym.is_weak = (flags &(1 << 16))!=0;
55  sym.is_type = (flags &(1 << 15))!=0;
56  sym.is_property = (flags &(1 << 14))!=0;
57  sym.is_macro = (flags &(1 << 13))!=0;
58  sym.is_exported = (flags &(1 << 12))!=0;
59  sym.is_input = (flags &(1 << 11))!=0;
60  sym.is_output = (flags &(1 << 10))!=0;
61  sym.is_state_var = (flags &(1 << 9))!=0;
62  sym.is_parameter = (flags &(1 << 8))!=0;
63  sym.is_auxiliary = (flags &(1 << 7))!=0;
64  // sym.binding = (flags &(1 << 6))!=0;
65  sym.is_lvalue = (flags &(1 << 5))!=0;
66  sym.is_static_lifetime = (flags &(1 << 4))!=0;
67  sym.is_thread_local = (flags &(1 << 3))!=0;
68  sym.is_file_local = (flags &(1 << 2))!=0;
69  sym.is_extern = (flags &(1 << 1))!=0;
70  sym.is_volatile = (flags &1)!=0;
71 
72  if(!sym.is_type && sym.type.id()==ID_code)
73  {
74  // makes sure there is an empty function for every function symbol
75  auto entry = functions.function_map.emplace(sym.name, goto_functiont());
76  entry.first->second.set_parameter_identifiers(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  instruction.code_nonconst() =
103  static_cast<const codet &>(irepconverter.reference_convert(in));
104  instruction.source_location = static_cast<const source_locationt &>(
105  irepconverter.reference_convert(in));
106  instruction.type = (goto_program_instruction_typet)
107  irepconverter.read_gb_word(in);
108  instruction.guard =
109  static_cast<const exprt &>(irepconverter.reference_convert(in));
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 also held in the goto_functiont object, and could
131  // 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,
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.empty())
199  message.error() << "Sorry, but I can't read ELF binary '" << filename
200  << "'" << 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  if(version < GOTO_BINARY_VERSION)
224  {
225  message.error() <<
226  "The input was compiled with an old version of "
227  "goto-cc; please recompile" << messaget::eom;
228  return true;
229  }
230  else if(version == GOTO_BINARY_VERSION)
231  {
232  return read_bin_goto_object(in, symbol_table, functions, irepconverter);
233  }
234  else
235  {
236  message.error() <<
237  "The input was compiled with an unsupported version of "
238  "goto-cc; please recompile" << messaget::eom;
239  return true;
240  }
241  }
242 
243  return false;
244 }
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:155
UNREACHABLE
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:503
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
GOTO_BINARY_VERSION
#define GOTO_BINARY_VERSION
Definition: write_goto_binary.h:15
symbolt::is_state_var
bool is_state_var
Definition: symbol.h:62
irep_serialization.h
binary irep conversions with hashing
symbol_tablet
The symbol table.
Definition: symbol_table.h:14
goto_programt::instructiont::source_location
source_locationt source_location
The location of the instruction in the source file.
Definition: goto_program.h:431
read_bin_goto_object.h
Read goto object files.
read_bin_goto_object
static bool read_bin_goto_object(std::istream &in, symbol_tablet &symbol_table, goto_functionst &functions, irep_serializationt &irepconverter)
read goto binary format
Definition: read_bin_goto_object.cpp:26
symbolt::is_macro
bool is_macro
Definition: symbol.h:61
irep_serializationt::read_gb_string
irep_idt read_gb_string(std::istream &)
reads a string from the stream
Definition: irep_serialization.cpp:199
typet
The type of an expression, extends irept.
Definition: type.h:28
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
goto_programt::instructiont::type
goto_program_instruction_typet type
What kind of instruction?
Definition: goto_program.h:434
symbolt::is_input
bool is_input
Definition: symbol.h:62
irep_serializationt::reference_convert
const irept & reference_convert(std::istream &)
Definition: irep_serialization.cpp:43
goto_functionst::compute_location_numbers
void compute_location_numbers()
Definition: goto_functions.cpp:20
exprt
Base class for all expressions.
Definition: expr.h:54
irep_serializationt::ireps_containert
Definition: irep_serialization.h:32
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
irep_serializationt::read_gb_word
static std::size_t read_gb_word(std::istream &)
Interpret a stream of byte as a 7-bit encoded unsigned number.
Definition: irep_serialization.cpp:155
messaget::eom
static eomt eom
Definition: message.h:297
goto_functionst::function_map
function_mapt function_map
Definition: goto_functions.h:29
write_goto_binary.h
Write GOTO binaries.
symbolt::pretty_name
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
symbolt::is_thread_local
bool is_thread_local
Definition: symbol.h:65
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::error
mstreamt & error() const
Definition: message.h:399
goto_programt::instructiont::labels
labelst labels
Definition: goto_program.h:493
symbolt::is_exported
bool is_exported
Definition: symbol.h:61
symbolt::is_parameter
bool is_parameter
Definition: symbol.h:67
irept::id
const irep_idt & id() const
Definition: irep.h:407
message_handlert
Definition: message.h:28
goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:24
goto_program_instruction_typet
goto_program_instruction_typet
The type of an instruction in a GOTO program.
Definition: goto_program.h:30
source_locationt
Definition: source_location.h:19
goto_functionst::goto_functiont
::goto_functiont goto_functiont
Definition: goto_functions.h:27
symbol_table_baset::add
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
Definition: symbol_table_base.cpp:18
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:25
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
messaget::message_handler
message_handlert * message_handler
Definition: message.h:439
goto_programt::instructiont::target_number
unsigned target_number
A number to identify branch targets.
Definition: goto_program.h:601
symbolt::is_output
bool is_output
Definition: symbol.h:62
symbolt::is_extern
bool is_extern
Definition: symbol.h:66
symbolt::is_volatile
bool is_volatile
Definition: symbol.h:66
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:28
symbolt::is_type
bool is_type
Definition: symbol.h:61
goto_programt::instructiont::guard
exprt guard
Guard for gotos, assume, assert Use get_condition() to read, and set_condition(c) to write.
Definition: goto_program.h:438
symbolt::is_auxiliary
bool is_auxiliary
Definition: symbol.h:67
CPROVER_PREFIX
#define CPROVER_PREFIX
Definition: cprover_prefix.h:14
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
goto_functions.h
Goto Programs with Functions.
irep_serializationt::read_string_ref
irep_idt read_string_ref(std::istream &)
Read a string reference from the stream.
Definition: irep_serialization.cpp:244
symbolt::is_file_local
bool is_file_local
Definition: symbol.h:66
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
INVARIANT
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
symbol_table.h
Author: Diffblue Ltd.
message.h
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
goto_programt::instructiont
This class represents an instruction in the GOTO intermediate representation.
Definition: goto_program.h:178
irep_serializationt
Definition: irep_serialization.h:29
goto_programt::instructiont::is_target
bool is_target() const
Is this node a branch target?
Definition: goto_program.h:499
goto_programt::instructiont::code_nonconst
codet & code_nonconst()
Set the code represented by this instruction.
Definition: goto_program.h:191
symbolt::is_weak
bool is_weak
Definition: symbol.h:67
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
symbolt::is_property
bool is_property
Definition: symbol.h:62
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:646
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:33