|
| 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