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__)
54 (void)building_executable;
60 message.
debug() <<
"merging " << output_file <<
" and " << goto_binary_file
61 <<
" using " << objcopy_cmd
67 std::vector<std::string> objcopy_argv = {
69 "--remove-section",
"goto-cc",
70 "--add-section",
"goto-cc=" + goto_binary_file, output_file};
72 const int add_section_result =
run(objcopy_argv[0], objcopy_argv);
73 if(add_section_result != 0)
76 message.
warning() <<
"cannot merge EFI binaries: goto-cc section lost"
79 result = add_section_result;
84 bool remove_result = std::filesystem::remove(goto_binary_file);
90 result = remove_result;
93 #elif defined(__APPLE__)
96 message.
debug() <<
"merging " << output_file <<
" and " << goto_binary_file
97 <<
" using " << (building_executable ?
"lipo" :
"ld")
100 if(building_executable)
104 std::vector<std::string> lipo_argv = {
105 "lipo", output_file,
"-create",
"-arch",
"hppa7100LC", goto_binary_file,
106 "-output", output_file };
108 result =
run(lipo_argv[0], lipo_argv);
115 mode_t current_umask = umask(0);
116 umask(current_umask);
117 int chmod_result = chmod(
118 output_file.c_str(), (S_IRWXU | S_IRWXG | S_IRWXO) & ~current_umask);
119 if(chmod_result != 0)
121 message.
error() <<
"Setting execute permissions failed: "
123 result = chmod_result;
130 std::vector<std::string> ld_argv = {
"ld",
140 result =
run(ld_argv[0], ld_argv);
144 bool remove_result = std::filesystem::remove(goto_binary_file);
150 result = remove_result;
155 (void)compiler_or_linker;
156 (void)goto_binary_file;
158 (void)building_executable;
159 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)