1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <elf.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <string>
28
29 template <typename Ehdr>
InitEhdr(Ehdr * ehdr,uint32_t elf_class,uint32_t machine)30 void InitEhdr(Ehdr* ehdr, uint32_t elf_class, uint32_t machine) {
31 memset(ehdr, 0, sizeof(Ehdr));
32 memcpy(&ehdr->e_ident[0], ELFMAG, SELFMAG);
33 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
34 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
35 ehdr->e_ident[EI_OSABI] = ELFOSABI_SYSV;
36 ehdr->e_ident[EI_CLASS] = elf_class;
37 ehdr->e_type = ET_DYN;
38 ehdr->e_machine = machine;
39 ehdr->e_version = EV_CURRENT;
40 ehdr->e_ehsize = sizeof(Ehdr);
41 }
42
43 template <typename Ehdr, typename Shdr>
GenElf(Ehdr * ehdr,int fd)44 void GenElf(Ehdr* ehdr, int fd) {
45 uint64_t offset = sizeof(Ehdr);
46 ehdr->e_shoff = offset;
47 ehdr->e_shnum = 3;
48 ehdr->e_shentsize = sizeof(Shdr);
49 ehdr->e_shstrndx = 2;
50 TEMP_FAILURE_RETRY(write(fd, ehdr, sizeof(Ehdr)));
51
52 Shdr shdr;
53 memset(&shdr, 0, sizeof(shdr));
54 shdr.sh_name = 0;
55 shdr.sh_type = SHT_NULL;
56 TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr)));
57 offset += ehdr->e_shentsize;
58
59 memset(&shdr, 0, sizeof(shdr));
60 shdr.sh_type = SHT_PROGBITS;
61 shdr.sh_name = 11;
62 shdr.sh_addr = 0x5000;
63 shdr.sh_offset = 0x5000;
64 shdr.sh_entsize = 0x100;
65 shdr.sh_size = 0x800;
66 TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr)));
67 offset += ehdr->e_shentsize;
68
69 memset(&shdr, 0, sizeof(shdr));
70 shdr.sh_type = SHT_STRTAB;
71 shdr.sh_name = 1;
72 shdr.sh_offset = 0x200;
73 shdr.sh_size = 24;
74 TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr)));
75
76 // Write out the name entries information.
77 lseek(fd, 0x200, SEEK_SET);
78 std::string name;
79 TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1));
80 name = ".shstrtab";
81 TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1));
82 name = ".debug_frame";
83 TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1));
84 }
85
main()86 int main() {
87 int elf32_fd = TEMP_FAILURE_RETRY(open("elf32", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
88 if (elf32_fd == -1) {
89 printf("Failed to create elf32: %s\n", strerror(errno));
90 return 1;
91 }
92
93 int elf64_fd = TEMP_FAILURE_RETRY(open("elf64", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644));
94 if (elf64_fd == -1) {
95 printf("Failed to create elf64: %s\n", strerror(errno));
96 return 1;
97 }
98
99 Elf32_Ehdr ehdr32;
100 InitEhdr<Elf32_Ehdr>(&ehdr32, ELFCLASS32, EM_ARM);
101 GenElf<Elf32_Ehdr, Elf32_Shdr>(&ehdr32, elf32_fd);
102 close(elf32_fd);
103
104 Elf64_Ehdr ehdr64;
105 InitEhdr<Elf64_Ehdr>(&ehdr64, ELFCLASS64, EM_AARCH64);
106 GenElf<Elf64_Ehdr, Elf64_Shdr>(&ehdr64, elf64_fd);
107 close(elf64_fd);
108 }
109