Skip to content

Commit 3531be6

Browse files
committed
Do not pass the compiler object to linker-script merging
There is no need to access the goto model stored in compilet as we read the goto binary that compilet wrote anyway. This avoids tight coupling and enables removing public class members from compilet.
1 parent 4c2b413 commit 3531be6

8 files changed

+80
-48
lines changed

scripts/expected_doxygen_warnings.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ warning: Included by graph for 'expr.h' not generated, too many nodes (88), thre
9191
warning: Included by graph for 'expr_util.h' not generated, too many nodes (60), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9292
warning: Included by graph for 'invariant.h' not generated, too many nodes (187), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9393
warning: Included by graph for 'irep.h' not generated, too many nodes (62), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
94-
warning: Included by graph for 'message.h' not generated, too many nodes (115), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
94+
warning: Included by graph for 'message.h' not generated, too many nodes (116), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9595
warning: Included by graph for 'namespace.h' not generated, too many nodes (111), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9696
warning: Included by graph for 'prefix.h' not generated, too many nodes (86), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9797
warning: Included by graph for 'simplify_expr.h' not generated, too many nodes (76), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
9898
warning: Included by graph for 'std_code.h' not generated, too many nodes (78), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
99-
warning: Included by graph for 'std_expr.h' not generated, too many nodes (245), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
99+
warning: Included by graph for 'std_expr.h' not generated, too many nodes (246), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
100100
warning: Included by graph for 'std_types.h' not generated, too many nodes (124), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.
101101
warning: Included by graph for 'symbol_table.h' not generated, too many nodes (95), threshold is 60. Consider increasing DOT_GRAPH_MAX_NODES.

src/goto-cc/compile.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -556,32 +556,33 @@ bool compilet::parse_stdin(languaget &language)
556556
return false;
557557
}
558558

559-
/// Writes the goto functions of \p src_goto_model to a binary format object
560-
/// file.
561-
/// \param file_name: Target file to serialize \p src_goto_model to
562-
/// \param src_goto_model: goto model to serialize
563-
/// \return true on error, false otherwise
564559
bool compilet::write_bin_object_file(
565560
const std::string &file_name,
566-
const goto_modelt &src_goto_model)
561+
const goto_modelt &src_goto_model,
562+
bool validate_goto_model,
563+
message_handlert &message_handler)
567564
{
565+
messaget log(message_handler);
566+
568567
if(validate_goto_model)
569568
{
570-
status() << "Validating goto model" << eom;
569+
log.status() << "Validating goto model" << messaget::eom;
571570
src_goto_model.validate();
572571
}
573572

574-
statistics() << "Writing binary format object '" << file_name << "'" << eom;
573+
log.statistics() << "Writing binary format object '" << file_name << "'"
574+
<< messaget::eom;
575575

576576
// symbols
577-
statistics() << "Symbols in table: "
578-
<< src_goto_model.symbol_table.symbols.size() << eom;
577+
log.statistics() << "Symbols in table: "
578+
<< src_goto_model.symbol_table.symbols.size()
579+
<< messaget::eom;
579580

580581
std::ofstream outfile(file_name, std::ios::binary);
581582

582583
if(!outfile.is_open())
583584
{
584-
error() << "Error opening file '" << file_name << "'" << eom;
585+
log.error() << "Error opening file '" << file_name << "'" << messaget::eom;
585586
return true;
586587
}
587588

@@ -590,12 +591,11 @@ bool compilet::write_bin_object_file(
590591

591592
const auto cnt = function_body_count(src_goto_model.goto_functions);
592593

593-
statistics() << "Functions: "
594-
<< src_goto_model.goto_functions.function_map.size() << "; "
595-
<< cnt << " have a body." << eom;
594+
log.statistics() << "Functions: "
595+
<< src_goto_model.goto_functions.function_map.size() << "; "
596+
<< cnt << " have a body." << messaget::eom;
596597

597598
outfile.close();
598-
wrote_object=true;
599599

600600
return false;
601601
}
@@ -661,8 +661,7 @@ compilet::~compilet()
661661
delete_directory(dir);
662662
}
663663

664-
std::size_t
665-
compilet::function_body_count(const goto_functionst &functions) const
664+
std::size_t compilet::function_body_count(const goto_functionst &functions)
666665
{
667666
std::size_t count = 0;
668667

src/goto-cc/compile.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ class compilet : public messaget
6868

6969
bool parse_source(const std::string &);
7070

71-
bool write_bin_object_file(const std::string &, const goto_modelt &);
71+
/// Writes the goto functions of \p src_goto_model to a binary format object
72+
/// file.
73+
/// \param file_name: Target file to serialize \p src_goto_model to
74+
/// \param src_goto_model: goto model to serialize
75+
/// \param validate_goto_model: enable goto-model validation
76+
/// \param message_handler: message handler
77+
/// \return true on error, false otherwise
78+
static bool write_bin_object_file(
79+
const std::string &file_name,
80+
const goto_modelt &src_goto_model,
81+
bool validate_goto_model,
82+
message_handlert &message_handler);
7283

7384
/// \brief Has this compiler written any object files?
7485
bool wrote_object_files() const { return wrote_object; }
@@ -88,8 +99,6 @@ class compilet : public messaget
8899
}
89100

90101
protected:
91-
namespacet ns;
92-
93102
std::string working_directory;
94103
std::string override_language;
95104

@@ -105,7 +114,22 @@ class compilet : public messaget
105114
/// \brief String to include in all mangled names
106115
const std::string file_local_mangle_suffix;
107116

108-
std::size_t function_body_count(const goto_functionst &) const;
117+
static std::size_t function_body_count(const goto_functionst &);
118+
119+
bool write_bin_object_file(
120+
const std::string &file_name,
121+
const goto_modelt &src_goto_model)
122+
{
123+
if(write_bin_object_file(
124+
file_name, src_goto_model, validate_goto_model, get_message_handler()))
125+
{
126+
return true;
127+
}
128+
129+
wrote_object = true;
130+
131+
return false;
132+
}
109133

110134
void add_compiler_specific_defines() const;
111135

src/goto-cc/gcc_mode.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,8 +972,10 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
972972
output_files.size()==1)
973973
{
974974
linker_script_merget ls_merge(
975-
compiler, output_files.front(), goto_binaries.front(),
976-
cmdline, gcc_message_handler);
975+
output_files.front(),
976+
goto_binaries.front(),
977+
cmdline,
978+
gcc_message_handler);
977979
result=ls_merge.add_linker_script_definitions();
978980
}
979981

src/goto-cc/ld_mode.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ int ld_modet::doit()
145145

146146
// We can generate hybrid ELF and Mach-O binaries
147147
// containing both executable machine code and the goto-binary.
148-
return ld_hybrid_binary(compiler);
148+
return ld_hybrid_binary(compiler.mode == compilet::COMPILE_LINK_EXECUTABLE);
149149
}
150150

151151
int ld_modet::run_ld()
@@ -170,7 +170,7 @@ int ld_modet::run_ld()
170170
return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
171171
}
172172

173-
int ld_modet::ld_hybrid_binary(compilet &compiler)
173+
int ld_modet::ld_hybrid_binary(bool building_executable)
174174
{
175175
std::string output_file;
176176

@@ -206,7 +206,7 @@ int ld_modet::ld_hybrid_binary(compilet &compiler)
206206
if(result == 0 && cmdline.isset('T'))
207207
{
208208
linker_script_merget ls_merge(
209-
compiler, output_file, goto_binary, cmdline, message_handler);
209+
output_file, goto_binary, cmdline, message_handler);
210210
result = ls_merge.add_linker_script_definitions();
211211
}
212212

@@ -218,7 +218,7 @@ int ld_modet::ld_hybrid_binary(compilet &compiler)
218218
native_linker,
219219
goto_binary,
220220
output_file,
221-
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE,
221+
building_executable,
222222
message_handler);
223223
}
224224

src/goto-cc/ld_mode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ld_modet : public goto_cc_modet
4040
/// \brief call ld with original command line
4141
int run_ld();
4242

43-
int ld_hybrid_binary(compilet &compiler);
43+
int ld_hybrid_binary(bool);
4444
};
4545

4646
#endif // CPROVER_GOTO_CC_LD_MODE_H

src/goto-cc/linker_script_merge.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,28 @@ Author: Kareem Khazem <[email protected]>, 2017
2525

2626
#include <goto-programs/read_goto_binary.h>
2727

28+
#include "compile.h"
29+
2830
int linker_script_merget::add_linker_script_definitions()
2931
{
3032
if(!cmdline.isset('T'))
3133
return 0;
3234

35+
auto original_goto_model =
36+
read_goto_binary(goto_binary, log.get_message_handler());
37+
38+
if(!original_goto_model.has_value())
39+
{
40+
log.error() << "Unable to read goto binary for linker script merging"
41+
<< messaget::eom;
42+
return 1;
43+
}
44+
3345
temporary_filet linker_def_outfile("goto-cc-linker-info", ".json");
3446
std::list<irep_idt> linker_defined_symbols;
3547
int fail = get_linker_script_data(
3648
linker_defined_symbols,
37-
compiler.goto_model.symbol_table,
49+
original_goto_model->symbol_table,
3850
elf_binary,
3951
linker_def_outfile());
4052
// ignore linker script parsing failures until the code is tested more widely
@@ -57,16 +69,6 @@ int linker_script_merget::add_linker_script_definitions()
5769
return fail;
5870
}
5971

60-
auto original_goto_model =
61-
read_goto_binary(goto_binary, log.get_message_handler());
62-
63-
if(!original_goto_model.has_value())
64-
{
65-
log.error() << "Unable to read goto binary for linker script merging"
66-
<< messaget::eom;
67-
return 1;
68-
}
69-
7072
fail=1;
7173
linker_valuest linker_values;
7274
const auto &pair =
@@ -106,7 +108,11 @@ int linker_script_merget::add_linker_script_definitions()
106108
return fail;
107109
}
108110

109-
fail = compiler.write_bin_object_file(goto_binary, *original_goto_model);
111+
fail = compilet::write_bin_object_file(
112+
goto_binary,
113+
*original_goto_model,
114+
cmdline.isset("validate-goto-model"),
115+
log.get_message_handler());
110116

111117
if(fail!=0)
112118
{
@@ -118,13 +124,11 @@ int linker_script_merget::add_linker_script_definitions()
118124
}
119125

120126
linker_script_merget::linker_script_merget(
121-
compilet &compiler,
122127
const std::string &elf_binary,
123128
const std::string &goto_binary,
124129
const cmdlinet &cmdline,
125130
message_handlert &message_handler)
126-
: compiler(compiler),
127-
elf_binary(elf_binary),
131+
: elf_binary(elf_binary),
128132
goto_binary(goto_binary),
129133
cmdline(cmdline),
130134
log(message_handler),

src/goto-cc/linker_script_merge.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
#include <functional>
99

1010
#include <util/json.h>
11+
#include <util/message.h>
12+
#include <util/std_expr.h>
1113

12-
#include "compile.h"
1314
#include "gcc_cmdline.h"
1415

16+
class goto_modelt;
17+
class goto_programt;
18+
class symbol_tablet;
19+
1520
/// \brief Patterns of expressions that should be replaced
1621
///
1722
/// Each instance of this class describes an expression 'shape' that should be
@@ -79,14 +84,12 @@ class linker_script_merget
7984
typedef std::map<irep_idt, std::pair<symbol_exprt, exprt>> linker_valuest;
8085

8186
linker_script_merget(
82-
compilet &,
8387
const std::string &elf_binary,
8488
const std::string &goto_binary,
8589
const cmdlinet &,
8690
message_handlert &);
8791

8892
protected:
89-
compilet &compiler;
9093
const std::string &elf_binary;
9194
const std::string &goto_binary;
9295
const cmdlinet &cmdline;

0 commit comments

Comments
 (0)