mirror of
https://github.com/corda/corda.git
synced 2025-01-19 11:16:54 +00:00
support embedded resources on darwin via binaryToMacho.cpp
This commit is contained in:
parent
73ba3fcef7
commit
618684de6e
21
makefile
21
makefile
@ -84,6 +84,7 @@ ifeq ($(platform),darwin)
|
||||
-I$(JAVA_HOME)/include/linux -I$(src)
|
||||
lflags = $(common-lflags) -ldl
|
||||
strip-all = -S -x
|
||||
binaryToMacho = $(native-build)/binaryToMacho
|
||||
endif
|
||||
|
||||
ifeq ($(platform),windows)
|
||||
@ -198,10 +199,6 @@ classpath-classes = \
|
||||
classpath-object = $(native-build)/classpath-jar.o
|
||||
classpath-dep = $(classpath-build)/dep
|
||||
|
||||
ifeq ($(platform),darwin)
|
||||
classpath-object =
|
||||
endif
|
||||
|
||||
test-sources = $(wildcard $(test)/*.java)
|
||||
test-classes = $(call java-classes,$(test-sources),$(test),$(test-build))
|
||||
test-dep = $(test-build)/dep
|
||||
@ -209,11 +206,7 @@ test-dep = $(test-build)/dep
|
||||
class-name = $(patsubst $(1)/%.class,%,$(2))
|
||||
class-names = $(foreach x,$(2),$(call class-name,$(1),$(x)))
|
||||
|
||||
ifeq ($(platform),darwin)
|
||||
flags = -cp $(classpath-build):$(test-build)
|
||||
else
|
||||
flags = -cp $(test-build)
|
||||
endif
|
||||
flags = -cp $(test-build)
|
||||
|
||||
args = $(flags) $(input)
|
||||
|
||||
@ -299,11 +292,19 @@ $(build)/classpath.jar: $(classpath-dep)
|
||||
cd $(classpath-build); \
|
||||
$(jar) c0f $${wd}/$(@) $$(find . -name '*.class'))
|
||||
|
||||
$(classpath-object): $(build)/classpath.jar
|
||||
$(binaryToMacho): $(src)/binaryToMacho.cpp
|
||||
$(cxx) $(^) -o $(@)
|
||||
|
||||
$(classpath-object): $(build)/classpath.jar $(binaryToMacho)
|
||||
ifeq ($(platform),darwin)
|
||||
$(binaryToMacho) $(build)/classpath.jar \
|
||||
__binary_classpath_jar_data __binary_classpath_jar_size > $(@)
|
||||
else
|
||||
(wd=$$(pwd); \
|
||||
cd $(build); \
|
||||
$(objcopy) -I binary classpath.jar \
|
||||
-O $(object-format) -B $(object-arch) $${wd}/$(@))
|
||||
endif
|
||||
|
||||
$(generator-objects): $(native-build)/%.o: $(src)/%.cpp
|
||||
@echo "compiling $(@)"
|
||||
|
158
src/binaryToMacho.cpp
Normal file
158
src/binaryToMacho.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
#include "stdint.h"
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "sys/stat.h"
|
||||
#include "sys/mman.h"
|
||||
#include "fcntl.h"
|
||||
#include "unistd.h"
|
||||
|
||||
#include "mach-o/loader.h"
|
||||
#include "mach-o/nlist.h"
|
||||
|
||||
namespace {
|
||||
|
||||
inline unsigned
|
||||
pad(unsigned n)
|
||||
{
|
||||
return (n + (4 - 1)) & ~(4 - 1);
|
||||
}
|
||||
|
||||
void
|
||||
writeObject(FILE* out, const uint8_t* data, unsigned size,
|
||||
const char* dataName, const char* sizeName)
|
||||
{
|
||||
unsigned dataNameLength = strlen(dataName) + 1;
|
||||
unsigned sizeNameLength = strlen(sizeName) + 1;
|
||||
|
||||
mach_header header = {
|
||||
MH_MAGIC, // magic
|
||||
CPU_TYPE_I386, // cputype
|
||||
CPU_SUBTYPE_I386_ALL, // cpusubtype
|
||||
MH_OBJECT, // filetype,
|
||||
2, // ncmds
|
||||
sizeof(segment_command)
|
||||
+ sizeof(section)
|
||||
+ sizeof(symtab_command), // sizeofcmds
|
||||
0 // flags
|
||||
};
|
||||
|
||||
segment_command segment = {
|
||||
LC_SEGMENT, // cmd
|
||||
sizeof(segment_command) + sizeof(section), // cmdsize
|
||||
"__TEXT", // segname
|
||||
0, // vmaddr
|
||||
pad(size), // vmsize
|
||||
sizeof(mach_header)
|
||||
+ sizeof(segment_command)
|
||||
+ sizeof(section)
|
||||
+ sizeof(symtab_command), // fileoff
|
||||
pad(size), // filesize
|
||||
7, // maxprot
|
||||
7, // initprot
|
||||
1, // nsects
|
||||
0 // flags
|
||||
};
|
||||
|
||||
section sect = {
|
||||
"__const", // sectname
|
||||
"__TEXT", // segname
|
||||
0, // addr
|
||||
pad(size), // size
|
||||
sizeof(mach_header)
|
||||
+ sizeof(segment_command)
|
||||
+ sizeof(section)
|
||||
+ sizeof(symtab_command), // offset
|
||||
0, // align
|
||||
0, // reloff
|
||||
0, // nreloc
|
||||
S_REGULAR, // flags
|
||||
0, // reserved1
|
||||
0, // reserved2
|
||||
};
|
||||
|
||||
symtab_command symbolTable = {
|
||||
LC_SYMTAB, // cmd
|
||||
sizeof(symtab_command), // cmdsize
|
||||
sizeof(mach_header)
|
||||
+ sizeof(segment_command)
|
||||
+ sizeof(section)
|
||||
+ sizeof(symtab_command)
|
||||
+ pad(size), // symoff
|
||||
2, // nsyms
|
||||
sizeof(mach_header)
|
||||
+ sizeof(segment_command)
|
||||
+ sizeof(section)
|
||||
+ sizeof(symtab_command)
|
||||
+ pad(size)
|
||||
+ (sizeof(struct nlist) * 2), // stroff
|
||||
1 + dataNameLength + sizeNameLength, // strsize
|
||||
};
|
||||
|
||||
struct nlist symbolList[] = {
|
||||
{
|
||||
reinterpret_cast<char*>(1), // n_un
|
||||
N_SECT | N_EXT, // n_type
|
||||
1, // n_sect
|
||||
0, // n_desc
|
||||
0 // n_value
|
||||
},
|
||||
{
|
||||
reinterpret_cast<char*>(1 + dataNameLength), // n_un
|
||||
N_ABS | N_EXT, // n_type
|
||||
NO_SECT, // n_sect
|
||||
0, // n_desc
|
||||
size // n_value
|
||||
}
|
||||
};
|
||||
|
||||
fwrite(&header, 1, sizeof(header), out);
|
||||
fwrite(&segment, 1, sizeof(segment), out);
|
||||
fwrite(§, 1, sizeof(sect), out);
|
||||
fwrite(&symbolTable, 1, sizeof(symbolTable), out);
|
||||
|
||||
fwrite(data, 1, size, out);
|
||||
for (unsigned i = 0; i < pad(size) - size; ++i) fputc(0, out);
|
||||
|
||||
fwrite(&symbolList, 1, sizeof(symbolList), out);
|
||||
|
||||
fputc(0, out);
|
||||
fwrite(dataName, 1, dataNameLength, out);
|
||||
fwrite(sizeName, 1, sizeNameLength, out);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int
|
||||
main(int argc, const char** argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
fprintf(stderr,
|
||||
"usage: %s <input file> <data symbol name> <size symbol name>\n",
|
||||
argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t* data = 0;
|
||||
unsigned size;
|
||||
int fd = open(argv[1], O_RDONLY);
|
||||
if (fd != -1) {
|
||||
struct stat s;
|
||||
int r = fstat(fd, &s);
|
||||
if (r != -1) {
|
||||
data = static_cast<uint8_t*>
|
||||
(mmap(0, s.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
|
||||
size = s.st_size;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
if (data) {
|
||||
writeObject(stdout, data, size, argv[2], argv[3]);
|
||||
munmap(data, size);
|
||||
return 0;
|
||||
} else {
|
||||
perror(argv[0]);
|
||||
return -1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user