| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 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 | #if !defined(EM_AARCH64) | 
|  | 30 | #define EM_AARCH64 183 | 
|  | 31 | #endif | 
|  | 32 |  | 
|  | 33 | template <typename Ehdr> | 
|  | 34 | void InitEhdr(Ehdr* ehdr, uint32_t elf_class, uint32_t machine) { | 
|  | 35 | memset(ehdr, 0, sizeof(Ehdr)); | 
|  | 36 | memcpy(&ehdr->e_ident[0], ELFMAG, SELFMAG); | 
|  | 37 | ehdr->e_ident[EI_DATA] = ELFDATA2LSB; | 
|  | 38 | ehdr->e_ident[EI_VERSION] = EV_CURRENT; | 
|  | 39 | ehdr->e_ident[EI_OSABI] = ELFOSABI_SYSV; | 
|  | 40 | ehdr->e_ident[EI_CLASS] = elf_class; | 
|  | 41 | ehdr->e_type = ET_DYN; | 
|  | 42 | ehdr->e_machine = machine; | 
|  | 43 | ehdr->e_version = EV_CURRENT; | 
|  | 44 | ehdr->e_ehsize = sizeof(Ehdr); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | template <typename Ehdr, typename Shdr> | 
|  | 48 | void GenElf(Ehdr* ehdr, int fd) { | 
|  | 49 | uint64_t offset = sizeof(Ehdr); | 
|  | 50 | ehdr->e_shoff = offset; | 
|  | 51 | ehdr->e_shnum = 3; | 
|  | 52 | ehdr->e_shentsize = sizeof(Shdr); | 
|  | 53 | ehdr->e_shstrndx = 2; | 
|  | 54 | TEMP_FAILURE_RETRY(write(fd, ehdr, sizeof(Ehdr))); | 
|  | 55 |  | 
|  | 56 | Shdr shdr; | 
|  | 57 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 58 | shdr.sh_name = 0; | 
|  | 59 | shdr.sh_type = SHT_NULL; | 
|  | 60 | TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr))); | 
|  | 61 | offset += ehdr->e_shentsize; | 
|  | 62 |  | 
|  | 63 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 64 | shdr.sh_type = SHT_PROGBITS; | 
|  | 65 | shdr.sh_name = 11; | 
|  | 66 | shdr.sh_addr = 0x5000; | 
|  | 67 | shdr.sh_offset = 0x5000; | 
|  | 68 | shdr.sh_entsize = 0x100; | 
|  | 69 | shdr.sh_size = 0x800; | 
|  | 70 | TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr))); | 
|  | 71 | offset += ehdr->e_shentsize; | 
|  | 72 |  | 
|  | 73 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 74 | shdr.sh_type = SHT_STRTAB; | 
|  | 75 | shdr.sh_name = 1; | 
|  | 76 | shdr.sh_offset = 0x200; | 
|  | 77 | shdr.sh_size = 24; | 
|  | 78 | TEMP_FAILURE_RETRY(write(fd, &shdr, sizeof(Shdr))); | 
|  | 79 |  | 
|  | 80 | // Write out the name entries information. | 
|  | 81 | lseek(fd, 0x200, SEEK_SET); | 
|  | 82 | std::string name; | 
|  | 83 | TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1)); | 
|  | 84 | name = ".shstrtab"; | 
|  | 85 | TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1)); | 
|  | 86 | name = ".debug_frame"; | 
|  | 87 | TEMP_FAILURE_RETRY(write(fd, name.data(), name.size() + 1)); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | int main() { | 
|  | 91 | int elf32_fd = TEMP_FAILURE_RETRY(open("elf32", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644)); | 
|  | 92 | if (elf32_fd == -1) { | 
|  | 93 | printf("Failed to create elf32: %s\n", strerror(errno)); | 
|  | 94 | return 1; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | int elf64_fd = TEMP_FAILURE_RETRY(open("elf64", O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0644)); | 
|  | 98 | if (elf64_fd == -1) { | 
|  | 99 | printf("Failed to create elf64: %s\n", strerror(errno)); | 
|  | 100 | return 1; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | Elf32_Ehdr ehdr32; | 
|  | 104 | InitEhdr<Elf32_Ehdr>(&ehdr32, ELFCLASS32, EM_ARM); | 
|  | 105 | GenElf<Elf32_Ehdr, Elf32_Shdr>(&ehdr32, elf32_fd); | 
|  | 106 | close(elf32_fd); | 
|  | 107 |  | 
|  | 108 | Elf64_Ehdr ehdr64; | 
|  | 109 | InitEhdr<Elf64_Ehdr>(&ehdr64, ELFCLASS64, EM_AARCH64); | 
|  | 110 | GenElf<Elf64_Ehdr, Elf64_Shdr>(&ehdr64, elf64_fd); | 
|  | 111 | close(elf64_fd); | 
|  | 112 | } |