22 const unsigned int SHF_X86_64_LARGE = 0x10000000;
24 const char ident_str[EI_NIDENT] = {
25 ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
26 ELFCLASS64, ELFDATA2LSB,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
32 void AppendToBuffer(std::vector<char>* dest,
const void* src,
size_t count) {
33 const size_t beg = dest->size();
34 dest->resize(beg + count);
35 std::memcpy(dest->data() + beg, src, count);
44 elf_buffer->resize(elf_buffer->size() +
sizeof(Elf64_Ehdr));
48 const size_t array_size = elf_buffer->size() -
sizeof(Elf64_Ehdr);
51 const char comment[] =
"\0GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0\0\0\0\0";
53 const size_t comment_padding = 4;
54 static_assert(
sizeof(comment) == 48,
".comment section has incorrect size");
57 const Elf64_Sym symtab[] = {
68 { 0, ELF64_ST_INFO(STB_LOCAL, STT_NOTYPE), STV_DEFAULT, SHN_UNDEF, 0, 0},
69 { 1, ELF64_ST_INFO(STB_LOCAL, STT_FILE), STV_DEFAULT, SHN_ABS, 0, 0},
70 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 1, 0, 0},
71 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 2, 0, 0},
72 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 3, 0, 0},
73 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 4, 0, 0},
74 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 6, 0, 0},
75 { 0, ELF64_ST_INFO(STB_LOCAL, STT_SECTION), STV_DEFAULT, 5, 0, 0},
76 {10, ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT), STV_DEFAULT, 4, 0, array_size},
78 static_assert(
sizeof(symtab) == 216,
".symtab has incorrect size");
81 const char strtab[] =
"\0arrays.c\0nodes";
82 static_assert(
sizeof(strtab) == 16,
".strtab has incorrect size");
85 const char shstrtab[] =
"\0.symtab\0.strtab\0.shstrtab\0.text\0.data\0.bss\0.lrodata\0.comment\0" 86 ".note.GNU-stack\0\0";
88 const size_t shstrtab_padding = 2;
89 static_assert(
sizeof(shstrtab) == 80,
".shstrtab has incorrect size");
92 Elf64_Ehdr elf_header;
94 const size_t e_shoff =
sizeof(elf_header) + array_size +
sizeof(comment)
95 +
sizeof(symtab) +
sizeof(strtab) +
sizeof(shstrtab);
97 std::memcpy(elf_header.e_ident, ident_str, EI_NIDENT);
98 elf_header.e_type = ET_REL;
99 elf_header.e_machine = EM_X86_64;
100 elf_header.e_version = EV_CURRENT;
101 elf_header.e_entry = 0;
102 elf_header.e_phoff = 0;
103 elf_header.e_shoff = e_shoff;
104 elf_header.e_flags = 0;
105 elf_header.e_ehsize = 64;
106 elf_header.e_phentsize = 0;
107 elf_header.e_phnum = 0;
108 elf_header.e_shentsize = 64;
109 elf_header.e_shnum = 10;
110 elf_header.e_shstrndx = 9;
115 Elf64_Shdr section_header[] = {
135 { 0, SHT_NULL, 0x0, 0x0, 0x0, 0, 0, 0, 0, 0},
136 {27, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0, 0x0, 0, 0, 0, 1, 0},
137 {33, SHT_PROGBITS, SHF_WRITE | SHF_ALLOC, 0x0, 0x0, 0, 0, 0, 1, 0},
138 {39, SHT_NOBITS, SHF_WRITE | SHF_ALLOC, 0x0, 0x0, 0, 0, 0, 1, 0},
139 {44, SHT_PROGBITS, SHF_ALLOC | SHF_X86_64_LARGE, 0x0, 0x0, array_size, 0, 0, 32, 0},
140 {53, SHT_PROGBITS, SHF_MERGE | SHF_STRINGS, 0x0, 0x0,
sizeof(comment), 0, 0, 1, 1},
141 {62, SHT_PROGBITS, 0x0, 0x0, 0x0, 0, 0, 0, 1, 0},
142 { 1, SHT_SYMTAB, 0x0, 0x0, 0x0,
sizeof(symtab), 8, 8, 8, 24},
143 { 9, SHT_STRTAB, 0x0, 0x0, 0x0,
sizeof(strtab), 0, 0, 1, 0},
144 {17, SHT_STRTAB, 0x0, 0x0, 0x0,
sizeof(shstrtab), 0, 0, 1, 0}
150 section_header[1].sh_offset = 0x40;
151 for (
size_t i = 2; i <
sizeof(section_header) /
sizeof(Elf64_Shdr); ++i) {
152 section_header[i].sh_offset = section_header[i - 1].sh_offset + section_header[i - 1].sh_size;
155 section_header[5].sh_size -= comment_padding;
156 section_header[6].sh_offset -= comment_padding;
157 section_header[9].sh_size -= shstrtab_padding;
184 std::memcpy(elf_buffer->data(), &elf_header,
sizeof(Elf64_Ehdr));
188 AppendToBuffer(elf_buffer, comment,
sizeof(comment));
190 AppendToBuffer(elf_buffer, symtab,
sizeof(symtab));
192 AppendToBuffer(elf_buffer, strtab,
sizeof(strtab));
194 AppendToBuffer(elf_buffer, shstrtab,
sizeof(shstrtab));
196 AppendToBuffer(elf_buffer, section_header,
sizeof(section_header));
208 TREELITE_LOG(FATAL) <<
"dump_array_as_elf is not supported in non-Linux OSes";
212 TREELITE_LOG(FATAL) <<
"dump_array_as_elf is not supported in non-Linux OSes";
logging facility for Treelite