Skip to content

Commit 313c1ac

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 2ceb3e3 commit 313c1ac

File tree

7 files changed

+76
-43
lines changed

7 files changed

+76
-43
lines changed

src/goto-cc/compile.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -549,33 +549,33 @@ bool compilet::parse_stdin(languaget &language)
549549
return false;
550550
}
551551

552-
/// Writes the goto functions of \p src_goto_model to a binary format object
553-
/// file.
554-
/// \param file_name: Target file to serialize \p src_goto_model to
555-
/// \param src_goto_model: goto model to serialize
556-
/// \return true on error, false otherwise
557552
bool compilet::write_bin_object_file(
558553
const std::string &file_name,
559-
const goto_modelt &src_goto_model)
554+
const goto_modelt &src_goto_model,
555+
bool validate_goto_model,
556+
message_handlert &message_handler)
560557
{
558+
messaget log(message_handler);
559+
561560
if(validate_goto_model)
562561
{
563-
status() << "Validating goto model" << eom;
562+
log.status() << "Validating goto model" << messaget::eom;
564563
src_goto_model.validate();
565564
}
566565

567-
statistics() << "Writing binary format object `"
568-
<< file_name << "'" << eom;
566+
log.statistics() << "Writing binary format object `" << file_name << "'"
567+
<< messaget::eom;
569568

570569
// symbols
571-
statistics() << "Symbols in table: "
572-
<< src_goto_model.symbol_table.symbols.size() << eom;
570+
log.statistics() << "Symbols in table: "
571+
<< src_goto_model.symbol_table.symbols.size()
572+
<< messaget::eom;
573573

574574
std::ofstream outfile(file_name, std::ios::binary);
575575

576576
if(!outfile.is_open())
577577
{
578-
error() << "Error opening file `" << file_name << "'" << eom;
578+
log.error() << "Error opening file `" << file_name << "'" << messaget::eom;
579579
return true;
580580
}
581581

@@ -584,12 +584,11 @@ bool compilet::write_bin_object_file(
584584

585585
const auto cnt = function_body_count(src_goto_model.goto_functions);
586586

587-
statistics() << "Functions: "
588-
<< src_goto_model.goto_functions.function_map.size() << "; "
589-
<< cnt << " have a body." << eom;
587+
log.statistics() << "Functions: "
588+
<< src_goto_model.goto_functions.function_map.size() << "; "
589+
<< cnt << " have a body." << messaget::eom;
590590

591591
outfile.close();
592-
wrote_object=true;
593592

594593
return false;
595594
}
@@ -647,8 +646,7 @@ compilet::~compilet()
647646
delete_directory(dir);
648647
}
649648

650-
std::size_t
651-
compilet::function_body_count(const goto_functionst &functions) const
649+
std::size_t compilet::function_body_count(const goto_functionst &functions)
652650
{
653651
std::size_t count = 0;
654652

src/goto-cc/compile.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,18 @@ class compilet : public messaget
7373

7474
bool parse_source(const std::string &);
7575

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

7889
/// \brief Has this compiler written any object files?
7990
bool wrote_object_files() const { return wrote_object; }
@@ -102,7 +113,22 @@ class compilet : public messaget
102113
/// \brief String to include in all mangled names
103114
const std::string file_local_mangle_suffix;
104115

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

107133
void add_compiler_specific_defines() const;
108134

src/goto-cc/gcc_mode.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,10 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
954954
output_files.size()==1)
955955
{
956956
linker_script_merget ls_merge(
957-
compiler, output_files.front(), goto_binaries.front(),
958-
cmdline, gcc_message_handler);
957+
output_files.front(),
958+
goto_binaries.front(),
959+
cmdline,
960+
gcc_message_handler);
959961
result=ls_merge.add_linker_script_definitions();
960962
}
961963

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()
@@ -169,7 +169,7 @@ int ld_modet::run_ld()
169169
return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
170170
}
171171

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

@@ -203,7 +203,7 @@ int ld_modet::ld_hybrid_binary(compilet &compiler)
203203
if(result == 0 && cmdline.isset('T'))
204204
{
205205
linker_script_merget ls_merge(
206-
compiler, output_file, goto_binary, cmdline, get_message_handler());
206+
output_file, goto_binary, cmdline, get_message_handler());
207207
result = ls_merge.add_linker_script_definitions();
208208
}
209209

@@ -215,7 +215,7 @@ int ld_modet::ld_hybrid_binary(compilet &compiler)
215215
native_linker,
216216
goto_binary,
217217
output_file,
218-
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE,
218+
building_executable,
219219
get_message_handler());
220220
}
221221

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: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@ 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, get_message_handler());
37+
38+
if(!original_goto_model.has_value())
39+
{
40+
error() << "Unable to read goto binary for linker script merging" << eom;
41+
return 1;
42+
}
43+
3344
temporary_filet linker_def_outfile("goto-cc-linker-info", ".json");
3445
std::list<irep_idt> linker_defined_symbols;
3546
int fail = get_linker_script_data(
3647
linker_defined_symbols,
37-
compiler.goto_model.symbol_table,
48+
original_goto_model->symbol_table,
3849
elf_binary,
3950
linker_def_outfile());
4051
// ignore linker script parsing failures until the code is tested more widely
@@ -57,15 +68,6 @@ int linker_script_merget::add_linker_script_definitions()
5768
return fail;
5869
}
5970

60-
auto original_goto_model =
61-
read_goto_binary(goto_binary, get_message_handler());
62-
63-
if(!original_goto_model.has_value())
64-
{
65-
error() << "Unable to read goto binary for linker script merging" << eom;
66-
return 1;
67-
}
68-
6971
fail=1;
7072
linker_valuest linker_values;
7173
const auto &pair =
@@ -103,7 +105,11 @@ int linker_script_merget::add_linker_script_definitions()
103105
return fail;
104106
}
105107

106-
fail = compiler.write_bin_object_file(goto_binary, *original_goto_model);
108+
fail = compilet::write_bin_object_file(
109+
goto_binary,
110+
*original_goto_model,
111+
cmdline.isset("validate-goto-model"),
112+
get_message_handler());
107113

108114
if(fail!=0)
109115
error() << "Could not write linkerscript-augmented binary" << eom;
@@ -112,13 +118,11 @@ int linker_script_merget::add_linker_script_definitions()
112118
}
113119

114120
linker_script_merget::linker_script_merget(
115-
compilet &compiler,
116121
const std::string &elf_binary,
117122
const std::string &goto_binary,
118123
const cmdlinet &cmdline,
119124
message_handlert &message_handler)
120125
: messaget(message_handler),
121-
compiler(compiler),
122126
elf_binary(elf_binary),
123127
goto_binary(goto_binary),
124128
cmdline(cmdline),

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:public messaget
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)