Skip to content

Commit c93c54c

Browse files
committed
Cope with EFI binaries
Running objcopy on EFI binaries does not work, hence we need to give up on the goto-cc sections in a graceful manner.
1 parent 1612f74 commit c93c54c

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

src/goto-cc/gcc_mode.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,34 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
929929
}
930930
objcopy_cmd+="objcopy";
931931

932+
#ifdef __linux__
933+
if(act_as_ld && cmdline.get_value('m')=="i386pep")
934+
{
935+
for(const auto &object_file : compiler.object_files)
936+
{
937+
debug() << "stripping goto-cc sections before building EFI binary" << eom;
938+
// create a backup copy
939+
std::string bin_name=object_file+goto_binary_tmp_suffix;
940+
941+
std::ifstream in(object_file, std::ios::binary);
942+
std::ofstream out(bin_name, std::ios::binary);
943+
out << in.rdbuf();
944+
945+
// remove any existing goto-cc section
946+
std::vector<std::string> objcopy_argv;
947+
948+
objcopy_argv.push_back(objcopy_cmd);
949+
objcopy_argv.push_back("--remove-section=goto-cc");
950+
objcopy_argv.push_back(object_file);
951+
952+
int result=run(objcopy_argv[0], objcopy_argv, "", "");
953+
if(result!=0)
954+
debug() << "EFI binary preparation: removing goto-cc section failed"
955+
<< eom;
956+
}
957+
}
958+
#endif
959+
932960
int result=run_gcc(compiler);
933961

934962
if(result==0)
@@ -938,6 +966,21 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
938966
result=ls_merge.add_linker_script_definitions();
939967
}
940968

969+
#ifdef __linux__
970+
if(act_as_ld && cmdline.get_value('m')=="i386pep")
971+
{
972+
debug() << "arch set with " << compiler.object_files.size() << eom;
973+
for(const auto &object_file : compiler.object_files)
974+
{
975+
debug() << "EFI binary preparation: restoring object files" << eom;
976+
std::string bin_name=object_file+goto_binary_tmp_suffix;
977+
int mv_result=rename(bin_name.c_str(), object_file.c_str());
978+
if(mv_result!=0)
979+
debug() << "Rename failed: " << std::strerror(errno) << eom;
980+
}
981+
}
982+
#endif
983+
941984
// merge output from gcc with goto-binaries
942985
// using objcopy, or do cleanup if an earlier call failed
943986
for(std::list<std::string>::const_iterator
@@ -958,7 +1001,9 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
9581001
objcopy_argv.push_back("--remove-section=goto-cc");
9591002
objcopy_argv.push_back(*it);
9601003

961-
result=run(objcopy_argv[0], objcopy_argv, "", "");
1004+
int rs_result=run(objcopy_argv[0], objcopy_argv, "", "");
1005+
if(rs_result!=0 && (!act_as_ld || cmdline.get_value('m')!="i386pep"))
1006+
result=rs_result;
9621007
}
9631008

9641009
if(result==0)
@@ -971,7 +1016,15 @@ int gcc_modet::gcc_hybrid_binary(compilet &compiler)
9711016
objcopy_argv.push_back("goto-cc="+saved);
9721017
objcopy_argv.push_back(*it);
9731018

974-
result=run(objcopy_argv[0], objcopy_argv, "", "");
1019+
int as_result=run(objcopy_argv[0], objcopy_argv, "", "");
1020+
if(as_result!=0)
1021+
{
1022+
if(act_as_ld && cmdline.get_value('m')=="i386pep")
1023+
warning() << "cannot merge EFI binaries: goto-cc section lost"
1024+
<< eom;
1025+
else
1026+
result=as_result;
1027+
}
9751028
}
9761029

9771030
int remove_result=remove(saved.c_str());

0 commit comments

Comments
 (0)