blob: 8755f163b8453e920ea349d77348098f79f2c456 [file] [log] [blame]
Christopher Ferris570b76f2017-06-30 17:18:16 -07001/*
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 <signal.h>
21#include <string.h>
22#include <sys/mman.h>
23#include <sys/ptrace.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <string>
28#include <vector>
29
30#include <gtest/gtest.h>
31
32#include "ElfTestUtils.h"
33
34template <typename Ehdr>
35void TestInitEhdr(Ehdr* ehdr, uint32_t elf_class, uint32_t machine_type) {
36 memset(ehdr, 0, sizeof(Ehdr));
37 memcpy(&ehdr->e_ident[0], ELFMAG, SELFMAG);
38 ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
39 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
40 ehdr->e_ident[EI_OSABI] = ELFOSABI_SYSV;
41 ehdr->e_ident[EI_CLASS] = elf_class;
42 ehdr->e_type = ET_DYN;
43 ehdr->e_machine = machine_type;
44 ehdr->e_version = EV_CURRENT;
45 ehdr->e_ehsize = sizeof(Ehdr);
46}
47
48template <typename Ehdr, typename Shdr>
49void TestInitGnuDebugdata(uint32_t elf_class, uint32_t machine, bool init_gnu_debugdata,
50 TestCopyFuncType copy_func) {
51 Ehdr ehdr;
52
53 TestInitEhdr(&ehdr, elf_class, machine);
54
55 uint64_t offset = sizeof(Ehdr);
56 ehdr.e_shoff = offset;
57 ehdr.e_shnum = 3;
58 ehdr.e_shentsize = sizeof(Shdr);
59 ehdr.e_shstrndx = 2;
60 copy_func(0, &ehdr, sizeof(ehdr));
61
62 Shdr shdr;
63 memset(&shdr, 0, sizeof(shdr));
64 shdr.sh_type = SHT_NULL;
65 copy_func(offset, &shdr, sizeof(shdr));
66 offset += ehdr.e_shentsize;
67
68 // Skip this header, it will contain the gnu_debugdata information.
69 uint64_t gnu_offset = offset;
70 offset += ehdr.e_shentsize;
71
72 uint64_t symtab_offset = sizeof(ehdr) + ehdr.e_shnum * ehdr.e_shentsize;
73 memset(&shdr, 0, sizeof(shdr));
74 shdr.sh_name = 1;
75 shdr.sh_type = SHT_STRTAB;
76 shdr.sh_offset = symtab_offset;
77 shdr.sh_size = 0x100;
78 copy_func(offset, &shdr, sizeof(shdr));
79
80 char value = '\0';
81 uint64_t symname_offset = symtab_offset;
82 copy_func(symname_offset, &value, 1);
83 symname_offset++;
84 std::string name(".shstrtab");
85 copy_func(symname_offset, name.c_str(), name.size() + 1);
86 symname_offset += name.size() + 1;
87 name = ".gnu_debugdata";
88 copy_func(symname_offset, name.c_str(), name.size() + 1);
89
90 ssize_t bytes = 0x100;
91 offset = symtab_offset + 0x100;
92 if (init_gnu_debugdata) {
93 // Read in the compressed elf data and copy it in.
94 name = "tests/files/";
95 if (elf_class == ELFCLASS32) {
96 name += "elf32.xz";
97 } else {
98 name += "elf64.xz";
99 }
100 int fd = TEMP_FAILURE_RETRY(open(name.c_str(), O_RDONLY));
101 ASSERT_NE(-1, fd) << "Cannot open " + name;
102 // Assumes the file is less than 1024 bytes.
103 std::vector<uint8_t> buf(1024);
104 bytes = TEMP_FAILURE_RETRY(read(fd, buf.data(), buf.size()));
105 ASSERT_GT(bytes, 0);
106 // Make sure the file isn't too big.
107 ASSERT_NE(static_cast<size_t>(bytes), buf.size())
108 << "File " + name + " is too big, increase buffer size.";
109 close(fd);
110 buf.resize(bytes);
111 copy_func(offset, buf.data(), buf.size());
112 }
113
114 memset(&shdr, 0, sizeof(shdr));
115 shdr.sh_type = SHT_PROGBITS;
116 shdr.sh_name = symname_offset - symtab_offset;
117 shdr.sh_addr = offset;
118 shdr.sh_offset = offset;
119 shdr.sh_size = bytes;
120 copy_func(gnu_offset, &shdr, sizeof(shdr));
121}
122
123template void TestInitEhdr<Elf32_Ehdr>(Elf32_Ehdr*, uint32_t, uint32_t);
124template void TestInitEhdr<Elf64_Ehdr>(Elf64_Ehdr*, uint32_t, uint32_t);
125
126template void TestInitGnuDebugdata<Elf32_Ehdr, Elf32_Shdr>(uint32_t, uint32_t, bool,
127 TestCopyFuncType);
128template void TestInitGnuDebugdata<Elf64_Ehdr, Elf64_Shdr>(uint32_t, uint32_t, bool,
129 TestCopyFuncType);