-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathld_mode.cpp
284 lines (226 loc) · 7.08 KB
/
ld_mode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************\
Module: LD Mode
Author: CM Wintersteiger, 2006
\*******************************************************************/
/// \file
/// LD Mode
#include "ld_mode.h"
#ifdef _WIN32
#define EX_OK 0
#define EX_USAGE 64
#define EX_SOFTWARE 70
#else
#include <sysexits.h>
#endif
#include <util/cmdline.h>
#include <util/config.h>
#include <util/invariant.h>
#include <util/run.h>
#include "compile.h"
#include "goto_cc_cmdline.h"
#include "hybrid_binary.h"
#include "linker_script_merge.h"
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
static std::string
linker_name(const cmdlinet &cmdline, const std::string &base_name)
{
if(cmdline.isset("native-linker"))
return cmdline.get_value("native-linker");
std::string::size_type pos = base_name.find("goto-ld");
if(
pos == std::string::npos || base_name == "goto-gcc" ||
base_name == "goto-ld")
return "ld";
std::string result = base_name;
result.replace(pos, 7, "ld");
return result;
}
ld_modet::ld_modet(goto_cc_cmdlinet &_cmdline, const std::string &_base_name)
: goto_cc_modet(_cmdline, _base_name, gcc_message_handler),
goto_binary_tmp_suffix(".goto-cc-saved")
{
}
/// does it.
int ld_modet::doit()
{
native_tool_name = linker_name(cmdline, base_name);
// When --help is requested, just reproduce the output of the original
// compiler. This is so as not to confuse configure scripts that depend on
// particular information (such as the list of supported targets).
if(
cmdline.isset("help") || cmdline.isset("version") ||
cmdline.isset("print-sysroot"))
{
help();
return run_ld();
}
messaget::eval_verbosity(
cmdline.get_value("verbosity"), messaget::M_ERROR, gcc_message_handler);
gcc_message_handler.print_warnings_as_errors(
cmdline.isset("fatal-warnings") && !cmdline.isset("no-fatal-warnings"));
compilet compiler(
cmdline,
gcc_message_handler,
cmdline.isset("fatal-warnings") && !cmdline.isset("no-fatal-warnings"));
// determine actions to be undertaken
compiler.mode = compilet::LINK_LIBRARY;
// model validation
compiler.validate_goto_model = cmdline.isset("validate-goto-model");
// get configuration
config.set(cmdline);
compiler.object_file_extension = "o";
if(cmdline.isset('L'))
compiler.library_paths = cmdline.get_values('L');
// Don't add the system paths!
if(cmdline.isset('l'))
compiler.libraries = cmdline.get_values('l');
if(cmdline.isset("static"))
compiler.libraries.push_back("c");
if(cmdline.isset('o'))
{
// given gcc -o file1 -o file2,
// gcc will output to file2, not file1
compiler.output_file_object = cmdline.get_values('o').back();
compiler.output_file_executable = cmdline.get_values('o').back();
}
else
{
compiler.output_file_object.clear();
compiler.output_file_executable = "a.out";
}
// We now iterate over any input files
for(const auto &arg : cmdline.parsed_argv)
if(arg.is_infile_name)
compiler.add_input_file(arg.arg);
// Revert to gcc in case there is no source to compile
// and no binary to link.
if(compiler.source_files.empty() && compiler.object_files.empty())
return run_ld(); // exit!
// do all the rest
if(compiler.doit())
return 1; // LD exit code for all kinds of errors
// We can generate hybrid ELF and Mach-O binaries
// containing both executable machine code and the goto-binary.
return ld_hybrid_binary(
compiler.mode == compilet::COMPILE_LINK_EXECUTABLE, compiler.object_files);
}
int ld_modet::run_ld()
{
PRECONDITION(!cmdline.parsed_argv.empty());
// build new argv
std::vector<std::string> new_argv;
new_argv.reserve(cmdline.parsed_argv.size());
for(const auto &a : cmdline.parsed_argv)
new_argv.push_back(a.arg);
// overwrite argv[0]
new_argv[0] = native_tool_name;
messaget log{gcc_message_handler};
log.debug() << "RUN:";
for(std::size_t i = 0; i < new_argv.size(); i++)
log.debug() << " " << new_argv[i];
log.debug() << messaget::eom;
return run(new_argv[0], new_argv, cmdline.stdin_file, "", "");
}
int ld_modet::ld_hybrid_binary(
bool building_executable,
const std::list<std::string> &object_files)
{
std::string output_file;
if(cmdline.isset('o'))
{
output_file = cmdline.get_value('o');
if(output_file == "/dev/null")
return EX_OK;
}
else
output_file = "a.out";
messaget log{gcc_message_handler};
log.debug() << "Running " << native_tool_name << " to generate hybrid binary"
<< messaget::eom;
// save the goto-cc output file
std::string goto_binary = output_file + goto_binary_tmp_suffix;
try
{
std::filesystem::rename(output_file, goto_binary);
}
catch(const std::filesystem::filesystem_error &e)
{
log.error() << "Rename failed: " << e.what() << messaget::eom;
return 1;
}
const bool linking_efi = cmdline.get_value('m') == "i386pep";
#ifdef __linux__
if(linking_efi)
{
const std::string objcopy_cmd = objcopy_command(native_tool_name);
for(const auto &object_file : object_files)
{
log.debug() << "stripping goto-cc sections before building EFI binary"
<< messaget::eom;
// create a backup copy
const std::string bin_name = object_file + goto_binary_tmp_suffix;
std::ifstream in(object_file, std::ios::binary);
std::ofstream out(bin_name, std::ios::binary);
out << in.rdbuf();
// remove any existing goto-cc section
std::vector<std::string> objcopy_argv;
objcopy_argv.push_back(objcopy_cmd);
objcopy_argv.push_back("--remove-section=goto-cc");
objcopy_argv.push_back(object_file);
if(run(objcopy_argv[0], objcopy_argv) != 0)
{
log.debug() << "EFI binary preparation: removing goto-cc section failed"
<< messaget::eom;
}
}
}
#else
(void)object_files; // unused parameter
#endif
int result = run_ld();
if(result == 0 && cmdline.isset('T'))
{
linker_script_merget ls_merge(
output_file, goto_binary, cmdline, message_handler);
result = ls_merge.add_linker_script_definitions();
}
#ifdef __linux__
if(linking_efi)
{
log.debug() << "arch set with " << object_files.size() << messaget::eom;
for(const auto &object_file : object_files)
{
log.debug() << "EFI binary preparation: restoring object files"
<< messaget::eom;
const std::string bin_name = object_file + goto_binary_tmp_suffix;
const int mv_result = rename(bin_name.c_str(), object_file.c_str());
if(mv_result != 0)
{
log.debug() << "Rename failed: " << std::strerror(errno)
<< messaget::eom;
}
}
}
#endif
if(result == 0)
{
std::string native_linker = linker_name(cmdline, base_name);
result = hybrid_binary(
native_linker,
goto_binary,
output_file,
building_executable,
message_handler,
linking_efi);
}
return result;
}
/// display command line help
void ld_modet::help_mode()
{
std::cout << "goto-ld understands the options of "
<< "ld plus the following.\n\n";
}