@@ -105,7 +105,7 @@ struct MObjectFileImp
105
105
106
106
virtual void Write (std::ofstream &inFile) = 0;
107
107
108
- static MObjectFileImp *Create (int machine, int elf_class, int elf_data, int flags);
108
+ static MObjectFileImp *Create (int machine, int elf_class, int elf_data, int elf_abi, int flags);
109
109
110
110
protected:
111
111
friend class MObjectFile ;
@@ -290,14 +290,16 @@ struct MELFObjectFileImp : public MObjectFileImp
290
290
291
291
virtual void Write (std::ofstream &inFile) override ;
292
292
293
- MELFObjectFileImp (int machine, int flags)
293
+ MELFObjectFileImp (int machine, uint8_t elf_abi, int flags)
294
294
: MObjectFileImp()
295
295
, mMachine (machine)
296
+ , mABI (elf_abi)
296
297
, mFlags (flags)
297
298
{
298
299
}
299
300
300
301
Elf_Half mMachine ;
302
+ uint8_t mABI ;
301
303
Elf_Word mFlags ;
302
304
};
303
305
@@ -331,7 +333,7 @@ void MELFObjectFileImp<ELF_CLASS, ELF_DATA>::Write(std::ofstream &f)
331
333
Elf_Ehdr eh = {
332
334
// e_ident
333
335
{ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
334
- ELF_CLASS, ELF_DATA, EV_CURRENT},
336
+ ELF_CLASS, ELF_DATA, EV_CURRENT, mABI },
335
337
ET_REL, // e_type
336
338
mMachine , // e_machine
337
339
EV_CURRENT, // e_version
@@ -772,18 +774,18 @@ void MCOFFObjectFileImp::Write(std::ofstream &f)
772
774
// --------------------------------------------------------------------
773
775
774
776
#if __has_include(<elf.h>)
775
- MObjectFileImp *MObjectFileImp::Create (int machine, int elf_class, int elf_data, int flags)
777
+ MObjectFileImp *MObjectFileImp::Create (int machine, int elf_class, int elf_data, int elf_abi, int flags)
776
778
{
777
779
MObjectFileImp *result = nullptr ;
778
780
779
781
if (elf_class == ELFCLASS32 and elf_data == ELFDATA2LSB)
780
- result = new MELFObjectFileImp<ELFCLASS32, ELFDATA2LSB>(machine, flags);
782
+ result = new MELFObjectFileImp<ELFCLASS32, ELFDATA2LSB>(machine, elf_abi, flags);
781
783
else if (elf_class == ELFCLASS32 and elf_data == ELFDATA2MSB)
782
- result = new MELFObjectFileImp<ELFCLASS32, ELFDATA2MSB>(machine, flags);
784
+ result = new MELFObjectFileImp<ELFCLASS32, ELFDATA2MSB>(machine, elf_abi, flags);
783
785
else if (elf_class == ELFCLASS64 and elf_data == ELFDATA2LSB)
784
- result = new MELFObjectFileImp<ELFCLASS64, ELFDATA2LSB>(machine, flags);
786
+ result = new MELFObjectFileImp<ELFCLASS64, ELFDATA2LSB>(machine, elf_abi, flags);
785
787
else if (elf_class == ELFCLASS64 and elf_data == ELFDATA2MSB)
786
- result = new MELFObjectFileImp<ELFCLASS64, ELFDATA2MSB>(machine, flags);
788
+ result = new MELFObjectFileImp<ELFCLASS64, ELFDATA2MSB>(machine, elf_abi, flags);
787
789
else
788
790
{
789
791
std::cerr << " Unsupported ELF class and/or data" << std::endl;
@@ -800,8 +802,8 @@ class MObjectFile
800
802
{
801
803
public:
802
804
#if __has_include(<elf.h>)
803
- MObjectFile (int machine, int elf_class, int elf_data, int flags)
804
- : mImpl(MObjectFileImp::Create(machine, elf_class, elf_data, flags))
805
+ MObjectFile (int machine, int elf_class, int elf_data, int elf_abi, int flags)
806
+ : mImpl(MObjectFileImp::Create(machine, elf_class, elf_data, elf_abi, flags))
805
807
{
806
808
}
807
809
#endif
@@ -1005,6 +1007,7 @@ int main(int argc, char *argv[])
1005
1007
( " elf-machine" , po::value<int >(), " The ELF machine type to use, default is same as this machine. Use one of the values from elf.h" )
1006
1008
( " elf-class" , po::value<int >(), " ELF class, default is same as this machine. Acceptable values are 1 (32bit) and 2 (64bit)." )
1007
1009
( " elf-data" , po::value<int >(), " ELF endianness, default is same as this machine. Acceptable values are 1 (little-endian, LSB) and 2 (big-endian, MSB)." )
1010
+ ( " elf-abi" , po::value<int >(), " ELF OS ABI value, see file elf.h for values (linux = 3, freebsd = 9)" )
1008
1011
( " elf-flags" , po::value<int >(), " Processor specific flags in the ELF header, e.g. the EABI version for ARM" )
1009
1012
#endif
1010
1013
@@ -1068,7 +1071,13 @@ int main(int argc, char *argv[])
1068
1071
// --------------------------------------------------------------------
1069
1072
// find out the native format. Simply look at how we were assembled ourselves
1070
1073
1071
- int elf_machine = EM_NONE, elf_class = 0 , elf_data = 0 , elf_flags = 0 ;
1074
+ int elf_machine = EM_NONE, elf_class = 0 , elf_data = 0 , elf_flags = 0 , elf_abi = ELFOSABI_NONE;
1075
+
1076
+ #if __linux or __linux__
1077
+ elf_abi = ELFOSABI_LINUX;
1078
+ #elif __FreeBSD__
1079
+ elf_abi = ELFOSABI_FREEBSD;
1080
+ #endif
1072
1081
1073
1082
#if not defined(_MSC_VER)
1074
1083
char exePath[PATH_MAX + 1 ];
@@ -1090,6 +1099,8 @@ int main(int argc, char *argv[])
1090
1099
1091
1100
elf_class = e_ident[EI_CLASS];
1092
1101
elf_data = e_ident[EI_DATA];
1102
+ if (e_ident[EI_ABIVERSION])
1103
+ elf_abi = e_ident[EI_ABIVERSION];
1093
1104
1094
1105
lseek (fd, 0 , SEEK_SET);
1095
1106
@@ -1136,42 +1147,60 @@ int main(int argc, char *argv[])
1136
1147
if (not file.is_open ())
1137
1148
throw std::runtime_error (" Could not open output file for writing" );
1138
1149
1150
+ COFF_Machine win_machine = {};
1151
+ #if defined(_MSC_VER)
1152
+ #if defined(_M_AMD64)
1153
+ machine = IMAGE_FILE_MACHINE_AMD64;
1154
+ #elif defined(_M_ARM64)
1155
+ machine = IMAGE_FILE_MACHINE_ARM64;
1156
+ #elif defined(_M_IX86)
1157
+ machine = IMAGE_FILE_MACHINE_I386;
1158
+ #endif
1159
+ #endif
1160
+
1139
1161
if (vm.count (" coff" ))
1140
1162
{
1141
- COFF_Machine machine;
1142
1163
if (vm[" coff" ].as <std::string>() == " x64" )
1143
- machine = IMAGE_FILE_MACHINE_AMD64;
1164
+ win_machine = IMAGE_FILE_MACHINE_AMD64;
1144
1165
else if (vm[" coff" ].as <std::string>() == " arm64" )
1145
- machine = IMAGE_FILE_MACHINE_ARM64;
1166
+ win_machine = IMAGE_FILE_MACHINE_ARM64;
1146
1167
else if (vm[" coff" ].as <std::string>() == " x86" )
1147
- machine = IMAGE_FILE_MACHINE_I386;
1168
+ win_machine = IMAGE_FILE_MACHINE_I386;
1148
1169
else
1149
1170
throw std::runtime_error (" Unsupported machine for COFF: " + vm[" coff" ].as <std::string>());
1171
+ }
1172
+
1173
+ bool target_elf = vm.count (" elf-machine" ) and vm.count (" elf-class" ) and vm.count (" elf-data" ) and vm.count (" elf-flags" );
1174
+ if (vm.count (" elf-machine" ))
1175
+ elf_machine = vm[" elf-machine" ].as <int >();
1176
+
1177
+ if (vm.count (" elf-class" ))
1178
+ elf_class = vm[" elf-class" ].as <int >();
1179
+
1180
+ if (vm.count (" elf-data" ))
1181
+ elf_data = vm[" elf-data" ].as <int >();
1150
1182
1151
- MObjectFile obj (machine);
1183
+ if (vm.count (" elf-abi" ))
1184
+ elf_abi = vm[" elf-abi" ].as <int >();
1185
+
1186
+ if (vm.count (" elf-flags" ))
1187
+ elf_flags = vm[" elf-flags" ].as <int >();
1188
+
1189
+ if (win_machine and not target_elf)
1190
+ {
1191
+ MObjectFile obj (win_machine);
1152
1192
rsrcFile.Write (obj);
1153
1193
obj.Write (file);
1154
1194
}
1155
1195
#if __has_include(<elf.h>)
1156
1196
else
1157
1197
{
1158
- if (vm.count (" elf-machine" ))
1159
- elf_machine = vm[" elf-machine" ].as <int >();
1160
-
1161
- if (vm.count (" elf-class" ))
1162
- elf_class = vm[" elf-class" ].as <int >();
1163
-
1164
- if (vm.count (" elf-data" ))
1165
- elf_data = vm[" elf-data" ].as <int >();
1166
-
1167
- if (vm.count (" elf-flags" ))
1168
- elf_flags = vm[" elf-flags" ].as <int >();
1169
-
1170
-
1171
- MObjectFile obj (elf_machine, elf_class, elf_data, elf_flags);
1198
+ MObjectFile obj (elf_machine, elf_class, elf_data, elf_abi, elf_flags);
1172
1199
rsrcFile.Write (obj);
1173
1200
obj.Write (file);
1174
1201
}
1202
+ #else
1203
+ throw std::runtime_error (" Could not create resource file, probably you're trying to create a ELF resource file on Windows?" );
1175
1204
#endif
1176
1205
}
1177
1206
catch (const std::exception &ex)
0 commit comments