-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathhybrid_binary.cpp
166 lines (138 loc) · 4.67 KB
/
hybrid_binary.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
/*******************************************************************\
Module: Create hybrid binary with goto-binary section
Author: Michael Tautschnig, 2018
\*******************************************************************/
/// \file
/// Create hybrid binary with goto-binary section
#include "hybrid_binary.h"
#include <util/message.h>
#include <util/run.h>
#include <util/suffix.h>
#include <cstring>
#include <filesystem>
#if defined(__APPLE__)
# include <sys/stat.h>
#endif
std::string objcopy_command(const std::string &compiler_or_linker)
{
if(has_suffix(compiler_or_linker, "-ld"))
{
std::string objcopy_cmd = compiler_or_linker;
objcopy_cmd.erase(objcopy_cmd.size() - 2);
objcopy_cmd += "objcopy";
return objcopy_cmd;
}
else
return "objcopy";
}
int hybrid_binary(
const std::string &compiler_or_linker,
const std::string &goto_binary_file,
const std::string &output_file,
bool building_executable,
message_handlert &message_handler,
bool linking_efi)
{
messaget message(message_handler);
int result = 0;
#if defined(__linux__) || defined(__FreeBSD_kernel__) || \
defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
defined(__gnu_hurd__)
// we can use objcopy for both object files and executables
(void)building_executable;
const std::string objcopy_cmd = objcopy_command(compiler_or_linker);
// merge output from gcc or ld with goto-binary using objcopy
message.debug() << "merging " << output_file << " and " << goto_binary_file
<< " using " << objcopy_cmd
<< messaget::eom;
{
// Now add goto-binary as goto-cc section.
// Remove if it exists before, or otherwise adding fails.
std::vector<std::string> objcopy_argv = {
objcopy_cmd,
"--remove-section", "goto-cc",
"--add-section", "goto-cc=" + goto_binary_file, output_file};
const int add_section_result = run(objcopy_argv[0], objcopy_argv);
if(add_section_result != 0)
{
if(linking_efi)
message.warning() << "cannot merge EFI binaries: goto-cc section lost"
<< messaget::eom;
else
result = add_section_result;
}
}
// delete the goto binary
bool remove_result = std::filesystem::remove(goto_binary_file);
if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
<< messaget::eom;
if(result == 0)
result = remove_result;
}
#elif defined(__APPLE__)
// Mac
message.debug() << "merging " << output_file << " and " << goto_binary_file
<< " using " << (building_executable ? "lipo" : "ld")
<< messaget::eom;
if(building_executable)
{
// Add goto-binary as hppa7100LC section.
// This overwrites if there's already one.
std::vector<std::string> lipo_argv = {
"lipo", output_file, "-create", "-arch", "hppa7100LC", goto_binary_file,
"-output", output_file };
result = run(lipo_argv[0], lipo_argv);
if(result == 0)
{
// lipo creates an output file, but it does not set execute permissions,
// so the user is unable to directly execute the output file until its
// chmod +x
mode_t current_umask = umask(0);
umask(current_umask);
int chmod_result = chmod(
output_file.c_str(), (S_IRWXU | S_IRWXG | S_IRWXO) & ~current_umask);
if(chmod_result != 0)
{
message.error() << "Setting execute permissions failed: "
<< std::strerror(errno) << messaget::eom;
result = chmod_result;
}
}
}
else
{
// This fails if there's already one.
std::vector<std::string> ld_argv = {"ld",
"-r",
"-sectcreate",
"__TEXT",
"goto-cc",
goto_binary_file,
output_file,
"-o",
output_file};
result = run(ld_argv[0], ld_argv);
}
// delete the goto binary
bool remove_result = std::filesystem::remove(goto_binary_file);
if(!remove_result)
{
message.error() << "Remove failed: " << std::strerror(errno)
<< messaget::eom;
if(result == 0)
result = remove_result;
}
#else
// unused parameters
(void)compiler_or_linker;
(void)goto_binary_file;
(void)output_file;
(void)building_executable;
message.error() << "binary merging not implemented for this platform"
<< messaget::eom;
result = 1;
#endif
return result;
}