Skip to content

Commit fa98f73

Browse files
author
Madeleine Goebel
committed
Works for simple module
1 parent fb32663 commit fa98f73

File tree

4 files changed

+231
-4
lines changed

4 files changed

+231
-4
lines changed

src/HLL/Compiler.nqp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class HLL::Compiler does HLL::Backend::Default {
2323
@!stages := nqp::split(' ', 'start parse ast ' ~ $!backend.stages());
2424

2525
# Command options and usage.
26-
@!cmdoptions := nqp::split(' ', 'e=s help|h target=s trace|t=s encoding=s output|o=s source-name=s combine version|v show-config verbose-config|V stagestats=s? ll-exception rxtrace nqpevent=s profile=s? profile-compile=s? profile-filename=s profile-kind=s profile-stage=s repl-mode=s bytecode|b compile=s'
26+
@!cmdoptions := nqp::split(' ', 'e=s help|h target=s trace|t=s encoding=s output|o=s source-name=s combine version|v show-config verbose-config|V stagestats=s? ll-exception rxtrace nqpevent=s profile=s? profile-compile=s? profile-filename=s profile-kind=s profile-stage=s repl-mode=s bytecode|b compile=s bm=s'
2727
#?if js
2828
~ ' substagestats beautify nqp-runtime=s perl6-runtime=s libpath=s shebang execname=s source-map'
2929
#?endif
@@ -268,6 +268,7 @@ class HLL::Compiler does HLL::Backend::Default {
268268
self.usage($program-name) if %adverbs<help> || %adverbs<h>;
269269

270270
if %adverbs<compile> {
271+
note("I'm in the first compile");
271272
%adverbs<target> := 'mbc'; # FIXME - This needs to be changed to allow for other backends
272273
%adverbs<output> := %adverbs<compile> ~ ".bc";
273274
}
@@ -379,9 +380,20 @@ class HLL::Compiler does HLL::Backend::Default {
379380
}
380381
}
381382
if %adverbs<compile> {
382-
my $command := "./linker/elfmaker " ~ %adverbs<output>;
383+
note("I'm in the second compile!");
384+
my $command;
385+
if (%adverbs<bm>) {
386+
$command := "./nqp/src/linker/elfmaker " ~ %adverbs<output> ~ " " ~ %adverbs<bm>;
387+
note("Hi!");
388+
note($command);
389+
}
390+
else {
391+
$command := "./nqp/src/linker/elfmaker " ~ %adverbs<output>;
392+
note("Goodbye!");
393+
note($command);
394+
}
383395
self.syscall($command);
384-
$command := "gcc -O3 -o " ~ %adverbs<compile> ~ " ./linker/attempt2.c " ~ %adverbs<output>;
396+
$command := "gcc -O3 -o " ~ %adverbs<compile> ~ " ./nqp/src/linker/attempt2.c " ~ %adverbs<output>;
385397
self.syscall($command);
386398
$command := "chmod 755 " ~ %adverbs<compile>;
387399
self.syscall($command);
@@ -505,8 +517,12 @@ class HLL::Compiler does HLL::Backend::Default {
505517
$err := 1;
506518
}
507519
elsif nqp::defined(%adverbs<bytecode>) || nqp::defined(%adverbs<b>) {
520+
if (nqp::defined(%adverbs<bm>)) {
521+
note(%adverbs<bm>);
522+
nqp::loadbytecode(%adverbs<bm>);
523+
}
508524
nqp::loadbytecode($filename);
509-
nqp::exit(0);
525+
# nqp::exit(0);
510526
}
511527
else {
512528
$in-handle := open($filename, :r, :enc($encoding));

src/linker/attempt2.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <fcntl.h>
6+
#include <ftw.h>
7+
8+
#define __USE_XOPEN_EXTENDED 500
9+
10+
/* These are external references to the symbols created by OBJCOPY */
11+
extern char _binary_test_txt_start[];
12+
extern char _binary_test_txt_end[];
13+
14+
int err;
15+
16+
// int delete_file(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
17+
// int rv = remove(fpath);
18+
// if (rv) {
19+
// perror(fpath);
20+
// }
21+
// return rv;
22+
// }
23+
24+
// int delete_directory(char *path) {
25+
// return nftw(path, delete_file, 64, FTW_DEPTH | FTW_PHYS);
26+
// }
27+
28+
int main(int argc, char* argv[])
29+
{
30+
char* command = calloc(100, sizeof(char));
31+
char *data_start = _binary_test_txt_start;
32+
char *data_end = _binary_test_txt_end;
33+
size_t data_size = _binary_test_txt_end - _binary_test_txt_start;
34+
35+
char* proto_name = "p6_XXXXXX";
36+
char temp_name[strlen(proto_name)+1];
37+
strcpy(temp_name, proto_name);
38+
int tar_ball = mkstemp(temp_name);
39+
FILE* sfd = fdopen(tar_ball, "wb");
40+
fwrite(data_start, data_size, 1, sfd);
41+
fclose(sfd);
42+
43+
sprintf(command, "mkdir %s", &temp_name[1]);
44+
system(command);
45+
46+
sprintf(command, "tar -xf ./%s -C %s", temp_name, &temp_name[1]);
47+
system(command);
48+
49+
sprintf(command, "sed -i -n -u '/^MOARVM*/,$p' %s/*", &temp_name[1]);
50+
system(command);
51+
52+
sprintf(command, "x=$(ls %s | grep -v '\\.bc'); perl6 --bm=./%s/$x -b ./%s/%s.bc", &temp_name[1], &temp_name[1], &temp_name[1], argv[0]);
53+
err = system(command);
54+
55+
remove(tar_ball);
56+
//sprintf(command, "rm -r %s", &temp_name[1]);
57+
//system(command);
58+
free(command);
59+
return 0;
60+
}

src/linker/elfmaker

12.7 KB
Binary file not shown.

src/linker/main.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
#include <elf.h>
6+
7+
int main(int argc, char* argv[]) {
8+
if (argc < 2) {
9+
printf("Input is of format './elfmaker <file to embed> <module file?>'\n");
10+
exit(0);
11+
}
12+
13+
Elf64_Ehdr elf_head;
14+
elf_head.e_ident[EI_MAG0] = (unsigned char)ELFMAG0;
15+
elf_head.e_ident[EI_MAG1] = (unsigned char)ELFMAG1;
16+
elf_head.e_ident[EI_MAG2] = (unsigned char)ELFMAG2;
17+
elf_head.e_ident[EI_MAG3] = (unsigned char)ELFMAG3;
18+
elf_head.e_ident[EI_CLASS] = (unsigned char)ELFCLASS64; /* Always 64 bit ELF FIXME*/
19+
#ifdef MVM_BIGENDIAN
20+
elf_head.e_ident[EI_DATA] = (unsigned char)ELFDATA2MSB;
21+
#else
22+
elf_head.e_ident[EI_DATA] = (unsigned char)ELFDATA2LSB;
23+
#endif
24+
elf_head.e_ident[EI_VERSION] = (unsigned char)EV_CURRENT;
25+
elf_head.e_ident[EI_OSABI] = (unsigned char)ELFOSABI_NONE; /* Always UNIX System V ABI FIXME */
26+
elf_head.e_type = (Elf64_Half)ET_REL; /* Object file type */
27+
elf_head.e_machine = EM_X86_64; /* Architecture FIXME*/
28+
elf_head.e_version = 0x1; /* Object file version */
29+
elf_head.e_entry = 0x0; /* Entry point virtual address */
30+
elf_head.e_phoff = 0; /*Program header table file offset */
31+
elf_head.e_shoff = sizeof(elf_head); /* Section header table file offset */
32+
elf_head.e_flags = 0x0; /* Processor-specific flags */
33+
elf_head.e_ehsize = sizeof(elf_head); /* ELF header size in bytes */
34+
elf_head.e_phentsize = 0; /* Program header table entry size */
35+
elf_head.e_phnum = 0; /* Program header table entry count */
36+
elf_head.e_shentsize = sizeof(Elf64_Shdr); /* Section header table entry size */
37+
elf_head.e_shnum = 5; /* Section header table entry count */
38+
elf_head.e_shstrndx = 4; /* Section header string table index */
39+
40+
Elf64_Shdr null_hdr = {0, SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0};
41+
42+
int i = 1;
43+
char* command = calloc(100, sizeof(char));
44+
sprintf(command, "echo %s > manifest.txt", argv[i]);
45+
system(command);
46+
while (i < argc) {
47+
i++;
48+
sprintf(command, "echo %s >> manifest.txt", argv[i]);
49+
system(command);
50+
}
51+
sprintf(command, "tar -cvf archive.tar -T manifest.txt --transform='s!^.*/!!'");
52+
system(command);
53+
free(command);
54+
55+
FILE* tarball = fopen("archive.tar", "rb");
56+
if (NULL == tarball)
57+
{
58+
printf("Error opening file\n");
59+
exit(0);
60+
}
61+
fseek(tarball, 0L, SEEK_END);
62+
long rodata_size = ftell(tarball);
63+
fseek(tarball, 0L, SEEK_SET);
64+
char* rodata_sgmt = (char*) calloc(rodata_size, sizeof(char));
65+
if (NULL == rodata_sgmt) {
66+
return 0;
67+
}
68+
rodata_size = fread(rodata_sgmt, 1, rodata_size, tarball);
69+
fclose(tarball);
70+
remove("archive.tar");
71+
72+
Elf64_Shdr rodata_hdr;
73+
rodata_hdr.sh_name = 1; /* Section name (string tbl index) */
74+
rodata_hdr.sh_type = SHT_PROGBITS; /* Section type */
75+
rodata_hdr.sh_flags = SHF_ALLOC; /* Section flags */
76+
rodata_hdr.sh_addr = 0; /* Section virtual addr at execution */
77+
rodata_hdr.sh_offset = sizeof(elf_head) + 5*sizeof(Elf64_Shdr); /* Section file offset */
78+
rodata_hdr.sh_size = rodata_size; /* Section size in bytes */
79+
rodata_hdr.sh_link = 0; /* Link to another section */
80+
rodata_hdr.sh_info = 0; /* Additional section information */
81+
rodata_hdr.sh_addralign = 1; /* Section alignment */
82+
rodata_hdr.sh_entsize = 00; /* Entry size if section holds table */
83+
84+
Elf64_Sym null_sym = {0, ELF64_ST_INFO(STB_LOCAL, STT_NOTYPE), ELF64_ST_VISIBILITY(STV_DEFAULT), SHN_UNDEF, 0, 0};
85+
Elf64_Sym first_sym = {0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), ELF64_ST_VISIBILITY(STV_DEFAULT), 1, 0, 0};
86+
Elf64_Sym start_sym = {1, ELF64_ST_INFO(STB_GLOBAL, STT_NOTYPE), ELF64_ST_VISIBILITY(STV_DEFAULT), 1, 0, 0};
87+
Elf64_Sym end_sym = {24, ELF64_ST_INFO(STB_GLOBAL, STT_NOTYPE), ELF64_ST_VISIBILITY(STV_DEFAULT), 1, rodata_hdr.sh_size, 0};
88+
Elf64_Sym size_sym = {45, ELF64_ST_INFO(STB_GLOBAL, STT_NOTYPE), ELF64_ST_VISIBILITY(STV_DEFAULT), SHN_ABS, rodata_hdr.sh_size, 0};
89+
90+
Elf64_Shdr symtab_hdr;
91+
symtab_hdr.sh_name = strlen(".rodata") + 2; /* Section name (string tbl index) */
92+
symtab_hdr.sh_type = SHT_SYMTAB; /* Section type */
93+
symtab_hdr.sh_flags = 0; /* Section flags */
94+
symtab_hdr.sh_addr = 0; /* Section virtual addr at execution */
95+
symtab_hdr.sh_offset = rodata_hdr.sh_offset + rodata_hdr.sh_size; /* Section file offset */
96+
symtab_hdr.sh_size = 5*sizeof(Elf64_Sym); /* Section size in bytes */
97+
symtab_hdr.sh_link = 3; /* Link to another section */
98+
symtab_hdr.sh_info = 2; /* Additional section information */
99+
symtab_hdr.sh_addralign = 8; /* Section alignment */
100+
symtab_hdr.sh_entsize = sizeof(Elf64_Sym); /* Entry size if section holds table */
101+
102+
char* strtab_sgmt = "\0_binary_test_txt_start\0_binary_test_txt_end\0_binary_test_txt_size\0";
103+
104+
Elf64_Shdr strtab_hdr;
105+
strtab_hdr.sh_name = symtab_hdr.sh_name + strlen(".symtab") + 1; /* Section name (string tbl index) */
106+
strtab_hdr.sh_type = SHT_STRTAB; /* Section type */
107+
strtab_hdr.sh_flags = 0; /* Section flags */
108+
strtab_hdr.sh_addr = 0; /* Section virtual addr at execution */
109+
strtab_hdr.sh_offset = symtab_hdr.sh_offset + symtab_hdr.sh_size; /* Section file offset */
110+
strtab_hdr.sh_size = 67; /* Section size in bytes */
111+
strtab_hdr.sh_link = 0; /* Link to another section */
112+
strtab_hdr.sh_info = 0; /* Additional section information */
113+
strtab_hdr.sh_addralign = 1; /* Section alignment */
114+
strtab_hdr.sh_entsize = 0; /* Entry size if section holds table */
115+
116+
char* shstrtab_sgmt = "\0.rodata\0.symtab\0.strtab\0.shstrtab\0";
117+
118+
Elf64_Shdr shstrtab_hdr;
119+
shstrtab_hdr.sh_name = strtab_hdr.sh_name + strlen(".strtab") + 1; /* Section name (string tbl index) */
120+
shstrtab_hdr.sh_type = SHT_STRTAB; /* Section type */
121+
shstrtab_hdr.sh_flags = 0; /* Section flags */
122+
shstrtab_hdr.sh_addr = 0; /* Section virtual addr at execution */
123+
shstrtab_hdr.sh_offset = strtab_hdr.sh_offset + strtab_hdr.sh_size; /* Section file offset */
124+
shstrtab_hdr.sh_size = 34; /* Section size in bytes */
125+
shstrtab_hdr.sh_link = 0; /* Link to another section */
126+
shstrtab_hdr.sh_info = 0; /* Additional section information */
127+
shstrtab_hdr.sh_addralign = 1; /* Section alignment */
128+
shstrtab_hdr.sh_entsize = 0; /* Entry size if section holds table */
129+
130+
FILE* elf_file = fopen(argv[1], "w+");
131+
if (NULL == elf_file) {
132+
printf("Error opening ELF file\n");
133+
exit(0);
134+
}
135+
136+
int err= fwrite(&elf_head, 1, sizeof(elf_head),elf_file);
137+
err = fwrite(&null_hdr, 1, sizeof(null_hdr), elf_file);
138+
err = fwrite(&rodata_hdr, 1, sizeof(rodata_hdr), elf_file);
139+
err = fwrite(&symtab_hdr, 1, sizeof(symtab_hdr), elf_file);
140+
err = fwrite(&strtab_hdr, 1, sizeof(strtab_hdr), elf_file);
141+
err = fwrite(&shstrtab_hdr, 1, sizeof(shstrtab_hdr), elf_file);
142+
err = fwrite(rodata_sgmt, 1, rodata_hdr.sh_size, elf_file);
143+
err = fwrite(&null_sym, 1, sizeof(Elf64_Sym), elf_file);
144+
err = fwrite(&first_sym, 1, sizeof(Elf64_Sym), elf_file);
145+
err = fwrite(&start_sym, 1, sizeof(Elf64_Sym), elf_file);
146+
err = fwrite(&end_sym, 1, sizeof(Elf64_Sym), elf_file);
147+
err = fwrite(&size_sym, 1, sizeof(Elf64_Sym), elf_file);
148+
err = fwrite(strtab_sgmt, 1, strtab_hdr.sh_size, elf_file);
149+
err = fwrite(shstrtab_sgmt, 1, shstrtab_hdr.sh_size, elf_file);
150+
fclose(elf_file);
151+
}

0 commit comments

Comments
 (0)