21 #if defined(__APPLE__)
22 # include <sys/stat.h>
29 std::string objcopy_cmd = compiler_or_linker;
30 objcopy_cmd.erase(objcopy_cmd.size() - 2);
31 objcopy_cmd +=
"objcopy";
40 const std::string &compiler_or_linker,
41 const std::string &goto_binary_file,
42 const std::string &output_file,
43 bool building_executable,
51 #if defined(__linux__) || defined(__FreeBSD_kernel__) || \
52 defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || \
55 (void)building_executable;
61 message.
debug() <<
"merging " << output_file <<
" and " << goto_binary_file
62 <<
" using " << objcopy_cmd
68 std::vector<std::string> objcopy_argv = {
70 "--remove-section",
"goto-cc",
71 "--add-section",
"goto-cc=" + goto_binary_file, output_file};
73 const int add_section_result =
run(objcopy_argv[0], objcopy_argv);
74 if(add_section_result != 0)
77 message.
warning() <<
"cannot merge EFI binaries: goto-cc section lost"
80 result = add_section_result;
85 bool remove_result = std::filesystem::remove(goto_binary_file);
91 result = remove_result;
94 #elif defined(__APPLE__)
97 message.
debug() <<
"merging " << output_file <<
" and " << goto_binary_file
98 <<
" using " << (building_executable ?
"lipo" :
"ld")
101 if(building_executable)
105 std::vector<std::string> lipo_argv = {
106 "lipo", output_file,
"-create",
"-arch",
"hppa7100LC", goto_binary_file,
107 "-output", output_file };
109 result =
run(lipo_argv[0], lipo_argv);
116 mode_t current_umask = umask(0);
117 umask(current_umask);
118 int chmod_result = chmod(
119 output_file.c_str(), (S_IRWXU | S_IRWXG | S_IRWXO) & ~current_umask);
120 if(chmod_result != 0)
122 message.
error() <<
"Setting execute permissions failed: "
124 result = chmod_result;
131 std::vector<std::string> ld_argv = {
"ld",
141 result =
run(ld_argv[0], ld_argv);
145 bool remove_result = std::filesystem::remove(goto_binary_file);
151 result = remove_result;
156 (void)compiler_or_linker;
157 (void)goto_binary_file;
159 (void)building_executable;
160 message.
error() <<
"binary merging not implemented for this platform"
Class that provides messages with a built-in verbosity 'level'.
mstreamt & warning() const
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)
Merges a goto binary into an object file (e.g.
std::string objcopy_command(const std::string &compiler_or_linker)
Return the name of the objcopy tool matching the chosen compiler or linker command.
Create hybrid binary with goto-binary section.
int run(const std::string &what, const std::vector< std::string > &argv)
char * strerror(int errnum)
bool has_suffix(const std::string &s, const std::string &suffix)